Re: Surface_Mesh_Deformation help

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Surface_Mesh_Deformation help

Sebastien Loriot (GeometryFactory)
The index used to refer to vertices is the iteration order of the
vertices. So if no modification was done before saving a ROI or a
set of control points this order should be the same when reading
the same file in your program.

HTH

Sebastien.

On 02/19/2018 10:56 PM, gligart wrote:

> Hello there, I'm having troubles with surface mesh deformation in CGAL.
> I explain you in details what's the matter and then I'll show you the code.
> My aim is to deform a mesh surface loading a pre-saved ROI/ControlVertices
> file where all the informations about the vertices are stored, the file you
> could extract from using the Polyhedron Demo from the menu:
> Operations->SurfaceMeshDeformation->Display and IO (TAB) and saving a file
> named ROI.txt, extracting then by hand PlaneROI.txt and PlaneCV.txt simply
> copying the corresponding lists of vertices from the original file) and then
> I have wrote a loop where I feed the algorithm with several affine matrices
> to create the new deformed meshes. If I load the ROI.txt file in the GUI of
> the Polyhedron DEMO it shows that the selection is right, but if I then use
> that file in the algorithm to choose which vertices to handle as control
> vertices and which as part of the ROI it seems that the vertices are not
> indexed as they were when creating the file for the selection, and so what I
> have as a result is a messy selection of vertices deformed. Here I show you
> two images to show you what I'm talking about:
>
> Selection file Loaded in the Polyhedron Demo
> https://ibb.co/eBWWwS <https://ibb.co/eBWWwS>
> Deformed Mesh Result
> https://ibb.co/cnYfO7 <https://ibb.co/cnYfO7>
>
> The Deformation Matrix used for the deformation is as follow:
>
> //////////
> # 4x4 handle transformation matrix
> 1 0 0 0
> 0 0.984808 0.173648 0
> 0 -0.173648 0.984808 0
> 0 0 0 1
> ////////
>
> And here I show you the same procedure applied to a simpler object, which
> actually works:
>
> Selection of the plane
> https://ibb.co/kNqYbS <https://ibb.co/kNqYbS>
> Deformed Mesh Result
> https://ibb.co/gB2RVn <https://ibb.co/gB2RVn>
>
> The *.cpp I'm running is based on deform_mesh_for_botsch08_format.cpp
> contained in the demo folder of the Surface Mesh Deformation package, and
> here there is the additional code I've used (which works for the plane nut
> not for the first object):
>
> typedef CGAL::Simple_cartesian<double>   Kernel;
> typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_with_id_3>
> Polyhedron;
> typedef boost::graph_traits<Polyhedron>::vertex_descriptor
> vertex_descriptor;
> typedef boost::graph_traits<Polyhedron>::vertex_iterator
> vertex_iterator;
> typedef CGAL::Surface_mesh_deformation<Polyhedron,CGAL::Default,
> CGAL::Default, CGAL::SRE_ARAP> Surface_mesh_deformation;
>
> int main(int argc,char** argv)
> {
>      Polyhedron mesh;
>      std::ifstream input(argv[1]);
>
>      if ( !input || !(input >> mesh) || mesh.empty() ) {
>          std::cerr<< argv[1] << " is not a valid off file" << std::endl;
>          return 1;
>      }
>      input.close();
>
>      //Personal Code
>      LISTT list;
>      ofstream Selection("PlaneSelection.sel");
>
>      // Creating Selection File from ROI and CV
>      list.initROI();
>      list.initCV();
>      list.writingSelection(Selection);
>
>      // Init the indices of the halfedges and the vertices.
>      set_halfedgeds_items_id(mesh);
>
>      //Variables
>      int iteratore = 0;
>      Surface_mesh_deformation deform_mesh(mesh);
>      vertex_iterator vb,ve;
>      boost::tie(vb, ve) = vertices(mesh);
>      std::string line;
>      std::vector<vertex_descriptor> control_vertices;
>      bool is_matrix_factorization_OK;
>      vertex_descriptor vd;
>      // Fine variabili
>
>      while (iteratore < 10) {
>
>          // The selection is set by a file
>          input.open("PlaneSelection.sel");
>          while(getline(input, line))
>          {
>              if (line[0]=='#') continue;
>              if (line[0]=='1') deform_mesh.insert_roi_vertex(*vb);
>              if (line[0]=='2') {
>                  deform_mesh.insert_control_vertex(*vb);
>                  control_vertices.push_back(*vb);
>              }
>              ++vb;
>              if (vb==ve) break;
>          }
>          input.close();
>
>          std::cout << "Using " << control_vertices.size() << " control
> vertices\n";
>          is_matrix_factorization_OK = deform_mesh.preprocess();
>          if(!is_matrix_factorization_OK){
>              std::cerr << "Error in preprocessing, check documentation of
> preprocess()" << std::endl;
>              return 1;
>          }
>
>          std::ostringstream FileNameDef;
>          FileNameDef << "Deform" << iteratore << ".def";
>          std::string NameDef = FileNameDef.str();
>          cout << "Using Def " << NameDef.c_str() << endl;
>          input.open(NameDef.c_str());
>          double m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23,
> hw, sink;
>          getline(input, line); // skip first comment line
>          input >> m00 >> m01 >> m02 >> m03;
>          input >> m10 >> m11 >> m12 >> m13;
>          input >> m20 >> m21 >> m22 >> m23;
>          input >> sink >> sink >> sink >> hw;
>
>          Kernel::Aff_transformation_3 aff(m00, m01, m02, m03, m10, m11, m12,
> m13, m20, m21, m22, m23);
>          BOOST_FOREACH(vd, control_vertices)
>          {
>            Surface_mesh_deformation::Point pos = vd->point().transform(aff);
>            deform_mesh.set_target_position(vd, pos);
>          }
>
>          // Call the function deform() with one-time parameters:
>          deform_mesh.deform(1000, 1e-4);
>
>          std::ostringstream FileName;
>          FileName << "Plane_" << iteratore << ".off";
>          std::string Names = FileName.str();
>
>          // Save the deformed mesh into a file
>          std::ofstream output;
>          output.open(Names.c_str());
>          output << mesh;
>          output.close();
>          iteratore++;
>      }
>
>      return 0;
>
> }
>
>   The writing of the .sel file works fine, as you can see from the plane,
> this the code for it:
>
> class LISTT
> {
> public:
>      fstream ROI,CV;
>      vector<double> xROI;
>      vector<double> yCV;
>      int k, size, toppa, check, ity;
>
>      void initROI();
>      void initCV();
>      void writingSelection(ofstream& Selection);
> };
>
> void LISTT::initROI()
> {
>      ROI.open("/PlaneROI.txt");
>      if (ROI.fail())
>      {
>          cout << "Error Opening File" << endl;
>          exit(1);
>      }
>
>      double tmp;
>      while (ROI >> tmp)
>      {
>          xROI.push_back(tmp);
>      }
>      ROI.close();
>      cout << xROI.size() << " number of values in file" << endl;
>
>      // Sorted print data from vector
>      sort(xROI.begin(), xROI.end(), sort_function);
>      for (vector<double>::iterator it = xROI.begin(); it != xROI.end(); ++it)
>      {
>        //  cout << *it << " ";
>      }
> }
>
> void LISTT::initCV()
> {
>      // Opening file
>      CV.open("PlaneCV2.txt");
>      if (CV.fail())
>      {
>          cout << "Error Opening File" << endl;
>          exit(1);
>      }
>
>      // Reading data and storing data
>      double tmp1;
>      while (CV >> tmp1)
>      {
>          yCV.push_back(tmp1);
>      }
>      CV.close();
>      cout << yCV.size() << " number of values in file" << endl;
>
>      // Sorted print data from vector
>      sort(yCV.begin(), yCV.end(), sort_function);
>      for (vector<double>::iterator it = yCV.begin(); it != yCV.end(); ++it)
>      {
>        //  cout << *it << " ";
>      }
>
>      cout << endl;
> }
>
>
> void LISTT::writingSelection(ofstream &Selection)
> {
>
>      ity = 0;
>      check = 0;
>      toppa = 1;
>      k=0;
>      size = 10000;   //Putting a bigger number of vertices (I'll directly get
> it from the input mesh size)
>
>      //ROI&CV array management
>      for(int it = 0; it < size; it ++)
>      {
>          if (xROI[it]==it+toppa)
>          {
>              if(xROI[it]==yCV[ity])
>              {
>                  Selection << 2 << endl;
>                  ity++;
>              }
>              else Selection << 1 << endl;
>              k++;
>          }
>          else
>          {
>              check = xROI[it] - k - 1;
>              for(int itt = 0; itt < check; itt++)
>              {
>                  Selection << 0 << endl;
>                  size--;         //Excluding vertices not in the ROI and not
> in the CV
>                  toppa++;     //Taking count of unselected vertices
>                  k++;           //
>              }
>              if(xROI[it]==yCV[ity])
>              {
>                  Selection << 2 << endl;
>                  ity++;
>              }
>              else Selection << 1 << endl;
>              k++;
>          }
>      }
> }
>
> I don't understand why it works for a simpler object (plane with 841
> vertices) and not for the first objects (3151 vertices).
> Could you give me a hand understanding this issue?
>
> Thank you in Advance
>
> LV
>
>
>
> --
> Sent from: http://cgal-discuss.949826.n4.nabble.com/
>

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: Surface_Mesh_Deformation help

gligart
Thank you for your contribution, I've finally managed to solve all of that,
actually there was also an error in processing the vertices, I was
considering starting from 1 instead of 0. I have another issue now, is there
a way to get the number of vertices of a mesh from the mesh object? I've
read the documentation and can't find anything like mesh.size_of_vertices()
or something similar. Should I refer to the vertex iterator or is there a
simpler way?

Thanks in advance.
LV



--
Sent from: http://cgal-discuss.949826.n4.nabble.com/

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss