1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #include "SbGeoAngle.h"
34 #include <Inventor/SbBasic.h>
35 #include <cmath>
36 #include <cassert>
37 
38 #define ANGLE_TOLERANCE 1e-5
39 
40 /*
41   Default constructor radians
42 */
43 
SbGeoAngle(const double d)44 SbGeoAngle::SbGeoAngle(const double d)
45 {
46   this->a = d;
47 }
48 
49 /*
50   Default constructor degrees
51 */
52 
SbGeoAngle(const double deg,const double min,const double sec,const char direction)53 SbGeoAngle::SbGeoAngle(const double deg,
54                        const double min,
55                        const double sec,
56                        const char direction)
57 {
58   assert(((direction == 'N') ||
59          (direction == 'S') ||
60          (direction == 'E') ||
61          (direction == 'W')) &&
62          "direction must be either N, S, E or W");
63 
64   this->a = deg * M_PI / 180.0;
65   this->a += (min / 60.0) * M_PI / 180;
66   this->a += (sec / 3600.0) * M_PI / 180;
67   if (direction == 'S') this->a = -this->a;
68 }
69 
70 /*
71   Copy  constructor
72 */
73 
SbGeoAngle(const SbGeoAngle & a)74 SbGeoAngle::SbGeoAngle(const SbGeoAngle & a)
75 {
76   this->a = a.a;
77 }
78 
79 void
setDegree(const int deg)80 SbGeoAngle::setDegree(const int deg)
81 {
82   this->a = deg * M_PI / 180.0;
83 }
84 
85 /*
86   Operators
87  */
88 SbGeoAngle &
operator =(const double d)89 SbGeoAngle::operator=(const double d)
90 {
91   this->a = d;
92   return *this;
93 }
94 
95 SbGeoAngle &
operator =(SbGeoAngle ang)96 SbGeoAngle::operator=(SbGeoAngle ang)
97 {
98   this->a = ang.a;
99   return *this;
100 }
101 
102 double
rad(void) const103 SbGeoAngle::rad(void) const
104 {
105   return this->a;
106 }
107 
108 int
deg(void) const109 SbGeoAngle::deg(void) const
110 {
111   return int(this->degrees());
112 }
113 
114 SbGeoAngle &
operator +=(const SbGeoAngle & a)115 SbGeoAngle::operator+=(const SbGeoAngle & a)
116 {
117   this->a += a.a;
118   return *this;
119 }
120 
121  SbGeoAngle &
operator -=(const SbGeoAngle & a)122 SbGeoAngle::operator-=(const SbGeoAngle & a)
123 {
124   this->a -= a.a;
125   return *this;
126 }
127 
128  SbGeoAngle
operator -() const129 SbGeoAngle::operator-() const
130 {
131   SbGeoAngle p;
132   p.a = -this->a;
133   return p;
134 }
135 
136  SbGeoAngle
operator +(SbGeoAngle a) const137 SbGeoAngle::operator+(SbGeoAngle a) const
138 {
139   a += *this;
140   return a;
141 }
142 
143  SbGeoAngle
operator -(SbGeoAngle a) const144 SbGeoAngle::operator-(SbGeoAngle a) const
145 {
146   a -= *this;
147   return -a;
148 }
149 
150 SbGeoAngle
operator +(double d) const151 SbGeoAngle::operator+(double d) const
152 {
153   return *this + SbGeoAngle(d);
154 }
155 
156 SbGeoAngle
operator -(double d) const157 SbGeoAngle::operator-(double d) const
158 {
159   return *this - SbGeoAngle(d);
160 }
161 
162 
163 SbGeoAngle &
operator *=(double d)164 SbGeoAngle::operator*=(double d)
165 {
166   this->a *= d;
167   return *this;
168 }
169 
170 SbGeoAngle
operator *(double d) const171 SbGeoAngle::operator*(double d) const
172 {
173   return SbGeoAngle(a*d);
174 }
175 
176 SbGeoAngle
operator *(double d,SbGeoAngle a)177 operator*(double d, SbGeoAngle a)
178 {
179   a *= d;
180   return a;
181 }
182 
183 SbGeoAngle
operator +(double d,SbGeoAngle a)184 operator+(double d, SbGeoAngle a)
185 {
186   a += d;
187   return a;
188 }
189 
190 SbGeoAngle
operator -(double d,SbGeoAngle a)191 operator-(double d, SbGeoAngle a)
192 {
193   a -= d;
194   return a;
195 }
196 
197 SbGeoAngle &
operator /=(double d)198 SbGeoAngle::operator/=(double d)
199 {
200   d = 1/d;
201   *this *= d;
202   return *this;
203 }
204 
205  SbGeoAngle
operator /(double d) const206 SbGeoAngle::operator/(double d) const
207 {
208   d = 1/d;
209   return *this * d;
210 }
211 
212  bool
operator ==(const SbGeoAngle & p) const213 SbGeoAngle::operator==(const SbGeoAngle & p) const
214 {
215   return fabs(a - p.a) < ANGLE_TOLERANCE;
216 }
217 
218  bool
operator !=(const SbGeoAngle & a) const219 SbGeoAngle::operator!=(const SbGeoAngle & a) const
220 {
221   return !(*this == a);
222 }
223 
224  bool
operator <(const SbGeoAngle & v) const225 SbGeoAngle::operator<(const SbGeoAngle & v) const
226 {
227   return this->a < v.a;
228 }
229 
230 
231  bool
operator >(const SbGeoAngle & v) const232 SbGeoAngle::operator>(const SbGeoAngle & v) const
233 {
234   return this->a > v.a;
235 }
236 
237  bool
operator <=(const SbGeoAngle & v) const238 SbGeoAngle::operator<=(const SbGeoAngle & v) const
239 {
240   return !(*this > v);
241 }
242 
243 bool
operator >=(const SbGeoAngle & v) const244 SbGeoAngle::operator>=(const SbGeoAngle & v) const
245 {
246   return !(*this < v);
247 }
248 
249 int
minutes() const250 SbGeoAngle::minutes() const
251 {
252   double tmp = this->degrees();
253   tmp -= (double) this->deg();
254 
255   return (int) (tmp * 60.0);
256 }
257 
258 double
seconds() const259 SbGeoAngle::seconds() const
260 {
261   double tmp = this->degrees();
262   tmp -= this->deg() + this->minutes() / 60.0;
263 
264   return tmp * 60.0 * 60.0;
265 }
266 
267 double
degrees(void) const268 SbGeoAngle::degrees(void) const
269 {
270   double tmp = fmod(180.0 * (this->a / M_PI), 360.0);
271   if (tmp > 180.0) {
272     return tmp - 360.0;
273   }
274   return tmp;
275 }
276 
277 #undef ANGLE_TOLERANCE
278