DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testCombinDSS.cpp
1 
33 
34 #include <iostream>
35 #include <fstream>
36 #include <list>
37 #include <vector>
38 
39 #include "DGtal/base/Common.h"
40 #include "DGtal/helpers/StdDefs.h"
41 #include "DGtal/io/boards/Board2D.h"
42 #include "DGtal/geometry/curves/CombinatorialDSS.h"
43 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
44 #include "ConfigTest.h"
45 #include "DGtal/geometry/curves/CBidirectionalSegmentComputer.h"
46 #include "DGtal/geometry/curves/GreedySegmentation.h"
47 
49 
50 using namespace std;
51 using namespace DGtal;
52 using namespace Z2i;
53 
55 // Functions for testing class CombinDSS.
57 
58 
61 
62 
63 
67 bool testCombinatorialDSS()
68 {
69  typedef Contour::ConstIterator ConstIterator;
70  typedef string::const_iterator codeIterator;
71  typedef CombinatorialDSS< list<char>::iterator, int> CombinatorialDSS_list;
72  typedef CombinatorialDSS<codeIterator, int> CombinatorialDSS_string;
73 
76 
77  trace.beginBlock ( "Test different initialization methods" );
78 
79  std::string filename = testPath + "samples/france.fc";
80  std::fstream fst;
81  fst.open (filename.c_str(), std::ios::in);
82  Contour theContour(fst);
83 
84  list<char> l;
85  for ( string::const_iterator it = theContour.chain.begin(); it != theContour.chain.end(); ++it )
86  {
87  l.push_back( *it );
88  }
89 
90  list<char>::iterator it = l.begin();
91 
92  CombinatorialDSS_list C1;
93  C1.init( it, theContour.firstPoint() );
94 
95  CombinatorialDSS_list C2;
96  C2.init( C1.begin() );
97 
98  CombinatorialDSS_string C3;
99  C3.init( theContour );
100 
101  CombinatorialDSS_string C4;
102  C4.init( theContour.begin() );
103 
104  int nbRetract = 0;
105  while ( C3.end() != theContour.chain.end() )
106  {
107  bool b1 = C1.extendForward();
108  bool b2 = C2.extendForward();
109  bool b3 = C3.extendForward();
110  bool b4 = C4.extendForward();
111  if ( b1 && b2 && b3 && b4 )
112  {
113  }
114  else if ( !b1 && !b2 && !b3 && !b4 )
115  {
116  C1.retractForward();
117  C2.retractForward();
118  C3.retractForward();
119  C4.retractForward();
120  ++nbRetract;
121  }
122  else
123  {
124  cout << b1 << " " << b2 << " " << b3 << " " << b4 << endl;
125  cout << C1 << endl;
126  cout << C2 << endl;
127  cout << C3 << endl;
128  cout << C4 << endl;
129  return false;
130  }
131  }
132  trace.endBlock();
133  return (nbRetract == 3485) ;
134 }
135 
136 
141 bool CompareToArithmetical()
142 {
143  typedef string::const_iterator codeIterator;
144  typedef CombinatorialDSS<codeIterator, int> TestedType;
145  typedef ArithmeticalDSS<Contour::ConstIterator,int,4> ReferenceType;
146 
147  trace.beginBlock ( "Comparing to ArithmeticalDSS" );
148 
149  std::string filename = testPath + "samples/manche.fc";
150  std::fstream fst;
151  fst.open (filename.c_str(), std::ios::in);
152  Contour theContour(fst);
153  Contour::ConstIterator it = theContour.begin();
154  TestedType C;
155  C.init( it );
157  A.extendForward();
158  bool res = true;
159  while ( C.end() != theContour.chain.end() )
160  {
161  bool a = A.extendForward();
162  bool c = C.extendForward();
163  if ( a ^ c )
164  {
165  res = false;
166  cout << "Extension test error\n";
167  break;
168  }
169  else if ( ! a )
170  {
171  A.retractForward();
172  C.retractForward();
173  }
174  // Compare positions
175  if ( ( C.getFirstPoint() != A.getFirstPoint() ) || ( C.getLastPoint() != A.getLastPoint() ) )
176  {
177  res = false;
178  cout << "Equality test error\n";
179  break;
180  }
181  // Compare arithmetic parameters
182  if ( ( C.getA() != A.getA() ) || ( C.getB() != A.getB() ) ||
183  ( C.getMu() != A.getMu() ) || ( C.getOmega() != A.getOmega() ) ||
184  ( C.getUf() != A.getUf() ) || ( C.getUl() != A.getUl() ) ||
185  ( C.getLf() != A.getLf() ) || ( C.getLl() != A.getLl() )
186  )
187  {
188  cout << "Arithmetic parameters error\n";
189  cout << C << endl;
190  cout << A << endl;
191  cout << "getA() " << C.getA() << " --- " << A.getA() << "\n";
192  cout << "getB() " << C.getB() << " --- " << A.getB() << "\n";
193  cout << "getMu() " << C.getMu() << " --- " << A.getMu() << "\n";
194  cout << "getOmega()" << C.getOmega() << " --- " << A.getOmega() << "\n";
195  cout << "getUf() " << C.getUf() << " --- " << A.getUf() << "\n";
196  cout << "getUl() " << C.getUl() << " --- " << A.getUl() << "\n";
197  cout << "getLf() " << C.getLf() << " --- " << A.getLf() << "\n";
198  cout << "getLl() " << C.getLl() << " --- " << A.getLl() << endl;
199  res = false;
200  break;
201  }
202  }
203  trace.endBlock();
204  return res;
205 }
206 
207 
208 
209 bool testInGreedySegmentation( )
210 {
211 
213  typedef GreedySegmentation<combinDSS> combinSegmentation;
214 
215  std::string filename = testPath + "samples/BigBall.fc";
216  std::fstream fst;
217  fst.open (filename.c_str(), std::ios::in);
218  Contour theContour(fst);
219 
220  trace.beginBlock ( "Test CombinatorialDSS in greedy segmentation" );
221  combinSegmentation combin_dec( theContour.chain.begin(), theContour.chain.end(), combinDSS() );
222  vector<combinDSS> theCombinDSS;
223  for ( combinSegmentation::SegmentComputerIterator i = combin_dec.begin();
224  i != combin_dec.end(); ++i )
225  {
226  combinDSS c( *i );
227  theCombinDSS.push_back( c );
228  }
229  bool ok = ( theCombinDSS.size() == 1593 );
230  trace.endBlock();
231 
232  return ok;
233 }
234 
235 
245 bool showGreedySegmantation()
246 {
247  trace.beginBlock ( "Example testCombinDSS-greedy" );
248 
250  typedef GreedySegmentation<combinDSS> Decomposition;
252 
253  std::stringstream ss(stringstream::in | stringstream::out);
254  ss << "31 16 11121212121212212121212212122122222322323233323333333323333323303330330030300000100010010010001000101010101111" << endl;
255  Contour theContour( ss );
256 
257  Decomposition theDecomposition( theContour.chain.begin(), theContour.chain.end(), combinDSS() );
258  Point p1( 0, 0 );
259  Point p2( 31, 31 );
260  Domain domain( p1, p2 );
261  Board2D aBoard;
262  aBoard << SetMode( domain.className(), "Grid" )
263  << domain
264  << SetMode( "PointVector", "Grid" )
265  << theContour;
266  //for each segment
267  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
268  string className = "ArithmeticalDSS/BoundingBox";
269  Point p;
270  p.at(0) = 31;
271  p.at(1) = 16;
272  for ( Decomposition::SegmentComputerIterator i = theDecomposition.begin();
273  i != theDecomposition.end(); ++i )
274  {
275  combinDSS segment(*i);
276  // set the position of the combinatorilDSS
277  segment.setPosition( p );
278  // Since both DSS overlap on one code, the start point of the next one is
279  // the penultimate point of the current one.
280  p = *( --( --( segment.pointEnd() )));
281 
282  // Build an ArithmeticDSS from the CombinatorialDSS.
283  arithDSS toShow( segment.pointBegin() );
284  while( toShow.end() != segment.pointEnd() )
285  {
286  toShow.extendForward();
287  }
288  aBoard << CustomStyle( className, new CustomPenColor( Color::Blue ) )
289  << toShow; // draw each segment
290  }
291  aBoard.saveSVG("testCombinDSS-greedy.svg");
292  trace.endBlock();
293  return 1;
294 }
295 
296 
298 // Standard services - public :
299 int main( int argc, char** argv )
300 {
301  trace.beginBlock ( "Testing class CombinDSS" );
302  trace.info() << "Args:";
303  for ( int i = 0; i < argc; ++i )
304  trace.info() << " " << argv[ i ];
305  trace.info() << endl;
306 
307  bool res = testCombinatorialDSS()
308  && CompareToArithmetical()
309  && testInGreedySegmentation()
310  && showGreedySegmantation();
311 
312  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
313  trace.endBlock();
314 
315  return res ? 0 : 1;
316 }
317 // //