DGtal  1.1.0
cubical-complex-collapse.cpp
Go to the documentation of this file.
1
67 #include <iostream>
69 #include <map>
70 #include "DGtal/base/Common.h"
71 #include "DGtal/helpers/StdDefs.h"
72
73 #include "DGtal/io/DrawWithDisplay3DModifier.h"
74 #include "DGtal/io/viewers/Viewer3D.h"
75 #include "DGtal/topology/KhalimskySpaceND.h"
76 #include "DGtal/topology/CubicalComplex.h"
77
79
80 using namespace std;
81 using namespace DGtal;
82 using namespace DGtal::Z3i;
83
84
90 template <typename CubicalComplex>
91 struct DiagonalPriority {
92  typedef typename CubicalComplex::KSpace KSpace;
93  typedef typename CubicalComplex::Point Point;
94  typedef typename CubicalComplex::Cell Cell;
95  typedef typename CubicalComplex::CellMapIterator CellMapIterator;
96
97  DiagonalPriority( const CubicalComplex& complex ) : myComplex( complex ) {}
98  bool operator()( const CellMapIterator& it1, const CellMapIterator& it2 ) const
99  {
100  Point k1 = myComplex.space().uKCoords( it1->first );
101  Point k2 = myComplex.space().uKCoords( it2->first );
102  double d1 = Point::diagonal( 1 ).dot( k1 ) / sqrt( (double) KSpace::dimension );
103  double d2 = Point::diagonal( 1 ).dot( k2 ) / sqrt( (double) KSpace::dimension );;
104  RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
105  RealPoint v2( k2[ 0 ] - d2 * k2[ 0 ], k2[ 1 ] - d2 * k2[ 1 ], k2[ 2 ] - d2 * k2[ 2 ] );
106  double n1 = v1.dot( v1 );
107  double n2 = v2.dot( v2 );
108  return ( n1 < n2 ) || ( ( n1 == n2 ) && ( it1->first < it2->first ) );
109  }
110
111  const CubicalComplex& myComplex;
112 };
114
115 int main( int argc, char** argv )
116 {
117  // JOL: unordered_map is approximately twice faster than map for
118  // collapsing.
119  typedef std::map<Cell, CubicalCellData> Map;
120  // typedef boost::unordered_map<Cell, CubicalCellData> Map;
122
123  trace.beginBlock( "Creating Cubical Complex" );
124  KSpace K;
125  K.init( Point( 0,0,0 ), Point( 512,512,512 ), true );
126  CC complex( K );
127  Integer m = 40;
128  std::vector<Cell> S;
129  for ( Integer x = 0; x <= m; ++x )
130  for ( Integer y = 0; y <= m; ++y )
131  for ( Integer z = 0; z <= m; ++z )
132  {
133  Point k1 = Point( x, y, z );
134  S.push_back( K.uCell( k1 ) );
135  double d1 = Point::diagonal( 1 ).dot( k1 ) / (double) KSpace::dimension; // sqrt( (double) KSpace::dimension );
136  RealPoint v1( k1[ 0 ], k1[ 1 ], k1[ 2 ] );
137  v1 -= d1 * RealPoint::diagonal( 1.0 );
138  //RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
139  double n1 = v1.norm();
140  bool fixed = ( ( x == 0 ) && ( y == 0 ) && ( z == 0 ) )
141  || ( ( x == 0 ) && ( y == m ) && ( z == 0 ) )
142  || ( ( x == m ) && ( y == 0 ) && ( z == 0 ) )
143  || ( ( x == m ) && ( y == m ) && ( z == 0 ) )
144  || ( ( x == m/3 ) && ( y == 2*m/3 ) && ( z == 2*m/3 ) )
145  || ( ( x == 0 ) && ( y == 0 ) && ( z == m ) )
146  || ( ( x == 0 ) && ( y == m ) && ( z == m ) )
147  || ( ( x == m ) && ( y == 0 ) && ( z == m ) )
148  || ( ( x == m ) && ( y == m ) && ( z == m ) )
149  || ( ( x == 0 ) && ( y == m ) )
150  || ( ( x == m ) && ( y == m ) )
151  || ( ( z == 0 ) && ( y == m ) )
152  || ( ( z == m ) && ( y == m ) );
153  complex.insertCell( S.back(),
154  fixed ? CC::FIXED
155  : (DGtal::uint32_t) floor(64.0 * n1 ) // This is the priority for collapse
156  );
157  }
158  //complex.close();
159  trace.info() << "After close: " << complex << std::endl;
160  trace.endBlock();
161
162  // for 3D display with Viewer3D
163  QApplication application(argc,argv);
165
166  {
167  MyViewer viewer(K);
168  viewer.show();
170  for ( Dimension d = 0; d <= 3; ++d )
171  for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
172  it != itE; ++it )
173  {
174  bool fixed = (it->second.data == CC::FIXED);
175  if ( fixed ) viewer.setFillColor( Color::Red );
176  else viewer.setFillColor( Color::White );
177  viewer << it->first;
178  }
179  viewer<< MyViewer::updateDisplay;
180  application.exec();
181  }
182
183  trace.beginBlock( "Collapsing complex" );
185  DGtal::uint64_t removed
186  = functions::collapse( complex, S.begin(), S.end(), P, true, true, true );
187  trace.info() << "Collapse removed " << removed << " cells." << std::endl;
188  trace.info() << "After collapse: " << complex << std::endl;
189  trace.endBlock();
190
191  {
192  MyViewer viewer(K);
193  viewer.show();
195  for ( Dimension d = 0; d <= 3; ++d )
196  for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
197  it != itE; ++it )
198  {
199  bool fixed = (it->second.data == CC::FIXED);
200  if ( fixed ) viewer.setFillColor( Color::Red );
201  else viewer.setFillColor( Color::White );
202  viewer << it->first;
203  }
204  viewer<< MyViewer::updateDisplay;
205  return application.exec();
206  }
207 }
DGtal::KhalimskySpaceND::dimension
static constexpr const Dimension dimension
Definition: KhalimskySpaceND.h:430
KSpace
Z3i::KSpace KSpace
Definition: sphereCotangentLaplaceOperator.cpp:70
DGtal::CubicalComplex::CellMapIterator
CellMap::iterator CellMapIterator
Iterator for visiting type CellMap.
Definition: CubicalComplex.h:253
DGtal::KhalimskySpaceND::uCell
Cell uCell(const PreCell &c) const
From an unsigned cell, returns an unsigned cell lying into this Khalismky space.
DGtal::Trace::endBlock
double endBlock()
DGtal::uint32_t
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
DGtal::Z3i::Integer
DGtal::int32_t Integer
Definition: StdDefs.h:143
DGtal::KhalimskySpaceND::init
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
CellMapConstIterator
CC::CellMapConstIterator CellMapConstIterator
Definition: testCubicalComplex.cpp:59
DGtal::CubicalComplex::begin
ConstIterator begin() const
DGtal::trace
Trace trace
Definition: Common.h:150
K
KSpace K
Definition: testCubicalComplex.cpp:62
DGtal::Dimension
DGtal::uint32_t Dimension
Definition: Common.h:133
DGtal::CubicalComplex::CellMapConstIterator
CellMap::const_iterator CellMapConstIterator
Const iterator for visiting type CellMap.
Definition: CubicalComplex.h:252
DGtal::Display3D::updateDisplay
@ updateDisplay
Definition: Display3D.h:249
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::CubicalComplex::insertCell
void insertCell(const Cell &aCell, const Data &data=Data())
DGtal::PointVector::diagonal
static Self diagonal(Component val=1)
Map
std::unordered_map< Cell, CubicalCellData > Map
Definition: testCubicalComplex.cpp:57
DGtal::CubicalComplex::KSpace
TKSpace KSpace
Type of the cellular grid space.
Definition: CubicalComplex.h:233
DGtal::Trace::info
std::ostream & info()
DGtal::Viewer3D< Space, KSpace >
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
main
int main(int argc, char **argv)
Definition: cubical-complex-collapse.cpp:115
DGtal::Z3i
Z3i this namespace gathers the standard of types for 3D imagery.
DGtal::uint64_t
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
CC
CubicalComplex< KSpace, Map > CC
Definition: testCubicalComplex.cpp:58
DGtal::Display3D::setFillColor
virtual void setFillColor(DGtal::Color aColor)
DGtal::PointVector::dot
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
DGtal::PointVector< dim, Integer >
DGtal::Viewer3D::show
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
Cell
KSpace::Cell Cell
Definition: testCubicalComplex.cpp:56
DGtal::CubicalComplex::end
ConstIterator end() const
DGtal::CubicalComplex
Aim: This class represents an arbitrary cubical complex living in some Khalimsky space....
Definition: CubicalComplex.h:176
Point
MyPointD Point
Definition: testClone2.cpp:383
DGtal::KhalimskyCell< dim, Integer >
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:394