USGS

Isis 3.0 Object Programmers' Reference

Home

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 
229  return (angle(Radians) < angle2.angle(Radians)) && *this != angle2;
230  }
231 
232 
240  bool Angle::operator>(const Angle& angle2) const {
241  if(!isValid() || !angle2.isValid()) {
242  IString msg = "Cannot compare a invalid angles with the > operator";
244  }
245 
246  return (angle(Radians) > angle2.angle(Radians)) && *this != angle2;
247  }
248 
249 
256  QString Angle::toString(bool includeUnits) const {
257  QString textResult = "";
258 
259  if (isValid()) {
260  textResult = Isis::toString(degrees());
261 
262  if (includeUnits)
263  textResult += " degrees";
264  }
265 
266  return textResult;
267  }
268 
269 
279  double Angle::unitWrapValue(const Units& unit) const {
280  switch (unit) {
281  case Radians:
282  return PI * 2.;
283  break;
284 
285  case Degrees:
286  return 360.;
287  break;
288  }
289 
290  IString msg = "Angle can not interpret the enumerated value [" +
291  IString(unit) + "] as a unit";
293  }
294 
295 
302  double Angle::angle(const Units& unit) const {
303  // Don't do math on special pixels
304  if(m_radians == Null) {
305  return Null;
306  }
307 
308  double angleValue = Null;
309 
310  switch (unit) {
311  case Radians:
312  angleValue = m_radians;
313  break;
314 
315  case Degrees:
316  angleValue = m_radians * RAD2DEG;
317  break;
318  }
319 
320  if(angleValue == Null) {
321  IString msg = "Angle can not interpret the enumerated value [" +
322  IString(unit) + "] as a unit";
324  }
325 
326  return angleValue;
327  }
328 
329 
336  void Angle::setAngle(const double &angle,const Units& unit) {
337  // Don't allow non-Null special pixels, Null means no value
338  if (IsSpecial(angle) && angle != Null) {
339  IString msg = "Angle cannot be a non-Null special pixel";
341  }
342 
343  // Don't do math on special pixels
344  if(angle == Null) {
345  m_radians = Null;
346  return;
347  }
348 
349  switch (unit) {
350  case Radians:
351  m_radians = angle;
352  break;
353 
354  case Degrees:
355  m_radians = angle * DEG2RAD;
356  break;
357 
358  default:
359  IString msg = "Angle can not interpret the enumerated value [" +
360  IString(unit) + "] as a unit";
362  }
363  }
364 
365 }
366 
367 
379 QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint) {
380  dbg.nospace() << angleToPrint.radians() << " <radians> ("
381  << angleToPrint.degrees() << " <degrees>)";
382 
383  return dbg.space();
384 }