DGtal  1.5.beta
fullConvexityLUT2D.cpp
Go to the documentation of this file.
1 
59 #include <vector>
60 #include <fstream>
61 #include "DGtal/shapes/Shapes.h"
62 #include "DGtal/io/boards/Board2D.h"
63 #include "DGtal/io/Color.h"
64 #include "DGtal/geometry/volumes/DigitalConvexity.h"
65 #include "DGtal/geometry/volumes/NeighborhoodConvexityAnalyzer.h"
66 #include "DGtal/helpers/Shortcuts.h"
67 #include "ConfigExamples.h"
68 
69 // Using standard 2D digital space.
71 
72 using namespace std;
73 using namespace DGtal;
74 using namespace Z2i;
75 
76 typedef std::vector<bool> ConfigMap;
80 
82 
85 void
86 outputTableAsArray( ostream & out,
87  const string & tableName,
88  const ConfigMap & map )
89 {
90  out << "const bool " << tableName << "[ " << map.size() << " ] = { ";
91  for ( auto it = map.cbegin(), it_end = map.cend();
92  it != it_end; )
93  {
94  out << (int) *it;
95  ++it;
96  if ( it != it_end ) out << ", ";
97  }
98  out << " };" << std::endl;
99 }
100 
101 void
103  const ConfigMap & map,
104  bool complement,
105  bool with )
106 {
107  Point p1 = Point::diagonal( -1 );
108  Point p2 = Point::diagonal( 1 );
109  Point c = Point::diagonal( 0 );
110  Domain domain( p1, p2 );
111 
112  Point q1 = Point::diagonal( -1 );
113  Point q2 = Point::diagonal( 4*16-1 );
114  Domain fullDomain( q1, q2 );
115  board << SetMode( fullDomain.className(), "Paving" );
116  unsigned int cfg = 0;
117  for ( unsigned int y = 0; y < 16; ++y )
118  for ( unsigned int x = 0; x < 16; ++x, ++cfg )
119  {
120  bool simple = map[ cfg ];
121  Point base( x*4, y*4 );
122  unsigned int mask = 1;
123  for ( auto it = domain.begin();
124  it != domain.end(); ++it )
125  {
126  Point q = base + (*it);
127  if ( *it == c ) {
128  if ( with )
129  board << CustomStyle( q.className(),
130  simple
131  ? new CustomColors( Color( 0, 0, 0 ),
132  Color( 30, 128, 30 ) )
133  : new CustomColors( Color( 0, 0, 0 ),
134  Color( 128, 30, 30 ) ) );
135  else
136  board << CustomStyle( q.className(),
137  simple
138  ? new CustomColors( Color( 0, 0, 0 ),
139  Color( 200, 255, 200 ) )
140  : new CustomColors( Color( 0, 0, 0 ),
141  Color( 255, 200, 200 ) ) );
142 
143  } else {
144  bool in_cfg = cfg & mask;
145  bool display = complement ? ( ! in_cfg ) : in_cfg;
146  if ( display )
147  board <<
148  CustomStyle( q.className(),
149  simple
150  ? new CustomColors( Color( 0, 0, 0 ),
151  Color( 10, 255, 10 ) )
152  : new CustomColors( Color( 0, 0, 0 ),
153  Color( 255, 10, 10 ) ) );
154  else
155  board <<
156  CustomStyle( q.className(),
157  simple
158  ? new CustomColors( Color( 0, 0, 0 ),
159  Color( 245, 255, 245 ) )
160  : new CustomColors( Color( 0, 0, 0 ),
161  Color( 255, 245, 245 ) ) );
162  mask <<= 1;
163  }
164  board << q;
165  }
166  }
167 }
168 
169 
170 int main( int argc, char** argv )
171 {
172  DConv dconv( Point::diagonal( -5 ), Point::diagonal( 5 ) );
173 
174  trace.beginBlock ( "Generate 2d tables" );
175  ConfigMap table_with ( 256, false );
176  ConfigMap table_without ( 256, false );
177  ConfigMap table_cwith ( 256, false );
178  ConfigMap table_cwithout( 256, false );
179  Point p1 = Point::diagonal( -1 );
180  Point p2 = Point::diagonal( 1 );
181  Point c = Point::diagonal( 0 );
182  Domain domain( p1, p2 );
183  unsigned int cfg = 0;
184  KSpace K;
185  K.init( p1, p2, true );
188  for ( unsigned int y = 0; y < 16; ++y )
189  for ( unsigned int x = 0; x < 16; ++x )
190  {
191  // Building a configuration.
192  std::vector< Point > Xwith;
193  std::vector< Point > Xwithout;
194  Point base( x, y );
195  unsigned int mask = 1;
196  for ( auto it = domain.begin(); it != domain.end(); ++it )
197  {
198  const Point p = *it;
199  if ( p != c )
200  {
201  image.setValue( p, cfg & mask );
202  mask <<= 1;
203  }
204  }
205  // Checking full convexity.
206  LCA.setCenter( c, image );
207  bool full_with = LCA.isFullyConvex( true );
208  bool full_without = LCA.isFullyConvex( false );
209  bool full_cwith = LCA.isComplementaryFullyConvex( true );
210  bool full_cwithout = LCA.isComplementaryFullyConvex( false );
211  table_with [ cfg ] = full_with;
212  table_without [ cfg ] = full_without;
213  table_cwith [ cfg ] = full_cwith;
214  table_cwithout[ cfg ] = full_cwithout;
215  cfg += 1;
216  }
217  trace.endBlock();
218  trace.beginBlock ( "Computing topology-related tables" );
219  ConfigMap table_regular ( 256, false );
220  for ( cfg = 0; cfg < 256; cfg++ )
221  table_regular[ cfg ] = table_with[ cfg ] && table_without[ 255 - cfg ];
222  ConfigMap table_collapsible( 256, false );
223  for ( cfg = 0; cfg < 256; cfg++ )
224  table_collapsible[ cfg ] = table_with[ cfg ] && table_without[ cfg ]
225  && ( cfg != 0 );
226  trace.endBlock();
227  trace.beginBlock ( "Display 2d tables" );
228  {
229  Board2D board;
230  displaySimplicityTable( board, table_with, false, true );
231  board.saveEPS( "table-fcvx-with-center.eps" );
232  }
233  {
234  Board2D board;
235  displaySimplicityTable( board, table_without, false, false );
236  board.saveEPS( "table-fcvx-without-center.eps" );
237  }
238  {
239  Board2D board;
240  displaySimplicityTable( board, table_cwith, true, true );
241  board.saveEPS( "table-complementary-fcvx-with-center.eps" );
242  }
243  {
244  Board2D board;
245  displaySimplicityTable( board, table_cwithout, true, false );
246  board.saveEPS( "table-complementary-fcvx-without-center.eps" );
247  }
248  {
249  Board2D board;
250  displaySimplicityTable( board, table_regular, false, true );
251  board.saveEPS( "table-fcvx-regular.eps" );
252  }
253  {
254  Board2D board;
255  displaySimplicityTable( board, table_collapsible, false, true );
256  board.saveEPS( "table-fcvx-collapsible.eps" );
257  }
258  trace.endBlock();
259  trace.beginBlock ( "Output 2d tables as C arrays" );
260  ofstream out( "table-fcvx.cpp" );
261  outputTableAsArray( out, "table-fcvx-with-center",
262  table_with );
263  outputTableAsArray( out, "table-fcvx-without-center",
264  table_without );
265  outputTableAsArray( out, "table-complementary-fcvx-with-center",
266  table_cwith );
267  outputTableAsArray( out, "table-complementary-fcvx-without-center",
268  table_cwithout );
269  outputTableAsArray( out, "table-fcvx-regular",
270  table_regular );
271  outputTableAsArray( out, "table-fcvx-collapsible",
272  table_collapsible );
273  out.close();
274  trace.endBlock();
275  return 0;
276 }
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
const ConstIterator & end() const
const ConstIterator & begin() const
std::string className() const
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Aim: A class that models a neighborhood and that provides services to analyse the convexity properti...
void setCenter(Point c, const PointPredicate &X)
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
void beginBlock(const std::string &keyword="")
double endBlock()
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:804
NeighborhoodConvexityAnalyzer< KSpace, 1 > NCA1
DigitalConvexity< KSpace > DConv
int main(int argc, char **argv)
std::vector< bool > ConfigMap
void displaySimplicityTable(Board2D &board, const ConfigMap &map, bool complement, bool with)
void outputTableAsArray(ostream &out, const string &tableName, const ConfigMap &map)
Shortcuts< KSpace > SH2
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
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
KSpace K
void display(ostream &out, const AContainer &C)
Domain domain
Image image(domain)