File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/config/TeX-MML-AM_CHTML/MathJax.js
DGtal 2.0.0
standardDigitalPolyhedronBuilder3D.cpp File Reference
#include <iostream>
#include <queue>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/io/viewers/PolyscopeViewer.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/shapes/SurfaceMesh.h"
#include "DGtal/io/readers/SurfaceMeshReader.h"
#include "DGtal/geometry/volumes/DigitalConvexity.h"
#include "ConfigExamples.h"
Include dependency graph for standardDigitalPolyhedronBuilder3D.cpp:

Go to the source code of this file.

Data Structures

struct  MedianPlane< Naive, Symmetric >

Namespaces

namespace  DGtal
 DGtal is the top-level namespace which contains all DGtal functions and types.

Typedefs

typedef std::vector< PointPointRange
typedef MedianPlane< false, true > Plane

Functions

int main (int argc, char **argv)

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2022/06/20

An example file named standardDigitalPolyhedronBuilder3D

This file is part of the DGtal library.

Definition in file standardDigitalPolyhedronBuilder3D.cpp.

Typedef Documentation

â—† Plane

â—† PointRange

typedef std::vector<Point> PointRange

Definition at line 88 of file standardDigitalPolyhedronBuilder3D.cpp.

Function Documentation

â—† main()

int main ( int argc,
char ** argv )

Definition at line 122 of file standardDigitalPolyhedronBuilder3D.cpp.

