DGtal  0.9.2
generateSimplicityTables2D.cpp
1 
32 #include "DGtal/topology/tables/NeighborhoodTablesGenerators.h"
34 #include <vector>
35 #include "DGtal/shapes/Shapes.h"
36 #include "DGtal/io/boards/Board2D.h"
37 #include "DGtal/io/Color.h"
38 
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 
59 template <typename Object, typename Map>
60 void
61 displaySimplicityTable( Board2D & board,
62  const typename Object::DigitalTopology & /*dt*/,
63  const Map & map )
64 {
65  typedef typename Object::DigitalSet DigitalSet;
66  typedef typename Object::Point Point;
67  typedef typename DigitalSet::Domain Domain;
68  typedef typename Domain::ConstIterator DomainConstIterator;
69 
70  Point p1 = Point::diagonal( -1 );
71  Point p2 = Point::diagonal( 1 );
72  Point c = Point::diagonal( 0 );
73  Domain domain( p1, p2 );
74 
75  Point q1 = Point::diagonal( -1 );
76  Point q2 = Point::diagonal( 4*16-1 );
77  Domain fullDomain( q1, q2 );
78  board << SetMode( fullDomain.className(), "Paving" );
79  unsigned int cfg = 0;
80  for ( unsigned int y = 0; y < 16; ++y )
81  for ( unsigned int x = 0; x < 16; ++x, ++cfg )
82  {
83  bool simple = map[ cfg ];
84  Point base( x*4, y*4 );
85  unsigned int mask = 1;
86  for ( DomainConstIterator it = domain.begin();
87  it != domain.end(); ++it )
88  {
89  Point q = base + (*it);
90  if ( *it == c )
91  board << CustomStyle( q.className(),
92  new CustomColors( Color( 0, 0, 0 ),
93  Color( 30, 30, 30 ) ) );
94  else
95  {
96  if ( cfg & mask )
97  board <<
98  CustomStyle( q.className(),
99  simple
100  ? new CustomColors( Color( 0, 0, 0 ),
101  Color( 10, 255, 10 ) )
102  : new CustomColors( Color( 0, 0, 0 ),
103  Color( 255, 10, 10 ) ) );
104  else
105  board <<
106  CustomStyle( q.className(),
107  simple
108  ? new CustomColors( Color( 0, 0, 0 ),
109  Color( 245, 255, 245 ) )
110  : new CustomColors( Color( 0, 0, 0 ),
111  Color( 255, 245, 245 ) ) );
112  mask <<= 1;
113  }
114  board << q;
115  }
116  }
117 }
118 
122 template <typename Map>
123 void
124 outputTableAsArray( ostream & out,
125  const Map & map,
126  const string & tableName )
127 {
128  typedef typename Map::const_iterator MapConstIterator;
129  out << "const bool " << tableName << "[ " << map.size() << " ] = { ";
130  for ( MapConstIterator it = map.begin(), it_end = map.end();
131  it != it_end; )
132  {
133  out << *it;
134  ++it;
135  if ( it != it_end ) out << ", ";
136  }
137  out << " };" << std::endl;
138 }
139 
140 
141 int main( int /*argc*/, char** /*argv*/ )
142 {
143  typedef std::vector<bool> ConfigMap;
144 
145  using namespace Z2i;
146  trace.beginBlock ( "Generate 2d table for 4-8 topology" );
147  ConfigMap table4_8( 256 );
148  functions::generateSimplicityTable< Object4_8 >( dt4_8, table4_8 );
149  trace.endBlock();
150 
151  trace.beginBlock ( "Generate 2d table for 8-4 topology" );
152  ConfigMap table8_4( 256 );
153  functions::generateSimplicityTable< Object8_4 >( dt8_4, table8_4 );
154  trace.endBlock();
155 
156  Board2D board;
157  trace.beginBlock ( "Display 2d table for 4-8 topology" );
158  displaySimplicityTable< Object4_8 >( board, dt4_8, table4_8 );
159  board.saveEPS( "table4_8.eps" );
160  trace.endBlock();
161 
162  board.clear();
163  trace.beginBlock ( "Display 2d table for 8-4 topology" );
164  displaySimplicityTable< Object8_4 >( board, dt8_4, table8_4 );
165  board.saveEPS( "table8_4.eps" );
166  trace.endBlock();
167 
168  outputTableAsArray( std::cout, table4_8, "simplicityTable4_8" );
169  outputTableAsArray( std::cout, table8_4, "simplicityTable8_4" );
170 
171  /* Output bitset tables to files,
172  * for using with NeighborhoodConfigurations.h interface.
173  * These tables can be accesed from NeighborhoodTables.h header.
174  */
175  using ConfigMapBit = std::bitset<256> ; // 2^8
176  ConfigMapBit bit_table8_4;
177  ConfigMapBit bit_table4_8;
178  functions::generateSimplicityTable< Object8_4 >( dt8_4, bit_table8_4 );
179  functions::generateSimplicityTable< Object4_8 >( dt4_8, bit_table4_8 );
180  string filename = "simplicity_table8_4.txt";
181  ofstream file( filename );
182  file << bit_table8_4;
183  file.close();
184 
185  filename = "simplicity_table4_8.txt";
186  ofstream file2( filename );
187  file2 << bit_table4_8;
188  file2.close();
189 
190  return 0;
191 }
192 // //
void beginBlock(const std::string &keyword="")
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
const ConstIterator & begin() const
DigitalSet::Point Point
Definition: Object.h:141
const ConstIterator & end() const
Trace trace
Definition: Common.h:130
TDigitalSet DigitalSet
Definition: Object.h:123
STL namespace.
double endBlock()
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
TDigitalTopology DigitalTopology
Definition: Object.h:138
DGtal is the top-level namespace which contains all DGtal functions and types.
Modifier class in a Board2D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithBoard2D.
Definition: Board2D.h:247
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70