DGtal  1.5.beta
PolarPointComparatorBy2x2DetComputer.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 PolarPointComparatorBy2x2DetComputer.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 2013/12/04
23  *
24  * Implementation of inline methods defined in PolarPointComparatorBy2x2DetComputer.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 
40 // ------------------------------------------------------------------------
41 template <typename TPoint, typename TDetComputer>
42 inline
43 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
44 ::PolarPointComparatorBy2x2DetComputer()
45  : myPole( NumberTraits<Coordinate>::ZERO, NumberTraits<Coordinate>::ZERO ),
46  myDetComputer(),
47  myThresholder()
48 {
49 }
50 
51 // ------------------------------------------------------------------------
52 template <typename TPoint, typename TDetComputer>
53 inline
54 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
55 ::PolarPointComparatorBy2x2DetComputer(const Point& aPoint)
56 : myPole( aPoint ),
57  myDetComputer(),
58  myThresholder()
59 {
60 }
61 
62 // ------------------------------------------------------------------------
63 template <typename TPoint, typename TDetComputer>
64 inline
65 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
66 ::PolarPointComparatorBy2x2DetComputer( const PolarPointComparatorBy2x2DetComputer & other )
67  : myPole( other.myPole ),
68  myDetComputer( other.myDetComputer ),
69  myThresholder( other.myThresholder )
70 {
71 }
72 
73 // ------------------------------------------------------------------------
74 template <typename TPoint, typename TDetComputer>
75 inline
76 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>&
77 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
78 ::operator=( const PolarPointComparatorBy2x2DetComputer & other )
79 {
80  if (this != &other)
81  {
82  myPole = other.myPole;
83  myDetComputer = other.myDetComputer;
84  myThresholder = other.myThresholder;
85  }
86  return *this;
87 }
88 
89 // ------------------------------------------------------------------------
90 template <typename TPoint, typename TDetComputer>
91 inline
92 void
93 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
94 ::setPole ( const Point& aPole )
95 {
96  myPole = aPole;
97 }
98 
99 // ------------------------------------------------------------------------
100 template <typename TPoint, typename TDetComputer>
101 inline
102 void
103 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
104 ::selfDisplay ( std::ostream & out ) const
105 {
106  out << "[PolarPointComparatorBy2x2DetComputer]";
107 }
108 
109 // ------------------------------------------------------------------------
110 template <typename TPoint, typename TDetComputer>
111 inline
112 bool
113 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
114 ::isValid() const
115 {
116  return true;
117 }
118 
119 // ------------------------------------------------------------------------
120 template <typename TPoint, typename TDetComputer>
121 inline
122 bool
123 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
124 ::isBefore( const Vector& aU, const Vector& aV) const
125 {
126  //the two y-coordinates must have the same sign
127  //the y-coordinate of aU must not be zero
128  ASSERT( ( (aU[1] > NumberTraits<Coordinate>::ZERO) &&
129  (aV[1] >= NumberTraits<Coordinate>::ZERO) ) ||
130  ( (aU[1] < NumberTraits<Coordinate>::ZERO) &&
131  (aV[1] <= NumberTraits<Coordinate>::ZERO) ) );
132 
133  ResultInteger det = myDetComputer( aU[0], aU[1], aV[0], aV[1] );
134 
135  if ( det == NumberTraits<ResultInteger>::ZERO )
136  { //aU is before aV iff the norm of aU is smaller than the one of aV.
137  //we compute the determinant of
138  // aV[1]+aU[1] , aV[0]-aU[0] , -aV[0]-aU[0] , aV[1]-aU[1]
139  //which is equal to aV[0]*aV[0] + aV[1]*aV[1] - aU[0]*aU[0] - aU[1]*aU[1]
140  return myThresholder( myDetComputer( aV[1]+aU[1] , aV[0]-aU[0] , -aV[0]-aU[0] , aV[1]-aU[1] ) );
141  }
142  else
143  {
144  return myThresholder( det );
145  }
146 }
147 
148 // ------------------------------------------------------------------------
149 template <typename TPoint, typename TDetComputer>
150 inline
151 bool
152 DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
153 ::operator()(const Point& aPoint1, const Point& aPoint2) const
154 {
155  Vector u = aPoint1 - myPole;
156  Vector v = aPoint2 - myPole;
157 
158  if ( u[1] == NumberTraits<Coordinate>::ZERO )
159  {
160  if ( v[1] == NumberTraits<Coordinate>::ZERO )
161  {
162  if ( u[0] >= NumberTraits<Coordinate>::ZERO )
163  {
164  return ( ( v[0] < NumberTraits<Coordinate>::ZERO )
165  || (v[0] > u[0]) );
166  }
167  else
168  {
169  return (v[0] < u[0]);
170  }
171  }
172  else if ( v[1] > NumberTraits<Coordinate>::ZERO )
173  {
174  return ( u[0] >= NumberTraits<Coordinate>::ZERO );
175  }
176  else // v[1] < 0
177  {
178  return true;
179  }
180  }
181  else if ( u[1] > NumberTraits<Coordinate>::ZERO )
182  {
183  if ( v[1] == NumberTraits<Coordinate>::ZERO )
184  {
185  return ( v[0] < NumberTraits<Coordinate>::ZERO );
186  }
187  else if ( v[1] > NumberTraits<Coordinate>::ZERO )
188  {
189  return isBefore(u, v);
190  }
191  else // v[1] < 0
192  {
193  return true;
194  }
195  }
196  else //u[1] < 0
197  {
198  if ( v[1] < NumberTraits<Coordinate>::ZERO )
199  {
200  return isBefore(u, v);
201  }
202  else //v[1] >= 0
203  {
204  return false;
205  }
206  }
207 }
208 
209 ///////////////////////////////////////////////////////////////////////////////
210 // Implementation of inline functions //
211 
212 template <typename TPoint, typename TDetComputer>
213 inline
214 std::ostream&
215 DGtal::functors::operator<< ( std::ostream & out,
216  const PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer> & object )
217 {
218  object.selfDisplay( out );
219  return out;
220 }
221 
222 // //
223 ///////////////////////////////////////////////////////////////////////////////
224 
225