Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
Angle.cpp
1
5
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "Angle.h"
8
9#include <cmath>
10
11#include <QDebug>
12#include <QObject>
13
14#include "Constants.h"
15#include "IString.h"
16#include "IException.h"
17#include "SpecialPixel.h"
18
19namespace Isis {
25 m_radians = Null;
26 }
27
34 Angle::Angle(double angle, Units unit) {
35 setAngle(angle, unit);
36 }
37
38
44 Angle::Angle(const Angle& fromAngle) {
45 m_radians = fromAngle.m_radians;
46 }
47
48
56 QString::SectionFlag flag = QString::SectionSkipEmpty;
57 bool degreesSucceeded, minutesSucceeded, secondsSucceeded;
58
59 double degrees = angle.section(' ', 0, 0, flag).toDouble(&degreesSucceeded);
60 double minutes = angle.section(' ', 1, 1, flag).toDouble(&minutesSucceeded);
61 double seconds = angle.section(' ', 2, 2, flag).toDouble(&secondsSucceeded);
62
63 if (!(degreesSucceeded && minutesSucceeded && secondsSucceeded) ) {
64 QString msg = QObject::tr("[%1] is not a vaid input to Angle. It needs to be of the form: "
65 "\"dd mm ss.ss\"").arg(angle);
66 throw IException(IException::Programmer, msg, _FILEINFO_);
67 }
68
69 //if the first digit is '-', everything should be negative
70 if (degrees < 0) {
71 minutes = -minutes;
72 seconds = -seconds;
73 }
74
75 double decimalDegrees = degrees + minutes/60.0 + seconds/3600.0;
76 setAngle(decimalDegrees, Angle::Degrees);
77 }
78
79
85 // Help prevent any accidental reuse of an Angle object
86 m_radians = Null;
87 }
88
89
96 bool Angle::isValid() const {
97 return m_radians != Isis::Null; // returns false if the value is Null or any other special value
98// return IsValidPixel(m_radians); // returns false if the value is Null or any other special value
99 }
100
101
108 return Angle(360, Degrees);
109 }
110
111
119 Angle Angle::operator+(const Angle& angle2) const {
120 if(!isValid() || !angle2.isValid()) return Angle();
121
122 double ourAngle = radians();
123 double otherAngle = angle2.radians();
124
125 return Angle(ourAngle + otherAngle, Radians);
126 }
127
128
137 Angle Angle::operator-(const Angle& angle2) const {
138 if(!isValid() || !angle2.isValid()) return Angle();
139
140 double ourAngle = radians();
141 double otherAngle = angle2.radians();
142
143 return Angle(ourAngle - otherAngle, Radians);
144 }
145
146
155 Angle Angle::operator*(double value) const {
156 if(!isValid()) return Angle();
157
158 return Angle(radians() * value, Radians);
159 }
160
161
171 Angle operator *(double mult, Angle angle) {
172 return angle * mult;
173 }
174
175
182 Angle Angle::operator/(double value) const {
183 if(!isValid()) return Angle();
184
185 return Angle(radians() / value, Radians);
186 }
187
188
195 double Angle::operator/(Angle value) const {
196 if(!isValid() || !value.isValid()) return Null;
197
198 return radians() / value.radians();
199 }
200
201
209 bool Angle::operator<(const Angle& angle2) const {
210 if(!isValid() || !angle2.isValid()) {
211 IString msg = "Cannot compare a invalid angles with the < operator";
212 throw IException(IException::Programmer, msg, _FILEINFO_);
213 }
214 // != comparison allows for angles that are considered equal to be treated
215 // as being equal. == operator override uses qFuzzyCompare().
216 return (angle(Radians) < angle2.angle(Radians)) && *this != angle2;
217 }
218
219
227 bool Angle::operator>(const Angle& angle2) const {
228 if(!isValid() || !angle2.isValid()) {
229 IString msg = "Cannot compare a invalid angles with the > operator";
230 throw IException(IException::Programmer, msg, _FILEINFO_);
231 }
232 // != comparison allows for angles that are considered equal to be treated
233 // as being equal. == operator override uses qFuzzyCompare().
234 return (angle(Radians) > angle2.angle(Radians)) && *this != angle2;
235 }
236
237
244 QString Angle::toString(bool includeUnits) const {
245 QString textResult = "";
246
247 if (isValid()) {
248 textResult = Isis::toString(degrees());
249
250 if (includeUnits)
251 textResult += " degrees";
252 }
253
254 return textResult;
255 }
256
257
267 double Angle::unitWrapValue(const Units& unit) const {
268 switch (unit) {
269 case Radians:
270 return PI * 2.;
271 break;
272
273 case Degrees:
274 return 360.;
275 break;
276 }
277
278 IString msg = "Angle can not interpret the enumerated value [" +
279 IString(unit) + "] as a unit";
280 throw IException(IException::Programmer, msg, _FILEINFO_);
281 }
282
283
290 double Angle::angle(const Units& unit) const {
291 // Don't do math on special pixels
292 if(m_radians == Null) {
293 return Null;
294 }
295
296 double angleValue = Null;
297
298 switch (unit) {
299 case Radians:
300 angleValue = m_radians;
301 break;
302
303 case Degrees:
304 angleValue = m_radians * RAD2DEG;
305 break;
306 }
307
308 if(angleValue == Null) {
309 IString msg = "Angle can not interpret the enumerated value [" +
310 IString(unit) + "] as a unit";
311 throw IException(IException::Programmer, msg, _FILEINFO_);
312 }
313
314 return angleValue;
315 }
316
317
324 void Angle::setAngle(const double &angle,const Units& unit) {
325 // Don't allow non-Null special pixels, Null means no value
326 if (IsSpecial(angle) && angle != Null) {
327 IString msg = "Angle cannot be a non-Null special pixel";
328 throw IException(IException::Programmer, msg, _FILEINFO_);
329 }
330
331 // Don't do math on special pixels
332 if(angle == Null) {
333 m_radians = Null;
334 return;
335 }
336
337 switch (unit) {
338 case Radians:
340 break;
341
342 case Degrees:
343 m_radians = angle * DEG2RAD;
344 break;
345
346 default:
347 IString msg = "Angle can not interpret the enumerated value [" +
348 IString(unit) + "] as a unit";
349 throw IException(IException::Programmer, msg, _FILEINFO_);
350 }
351 }
352
353}
354
355
369QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint) {
370 dbg.nospace() << angleToPrint.radians() << " <radians> ("
371 << angleToPrint.degrees() << " <degrees>)";
372
373 return dbg.space();
374}
Defines an angle and provides unit conversions.
Definition Angle.h:45
bool operator>(const Angle &angle2) const
Test if the other angle is greater than the current angle.
Definition Angle.cpp:227
bool isValid() const
This indicates whether we have a legitimate angle stored or are in an unset, or invalid,...
Definition Angle.cpp:96
Angle operator-(const Angle &angle2) const
Subtract angle value from another and return the resulting angle.
Definition Angle.cpp:137
virtual ~Angle()
Destroys the angle object.
Definition Angle.cpp:84
Angle()
Constructs a blank angle object which needs a value to be set in order to do any calculations.
Definition Angle.cpp:24
virtual void setAngle(const double &angle, const Units &unit)
Set angle value in desired units.
Definition Angle.cpp:324
bool operator<(const Angle &angle2) const
Test if the other angle is less than the current angle.
Definition Angle.cpp:209
double unitWrapValue(const Units &unit) const
Return wrap value in desired units.
Definition Angle.cpp:267
double m_radians
The angle measure, always stored in radians.
Definition Angle.h:258
double radians() const
Convert an angle to a double.
Definition Angle.h:226
double degrees() const
Get the angle in units of Degrees.
Definition Angle.h:232
Angle operator*(double value) const
Multiply this angle by a double and return the resulting angle.
Definition Angle.cpp:155
static Angle fullRotation()
Makes an angle to represent a full rotation (0-360 or 0-2pi).
Definition Angle.cpp:107
Angle operator+(const Angle &angle2) const
Add angle value to another.
Definition Angle.cpp:119
Angle operator/(double value) const
Divide this angle by a double.
Definition Angle.cpp:182
virtual double angle(const Units &unit) const
Return angle value in desired units.
Definition Angle.cpp:290
Units
The set of usable angle measurement units.
Definition Angle.h:49
@ Degrees
Degrees are generally considered more human readable, 0-360 is one circle, however most math does not...
Definition Angle.h:56
@ Radians
Radians are generally used in mathematical equations, 0-2*PI is one circle, however these are more di...
Definition Angle.h:63
virtual QString toString(bool includeUnits=true) const
Get the angle in human-readable form.
Definition Angle.cpp:244
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(const LinearAlgebra::Vector &vector, int precision)
A global function to format LinearAlgebra::Vector as a QString with the given precision.
Angle operator*(double mult, Angle angle)
Multiply this angle by a double and return the resulting angle.
Definition Angle.cpp:171