DGtal 1.4.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/Viewer3D.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 QApplication application(argc,argv);
108 typedef Viewer3D<Space,KSpace> MViewer;
109 MViewer viewer;
110 viewer.setWindowTitle("digitalPolyhedronBuilder3D");
111 viewer.show();
112
113 Point lo(-500,-500,-500);
114 Point up(500,500,500);
115 DigitalConvexity< KSpace > dconv( lo, up );
117
118 auto vertices = std::vector<Point>( surfmesh.nbVertices() );
119 for ( auto v : surfmesh )
120 {
121 RealPoint p = (1.0 / h) * surfmesh.position( v );
122 Point q ( (Integer) round( p[ 0 ] ),
123 (Integer) round( p[ 1 ] ),
124 (Integer) round( p[ 2 ] ) );
125 vertices[ v ] = q;
126 }
127 std::set< Point > faces_set, edges_set;
128 auto faceVertices = surfmesh.allIncidentVertices();
129 auto edgeVertices = surfmesh.allEdgeVertices();
130
131 trace.beginBlock( "Computing polyhedron" );
132 for ( int f = 0; f < surfmesh.nbFaces(); ++f )
133 {
134 PointRange X;
135 for ( auto v : faceVertices[ f ] )
136 X.push_back( vertices[ v ] );
137 auto F = dconv.envelope( X, Algorithm::DIRECT );
138 faces_set.insert( F.cbegin(), F.cend() );
139 }
140 for ( int e = 0; e < surfmesh.nbEdges(); ++e )
141 {
142 PointRange X =
143 { vertices[ edgeVertices[ e ].first ],
144 vertices[ edgeVertices[ e ].second ] };
145 auto E = dconv.envelope( X, Algorithm::DIRECT );
146 edges_set.insert( E.cbegin(), E.cend() );
147 }
148 trace.endBlock();
149 std::vector< Point > face_points, edge_points;
150 std::vector< Point > vertex_points = vertices;
151 std::sort( vertex_points.begin(), vertex_points.end() );
152 std::set_difference( faces_set.cbegin(), faces_set.cend(),
153 edges_set.cbegin(), edges_set.cend(),
154 std::back_inserter( face_points ) );
155 std::set_difference( edges_set.cbegin(), edges_set.cend(),
156 vertex_points.cbegin(), vertex_points.cend(),
157 std::back_inserter( edge_points ) );
158 auto total = vertex_points.size() + edge_points.size() + face_points.size();
159 trace.info() << "#vertex points=" << vertex_points.size() << std::endl;
160 trace.info() << "#edge points=" << edge_points.size() << std::endl;
161 trace.info() << "#face points=" << face_points.size() << std::endl;
162 trace.info() << "#total points=" << total << std::endl;
163
164 // display everything
165 Color colors[] = { Color::Black, Color( 100, 100, 100 ), Color( 200, 200, 200 ) };
166 if ( view & 0x1 )
167 {
168 viewer.setLineColor( colors[ 0 ] );
169 viewer.setFillColor( colors[ 0 ] );
170 for ( auto p : vertices ) viewer << p;
171 }
172 if ( view & 0x2 )
173 {
174 viewer.setLineColor( colors[ 1 ] );
175 viewer.setFillColor( colors[ 1 ] );
176 for ( auto p : edge_points ) viewer << p;
177 }
178 if ( view & 0x4 )
179 {
180 viewer.setLineColor( colors[ 2 ] );
181 viewer.setFillColor( colors[ 2 ] );
182 for ( auto p : face_points ) viewer << p;
183 }
184 viewer << MViewer::updateDisplay;
185 return application.exec();
186
187}
188// //
190
Structure representing an RGB triple with alpha component.
Definition Color.h:68
static const Color Black
Definition Color.h:413
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,...
void beginBlock(const std::string &keyword="")
std::ostream & error()
std::ostream & info()
double endBlock()
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
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
Definition Common.h:153
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
Size nbFaces() const
Size nbEdges() const
const std::vector< Vertices > & allIncidentVertices() const
RealPoint & position(Vertex v)
Size nbVertices() const
const std::vector< VertexPair > & allEdgeVertices() const
int main()
Definition testBits.cpp:56
MyPointD Point