DGtal 1.4.0
Loading...
Searching...
No Matches
exampleRationalConvexity.cpp
1
44#include <iostream>
45
46#include "DGtal/base/Common.h"
47#include "DGtal/helpers/StdDefs.h"
48#include "ConfigExamples.h"
49
50#include "DGtal/topology/KhalimskySpaceND.h"
51#include "DGtal/geometry/curves/FreemanChain.h"
52#include "DGtal/geometry/curves/GridCurve.h"
53#include "DGtal/geometry/volumes/DigitalConvexity.h"
54
55#include "DGtal/io/boards/Board2D.h"
56
58
59using namespace std;
60using namespace DGtal;
61using namespace Z2i;
62
63
65int main( int argc, char** argv )
66{
67 trace.beginBlock ( "Example for 2d gridcurves" );
68 string S = examplesPath + "samples/contourS.fc";
69
70 // domain
71 const Point lowerBound( -200, -200 );
72 const Point upperBound( 200, 200 );
73 DigitalConvexity<KSpace> dconv( lowerBound, upperBound );
74
75 fstream inputStream( S.c_str(), ios::in );
76 FreemanChain<int> fc(inputStream);
77 inputStream.close();
78 Curve c;
79 c.initFromPointsRange( fc.begin(), fc.end() );
80 auto points = c.getPointsRange();
81 std::vector<Point> T( points.begin(), points.end() );
82 auto midpoints = c.getMidPointsRange();
83 std::vector<RealPoint> RT( midpoints.begin(), midpoints.end() );
84 std::vector<Point> T2;
85 for ( auto && rp : midpoints )
86 // there is a shift of (0.5,0.5) between points and cells embedder.
87 T2.push_back( Point( (int) round( 2. * rp[ 0 ] + 1. ),
88 (int) round( 2. * rp[ 1 ] + 1. ) ) );
89 Board2D aBoard;
90 aBoard.setUnit(Board2D::UCentimeter);
91 // Display cells
92 const KSpace& K = dconv.space();
93 Color grey( 200, 200, 200 );
94 std::set<Cell> pixels;
95 for ( auto p : T )
96 {
97 pixels.insert( K.uCell( Point( 2*p[ 0 ] - 1, 2*p[ 1 ] - 1 ) ) );
98 pixels.insert( K.uCell( Point( 2*p[ 0 ] + 1, 2*p[ 1 ] - 1 ) ) );
99 pixels.insert( K.uCell( Point( 2*p[ 0 ] - 1, 2*p[ 1 ] + 1 ) ) );
100 pixels.insert( K.uCell( Point( 2*p[ 0 ] + 1, 2*p[ 1 ] + 1 ) ) );
101 }
102 for ( auto && pixel : pixels )
103 aBoard << CustomStyle( pixel.className(), new CustomColors( grey, grey ) )
104 << pixel;
105 // Display contour
106 aBoard.setPenColor( Color::Black );
107 aBoard << c;
108 // Compute subconvex rational segments.
109 auto c_cover = dconv.makeCellCover( T.begin(), T.end(), 1, 1 );
110 trace.beginBlock( "Compute fully subconvex rational sets" );
111 Point denominator( 2, 2 );
112 unsigned int last_j = 0;
113 unsigned int j = 0;
114 for ( unsigned int i = 0; i < T2.size(); ++i )
115 {
116 aBoard.setPenColorRGBi( rand() % 255, rand() % 255, rand() % 255 );
117 unsigned int start_j = ( i + 1 ) % T2.size();
118 for ( j = ( start_j + 1 ) % T2.size(); j != start_j; j = ( j + 1 ) % T2.size() )
119 {
120 auto segment = dconv.makeRationalSimplex( { denominator, T2[i], T2[j] } );
121 if ( ! dconv.isFullySubconvex( segment, c_cover ) ) break;
122 }
123 j = (unsigned int)( j + T2.size() - 1 ) % T2.size();
124 if ( j != last_j )
125 { // display fully subconvex segments
126 aBoard.setLineWidth( 2.5 );
127 aBoard.drawLine( RT[i][0], RT[i][1], RT[j][0], RT[j][1] );
128 }
129 last_j = j;
130 }
131 trace.endBlock();
132 aBoard.saveEPS( "myGridCurve.eps", Board2D::BoundingBox );//, 5000 );
133 trace.endBlock();
134 return 0;
135}
136// //
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
static const Color Black
Definition Color.h:413
Aim: A helper class to build polytopes from digital sets and to check digital k-convexity and full co...
static RationalPolytope makeRationalSimplex(Integer d, PointIterator itB, PointIterator itE)
bool isFullySubconvex(const PointRange &Y, const LatticeSet &StarX) const
const KSpace & space() const
CellGeometry makeCellCover(PointIterator itB, PointIterator itE, Dimension i=0, Dimension k=KSpace::dimension) const
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition GridCurve.h:173
bool initFromPointsRange(const TIterator &itb, const TIterator &ite)
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Cell uCell(const PreCell &c) const
From an unsigned cell, returns an unsigned cell lying into this Khalismky space.
void beginBlock(const std::string &keyword="")
double endBlock()
Board & setPenColor(const DGtal::Color &color)
Definition Board.cpp:297
void drawLine(double x1, double y1, double x2, double y2, int depthValue=-1)
Definition Board.cpp:367
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:277
Board & setLineWidth(double width)
Definition Board.cpp:328
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition Board.cpp:804
void setUnit(Unit unit)
Definition Board.cpp:239
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition Common.h:153
STL namespace.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition Board2D.h:279
int main()
Definition testBits.cpp:56
KSpace K