DGtal 2.1.0
Loading...
Searching...
No Matches
digitalPolyhedronBuilder3D.cpp
Go to the documentation of this file.
1
30namespace DGtal {
57} // namespace DGtal {
58
60#include <iostream>
61#include <queue>
62#include "DGtal/base/Common.h"
63#include "DGtal/helpers/StdDefs.h"
64#include "DGtal/io/viewers/PolyscopeViewer.h"
65#include "DGtal/shapes/Shapes.h"
66#include "DGtal/shapes/SurfaceMesh.h"
67#include "DGtal/io/readers/SurfaceMeshReader.h"
68#include "DGtal/geometry/volumes/DigitalConvexity.h"
69#include "ConfigExamples.h"
70
72
73using namespace std;
74using namespace DGtal;
84typedef std::vector<Point> PointRange;
85
86int main( int argc, char** argv )
87{
88 trace.info() << "Usage: " << argv[ 0 ] << " <input.obj> <h> <view>" << std::endl;
89 trace.info() << "\tComputes a digital polyhedron from an OBJ file" << std::endl;
90 trace.info() << "\t- input.obj: choose your favorite mesh" << std::endl;
91 trace.info() << "\t- h [==1]: the digitization gridstep" << std::endl;
92 trace.info() << "\t- view [==7]: display vertices(1), edges(2), faces(4)" << std::endl;
93 string filename = examplesPath + "samples/lion.obj";
94 std::string fn = argc > 1 ? argv[ 1 ] : filename; //< vol filename
95 double h = argc > 2 ? atof( argv[ 2 ] ) : 1.0;
96 int view = argc > 3 ? atoi( argv[ 3 ] ) : 7;
97 // Read OBJ file
98 std::ifstream input( fn.c_str() );
101 if ( ! ok )
102 {
103 trace.error() << "Unable to read obj file : " << fn << std::endl;
104 return 1;
105 }
106
107 typedef PolyscopeViewer<Space,KSpace> MViewer;
108 MViewer viewer;
109
110 Point lo(-500,-500,-500);
111 Point up(500,500,500);
112 DigitalConvexity< KSpace > dconv( lo, up );
114
115 auto vertices = std::vector<Point>( surfmesh.nbVertices() );
116 for ( auto v : surfmesh )
117 {
118 RealPoint p = (1.0 / h) * surfmesh.position( v );
119 Point q ( (Integer) round( p[ 0 ] ),
120 (Integer) round( p[ 1 ] ),
121 (Integer) round( p[ 2 ] ) );
122 vertices[ v ] = q;
123 }
124 std::set< Point > faces_set, edges_set;
125 auto faceVertices = surfmesh.allIncidentVertices();
126 auto edgeVertices = surfmesh.allEdgeVertices();
127
128 trace.beginBlock( "Computing polyhedron" );
129 for ( size_t f = 0; f < surfmesh.nbFaces(); ++f )
130 {
131 PointRange X;
132 for ( auto v : faceVertices[ f ] )
133 X.push_back( vertices[ v ] );
134 auto F = dconv.envelope( X, Algorithm::DIRECT );
135 faces_set.insert( F.cbegin(), F.cend() );
136 }
137 for ( size_t e = 0; e < surfmesh.nbEdges(); ++e )
138 {
139 PointRange X =
140 { vertices[ edgeVertices[ e ].first ],
141 vertices[ edgeVertices[ e ].second ] };
142 auto E = dconv.envelope( X, Algorithm::DIRECT );
143 edges_set.insert( E.cbegin(), E.cend() );
144 }
145 trace.endBlock();
146 std::vector< Point > face_points, edge_points;
147 std::vector< Point > vertex_points = vertices;
148 std::sort( vertex_points.begin(), vertex_points.end() );
149 std::set_difference( faces_set.cbegin(), faces_set.cend(),
150 edges_set.cbegin(), edges_set.cend(),
151 std::back_inserter( face_points ) );
152 std::set_difference( edges_set.cbegin(), edges_set.cend(),
153 vertex_points.cbegin(), vertex_points.cend(),
154 std::back_inserter( edge_points ) );
155 auto total = vertex_points.size() + edge_points.size() + face_points.size();
156 trace.info() << "#vertex points=" << vertex_points.size() << std::endl;
157 trace.info() << "#edge points=" << edge_points.size() << std::endl;
158 trace.info() << "#face points=" << face_points.size() << std::endl;
159 trace.info() << "#total points=" << total << std::endl;
160
161 // display everything
162 Color colors[] = { Color::Black, Color( 100, 100, 100 ), Color( 200, 200, 200 ) };
163 if ( view & 0x1 )
164 {
165 viewer.drawColor( colors[ 0 ] );
166 viewer.drawColor( colors[ 0 ] );
167 for ( auto p : vertices ) viewer << p;
168 }
169 if ( view & 0x2 )
170 {
171 viewer.drawColor( colors[ 1 ] );
172 viewer.drawColor( colors[ 1 ] );
173 for ( auto p : edge_points ) viewer << p;
174 }
175 if ( view & 0x4 )
176 {
177 viewer.drawColor( colors[ 2 ] );
178 viewer.drawColor( colors[ 2 ] );
179 for ( auto p : face_points ) viewer << p;
180 }
181
182 viewer.show();
183 return 0;
184
185}
186// //
188
Structure representing an RGB triple with alpha component.
Definition Color.h:77
static const Color Black
Definition Color.h:422
PointRange envelope(const PointRange &Z, EnvelopeAlgorithm algo=EnvelopeAlgorithm::DIRECT) const
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
PointVector< dim, Integer > Point
Points in DGtal::SpaceND.
Definition SpaceND.h:110
PointVector< dim, double > RealPoint
Definition SpaceND.h:117
PointVector< dim, double > RealVector
Definition SpaceND.h:121
PointVector< dim, Integer > Vector
Vectors in DGtal::SpaceND.
Definition SpaceND.h:113
void beginBlock(const std::string &keyword="")
std::ostream & error()
std::ostream & info()
double endBlock()
SurfMesh surfmesh
Space::Point Point
Z3i::Integer Integer
Z3i::KSpace KSpace
Z3i::SCell SCell
std::vector< Point > PointRange
Space::Vector Vector
Z3i::Space Space
Space::RealPoint RealPoint
Z3i::Domain Domain
Space::RealVector RealVector
DGtal::int32_t Integer
Definition StdDefs.h:143
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
STL namespace.
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
static bool readOBJ(std::istream &input, SurfaceMesh &smesh)
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition SurfaceMesh.h:92
int main()
Definition testBits.cpp:56