32 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/SpaceND.h"
36 #include "DGtal/kernel/domains/HyperRectDomain.h"
37 #include "DGtal/topology/KhalimskySpaceND.h"
40 #include "DGtal/shapes/ShapeFactory.h"
41 #include "DGtal/shapes/Shapes.h"
42 #include "DGtal/topology/helpers/Surfaces.h"
43 #include "DGtal/shapes/GaussDigitizer.h"
44 #include "DGtal/geometry/curves/GridCurve.h"
47 #include "DGtal/geometry/curves/CBidirectionalSegmentComputer.h"
49 #include "DGtal/geometry/curves/GeometricalDCA.h"
51 #include "DGtal/geometry/curves/SegmentComputerUtils.h"
52 #include "DGtal/geometry/curves/GreedySegmentation.h"
53 #include "DGtal/geometry/curves/SaturatedSegmentation.h"
56 #include "DGtal/io/boards/Board2D.h"
57 #include "DGtal/io/boards/CDrawableWithBoard2D.h"
59 #include "ConfigTest.h"
64 using namespace DGtal;
68 template<
typename TKSpace>
70 ballGenerator(
double aCx,
double aCy,
double aR,
bool aFlagIsCW)
75 typedef typename KSpace::SCell
SCell;
77 typedef typename KSpace::Space
Space;
79 typedef typename Space::Point
Point;
80 typedef typename Space::RealPoint RealPoint;
84 Shape aShape(Point(aCx,aCy), aR);
87 RealPoint xLow ( -aR-1, -aR-1 );
88 RealPoint xUp( aR+1, aR+1 );
91 dig.
init( xLow, xUp, 1 );
99 <<
" error in creating KSpace." << std::endl;
109 std::vector<Point> points, points2;
115 points2.assign( points.rbegin(), points.rend() );
127 <<
" error in finding a bel." << std::endl;
136 void testGeometricalDCAConceptChecking()
139 typedef std::vector<Pair>::const_iterator ConstIterator;
148 template <
typename TCurve>
149 bool drawingTestGeometricalDCA(
const TCurve& curve,
const string& suffix)
152 typedef typename TCurve::IncidentPointsRange
Range;
153 Range r = curve.getIncidentPointsRange();
156 typedef typename Range::ConstIterator ConstIterator;
162 std::stringstream ss;
163 ss <<
"GeometricalDCADrawingTest" << suffix <<
".eps";
164 board.saveEPS(ss.str().c_str());
168 typedef typename Range::ConstReverseIterator ConstReverseIterator;
174 std::stringstream ss;
175 ss <<
"GeometricalDCADrawingTest" << suffix <<
"2.eps";
176 board.saveEPS(ss.str().c_str());
185 template <
typename TCurve>
186 bool testGeometricalDCA(
const TCurve& curve)
189 typedef typename TCurve::IncidentPointsRange Range;
190 typedef typename Range::ConstIterator ConstIterator;
191 typedef typename Range::ConstReverseIterator ConstReverseIterator;
193 unsigned int nbok = 0;
198 Range r = curve.getIncidentPointsRange();
210 trace.
info() << s4.isValid() << s4 << endl;
211 trace.
info() << s5.isValid() << s5 << endl;
214 &&(s2.
isValid())&&(s4.isValid())&&(s5.isValid())
215 &&(s2 == s4)&&(s2 != s5)&&(s2 != s1)
216 &&(s3 != s5)&&(s1 == s3);
218 nbok += myFlag ? 1 : 0;
226 Range r = curve.getIncidentPointsRange();
230 trace.
info() <<
"forward extension " << endl;
232 ConstIterator itBegin (r.
begin());
233 ConstIterator itEnd (r.
end());
239 ConstIterator itLast (s.
end()); --itLast;
245 trace.
info() <<
"backward extension " << endl;
248 ConstReverseIterator ritBegin ( s.
end() );
249 ConstReverseIterator ritEnd ( itBegin );
255 ConstReverseIterator ritLast (rs.
end()); --ritLast;
262 trace.
info() <<
"comparison... " << endl;
263 bool myFlag = (s == t)
266 nbok += myFlag ? 1 : 0;
271 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " << endl;
278 bool testRecognition()
284 unsigned int nbok = 0;
289 for (
unsigned int i = 0; i < 50; ++i)
292 double cx = (rand()%100 ) / 100.0;
293 double cy = (rand()%100 ) / 100.0;
294 double radius = (rand()%100 )+100;
295 c = ballGenerator<KSpace>( cx, cy, radius, ((i%2)==1) );
296 trace.
info() <<
" #ball #" << i <<
" c(" << cx <<
"," << cy <<
") r=" << radius << endl;
303 typedef Range::ConstIterator ConstIterator;
314 for (ConstIterator it = s.
begin(); ((it != s.
end()) && flag) ; ++it)
318 flag = ( p1(it->first)&&p2(it->second) );
322 nbok += flag ? 1 : 0;
328 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " << endl;
335 template <
typename TCurve>
336 bool testSegmentation(
const TCurve& curve)
339 typedef typename TCurve::IncidentPointsRange Range;
340 Range r = curve.getIncidentPointsRange();
342 typedef typename Range::ConstIterator ConstIterator;
345 unsigned int nbok = 0;
351 Segmentation theSegmentation( r.
begin(), r.
end(), SegmentComputer() );
356 typename Segmentation::SegmentComputerIterator it = theSegmentation.
begin();
357 typename Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
359 unsigned int suml = 0;
360 for ( ; it != itEnd; ++it, ++n) {
361 board <<
SetMode(SegmentComputer().className(),
"Sector")
363 for (ConstIterator i = it->begin(); i != it->end(); ++i)
367 board.saveSVG(
"GeometricalDCAGreedySegmentationTest.svg", Board2D::BoundingBox, 5000 );
369 trace.
info() << r.size() <<
";" << n <<
";" << suml << endl;
371 bool flag = ((r.size()==85)&&(n==6)&&(suml==90)&&((r.size()+n-1)==suml));
372 nbok += flag ? 1 : 0;
380 Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer() );
381 theSegmentation.
setMode(
"Last");
386 typename Segmentation::SegmentComputerIterator it = theSegmentation.begin();
387 typename Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
389 unsigned int suml = 0;
390 for ( ; it != itEnd; ++it, ++n) {
391 board <<
SetMode(SegmentComputer().className(),
"Annulus")
393 for (ConstIterator i = it->begin(); i != it->end(); ++i)
397 board.
saveSVG(
"GeometricalDCASaturatedSegmentationTest.svg", Board2D::BoundingBox, 5000 );
399 trace.
info() << r.size() <<
";" << n <<
";" << suml << endl;
401 nbok += ((r.size()==85)&&(n==20)&&(suml==326)) ? 1 : 0;
406 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " << endl;
412 int main(
int argc,
char** argv )
416 for (
int i = 0; i < argc; ++i )
423 testGeometricalDCAConceptChecking();
430 c = ballGenerator<KSpace>(0,0,6,
false);
431 std::vector<PointVector<2,int> > rv;
432 rc = ballGenerator<KSpace>(0,0,6,
true);
434 res = testGeometricalDCA(c)
435 && drawingTestGeometricalDCA(c,
"CCW")
436 && drawingTestGeometricalDCA(rc,
"CW");
440 res = res && testRecognition();
444 std::string filename = testPath +
"samples/sinus2D4.dat";
446 instream.open (filename.c_str(), ifstream::in);
452 res = res && testSegmentation(c);
455 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;