DGtal  1.1.0
MeshHelpers.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file MeshHelpers.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21  *
22  * @date 2017/02/11
23  *
24  * Implementation of inline methods defined in MeshHelpers.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include "DGtal/topology/helpers/Surfaces.h"
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 template <typename Point>
43 inline
44 bool
45 DGtal::MeshHelpers::mesh2TriangulatedSurface
46 ( const Mesh<Point>& mesh,
47  TriangulatedSurface<Point>& trisurf )
48 {
49  trisurf.clear();
50  for ( auto it = mesh.vertexBegin(), itE = mesh.vertexEnd(); it != itE; ++it )
51  trisurf.addVertex( *it );
52  for ( auto it = mesh.faceBegin(), itE = mesh.faceEnd(); it != itE; ++it )
53  {
54  typename Mesh<Point>::MeshFace face = *it;
55  for (unsigned int i = 1; i < face.size() - 1; i++ )
56  {
57  trisurf.addTriangle( face[ 0 ], face[ i ], face[ i+1 ] );
58  }
59  }
60  return trisurf.build();
61 }
62 
63 template <typename Point>
64 inline
65 void
66 DGtal::MeshHelpers::polygonalSurface2TriangulatedSurface
67 ( const PolygonalSurface<Point>& polysurf,
68  TriangulatedSurface<Point>& trisurf,
69  bool centroid )
70 {
71  typedef typename PolygonalSurface<Point>::Index Index;
72  trisurf.clear();
73  for ( Index idx = 0; idx < polysurf.nbVertices(); ++idx )
74  trisurf.addVertex( polysurf.position( idx ) );
75  for ( Index idx = 0; idx < polysurf.nbFaces(); ++idx )
76  {
77  auto vertices = polysurf.verticesAroundFace( idx );
78  const auto nb = vertices.size();
79  if ( nb == 3 || ! centroid ) {
80  for (unsigned int i = 1; i < nb - 1; i++ )
81  trisurf.addTriangle( vertices[ 0 ], vertices[ i ], vertices[ i+1 ] );
82  } else {
83  Point c = polysurf.position( vertices[ 0 ] );
84  for (unsigned int i = 1; i < nb ; i++ )
85  c += polysurf.position( vertices[ i ] );
86  c /= nb;
87  auto idx_c = trisurf.addVertex( c );
88  for (unsigned int i = 0; i < nb; i++ )
89  trisurf.addTriangle( vertices[ i ],
90  vertices[ (i+1) % nb ], idx_c );
91  }
92  }
93  bool ok = trisurf.build();
94  if ( ! ok )
95  trace.error() << "[MeshHelpers::polygonalSurface2TriangulatedSurface]"
96  << " Error building triangulated surface." << std::endl;
97 }
98 
99 template <typename Point>
100 inline
101 bool
102 DGtal::MeshHelpers::mesh2PolygonalSurface
103 ( const Mesh<Point>& mesh,
104  PolygonalSurface<Point>& polysurf )
105 {
106  typedef typename PolygonalSurface<Point>::PolygonalFace PolygonalFace;
107  polysurf.clear();
108  for ( auto it = mesh.vertexBegin(), itE = mesh.vertexEnd(); it != itE; ++it )
109  polysurf.addVertex( *it );
110  for ( auto it = mesh.faceBegin(), itE = mesh.faceEnd(); it != itE; ++it )
111  polysurf.addPolygonalFace( PolygonalFace( it->cbegin(), it->cend() ) );
112  return polysurf.build();
113 }
114 
115 template <typename Point>
116 inline
117 void
118 DGtal::MeshHelpers::triangulatedSurface2Mesh
119 ( const TriangulatedSurface<Point>& trisurf,
120  Mesh<Point>& mesh )
121 {
122  typedef typename TriangulatedSurface<Point>::Index Index;
123  for ( Index idx = 0; idx < trisurf.nbVertices(); ++idx )
124  mesh.addVertex( trisurf.position( idx ) );
125  for ( Index idx = 0; idx < trisurf.nbFaces(); ++idx )
126  {
127  auto vertices = trisurf.verticesAroundFace( idx );
128  mesh.addTriangularFace( vertices[ 0 ], vertices[ 1 ], vertices[ 2 ] );
129  }
130 }
131 
132 template <typename Point>
133 inline
134 void
135 DGtal::MeshHelpers::polygonalSurface2Mesh
136 ( const PolygonalSurface<Point>& polysurf,
137  Mesh<Point>& mesh )
138 {
139  typedef typename Mesh<Point>::MeshFace MeshFace;
140  typedef typename PolygonalSurface<Point>::Index Index;
141  for ( Index idx = 0; idx < polysurf.nbVertices(); ++idx )
142  mesh.addVertex( polysurf.position( idx ) );
143  for ( Index idx = 0; idx < polysurf.nbFaces(); ++idx )
144  {
145  auto vertices = polysurf.verticesAroundFace( idx );
146  MeshFace face( vertices.cbegin(), vertices.cend() );
147  mesh.addFace( face );
148  }
149 }
150 
151 template < typename RealPoint, typename RealVector >
152 inline
153 void
154 DGtal::MeshHelpers::surfaceMesh2Mesh
155 ( const SurfaceMesh< RealPoint, RealVector >& smesh,
156 Mesh< RealPoint >& mesh, const std::vector<Color> &cols )
157 {
158  bool hasColor = cols.size() == smesh.nbFaces();
159  for ( auto&& v : smesh.positions() )
160  mesh.addVertex( v );
161  unsigned int i = 0;
162  for ( auto&& f : smesh.allIncidentVertices() )
163  {
164  typename Mesh< RealPoint >::MeshFace face( f.cbegin(), f.cend() );
165  if (hasColor){
166  mesh.addFace( face, cols[i] );
167  i++;
168  }
169  mesh.addFace( face );
170  }
171 }
172 
173 
174 template < typename DigitalSurfaceContainer,
175  typename CellEmbedder,
176  typename VertexMap >
177 inline
178 void
179 DGtal::MeshHelpers::digitalSurface2DualTriangulatedSurface
180 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
181  const CellEmbedder& cembedder,
182  TriangulatedSurface<typename CellEmbedder::Value>& trisurf,
183  VertexMap& vertexmap )
184 {
185  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
186  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
187  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
188  typedef typename Surface::KSpace SKSpace;
189  typedef typename Surface::Vertex SVertex;
190  typedef typename Surface::VertexRange SVertexRange;
191  typedef typename CellEmbedder::Value SPoint;
192  typedef typename TriangulatedSurface< SPoint >::Index SIndex;
193  BOOST_STATIC_ASSERT(( SKSpace::dimension == 3 ));
194 
195  trisurf.clear();
196  // Numbers all vertices and add them to the triangulated surface.
197  const SKSpace & K = dsurf.container().space();
198  for ( auto it = dsurf.begin(), it_end = dsurf.end(); it != it_end; ++it )
199  {
200  const SVertex& v = *it;
201  vertexmap[ v ] = trisurf.addVertex( cembedder( K.unsigns( v ) ) );
202  }
203 
204  // Outputs closed faces.
205  auto faces = dsurf.allClosedFaces();
206  for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
207  {
208  SVertexRange vtcs = dsurf.verticesAroundFace( *itf );
209  if ( vtcs.size() == 3 )
210  trisurf.addTriangle( vertexmap[ vtcs[ 0 ] ],
211  vertexmap[ vtcs[ 1 ] ],
212  vertexmap[ vtcs[ 2 ] ] );
213  else
214  { // We must add a vertex before triangulating.
215  SPoint barycenter;
216  for ( unsigned int i = 0; i < vtcs.size(); ++i )
217  barycenter += cembedder( K.unsigns( vtcs[ i ] ) );
218  barycenter /= vtcs.size();
219  SIndex idx = trisurf.addVertex( barycenter );
220  for ( unsigned int i = 0; i < vtcs.size(); ++i )
221  trisurf.addTriangle( vertexmap[ vtcs[ i ] ],
222  vertexmap[ vtcs[ (i+1) % vtcs.size() ] ],
223  idx );
224  }
225  }
226  trisurf.build();
227 }
228 
229 template < typename DigitalSurfaceContainer,
230  typename CellEmbedder,
231  typename VertexMap >
232 inline
233 void
234 DGtal::MeshHelpers::digitalSurface2DualPolygonalSurface
235 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
236  const CellEmbedder& cembedder,
237  PolygonalSurface<typename CellEmbedder::Value>& polysurf,
238  VertexMap& vertexmap )
239 {
240  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
241  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
242  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
243  typedef typename Surface::KSpace KSpace;
244  typedef typename Surface::Vertex Vertex;
245  typedef typename Surface::VertexRange VertexRange;
246  typedef typename CellEmbedder::Value Point;
247  typedef typename PolygonalSurface< Point >::PolygonalFace PolygonalFace;
248  BOOST_STATIC_ASSERT(( KSpace::dimension == 3 ));
249 
250  polysurf.clear();
251  // Numbers all vertices and add them to the triangulated surface.
252  const KSpace & K = dsurf.container().space();
253  for ( auto it = dsurf.begin(), it_end = dsurf.end(); it != it_end; ++it )
254  {
255  const Vertex& v = *it;
256  vertexmap[ v ] = polysurf.addVertex( cembedder( K.unsigns( v ) ) );
257  }
258 
259  // Outputs closed faces.
260  auto faces = dsurf.allClosedFaces();
261  for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
262  {
263  VertexRange vtcs = dsurf.verticesAroundFace( *itf );
264  PolygonalFace face( vtcs.size() );
265  std::transform( vtcs.cbegin(), vtcs.cend(), face.begin(),
266  [ &vertexmap ] ( const Vertex& v ) { return vertexmap[ v ]; } );
267  polysurf.addPolygonalFace( face );
268  }
269  polysurf.build();
270 }
271 
272 template < typename DigitalSurfaceContainer,
273  typename CellEmbedder,
274  typename CellMap >
275 inline
276 bool
277 DGtal::MeshHelpers::digitalSurface2PrimalPolygonalSurface
278 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
279  const CellEmbedder& cembedder,
280  PolygonalSurface<typename CellEmbedder::Value>& polysurf,
281  CellMap& cellmap )
282 {
283  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
284  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
285  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
286  typedef typename Surface::KSpace KSpace;
287  typedef typename KSpace::Cell Cell;
288  typedef typename CellEmbedder::Value Point;
289  typedef typename PolygonalSurface< Point >::PolygonalFace PolygonalFace;
290  BOOST_STATIC_ASSERT(( KSpace::dimension == 3 ));
291 
292  polysurf.clear();
293  cellmap.clear();
294  // Numbers all vertices and add them to the triangulated surface.
295  const KSpace & K = dsurf.container().space();
296  for ( auto&& s : dsurf ) {
297  auto primal_vertices = Surfaces<KSpace>::getPrimalVertices( K, s, true );
298  for ( auto&& primal_vtx : primal_vertices ) {
299  if ( ! cellmap.count( primal_vtx ) ) {
300  auto p = cembedder( primal_vtx );
301  cellmap[ primal_vtx ] = polysurf.addVertex( p );
302  }
303  }
304  }
305 
306  // Outputs all faces
307  for ( auto&& s : dsurf ) {
308  auto primal_vertices = Surfaces<KSpace>::getPrimalVertices( K, s, true );
309  PolygonalFace face( primal_vertices.size() );
310  std::transform( primal_vertices.cbegin(), primal_vertices.cend(), face.begin(),
311  [ &cellmap ] ( const Cell& v ) { return cellmap[ v ]; } );
312  polysurf.addPolygonalFace( face );
313  }
314  return polysurf.build();
315 }
316 
317 template <typename Point>
318 bool
319 DGtal::MeshHelpers::exportOBJ
320 ( std::ostream& output,
321  const TriangulatedSurface<Point>& trisurf )
322 {
323  output << "# DGtal::MeshHelpers::exportOBJ(std::ostream&,const TriangulatedSurface<Point>&)" << std::endl;
324  // Outputing vertices
325  for ( auto i : trisurf ) {
326  Point p = trisurf.position( i );
327  output << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
328  }
329  // Outputing faces
330  auto faces = trisurf.allFaces();
331  for ( auto f : faces ) {
332  output << "f";
333  auto vertices = trisurf.verticesAroundFace( f );
334  for ( auto i : vertices ) output << " " << (i+1);
335  output << std::endl;
336  }
337  return output.good();
338 }
339 
340 template <typename Point>
341 bool
342 DGtal::MeshHelpers::exportOBJ
343 ( std::ostream& output,
344  const PolygonalSurface<Point>& polysurf )
345 {
346  output << "# DGtal::MeshHelpers::exportOBJ(std::ostream&,const PolygonalSurface<Point>&)" << std::endl;
347  // Outputing vertices
348  for ( auto i : polysurf ) {
349  Point p = polysurf.position( i );
350  output << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
351  }
352  // Outputing faces
353  auto faces = polysurf.allFaces();
354  for ( auto f : faces ) {
355  output << "f";
356  auto vertices = polysurf.verticesAroundFace( f );
357  for ( auto i : vertices ) output << " " << (i+1);
358  output << std::endl;
359  }
360  return output.good();
361 }
362 
363 inline
364 bool
365 DGtal::MeshHelpers::exportMTLNewMaterial
366 ( std::ostream& output_mtl,
367  unsigned int idxMaterial,
368  const Color& ambient_color,
369  const Color& diffuse_color,
370  const Color& specular_color )
371 {
372  output_mtl << "newmtl material_" << idxMaterial << std::endl;
373  output_mtl << "Ka " << ambient_color.red()/255.0
374  << " " << ambient_color.green()/255.0
375  << " " << ambient_color.blue()/255.0 << std::endl;
376  output_mtl << "Kd " << diffuse_color.red()/255.0
377  << " " << diffuse_color.green()/255.0
378  << " " << diffuse_color.blue()/255.0 << std::endl;
379  output_mtl << "Ks " << specular_color.red()/255.0
380  << " " << specular_color.green()/255.0
381  << " " << specular_color.blue()/255.0 << std::endl;
382  if ( diffuse_color.alpha() < 255 )
383  output_mtl << "d " << diffuse_color.alpha()/255.0 << std::endl;
384  return output_mtl.good();
385 }
386 
387 template <typename TTriangulatedOrPolygonalSurface>
388 bool
389 DGtal::MeshHelpers::exportOBJwithFaceNormalAndColor
390 ( std::ostream& output_obj,
391  const std::string& mtl_filename,
392  const TTriangulatedOrPolygonalSurface& polysurf,
393  const std::vector< typename TTriangulatedOrPolygonalSurface::Point >& normals,
394  const std::vector< Color >& diffuse_colors,
395  const Color& ambient_color,
396  const Color& diffuse_color,
397  const Color& specular_color )
398 {
399  output_obj << "# OBJ format" << std::endl;
400  output_obj << "# DGtal::MeshHelpers::exportOBJwithFaceNormalAndColor" << std::endl;
401  output_obj << "o anObject" << std::endl;
402  output_obj << "mtllib " << mtl_filename << std::endl;
403  std::ofstream output_mtl( mtl_filename.c_str() );
404  output_mtl << "# MTL format"<< std::endl;
405  output_mtl << "# generated from MeshWriter from the DGTal library"<< std::endl;
406  // Outputing vertices
407  for ( auto i : polysurf ) {
408  auto p = polysurf.position( i );
409  output_obj << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
410  }
411  // Outputing faces
412  auto faces = polysurf.allFaces();
413  // Taking care of normals
414  bool has_normals = ( faces.size() == normals.size() );
415  if ( has_normals ) {
416  for ( auto f : faces ) {
417  const auto& p = normals[ f ];
418  output_obj << "vn " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
419  }
420  }
421  // Taking care of materials
422  bool has_material = ( faces.size() == diffuse_colors.size() );
423  std::map<Color, unsigned int > mapMaterial;
424  unsigned int idxMaterial = 0;
425  if ( has_material ) {
426  for ( auto f : faces ) {
427  Color c = diffuse_colors[ f ];
428  if ( mapMaterial.count( c ) == 0 ) {
429  exportMTLNewMaterial( output_mtl, idxMaterial,
430  ambient_color, c, specular_color );
431  mapMaterial[ c ] = idxMaterial++;
432  }
433  }
434  } else {
435  exportMTLNewMaterial( output_mtl, idxMaterial,
436  ambient_color, diffuse_color, specular_color );
437  }
438  // Taking care of faces
439  for ( auto f : faces ) {
440  output_obj << "usemtl material_"
441  << ( has_material ? mapMaterial[ diffuse_colors[ f ] ] : idxMaterial )
442  << std::endl;
443  output_obj << "f";
444  auto vertices = polysurf.verticesAroundFace( f );
445  if ( has_normals ) {
446  for ( auto i : vertices ) output_obj << " " << (i+1) << "//" << (f+1);
447  } else {
448  for ( auto i : vertices ) output_obj << " " << (i+1);
449  }
450  output_obj << std::endl;
451  }
452  output_mtl.close();
453  return output_obj.good();
454 }
455 
456 
457 // //
458 ///////////////////////////////////////////////////////////////////////////////
459 
460