DGtal  1.5.beta
testStabbingLineComputer.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/PointVector.h"
34 #include "DGtal/topology/KhalimskySpaceND.h"
35 #include "DGtal/geometry/curves/GridCurve.h"
36 
37 #include "DGtal/geometry/curves/CBidirectionalSegmentComputer.h"
38 
39 #include "DGtal/geometry/curves/StabbingLineComputer.h"
40 
41 #include "DGtal/geometry/curves/GreedySegmentation.h"
42 #include "DGtal/geometry/curves/SaturatedSegmentation.h"
43 
44 #include "DGtal/io/boards/Board2D.h"
45 #include "DGtal/io/boards/CDrawableWithBoard2D.h"
46 
47 #include "ConfigTest.h"
48 
50 
51 using namespace std;
52 using namespace DGtal;
53 
55 // Functions for testing class StabbingLineComputer.
57 
60 template <typename TCurve>
61 bool testStabbingLineComputer(const TCurve& curve)
62 {
63 
64  typedef typename TCurve::IncidentPointsRange Range; //range
65  typedef typename Range::ConstIterator ConstIterator; //iterator
66  typedef typename Range::ConstReverseIterator ConstReverseIterator; //reverse iterator
67 
68  unsigned int nbok = 0;
69  unsigned int nb = 0;
70 
71  trace.beginBlock ( "Constructors, copy, assignement" );
72  {
73  Range r = curve.getIncidentPointsRange(); //range
74 
76  s2.init(r.begin());
77  s3.init(++r.begin());
80  s3 = s1;
81 
82  trace.info() << s1.isValid() << s1 << endl;
83  trace.info() << s2.isValid() << s2 << endl;
84  trace.info() << s3.isValid() << s3 << endl;
85  trace.info() << s4.isValid() << s4 << endl;
86  trace.info() << s5.isValid() << s5 << endl;
87 
88  bool myFlag = (!s1.isValid())&&(!s3.isValid())
89  &&(s2.isValid())&&(s4.isValid())&&(s5.isValid())
90  &&(s2 == s4)&&(s3 != s5)&&(s1 == s3)&&(s2 != s5);
91 
92  nbok += myFlag ? 1 : 0;
93  nb++;
94  }
95  trace.endBlock();
96 
97  trace.beginBlock ( "Extension operations" );
98  {
99  Range r = curve.getIncidentPointsRange(); //range
100 
102 
103  trace.info() << "forward extension " << endl;
104  ConstIterator itBegin (r.begin());
105  ConstIterator itEnd (r.end());
106  s.init( itBegin+1 );
107  while ( (s.end() != itEnd) && (s.isExtendableFront()) && (s.extendFront()) ) {}
108  trace.info() << s << endl;
109  double a, b, c;
110  s.getParameters(a,b,c);
111  trace.info() << a << " " << b << " " << c << endl;
112 
113  t.init( (itBegin + (itEnd - itBegin)/2) );
114  while ( (t.end() != itEnd) && (t.extendFront())
115  && (t.begin() != itBegin) && (t.extendBack()) ) {}
116  trace.info() << t << endl;
117 
118  trace.info() << "backward extension " << endl;
120  ConstReverseIterator ritBegin (t.end());
121  ConstReverseIterator ritEnd (r.rend());
122  rs.init( ritBegin );
123  while ( (rs.end() != ritEnd) && (rs.isExtendableFront()) && (rs.extendFront()) ) {}
124  trace.info() << rs << endl;
125  double ap, bp, cp;
126  rs.getParameters(ap,bp,cp);
127  trace.info() << ap << " " << bp << " " << cp << endl;
128 
130  rt.init( (ritBegin + (ritEnd - ritBegin)/2) );
131  while ( (rt.begin() != ritBegin) && (rt.extendBack())
132  && (rt.end() != ritEnd) && (rt.extendFront()) ) {}
133  trace.info() << rt << endl;
134 
135  trace.info() << "comparison... " << endl;
136  bool myFlag = ( (s == t)&&(rs == rt) )
137  && ( s.Uf() == rs.Uf() )
138  && ( s.Ul() == rs.Ul() )
139  && ( s.Lf() == rs.Lf() )
140  && ( s.Ll() == rs.Ll() )
141  && (a == ap)
142  && (b == bp)
143  && (c == cp)
144  ;
145 
146  nbok += myFlag ? 1 : 0;
147  nb++;
148  }
149  trace.endBlock();
150 
151  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
152  return nbok == nb;
153 }
154 
155 /*
156 * simple drawing
157 */
158 template <typename TCurve>
159 bool drawingTestStabbingLineComputer(const TCurve& curve)
160 {
161 
162  typedef typename TCurve::IncidentPointsRange Range; //range
163  typedef typename Range::ConstIterator ConstIterator; //iterator
164 
165  Range r = curve.getIncidentPointsRange(); //range
166 
168  ConstIterator itEnd (r.end());
169  s.init( r.begin() );
170  while ( (s.end() != itEnd) && (s.extendFront()) ) {}
171 
172  double a, b, c;
173  s.getParameters(a,b,c);
174 
175  Board2D board;
176  board << r << s;
177  board.saveEPS("StabbingLineComputerdrawingTest.eps");
178  return true;
179 }
180 
182 {
183  typedef std::pair<PointVector<2,int>, PointVector<2,int> > Pair;
184  typedef std::vector<Pair>::const_iterator ConstIterator;
185  typedef StabbingLineComputer<ConstIterator> GeomDSS;
186  BOOST_CONCEPT_ASSERT(( concepts::CDrawableWithBoard2D<GeomDSS> ));
187  BOOST_CONCEPT_ASSERT(( concepts::CBidirectionalSegmentComputer<GeomDSS> ));
188 }
189 
190 template <typename TCurve>
191 bool testSegmentation(const TCurve& curve)
192 {
193 
194  typedef typename TCurve::IncidentPointsRange Range; //range
195  Range r = curve.getIncidentPointsRange(); //range
196 
197  typedef typename Range::ConstIterator ConstIterator; //iterator
198  typedef StabbingLineComputer<ConstIterator> SegmentComputer; //segment computer
199 
200  unsigned int nbok = 0;
201  unsigned int nb = 0;
202 
203 
204  trace.beginBlock ( "Greedy segmentation" );
205  {
207  Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer() );
208 
209  Board2D board;
210  board << r;
211 
212  typename Segmentation::SegmentComputerIterator it = theSegmentation.begin();
213  typename Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
214  unsigned int n = 0;
215  unsigned int suml = 0;
216  for ( ; it != itEnd; ++it, ++n) {
217  board << (*it);
218  for (ConstIterator i = it->begin(); i != it->end(); ++i)
219  suml += 1;
220  }
221 
222  board.saveEPS("StabbingLineComputerGreedySegmentationTest.eps", Board2D::BoundingBox, 5000 );
223 
224  trace.info() << r.size() << ";" << n << ";" << suml << endl;
225  //comparison with the results gave by another program
226  nbok += ((r.size()==85)&&(n==10)&&(suml==94)) ? 1 : 0;
227  nb++;
228  }
229  trace.endBlock();
230 
231  trace.beginBlock ( "Saturated segmentation" );
232  {
234  Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer() );
235 
236  Board2D board;
237  board << r;
238 
239  typename Segmentation::SegmentComputerIterator it = theSegmentation.begin();
240  typename Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
241  unsigned int n = 0;
242  unsigned int suml = 0;
243  for ( ; it != itEnd; ++it, ++n) {
244  board << (*it);
245  for (ConstIterator i = it->begin(); i != it->end(); ++i)
246  suml += 1;
247  }
248 
249  board.saveEPS("StabbingLineComputerSaturatedSegmentationTest.eps", Board2D::BoundingBox, 5000 );
250 
251  trace.info() << r.size() << ";" << n << ";" << suml << endl;
252  //comparison with the results gave by another program
253  nbok += ((r.size()==85)&&(n==25)&&(suml==255)) ? 1 : 0;
254  nb++;
255  }
256  trace.endBlock();
257 
258 
259  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
260  return (nbok == nb);
261 }
263 // Standard services - public :
264 
265 int main( int argc, char** argv )
266 {
267  trace.beginBlock ( "Testing class StabbingLineComputer" );
268  trace.info() << "Args:";
269  for ( int i = 0; i < argc; ++i )
270  trace.info() << " " << argv[ i ];
271  trace.info() << endl;
272 
273  bool res;
274 
275  {//concept checking
277  }
278 
279  {//basic operations
280  std::string filename = testPath + "samples/DSS.dat";
281  ifstream instream; // input stream
282  instream.open (filename.c_str(), ifstream::in);
283 
285  GridCurve<KSpace> c; //grid curve
286  c.initFromVectorStream(instream);
287 
288  res = testStabbingLineComputer(c)
290  }
291 
292  {//segmentations
293  std::string filename = testPath + "samples/sinus2D4.dat";
294  ifstream instream; // input stream
295  instream.open (filename.c_str(), ifstream::in);
296 
298  GridCurve<KSpace> c; //grid curve
299  c.initFromVectorStream(instream);
300 
301  res = res && testSegmentation(c);
302  }
303 
304  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
305  trace.endBlock();
306  return res ? 0 : 1;
307 }
308 // //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: Computes the greedy segmentation of a range given by a pair of ConstIterators....
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 initFromVectorStream(std::istream &in)
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: Specific iterator to visit all the maximal segments of a saturated segmentation.
Aim: Computes the saturated segmentation, that is the whole set of maximal segments within a range gi...
SaturatedSegmentation::SegmentComputerIterator end() const
SaturatedSegmentation::SegmentComputerIterator begin() const
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
std::reverse_iterator< ConstIterator > ConstReverseIterator
Aim: On-line recognition of a digital straight segment (DSS) defined as a sequence of connected grid ...
Reverse getReverse() const
ConstIterator end() const
ConstIterator begin() const
void init(const ConstIterator &anIt)
void getParameters(double &alpha, double &beta, double &gamma) const
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:804
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Aim: Defines the concept describing a bidirectional segment computer, ie. a model of concepts::CSeg...
Aim: The concept CDrawableWithBoard2D specifies what are the classes that admit an export with Board2...
ArithmeticalDSSComputer< std::vector< Z2i::Point >::const_iterator, int, 4 > SegmentComputer
SaturatedSegmentation< SegmentComputer > Segmentation
void testStabbingLineComputerConceptChecking()
int main(int argc, char **argv)
bool drawingTestStabbingLineComputer(const TCurve &curve)
bool testSegmentation(const TCurve &curve)
bool testStabbingLineComputer(const TCurve &curve)