DGtal 1.4.0
Loading...
Searching...
No Matches
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
41template <typename TConstIterator>
42inline
43DGtal::StabbingLineComputer<TConstIterator>::StabbingLineComputer()
44:myBegin(), myEnd(), myPreimagePtr(), myFlagIsInit(false), myFlagIsCW(true)
45{
46}
47
48template <typename TConstIterator>
49inline
50DGtal::StabbingLineComputer<TConstIterator>::StabbingLineComputer( const Self& other )
51:myBegin(other.myBegin), myEnd(other.myEnd), myPreimagePtr(other.myPreimagePtr),
52myFlagIsInit(other.myFlagIsInit), myFlagIsCW(other.myFlagIsCW)
53{
54}
55
56template <typename TConstIterator>
57inline
58typename DGtal::StabbingLineComputer<TConstIterator>::Self&
59DGtal::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
72template <typename TConstIterator>
73inline
74DGtal::StabbingLineComputer<TConstIterator>::~StabbingLineComputer()
75{
76}
77
78template <typename TConstIterator>
79inline
80bool
81DGtal::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
112template <typename TConstIterator>
113inline
114bool
115DGtal::StabbingLineComputer<TConstIterator>::operator!=( const Self& other ) const
116{
117 return !(*this == other);
118}
119
120template <typename TConstIterator>
121inline
122typename DGtal::StabbingLineComputer<TConstIterator>::Reverse
123DGtal::StabbingLineComputer<TConstIterator>::getReverse() const
124{
125 return Reverse();
126}
127
128
129template <typename TConstIterator>
130inline
131typename DGtal::StabbingLineComputer<TConstIterator>::Self
132DGtal::StabbingLineComputer<TConstIterator>::getSelf() const
133{
134 return Self();
135}
136
137
138
139///////////////////////////////////////////////////////////////////////////////
140// Interface - public :
141
142template <typename TConstIterator>
143inline
144bool
145DGtal::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
157template <typename TConstIterator>
158inline
159typename DGtal::StabbingLineComputer<TConstIterator>::ConstIterator
160DGtal::StabbingLineComputer<TConstIterator>::begin() const
161{
162 return myBegin;
163}
164
165template <typename TConstIterator>
166inline
167typename DGtal::StabbingLineComputer<TConstIterator>::ConstIterator
168DGtal::StabbingLineComputer<TConstIterator>::end() const
169{
170 return myEnd;
171}
172
173template <typename TConstIterator>
174inline
175typename DGtal::StabbingLineComputer<TConstIterator>::Point
176DGtal::StabbingLineComputer<TConstIterator>::Uf() const
177{
178 ASSERT( myPreimagePtr.get() != 0 );
179 return myPreimagePtr->Uf();
180}
181
182template <typename TConstIterator>
183inline
184typename DGtal::StabbingLineComputer<TConstIterator>::Point
185DGtal::StabbingLineComputer<TConstIterator>::Ul() const
186{
187 ASSERT( myPreimagePtr.get() != 0 );
188 return myPreimagePtr->Ul();
189}
190
191template <typename TConstIterator>
192inline
193typename DGtal::StabbingLineComputer<TConstIterator>::Point
194DGtal::StabbingLineComputer<TConstIterator>::Lf() const
195{
196 ASSERT( myPreimagePtr.get() != 0 );
197 return myPreimagePtr->Lf();
198}
199
200template <typename TConstIterator>
201inline
202typename DGtal::StabbingLineComputer<TConstIterator>::Point
203DGtal::StabbingLineComputer<TConstIterator>::Ll() const
204{
205 ASSERT( myPreimagePtr.get() != 0 );
206 return myPreimagePtr->Ll();
207}
208
209
210template <typename TConstIterator>
211inline
212typename DGtal::StabbingLineComputer<TConstIterator>::PreimagePtr
213DGtal::StabbingLineComputer<TConstIterator>::getPreimage() const
214{
215 ASSERT( myPreimagePtr.get() != 0 );
216 return myPreimagePtr;
217}
218
219
220template <typename TConstIterator>
221inline
222bool
223DGtal::StabbingLineComputer<TConstIterator>::isClockwiseOriented() const
224{
225 return myFlagIsCW;
226}
227
228
229template <typename TConstIterator>
230inline
231void
232DGtal::StabbingLineComputer<TConstIterator>::getParameters(double& alpha, double& beta, double& gamma) const
233{
234 ASSERT( myPreimagePtr.get() != 0 );
235 myPreimagePtr->getSeparatingStraightLine(alpha, beta, gamma);
236}
237
238template <typename TConstIterator>
239inline
240void
241DGtal::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
254template <typename TConstIterator>
255inline
256void
257DGtal::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
266template <typename TConstIterator>
267inline
268bool
269DGtal::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
288template <typename TConstIterator>
289inline
290bool
291DGtal::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
310template <typename TConstIterator>
311inline
312bool
313DGtal::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
334template <typename TConstIterator>
335inline
336bool
337DGtal::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
359template <typename TConstIterator>
360inline
361bool
362DGtal::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
387template <typename TConstIterator>
388inline
389bool
390DGtal::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
426template <typename TConstIterator>
427inline
428bool
429DGtal::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
456template <typename TConstIterator>
457inline
458bool
459DGtal::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
500template <typename TConstIterator>
501inline
502void
503DGtal::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
525template <typename TConstIterator>
526inline
527std::string
528DGtal::StabbingLineComputer<TConstIterator>::className() const
529{
530 return "StabbingLineComputer";
531}
532
533///////////////////////////////////////////////////////////////////////////////
534// Implementation of inline functions //
535
536template <typename TConstIterator>
537inline
538std::ostream&
539DGtal::operator<< ( std::ostream & out,
540 const StabbingLineComputer<TConstIterator> & object )
541{
542 object.selfDisplay( out );
543 return out;
544}
545
546// //
547///////////////////////////////////////////////////////////////////////////////
548
549