DGtal 2.1.0
Loading...
Searching...
No Matches
viewDualSurface.cpp File Reference
#include <iostream>
#include <algorithm>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/topology/helpers/Surfaces.h"
#include "ConfigExamples.h"
#include "DGtal/io/viewers/PolyscopeViewer.h"
Include dependency graph for viewDualSurface.cpp:

Go to the source code of this file.

Data Structures

struct  LessThanOnFace< Vector >
 
struct  ConfigPointPredicate< Vector >
 

Functions

template<typename Vector >
Vector wedge (const Vector &v1, const Vector &v2)
 
template<typename Vector >
void naiveConvexHull (std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points, bool left_handed)
 
double rescale (double x)
 
template<typename Viewer , typename Vector >
void viewPolygons (Viewer &viewer, const std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points)
 
template<typename Vector >
unsigned int dim (const Vector &z)
 
template<typename Vector >
unsigned int openDim (const Vector &z)
 
template<typename Vector >
Vector lower (const Vector &z, unsigned int k)
 
template<typename Vector >
Vector upper (const Vector &z, unsigned int k)
 
template<typename Vector >
unsigned int nbLighted (std::map< Vector, bool > &f, const Vector &z)
 
template<typename Vector >
bool lightBetween (std::map< Vector, bool > &f, const Vector &z)
 
template<typename Vector >
bool lightMax (std::map< Vector, bool > &f, const Vector &z)
 
template<typename Vector >
bool lightMinMax (std::map< Vector, bool > &f, const Vector &z)
 
template<typename Vector >
bool lightMaxMin (std::map< Vector, bool > &f, const Vector &z)
 
template<typename Vector >
bool lightEpsilon (std::map< Vector, bool > &f, const Vector &z, unsigned int epsilon)
 
template<typename Vector >
void fillCfg (std::map< Vector, bool > &f, const Vector &z, unsigned int cfg)
 
template<typename Vector >
void localDualVolume (std::vector< Vector > &points, std::map< Vector, bool > &f, const Vector &z)
 
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/.

Date
2011/03/25

An example file named viewDualSurface.

This file is part of the DGtal library.

Definition in file viewDualSurface.cpp.

Function Documentation

◆ dim()

template<typename Vector >
unsigned int dim ( const Vector z)

Definition at line 173 of file viewDualSurface.cpp.

174{
175 unsigned int d = 0;
176 for ( unsigned int i = 0; i < Vector::dimension; ++i )
177 if ( ( z[ i ] % 2 ) == 1 ) ++d;
178 return d;
179}
static const Dimension dimension
Copy of the static dimension of the Point/Vector.

References DGtal::PointVector< dim, Integer >::dimension.

◆ fillCfg()

template<typename Vector >
void fillCfg ( std::map< Vector, bool > &  f,
const Vector z,
unsigned int  cfg 
)
Examples
io/viewDualSurface.cpp.

Definition at line 336 of file viewDualSurface.cpp.

339{
340 unsigned int d = dim( z );
341 if ( d == 0 )
342 {
343 f[ z ] = (cfg == 1);
344 //std::cerr << "f[" << z << "] = " << f[ z ] << std::endl;
345 }
346 else
347 {
348 unsigned n = 1 << ( d - 1 );
349 unsigned int cfgLow = 0;
350 unsigned int cfgUp = 0;
351 for ( unsigned int j = 0; j < n; ++j )
352 {
353 cfgLow += ( cfg & 1 ) << j;
354 cfg >>= 1;
355 cfgUp += ( cfg & 1 ) << j;
356 cfg >>= 1;
357 }
358 unsigned int i = openDim( z );
359 fillCfg( f, lower( z, i ), cfgLow );
360 fillCfg( f, upper( z, i ), cfgUp );
361 }
362}
Vector lower(const Vector &z, unsigned int k)
unsigned int openDim(const Vector &z)
void fillCfg(std::map< Vector, bool > &f, const Vector &z, unsigned int cfg)
Vector upper(const Vector &z, unsigned int k)

References dim, fillCfg(), lower(), openDim(), and upper().

Referenced by fillCfg(), and main().

◆ lightBetween()