123{
124 trace.info() << "Usage: " << argv[ 0 ] << " <input.obj> <h> <view>" << std::endl;
125 trace.info() << "\tComputes a digital polyhedron from an OBJ file" << std::endl;
126 trace.info() << "\t- input.obj: choose your favorite mesh" << std::endl;
127 trace.info() << "\t- h [==1]: the digitization gridstep" << std::endl;
128 trace.info() << "\t- view [==31]: display vertices(1), common edges(2), positive side f edges(4), negative side f edges (8), faces(16)" << std::endl;
129 string filename = examplesPath + "samples/lion.obj";
130 std::string fn = argc > 1 ? argv[ 1 ] : filename; //< vol filename
131 double h = argc > 2 ? atof( argv[ 2 ] ) : 1.0;
132 int view = argc > 3 ? atoi( argv[ 3 ] ) : 31;
133 // Read OBJ file
134 std::ifstream input( fn.c_str() );
137 if ( ! ok )
138 {
139 trace.error() << "Unable to read obj file : " << fn << std::endl;
140 return 1;
141 }
142
143 typedef PolyscopeViewer<Space,KSpace> MViewer;
144 MViewer viewer;
145
146 Point lo(-500,-500,-500);
147 Point up(500,500,500);
148 DigitalConvexity< KSpace > dconv( lo, up );
150
151 auto vertices = std::vector<Point>( surfmesh.nbVertices() );
152 for ( auto v : surfmesh )
153 {
154 RealPoint p = (1.0 / h) * surfmesh.position( v );
155 Point q ( (Integer) round( p[ 0 ] ),
156 (Integer) round( p[ 1 ] ),
157 (Integer) round( p[ 2 ] ) );
158 vertices[ v ] = q;
159 }
160 std::set< Point > faces_set, pos_edges_set, neg_edges_set;
161 auto faceVertices = surfmesh.allIncidentVertices();
162
163 trace.beginBlock( "Checking face planarity" );
164 std::vector< Plane > face_planes;
165 face_planes.resize( surfmesh.nbFaces() );
166 bool planarity = true;
167 for ( int f = 0; f < surfmesh.nbFaces() && planarity; ++f )
168 {
169 PointRange X;
170 for ( auto v : faceVertices[ f ] )
171 X.push_back( vertices[ v ] );
172 face_planes[ f ] = Plane( X[ 0 ], X[ 1 ], X[ 2 ] );
173 for ( int v = 3; v < X.size(); v++ )
174 if ( ! face_planes[ f ]( X[ v ] ) )
175 {
176 trace.error() << "Face " << f << " is not planar." << std::endl;
177 planarity = false; break;
178 }
179 }
180 trace.endBlock();
181 if ( ! planarity ) return 1;
182 trace.beginBlock( "Computing polyhedron" );
183 for ( int f = 0; f < surfmesh.nbFaces(); ++f )
184 {
185 PointRange X;
186 for ( auto v : faceVertices[ f ] )
187 X.push_back( vertices[ v ] );
188 auto F = dconv.relativeEnvelope( X, face_planes[ f ], Algorithm::DIRECT );
189 faces_set.insert( F.cbegin(), F.cend() );
190 for ( int i = 0; i < X.size(); i++ )
191 {
192 PointRange Y { X[ i ], X[ (i+1)%X.size() ] };
193 if ( Y[ 1 ] < Y[ 0 ] ) std::swap( Y[ 0 ], Y[ 1 ] );
194 int idx1 = faceVertices[ f ][ i ];
195 int idx2 = faceVertices[ f ][ (i+1)%X.size() ];
196 // Variant (1): edges of both sides have many points in common
197 // auto A = dconv.relativeEnvelope( Y, face_planes[ f ], Algorithm::DIRECT );
198 // Variant (2): edges of both sides have much less points in common
199 auto A = dconv.relativeEnvelope( Y, F, Algorithm::DIRECT );
200 bool pos = idx1 < idx2;
201 (pos ? pos_edges_set : neg_edges_set).insert( A.cbegin(), A.cend() );
202 }
203 }
204 trace.endBlock();
205 std::vector< Point > face_points, common_edge_points, arc_points, final_arc_points ;
206 std::vector< Point > pos_edge_points, neg_edge_points, both_edge_points;
207 std::vector< Point > vertex_points = vertices;
208 std::sort( vertex_points.begin(), vertex_points.end() );
209 std::set_symmetric_difference( pos_edges_set.cbegin(), pos_edges_set.cend(),
210 neg_edges_set.cbegin(), neg_edges_set.cend(),
211 std::back_inserter( arc_points ) );
212 std::set_intersection( pos_edges_set.cbegin(), pos_edges_set.cend(),
213 neg_edges_set.cbegin(), neg_edges_set.cend(),
214 std::back_inserter( common_edge_points ) );
215 std::set_union( pos_edges_set.cbegin(), pos_edges_set.cend(),
216 neg_edges_set.cbegin(), neg_edges_set.cend(),
217 std::back_inserter( both_edge_points ) );
218 std::set_difference( faces_set.cbegin(), faces_set.cend(),
219 both_edge_points.cbegin(), both_edge_points.cend(),
220 std::back_inserter( face_points ) );
221 std::set_difference( pos_edges_set.cbegin(), pos_edges_set.cend(),
222 common_edge_points.cbegin(), common_edge_points.cend(),
223 std::back_inserter( pos_edge_points ) );
224 std::set_difference( neg_edges_set.cbegin(), neg_edges_set.cend(),
225 common_edge_points.cbegin(), common_edge_points.cend(),
226 std::back_inserter( neg_edge_points ) );
227 std::set_difference( common_edge_points.cbegin(), common_edge_points.cend(),
228 vertex_points.cbegin(), vertex_points.cend(),
229 std::back_inserter( final_arc_points ) );
230 auto total = vertex_points.size() + pos_edge_points.size()
231 + neg_edge_points.size()
232 + final_arc_points.size() + face_points.size();
233 trace.info() << "#vertex points=" << vertex_points.size() << std::endl;
234 trace.info() << "#pos edge points=" << pos_edge_points.size() << std::endl;
235 trace.info() << "#neg edge points=" << neg_edge_points.size() << std::endl;
236 trace.info() << "#arc points=" << final_arc_points.size() << std::endl;
237 trace.info() << "#face points=" << face_points.size() << std::endl;
238 trace.info() << "#total points=" << total << std::endl;
239
240 // display everything
241 Color colors[] =
243 Color::Magenta, Color( 200, 200, 200 ) };
244 if ( view & 0x1 )
245 {
246 viewer.drawColor( colors[ 0 ] );
247 viewer.drawColor( colors[ 0 ] );
248 for ( auto p : vertices ) viewer << p;
249 }
250 if ( view & 0x2 )
251 {
252 viewer.drawColor( colors[ 3 ] );
253 viewer.drawColor( colors[ 3 ] );
254 for ( auto p : final_arc_points ) viewer << p;
255 }
256 if ( view & 0x4 )
257 {
258 viewer.drawColor( colors[ 1 ] );
259 viewer.drawColor( colors[ 1 ] );
260 for ( auto p : pos_edge_points ) viewer << p;
261 }
262 if ( view & 0x8 )
263 {
264 viewer.drawColor( colors[ 2 ] );
265 viewer.drawColor( colors[ 2 ] );
266 for ( auto p : neg_edge_points ) viewer << p;
267 }
268 if ( view & 0x10 )
269 {
270 viewer.drawColor( colors[ 4 ] );
271 viewer.drawColor( colors[ 4 ] );
272 for ( auto p : face_points ) viewer << p;
273 }
274
275 viewer.show();
276 return 0;
277
278}
Structure representing an RGB triple with alpha component.
Definition Color.h:77
static const Color Red
Definition Color.h:425
static const Color Black
Definition Color.h:422
static const Color Blue
Definition Color.h:428
static const Color Magenta
Definition Color.h:430
SurfMesh surfmesh
std::vector< Point > PointRange
Trace trace
std::pair< typename graph_traits< DGtal::DigitalSurface< TDigitalSurfaceContainer > >::vertex_iterator, typename graph_traits< DGtal::DigitalSurface< TDigitalSurfaceContainer > >::vertex_iterator > vertices(const DGtal::DigitalSurface< TDigitalSurfaceContainer > &digSurf)
MedianPlane< false, true > Plane
static bool readOBJ(std::istream &input, SurfaceMesh &smesh)
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
PointVector< 3, double > RealPoint

References DGtal::Color::Black, DGtal::Color::Blue, insert(), DGtal::Color::Magenta, DGtal::SurfaceMeshReader< TRealPoint, TRealVector >::readOBJ(), DGtal::Color::Red, surfmesh, and DGtal::trace.