Isis 3 Programmer Reference
Angle.cpp
Go to the documentation of this file.
1 
22 #include "Angle.h"
23 
24 #include <cmath>
25 
26 #include <QDebug>
27 
28 #include "Constants.h"
29 #include "IString.h"
30 #include "IException.h"
31 #include "SpecialPixel.h"
32 
33 namespace Isis {
39  m_radians = Null;
40  }
41 
48  Angle::Angle(double angle, Units unit) {
49  setAngle(angle, unit);
50  }
51 
52 
58  Angle::Angle(const Angle& fromAngle) {
59  m_radians = fromAngle.m_radians;
60  }
61 
62 
69  Angle::Angle(QString angle) {
70  QString::SectionFlag flag = QString::SectionSkipEmpty;
71  bool degreesSucceeded, minutesSucceeded, secondsSucceeded;
72 
73  double degrees = angle.section(' ', 0, 0, flag).toDouble(&degreesSucceeded);
74  double minutes = angle.section(' ', 1, 1, flag).toDouble(&minutesSucceeded);
75  double seconds = angle.section(' ', 2, 2, flag).toDouble(&secondsSucceeded);
76 
77  if (!(degreesSucceeded && minutesSucceeded && secondsSucceeded) ) {
78  QString msg = QObject::tr("[%1] is not a vaid input to Angle. It needs to be of the form: "
79  "\"dd mm ss.ss\"").arg(angle);
81  }
82 
83  //if the first digit is '-', everything should be negative
84  if (degrees < 0) {
85  minutes = -minutes;
86  seconds = -seconds;
87  }
88 
89  double decimalDegrees = degrees + minutes/60.0 + seconds/3600.0;
90  setAngle(decimalDegrees, Angle::Degrees);
91  }
92 
93 
99  // Help prevent any accidental reuse of an Angle object
100  m_radians = Null;
101  }
102 
103 
110  bool Angle::isValid() const {
111  return m_radians != Isis::Null; // returns false if the value is Null or any other special value
112 // return IsValidPixel(m_radians); // returns false if the value is Null or any other special value
113  }
114 
115 
122  return Angle(360, Degrees);
123  }
124 
125 
133  Angle Angle::operator+(const Angle& angle2) const {
134  if(!isValid() || !angle2.isValid()) return Angle();
135 
136  double ourAngle = radians();
137  double otherAngle = angle2.radians();
138 
139  return Angle(ourAngle + otherAngle, Radians);
140  }
141 
142 
151  Angle Angle::operator-(const Angle& angle2) const {
152  if(!isValid() || !angle2.isValid()) return Angle();
153 
154  double ourAngle = radians();
155  double otherAngle = angle2.radians();
156 
157  return Angle(ourAngle - otherAngle, Radians);
158  }
159 
160 
169  Angle Angle::operator*(double value) const {
170  if(!isValid()) return Angle();
171 
172  return Angle(radians() * value, Radians);
173  }
174 
175 
185  Angle operator *(double mult, Angle angle) {
186  return angle * mult;
187  }
188 
189 
196  Angle Angle::operator/(double value) const {
197  if(!isValid()) return Angle();
198 
199  return Angle(radians() / value, Radians);
200  }
201 
202 
209  double Angle::operator/(Angle value) const {
210  if(!isValid() || !value.isValid()) return Null;
211 
212  return radians() / value.radians();
213  }
214 
215 
223  bool Angle::operator<(const Angle& angle2) const {
224  if(!isValid() || !angle2.isValid()) {
225  IString msg = "Cannot compare a invalid angles with the < operator";
227  }
228  // != comparison allows for angles that are considered equal to be treated
229  // as being equal. == operator override uses qFuzzyCompare().
230  return (angle(Radians) < angle2.angle(Radians)) && *this != angle2;
231  }
232 
233 
241  bool Angle::operator>(const Angle& angle2) const {
242  if(!isValid() || !angle2.isValid()) {
243  IString msg = "Cannot compare a invalid angles with the > operator";
245  }
246  // != comparison allows for angles that are considered equal to be treated
247  // as being equal. == operator override uses qFuzzyCompare().
248  return (angle(Radians) > angle2.angle(Radians)) && *this != angle2;
249  }
250 
251 
258  QString Angle::toString(bool includeUnits) const {
259  QString textResult = "";
260 
261  if (isValid()) {
262  textResult = Isis::toString(degrees());
263 
264  if (includeUnits)
265  textResult += " degrees";
266  }
267 
268  return textResult;
269  }
270 
271 
281  double Angle::unitWrapValue(const Units& unit) const {
282  switch (unit) {
283  case Radians:
284  return PI * 2.;
285  break;
286 
287  case Degrees:
288  return 360.;
289  break;
290  }
291 
292  IString msg = "Angle can not interpret the enumerated value [" +
293  IString(unit) + "] as a unit";
295  }
296 
297 
304  double Angle::angle(const Units& unit) const {
305  // Don't do math on special pixels
306  if(m_radians == Null) {
307  return Null;
308  }
309 
310  double angleValue = Null;
311 
312  switch (unit) {
313  case Radians:
314  angleValue = m_radians;
315  break;
316 
317  case Degrees:
318  angleValue = m_radians * RAD2DEG;
319  break;
320  }
321 
322  if(angleValue == Null) {
323  IString msg = "Angle can not interpret the enumerated value [" +
324  IString(unit) + "] as a unit";
326  }
327 
328  return angleValue;
329  }
330 
331 
338  void Angle::setAngle(const double &angle,const Units& unit) {
339  // Don't allow non-Null special pixels, Null means no value
340  if (IsSpecial(angle) && angle != Null) {
341  IString msg = "Angle cannot be a non-Null special pixel";
343  }
344 
345  // Don't do math on special pixels
346  if(angle == Null) {
347  m_radians = Null;
348  return;
349  }
350 
351  switch (unit) {
352  case Radians:
353  m_radians = angle;
354  break;
355 
356  case Degrees:
357  m_radians = angle * DEG2RAD;
358  break;
359 
360  default:
361  IString msg = "Angle can not interpret the enumerated value [" +
362  IString(unit) + "] as a unit";
364  }
365  }
366 
367 }
368 
369 
383 QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint) {
384  dbg.nospace() << angleToPrint.radians() << " <radians> ("
385  << angleToPrint.degrees() << " <degrees>)";
386 
387  return dbg.space();
388 }
Angle operator*(double value) const
Multiply this angle by a double and return the resulting angle.
Definition: Angle.cpp:169
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
Units
The set of usable angle measurement units.
Definition: Angle.h:66
const double PI
The mathematical constant PI.
Definition: Constants.h:56
Angle operator-(const Angle &angle2) const
Subtract angle value from another and return the resulting angle.
Definition: Angle.cpp:151
double radians() const
Convert an angle to a double.
Definition: Angle.h:243
virtual void setAngle(const double &angle, const Units &unit)
Set angle value in desired units.
Definition: Angle.cpp:338
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
Angle operator*(double mult, Angle angle)
Multiply this angle by a double and return the resulting angle.
Definition: Angle.cpp:185
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
double degrees() const
Get the angle in units of Degrees.
Definition: Angle.h:249
Angle operator+(const Angle &angle2) const
Add angle value to another.
Definition: Angle.cpp:133
QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint)
Display an Angle for a debugging statement.
Definition: Angle.cpp:383
Degrees are generally considered more human readable, 0-360 is one circle, however most math does not...
Definition: Angle.h:73
Angle()
Constructs a blank angle object which needs a value to be set in order to do any calculations.
Definition: Angle.cpp:38
virtual QString toString(bool includeUnits=true) const
Get the angle in human-readable form.
Definition: Angle.cpp:258
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:212
double unitWrapValue(const Units &unit) const
Return wrap value in desired units.
Definition: Angle.cpp:281
const double DEG2RAD
Multiplier for converting from degrees to radians.
Definition: Constants.h:59
static Angle fullRotation()
Makes an angle to represent a full rotation (0-360 or 0-2pi).
Definition: Angle.cpp:121
Defines an angle and provides unit conversions.
Definition: Angle.h:62
bool operator<(const Angle &angle2) const
Test if the other angle is less than the current angle.
Definition: Angle.cpp:223
Angle operator/(double value) const
Divide this angle by a double.
Definition: Angle.cpp:196
virtual ~Angle()
Destroys the angle object.
Definition: Angle.cpp:98
virtual double angle(const Units &unit) const
Return angle value in desired units.
Definition: Angle.cpp:304
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool isValid() const
This indicates whether we have a legitimate angle stored or are in an unset, or invalid, state.
Definition: Angle.cpp:110
Radians are generally used in mathematical equations, 0-2*PI is one circle, however these are more di...
Definition: Angle.h:80
const double RAD2DEG
Multiplier for converting from radians to degrees.
Definition: Constants.h:60
double m_radians
The angle measure, always stored in radians.
Definition: Angle.h:275
bool operator>(const Angle &angle2) const
Test if the other angle is greater than the current angle.
Definition: Angle.cpp:241