template<typename Vector >
bool lightBetween ( std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 217 of file viewDualSurface.cpp.

219{
220 unsigned int d = dim( z );
221 if ( d == 0 ) return f[ z ];
222 else if ( d == 1 )
223 {
224 unsigned int i = openDim( z );
225 return f[ lower( z, i ) ] || f[ upper( z, i ) ];
226 }
227 else
228 {
229 Vector v1, v2;
230 for ( unsigned int i = 0; i < Vector::dimension; ++i )
231 {
232 v1[ i ] = ( ( z[ i ] % 2 ) == 1 ) ? z[ i ] - 1 : z[ i ];
233 v2[ i ] = ( ( z[ i ] % 2 ) == 1 ) ? z[ i ] + 1 : z[ i ];
234 }
235 Domain domain( v1, v2 );
236 for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
237 it != itE; ++it )
238 {
239 if ( *it == z ) break;
240 Point zp = z*2 - *it;
241 // std::cerr << *it << " <--> " << zp << std::endl;
242 if ( lightBetween( f, *it ) && lightBetween( f, zp ) )
243 return true;
244 }
245 return false;
246 }
247
248}
const ConstIterator & begin() const
const ConstIterator & end() const
Domain domain
bool lightBetween(std::map< Vector, bool > &f, const Vector &z)

References DGtal::HyperRectDomain< TSpace >::begin(), dim, DGtal::PointVector< dim, Integer >::dimension, domain, DGtal::HyperRectDomain< TSpace >::end(), lightBetween(), lower(), openDim(), and upper().

Referenced by lightBetween(), and main().

◆ lightEpsilon()

template<typename Vector >
bool lightEpsilon ( std::map< Vector, bool > &  f,
const Vector z,
unsigned int  epsilon 
)
Examples
io/viewDualSurface.cpp.

Definition at line 308 of file viewDualSurface.cpp.

311{
312 unsigned int d = dim( z );
313 if ( d == 0 ) return f[ z ];
314 else
315 {
316 Vector tmp( z );
317 bool eps_d = ( ( epsilon >> (d-1)) & 1 ) != 0;
318 bool val = eps_d ? true : false;
319 for ( unsigned i = 0; i < d; ++i )
320 {
321 unsigned int k = openDim( tmp );
322 tmp = lower( tmp, k );
323 if ( eps_d )
324 val = val && ( lightEpsilon( f, lower( z, k ), epsilon )
325 || lightEpsilon( f, upper( z, k ), epsilon ) );
326 else
327 val = val || ( lightEpsilon( f, lower( z, k ), epsilon )
328 && lightEpsilon( f, upper( z, k ), epsilon ) );
329 }
330 return val;
331 }
332}
bool lightEpsilon(std::map< Vector, bool > &f, const Vector &z, unsigned int epsilon)

References dim, lightEpsilon(), lower(), openDim(), and upper().

Referenced by lightEpsilon().

◆ lightMax()

