DGtal  0.9.3
generateSimplicityTables2D.cpp
Go to the documentation of this file.
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 
39 // For saving compressed tables.
40 #include <boost/iostreams/filtering_streambuf.hpp>
41 #include <boost/iostreams/copy.hpp>
42 #include <boost/iostreams/filter/zlib.hpp>
44 
45 using namespace std;
46 using namespace DGtal;
47 
49 
63 template <typename Object, typename Map>
64 void
66  const typename Object::DigitalTopology & /*dt*/,
67  const Map & map )
68 {
69  typedef typename Object::DigitalSet DigitalSet;
70  typedef typename Object::Point Point;
71  typedef typename DigitalSet::Domain Domain;
72  typedef typename Domain::ConstIterator DomainConstIterator;
73 
74  Point p1 = Point::diagonal( -1 );
75  Point p2 = Point::diagonal( 1 );
76  Point c = Point::diagonal( 0 );
77  Domain domain( p1, p2 );
78 
79  Point q1 = Point::diagonal( -1 );
80  Point q2 = Point::diagonal( 4*16-1 );
81  Domain fullDomain( q1, q2 );
82  board << SetMode( fullDomain.className(), "Paving" );
83  unsigned int cfg = 0;
84  for ( unsigned int y = 0; y < 16; ++y )
85  for ( unsigned int x = 0; x < 16; ++x, ++cfg )
86  {
87  bool simple = map[ cfg ];
88  Point base( x*4, y*4 );
89  unsigned int mask = 1;
90  for ( DomainConstIterator it = domain.begin();
91  it != domain.end(); ++it )
92  {
93  Point q = base + (*it);
94  if ( *it == c )
95  board << CustomStyle( q.className(),
96  new CustomColors( Color( 0, 0, 0 ),
97  Color( 30, 30, 30 ) ) );
98  else
99  {
100  if ( cfg & mask )
101  board <<
102  CustomStyle( q.className(),
103  simple
104  ? new CustomColors( Color( 0, 0, 0 ),
105  Color( 10, 255, 10 ) )
106  : new CustomColors( Color( 0, 0, 0 ),
107  Color( 255, 10, 10 ) ) );
108  else
109  board <<
110  CustomStyle( q.className(),
111  simple
112  ? new CustomColors( Color( 0, 0, 0 ),
113  Color( 245, 255, 245 ) )
114  : new CustomColors( Color( 0, 0, 0 ),
115  Color( 255, 245, 245 ) ) );
116  mask <<= 1;
117  }
118  board << q;
119  }
120  }
121 }
122 
126 template <typename Map>
127 void
128 outputTableAsArray( ostream & out,
129  const Map & map,
130  const string & tableName )
131 {
132  typedef typename Map::const_iterator MapConstIterator;
133  out << "const bool " << tableName << "[ " << map.size() << " ] = { ";
134  for ( MapConstIterator it = map.begin(), it_end = map.end();
135  it != it_end; )
136  {
137  out << *it;
138  ++it;
139  if ( it != it_end ) out << ", ";
140  }
141  out << " };" << std::endl;
142 }
143 
144 
145 int main( int /*argc*/, char** /*argv*/ )
146 {
147  typedef std::vector<bool> ConfigMap;
148 
149  using namespace Z2i;
150  trace.beginBlock ( "Generate 2d table for 4-8 topology" );
151  ConfigMap table4_8( 256 );
152  functions::generateSimplicityTable< Object4_8 >( dt4_8, table4_8 );
153  trace.endBlock();
154 
155  trace.beginBlock ( "Generate 2d table for 8-4 topology" );
156  ConfigMap table8_4( 256 );
157  functions::generateSimplicityTable< Object8_4 >( dt8_4, table8_4 );
158  trace.endBlock();
159 
160  Board2D board;
161  trace.beginBlock ( "Display 2d table for 4-8 topology" );
162  displaySimplicityTable< Object4_8 >( board, dt4_8, table4_8 );
163  board.saveEPS( "table4_8.eps" );
164  trace.endBlock();
165 
166  board.clear();
167  trace.beginBlock ( "Display 2d table for 8-4 topology" );
168  displaySimplicityTable< Object8_4 >( board, dt8_4, table8_4 );
169  board.saveEPS( "table8_4.eps" );
170  trace.endBlock();
171 
172  outputTableAsArray( std::cout, table4_8, "simplicityTable4_8" );
173  outputTableAsArray( std::cout, table8_4, "simplicityTable8_4" );
174 
175  /* Output bitset tables to files,
176  * for using with NeighborhoodConfigurations.h interface.
177  * These tables can be accesed from NeighborhoodTables.h header.
178  */
179  using ConfigMapBit = std::bitset<256> ; // 2^8
180  ConfigMapBit bit_table8_4;
181  ConfigMapBit bit_table4_8;
182  functions::generateSimplicityTable< Object8_4 >( dt8_4, bit_table8_4 );
183  functions::generateSimplicityTable< Object4_8 >( dt4_8, bit_table4_8 );
184  // string filename = "simplicity_table8_4.txt";
185  // ofstream file1( filename );
186  // file1 << bit_table8_4;
187  // file1.close();
188  {
189  string filename = "simplicity_table8_4.zlib";
190  ofstream file( filename );
191  ConfigMapBit* table = &bit_table8_4;
192  std::stringstream table_stream;
193  table_stream << *table;
194  namespace io = boost::iostreams;
195  io::filtering_streambuf<io::input> filter;
196  filter.push(io::zlib_compressor());
197  filter.push(table_stream);
198  io::copy(filter,file);
199  file.close();
200  }
201 
202  // string filename = "simplicity_table4_8.txt";
203  // ofstream file2( filename );
204  // file2 << bit_table4_8;
205  // file2.close();
206  {
207  string filename = "simplicity_table4_8.zlib";
208  ofstream file( filename );
209  ConfigMapBit* table = &bit_table4_8;
210  std::stringstream table_stream;
211  table_stream << *table;
212  namespace io = boost::iostreams;
213  io::filtering_streambuf<io::input> filter;
214  filter.push(io::zlib_compressor());
215  filter.push(table_stream);
216  io::copy(filter,file);
217  file.close();
218  }
219 
220  return 0;
221 }
222 // //
void beginBlock(const std::string &keyword="")
const ConstIterator & end() const
std::unordered_map< Cell, CubicalCellData > Map
HyperRectDomain< Space > Domain
DigitalSet::Point Point
Definition: Object.h:141
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
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
void outputTableAsArray(ostream &out, const Map &map, const string &tableName)
const ConstIterator & begin() const
void displaySimplicityTable(Board2D &board, const typename Object::DigitalTopology &, const Map &map)
std::string className() const
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
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
int main(int, char **)
Aim: This class specializes a &#39;Board&#39; class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70