DGtal  1.5.beta
AngleComputer.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 AngleComputer.ih
19  *
20  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
21  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
22  *
23  * @author (backported by) Bertrand Kerautret (\c kerautre@loria.fr )
24  * LORIA (CNRS, UMR 7503), University of Nancy, France
25  *
26  * @date 2011/08/31
27  *
28  * Implementation of inline methods defined in AngleComputer.h
29  *
30  * This file is part of the DGtal library.
31  */
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // IMPLEMENTATION of inline methods.
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 //////////////////////////////////////////////////////////////////////////////
38 #include <cstdlib>
39 //////////////////////////////////////////////////////////////////////////////
40 
41 
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // Implementation of inline methods //
45 
46 
47 
48 /**
49  * @param i any angle.
50  * @return the corresponding angle in [0:2pi[
51  */
52 inline
53 float
54 DGtal::AngleComputer::cast( float i )
55 {
56  while ( i < 0.0f ) i += (float) (M_PI*2.0);
57  while ( i > (double)(M_PI*2.0) ) i -= (float)(M_PI*2.0);
58  return i;
59 }
60 
61 
62 /**
63  * Less comparator modulo. Be careful, modulo comparisons have no
64  * sense when the absolute difference of the values are around pi.
65  *
66  * @param i any angle in [0:2pi[
67  * @param j any angle in [0:2pi[
68  * @return 'true' if [i] strictly precedes [j] in a window 'pi'.
69  */
70 inline
71 bool
72 DGtal::AngleComputer::less( float i, float j )
73 {
74  float d = j - i;
75  if ( d > 0.0f )
76  return d < M_PI;
77  else
78  return (-d) >= M_PI;
79 }
80 
81 
82 /**
83  * Performs j - i modulo 2pi, assuming less(i,j) is true.
84  *
85  * @param j any angle in [0:2pi[
86  * @param i any angle in [0:2pi[
87  * @return the value j - i, always positive.
88  */
89 inline
90 float
91 DGtal::AngleComputer::posDiff( float j, float i )
92 {
93  return ( i <= j ) ? j - i : j + (float)(M_PI*2.0) - i;
94 }
95 
96 /**
97  * Performs j - i, assuming th result is in [-pi:pi[
98  *
99  * @param j any angle in [0:2pi[
100  * @param i any angle in [0:2pi[
101  * @return the value j - i, always positive.
102  */
103 inline
104 float
105 DGtal::AngleComputer::deviation( float j, float i )
106 {
107  return less( i, j ) ? posDiff( j, i ) : -posDiff( i, j );
108 }
109 
110 
111 /**
112  * Equivalent to 'less( i, j ) ? i : j'.
113  *
114  * @param i any angle in [0:2pi[
115  * @param j any angle in [0:2pi[
116  * @return the smallest angle of [i] and [j] in a window 'pi'.
117  */
118 inline
119 float
120 DGtal::AngleComputer::min( float i, float j )
121 {
122  return less( i, j ) ? i : j;
123 }
124 
125 /**
126  * Equivalent to 'less( i, j ) ? j : i'.
127  *
128  * @param i any angle in [0:2pi[
129  * @param j any angle in [0:2pi[
130  * @return the greatest angle of [i] and [j] in a window 'pi'.
131  */
132 inline
133 float
134 DGtal::AngleComputer::max( float i, float j )
135 {
136  return less( i, j ) ? j : i;
137 }
138 
139 
140 
141 /**
142  * @param i any angle.
143  * @return the corresponding angle in [0:2pi[
144  */
145 inline
146 double
147 DGtal::AngleComputer::cast( double i )
148 {
149  while ( i < 0.0 ) i += (float)(M_PI*2.0);
150  while ( i > (float)(M_PI*2.0) ) i -= (float)(M_PI*2.0);
151  return i;
152 }
153 
154 
155 /**
156  * Less comparator modulo. Be careful, modulo comparisons have no
157  * sense when the absolute difference of the values are around pi.
158  *
159  * @param i any angle in [0:2pi[
160  * @param j any angle in [0:2pi[
161  * @return 'true' if [i] strictly precedes [j] in a window 'pi'.
162  */
163 inline
164 bool
165 DGtal::AngleComputer::less( double i, double j )
166 {
167  double d = j - i;
168  if ( d > 0.0 )
169  return d < M_PI;
170  else
171  return (-d) >= M_PI;
172 }
173 
174 
175 /**
176  * Performs j - i modulo 2pi, assuming less(i,j) is true.
177  *
178  * @param j any angle in [0:2pi[
179  * @param i any angle in [0:2pi[
180  * @return the value j - i, always positive.
181  */
182 inline
183 double
184 DGtal::AngleComputer::posDiff( double j, double i )
185 {
186  return ( i <= j ) ? j - i : j + (float)(M_PI*2.0) - i;
187 }
188 
189 /**
190  * Performs j - i, assuming th result is in [-pi:pi[
191  *
192  * @param j any angle in [0:2pi[
193  * @param i any angle in [0:2pi[
194  * @return the value j - i, always positive.
195  */
196 inline
197 double
198 DGtal::AngleComputer::deviation( double j, double i )
199 {
200  return less( i, j ) ? posDiff( j, i ) : -posDiff( i, j );
201 }
202 
203 
204 /**
205  * Equivalent to 'less( i, j ) ? i : j'.
206  *
207  * @param i any angle in [0:2pi[
208  * @param j any angle in [0:2pi[
209  * @return the smallest angle of [i] and [j] in a window 'pi'.
210  */
211 inline
212 double
213 DGtal::AngleComputer::min( double i, double j )
214 {
215  return less( i, j ) ? i : j;
216 }
217 
218 /**
219  * Equivalent to 'less( i, j ) ? j : i'.
220  *
221  * @param i any angle in [0:2pi[
222  * @param j any angle in [0:2pi[
223  * @return the greatest angle of [i] and [j] in a window 'pi'.
224  */
225 inline
226 double
227 DGtal::AngleComputer::max( double i, double j )
228 {
229  return less( i, j ) ? j : i;
230 }
231 
232 
233 
234 
235 // //
236 ///////////////////////////////////////////////////////////////////////////////
237 
238