DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GeometricalDSS.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 template <typename TConstIterator>
42 inline
44 :myBegin(), myEnd(), myPreimagePtr(), myFlagIsInit(false), myFlagIsCW(true)
45 {
46 }
47 
48 template <typename TConstIterator>
49 inline
51 :myBegin(other.myBegin), myEnd(other.myEnd), myPreimagePtr(other.myPreimagePtr),
52 myFlagIsInit(other.myFlagIsInit), myFlagIsCW(other.myFlagIsCW)
53 {
54 }
55 
56 template <typename TConstIterator>
57 inline
60 {
61  if ( this != &other )
62  {
63  myBegin = other.myBegin;
64  myEnd = other.myEnd;
65  myPreimagePtr = other.myPreimagePtr;
66  myFlagIsInit = other.myFlagIsInit;
67  myFlagIsCW = other.myFlagIsCW;
68  }
69  return *this;
70 }
71 
72 template <typename TConstIterator>
73 inline
75 {
76 }
77 
78 template <typename TConstIterator>
79 inline
80 bool
82 {
83  if (isValid()&&other.isValid())
84  {
85  Pair firstPair( *myBegin );
86  Pair oFirstPair( *(other.myBegin) );
87  if ( (firstPair.first == oFirstPair.first)
88  &&(firstPair.second == oFirstPair.second) )
89  {
90  ConstIterator it (myEnd);
91  --it;
92  Pair lastPair( *it );
93  ConstIterator oIt (other.myEnd);
94  --oIt;
95  Pair oLastPair( *oIt );
96 
97  if ( (lastPair.first == oLastPair.first)
98  &&(lastPair.second == oLastPair.second) )
99  {
100  return ( *myPreimagePtr == *(other.myPreimagePtr) );
101  }
102  else return false;
103  }
104  else return false;
105  }
106  else
107  {
108  return ( (!isValid()) && (!other.isValid()) );
109  }
110 }
111 
112 template <typename TConstIterator>
113 inline
114 bool
116 {
117  return !(*this == other);
118 }
119 
120 template <typename TConstIterator>
121 inline
124 {
125  return Reverse();
126 }
127 
128 
129 template <typename TConstIterator>
130 inline
133 {
134  return Self();
135 }
136 
137 
138 
140 // Interface - public :
141 
142 template <typename TConstIterator>
143 inline
144 bool
146 {
147  if ( myPreimagePtr.get() != 0 )
148  {
149  return myPreimagePtr->isValid();
150  }
151  else
152  {
153  return false;
154  }
155 }
156 
157 template <typename TConstIterator>
158 inline
161 {
162  return myBegin;
163 }
164 
165 template <typename TConstIterator>
166 inline
169 {
170  return myEnd;
171 }
172 
173 template <typename TConstIterator>
174 inline
177 {
178  ASSERT( myPreimagePtr.get() != 0 );
179  return myPreimagePtr->getUf();
180 }
181 
182 template <typename TConstIterator>
183 inline
186 {
187  ASSERT( myPreimagePtr.get() != 0 );
188  return myPreimagePtr->getUl();
189 }
190 
191 template <typename TConstIterator>
192 inline
195 {
196  ASSERT( myPreimagePtr.get() != 0 );
197  return myPreimagePtr->getLf();
198 }
199 
200 template <typename TConstIterator>
201 inline
204 {
205  ASSERT( myPreimagePtr.get() != 0 );
206  return myPreimagePtr->getLl();
207 }
208 
209 template <typename TConstIterator>
210 inline
211 bool
213 {
214  return myFlagIsCW;
215 }
216 
217 
218 template <typename TConstIterator>
219 inline
220 void
221 DGtal::GeometricalDSS<TConstIterator>::getParameters(double& alpha, double& beta, double& gamma) const
222 {
223  ASSERT( myPreimagePtr.get() != 0 );
224  myPreimagePtr->getSeparatingStraightLine(alpha, beta, gamma);
225 }
226 
227 template <typename TConstIterator>
228 inline
229 void
231  const double& alpha, const double& beta, const double& gamma) const
232 {
233  ASSERT( (alpha!=0)||(beta!=0) );
234  double d2 = ( alpha * alpha + beta * beta );
235  double s = beta*x - alpha*y;
236  x = (beta*s - alpha*gamma)/d2;
237  y = -(alpha*s + beta*gamma)/d2;
238 }
239 
241 // Growth operations //
242 
243 template <typename TConstIterator>
244 inline
245 void
247 {
248  myBegin = anIt;
249  myEnd = anIt;
250  ++myEnd;
251  Pair aPair( *anIt);
252  myPreimagePtr = PreimagePtr( new Preimage( aPair.first, aPair.second, StraightLine() ) );
253 }
254 
255 template <typename TConstIterator>
256 inline
257 bool
259 {
260  ASSERT( myPreimagePtr.get() != 0 );
261  Pair aPair( *myEnd );
262  bool isExterior = false;
263 
264  if (myFlagIsInit)
265  { //initialized
266 
267  if (myFlagIsCW)
268  isExterior = myPreimagePtr->isLeftExteriorAtTheFront( aPair.first, aPair.second );
269  else
270  isExterior = myPreimagePtr->isRightExteriorAtTheBack( aPair.first, aPair.second );
271 
272  }
273 
274  return isExterior;
275 }
276 
277 template <typename TConstIterator>
278 inline
279 bool
281 {
282  ASSERT( myPreimagePtr.get() != 0 );
283  Pair aPair( *myEnd );
284  bool isExterior = false;
285 
286  if (myFlagIsInit)
287  { //initialized
288 
289  if (myFlagIsCW)
290  isExterior = myPreimagePtr->isRightExteriorAtTheFront( aPair.first, aPair.second );
291  else
292  isExterior = myPreimagePtr->isLeftExteriorAtTheBack( aPair.first, aPair.second );
293 
294  }
295 
296  return isExterior;
297 }
298 
299 template <typename TConstIterator>
300 inline
301 bool
303 {
304  ASSERT( myPreimagePtr.get() != 0 );
305  ConstIterator it( myBegin );
306  --it;
307  Pair aPair( *it );
308  bool isExterior = false;
309 
310  if (myFlagIsInit)
311  { //initialized
312 
313  if (myFlagIsCW)
314  isExterior = myPreimagePtr->isRightExteriorAtTheBack( aPair.first, aPair.second );
315  else
316  isExterior = myPreimagePtr->isLeftExteriorAtTheFront( aPair.first, aPair.second );
317 
318  }
319 
320  return isExterior;
321 }
322 
323 template <typename TConstIterator>
324 inline
325 bool
327 {
328  ASSERT( myPreimagePtr.get() != 0 );
329  ConstIterator it( myBegin );
330  --it;
331  Pair aPair( *it );
332  bool isExterior = false;
333 
334  if (myFlagIsInit)
335  { //initialized
336 
337  if (myFlagIsCW)
338  isExterior = myPreimagePtr->isLeftExteriorAtTheBack( aPair.first, aPair.second );
339  else
340  isExterior = myPreimagePtr->isRightExteriorAtTheFront( aPair.first, aPair.second );
341 
342  }
343 
344  return isExterior;
345 }
346 
347 
348 template <typename TConstIterator>
349 inline
350 bool
352 {
353  ASSERT( myPreimagePtr.get() != 0 );
354  Pair aPair( *myEnd );
355  bool isOK = false;
356 
357  if (myFlagIsInit)
358  { //initialized
359 
360  if (myFlagIsCW)
361  isOK = myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second );
362  else
363  isOK = myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second );
364 
365  } else
366  { //not initialized yet
367  if ( myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second ) )
368  isOK = true;
369  else if ( myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second ) )
370  isOK = true;
371  }
372 
373  return isOK;
374 }
375 
376 template <typename TConstIterator>
377 inline
378 bool
380 {
381  ASSERT( myPreimagePtr.get() != 0 );
382  Pair aPair( *myEnd );
383  bool isOK = false;
384 
385  if (myFlagIsInit)
386  { //initialized
387 
388  if (myFlagIsCW)
389  isOK = myPreimagePtr->addFront( aPair.first, aPair.second );
390  else
391  isOK = myPreimagePtr->addBack( aPair.first, aPair.second );
392 
393  } else
394  { //not initialized yet
395  if ( myPreimagePtr->addFront( aPair.first, aPair.second ) )
396  {
397  myFlagIsCW = true;
398  isOK = true;
399  }
400  else if ( myPreimagePtr->addBack( aPair.first, aPair.second ) )
401  {
402  myFlagIsCW = false;
403  isOK = true;
404  }
405  myFlagIsInit = true;
406  }
407 
408  if (isOK)
409  {
410  ++myEnd;
411  return true;
412  } else return false;
413 }
414 
415 template <typename TConstIterator>
416 inline
417 bool
419 {
420  ASSERT( myPreimagePtr.get() != 0 );
421  ConstIterator it( myBegin );
422  --it;
423  Pair aPair( *it );
424  bool isOK = false;
425 
426  if (myFlagIsInit)
427  { //initialized
428 
429  if (myFlagIsCW)
430  isOK = myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second );
431  else
432  isOK = myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second );
433 
434  } else
435  { //not initialized yet
436  if ( myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second ) )
437  isOK = true;
438  else if ( myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second ) )
439  isOK = true;
440  }
441 
442  return isOK;
443 }
444 
445 template <typename TConstIterator>
446 inline
447 bool
449 {
450  ASSERT( myPreimagePtr.get() != 0 );
451  ConstIterator it( myBegin );
452  --it;
453  Pair aPair( *it );
454  bool isOK = false;
455 
456  if (myFlagIsInit)
457  { //initialized
458 
459  if (myFlagIsCW)
460  isOK = myPreimagePtr->addBack( aPair.first, aPair.second );
461  else
462  isOK = myPreimagePtr->addFront( aPair.first, aPair.second );
463 
464  } else
465  { //not initialized yet
466  if ( myPreimagePtr->addBack( aPair.first, aPair.second ) )
467  {
468  myFlagIsCW = true;
469  isOK = true;
470  }
471  else if ( myPreimagePtr->addFront( aPair.first, aPair.second ) )
472  {
473  myFlagIsCW = false;
474  isOK = true;
475  }
476  myFlagIsInit = true;
477  }
478 
479  if (isOK)
480  {
481  myBegin = it;
482  return true;
483  } else return false;
484 }
485 
487 // Display :
488 
489 template <typename TConstIterator>
490 inline
491 void
493 {
494  out << std::endl;
495  out << "[GeometricalDSS]" << std::endl;
496  if (isValid())
497  {
498  Pair firstPair( *myBegin );
499  out << "\t From " << firstPair.first << firstPair.second << std::endl;
500  ConstIterator it (myEnd);
501  --it;
502  Pair lastPair( *it );
503  out << "\t To " << lastPair.first << lastPair.second << std::endl;
504  ASSERT( myPreimagePtr.get() != 0 );
505  out << (*myPreimagePtr);
506  }
507  else
508  {
509  out << "\t not valid" << std::endl;
510  }
511  out << "[end of GeometricalDSS]" << std::endl;
512 }
513 
514 template <typename TConstIterator>
515 inline
516 std::string
518 {
519  return "GeometricalDSS";
520 }
521 
523 // Implementation of inline functions //
524 
525 template <typename TConstIterator>
526 inline
527 std::ostream&
528 DGtal::operator<< ( std::ostream & out,
529  const GeometricalDSS<TConstIterator> & object )
530 {
531  object.selfDisplay( out );
532  return out;
533 }
534 
535 // //
537 
538