コード例 #1
0
ファイル: GModelIO_PLY.cpp プロジェクト: feelpp/debian-gmsh
int GModel::readPLY(const std::string &name)
{
  // this is crazy!?
  replaceCommaByDot(name);

  FILE *fp = Fopen(name.c_str(), "r");
  if(!fp){
    Msg::Error("Unable to open file '%s'", name.c_str());
    return 0;
  }

  std::vector<MVertex*> vertexVector;
  std::map<int, std::vector<MElement*> > elements[5];
  std::map<int, std::vector<double> > properties;

  char buffer[256], str[256], str2[256], str3[256];
  std::string s1;
  int elementary = getMaxElementaryNumber(-1) + 1;
  int nbv = 0, nbf = 0;
  int nbprop = 0;
  int nbView = 0;
  std::vector<std::string> propName;
  while(!feof(fp)) {
    if(!fgets(buffer, sizeof(buffer), fp)) break;
    if(buffer[0] != '#'){ // skip comments
      sscanf(buffer, "%s %s", str, str2);
      if(!strcmp(str, "element") && !strcmp(str2, "vertex")){
	sscanf(buffer, "%s %s %d", str, str2, &nbv);
      }
      if(!strcmp(str, "format") && strcmp(str2, "ascii")){
	Msg::Error("Only reading of ascii PLY files implemented");
        fclose(fp);
	return 0;
      }
      if(!strcmp(str, "property") && strcmp(str2, "list")){
	nbprop++;
	sscanf(buffer, "%s %s %s", str, str2, str3);
	if (nbprop > 3) propName.push_back(s1+str3);
      }
      if(!strcmp(str, "element") && !strcmp(str2, "face")){
	sscanf(buffer, "%s %s %d", str, str2, &nbf);
      }
      if(!strcmp(str, "end_header")){
	nbView = nbprop -3;
	Msg::Info("%d elements", nbv);
	Msg::Info("%d triangles", nbf);
	Msg::Info("%d properties", nbView);

	vertexVector.resize(nbv);
	for(int i = 0; i < nbv; i++) {
	  double x,y,z;
	  char line[10000], *pEnd, *pEnd2, *pEnd3;
	  if(!fgets(line, sizeof(line), fp)){ fclose(fp); return 0; }
	  x = strtod(line, &pEnd);
	  y = strtod(pEnd, &pEnd2);
	  z = strtod(pEnd2, &pEnd3);
	  vertexVector[i] = new MVertex(x, y, z);

	  pEnd = pEnd3;
          std::vector<double> prop(nbView);
	  for (int k = 0; k < nbView; k++){
	    prop[k]=strtod(pEnd, &pEnd2);
	    pEnd = pEnd2;
	    properties[k].push_back(prop[k]);
	  }
	}

	for(int i = 0; i < nbf; i++) {
	  if(!fgets(buffer, sizeof(buffer), fp)) break;
	  int n[3], nbe;
	  sscanf(buffer, "%d %d %d %d", &nbe, &n[0], &n[1], &n[2]);
	  std::vector<MVertex*> vertices;
	  if(!getVertices(3, n, vertexVector, vertices)){ fclose(fp); return 0; }
	  elements[0][elementary].push_back(new MTriangle(vertices));
	}

      }

    }
  }

  for(int i = 0; i < (int)(sizeof(elements) / sizeof(elements[0])); i++)
    _storeElementsInEntities(elements[i]);
  _associateEntityWithMeshVertices();
  _storeVerticesInEntities(vertexVector);

#if defined(HAVE_POST)
  // create PViews here
  std::vector<GEntity*> _entities;
  getEntities(_entities);
  for (int iV=0; iV< nbView; iV++){
    PView *view = new PView();
    PViewDataList *data = dynamic_cast<PViewDataList*>(view->getData());
    for(unsigned int ii = 0; ii < _entities.size(); ii++){
	for(unsigned int i = 0; i < _entities[ii]->getNumMeshElements(); i++){
	  MElement *e = _entities[ii]->getMeshElement(i);
	  int numNodes = e->getNumVertices();
	  std::vector<double> x(numNodes), y(numNodes), z(numNodes);
	  std::vector<double> *out = data->incrementList(1, e->getType());
	  for(int nod = 0; nod < numNodes; nod++) out->push_back((e->getVertex(nod))->x());
	  for(int nod = 0; nod < numNodes; nod++) out->push_back((e->getVertex(nod))->y());
	  for(int nod = 0; nod < numNodes; nod++) out->push_back((e->getVertex(nod))->z());
	  std::vector<double> props;
	  int n[3];
	  n[0] = e->getVertex(0)->getNum()-1;
	  n[1] = e->getVertex(1)->getNum()-1;
	  n[2] = e->getVertex(2)->getNum()-1;
	  if(!getProperties(3, n, properties[iV], props)){ fclose(fp); return 0; }
	  for(int nod = 0; nod < numNodes; nod++) out->push_back(props[nod]);
	}
    }
    data->setName(propName[iV]);
    data->Time.push_back(0);
    data->setFileName("property.pos");
    data->finalize();
  }
#endif

  fclose(fp);

  return 1;
}
コード例 #2
0
ファイル: Eigenvectors.cpp プロジェクト: feelpp/debian-gmsh
PView *GMSH_EigenvectorsPlugin::execute(PView *v)
{
  int scale = (int)EigenvectorsOptions_Number[0].def;
  int iView = (int)EigenvectorsOptions_Number[1].def;

  PView *v1 = getView(iView, v);
  if(!v1) return v;

  PViewData *data1 = getPossiblyAdaptiveData(v1);
  if(data1->hasMultipleMeshes()){
    Msg::Error("Eigenvectors plugin cannot be run on multi-mesh views");
    return v;
  }

  PView *min = new PView();
  PView *mid = new PView();
  PView *max = new PView();

  PViewDataList *dmin = getDataList(min);
  PViewDataList *dmid = getDataList(mid);
  PViewDataList *dmax = getDataList(max);

  int nbcomplex = 0;
  fullMatrix<double> mat(3, 3), vl(3, 3), vr(3, 3);
  fullVector<double> dr(3), di(3);
  for(int ent = 0; ent < data1->getNumEntities(0); ent++){
    for(int ele = 0; ele < data1->getNumElements(0, ent); ele++){
      if(data1->skipElement(0, ent, ele)) continue;
      int numComp = data1->getNumComponents(0, ent, ele);
      if(numComp != 9) continue;
      int type = data1->getType(0, ent, ele);
      int numNodes = data1->getNumNodes(0, ent, ele);
      std::vector<double> *outmin = dmin->incrementList(3, type, numNodes);
      std::vector<double> *outmid = dmid->incrementList(3, type, numNodes);
      std::vector<double> *outmax = dmax->incrementList(3, type, numNodes);
      if(!outmin || !outmid || !outmax) continue;
      double xyz[3][8];
      for(int nod = 0; nod < numNodes; nod++)
        data1->getNode(0, ent, ele, nod, xyz[0][nod], xyz[1][nod], xyz[2][nod]);
      for(int i = 0; i < 3; i++){
        for(int nod = 0; nod < numNodes; nod++){
          outmin->push_back(xyz[i][nod]);
          outmid->push_back(xyz[i][nod]);
          outmax->push_back(xyz[i][nod]);
        }
      }
      for(int step = 0; step < data1->getNumTimeSteps(); step++){
        for(int nod = 0; nod < numNodes; nod++){
          for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
              data1->getValue(step, ent, ele, nod, 3 * i + j, mat(i, j));
          if(mat.eig(dr, di, vl, vr, true)){
            if(!scale) dr(0) = dr(1) = dr(2) = 1.;
            for(int i = 0; i < 3; i++){
              double res;
              res = dr(0) * vr(i, 0); outmin->push_back(res);
              res = dr(1) * vr(i, 1); outmid->push_back(res);
              res = dr(2) * vr(i, 2); outmax->push_back(res);
            }
            if(di(0) || di(1) || di(2)) nbcomplex++;
          }
          else{
            Msg::Error("Could not compute eigenvalues/vectors");
          }
        }
      }
    }
  }

  if(nbcomplex)
    Msg::Error("%d tensors have complex eigenvalues", nbcomplex);
  
  for(int i = 0; i < data1->getNumTimeSteps(); i++){
    double time = data1->getTime(i);
    dmin->Time.push_back(time);
    dmid->Time.push_back(time);
    dmax->Time.push_back(time);
  }
  dmin->setName(data1->getName() + "_MinEigenvectors");
  dmin->setFileName(data1->getName() + "_MinEigenvectors.pos");
  dmin->finalize();
  dmid->setName(data1->getName() + "_MidEigenvectors");
  dmid->setFileName(data1->getName() + "_MidEigenvectors.pos");
  dmid->finalize();
  dmax->setName(data1->getName() + "_MaxEigenvectors");
  dmax->setFileName(data1->getName() + "_MaxEigenvectors.pos");
  dmax->finalize();

  return 0;
}
コード例 #3
0
ファイル: Scal2Vec.cpp プロジェクト: iyer-arvind/gmsh
PView *GMSH_Scal2VecPlugin::execute(PView *v)
{
  // Load options
  int iView[3];
  for (int comp=0; comp<3; comp++)
    iView[comp] = (int)Scal2VecOptions_Number[comp].def;
  
  // Load data
  PView *vRef=0, *vComp[3];
  for (int comp=0; comp<3; comp++) {
    if (iView[comp]<0) vComp[comp] = 0;
    else {
      vComp[comp] = getView(iView[comp], v);
      if (!vComp[comp]) {
        Msg::Error("Scal2Vec plugin could not find View '%i'", iView[comp]);
        return v;
      }
      if (!vRef) vRef = vComp[comp];
    }
  }
  if (!vRef) {
    Msg::Error("Scal2Vec plugin could not find any view.");
    return v;
  }
  PViewData *dataRef = vRef->getData();
  
  // Initialize the new view
  PView *vNew = new PView();
  PViewDataList *dataNew = getDataList(vNew);
  
  int step0 = dataRef->getFirstNonEmptyTimeStep();
  for (int ent=0; ent < dataRef->getNumEntities(step0); ent++) {
    for (int ele=0; ele < dataRef->getNumElements(step0, ent); ele++) {
      if (dataRef->skipElement(step0, ent, ele)) continue;
      int type = dataRef->getType(step0, ent, ele);
      int numNodes = dataRef->getNumNodes(step0, ent, ele);
      std::vector<double> *out = dataNew->incrementList(3, type, numNodes); // Pointer in data of the new view
      if (!out) continue;
      double x[8], y[8], z[8];
      for (int nod=0; nod<numNodes; nod++)
        dataRef->getNode(step0, ent, ele, nod, x[nod], y[nod], z[nod]);
      int dim = dataRef->getDimension(step0, ent, ele);
      elementFactory factory;
      element *element = factory.create(numNodes, dim, x, y, z);
      if (!element) continue;
      for (int nod=0; nod<numNodes; nod++) out->push_back(x[nod]); // Save coordinates (x,y,z)
      for (int nod=0; nod<numNodes; nod++) out->push_back(y[nod]);
      for (int nod=0; nod<numNodes; nod++) out->push_back(z[nod]);
      for (int step=step0; step < dataRef->getNumTimeSteps(); step++) {
        if (!dataRef->hasTimeStep(step)) continue;
        for (int nod=0; nod<numNodes; nod++) {
          for (int comp=0; comp<3; comp++) {
            double val=0.;
            if(vComp[comp]) vComp[comp]->getData()->getValue(step, ent, ele, nod, 0, val);
            out->push_back(val); // Save value
          }
        }
      }
      delete element;
    }
  }
  
  for (int step=step0; step < dataRef->getNumTimeSteps(); step++) {
    if (!dataRef->hasTimeStep(step)) continue;
    dataNew->Time.push_back(dataRef->getTime(step));
  }
  
  std::string nameNewView = Scal2VecOptions_String[0].def;
  dataNew->setName(nameNewView);
  dataNew->setFileName(nameNewView + ".pos");
  dataNew->finalize();
  
  return vNew;
}
コード例 #4
0
ファイル: Scal2Vec.cpp プロジェクト: cycheung/gmsh
PView *GMSH_Scal2VecPlugin::execute(PView *v)
{
  int iViewX = (int)Scal2VecOptions_Number[0].def;
  int iViewY = (int)Scal2VecOptions_Number[1].def;
  int iViewZ = (int)Scal2VecOptions_Number[2].def;
  std::string nameNewView = Scal2VecOptions_String[0].def;
  
  PView *vRef = 0, *vX = 0, *vY = 0, *vZ = 0;
  PViewData *dataRef = 0, *dataX = 0, *dataY = 0, *dataZ = 0;
  
  // Load data
  if(iViewX >= 0){
    vX = getView(iViewX, v);
    if(!vX){
      Msg::Error("Scal2Vec plugin could not find View X: %i", iViewX);
      return v;
    }
    dataX = vX->getData();
    if(!vRef){
      vRef = vX;
      dataRef = dataX;
    }
  }
  if(iViewY >= 0){
    vY = getView(iViewY, v);
    if(!vY){
      Msg::Error("Scal2Vec plugin could not find View Y: %i", iViewY);
      return v;
    }
    dataY = vY->getData();
    if(!vRef){
      vRef = vY;
      dataRef = dataY;
    }
  }
  if(iViewZ >= 0){
    vZ = getView(iViewZ, v);
    if(!vZ){
      Msg::Error("Scal2Vec plugin could not find View Z: %i", iViewZ);
      return v;
    }
    dataZ = vZ->getData();
    if(!vRef){
      vRef = vZ;
      dataRef = dataZ;
    }
  }
  if(!vRef){
    Msg::Error("Scal2Vec plugin could not find any view.", iViewZ);
    return v;
  }
  
  // Initialize the new view
  PView *vNew = new PView();
  PViewDataList *dataNew = getDataList(vNew);
  
  for(int ent = 0; ent < dataRef->getNumEntities(0); ent++){
    for(int ele = 0; ele < dataRef->getNumElements(0, ent); ele++){
      if(dataRef->skipElement(0, ent, ele)) continue;
      int numComp = 3; // The 3 components of the new view: x,y,z
      int type = dataRef->getType(0, ent, ele);
      int numNodes = dataRef->getNumNodes(0, ent, ele);
      std::vector<double> *out = dataNew->incrementList(numComp, type, numNodes); // Pointer in data of the new view
      if(!out) continue;
      double x[8], y[8], z[8], valX, valY, valZ;
      for(int nod = 0; nod < numNodes; nod++)
        dataRef->getNode(0, ent, ele, nod, x[nod], y[nod], z[nod]);
      int dim = dataRef->getDimension(0, ent, ele);
      elementFactory factory;
      element *element = factory.create(numNodes, dim, x, y, z);
      if(!element) continue;
      for(int nod = 0; nod < numNodes; nod++) out->push_back(x[nod]); // Save coordinates (x,y,z)
      for(int nod = 0; nod < numNodes; nod++) out->push_back(y[nod]);
      for(int nod = 0; nod < numNodes; nod++) out->push_back(z[nod]);
      for(int step = 0; step < dataRef->getNumTimeSteps(); step++){
        for(int nod = 0; nod < numNodes; nod++){
          if(vX) dataX->getValue(step, ent, ele, nod, 0, valX); else valX = 0;
          if(vY) dataY->getValue(step, ent, ele, nod, 0, valY); else valY = 0;
          if(vZ) dataZ->getValue(step, ent, ele, nod, 0, valZ); else valZ = 0;
          out->push_back(valX); // Save values (fx,fy,fz)
          out->push_back(valY);
          out->push_back(valZ);
        }
      }
      delete element;
    }
  }
  
  for(int step = 0; step < dataRef->getNumTimeSteps(); step++){
    if(dataRef->hasTimeStep(step)){
      double time = dataRef->getTime(step);
      dataNew->Time.push_back(time);
    }
  }
  
  dataNew->setName(nameNewView);
  dataNew->setFileName(nameNewView + ".pos");
  dataNew->finalize();
  
  return vNew;
}
コード例 #5
0
ファイル: Eigenvalues.cpp プロジェクト: iyer-arvind/gmsh
PView *GMSH_EigenvaluesPlugin::execute(PView *v)
{
  int iView = (int)EigenvaluesOptions_Number[0].def;

  PView *v1 = getView(iView, v);
  if(!v1) return v;

  PViewData *data1 = getPossiblyAdaptiveData(v1);
  if(data1->hasMultipleMeshes()){
    Msg::Error("Eigenvalues plugin cannot be run on multi-mesh views");
    return v;
  }

  PView *min = new PView();
  PView *mid = new PView();
  PView *max = new PView();

  PViewDataList *dmin = getDataList(min);
  PViewDataList *dmid = getDataList(mid);
  PViewDataList *dmax = getDataList(max);

  for(int ent = 0; ent < data1->getNumEntities(0); ent++){
    for(int ele = 0; ele < data1->getNumElements(0, ent); ele++){
      if(data1->skipElement(0, ent, ele)) continue;
      int numComp = data1->getNumComponents(0, ent, ele);
      if(numComp != 9) continue;
      int type = data1->getType(0, ent, ele);
      int numNodes = data1->getNumNodes(0, ent, ele);
      std::vector<double> *outmin = dmin->incrementList(1, type, numNodes);
      std::vector<double> *outmid = dmid->incrementList(1, type, numNodes);
      std::vector<double> *outmax = dmax->incrementList(1, type, numNodes);
      if(!outmin || !outmid || !outmax) continue;
      double xyz[3][8];
      for(int nod = 0; nod < numNodes; nod++)
        data1->getNode(0, ent, ele, nod, xyz[0][nod], xyz[1][nod], xyz[2][nod]);
      for(int i = 0; i < 3; i++){
        for(int nod = 0; nod < numNodes; nod++){
          outmin->push_back(xyz[i][nod]);
          outmid->push_back(xyz[i][nod]);
          outmax->push_back(xyz[i][nod]);
        }
      }
      for(int step = 0; step < data1->getNumTimeSteps(); step++){
        for(int nod = 0; nod < numNodes; nod++){
          double val[9], w[3];
          for(int comp = 0; comp < numComp; comp++)
            data1->getValue(step, ent, ele, nod, comp, val[comp]);
          double A[3][3] = {{val[0], val[1], val[2]},
                            {val[3], val[4], val[5]},
                            {val[6], val[7], val[8]}};
          eigenvalue(A, w);
          outmin->push_back(w[2]);
          outmid->push_back(w[1]);
          outmax->push_back(w[0]);
        }
      }
    }
  }
  
  for(int i = 0; i < data1->getNumTimeSteps(); i++){
    double time = data1->getTime(i);
    dmin->Time.push_back(time);
    dmid->Time.push_back(time);
    dmax->Time.push_back(time);
  }
  dmin->setName(data1->getName() + "_MinEigenvalues");
  dmin->setFileName(data1->getName() + "_MinEigenvalues.pos");
  dmin->finalize();
  dmid->setName(data1->getName() + "_MidEigenvalues");
  dmid->setFileName(data1->getName() + "_MidEigenvalues.pos");
  dmid->finalize();
  dmax->setName(data1->getName() + "_MaxEigenvalues");
  dmax->setFileName(data1->getName() + "_MaxEigenvalues.pos");
  dmax->finalize();

  return 0;
}