DGtal 2.1.0
Loading...
Searching...
No Matches
exampleGenericLatticeConvexHull4D.cpp
Go to the documentation of this file.
1
44#include <iostream>
45#include <vector>
46#include <random>
47#include <algorithm>
48#include <polyscope/polyscope.h>
49#include <polyscope/surface_mesh.h>
50#include <polyscope/point_cloud.h>
51#include <polyscope/curve_network.h>
52
53#include "DGtal/base/Common.h"
54#include "DGtal/helpers/StdDefs.h"
55#include "DGtal/geometry/tools/GenericLatticeConvexHull.h"
56
57
58using namespace DGtal;
59
60
61//Polyscope global
62polyscope::PointCloud *psPoints[4];
63polyscope::PointCloud *psVertices[4];
64polyscope::PointCloud *psBoundary0[4];
65polyscope::CurveNetwork *psBoundary1[4];
66polyscope::SurfaceMesh *psBoundary2[4];
67polyscope::Group *group[4];
68
69std::random_device rd;
70std::mt19937 g(rd());
71
72template < Dimension dim, typename TComponent, typename TContainer >
73static
74DGtal::PointVector< dim-1, TComponent, TContainer >
77{
78 DGtal::PointVector< dim-1, TComponent, TContainer > pp;
79 Dimension l = 0;
80 for ( Dimension i = 0; i < dim; i++ )
81 if ( i != k ) pp[ l++ ] = p[ i ];
82 return pp;
83}
84
85template < Dimension dim, typename TComponent, typename TContainer >
86static
87std::vector< DGtal::PointVector< dim-1, TComponent, TContainer > >
90{
91 std::vector< DGtal::PointVector< dim-1, TComponent, TContainer > > pV( V.size() );
92 for ( auto i = 0; i < pV.size(); i++ )
93 pV[ i ] = project( k, V[ i ] );
94 return pV;
95}
96
97
98template < typename Point >
99static
100std::vector< Point >
101makeRandomLatticePointsFromDirVectors( Point A, const std::vector< Point>& V,
102 int nb, double radius, int amplitude, int aff_dim )
103{
104 std::uniform_int_distribution<int> U(-amplitude, amplitude);
105 std::vector< Point > P;
106 int m = std::min( aff_dim, (int) V.size() );
107 for ( auto k = 0; P.size() < nb && k < 100000; k++ )
108 {
109 Point B = A;
110 for ( auto i = 0; i < m; i++ )
111 {
112 int l = U( g );
113 B += l * V[ i ];
114 }
115 if ( (B-A).norm() <= radius )
116 P.push_back( B );
117 }
118 std::shuffle( P.begin(), P.end(), g );
119 return P;
120}
121
122int main( int argc, char* argv[] )
123{
125 typedef SpaceND< 4, int > Space;
126 typedef Space::Point Point4;
127
128 std::cout << "Usage: " << argv[ 0 ] << " [R=30] [N=30] [D=2]\n";
129 std::cout << "Computes the convex hull of N 4D points within a ball of radius R, these points belonging to a lattice of chosen dimension 0<=D<=3. The output is projected along the 4 canonic projections onto 3D space. You cannot choose D=4 since we cannot diplay the result in 3D.\n";
130 double radius = argc > 1 ? atof( argv[ 1 ] ) : 30.0;
131 int nb = argc > 2 ? atoi( argv[ 2 ] ) : 30;
132 int adim = argc > 3 ? atoi( argv[ 3 ] ) : 2;
133 if ( nb < 0 ) return 1;
134 if ( adim < 0 || adim > 3 ) return 1;
135
136 // Create points
137 std::vector< Point4 > L = { Point4{ 4, 1, -3, 1 },
138 Point4{ 0, 2, 5, -1 },
139 Point4{ -1, -3, 5, 0 },
140 Point4{ 2, 0, 3, -3 } };
141 std::vector< Point4 > X
142 = makeRandomLatticePointsFromDirVectors( Point4(1,2,-1,0),
143 L, nb,
144 radius,
145 int( round( radius+0.5 ) ),
146 adim );
147
148 // Compute convex hull
149 QHull hull;
150 bool ok = hull.compute( X );
151 std:: cout << ( ok ? "[PASSED]" : "[FAILED]" ) << " hull=" << hull << "\n";
152 // Initialize polyscope
153 polyscope::init();
154 std::string proj[ 4 ] = { "(yzw)", "(xzw)", "(xyw)", "(xyz)" };
155 for ( Dimension k = 0; k < 4; k++ )
156 {
157 group [k] = polyscope::createGroup( proj[k] );
158 psPoints [k] = polyscope::registerPointCloud( proj[k]+" Points",
159 project( k, X ) );
160 psVertices[k] = polyscope::registerPointCloud( proj[k]+" Vertices",
161 project( k, hull.positions ) );
162 psPoints [k]->addToGroup( proj[k] ); // add by name
163 psVertices[k]->addToGroup( proj[k] ); // add by name
164
165 if ( hull.affine_dimension <= 1 ) // 1 or 2 points
166 {
167 // no facets
168 psBoundary0[k] = polyscope::registerPointCloud( proj[k]+" Convex hull bdy dim=0",
169 project( k, hull.positions ) );
170 psBoundary0[k]->addToGroup( proj[k] ); // add by name
171 }
172 else if ( hull.affine_dimension == 2 ) // 2D
173 {
174 // facets are edges (and implicitly converted by polyscope)
175 psBoundary1[k] = polyscope::registerCurveNetwork( proj[k]+" Convex hull bdy dim=1",
176 project( k, hull.positions ),
177 hull.facets );
178 psBoundary1[k]->addToGroup( proj[k] ); // add by name
179 }
180 else if ( hull.affine_dimension == 3 ) // 3D
181 {
182 // facets are polygons
183 psBoundary2[k] = polyscope::registerSurfaceMesh( proj[k]+" Convex hull bdy dim=2",
184 project( k, hull.positions ),
185 hull.facets );
186 psBoundary2[k]->addToGroup( proj[k] ); // add by name
187 }
188 }
189
190 polyscope::show();
191 return EXIT_SUCCESS;
192
193}
Aim: Implements basic operations that will be used in Point and Vector classes.
std::mt19937 g(rd())
polyscope::PointCloud * psBoundary0[4]
static DGtal::PointVector< dim-1, TComponent, TContainer > project(Dimension k, const DGtal::PointVector< dim, TComponent, TContainer > &p)
polyscope::SurfaceMesh * psBoundary2[4]
polyscope::Group * group[4]
std::random_device rd
polyscope::PointCloud * psVertices[4]
polyscope::PointCloud * psPoints[4]
polyscope::CurveNetwork * psBoundary1[4]
static std::vector< Point > makeRandomLatticePointsFromDirVectors(Point A, const std::vector< Point > &V, int nb, double radius, int amplitude, int aff_dim)
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition Common.h:119
Aim: Implements the quickhull algorithm by Barber et al. , a famous arbitrary dimensional convex hull...
int main()
Definition testBits.cpp:56