DGtal  1.3.beta
topology/generateSimplicityTables2D.cpp

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

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"
// For saving compressed tables.
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
using namespace std;
using namespace DGtal;
template <typename Object, typename Map>
void
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 );
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 ) ) );
}
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 file1( filename );
// file1 << bit_table8_4;
// file1.close();
{
string filename = "simplicity_table8_4.zlib";
ofstream file( filename );
ConfigMapBit* table = &bit_table8_4;
std::stringstream table_stream;
table_stream << *table;
namespace io = boost::iostreams;
io::filtering_streambuf<io::input> filter;
filter.push(io::zlib_compressor());
filter.push(table_stream);
io::copy(filter,file);
file.close();
}
// string filename = "simplicity_table4_8.txt";
// ofstream file2( filename );
// file2 << bit_table4_8;
// file2.close();
{
string filename = "simplicity_table4_8.zlib";
ofstream file( filename );
ConfigMapBit* table = &bit_table4_8;
std::stringstream table_stream;
table_stream << *table;
namespace io = boost::iostreams;
io::filtering_streambuf<io::input> filter;
filter.push(io::zlib_compressor());
filter.push(table_stream);
io::copy(filter,file);
file.close();
}
return 0;
}
// //
DGtal::DigitalSetByAssociativeContainer::Domain
TDomain Domain
Domain type.
Definition: DigitalSetByAssociativeContainer.h:94
DGtal::HyperRectDomain< Space >
DGtal::Trace::endBlock
double endBlock()
displaySimplicityTable
void displaySimplicityTable(Board2D &board, const ConfigMap &map, bool complement, bool with)
Definition: fullConvexityLUT2D.cpp:102
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::HyperRectDomain::className
std::string className() const
DGtal::Object::DigitalTopology
TDigitalTopology DigitalTopology
Definition: Object.h:138
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
outputTableAsArray
void outputTableAsArray(ostream &out, const string &tableName, const ConfigMap &map)
Definition: fullConvexityLUT2D.cpp:86
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
ConfigMap
std::vector< bool > ConfigMap
Definition: fullConvexityLUT2D.cpp:76
Map
std::unordered_map< Cell, CubicalCellData > Map
Definition: testCubicalComplex.cpp:57
DGtal::CustomStyle
Definition: Board2D.h:217
DGtal::Object::DigitalSet
TDigitalSet DigitalSet
Definition: Object.h:123
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Domain
HyperRectDomain< Space > Domain
Definition: testSimpleRandomAccessRangeFromPoint.cpp:44
DGtal::CustomColors
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
DGtal::HyperRectDomain::end
const ConstIterator & end() const
Definition: HyperRectDomain.h:201
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:70
DGtal::HyperRectDomain_Iterator
Iterator for HyperRectDomain.
Definition: HyperRectDomain_Iterator.h:142
LibBoard::Board::saveEPS
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector< dim, Integer >
DGtal::SetMode
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
DGtal::HyperRectDomain::begin
const ConstIterator & begin() const
Definition: HyperRectDomain.h:176
Point
MyPointD Point
Definition: testClone2.cpp:383
DigitalSet
Z2i::DigitalSet DigitalSet
Definition: testVoronoiMapComplete.cpp:41