DGtal 1.4.0
Loading...
Searching...
No Matches
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// ------------------------------------------------------------------------
41template <typename TPoint, typename TDetComputer>
42inline
43DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
44::PolarPointComparatorBy2x2DetComputer()
45 : myPole( NumberTraits<Coordinate>::ZERO, NumberTraits<Coordinate>::ZERO ),
46 myDetComputer(),
47 myThresholder()
48{
49}
50
51// ------------------------------------------------------------------------
52template <typename TPoint, typename TDetComputer>
53inline
54DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
55::PolarPointComparatorBy2x2DetComputer(const Point& aPoint)
56: myPole( aPoint ),
57 myDetComputer(),
58 myThresholder()
59{
60}
61
62// ------------------------------------------------------------------------
63template <typename TPoint, typename TDetComputer>
64inline
65DGtal::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// ------------------------------------------------------------------------
74template <typename TPoint, typename TDetComputer>
75inline
76DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>&
77DGtal::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// ------------------------------------------------------------------------
90template <typename TPoint, typename TDetComputer>
91inline
92void
93DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
94::setPole ( const Point& aPole )
95{
96 myPole = aPole;
97}
98
99// ------------------------------------------------------------------------
100template <typename TPoint, typename TDetComputer>
101inline
102void
103DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
104::selfDisplay ( std::ostream & out ) const
105{
106 out << "[PolarPointComparatorBy2x2DetComputer]";
107}
108
109// ------------------------------------------------------------------------
110template <typename TPoint, typename TDetComputer>
111inline
112bool
113DGtal::functors::PolarPointComparatorBy2x2DetComputer<TPoint,TDetComputer>
114::isValid() const
115{
116 return true;
117}
118
119// ------------------------------------------------------------------------
120template <typename TPoint, typename TDetComputer>
121inline
122bool
123DGtal::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// ------------------------------------------------------------------------
149template <typename TPoint, typename TDetComputer>
150inline
151bool
152DGtal::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
212template <typename TPoint, typename TDetComputer>
213inline
214std::ostream&
215DGtal::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