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.
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.
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/>.
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
24 * Implementation of inline methods defined in MeshHelpers.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 #include "DGtal/topology/helpers/Surfaces.h"
33 //////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
42 template <typename Point>
45 DGtal::MeshHelpers::mesh2TriangulatedSurface
46 ( const Mesh<Point>& mesh,
47 TriangulatedSurface<Point>& trisurf )
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 )
54 typename Mesh<Point>::MeshFace face = *it;
55 for (unsigned int i = 1; i < face.size() - 1; i++ )
57 trisurf.addTriangle( face[ 0 ], face[ i ], face[ i+1 ] );
60 return trisurf.build();
63 template <typename Point>
66 DGtal::MeshHelpers::polygonalSurface2TriangulatedSurface
67 ( const PolygonalSurface<Point>& polysurf,
68 TriangulatedSurface<Point>& trisurf,
71 typedef typename PolygonalSurface<Point>::Index Index;
73 for ( Index idx = 0; idx < polysurf.nbVertices(); ++idx )
74 trisurf.addVertex( polysurf.position( idx ) );
75 for ( Index idx = 0; idx < polysurf.nbFaces(); ++idx )
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 ] );
83 Point c = polysurf.position( vertices[ 0 ] );
84 for (unsigned int i = 1; i < nb ; i++ )
85 c += polysurf.position( vertices[ i ] );
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 );
93 bool ok = trisurf.build();
95 trace.error() << "[MeshHelpers::polygonalSurface2TriangulatedSurface]"
96 << " Error building triangulated surface." << std::endl;
99 template <typename Point>
102 DGtal::MeshHelpers::mesh2PolygonalSurface
103 ( const Mesh<Point>& mesh,
104 PolygonalSurface<Point>& polysurf )
106 typedef typename PolygonalSurface<Point>::PolygonalFace PolygonalFace;
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();
115 template <typename Point>
118 DGtal::MeshHelpers::triangulatedSurface2Mesh
119 ( const TriangulatedSurface<Point>& trisurf,
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 )
127 auto vertices = trisurf.verticesAroundFace( idx );
128 mesh.addTriangularFace( vertices[ 0 ], vertices[ 1 ], vertices[ 2 ] );
132 template <typename Point>
135 DGtal::MeshHelpers::polygonalSurface2Mesh
136 ( const PolygonalSurface<Point>& polysurf,
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 )
145 auto vertices = polysurf.verticesAroundFace( idx );
146 MeshFace face( vertices.cbegin(), vertices.cend() );
147 mesh.addFace( face );
151 template < typename RealPoint, typename RealVector >
154 DGtal::MeshHelpers::surfaceMesh2Mesh
155 ( const SurfaceMesh< RealPoint, RealVector >& smesh,
156 Mesh< RealPoint >& mesh, const std::vector<Color> &cols )
158 bool hasColor = cols.size() == smesh.nbFaces();
159 for ( auto&& v : smesh.positions() )
162 for ( auto&& f : smesh.allIncidentVertices() )
164 typename Mesh< RealPoint >::MeshFace face( f.cbegin(), f.cend() );
166 mesh.addFace( face, cols[i] );
169 mesh.addFace( face );
174 template < typename DigitalSurfaceContainer,
175 typename CellEmbedder,
179 DGtal::MeshHelpers::digitalSurface2DualTriangulatedSurface
180 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
181 const CellEmbedder& cembedder,
182 TriangulatedSurface<typename CellEmbedder::Value>& trisurf,
183 VertexMap& vertexmap )
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 ));
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 )
200 const SVertex& v = *it;
201 vertexmap[ v ] = trisurf.addVertex( cembedder( K.unsigns( v ) ) );
204 // Outputs closed faces.
205 auto faces = dsurf.allClosedFaces();
206 for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
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 ] ] );
214 { // We must add a vertex before triangulating.
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() ] ],
229 template < typename DigitalSurfaceContainer,
230 typename CellEmbedder,
234 DGtal::MeshHelpers::digitalSurface2DualPolygonalSurface
235 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
236 const CellEmbedder& cembedder,
237 PolygonalSurface<typename CellEmbedder::Value>& polysurf,
238 VertexMap& vertexmap )
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 ));
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 )
255 const Vertex& v = *it;
256 vertexmap[ v ] = polysurf.addVertex( cembedder( K.unsigns( v ) ) );
259 // Outputs closed faces.
260 auto faces = dsurf.allClosedFaces();
261 for ( auto itf = faces.begin(), itf_end = faces.end(); itf != itf_end; ++itf )
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 );
272 template < typename DigitalSurfaceContainer,
273 typename CellEmbedder,
277 DGtal::MeshHelpers::digitalSurface2PrimalPolygonalSurface
278 ( const DigitalSurface<DigitalSurfaceContainer>& dsurf,
279 const CellEmbedder& cembedder,
280 PolygonalSurface<typename CellEmbedder::Value>& polysurf,
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 ));
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 );
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 );
314 return polysurf.build();
317 template <typename Point>
319 DGtal::MeshHelpers::exportOBJ
320 ( std::ostream& output,
321 const TriangulatedSurface<Point>& trisurf )
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;
330 auto faces = trisurf.allFaces();
331 for ( auto f : faces ) {
333 auto vertices = trisurf.verticesAroundFace( f );
334 for ( auto i : vertices ) output << " " << (i+1);
337 return output.good();
340 template <typename Point>
342 DGtal::MeshHelpers::exportOBJ
343 ( std::ostream& output,
344 const PolygonalSurface<Point>& polysurf )
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;
353 auto faces = polysurf.allFaces();
354 for ( auto f : faces ) {
356 auto vertices = polysurf.verticesAroundFace( f );
357 for ( auto i : vertices ) output << " " << (i+1);
360 return output.good();
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 )
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();
387 template <typename TTriangulatedOrPolygonalSurface>
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 )
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;
412 auto faces = polysurf.allFaces();
413 // Taking care of normals
414 bool has_normals = ( faces.size() == normals.size() );
416 for ( auto f : faces ) {
417 const auto& p = normals[ f ];
418 output_obj << "vn " << p[ 0 ] << " " << p[ 1 ] << " " << p[ 2 ] << std::endl;
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++;
435 exportMTLNewMaterial( output_mtl, idxMaterial,
436 ambient_color, diffuse_color, specular_color );
438 // Taking care of faces
439 for ( auto f : faces ) {
440 output_obj << "usemtl material_"
441 << ( has_material ? mapMaterial[ diffuse_colors[ f ] ] : idxMaterial )
444 auto vertices = polysurf.verticesAroundFace( f );
446 for ( auto i : vertices ) output_obj << " " << (i+1) << "//" << (f+1);
448 for ( auto i : vertices ) output_obj << " " << (i+1);
450 output_obj << std::endl;
453 return output_obj.good();
458 ///////////////////////////////////////////////////////////////////////////////