DGtal 1.3.0
Loading...
Searching...
No Matches
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 */
52inline
53float
54DGtal::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 */
70inline
71bool
72DGtal::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 */
89inline
90float
91DGtal::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 */
103inline
104float
105DGtal::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 */
118inline
119float
120DGtal::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 */
132inline
133float
134DGtal::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 */
145inline
146double
147DGtal::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 */
163inline
164bool
165DGtal::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 */
182inline
183double
184DGtal::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 */
196inline
197double
198DGtal::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 */
211inline
212double
213DGtal::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 */
225inline
226double
227DGtal::AngleComputer::max( double i, double j )
228{
229 return less( i, j ) ? j : i;
230}
231
232
233
234
235// //
236///////////////////////////////////////////////////////////////////////////////
237
238