Isis 3 Programmer Reference
Distance.cpp
Go to the documentation of this file.
1 
20 #include "Distance.h"
21 
22 #include "Displacement.h"
23 #include "IException.h"
24 #include "IString.h"
25 #include "SpecialPixel.h"
26 
27 namespace Isis {
28 
35  }
36 
37 
48  Distance::Distance(double distance, Units distanceUnit) {
49  if(distanceUnit == Pixels)
50  distanceUnit = Meters;
51 
52  setDistance(distance, distanceUnit);
53  }
54 
55 
64  Distance::Distance(double distanceInPixels, double pixelsPerMeter) {
65  setDistance(distanceInPixels / pixelsPerMeter, Meters);
66  }
67 
68 
76  Distance::Distance(const Distance &distanceToCopy) {
77  // Use meters because it is the stored format, no precision loss
78  setDistance(distanceToCopy.meters(), Meters);
79  }
80 
81 
86  // This will help debug memory problems, better to reset to obviously bad
87  // values in case we're used after we're deleted.
89  }
90 
91 
97  double Distance::meters() const {
98  return distance(Meters);
99  }
100 
101 
108  void Distance::setMeters(double distanceInMeters) {
109  setDistance(distanceInMeters, Meters);
110  }
111 
112 
118  double Distance::kilometers() const {
119  return distance(Kilometers);
120  }
121 
122 
130  void Distance::setKilometers(double distanceInKilometers) {
131  setDistance(distanceInKilometers, Kilometers);
132  }
133 
134 
143  double Distance::pixels(double pixelsPerMeter) const {
144  return distance(Meters) * pixelsPerMeter;
145  }
146 
147 
157  void Distance::setPixels(double distanceInPixels, double pixelsPerMeter) {
158  setDistance(distanceInPixels / pixelsPerMeter, Meters);
159  }
160 
161 
167  double Distance::solarRadii() const {
168  return distance(SolarRadii);
169  }
170 
171 
179  void Distance::setSolarRadii(double distanceInSolarRadii) {
180  setDistance(distanceInSolarRadii, SolarRadii);
181  }
182 
183 
189  QString Distance::toString() const {
190  QString string;
191 
192  if (isValid())
193  string = Isis::toString(meters()) + " meters";
194 
195  return string;
196  }
197 
198 
204  bool Distance::isValid() const {
205  return distance(Meters) != Null;
206  }
207 
208 
216  bool Distance::operator >(const Distance &otherDistance) const {
217  if(!isValid() || !otherDistance.isValid()) {
218  IString msg = "Distance has not been initialized, you must initialize "
219  "it first before comparing with another distance using [>]";
221  }
222 
223  return meters() > otherDistance.meters();
224  }
225 
226 
234  bool Distance::operator <(const Distance &otherDistance) const {
235  if(!isValid() || !otherDistance.isValid()) {
236  IString msg = "Distance has not been initialized, you must initialize "
237  "it first before comparing with another distance using [<]";
239  }
240 
241  return meters() < otherDistance.meters();
242  }
243 
244 
251  Distance &Distance::operator =(const Distance &distanceToCopy) {
252  if(this == &distanceToCopy) return *this;
253 
254  setDistance(distanceToCopy.meters(), Meters);
255 
256  return *this;
257  }
258 
259 
266  Distance Distance::operator +(const Distance &distanceToAdd) const {
267  if(!isValid() || !distanceToAdd.isValid()) return Distance();
268 
269  return Distance(meters() + distanceToAdd.meters(), Meters);
270  }
271 
272 
280  Displacement Distance::operator -(const Distance &distanceToSub) const {
281  if(!isValid() || !distanceToSub.isValid()) return Displacement();
282 
283  Displacement result(meters() - distanceToSub.meters(),
285 
286  return result;
287  }
288 
289 
296  double Distance::operator /(const Distance &distanceToDiv) const {
297  if(!isValid() || !distanceToDiv.isValid()) return Null;
298 
299  double result = meters() / distanceToDiv.meters();
300  return result;
301  }
302 
303 
310  Distance Distance::operator /(const double &valueToDiv) const {
311  if(!isValid() || IsSpecial(valueToDiv)) return Distance();
312 
313  Distance result = Distance(meters() / valueToDiv, Meters);
314 
315  return result;
316  }
317 
318 
325  Distance Distance::operator *(const double &valueToMult) const {
326  if(!isValid() || IsSpecial(valueToMult)) return Distance();
327 
328  Distance result = Distance(meters() * valueToMult, Meters);
329 
330  return result;
331  }
332 
333 
341  Distance operator *(double mult, Distance dist) {
342  Distance result = dist * mult;
343  return result;
344  }
345 
346 
352  void Distance::operator +=(const Distance &distanceToAdd) {
353  if(!isValid() || !distanceToAdd.isValid()) {
355  }
356  else {
357  setDistance(meters() + distanceToAdd.meters(), Meters);
358  }
359  }
360 
361 
369  void Distance::operator -=(const Distance &distanceToSub) {
370  if(!isValid() || !distanceToSub.isValid()) {
372  }
373  else {
374  setDistance(meters() - distanceToSub.meters(), Meters);
375  }
376  }
377 
378 
384  void Distance::operator /=(const double &valueToDiv) {
385  if(!isValid() || IsSpecial(valueToDiv)) {
387  }
388  else {
389  setDistance(meters() / valueToDiv, Meters);
390  }
391  }
392 
393 
399  void Distance::operator *=(const double &valueToMult) {
400  if(!isValid() || IsSpecial(valueToMult)) {
402  }
403  else {
404  setDistance(meters() * valueToMult, Meters);
405  }
406  }
407 
408 
417  double Distance::distance(Units distanceUnit) const {
418  double distanceInMeters = m_distanceInMeters;
419  double resultingDistance = Null;
420 
421  if(m_distanceInMeters == Null) return Null;
422 
423  switch(distanceUnit) {
424  case Meters:
425  resultingDistance = distanceInMeters;
426  break;
427 
428  case Kilometers:
429  resultingDistance = distanceInMeters / 1000.0;
430  break;
431 
432  case Pixels: {
433  IString msg = "Cannot call distance() with pixels, ask for another "
434  "unit";
436  break;
437  }
438 
439  case SolarRadii:
440  resultingDistance = distanceInMeters / 6.9599e8;
441  break;
442  }
443 
444  if(resultingDistance == Null) {
445  IString msg = "Distance does not understand the enumerated value [" +
446  IString(distanceUnit) + "] as a unit";
448  }
449 
450  return resultingDistance;
451  }
452 
453 
464  void Distance::setDistance(const double &distance, Units distanceUnit) {
465  double distanceInMeters = Null;
466 
467  if(IsSpecial(distance)) {
469  return;
470  }
471 
472  switch(distanceUnit) {
473  case Meters:
474  distanceInMeters = distance;
475  break;
476 
477  case Kilometers:
478  distanceInMeters = distance * 1000.0;
479  break;
480 
481  case Pixels: {
482  IString msg = "Cannot setDistance with pixels, must convert to another "
483  "unit first";
485  break;
486  }
487 
488  case SolarRadii:
489  distanceInMeters = distance * 6.9599e8;
490  break;
491  }
492 
493  if(distanceInMeters == Null) {
494  IString msg = "Distance does not understand the enumerated value [" +
495  IString(distanceUnit) + "] as a unit";
497  }
498 
499  if (distanceInMeters < 0.0) {
500  IString msg = "Negative distances are not supported, the value [" +
501  IString(distanceInMeters) + " meters] cannot be stored in the Distance "
502  "class";
504  }
505 
506  m_distanceInMeters = distanceInMeters;
507  }
508 }
double meters() const
Get the distance in meters.
Definition: Distance.cpp:97
void operator/=(const double &valueToDiv)
Divide this distance by a value and assign the result to ourself.
Definition: Distance.cpp:384
Distance operator+(const Distance &distanceToAdd) const
Add another distance to this distance (1km + 1m = 1005m)
Definition: Distance.cpp:266
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
bool operator<(const Distance &otherDistance) const
Compare two distances with the less than operator.
Definition: Distance.cpp:234
Units
This is a list of available units to access and store Distances in.
Definition: Distance.h:54
double solarRadii() const
Get the distance in solar radii (a unit of ~696,265km).
Definition: Distance.cpp:167
void operator+=(const Distance &distanceToAdd)
Add and assign the given distance to ourselves.
Definition: Distance.cpp:352
QString toString() const
Get a textual representation of this distance.
Definition: Distance.cpp:189
Displacement operator-(const Distance &distanceToSub) const
Subtract another distance from this distance (1km - 1m = 995m).
Definition: Distance.cpp:280
virtual double distance(Units distanceUnit) const
This is a helper method to access distances in a universal manner with uniform error checking...
Definition: Distance.cpp:417
void operator*=(const double &valueToMult)
Multiply this distance by a value and assign the result to ourself.
Definition: Distance.cpp:399
"Solar radius is a unit of distance used to express the size of stars in astronomy equal to the curre...
Definition: Distance.h:72
double m_distanceInMeters
This is the distance value that this class is encapsulating, always stored in meters.
Definition: Distance.h:172
The distance is being specified in kilometers.
Definition: Distance.h:58
double pixels(double pixelsPerMeter=1.0) const
Get the distance in pixels using the given conversion ratio.
Definition: Distance.cpp:143
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
Distance()
This initializes the distance to an invalid state.
Definition: Distance.cpp:33
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
double kilometers() const
Get the distance in kilometers.
Definition: Distance.cpp:118
The distance is being specified in meters.
Definition: Displacement.h:52
Distance measurement, usually in meters.
Definition: Distance.h:47
double operator/(const Distance &distanceToDiv) const
Divide another distance into this distance (5m / 1m = 5).
Definition: Distance.cpp:296
void setMeters(double distanceInMeters)
Set the distance in meters.
Definition: Distance.cpp:108
void setPixels(double distanceInPixels, double pixelsPerMeter=1.0)
Set the distance in pixels.
Definition: Distance.cpp:157
Distance operator*(const double &valueToMult) const
Multiply this distance by a value (5m * 2 = 10m).
Definition: Distance.cpp:325
bool operator>(const Distance &otherDistance) const
Compare two distances with the greater than operator.
Definition: Distance.cpp:216
virtual ~Distance()
This will free the memory allocated by this instance of the Distance class.
Definition: Distance.cpp:85
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
void setKilometers(double distanceInKilometers)
Set the distance in kilometers.
Definition: Distance.cpp:130
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:212
bool isValid() const
Test if this distance has been initialized or not.
Definition: Distance.cpp:204
void operator-=(const Distance &distanceToSub)
Subtract and assign the given distance from ourself.
Definition: Distance.cpp:369
Displacement is a signed length, usually in meters.
Definition: Displacement.h:43
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
void setSolarRadii(double distanceInSolarRadii)
Set the distance in solar radii.
Definition: Distance.cpp:179
The distance is being specified in meters.
Definition: Distance.h:56
virtual void setDistance(const double &distance, Units distanceUnit)
This is a helper method to set distances in a universal manner with uniform error checking...
Definition: Distance.cpp:464
The distance is being specified in pixels.
Definition: Distance.h:60
Distance & operator=(const Distance &distanceToCopy)
Assign this distance to the value of another distance.
Definition: Distance.cpp:251