Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
cubical-complex-collapse.cpp
Go to the documentation of this file.
1
16
29
30
65
66
68#include <iostream>
69#include <map>
70#include "DGtal/base/Common.h"
71#include "DGtal/helpers/StdDefs.h"
72
73#include "DGtal/io/viewers/PolyscopeViewer.h"
74#include "DGtal/topology/KhalimskySpaceND.h"
75#include "DGtal/topology/CubicalComplex.h"
76
78
79using namespace std;
80using namespace DGtal;
81using namespace DGtal::Z3i;
82
83
89template <typename CubicalComplex>
92 typedef typename CubicalComplex::Point Point;
93 typedef typename CubicalComplex::Cell Cell;
95
96 DiagonalPriority( const CubicalComplex& complex ) : myComplex( complex ) {}
97 bool operator()( const CellMapIterator& it1, const CellMapIterator& it2 ) const
98 {
99 Point k1 = myComplex.space().uKCoords( it1->first );
100 Point k2 = myComplex.space().uKCoords( it2->first );
101 double d1 = Point::diagonal( 1 ).dot( k1 ) / sqrt( (double) KSpace::dimension );
102 double d2 = Point::diagonal( 1 ).dot( k2 ) / sqrt( (double) KSpace::dimension );;
103 RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
104 RealPoint v2( k2[ 0 ] - d2 * k2[ 0 ], k2[ 1 ] - d2 * k2[ 1 ], k2[ 2 ] - d2 * k2[ 2 ] );
105 double n1 = v1.dot( v1 );
106 double n2 = v2.dot( v2 );
107 return ( n1 < n2 ) || ( ( n1 == n2 ) && ( it1->first < it2->first ) );
108 }
109
111};
112
113
114int main( int argc, char** argv )
115{
116 // JOL: unordered_map is approximately twice faster than map for
117 // collapsing.
118 typedef std::map<Cell, CubicalCellData> Map;
119 // typedef boost::unordered_map<Cell, CubicalCellData> Map;
121
122 trace.beginBlock( "Creating Cubical Complex" );
123 KSpace K;
124 K.init( Point( 0,0,0 ), Point( 512,512,512 ), true );
125 CC complex( K );
126 Integer m = 40;
127 std::vector<Cell> S;
128 for ( Integer x = 0; x <= m; ++x )
129 for ( Integer y = 0; y <= m; ++y )
130 for ( Integer z = 0; z <= m; ++z )
131 {
132 Point k1 = Point( x, y, z );
133 S.push_back( K.uCell( k1 ) );
134 double d1 = Point::diagonal( 1 ).dot( k1 ) / (double) KSpace::dimension; // sqrt( (double) KSpace::dimension );
135 RealPoint v1( k1[ 0 ], k1[ 1 ], k1[ 2 ] );
136 v1 -= d1 * RealPoint::diagonal( 1.0 );
137 //RealPoint v1( k1[ 0 ] - d1 * k1[ 0 ], k1[ 1 ] - d1 * k1[ 1 ], k1[ 2 ] - d1 * k1[ 2 ] );
138 double n1 = v1.norm();
139 bool fixed = ( ( x == 0 ) && ( y == 0 ) && ( z == 0 ) )
140 || ( ( x == 0 ) && ( y == m ) && ( z == 0 ) )
141 || ( ( x == m ) && ( y == 0 ) && ( z == 0 ) )
142 || ( ( x == m ) && ( y == m ) && ( z == 0 ) )
143 || ( ( x == m/3 ) && ( y == 2*m/3 ) && ( z == 2*m/3 ) )
144 || ( ( x == 0 ) && ( y == 0 ) && ( z == m ) )
145 || ( ( x == 0 ) && ( y == m ) && ( z == m ) )
146 || ( ( x == m ) && ( y == 0 ) && ( z == m ) )
147 || ( ( x == m ) && ( y == m ) && ( z == m ) )
148 || ( ( x == 0 ) && ( y == m ) )
149 || ( ( x == m ) && ( y == m ) )
150 || ( ( z == 0 ) && ( y == m ) )
151 || ( ( z == m ) && ( y == m ) );
152 complex.insertCell( S.back(),
153 fixed ? CC::FIXED
154 : (DGtal::uint32_t) floor(64.0 * n1 ) // This is the priority for collapse
155 );
156 }
157 //complex.close();
158 trace.info() << "After close: " << complex << std::endl;
159 trace.endBlock();
160
161 // for 3D display with PolyscopeViewer
163
164 {
165 MyViewer viewer(K);
167 for ( Dimension d = 0; d <= 3; ++d )
168 for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
169 it != itE; ++it )
170 {
171 bool fixed = (it->second.data == CC::FIXED);
172 if ( fixed ) viewer.drawColor( Color::Red );
173 else viewer.drawColor( Color::White );
174 viewer << it->first;
175 }
176 viewer.show();
177 }
178
179 trace.beginBlock( "Collapsing complex" );
180 CC::DefaultCellMapIteratorPriority P;
181 DGtal::uint64_t removed
182 = functions::collapse( complex, S.begin(), S.end(), P, true, true, true );
183 trace.info() << "Collapse removed " << removed << " cells." << std::endl;
184 trace.info() << "After collapse: " << complex << std::endl;
185 trace.endBlock();
186
187 {
188 MyViewer viewer(K);
190 for ( Dimension d = 0; d <= 3; ++d )
191 for ( CellMapConstIterator it = complex.begin( d ), itE = complex.end( d );
192 it != itE; ++it )
193 {
194 bool fixed = (it->second.data == CC::FIXED);
195 if ( fixed ) viewer.drawColor( Color::Red );
196 else viewer.drawColor( Color::White );
197 viewer << it->first;
198 }
199 viewer.show();
200 return 0;
201 }
202}
203
static const Color Red
Definition Color.h:425
static const Color White
Definition Color.h:424
Aim: This class represents an arbitrary cubical complex living in some Khalimsky space....
TKSpace KSpace
Type of the cellular grid space.
KSpace::Cell Cell
Type for a cell in the space.
KSpace::Point Point
Type for a point in the digital space.
CellMap::iterator CellMapIterator
Iterator for visiting type CellMap.
CellMap::const_iterator CellMapConstIterator
void drawColor(const DGtal::Color &color)
static const constexpr Dimension dimension
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
double norm(const NormType type=L_2) const
static Self diagonal(Component val=1)
void show() override
Starts the event loop and display of elements.
Z3i this namespace gathers the standard of types for 3D imagery.
KhalimskySpaceND< 3, Integer > KSpace
Definition StdDefs.h:146
Space::RealPoint RealPoint
Definition StdDefs.h:170
Space::Point Point
Definition StdDefs.h:168
DGtal::int32_t Integer
Definition StdDefs.h:143
uint64_t collapse(CubicalComplex< TKSpace, TCellContainer > &K, CellConstIterator S_itB, CellConstIterator S_itE, const CellMapIteratorPriority &priority, bool hintIsSClosed=false, bool hintIsKClosed=false, bool verbose=false)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::uint64_t uint64_t
unsigned 64-bit integer.
Definition BasicTypes.h:64
std::uint32_t uint32_t
unsigned 32-bit integer.
Definition BasicTypes.h:62
DGtal::uint32_t Dimension
Definition Common.h:119
Trace trace
STL namespace.
CubicalComplex::KSpace KSpace
bool operator()(const CellMapIterator &it1, const CellMapIterator &it2) const
CubicalComplex::Point Point
CubicalComplex::Cell Cell
const CubicalComplex & myComplex
CubicalComplex::CellMapIterator CellMapIterator
DiagonalPriority(const CubicalComplex &complex)
int main()
Definition testBits.cpp:56
KSpace K
std::unordered_map< Cell, CubicalCellData > Map
CubicalComplex< KSpace, Map > CC
CC::CellMapConstIterator CellMapConstIterator
PolyscopeViewer< Space, KSpace > MyViewer