DGtal  1.0.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 
152 template < typename DigitalSurfaceContainer,
153  typename CellEmbedder,
154  typename VertexMap >
155 inline
156 void
157 DGtal::MeshHelpers::digitalSurface2DualTriangulatedSurface
158 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
159  const CellEmbedder& cembedder,
160  TriangulatedSurface<typename CellEmbedder::Value>& trisurf,
161  VertexMap& vertexmap )
162 {
163  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
164  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
165  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
166  typedef typename Surface::KSpace SKSpace;
167  typedef typename Surface::Vertex SVertex;
168  typedef typename Surface::VertexRange SVertexRange;
169  typedef typename CellEmbedder::Value SPoint;
170  typedef typename TriangulatedSurface< SPoint >::Index SIndex;
171  BOOST_STATIC_ASSERT(( SKSpace::dimension == 3 ));
172 
173  trisurf.clear();
174  // Numbers all vertices and add them to the triangulated surface.
175  const SKSpace & K = dsurf.container().space();
176  for ( auto it = dsurf.begin(), it_end = dsurf.end(); it != it_end; ++it )
177  {
178  const SVertex& v = *it;
179  vertexmap[ v ] = trisurf.addVertex( cembedder( K.unsigns( v ) ) );
180  }
181 
182  // Outputs closed faces.
183  auto faces = dsurf.allClosedFaces();
184  for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
185  {
186  SVertexRange vtcs = dsurf.verticesAroundFace( *itf );
187  if ( vtcs.size() == 3 )
188  trisurf.addTriangle( vertexmap[ vtcs[ 0 ] ],
189  vertexmap[ vtcs[ 1 ] ],
190  vertexmap[ vtcs[ 2 ] ] );
191  else
192  { // We must add a vertex before triangulating.
193  SPoint barycenter;
194  for ( unsigned int i = 0; i < vtcs.size(); ++i )
195  barycenter += cembedder( K.unsigns( vtcs[ i ] ) );
196  barycenter /= vtcs.size();
197  SIndex idx = trisurf.addVertex( barycenter );
198  for ( unsigned int i = 0; i < vtcs.size(); ++i )
199  trisurf.addTriangle( vertexmap[ vtcs[ i ] ],
200  vertexmap[ vtcs[ (i+1) % vtcs.size() ] ],
201  idx );
202  }
203  }
204  trisurf.build();
205 }
206 
207 template < typename DigitalSurfaceContainer,
208  typename CellEmbedder,
209  typename VertexMap >
210 inline
211 void
212 DGtal::MeshHelpers::digitalSurface2DualPolygonalSurface
213 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
214  const CellEmbedder& cembedder,
215  PolygonalSurface<typename CellEmbedder::Value>& polysurf,
216  VertexMap& vertexmap )
217 {
218  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
219  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
220  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
221  typedef typename Surface::KSpace KSpace;
222  typedef typename Surface::Vertex Vertex;
223  typedef typename Surface::VertexRange VertexRange;
224  typedef typename CellEmbedder::Value Point;
225  typedef typename PolygonalSurface< Point >::PolygonalFace PolygonalFace;
226  BOOST_STATIC_ASSERT(( KSpace::dimension == 3 ));
227 
228  polysurf.clear();
229  // Numbers all vertices and add them to the triangulated surface.
230  const KSpace & K = dsurf.container().space();
231  for ( auto it = dsurf.begin(), it_end = dsurf.end(); it != it_end; ++it )
232  {
233  const Vertex& v = *it;
234  vertexmap[ v ] = polysurf.addVertex( cembedder( K.unsigns( v ) ) );
235  }
236 
237  // Outputs closed faces.
238  auto faces = dsurf.allClosedFaces();
239  for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
240  {
241  VertexRange vtcs = dsurf.verticesAroundFace( *itf );
242  PolygonalFace face( vtcs.size() );
243  std::transform( vtcs.cbegin(), vtcs.cend(), face.begin(),
244  [ &vertexmap ] ( const Vertex& v ) { return vertexmap[ v ]; } );
245  polysurf.addPolygonalFace( face );
246  }
247  polysurf.build();
248 }
249 
250 template < typename DigitalSurfaceContainer,
251  typename CellEmbedder,
252  typename CellMap >
253 inline
254 bool
255 DGtal::MeshHelpers::digitalSurface2PrimalPolygonalSurface
256 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
257  const CellEmbedder& cembedder,
258  PolygonalSurface<typename CellEmbedder::Value>& polysurf,
259  CellMap& cellmap )
260 {
261  BOOST_CONCEPT_ASSERT(( concepts::CCellEmbedder< CellEmbedder > ));
262  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSurfaceContainer< DigitalSurfaceContainer > ));
263  typedef DigitalSurface< DigitalSurfaceContainer > Surface;
264  typedef typename Surface::KSpace KSpace;
265  typedef typename KSpace::Cell Cell;
266  typedef typename CellEmbedder::Value Point;
267  typedef typename PolygonalSurface< Point >::PolygonalFace PolygonalFace;
268  BOOST_STATIC_ASSERT(( KSpace::dimension == 3 ));
269 
270  polysurf.clear();
271  cellmap.clear();
272  // Numbers all vertices and add them to the triangulated surface.
273  const KSpace & K = dsurf.container().space();
274  for ( auto&& s : dsurf ) {
275  auto primal_vertices = Surfaces<KSpace>::getPrimalVertices( K, s );
276  for ( auto&& primal_vtx : primal_vertices ) {
277  if ( ! cellmap.count( primal_vtx ) ) {
278  auto p = cembedder( primal_vtx );
279  cellmap[ primal_vtx ] = polysurf.addVertex( p );
280  }
281  }
282  }
283 
284  // Outputs all faces
285  for ( auto&& s : dsurf ) {
286  auto primal_vertices = Surfaces<KSpace>::getPrimalVertices( K, s, true );
287  PolygonalFace face( primal_vertices.size() );
288  std::transform( primal_vertices.cbegin(), primal_vertices.cend(), face.begin(),
289  [ &cellmap ] ( const Cell& v ) { return cellmap[ v ]; } );
290  polysurf.addPolygonalFace( face );
291  }
292  return polysurf.build();
293 }
294 
295 template <typename Point>
296 bool
297 DGtal::MeshHelpers::exportOBJ
298 ( std::ostream& output,
299  const TriangulatedSurface<Point>& trisurf )
300 {
301  output << "# DGtal::MeshHelpers::exportOBJ(std::ostream&,const TriangulatedSurface<Point>&)" << std::endl;
302  // Outputing vertices
303  for ( auto i : trisurf ) {
304  Point p = trisurf.position( i );
305  output << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
306  }
307  // Outputing faces
308  auto faces = trisurf.allFaces();
309  for ( auto f : faces ) {
310  output << "f";
311  auto vertices = trisurf.verticesAroundFace( f );
312  for ( auto i : vertices ) output << " " << (i+1);
313  output << std::endl;
314  }
315  return output.good();
316 }
317 
318 template <typename Point>
319 bool
320 DGtal::MeshHelpers::exportOBJ
321 ( std::ostream& output,
322  const PolygonalSurface<Point>& polysurf )
323 {
324  output << "# DGtal::MeshHelpers::exportOBJ(std::ostream&,const PolygonalSurface<Point>&)" << std::endl;
325  // Outputing vertices
326  for ( auto i : polysurf ) {
327  Point p = polysurf.position( i );
328  output << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
329  }
330  // Outputing faces
331  auto faces = polysurf.allFaces();
332  for ( auto f : faces ) {
333  output << "f";
334  auto vertices = polysurf.verticesAroundFace( f );
335  for ( auto i : vertices ) output << " " << (i+1);
336  output << std::endl;
337  }
338  return output.good();
339 }
340 
341 inline
342 bool
343 DGtal::MeshHelpers::exportMTLNewMaterial
344 ( std::ostream& output_mtl,
345  unsigned int idxMaterial,
346  const Color& ambient_color,
347  const Color& diffuse_color,
348  const Color& specular_color )
349 {
350  output_mtl << "newmtl material_" << idxMaterial << std::endl;
351  output_mtl << "Ka " << ambient_color.red()/255.0
352  << " " << ambient_color.green()/255.0
353  << " " << ambient_color.blue()/255.0 << std::endl;
354  output_mtl << "Kd " << diffuse_color.red()/255.0
355  << " " << diffuse_color.green()/255.0
356  << " " << diffuse_color.blue()/255.0 << std::endl;
357  output_mtl << "Ks " << specular_color.red()/255.0
358  << " " << specular_color.green()/255.0
359  << " " << specular_color.blue()/255.0 << std::endl;
360  if ( diffuse_color.alpha() < 255 )
361  output_mtl << "d " << diffuse_color.alpha()/255.0 << std::endl;
362  return output_mtl.good();
363 }
364 
365 template <typename TTriangulatedOrPolygonalSurface>
366 bool
367 DGtal::MeshHelpers::exportOBJwithFaceNormalAndColor
368 ( std::ostream& output_obj,
369  const std::string& mtl_filename,
370  const TTriangulatedOrPolygonalSurface& polysurf,
371  const std::vector< typename TTriangulatedOrPolygonalSurface::Point >& normals,
372  const std::vector< Color >& diffuse_colors,
373  const Color& ambient_color,
374  const Color& diffuse_color,
375  const Color& specular_color )
376 {
377  output_obj << "# OBJ format" << std::endl;
378  output_obj << "# DGtal::MeshHelpers::exportOBJwithFaceNormalAndColor" << std::endl;
379  output_obj << "o anObject" << std::endl;
380  output_obj << "mtllib " << mtl_filename << std::endl;
381  std::ofstream output_mtl( mtl_filename.c_str() );
382  output_mtl << "# MTL format"<< std::endl;
383  output_mtl << "# generated from MeshWriter from the DGTal library"<< std::endl;
384  // Outputing vertices
385  for ( auto i : polysurf ) {
386  auto p = polysurf.position( i );
387  output_obj << "v " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
388  }
389  // Outputing faces
390  auto faces = polysurf.allFaces();
391  // Taking care of normals
392  bool has_normals = ( faces.size() == normals.size() );
393  if ( has_normals ) {
394  for ( auto f : faces ) {
395  const auto& p = normals[ f ];
396  output_obj << "vn " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
397  }
398  }
399  // Taking care of materials
400  bool has_material = ( faces.size() == diffuse_colors.size() );
401  std::map<Color, unsigned int > mapMaterial;
402  unsigned int idxMaterial = 0;
403  if ( has_material ) {
404  for ( auto f : faces ) {
405  Color c = diffuse_colors[ f ];
406  if ( mapMaterial.count( c ) == 0 ) {
407  exportMTLNewMaterial( output_mtl, idxMaterial,
408  ambient_color, c, specular_color );
409  mapMaterial[ c ] = idxMaterial++;
410  }
411  }
412  } else {
413  exportMTLNewMaterial( output_mtl, idxMaterial,
414  ambient_color, diffuse_color, specular_color );
415  }
416  // Taking care of faces
417  for ( auto f : faces ) {
418  output_obj << "usemtl material_"
419  << ( has_material ? mapMaterial[ diffuse_colors[ f ] ] : idxMaterial )
420  << std::endl;
421  output_obj << "f";
422  auto vertices = polysurf.verticesAroundFace( f );
423  if ( has_normals ) {
424  for ( auto i : vertices ) output_obj << " " << (i+1) << "//" << (f+1);
425  } else {
426  for ( auto i : vertices ) output_obj << " " << (i+1);
427  }
428  output_obj << std::endl;
429  }
430  output_mtl.close();
431  return output_obj.good();
432 }
433 
434 
435 // //
436 ///////////////////////////////////////////////////////////////////////////////
437 
438