DGtal  1.5.beta
StabbingLineComputer.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file StabbingLineComputer.ih
19  * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2011/09/26
23  *
24  * Implementation of inline methods defined in StabbingLineComputer.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 template <typename TConstIterator>
42 inline
43 DGtal::StabbingLineComputer<TConstIterator>::StabbingLineComputer()
44 :myBegin(), myEnd(), myPreimagePtr(), myFlagIsInit(false), myFlagIsCW(true)
45 {
46 }
47 
48 template <typename TConstIterator>
49 inline
50 DGtal::StabbingLineComputer<TConstIterator>::StabbingLineComputer( const Self& other )
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
58 typename DGtal::StabbingLineComputer<TConstIterator>::Self&
59 DGtal::StabbingLineComputer<TConstIterator>::operator= ( const Self& other )
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
74 DGtal::StabbingLineComputer<TConstIterator>::~StabbingLineComputer()
75 {
76 }
77 
78 template <typename TConstIterator>
79 inline
80 bool
81 DGtal::StabbingLineComputer<TConstIterator>::operator==( const Self& other ) const
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
115 DGtal::StabbingLineComputer<TConstIterator>::operator!=( const Self& other ) const
116 {
117  return !(*this == other);
118 }
119 
120 template <typename TConstIterator>
121 inline
122 typename DGtal::StabbingLineComputer<TConstIterator>::Reverse
123 DGtal::StabbingLineComputer<TConstIterator>::getReverse() const
124 {
125  return Reverse();
126 }
127 
128 
129 template <typename TConstIterator>
130 inline
131 typename DGtal::StabbingLineComputer<TConstIterator>::Self
132 DGtal::StabbingLineComputer<TConstIterator>::getSelf() const
133 {
134  return Self();
135 }
136 
137 
138 
139 ///////////////////////////////////////////////////////////////////////////////
140 // Interface - public :
141 
142 template <typename TConstIterator>
143 inline
144 bool
145 DGtal::StabbingLineComputer<TConstIterator>::isValid() const
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
159 typename DGtal::StabbingLineComputer<TConstIterator>::ConstIterator
160 DGtal::StabbingLineComputer<TConstIterator>::begin() const
161 {
162  return myBegin;
163 }
164 
165 template <typename TConstIterator>
166 inline
167 typename DGtal::StabbingLineComputer<TConstIterator>::ConstIterator
168 DGtal::StabbingLineComputer<TConstIterator>::end() const
169 {
170  return myEnd;
171 }
172 
173 template <typename TConstIterator>
174 inline
175 typename DGtal::StabbingLineComputer<TConstIterator>::Point
176 DGtal::StabbingLineComputer<TConstIterator>::Uf() const
177 {
178  ASSERT( myPreimagePtr.get() != 0 );
179  return myPreimagePtr->Uf();
180 }
181 
182 template <typename TConstIterator>
183 inline
184 typename DGtal::StabbingLineComputer<TConstIterator>::Point
185 DGtal::StabbingLineComputer<TConstIterator>::Ul() const
186 {
187  ASSERT( myPreimagePtr.get() != 0 );
188  return myPreimagePtr->Ul();
189 }
190 
191 template <typename TConstIterator>
192 inline
193 typename DGtal::StabbingLineComputer<TConstIterator>::Point
194 DGtal::StabbingLineComputer<TConstIterator>::Lf() const
195 {
196  ASSERT( myPreimagePtr.get() != 0 );
197  return myPreimagePtr->Lf();
198 }
199 
200 template <typename TConstIterator>
201 inline
202 typename DGtal::StabbingLineComputer<TConstIterator>::Point
203 DGtal::StabbingLineComputer<TConstIterator>::Ll() const
204 {
205  ASSERT( myPreimagePtr.get() != 0 );
206  return myPreimagePtr->Ll();
207 }
208 
209 
210 template <typename TConstIterator>
211 inline
212 typename DGtal::StabbingLineComputer<TConstIterator>::PreimagePtr
213 DGtal::StabbingLineComputer<TConstIterator>::getPreimage() const
214 {
215  ASSERT( myPreimagePtr.get() != 0 );
216  return myPreimagePtr;
217 }
218 
219 
220 template <typename TConstIterator>
221 inline
222 bool
223 DGtal::StabbingLineComputer<TConstIterator>::isClockwiseOriented() const
224 {
225  return myFlagIsCW;
226 }
227 
228 
229 template <typename TConstIterator>
230 inline
231 void
232 DGtal::StabbingLineComputer<TConstIterator>::getParameters(double& alpha, double& beta, double& gamma) const
233 {
234  ASSERT( myPreimagePtr.get() != 0 );
235  myPreimagePtr->getSeparatingStraightLine(alpha, beta, gamma);
236 }
237 
238 template <typename TConstIterator>
239 inline
240 void
241 DGtal::StabbingLineComputer<TConstIterator>::projects(double& x, double& y,
242  const double& alpha, const double& beta, const double& gamma) const
243 {
244  ASSERT( (alpha!=0)||(beta!=0) );
245  double d2 = ( alpha * alpha + beta * beta );
246  double s = beta*x - alpha*y;
247  x = (beta*s - alpha*gamma)/d2;
248  y = -(alpha*s + beta*gamma)/d2;
249 }
250 
251 ///////////////////////////////////////////////////////////////////////////////
252 // Growth operations //
253 
254 template <typename TConstIterator>
255 inline
256 void
257 DGtal::StabbingLineComputer<TConstIterator>::init(const ConstIterator& anIt)
258 {
259  myBegin = anIt;
260  myEnd = anIt;
261  ++myEnd;
262  Pair aPair( *anIt);
263  myPreimagePtr = PreimagePtr( new Preimage( aPair.first, aPair.second, StraightLine() ) );
264 }
265 
266 template <typename TConstIterator>
267 inline
268 bool
269 DGtal::StabbingLineComputer<TConstIterator>::isConcave()
270 {
271  ASSERT( myPreimagePtr.get() != 0 );
272  Pair aPair( *myEnd );
273  bool isExterior = false;
274 
275  if (myFlagIsInit)
276  { //initialized
277 
278  if (myFlagIsCW)
279  isExterior = myPreimagePtr->isLeftExteriorAtTheFront( aPair.first, aPair.second );
280  else
281  isExterior = myPreimagePtr->isRightExteriorAtTheBack( aPair.first, aPair.second );
282 
283  }
284 
285  return isExterior;
286 }
287 
288 template <typename TConstIterator>
289 inline
290 bool
291 DGtal::StabbingLineComputer<TConstIterator>::isConvex()
292 {
293  ASSERT( myPreimagePtr.get() != 0 );
294  Pair aPair( *myEnd );
295  bool isExterior = false;
296 
297  if (myFlagIsInit)
298  { //initialized
299 
300  if (myFlagIsCW)
301  isExterior = myPreimagePtr->isRightExteriorAtTheFront( aPair.first, aPair.second );
302  else
303  isExterior = myPreimagePtr->isLeftExteriorAtTheBack( aPair.first, aPair.second );
304 
305  }
306 
307  return isExterior;
308 }
309 
310 template <typename TConstIterator>
311 inline
312 bool
313 DGtal::StabbingLineComputer<TConstIterator>::isOppositeEndConcave()
314 {
315  ASSERT( myPreimagePtr.get() != 0 );
316  ConstIterator it( myBegin );
317  --it;
318  Pair aPair( *it );
319  bool isExterior = false;
320 
321  if (myFlagIsInit)
322  { //initialized
323 
324  if (myFlagIsCW)
325  isExterior = myPreimagePtr->isRightExteriorAtTheBack( aPair.first, aPair.second );
326  else
327  isExterior = myPreimagePtr->isLeftExteriorAtTheFront( aPair.first, aPair.second );
328 
329  }
330 
331  return isExterior;
332 }
333 
334 template <typename TConstIterator>
335 inline
336 bool
337 DGtal::StabbingLineComputer<TConstIterator>::isOppositeEndConvex()
338 {
339  ASSERT( myPreimagePtr.get() != 0 );
340  ConstIterator it( myBegin );
341  --it;
342  Pair aPair( *it );
343  bool isExterior = false;
344 
345  if (myFlagIsInit)
346  { //initialized
347 
348  if (myFlagIsCW)
349  isExterior = myPreimagePtr->isLeftExteriorAtTheBack( aPair.first, aPair.second );
350  else
351  isExterior = myPreimagePtr->isRightExteriorAtTheFront( aPair.first, aPair.second );
352 
353  }
354 
355  return isExterior;
356 }
357 
358 
359 template <typename TConstIterator>
360 inline
361 bool
362 DGtal::StabbingLineComputer<TConstIterator>::isExtendableFront()
363 {
364  ASSERT( myPreimagePtr.get() != 0 );
365  Pair aPair( *myEnd );
366  bool isOK = false;
367 
368  if (myFlagIsInit)
369  { //initialized
370 
371  if (myFlagIsCW)
372  isOK = myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second );
373  else
374  isOK = myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second );
375 
376  } else
377  { //not initialized yet
378  if ( myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second ) )
379  isOK = true;
380  else if ( myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second ) )
381  isOK = true;
382  }
383 
384  return isOK;
385 }
386 
387 template <typename TConstIterator>
388 inline
389 bool
390 DGtal::StabbingLineComputer<TConstIterator>::extendFront()
391 {
392  ASSERT( myPreimagePtr.get() != 0 );
393  Pair aPair( *myEnd );
394  bool isOK = false;
395 
396  if (myFlagIsInit)
397  { //initialized
398 
399  if (myFlagIsCW)
400  isOK = myPreimagePtr->addFront( aPair.first, aPair.second );
401  else
402  isOK = myPreimagePtr->addBack( aPair.first, aPair.second );
403 
404  } else
405  { //not initialized yet
406  if ( myPreimagePtr->addFront( aPair.first, aPair.second ) )
407  {
408  myFlagIsCW = true;
409  isOK = true;
410  }
411  else if ( myPreimagePtr->addBack( aPair.first, aPair.second ) )
412  {
413  myFlagIsCW = false;
414  isOK = true;
415  }
416  myFlagIsInit = true;
417  }
418 
419  if (isOK)
420  {
421  ++myEnd;
422  return true;
423  } else return false;
424 }
425 
426 template <typename TConstIterator>
427 inline
428 bool
429 DGtal::StabbingLineComputer<TConstIterator>::isExtendableBack()
430 {
431  ASSERT( myPreimagePtr.get() != 0 );
432  ConstIterator it( myBegin );
433  --it;
434  Pair aPair( *it );
435  bool isOK = false;
436 
437  if (myFlagIsInit)
438  { //initialized
439 
440  if (myFlagIsCW)
441  isOK = myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second );
442  else
443  isOK = myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second );
444 
445  } else
446  { //not initialized yet
447  if ( myPreimagePtr->canBeAddedAtTheBack( aPair.first, aPair.second ) )
448  isOK = true;
449  else if ( myPreimagePtr->canBeAddedAtTheFront( aPair.first, aPair.second ) )
450  isOK = true;
451  }
452 
453  return isOK;
454 }
455 
456 template <typename TConstIterator>
457 inline
458 bool
459 DGtal::StabbingLineComputer<TConstIterator>::extendBack()
460 {
461  ASSERT( myPreimagePtr.get() != 0 );
462  ConstIterator it( myBegin );
463  --it;
464  Pair aPair( *it );
465  bool isOK = false;
466 
467  if (myFlagIsInit)
468  { //initialized
469 
470  if (myFlagIsCW)
471  isOK = myPreimagePtr->addBack( aPair.first, aPair.second );
472  else
473  isOK = myPreimagePtr->addFront( aPair.first, aPair.second );
474 
475  } else
476  { //not initialized yet
477  if ( myPreimagePtr->addBack( aPair.first, aPair.second ) )
478  {
479  myFlagIsCW = true;
480  isOK = true;
481  }
482  else if ( myPreimagePtr->addFront( aPair.first, aPair.second ) )
483  {
484  myFlagIsCW = false;
485  isOK = true;
486  }
487  myFlagIsInit = true;
488  }
489 
490  if (isOK)
491  {
492  myBegin = it;
493  return true;
494  } else return false;
495 }
496 
497 ///////////////////////////////////////////////////////////////////////////////
498 // Display :
499 
500 template <typename TConstIterator>
501 inline
502 void
503 DGtal::StabbingLineComputer<TConstIterator>::selfDisplay ( std::ostream & out ) const
504 {
505  out << std::endl;
506  out << "[StabbingLineComputer]" << std::endl;
507  if (isValid())
508  {
509  Pair firstPair( *myBegin );
510  out << "\t From " << firstPair.first << firstPair.second << std::endl;
511  ConstIterator it (myEnd);
512  --it;
513  Pair lastPair( *it );
514  out << "\t To " << lastPair.first << lastPair.second << std::endl;
515  ASSERT( myPreimagePtr.get() != 0 );
516  out << (*myPreimagePtr);
517  }
518  else
519  {
520  out << "\t not valid" << std::endl;
521  }
522  out << "[end of StabbingLineComputer]" << std::endl;
523 }
524 
525 template <typename TConstIterator>
526 inline
527 std::string
528 DGtal::StabbingLineComputer<TConstIterator>::className() const
529 {
530  return "StabbingLineComputer";
531 }
532 
533 ///////////////////////////////////////////////////////////////////////////////
534 // Implementation of inline functions //
535 
536 template <typename TConstIterator>
537 inline
538 std::ostream&
539 DGtal::operator<< ( std::ostream & out,
540  const StabbingLineComputer<TConstIterator> & object )
541 {
542  object.selfDisplay( out );
543  return out;
544 }
545 
546 // //
547 ///////////////////////////////////////////////////////////////////////////////
548 
549