Ejemplo n.º 1
0
bool PViewData::toVector(std::vector<std::vector<double> > &vec)
{
  vec.resize(getNumTimeSteps());
  for(int step = 0; step < getNumTimeSteps(); step++){
    vec[step].clear();
    for(int ent = 0; ent < getNumEntities(step); ent++){
      for(int ele = 0; ele < getNumElements(step, ent); ele++){
        if(skipElement(step, ent, ele)) continue;
        for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++){
          for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++){
            double val;
            getValue(step, ent, ele, nod, comp, val);
            vec[step].push_back(val);
          }
        }
      }
    }
  }
  return true;
}
Ejemplo n.º 2
0
bool PViewData::fromVector(const std::vector<std::vector<double> > &vec)
{
  if(empty() || !getNumTimeSteps()){
    Msg::Warning("Cannot import vector in an empty view; skipping");
    return false;
  }

  if((int)vec.size() != getNumTimeSteps()){
    Msg::Error("Incompatible number of steps in vector for view import (%d!=%d)",
               (int)vec.size(), getNumTimeSteps());
    return false;
  }

  for(int step = 0; step < getNumTimeSteps(); step++){
    int i = 0;
    for(int ent = 0; ent < getNumEntities(step); ent++){
      for(int ele = 0; ele < getNumElements(step, ent); ele++){
        if(skipElement(step, ent, ele)) continue;
        for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++){
          double x, y, z;
          getNode(step, ent, ele, nod, x, y, z);
          for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++){
            if(i < (int)vec[step].size()){
              setValue(step, ent, ele, nod, comp, vec[step][i++]);
            }
            else{
              Msg::Error("Bad index (%d) in vector (%d) for view import",
                         i, (int)vec[step].size());
              return false;
            }
          }
        }
      }
    }
  }
  return true;
}
Ejemplo n.º 3
0
//------------------------------------------------------------------------------
// Write a bitmap (BMP) format file
//------------------------------------------------------------------------------
bool Image::writeFileBMP(const char* const filename, const char* const path)
{
   static const unsigned int BITMAPFILE_SIZE = (MAX_PATH_LEN+MAX_FILENAME_LEN);
   char bitmapFile[BITMAPFILE_SIZE];
   bitmapFile[0] = '\0';

   // append path name
   const char* p1 = path;
   if (p1 != nullptr && std::strlen(path) > 0) {
      base::utStrcat(bitmapFile, sizeof(bitmapFile), p1);
      base::utStrcat(bitmapFile, sizeof(bitmapFile), "/");
   }

   // append file name
   const char* p2 = filename;
   if (p2 != nullptr && std::strlen(p2) > 0) {
      base::utStrcat(bitmapFile, sizeof(bitmapFile), p2);
   }

   // Do we have a full path name?
   if (std::strlen(bitmapFile) <= 1) {
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::writeFileBMP(): invalid file name: " << bitmapFile << std::endl;
      }
      return false;
   }

   // Create the output stream
   auto fout = new std::ofstream();

   // Open the output file
   fout->open(bitmapFile, std::ios::out | std::ios::binary);
   if ( !(fout->is_open()) ) {
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::writeFileBMP(): unable to open bitmap file: " << bitmapFile << std::endl;
      }
      return false;
   }


   // BITMAPFILEHEADER
   unsigned short bfType(0);
   unsigned int   bfSize(0);
   unsigned short bfReserved1(0);
   unsigned short bfReserved2(0);
   unsigned int   bfOffBits(0);

   unsigned int bitmapFileHdrSize =
      sizeof(bfType) + sizeof(bfSize) + sizeof(bfReserved1) + sizeof(bfReserved2) + sizeof(bfOffBits);

   // Number of bytes per row of pixels
   size_t widthBytes = getWidth() * getNumComponents();

   // Offset to bitmap data
   unsigned int offset = bitmapFileHdrSize + sizeof(BITMAPINFOHEADER_X);

   // Image size
   unsigned int isize = getHeight() * static_cast<unsigned int>(widthBytes);

   // File size (active bytes)
   unsigned int size = isize + offset;

   // File size (4 byte words)
   unsigned int sizew = (size + 3)/4;

   // Total file size (with padding)
   unsigned int tsize = sizew*4;

   // Number of padding bytes at the end if the file to give a even word boundary
   unsigned int filePadding = tsize - size;

   // ---
   // Write the bitmap file header (BITMAPFILEHEADER)
   // ---
   bfType = 0x4D42;
   bfSize = tsize;
   bfReserved1 = 0;
   bfReserved2 = 0;
   bfOffBits = offset;

   fout->write(reinterpret_cast<char*>(&bfType),      sizeof(bfType));
   fout->write(reinterpret_cast<char*>(&bfSize),      sizeof(bfSize));
   fout->write(reinterpret_cast<char*>(&bfReserved1), sizeof(bfReserved1));
   fout->write(reinterpret_cast<char*>(&bfReserved2), sizeof(bfReserved2));
   fout->write(reinterpret_cast<char*>(&bfOffBits),   sizeof(bfOffBits));

   // ---
   // Write the bitmap file info
   // ---
   BITMAPINFOHEADER_X bmfi;
   bmfi.biSize = sizeof(BITMAPINFOHEADER_X);
   bmfi.biWidth = getWidth();
   bmfi.biHeight = getHeight();
   bmfi.biPlanes = 1;
   bmfi.biBitCount = getNumComponents() * 8;
   bmfi.biCompression = BI_RGB;
   bmfi.biSizeImage = 0;
   bmfi.biXPelsPerMeter = getXResolutionPPM();
   bmfi.biYPelsPerMeter = getYResolutionPPM();
   bmfi.biClrUsed = 0;
   bmfi.biClrImportant = 0;
   fout->write(reinterpret_cast<char*>(&bmfi), sizeof(bmfi));

   // ---
   // Write the pixel bit map
   // ---
   {
      const GLubyte* bmap = getPixels();
      for (unsigned int i = 0; i < getHeight(); i++) {
         const GLubyte* p = bmap + (i * widthBytes);
         fout->write(reinterpret_cast<const char*>(p), width* PIXEL_SIZE);
      }
      if (filePadding > 0) {
         unsigned int padding = 0;
         fout->write(reinterpret_cast<char*>(&padding), filePadding);
      }
   }

   // close the file
   fout->close();
   delete fout;
   fout = nullptr;

   return true;
}
Ejemplo n.º 4
0
bool PViewData::writePOS(const std::string &fileName, bool binary, bool parsed,
                         bool append)
{
  if(_adaptive){
    Msg::Warning("Writing adapted dataset (will only export current time step)");
    return _adaptive->getData()->writePOS(fileName, binary, parsed, append);
  }
  if(hasMultipleMeshes()){
    Msg::Error("Cannot export multi-mesh datasets in .pos format");
    return false;
  }
  if(haveInterpolationMatrices())
    Msg::Warning("Discarding interpolation matrices when saving in .pos format");
  if(binary || !parsed)
    Msg::Warning("Only parsed .pos files can be exported for this view type");

  FILE *fp = Fopen(fileName.c_str(), append ? "a" : "w");
  if(!fp){
    Msg::Error("Unable to open file '%s'", fileName.c_str());
    return false;
  }

  fprintf(fp, "View \"%s\" {\n", getName().c_str());

  int firstNonEmptyStep = getFirstNonEmptyTimeStep();
  for(int ent = 0; ent < getNumEntities(firstNonEmptyStep); ent++){
    for(int ele = 0; ele < getNumElements(firstNonEmptyStep, ent); ele++){
      if(skipElement(firstNonEmptyStep, ent, ele)) continue;
      int type = getType(firstNonEmptyStep, ent, ele);
      int numComp = getNumComponents(firstNonEmptyStep, ent, ele);
      const char *s = 0;
      switch(type){
      case TYPE_PNT: s = (numComp == 9) ? "TP" : (numComp == 3) ? "VP" : "SP"; break;
      case TYPE_LIN: s = (numComp == 9) ? "TL" : (numComp == 3) ? "VL" : "SL"; break;
      case TYPE_TRI: s = (numComp == 9) ? "TT" : (numComp == 3) ? "VT" : "ST"; break;
      case TYPE_QUA: s = (numComp == 9) ? "TQ" : (numComp == 3) ? "VQ" : "SQ"; break;
      case TYPE_TET: s = (numComp == 9) ? "TS" : (numComp == 3) ? "VS" : "SS"; break;
      case TYPE_HEX: s = (numComp == 9) ? "TH" : (numComp == 3) ? "VH" : "SH"; break;
      case TYPE_PRI: s = (numComp == 9) ? "TI" : (numComp == 3) ? "VI" : "SI"; break;
      case TYPE_PYR: s = (numComp == 9) ? "TY" : (numComp == 3) ? "VY" : "SY"; break;
      }
      if(s){
        fprintf(fp, "%s(", s);
        int numNod = getNumNodes(firstNonEmptyStep, ent, ele);
        for(int nod = 0; nod < numNod; nod++){
          double x, y, z;
          getNode(firstNonEmptyStep, ent, ele, nod, x, y, z);
          fprintf(fp, "%.16g,%.16g,%.16g", x, y, z);
          if(nod != numNod - 1) fprintf(fp, ",");
        }
        bool first = true;
        for(int step = 0; step < getNumTimeSteps(); step++){
          if(hasTimeStep(step)){
            for(int nod = 0; nod < numNod; nod++){
              for(int comp = 0; comp < numComp; comp++){
                double val;
                getValue(step, ent, ele, nod, comp, val);
                if(first){ fprintf(fp, "){%.16g", val); first = false; }
                else fprintf(fp, ",%.16g", val);
              }
            }
          }
        }
        fprintf(fp, "};\n");
      }
    }
  }

  fprintf(fp, "};\n");
  fclose(fp);

  return true;
}
Ejemplo n.º 5
0
void QSofaLibrary::build(const std::vector< std::string >& examples)
{
    SofaLibrary::build(examples);
    this->setItemLabel(0,QString("Sofa Components [") + QString::number(getNumComponents()) + QString("]"));
}