DGtal  1.4.beta
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/Viewer3D.h"
Include dependency graph for viewDualSurface.cpp:

Go to the source code of this file.

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 DGtal::Color &color, 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()

◆ fillCfg()

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

Definition at line 337 of file viewDualSurface.cpp.

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

Referenced by main().

◆ lightBetween()

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

Definition at line 218 of file viewDualSurface.cpp.

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

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

Referenced by 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 309 of file viewDualSurface.cpp.

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

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

◆ lightMax()

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

Definition at line 253 of file viewDualSurface.cpp.

255 {
256  unsigned int d = dim( z );
257  if ( d == 0 ) return f[ z ];
258  else if ( d == 1 )
259  {
260  unsigned int i = openDim( z );
261  return f[ lower( z, i ) ] || f[ upper( z, i ) ];
262  }
263  else // if ( d > 1 )
264  {
265  unsigned int n = nbLighted( f, z );
266  return n >= 2;
267  }
268 }
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 289 of file viewDualSurface.cpp.

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

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

◆ lightMinMax()

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

Definition at line 270 of file viewDualSurface.cpp.

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

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

◆ 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 366 of file viewDualSurface.cpp.

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

References DGtal::HyperRectDomain< TSpace >::begin(), domain, and DGtal::HyperRectDomain< TSpace >::end().

Referenced by main().

◆ lower()

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 392 of file viewDualSurface.cpp.

393 {
394  typedef KSpace::CellSet CellSet;
395  QApplication application(argc,argv);
396 
397  KSpace KS;
398 
400  viewer.show();
401  DGtal::Color fillColor( 200, 200, 220, 255 );
402  DGtal::Color surfelColor( 255, 0, 0, 150 );
403  DGtal::Color voxelColor( 150, 150, 0, 150 );
404 
405  std::vector<Vector> pts;
406 
407  unsigned int cfg = argc > 1 ? atoi( argv[1] ) : 0;
408  unsigned int cfg2 = argc > 2 ? atoi( argv[2] ) : 255;
409  std::map< Vector, bool > f;
410  for ( unsigned int y = 0; (y < 16) && (cfg <= cfg2); ++y )
411  for ( unsigned int x = 0; (x < 16) && (cfg <= cfg2); ++x, ++cfg )
412  {
413  Vector offset( x*6, y*6, 0 );
414  fillCfg( f, offset + Vector( 1, 1, 1 ), cfg );
415  Domain domain( offset + Vector( 0, 0, 0), offset + Vector( 2, 2, 2 ) );
416  KSpace K;
417  K.init( Vector( 0, 0, 0), Vector( 2, 2, 2 ), true );
418  ConfigPointPredicate<Vector> cpp( f, offset );
419  CellSet aBoundary;
420  Surfaces<KSpace>::uMakeBoundary( aBoundary, K, cpp, Vector( 0, 0, 0), Vector( 1, 1, 1 ) );
421  for ( CellSet::const_iterator it = aBoundary.begin(), itE = aBoundary.end();
422  it != itE; ++it )
423  {
424  viewer << CustomColors3D( surfelColor, surfelColor );
425  viewer << KS.uTranslation( *it, offset/2 );
426  }
427  for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
428  it != itE; ++it )
429  {
430  // lightEpsilon( f, *it, 5 ); // {1,-1,1}=5 // interesting
431  f[ *it ] = lightBetween( f, *it );
432  }
433  viewer << CustomColors3D( DGtal::Color( 255, 0, 0, 255 ), fillColor );
434  std::vector< std::vector< unsigned int > > indices;
435  Domain domain2( offset + Vector( 0, 0, 0), offset + Vector( 1, 1, 1 ) );
436 
437  for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
438  it != itE; ++it )
439  {
440  localDualVolume( pts, f, *it );
441  indices.clear();
442  naiveConvexHull( indices, pts, false ); // right_handed
443  viewPolygons( viewer, fillColor, indices, pts );
444  }
445  }
446  viewer << Viewer3D<>::updateDisplay;
447 
448  return application.exec();
449 }
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
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).
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:79
DigitalPlane::Point Vector
KSpace K
void naiveConvexHull(std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points, bool left_handed)
void viewPolygons(Viewer &viewer, const DGtal::Color &color, 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::HyperRectDomain< TSpace >::begin(), domain, DGtal::HyperRectDomain< TSpace >::end(), fillCfg(), DGtal::KhalimskySpaceND< dim, TInteger >::init(), K, lightBetween(), localDualVolume(), naiveConvexHull(), DGtal::Viewer3D< TSpace, TKSpace >::show(), 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.
Definition: PointVector.h:614
Vector wedge(const Vector &v1, const Vector &v2)

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

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 205 of file viewDualSurface.cpp.

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

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

Referenced by lightMax().

◆ openDim()

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

Definition at line 183 of file viewDualSurface.cpp.

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

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 DGtal::Color color,
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.

152 {
153  typedef typename Viewer::RealPoint RealPoint;
154  std::vector<RealPoint> pts3d;
155  DGtal::Color fillColorSave = viewer.getFillColor();
156  for ( unsigned int f = 0; f < indices.size(); ++f )
157  {
158  pts3d.clear();
159  RealPoint P;
160  for ( unsigned int v = 0; v < indices[ f ].size(); ++v )
161  {
162  unsigned int i = indices[ f ][ v ];
163  P[0] = rescale( points[ i ][ 0 ] );
164  P[1] = rescale( points[ i ][ 1 ] );
165  P[2] = rescale( points[ i ][ 2 ] );
166  pts3d.push_back( P );
167  }
168  viewer.setFillColor(color);
169  viewer.addPolygon( pts3d );
170  }
171 }
virtual DGtal::Color getFillColor()
virtual void setFillColor(DGtal::Color aColor)
void addPolygon(const std::vector< RealPoint > &vertices)
PointVector< 3, double > RealPoint
double rescale(double x)

References DGtal::Display3D< Space, KSpace >::addPolygon(), DGtal::Display3D< Space, KSpace >::getFillColor(), rescale(), and DGtal::Display3D< Space, KSpace >::setFillColor().

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().