Isis 3 Programmer Reference
Displacement.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "Displacement.h"
8 
9 #include "Distance.h"
10 #include "IException.h"
11 #include "IString.h"
12 #include "SpecialPixel.h"
13 
14 namespace Isis {
15 
22  }
23 
24 
30  Displacement::Displacement(const Distance &distanceToCopy) {
31  // Use meters because it is the stored format, no precision loss
32  setDisplacement(distanceToCopy.meters(), Meters);
33  }
34 
35 
46  Displacement::Displacement(double displacement, Units displacementUnit) {
47  if(displacementUnit == Pixels)
48  displacementUnit = Meters;
49 
50  setDisplacement(displacement, displacementUnit);
51  }
52 
53 
62  Displacement::Displacement(double displacementInPixels,
63  double pixelsPerMeter) {
64  setDisplacement(displacementInPixels / pixelsPerMeter, Meters);
65  }
66 
67 
73  double Displacement::meters() const {
74  return displacement(Meters);
75  }
76 
77 
84  void Displacement::setMeters(double displacementInMeters) {
85  setDisplacement(displacementInMeters, Meters);
86  }
87 
88 
94  double Displacement::kilometers() const {
95  return displacement(Kilometers);
96  }
97 
98 
105  void Displacement::setKilometers(double displacementInKilometers) {
106  setDisplacement(displacementInKilometers, Kilometers);
107  }
108 
109 
117  double Displacement::pixels(double pixelsPerMeter) const {
118  return displacement(Meters) * pixelsPerMeter;
119  }
120 
121 
130  void Displacement::setPixels(double displacementInPixels,
131  double pixelsPerMeter) {
132  setDisplacement(displacementInPixels / pixelsPerMeter, Meters);
133  }
134 
135 
141  bool Displacement::isValid() const {
142  return displacement(Meters) != Null;
143  }
144 
145 
154  bool Displacement::operator >(const Displacement &otherDisplacement) const {
155  if(!isValid() || !otherDisplacement.isValid()) {
156  IString msg = "Displacement has not been initialized, you must initialize "
157  "it first before comparing with another displacement using [>]";
158  throw IException(IException::Programmer, msg, _FILEINFO_);
159  }
160 
161  return meters() > otherDisplacement.meters();
162  }
163 
164 
173  bool Displacement::operator <(const Displacement &otherDisplacement) const {
174  if(!isValid() || !otherDisplacement.isValid()) {
175  IString msg = "Displacement has not been initialized, you must initialize "
176  "it first before comparing with another displacement using [<]";
177  throw IException(IException::Programmer, msg, _FILEINFO_);
178  }
179 
180  return meters() < otherDisplacement.meters();
181  }
182 
183 
190  Displacement Displacement::operator
191  +(const Displacement &displacementToAdd) const {
192  if(!isValid() || !displacementToAdd.isValid()) return Displacement();
193 
194  return Displacement(meters() + displacementToAdd.meters(), Meters);
195  }
196 
197 
205  Displacement Displacement::operator
206  -(const Displacement &displacementToSub) const {
207  if(!isValid() || !displacementToSub.isValid()) return Displacement();
208 
209  Displacement result(meters() - displacementToSub.meters(), Meters);
210  return result;
211  }
212 
213 
221  Displacement Displacement::operator
222  -(const Distance &distanceToSub) const {
223  if(!isValid() || !distanceToSub.isValid()) return Displacement();
224 
225  Displacement result(meters() - distanceToSub.meters(), Meters);
226  return result;
227  }
228 
229 
236  double Displacement::operator /(const Displacement &displacementToDiv) const {
237  if(!isValid() || !displacementToDiv.isValid()) return Null;
238 
239  double result = meters() / displacementToDiv.meters();
240  return result;
241  }
242 
243 
250  Displacement Displacement::operator /(const double &valueToDiv) const {
251  if(!isValid() || IsSpecial(valueToDiv)) return Displacement();
252 
253  Displacement result = Displacement(meters() / valueToDiv, Meters);
254  return result;
255  }
256 
257 
264  Displacement Displacement::operator *(const double &valueToMult) const {
265  if(!isValid() || IsSpecial(valueToMult)) return Displacement();
266 
267  Displacement result = Displacement(meters() * valueToMult, Meters);
268  return result;
269  }
270 
271 
279  Displacement operator *(double mult, Displacement displacement) {
280  Displacement result = displacement * mult;
281  return result;
282  }
283 
284 
290  void Displacement::operator +=(const Displacement &displacementToAdd) {
291  if(!isValid() || !displacementToAdd.isValid())
293  else
294  setDisplacement(meters() + displacementToAdd.meters(), Meters);
295  }
296 
297 
303  void Displacement::operator -=(const Displacement &displacementToSub) {
304  if(!isValid() || !displacementToSub.isValid())
306  else
307  setDisplacement(meters() - displacementToSub.meters(), Meters);
308  }
309 
310 
316  void Displacement::operator -=(const Distance &distanceToSub) {
317  if(!isValid() || !distanceToSub.isValid())
319  else
320  setDisplacement(meters() - distanceToSub.meters(), Meters);
321  }
322 
323 
329  void Displacement::operator /=(const double &valueToDiv) {
330  if(!isValid() || IsSpecial(valueToDiv))
332  else
333  setDisplacement(meters() / valueToDiv, Meters);
334  }
335 
336 
342  void Displacement::operator *=(const double &valueToMult) {
343  if(!isValid() || IsSpecial(valueToMult))
345  else
346  setDisplacement(meters() * valueToMult, Meters);
347  }
348 
349 
358  double Displacement::displacement(Units displacementUnit) const {
359  double displacementInMeters = m_displacementInMeters;
360  double resultingDisplacement = Null;
361 
362  if(m_displacementInMeters == Null) return Null;
363 
364  switch(displacementUnit) {
365  case Meters:
366  resultingDisplacement = displacementInMeters;
367  break;
368 
369  case Kilometers:
370  resultingDisplacement = displacementInMeters / 1000.0;
371  break;
372 
373  case Pixels:
374  IString msg = "Cannot call displacement with pixels, ask for another "
375  "unit";
376  throw IException(IException::Programmer, msg, _FILEINFO_);
377  break;
378  }
379 
380  if(resultingDisplacement == Null) {
381  IString msg = "Displacement does not understand the enumerated value [" +
382  IString(displacementUnit) + "] as a unit";
383  throw IException(IException::Programmer, msg, _FILEINFO_);
384  }
385 
386  return resultingDisplacement;
387  }
388 
389 
400  void Displacement::setDisplacement(const double &displacement, Units displacementUnit) {
401  double displacementInMeters = Null;
402 
403  if(IsSpecial(displacement)) {
405  return;
406  }
407 
408  switch(displacementUnit) {
409  case Meters:
410  displacementInMeters = displacement;
411  break;
412 
413  case Kilometers:
414  displacementInMeters = displacement * 1000.0;
415  break;
416 
417  case Pixels:
418  IString msg = "Cannot setDisplacement with pixels, must convert to "
419  "another unit first";
420  throw IException(IException::Programmer, msg, _FILEINFO_);
421  break;
422  }
423 
424  if(displacementInMeters == Null) {
425  IString msg = "Displacement does not understand the enumerated value [" +
426  IString(displacementUnit) + "] as a unit";
427  throw IException(IException::Programmer, msg, _FILEINFO_);
428  }
429 
430  m_displacementInMeters = displacementInMeters;
431  }
432 }
Isis::Displacement::m_displacementInMeters
double m_displacementInMeters
This is the displacement value that this class is encapsulating, always stored in meters.
Definition: Displacement.h:157
Isis::Displacement::Meters
@ Meters
The distance is being specified in meters.
Definition: Displacement.h:40
Isis::Displacement::operator/=
void operator/=(const double &valueToDiv)
Divide this displacement by a value and assign the result to ourself.
Definition: Displacement.cpp:329
Isis::Displacement::operator*
Displacement operator*(const double &valueToMult) const
Multiply this displacement by a value (5m * 2 = 10m).
Definition: Displacement.cpp:264
Isis::operator*
Angle operator*(double mult, Angle angle)
Multiply this angle by a double and return the resulting angle.
Definition: Angle.cpp:170
Isis::Displacement::Units
Units
This is a list of available units to access and store Distances in.
Definition: Displacement.h:38
Isis::Displacement::operator<
bool operator<(const Displacement &otherDisplacement) const
Compare two displacements with the less than operator.
Definition: Displacement.cpp:173
Isis::Displacement::setDisplacement
void setDisplacement(const double &displacement, Units displacementUnit)
This is a helper method to set displacements in a universal manner with uniform error checking.
Definition: Displacement.cpp:400
Isis::Displacement::setKilometers
void setKilometers(double displacementInKilometers)
Set the displacement in kilometers.
Definition: Displacement.cpp:105
Isis::Displacement::Pixels
@ Pixels
The distance is being specified in pixels.
Definition: Displacement.h:44
Isis::Displacement::operator-=
void operator-=(const Displacement &displacementToSub)
Subtract the given displacement from ourself and assign.
Definition: Displacement.cpp:303
Isis::IsSpecial
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:197
Isis::Distance
Distance measurement, usually in meters.
Definition: Distance.h:34
Isis::Displacement
Displacement is a signed length, usually in meters.
Definition: Displacement.h:31
Isis::Displacement::operator/
double operator/(const Displacement &displacementToDiv) const
Divide another displacement into this displacement (5m / 1m = 5).
Definition: Displacement.cpp:236
Isis::Displacement::meters
double meters() const
Get the displacement in meters.
Definition: Displacement.cpp:73
Isis::Displacement::setMeters
void setMeters(double displacementInMeters)
Set the displacement in meters.
Definition: Displacement.cpp:84
Isis::Displacement::operator+=
void operator+=(const Displacement &displacementToAdd)
Add and assign the given displacement to ourselves.
Definition: Displacement.cpp:290
Isis::Displacement::Displacement
Displacement()
This initializes the displacement to an invalid state.
Definition: Displacement.cpp:20
Isis::Distance::isValid
bool isValid() const
Test if this distance has been initialized or not.
Definition: Distance.cpp:192
Isis::Displacement::pixels
double pixels(double pixelsPerMeter=1.0) const
Get the displacement in pixels using the given conversion ratio.
Definition: Displacement.cpp:117
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Null
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:95
Isis::Displacement::setPixels
void setPixels(double distanceInPixels, double pixelsPerMeter=1.0)
Set the displacement in pixels.
Definition: Displacement.cpp:130
Isis::Displacement::kilometers
double kilometers() const
Get the displacement in kilometers.
Definition: Displacement.cpp:94
Isis::Displacement::displacement
double displacement(Units displacementUnit) const
This is a helper method to access displacements in a universal manner with uniform error checking.
Definition: Displacement.cpp:358
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::Displacement::isValid
bool isValid() const
Test if this displacement has been initialized or not.
Definition: Displacement.cpp:141
Isis::Distance::meters
double meters() const
Get the distance in meters.
Definition: Distance.cpp:85
Isis::Displacement::operator*=
void operator*=(const double &valueToMult)
Multiply this displacement by a value and assign the result to ourself.
Definition: Displacement.cpp:342
Isis::Displacement::operator>
bool operator>(const Displacement &otherDisplacement) const
Get the displacement in meters.
Definition: Displacement.cpp:154
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::Displacement::Kilometers
@ Kilometers
The distance is being specified in kilometers.
Definition: Displacement.h:42
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16