MSDM
 All Classes Namespaces Files Functions Variables Typedefs Pages
MatlabPolyhedron.h
Go to the documentation of this file.
1 /* Copyright © 2014 – Technicolor R&D France SNC
2 All Rights Reserved
3 
4 This program is protected by intellectual property under applicable authorship & copyright laws and international conventions.
5 This program may also be subject to some patent and pending patent applications.
6 You are only permitted, under Technicolor R&D France SNC owned copyrights, to reproduce, make copy, modification thereof, and reproduction of the same for research and academic purposes only.
7 No other use is permitted unless expressly otherwise previously permitted in a written agreement with Technicolor R&D France SNC
8 
9 Except as expressly provided herein, no license or right, express or implied, is hereby conveyed or granted by Technicolor R&D France SNC to you for any intellectual property of Technicolor R&D France SNC or its affiliated company.
10 
11 YOU EXPRESSLY ACKNOWLEDGE AND AGREE THAT USE OF THE PROGRAM IS AT YOUR SOLE RISK AND THAT THE ENTIRE RISK AS TO SATISFACTORY QUALITY, PERFORMANCE, ACCURACY AND EFFORT IS WITH YOU. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED "AS IS" AND “AS AVAILABLE”, WITH ALL FAULTS AND WITHOUT WARRANTY OF ANY KIND, AND TECHNICOLOR R&D FRANCE SNC HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS WITH RESPECT TO THE PROGRAM, EITHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES AND/OR CONDITIONS OF MERCHANTABILITY, OF SATISFACTORY QUALITY, OF FITNESS FOR A PARTICULAR PURPOSE, OF ACCURACY, OF QUIET ENJOYMENT, AND NON-INFRINGEMENT OF THIRD PARTY RIGHTS. TECHNICOLOR R&D FRANCE SNC DOES NOT WARRANT AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM, THAT THE FUNCTIONS CONTAINED PERFORMED BY THE PROGRAM WILL MEET YOUR REQUIREMENTS, THAT THE OPERATION OF THE PROGRAM WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT DEFECTS IN THE PROGRAM WILL BE CORRECTED NOR IMPROVED. IF ANY CORRECTION OR IMPROVEMENT OF THE PROGRAM IS MADE BY TECHNICOLOR R&D FRANCE SNC, NOTHING IN THIS LICENSE IMPLIES THAT TECHNICOLOR R&D FRANCE SNC SHALL GIVE YOU ACCESS TO SUCH CORRECTION OR IMPROVEMENT. NO ORAL OR WRITTEN INFORMATION OR ADVICE GIVEN BY TECHNICOLOR R&D FRANCE SNC OR ITS AUTHORIZED REPRESENTATIVES SHALL CREATE A WARRANTY. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES OR LIMITATIONS ON APPLICABLE STATUTORY RIGHTS OF A CONSUMER, SO THE ABOVE EXCLUSION AND LIMITATIONS MAY NOT APPLY TO YOU.
12 
13 The use of the program implies the full acceptance of the present License.
14 
15 \author Guillaume Lavoué, Xavier Rolland-Nevière
16 */
17 
18 #ifndef MATLABPOLYHEDRON_H
19 #define MATLABPOLYHEDRON_H
20 
21 #include <CGAL/Polyhedron_incremental_builder_3.h>
22 
51 namespace Utils
52 {
63  std::unique_ptr<Polyhedron> MATLAB2Polyhedron(const mxArray *mxV, const mxArray *mxF);
64 
83  void Polyhedron2MATLAB(const Polyhedron& polyhedron, double* points, double* facets);
84 
86  template<class HDS>
87  class polyhedron_builder : public CGAL::Modifier_base<HDS>
88  {
89  private:
90  std::vector<double> &coords;
91  std::vector<int> &tris;
92  public:
100  polyhedron_builder(std::vector<double> &_coords, std::vector<int> &_tris ) : coords(_coords), tris(_tris) {}
102  void operator()( HDS& hds)
103  {
104  // create a cgal incremental builder
105  CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
106  B.begin_surface(coords.size() / 3, tris.size() / 3);
107  // add the polyhedron vertices
108  for (int i = 0; i < (int) coords.size(); i+=3)
109  {
110  B.add_vertex(HDS::Vertex::Point(coords[i+0], coords[i+1], coords[i+2]));
111  }
112  // add the polyhedron triangles
113  for(int i = 0; i < (int) tris.size(); i+=3)
114  {
115  B.begin_facet();
116  B.add_vertex_to_facet(tris[i+0]);
117  B.add_vertex_to_facet(tris[i+1]);
118  B.add_vertex_to_facet(tris[i+2]);
119  B.end_facet();
120  }
121  // finish up the surface
122  B.end_surface();
123  }
124  };
125 
126  void Polyhedron2MATLAB(const Polyhedron& polyhedron, double* points, double* facets)
127  {
128  Polyhedron::Point_const_iterator pit = polyhedron.points_begin();
129  CGAL_For_all(pit, polyhedron.points_end())
130  {
131  *points++ = pit->x();
132  *points++ = pit->y();
133  *points++ = pit->z();
134  }
135  CGAL::Inverse_index<Polyhedron::Vertex_const_iterator> index(polyhedron.vertices_begin(), polyhedron.vertices_end());
136  Polyhedron::Facet_const_iterator fit = polyhedron.facets_begin();
137  CGAL_For_all(fit, polyhedron.facets_end())
138  {
139  auto hc = fit->facet_begin();
140  auto hc_end = hc;
141  std::size_t n = circulator_size(hc);
142  CGAL_assertion(n == 3);
143  do
144  {
145  *facets++ = static_cast<double>(1 + index[Polyhedron::Vertex_const_iterator(hc->vertex())]);
146  ++hc;
147  }
148  while(hc != hc_end);
149  }
150  }
151 
152  std::unique_ptr<Polyhedron> MATLAB2Polyhedron(const mxArray *mxV, const mxArray *mxF)
153  {
154  double* V = (double*) mxGetData(mxV);
155  size_t nbv = mxGetN(mxV);
156  std::vector<double> coords(V, V + 3 * nbv);
157  size_t nbf = mxGetN(mxF);
158  double* F = (double*) mxGetData(mxF);
159  std::vector<int> ids;
160  for (size_t i = 0 ; i < nbf ; i++)
161  {
162  ids.push_back(static_cast<int>(*F++ - 1));
163  ids.push_back(static_cast<int>(*F++ - 1));
164  ids.push_back(static_cast<int>(*F++ - 1));
165  }
166  std::unique_ptr<Polyhedron> pP = std::unique_ptr<Polyhedron>(new Polyhedron);
167  polyhedron_builder<Polyhedron::HalfedgeDS> builder(coords, ids);
168  pP->delegate(builder);
169  return pP;
170  }
171 
172 }
173 #endif
void operator()(HDS &hds)
Perform the construction of the triangle surface mesh.
Definition: MatlabPolyhedron.h:102
std::unique_ptr< Polyhedron > MATLAB2Polyhedron(const mxArray *mxV, const mxArray *mxF)
Conversion from MATLAB mesh representation to CGAL polyhedron representation.
Definition: MatlabPolyhedron.h:152
void Polyhedron2MATLAB(const Polyhedron &polyhedron, double *points, double *facets)
Conversion from CGAL polyhedron to a MATLAB-compatible representation of the mesh.
Definition: MatlabPolyhedron.h:126
polyhedron_builder(std::vector< double > &_coords, std::vector< int > &_tris)
Construct a polyhedron builder class from a series of vertex coordinates and a series of triangle fac...
Definition: MatlabPolyhedron.h:100
Enriched_polyhedron< K, Enriched_items > Polyhedron
Definition: Header.h:137
Builder class for a triangle surface mesh based on a list of facets and vertices. ...
Definition: MatlabPolyhedron.h:87
Procedures to convert between MATLAB and CGAL mesh representations.
Definition: MatlabPolyhedron.h:51
K::Point_3 Point
Definition: Header.h:23
Polyhedron class for the MSDM computation.
Definition: enriched_polyhedron.h:189