template<typename Vector >
bool lightMax ( std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 252 of file viewDualSurface.cpp.

254{
255 unsigned int d = dim( z );
256 if ( d == 0 ) return f[ z ];
257 else if ( d == 1 )
258 {
259 unsigned int i = openDim( z );
260 return f[ lower( z, i ) ] || f[ upper( z, i ) ];
261 }
262 else // if ( d > 1 )
263 {
264 unsigned int n = nbLighted( f, z );
265 return n >= 2;
266 }
267}
unsigned int nbLighted(std::map< Vector, bool > &f, const Vector &z)

References dim, lower(), nbLighted(), openDim(), and upper().

◆ lightMaxMin()

template<typename Vector >
bool lightMaxMin ( std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 288 of file viewDualSurface.cpp.

290{
291 unsigned int d = dim( z );
292 if ( d == 0 ) return f[ z ];
293 else
294 {
295 Vector tmp( z );
296 bool val = false;
297 for ( unsigned i = 0; i < d; ++i )
298 {
299 unsigned int k = openDim( tmp );
300 tmp = lower( tmp, k );
301 val = val || ( lightMaxMin( f, lower( z, k ) ) && lightMaxMin( f, upper( z, k ) ) );
302 }
303 return val;
304 }
305}
bool lightMaxMin(std::map< Vector, bool > &f, const Vector &z)

References dim, lightMaxMin(), lower(), openDim(), and upper().

Referenced by lightMaxMin().

◆ lightMinMax()

template<typename Vector >
bool lightMinMax ( std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 269 of file viewDualSurface.cpp.

271{
272 unsigned int d = dim( z );
273 if ( d == 0 ) return f[ z ];
274 else
275 {
276 Vector tmp( z );
277 bool val = true;
278 for ( unsigned i = 0; i < d; ++i )
279 {
280 unsigned int k = openDim( tmp );
281 tmp = lower( tmp, k );
282 val = val && ( lightMinMax( f, lower( z, k ) ) || lightMinMax( f, upper( z, k ) ) );
283 }
284 return val;
285 }
286}
bool lightMinMax(std::map< Vector, bool > &f, const Vector &z)

References dim, lightMinMax(), lower(), openDim(), and upper().

Referenced by lightMinMax().

◆ localDualVolume()

template<typename Vector >
void localDualVolume ( std::vector< Vector > &  points,
std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 365 of file viewDualSurface.cpp.

368{
369 points.clear();
371 for ( Z3i::Domain::ConstIterator it = domain.begin(), itE = domain.end();
372 it != itE; ++it )
373 {
374 if ( f[ *it ] ) points.push_back( *it );
375 }
376}
static Self diagonal(Component val=1)

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::PointVector< dim, Integer >::diagonal(), domain, and DGtal::HyperRectDomain< TSpace >::end().

Referenced by main().

◆ lower()

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 391 of file viewDualSurface.cpp.

392{
393 typedef KSpace::CellSet CellSet;
394
395 KSpace KS;
396
398 viewer.allowReuseList = true;
399
400 DGtal::Color fillColor( 200, 200, 220, 255 );
401 DGtal::Color surfelColor( 255, 0, 0, 150 );
402 DGtal::Color voxelColor( 150, 150, 0, 150 );
403
404 // Create lists for display:
405 // List will inherit current parameters of viewer
406 viewer << surfelColor;
407 std::string surfels = viewer.newQuadList("Surfels");
408 viewer << voxelColor;
409 std::string polys = viewer.newPolygonList("Polygons");
410
411 std::vector<Vector> pts;
412
413 unsigned int cfg = argc > 1 ? atoi( argv[1] ) : 0;
414 unsigned int cfg2 = argc > 2 ? atoi( argv[2] ) : 255;
415 std::map< Vector, bool > f;
416 for ( unsigned int y = 0; (y < 16) && (cfg <= cfg2); ++y )
417 for ( unsigned int x = 0; (x < 16) && (cfg <= cfg2); ++x, ++cfg )
418 {
419 Vector offset( x*6, y*6, 0 );
420 fillCfg( f, offset + Vector( 1, 1, 1 ), cfg );
421 Domain domain( offset + Vector( 0, 0, 0), offset + Vector( 2, 2, 2 ) );
422 KSpace K;
423 K.init( Vector( 0, 0, 0), Vector( 2, 2, 2 ), true );
424 ConfigPointPredicate<Vector> cpp( f, offset );
425 CellSet aBoundary;
426 Surfaces<KSpace>::uMakeBoundary( aBoundary, K, cpp, Vector( 0, 0, 0), Vector( 1, 1, 1 ) );
427
428 viewer.setCurrentList(surfels);
429 for ( CellSet::const_iterator it = aBoundary.begin(), itE = aBoundary.end();
430 it != itE; ++it )
431 {
432 viewer << surfelColor;
433 viewer << KS.uTranslation( *it, offset/2 );
434 }
435 for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
436 it != itE; ++it )
437 {
438 // lightEpsilon( f, *it, 5 ); // {1,-1,1}=5 // interesting
439 f[ *it ] = lightBetween( f, *it );
440 }
441
442 viewer << fillColor;
443 std::vector< std::vector< unsigned int > > indices;
444 Domain domain2( offset + Vector( 0, 0, 0), offset + Vector( 1, 1, 1 ) );
445
446 viewer.setCurrentList(polys);
447 for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
448 it != itE; ++it )
449 {
450 localDualVolume( pts, f, *it );
451 indices.clear();
452 naiveConvexHull( indices, pts, false ); // right_handed
453 viewPolygons( viewer, indices, pts );
454 }
455 }
456 viewer.show();
457 return 0;
458}
Structure representing an RGB triple with alpha component.
Definition Color.h:77
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Cell uTranslation(const Cell &p, const Vector &vec) const
Add the vector [vec] to [p].
std::set< Cell > CellSet
Preferred type for defining a set of Cell(s).
static void uMakeBoundary(CellSet &aBoundary, const KSpace &aKSpace, const PointPredicate &pp, const Point &aLowerBound, const Point &aUpperBound)
Space::Vector Vector
Definition StdDefs.h:169
KSpace K
void naiveConvexHull(std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points, bool left_handed)
void viewPolygons(Viewer &viewer, const std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points)
void localDualVolume(std::vector< Vector > &points, std::map< Vector, bool > &f, const Vector &z)

References DGtal::Display3D< Space, KSpace >::allowReuseList, DGtal::HyperRectDomain< TSpace >::begin(), domain, DGtal::HyperRectDomain< TSpace >::end(), fillCfg(), DGtal::KhalimskySpaceND< dim, TInteger >::init(), K, lightBetween(), localDualVolume(), naiveConvexHull(), DGtal::Display3D< Space, KSpace >::newPolygonList(), DGtal::Display3D< Space, KSpace >::newQuadList(), DGtal::Display3D< Space, KSpace >::setCurrentList(), DGtal::PolyscopeViewer< Space, KSpace >::show(), DGtal::Surfaces< TKSpace >::uMakeBoundary(), DGtal::KhalimskySpaceND< dim, TInteger >::uTranslation(), and viewPolygons().

◆ naiveConvexHull()

template<typename Vector >
void naiveConvexHull ( std::vector< std::vector< unsigned int > > &  indices,
const std::vector< Vector > &  points,
bool  left_handed 
)
Examples
io/viewDualSurface.cpp.

Definition at line 80 of file viewDualSurface.cpp.

83{
84 typedef typename Vector::Component Scalar;
85 // Checks all triplets of points.
86 std::vector< unsigned int > aFace;
87 for ( unsigned int i1 = 0; i1 < points.size(); ++i1 )
88 for ( unsigned int i2 = 0; i2 < points.size(); ++i2 )
89 if ( i1 != i2 )
90 for ( unsigned int i3 = i1 > i2 ? i1+1 : i2+1; i3 < points.size(); ++i3 )
91 {
92 Vector P12 = points[ i2 ] - points[ i1 ];
93 Vector P13 = points[ i3 ] - points[ i1 ];
94 Vector N = wedge( P12, P13 );
95 if ( N == Vector::zero ) continue;
96
97 unsigned int nbBadPos = 0;
98 for ( unsigned int i4 = 0; i4 < points.size(); ++i4 )
99 {
100 Vector P14 = points[ i4 ] - points[ i1 ];
101 Scalar c = N.dot( P14 );
102 if ( c == 0 ) aFace.push_back( i4 );
103 else if ( ( left_handed && ( c > 0 ) )
104 || ( ! left_handed && ( c < 0 ) ) )
105 ++nbBadPos;
106 }
107 if ( nbBadPos == 0 )
108 {
109 LessThanOnFace<Vector> LTOF( N, points[ aFace[ 0 ] ], points );
110 std::sort( ++aFace.begin(), aFace.end(), LTOF );
111 indices.push_back( aFace );
112 }
113 aFace.clear();
114 }
115 // purge faces.
116 for ( unsigned int i = 0; i < indices.size(); ++i )
117 {
118 auto s = indices[ i ].size();
119 for ( unsigned int j = i+1; j < indices.size(); )
120 {
121 if ( indices[ j ].size() == s )
122 {
123 bool equal = true;
124 for ( unsigned int k = 0; equal && ( k < s ); ++k )
125 if ( indices[ i ][ k ] != indices[ j ][ k ] )
126 equal = false;
127 if ( equal )
128 {
129 std::swap( indices[ j ], indices.back() );
130 indices.pop_back();
131 }
132 else
133 ++j;
134 }
135 else ++j;
136 }
137 }
138}
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
Integer Component
Type for Vector elements.
static Self zero
Static const for zero PointVector.
Vector wedge(const Vector &v1, const Vector &v2)

References DGtal::PointVector< dim, TEuclideanRing, TContainer >::dot(), wedge(), and DGtal::PointVector< dim, Integer >::zero.

Referenced by main().

◆ nbLighted()

template<typename Vector >
unsigned int nbLighted ( std::map< Vector, bool > &  f,
const Vector z 
)
Examples
io/viewDualSurface.cpp.

Definition at line 204 of file viewDualSurface.cpp.

206{ // z of dim >=2
207 unsigned int d = dim( z );
208 if ( d == 0 ) return f[ z ] ? 1 : 0;
209 unsigned int i = openDim( z );
210 return nbLighted( f, lower( z, i ) )
211 + nbLighted( f, upper( z, i ) );
212}

References dim, lower(), nbLighted(), openDim(), and upper().

Referenced by lightMax(), and nbLighted().

◆ openDim()

template<typename Vector >
unsigned int openDim ( const Vector z)
Examples
io/viewDualSurface.cpp.

Definition at line 182 of file viewDualSurface.cpp.

183{
184 for ( unsigned int i = 0; i < Vector::dimension; ++i )
185 if ( ( z[ i ] % 2 ) == 1 ) return i;
186 return Vector::dimension;
187}

References DGtal::PointVector< dim, Integer >::dimension.

Referenced by fillCfg(), lightBetween(), lightEpsilon(), lightMax(), lightMaxMin(), lightMinMax(), and nbLighted().

◆ rescale()

double rescale ( double  x)
Examples
io/viewDualSurface.cpp.

Definition at line 140 of file viewDualSurface.cpp.

141{
142 return ( x - 1.0 ) * 0.5 + 0.5;
143}

Referenced by viewPolygons().

◆ upper()

◆ viewPolygons()

template<typename Viewer , typename Vector >
void viewPolygons ( Viewer viewer,
const std::vector< std::vector< unsigned int > > &  indices,
const std::vector< Vector > &  points 
)
Examples
io/viewDualSurface.cpp.

Definition at line 147 of file viewDualSurface.cpp.

151{
152 typedef typename Viewer::RealPoint RealPoint;
153 std::vector<RealPoint> pts3d;
154
155 for ( unsigned int f = 0; f < indices.size(); ++f )
156 {
157 pts3d.clear();
158 RealPoint P;
159 for ( unsigned int v = 0; v < indices[ f ].size(); ++v )
160 {
161 unsigned int i = indices[ f ][ v ];
162 P[0] = rescale( points[ i ][ 0 ] );
163 P[1] = rescale( points[ i ][ 1 ] );
164 P[2] = rescale( points[ i ][ 2 ] );
165 pts3d.push_back( P );
166 }
167
168 viewer.drawPolygon( pts3d );
169 }
170}
std::string drawPolygon(const std::vector< Vec > &vertices, const std::string &uname="Polygon_{i}")
Draws a polygon.
typename Space::RealPoint RealPoint
Definition Display3D.h:397
void clear()
Resets all the values to zero.
Space::RealPoint RealPoint
Definition StdDefs.h:170
double rescale(double x)

References DGtal::PointVector< dim, TEuclideanRing, TContainer >::clear(), DGtal::Display3D< Space, KSpace >::drawPolygon(), and rescale().

Referenced by main().

◆ wedge()

template<typename Vector >
Vector wedge ( const Vector v1,
const Vector v2 
)
Examples
io/viewDualSurface.cpp.

Definition at line 54 of file viewDualSurface.cpp.

55{
56 return Vector( v1[ 1 ] * v2[ 2 ] - v1[ 2 ] * v2[ 1 ],
57 v1[ 2 ] * v2[ 0 ] - v1[ 0 ] * v2[ 2 ],
58 v1[ 0 ] * v2[ 1 ] - v1[ 1 ] * v2[ 0 ] );
59}

Referenced by naiveConvexHull(), and LessThanOnFace< Vector >::operator()().