DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testGeometricalDSS.cpp
1 
30 
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/GeometricalDSS.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 GeometricalDSS.
57 
60 template <typename TCurve>
61 bool testGeometricalDSS(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.isExtendableForward()) && (s.extendForward()) ) {}
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.extendForward())
115  && (t.begin() != itBegin) && (t.extendBackward()) ) {}
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.isExtendableForward()) && (rs.extendForward()) ) {}
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.extendBackward())
132  && (rt.end() != ritEnd) && (rt.extendForward()) ) {}
133  trace.info() << rt << endl;
134 
135  trace.info() << "comparison... " << endl;
136  bool myFlag = ( (s == t)&&(rs == rt) )
137  && ( s.getUf() == rs.getUf() )
138  && ( s.getUl() == rs.getUl() )
139  && ( s.getLf() == rs.getLf() )
140  && ( s.getLl() == rs.getLl() )
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 drawingTestGeometricalDSS(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.extendForward()) ) {}
171 
172  double a, b, c;
173  s.getParameters(a,b,c);
174 
175  Board2D board;
176  board << r << s;
177  board.saveEPS("GeometricalDSSdrawingTest.eps");
178  return true;
179 }
180 
181 void testGeometricalDSSConceptChecking()
182 {
183  typedef std::pair<PointVector<2,int>, PointVector<2,int> > Pair;
184  typedef std::vector<Pair>::const_iterator ConstIterator;
185  typedef GeometricalDSS<ConstIterator> GeomDSS;
186  BOOST_CONCEPT_ASSERT(( CDrawableWithBoard2D<GeomDSS> ));
187  BOOST_CONCEPT_ASSERT(( 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 GeometricalDSS<ConstIterator> SegmentComputer; //segment computer
199 
200  unsigned int nbok = 0;
201  unsigned int nb = 0;
202 
203 
204  trace.beginBlock ( "Greedy segmentation" );
205  {
206  typedef GreedySegmentation<SegmentComputer> Segmentation;
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("GeometricalDSSGreedySegmentationTest.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  {
233  typedef SaturatedSegmentation<SegmentComputer> Segmentation;
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("GeometricalDSSSaturatedSegmentationTest.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 GeometricalDSS" );
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
276  testGeometricalDSSConceptChecking();
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 = testGeometricalDSS(c)
289  && drawingTestGeometricalDSS(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 
297  typedef KhalimskySpaceND<2,int> KSpace;
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 // //