DGtal  0.9.3beta
topology/generateSimplicityTables2D.cpp

Creates precomputed tables for determining whether some point is simple within an 2D object.

table4_8.png
Resulting table for 4-8 topology.
#include "DGtal/topology/tables/NeighborhoodTablesGenerators.h"
#include <vector>
#include "DGtal/shapes/Shapes.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/io/Color.h"
using namespace std;
using namespace DGtal;
template <typename Object, typename Map>
void
displaySimplicityTable( Board2D & board,
const typename Object::DigitalTopology & /*dt*/,
const Map & map )
{
typedef typename Object::DigitalSet DigitalSet;
typedef typename Object::Point Point;
typedef typename DigitalSet::Domain Domain;
typedef typename Domain::ConstIterator DomainConstIterator;
Point p1 = Point::diagonal( -1 );
Point p2 = Point::diagonal( 1 );
Point c = Point::diagonal( 0 );
Domain domain( p1, p2 );
Point q1 = Point::diagonal( -1 );
Point q2 = Point::diagonal( 4*16-1 );
Domain fullDomain( q1, q2 );
board << SetMode( fullDomain.className(), "Paving" );
unsigned int cfg = 0;
for ( unsigned int y = 0; y < 16; ++y )
for ( unsigned int x = 0; x < 16; ++x, ++cfg )
{
bool simple = map[ cfg ];
Point base( x*4, y*4 );
unsigned int mask = 1;
for ( DomainConstIterator it = domain.begin();
it != domain.end(); ++it )
{
Point q = base + (*it);
if ( *it == c )
board << CustomStyle( q.className(),
new CustomColors( Color( 0, 0, 0 ),
Color( 30, 30, 30 ) ) );
else
{
if ( cfg & mask )
board <<
CustomStyle( q.className(),
simple
? new CustomColors( Color( 0, 0, 0 ),
Color( 10, 255, 10 ) )
: new CustomColors( Color( 0, 0, 0 ),
Color( 255, 10, 10 ) ) );
else
board <<
CustomStyle( q.className(),
simple
? new CustomColors( Color( 0, 0, 0 ),
Color( 245, 255, 245 ) )
: new CustomColors( Color( 0, 0, 0 ),
Color( 255, 245, 245 ) ) );
mask <<= 1;
}
board << q;
}
}
}
template <typename Map>
void
outputTableAsArray( ostream & out,
const Map & map,
const string & tableName )
{
typedef typename Map::const_iterator MapConstIterator;
out << "const bool " << tableName << "[ " << map.size() << " ] = { ";
for ( MapConstIterator it = map.begin(), it_end = map.end();
it != it_end; )
{
out << *it;
++it;
if ( it != it_end ) out << ", ";
}
out << " };" << std::endl;
}
int main( int /*argc*/, char** /*argv*/ )
{
typedef std::vector<bool> ConfigMap;
using namespace Z2i;
trace.beginBlock ( "Generate 2d table for 4-8 topology" );
ConfigMap table4_8( 256 );
functions::generateSimplicityTable< Object4_8 >( dt4_8, table4_8 );
trace.beginBlock ( "Generate 2d table for 8-4 topology" );
ConfigMap table8_4( 256 );
functions::generateSimplicityTable< Object8_4 >( dt8_4, table8_4 );
Board2D board;
trace.beginBlock ( "Display 2d table for 4-8 topology" );
displaySimplicityTable< Object4_8 >( board, dt4_8, table4_8 );
board.saveEPS( "table4_8.eps" );
board.clear();
trace.beginBlock ( "Display 2d table for 8-4 topology" );
displaySimplicityTable< Object8_4 >( board, dt8_4, table8_4 );
board.saveEPS( "table8_4.eps" );
outputTableAsArray( std::cout, table4_8, "simplicityTable4_8" );
outputTableAsArray( std::cout, table8_4, "simplicityTable8_4" );
/* Output bitset tables to files,
* for using with NeighborhoodConfigurations.h interface.
* These tables can be accesed from NeighborhoodTables.h header.
*/
using ConfigMapBit = std::bitset<256> ; // 2^8
ConfigMapBit bit_table8_4;
ConfigMapBit bit_table4_8;
functions::generateSimplicityTable< Object8_4 >( dt8_4, bit_table8_4 );
functions::generateSimplicityTable< Object4_8 >( dt4_8, bit_table4_8 );
string filename = "simplicity_table8_4.txt";
ofstream file( filename );
file << bit_table8_4;
file.close();
filename = "simplicity_table4_8.txt";
ofstream file2( filename );
file2 << bit_table4_8;
file2.close();
return 0;
}
// //