DGtal  1.5.beta
generateSimplicityTables2D.cpp File Reference
#include "DGtal/topology/tables/NeighborhoodTablesGenerators.h"
#include <vector>
#include "DGtal/shapes/Shapes.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/io/Color.h"
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
Include dependency graph for generateSimplicityTables2D.cpp:

Go to the source code of this file.

Functions

template<typename Object , typename Map >
void displaySimplicityTable (Board2D &board, const typename Object::DigitalTopology &, const Map &map)
 
template<typename Map >
void outputTableAsArray (ostream &out, const Map &map, const string &tableName)
 
int main (int, char **)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2012/06/22

An example file named generateSimplicityTables2D. Creates precomputed tables for determining whether some point is simple within an object.

This file is part of the DGtal library.

Definition in file generateSimplicityTables2D.cpp.

Function Documentation

◆ displaySimplicityTable()

template<typename Object , typename Map >
void displaySimplicityTable ( Board2D board,
const typename Object::DigitalTopology ,
const Map map 
)

Given a digital topology dt and a mapping configuration -> bool map, displays the simplicity tables in the given board.

Template Parameters
Objectthe type of object whose simpleness we wish to precompute. Includes the topology.
Mapthe type used to store the mapping configuration -> bool.
Parameters
board(updated) the board where the tables are displayed. UNUSED_PARAM dt an instance of the digital topology.
mapthe mapping configuration -> bool.

Definition at line 65 of file generateSimplicityTables2D.cpp.

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 }
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
Iterator for HyperRectDomain.
const ConstIterator & end() const
const ConstIterator & begin() const
TDigitalSet DigitalSet
Definition: Object.h:123
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
HyperRectDomain< Space > Domain
Z2i::DigitalSet DigitalSet

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::HyperRectDomain< TSpace >::className(), domain, and DGtal::HyperRectDomain< TSpace >::end().

◆ main()

int main ( int  ,
char **   
)

Definition at line 145 of file generateSimplicityTables2D.cpp.

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 }
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
void beginBlock(const std::string &keyword="")
double endBlock()
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:151
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:804
std::vector< bool > ConfigMap
void outputTableAsArray(ostream &out, const Map &map, const string &tableName)
Trace trace
Definition: Common.h:153

References DGtal::Trace::beginBlock(), LibBoard::Board::clear(), DGtal::Trace::endBlock(), outputTableAsArray(), LibBoard::Board::saveEPS(), and DGtal::trace.

◆ outputTableAsArray()

template<typename Map >
void outputTableAsArray ( ostream &  out,
const Map map,
const string &  tableName 
)

Output simplicity configuration table as a C(++) array.

Definition at line 128 of file generateSimplicityTables2D.cpp.

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 }

Referenced by main().