DGtal  0.9.2
cubical-complex-collapse.cpp
1
30 #include <iostream>
32 #include <map>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35
36 #include "DGtal/io/DrawWithDisplay3DModifier.h"
37 #include "DGtal/io/viewers/Viewer3D.h"
38 #include "DGtal/topology/KhalimskySpaceND.h"
39 #include "DGtal/topology/CubicalComplex.h"
40 #include "DGtal/topology/CubicalComplexFunctions.h"
41
43
44 using namespace std;
45 using namespace DGtal;
46 using namespace DGtal::Z3i;
47
48
54 template <typename CubicalComplex>
55 struct DiagonalPriority {
56  typedef typename CubicalComplex::KSpace KSpace;
57  typedef typename CubicalComplex::Point Point;
58  typedef typename CubicalComplex::Cell Cell;
59  typedef typename CubicalComplex::CellMapIterator CellMapIterator;
60
61  DiagonalPriority( const CubicalComplex& complex ) : myComplex( complex ) {}
62  bool operator()( const CellMapIterator& it1, const CellMapIterator& it2 ) const
63  {
64  Point k1 = myComplex.space().uKCoords( it1->first );
65  Point k2 = myComplex.space().uKCoords( it2->first );
66  double d1 = Point::diagonal( 1 ).dot( k1 ) / sqrt( (double) KSpace::dimension );
67  double d2 = Point::diagonal( 1 ).dot( k2 ) / sqrt( (double) KSpace::dimension );;
68  RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
69  RealPoint v2( k2[ 0 ] - d2 * k2[ 0 ], k2[ 1 ] - d2 * k2[ 1 ], k2[ 2 ] - d2 * k2[ 2 ] );
70  double n1 = v1.dot( v1 );
71  double n2 = v2.dot( v2 );
72  return ( n1 < n2 ) || ( ( n1 == n2 ) && ( it1->first < it2->first ) );
73  }
74
75  const CubicalComplex& myComplex;
76 };
78
79 int main( int argc, char** argv )
80 {
81  // JOL: unordered_map is approximately twice faster than map for
82  // collapsing.
83  typedef std::map<Cell, CubicalCellData> Map;
84  // typedef boost::unordered_map<Cell, CubicalCellData> Map;
86
87  trace.beginBlock( "Creating Cubical Complex" );
88  KSpace K;
89  K.init( Point( 0,0,0 ), Point( 512,512,512 ), true );
90  CC complex( K );
91  Integer m = 40;
92  std::vector<Cell> S;
93  for ( Integer x = 0; x <= m; ++x )
94  for ( Integer y = 0; y <= m; ++y )
95  for ( Integer z = 0; z <= m; ++z )
96  {
97  Point k1 = Point( x, y, z );
98  S.push_back( K.uCell( k1 ) );
99  double d1 = Point::diagonal( 1 ).dot( k1 ) / (double) KSpace::dimension; // sqrt( (double) KSpace::dimension );
100  RealPoint v1( k1[ 0 ], k1[ 1 ], k1[ 2 ] );
101  v1 -= d1 * RealPoint::diagonal( 1.0 );
102  //RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
103  double n1 = v1.norm();
104  bool fixed = ( ( x == 0 ) && ( y == 0 ) && ( z == 0 ) )
105  || ( ( x == 0 ) && ( y == m ) && ( z == 0 ) )
106  || ( ( x == m ) && ( y == 0 ) && ( z == 0 ) )
107  || ( ( x == m ) && ( y == m ) && ( z == 0 ) )
108  || ( ( x == m/3 ) && ( y == 2*m/3 ) && ( z == 2*m/3 ) )
109  || ( ( x == 0 ) && ( y == 0 ) && ( z == m ) )
110  || ( ( x == 0 ) && ( y == m ) && ( z == m ) )
111  || ( ( x == m ) && ( y == 0 ) && ( z == m ) )
112  || ( ( x == m ) && ( y == m ) && ( z == m ) )
113  || ( ( x == 0 ) && ( y == m ) )
114  || ( ( x == m ) && ( y == m ) )
115  || ( ( z == 0 ) && ( y == m ) )
116  || ( ( z == m ) && ( y == m ) );
117  complex.insertCell( S.back(),
118  fixed ? CC::FIXED
119  : (DGtal::uint32_t) floor(64.0 * n1 ) // This is the priority for collapse
120  );
121  }
122  //complex.close();
123  trace.info() << "After close: " << complex << std::endl;
124  trace.endBlock();
125
126  // for 3D display with Viewer3D
127  QApplication application(argc,argv);
129
130  {
131  MyViewer viewer(K);
132  viewer.show();
133  typedef CC::CellMapConstIterator CellMapConstIterator;
134  for ( Dimension d = 0; d <= 3; ++d )
135  for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
136  it != itE; ++it )
137  {
138  bool fixed = (it->second.data == CC::FIXED);
139  if ( fixed ) viewer.setFillColor( Color::Red );
140  else viewer.setFillColor( Color::White );
141  viewer << it->first;
142  }
143  viewer<< MyViewer::updateDisplay;
144  application.exec();
145  }
146
147  trace.beginBlock( "Collapsing complex" );
149  DGtal::uint64_t removed
150  = functions::collapse( complex, S.begin(), S.end(), P, true, true, true );
151  trace.info() << "Collapse removed " << removed << " cells." << std::endl;
152  trace.info() << "After collapse: " << complex << std::endl;
153  trace.endBlock();
154
155  {
156  MyViewer viewer(K);
157  viewer.show();
158  typedef CC::CellMapConstIterator CellMapConstIterator;
159  for ( Dimension d = 0; d <= 3; ++d )
160  for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
161  it != itE; ++it )
162  {
163  bool fixed = (it->second.data == CC::FIXED);
164  if ( fixed ) viewer.setFillColor( Color::Red );
165  else viewer.setFillColor( Color::White );
166  viewer << it->first;
167  }
168  viewer<< MyViewer::updateDisplay;
169  return application.exec();
170  }
171 }
void beginBlock(const std::string &keyword="")
CellMap::iterator CellMapIterator
Iterator for visiting type CellMap.
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
Z3i this namespace gathers the standard of types for 3D imagery.
Trace trace
Definition: Common.h:130
DGtal::uint32_t Dimension
Definition: Common.h:113
STL namespace.
double endBlock()
TKSpace KSpace
Type of the cellular grid space.
CellMap::const_iterator CellMapConstIterator
Const iterator for visiting type CellMap.
Cell uCell(const PreCell &c) const
bool init(const Point &lower, const Point &upper, bool isClosed)
DGtal::int32_t Integer
Definition: StdDefs.h:143
Aim: This class represents an arbitrary cubical complex living in some Khalimsky space. Cubical complexes are sets of cells of different dimensions related together with incidence relations. Two cells in a cubical complex are incident if and only if they are incident in the surrounding Khalimsky space. In other words, cubical complexes are defined here as subsets of Khalimsky spaces.
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...