DGtal  1.2.0
testCombinDSS.cpp
Go to the documentation of this file.
1 
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/OneBalancedWordComputer.h"
43 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
44 #include "ConfigTest.h"
45 #include "DGtal/geometry/curves/CDynamicBidirectionalSegmentComputer.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 
68 {
69  typedef string::const_iterator codeIterator;
70  typedef OneBalancedWordComputer< list<char>::iterator, int> OneBalancedWordComputer_list;
71  typedef OneBalancedWordComputer<codeIterator, int> OneBalancedWordComputer_string;
72 
75 
76  trace.beginBlock ( "Test different initialization methods" );
77 
78  std::string filename = testPath + "samples/france.fc";
79  std::fstream fst;
80  fst.open (filename.c_str(), std::ios::in);
81  Contour theContour(fst);
82 
83  list<char> l;
84  for ( string::const_iterator it = theContour.chain.begin(); it != theContour.chain.end(); ++it )
85  {
86  l.push_back( *it );
87  }
88 
89  list<char>::iterator it = l.begin();
90 
91  OneBalancedWordComputer_list C1;
92  C1.init( it, theContour.firstPoint() );
93 
94  OneBalancedWordComputer_list C2;
95  C2.init( C1.begin() );
96 
97  OneBalancedWordComputer_string C3;
98  C3.init( theContour );
99 
100  OneBalancedWordComputer_string C4;
101  C4.init( theContour.begin() );
102 
103  int nbRetract = 0;
104  while ( C3.end() != theContour.chain.end() )
105  {
106  bool b1 = C1.extendFront();
107  bool b2 = C2.extendFront();
108  bool b3 = C3.extendFront();
109  bool b4 = C4.extendFront();
110  if ( b1 && b2 && b3 && b4 )
111  {
112  }
113  else if ( !b1 && !b2 && !b3 && !b4 )
114  {
115  C1.retractBack();
116  C2.retractBack();
117  C3.retractBack();
118  C4.retractBack();
119  ++nbRetract;
120  }
121  else
122  {
123  cout << b1 << " " << b2 << " " << b3 << " " << b4 << endl;
124  cout << C1 << endl;
125  cout << C2 << endl;
126  cout << C3 << endl;
127  cout << C4 << endl;
128  return false;
129  }
130  }
131  trace.endBlock();
132  return (nbRetract == 3485) ;
133 }
134 
135 
141 {
142  typedef string::const_iterator codeIterator;
145 
146  trace.beginBlock ( "Comparing to ArithmeticalDSSComputer" );
147 
148  std::string filename = testPath + "samples/manche.fc";
149  std::fstream fst;
150  fst.open (filename.c_str(), std::ios::in);
151  Contour theContour(fst);
152  Contour::ConstIterator it = theContour.begin();
153  TestedType C;
154  C.init( it );
155  ReferenceType A(it);
156  A.extendFront();
157  bool res = true;
158  while ( C.end() != theContour.chain.end() )
159  {
160  bool a = A.extendFront();
161  bool c = C.extendFront();
162  if ( a ^ c )
163  {
164  res = false;
165  cout << "Extension test error\n";
166  break;
167  }
168  else if ( ! a )
169  {
170  A.retractBack();
171  C.retractBack();
172  }
173  // Compare positions
174  if ( ( C.back() != A.back() ) || ( C.front() != A.front() ) )
175  {
176  res = false;
177  cout << "Equality test error\n";
178  break;
179  }
180  // Compare arithmetic parameters
181  if ( ( C.getA() != A.a() ) || ( C.getB() != A.b() ) ||
182  ( C.getMu() != A.mu() ) || ( C.getOmega() != A.omega() ) ||
183  ( C.Uf() != A.Uf() ) || ( C.Ul() != A.Ul() ) ||
184  ( C.Lf() != A.Lf() ) || ( C.Ll() != A.Ll() )
185  )
186  {
187  cout << "Arithmetic parameters error\n";
188  cout << C << endl;
189  cout << A << endl;
190  cout << "a() " << C.getA() << " --- " << A.a() << "\n";
191  cout << "b() " << C.getB() << " --- " << A.b() << "\n";
192  cout << "mu() " << C.getMu() << " --- " << A.mu() << "\n";
193  cout << "omega()" << C.getOmega() << " --- " << A.omega() << "\n";
194  cout << "Uf() " << C.Uf() << " --- " << A.Uf() << "\n";
195  cout << "Ul() " << C.Ul() << " --- " << A.Ul() << "\n";
196  cout << "Lf() " << C.Lf() << " --- " << A.Lf() << "\n";
197  cout << "Ll() " << C.Ll() << " --- " << A.Ll() << endl;
198  res = false;
199  break;
200  }
201  }
202  trace.endBlock();
203  return res;
204 }
205 
206 
207 
209 {
210 
212  typedef GreedySegmentation<combinDSS> combinSegmentation;
213 
214  std::string filename = testPath + "samples/BigBall.fc";
215  std::fstream fst;
216  fst.open (filename.c_str(), std::ios::in);
217  Contour theContour(fst);
218 
219  trace.beginBlock ( "Test OneBalancedWordComputer in greedy segmentation" );
220  combinSegmentation combin_dec( theContour.chain.begin(), theContour.chain.end(), combinDSS() );
221  vector<combinDSS> theCombinDSS;
222  for ( combinSegmentation::SegmentComputerIterator i = combin_dec.begin();
223  i != combin_dec.end(); ++i )
224  {
225  combinDSS c( *i );
226  theCombinDSS.push_back( c );
227  }
228  bool ok = ( theCombinDSS.size() == 1593 );
229  trace.endBlock();
230 
231  return ok;
232 }
233 
234 
245 {
246  trace.beginBlock ( "Example testCombinDSS-greedy" );
247 
249  typedef GreedySegmentation<combinDSS> Decomposition;
250  typedef StandardDSS4<int> arithDSS;
251 
252  std::stringstream ss(stringstream::in | stringstream::out);
253  ss << "31 16 11121212121212212121212212122122222322323233323333333323333323303330330030300000100010010010001000101010101111" << endl;
254  Contour theContour( ss );
255 
256  Decomposition theDecomposition( theContour.chain.begin(), theContour.chain.end(), combinDSS() );
257  Point p1( 0, 0 );
258  Point p2( 31, 31 );
259  Domain domain( p1, p2 );
260  Board2D aBoard;
261  aBoard << SetMode( domain.className(), "Grid" )
262  << domain
263  << SetMode( "PointVector", "Grid" )
264  << theContour;
265  //for each segment
266  Point p;
267  p[0] = 31;
268  p[1] = 16;
269  for ( Decomposition::SegmentComputerIterator i = theDecomposition.begin();
270  i != theDecomposition.end(); ++i )
271  {
272  combinDSS segment(*i);
273  // set the position of the combinatorilDSS
274  segment.setPosition( p );
275  // Since both DSS overlap on one code, the start point of the next one is
276  // the penultimate point of the current one.
277  p = *( --( --( segment.pointEnd() )));
278 
279  // Build an ArithmeticDSS from the OneBalancedWordComputer.
280  arithDSS toShow( *segment.pointBegin(), *segment.pointBegin() );
281  for (combinDSS::ConstPointIterator it = segment.pointBegin(),
282  itEnd = segment.pointEnd(); it != itEnd; ++it )
283  toShow.extendFront( *it );
284 
285  aBoard << SetMode( toShow.className(), "BoundingBox" )
286  << CustomStyle( toShow.className()+"/BoundingBox", new CustomPenColor( Color::Blue ) )
287  << toShow; // draw each segment
288  }
289  aBoard.saveSVG("testCombinDSS-greedy.svg");
290  trace.endBlock();
291  return 1;
292 }
293 
294 
296 // Standard services - public :
297 int main( int argc, char** argv )
298 {
299  trace.beginBlock ( "Testing class CombinDSS" );
300  trace.info() << "Args:";
301  for ( int i = 0; i < argc; ++i )
302  trace.info() << " " << argv[ i ];
303  trace.info() << endl;
304 
305  bool res = testOneBalancedWordComputer()
309 
310  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
311  trace.endBlock();
312 
313  return res ? 0 : 1;
314 }
315 // //
Aim: This class is a wrapper around ArithmeticalDSS that is devoted to the dynamic recognition of dig...
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Point firstPoint() const
Definition: FreemanChain.h:761
ConstIterator begin() const
Aim: Computes the greedy segmentation of a range given by a pair of ConstIterators....
std::string className() const
Aim: This class represents a standard digital straight segment (DSS), ie. the sequence of simply 4-co...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Custom style class redefining the pen color. You may use Board2D::Color::None for transparent color.
Definition: Board2D.h:313
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
Aim: Defines the concept describing a dynamic and bidirectional segment computer, ie....
bool showGreedySegmantation()
int main(int argc, char **argv)
bool testOneBalancedWordComputer()
FreemanChain< int >::Vector Vector
bool testInGreedySegmentation()
FreemanChain< int > Contour
bool CompareToArithmetical()
Domain domain