DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
generateSimplicityTables2D.cpp
1 
32 
33 #include <iostream>
34 #include <vector>
35 #include <bitset>
36 #include "DGtal/topology/Object.h"
37 #include "DGtal/shapes/Shapes.h"
38 #include "DGtal/helpers/StdDefs.h"
39 #include "DGtal/io/boards/Board2D.h"
40 #include "DGtal/io/Color.h"
41 
43 
44 using namespace std;
45 using namespace DGtal;
46 
48 
64 template <typename Object, typename Map>
65 void
66 generateSimplicityTable( const typename Object::DigitalTopology & dt,
67  Map & map )
68 {
70  typedef typename Object::DigitalSet DigitalSet;
71  typedef typename Object::Point Point;
72  typedef typename DigitalSet::Domain Domain;
73  typedef typename Domain::ConstIterator DomainConstIterator;
74 
75  Point p1 = Point::diagonal( -1 );
76  Point p2 = Point::diagonal( 1 );
77  Point c = Point::diagonal( 0 );
78  Domain domain( p1, p2 );
79  DigitalSet shapeSet( domain );
80  Object shape( dt, shapeSet );
81  unsigned int k = 0;
82  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
83  if ( *it != c ) ++k;
84  ASSERT( ( k < 32 )
85  && "[generateSimplicityTable] number of configurations is too high." );
86  unsigned int nbCfg = 1 << k;
87  for ( unsigned int cfg = 0; cfg < nbCfg; ++cfg )
88  {
89  shape.pointSet().clear();
90  shape.pointSet().insert( c );
91  unsigned int mask = 1;
92  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
93  {
94  if ( *it != c )
95  {
96  if ( cfg & mask ) shape.pointSet().insert( *it );
97  mask <<= 1;
98  }
99  }
100  bool simple = shape.isSimple( c );
101  map[ cfg ] = simple;
102  }
103 }
104 
118 template <typename Object, typename Map>
119 void
120 displaySimplicityTable( Board2D & board,
121  const typename Object::DigitalTopology & dt,
122  const Map & map )
123 {
124  typedef typename Object::DigitalTopology DigitalTopology;
125  typedef typename Object::DigitalSet DigitalSet;
126  typedef typename Object::Point Point;
127  typedef typename DigitalSet::Domain Domain;
128  typedef typename Domain::ConstIterator DomainConstIterator;
129 
130  Point p1 = Point::diagonal( -1 );
131  Point p2 = Point::diagonal( 1 );
132  Point c = Point::diagonal( 0 );
133  Domain domain( p1, p2 );
134 
135  Point q1 = Point::diagonal( -1 );
136  Point q2 = Point::diagonal( 4*16-1 );
137  Domain fullDomain( q1, q2 );
138  board << SetMode( fullDomain.className(), "Paving" );
139  unsigned int cfg = 0;
140  for ( unsigned int y = 0; y < 16; ++y )
141  for ( unsigned int x = 0; x < 16; ++x, ++cfg )
142  {
143  bool simple = map[ cfg ];
144  Point base( x*4, y*4 );
145  unsigned int mask = 1;
146  for ( DomainConstIterator it = domain.begin();
147  it != domain.end(); ++it )
148  {
149  Point q = base + (*it);
150  if ( *it == c )
151  board << CustomStyle( q.className(),
152  new CustomColors( Color( 0, 0, 0 ),
153  Color( 30, 30, 30 ) ) );
154  else
155  {
156  if ( cfg & mask )
157  board <<
158  CustomStyle( q.className(),
159  simple
160  ? new CustomColors( Color( 0, 0, 0 ),
161  Color( 10, 255, 10 ) )
162  : new CustomColors( Color( 0, 0, 0 ),
163  Color( 255, 10, 10 ) ) );
164  else
165  board <<
166  CustomStyle( q.className(),
167  simple
168  ? new CustomColors( Color( 0, 0, 0 ),
169  Color( 245, 255, 245 ) )
170  : new CustomColors( Color( 0, 0, 0 ),
171  Color( 255, 245, 245 ) ) );
172  mask <<= 1;
173  }
174  board << q;
175  }
176  }
177 }
178 
182 template <typename Map>
183 void
184 outputTableAsArray( ostream & out,
185  const Map & map,
186  const string & tableName )
187 {
188  typedef typename Map::const_iterator MapConstIterator;
189  out << "const bool " << tableName << "[ " << map.size() << " ] = { ";
190  for ( MapConstIterator it = map.begin(), it_end = map.end();
191  it != it_end; )
192  {
193  out << *it;
194  ++it;
195  if ( it != it_end ) out << ", ";
196  }
197  out << " };" << std::endl;
198 }
199 
200 
201 int main( int argc, char** argv )
202 {
203  typedef std::vector<bool> ConfigMap;
204 
205  using namespace Z2i;
206  trace.beginBlock ( "Generate 2d table for 4-8 topology" );
207  ConfigMap table4_8( 256 );
208  generateSimplicityTable< Object4_8 >( dt4_8, table4_8 );
209  trace.endBlock();
210 
211  trace.beginBlock ( "Generate 2d table for 8-4 topology" );
212  ConfigMap table8_4( 256 );
213  generateSimplicityTable< Object8_4 >( dt8_4, table8_4 );
214  trace.endBlock();
215 
216  Board2D board;
217  trace.beginBlock ( "Display 2d table for 4-8 topology" );
218  displaySimplicityTable< Object4_8 >( board, dt4_8, table4_8 );
219  board.saveEPS( "table4_8.eps" );
220  trace.endBlock();
221 
222  board.clear();
223  trace.beginBlock ( "Display 2d table for 8-4 topology" );
224  displaySimplicityTable< Object8_4 >( board, dt8_4, table8_4 );
225  board.saveEPS( "table8_4.eps" );
226  trace.endBlock();
227 
228  outputTableAsArray( std::cout, table4_8, "simplicityTable4_8" );
229  outputTableAsArray( std::cout, table8_4, "simplicityTable8_4" );
230 
231  return 0;
232 }
233 // //