Isis 3 Programmer Reference
Area3D.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "Area3D.h"
9 
10 #include <algorithm>
11 
12 #include <QObject>
13 #include <QString>
14 
15 #include "Displacement.h"
16 #include "Distance.h"
17 #include "IException.h"
18 #include "IString.h"
19 
20 namespace Isis {
25  nullTheData();
26  }
27 
28 
39  Area3D::Area3D(const Displacement &startX, const Displacement &startY,
40  const Displacement &startZ,
41  const Distance &width, const Distance &height,
42  const Distance &depth) {
43  nullTheData();
44  setArea(startX, startY, startZ,
45  startX + width, startY + height, startZ + depth);
46  }
47 
48 
59  Area3D::Area3D(const Displacement &startX, const Displacement &startY,
60  const Displacement &startZ,
61  const Displacement &endX, const Displacement &endY,
62  const Displacement &endZ) {
63  nullTheData();
64  setArea(startX, startY, startZ, endX, endY, endZ);
65  }
66 
67 
73  Area3D::Area3D(const Area3D &areaToCopy) {
74  nullTheData();
75 
76  if(areaToCopy.m_startX) {
77  m_startX = new Displacement(*areaToCopy.m_startX);
78  }
79 
80  if(areaToCopy.m_startY) {
81  m_startY = new Displacement(*areaToCopy.m_startY);
82  }
83 
84  if(areaToCopy.m_startZ) {
85  m_startZ = new Displacement(*areaToCopy.m_startZ);
86  }
87 
88  if(areaToCopy.m_endX) {
89  m_endX = new Displacement(*areaToCopy.m_endX);
90  }
91 
92  if(areaToCopy.m_endY) {
93  m_endY = new Displacement(*areaToCopy.m_endY);
94  }
95 
96  if(areaToCopy.m_endZ) {
97  m_endZ = new Displacement(*areaToCopy.m_endZ);
98  }
99  }
100 
101 
106  deleteTheData();
107  }
108 
109 
116  Displacement startX;
117 
118  if(m_startX)
119  startX = *m_startX;
120 
121  return startX;
122  }
123 
124 
131  Displacement startY;
132 
133  if(m_startY)
134  startY = *m_startY;
135 
136  return startY;
137  }
138 
139 
146  Displacement startZ;
147 
148  if(m_startZ)
149  startZ = *m_startZ;
150 
151  return startZ;
152  }
153 
154 
161  return Distance(
162  (getEndX() - getStartX()).meters(),
164  }
165 
166 
173  return Distance(
174  (getEndY() - getStartY()).meters(),
176  }
177 
178 
185  return Distance(
186  (getEndZ() - getStartZ()).meters(),
188  }
189 
190 
197  Displacement endX;
198 
199  if(m_endX)
200  endX = *m_endX;
201 
202  return endX;
203  }
204 
205 
212  Displacement endY;
213 
214  if(m_endY)
215  endY = *m_endY;
216 
217  return endY;
218  }
219 
220 
227  Displacement endZ;
228 
229  if(m_endZ)
230  endZ = *m_endZ;
231 
232  return endZ;
233  }
234 
235 
242  void Area3D::setStartX(const Displacement &startX) {
243  setArea(startX, getStartY(), getStartZ(),
244  getEndX(), getEndY(), getEndZ());
245  }
246 
247 
254  void Area3D::setStartY(const Displacement &startY) {
255  setArea(getStartX(), startY, getStartZ(),
256  getEndX(), getEndY(), getEndZ());
257  }
258 
259 
266  void Area3D::setStartZ(const Displacement &startZ) {
267  setArea(getStartX(), getStartY(), startZ,
268  getEndX(), getEndY(), getEndZ());
269  }
270 
271 
278  void Area3D::moveStartX(const Displacement &startX) {
279  setArea(startX, getStartY(), getStartZ(),
280  getEndX() + (startX - getStartX()), getEndY(), getEndZ());
281  }
282 
283 
290  void Area3D::moveStartY(const Displacement &startY) {
291  setArea(getStartX(), startY, getStartZ(),
292  getEndX(), getEndY() + (startY - getStartY()), getEndZ());
293  }
294 
295 
302  void Area3D::moveStartZ(const Displacement &startZ) {
303  setArea(getStartX(), getStartY(), startZ,
304  getEndX(), getEndY(), getEndZ() + (startZ - getStartZ()));
305  }
306 
307 
313  void Area3D::setWidth(const Distance &width) {
315  getStartX() + width, getEndY(), getEndZ());
316  }
317 
318 
324  void Area3D::setHeight(const Distance &height) {
326  getEndX(), getStartY() + height, getEndZ());
327  }
328 
329 
335  void Area3D::setDepth(const Distance &depth) {
337  getEndX(), getEndY(), getStartZ() + depth);
338  }
339 
340 
347  void Area3D::setEndX(const Displacement &endX) {
349  endX, getEndY(), getEndZ());
350  }
351 
352 
359  void Area3D::setEndY(const Displacement &endY) {
361  getEndX(), endY, getEndZ());
362  }
363 
364 
371  void Area3D::setEndZ(const Displacement &endZ) {
373  getEndX(), getEndY(), endZ);
374  }
375 
376 
383  void Area3D::moveEndX(const Displacement &endX) {
384  setArea(getStartX() + (endX - getEndX()), getStartY(), getStartZ(),
385  endX, getEndY(), getEndZ());
386  }
387 
388 
395  void Area3D::moveEndY(const Displacement &endY) {
396  setArea(getStartX(), getStartY() + (endY - getEndY()), getStartZ(),
397  getEndX(), endY, getEndZ());
398  }
399 
400 
407  void Area3D::moveEndZ(const Displacement &endZ) {
408  setArea(getStartX(), getStartY(), getStartZ() + (endZ - getEndZ()),
409  getEndX(), getEndY(), endZ);
410  }
411 
412 
421  const Distance &width) {
422  setArea(startX, getStartY(), getStartZ(),
423  startX + width, getEndY(), getEndZ());
424  }
425 
426 
435  const Distance &height) {
436  setArea(getStartX(), startY, getStartZ(),
437  getEndX(), startY + height, getEndZ());
438  }
439 
440 
449  const Distance &depth) {
450  setArea(getStartX(), getStartY(), startZ,
451  getEndX(), getEndY(), startZ + depth);
452  }
453 
454 
462  Area3D Area3D::intersect(const Area3D &otherArea) const {
463  Area3D result;
464 
465  // Check validity because of comparison operators.
466  if (isValid() && otherArea.isValid()) {
467  Displacement startX(std::max(getStartX(), otherArea.getStartX()));
468  Displacement startY(std::max(getStartY(), otherArea.getStartY()));
469  Displacement startZ(std::max(getStartZ(), otherArea.getStartZ()));
470  Displacement endX(std::min(getEndX(), otherArea.getEndX()));
471  Displacement endY(std::min(getEndY(), otherArea.getEndY()));
472  Displacement endZ(std::min(getEndZ(), otherArea.getEndZ()));
473 
474 
475  if (startX <= endX && startY <= endY && startZ <= endZ)
476  result = Area3D(startX, startY, startZ, endX, endY, endZ);
477  }
478 
479  return result;
480  }
481 
482 
489  bool Area3D::isValid() const {
490  bool valid = true;
491 
492  if(!m_startX)
493  valid = false;
494  else if(!m_startY)
495  valid = false;
496  else if(!m_startZ)
497  valid = false;
498  else if(!m_endX)
499  valid = false;
500  else if(!m_endY)
501  valid = false;
502  else if(!m_endZ)
503  valid = false;
504 
505  return valid;
506  }
507 
508 
517  bool Area3D::operator ==(const Area3D &otherArea) const {
518  return getStartX() == otherArea.getStartX() &&
519  getStartY() == otherArea.getStartY() &&
520  getStartZ() == otherArea.getStartZ() &&
521  getEndX() == otherArea.getEndX() &&
522  getEndY() == otherArea.getEndY() &&
523  getEndZ() == otherArea.getEndZ();
524  }
525 
526 
535  bool Area3D::operator !=(const Area3D &otherArea) const {
536  return !(*this == otherArea);
537  }
538 
539 
546  Area3D &Area3D::operator =(const Area3D &areaToCopy) {
547  deleteTheData();
548 
549  if(areaToCopy.m_startX) {
550  m_startX = new Displacement(*areaToCopy.m_startX);
551  }
552 
553  if(areaToCopy.m_startY) {
554  m_startY = new Displacement(*areaToCopy.m_startY);
555  }
556 
557  if(areaToCopy.m_startZ) {
558  m_startZ = new Displacement(*areaToCopy.m_startZ);
559  }
560 
561  if(areaToCopy.m_endX) {
562  m_endX = new Displacement(*areaToCopy.m_endX);
563  }
564 
565  if(areaToCopy.m_endY) {
566  m_endY = new Displacement(*areaToCopy.m_endY);
567  }
568 
569  if(areaToCopy.m_endZ) {
570  m_endZ = new Displacement(*areaToCopy.m_endZ);
571  }
572 
573  return *this;
574  }
575 
576 
593  void Area3D::setArea(const Displacement &startX,
594  const Displacement &startY, const Displacement &startZ,
595  const Displacement &endX, const Displacement &endY,
596  const Displacement &endZ) {
597  bool startXValid = startX.isValid();
598  bool endXValid = endX.isValid();
599  bool startYValid = startY.isValid();
600  bool endYValid = endY.isValid();
601  bool startZValid = startZ.isValid();
602  bool endZValid = endZ.isValid();
603 
604  deleteTheData();
605 
606  // Optimized for success.
607  if(startXValid && startYValid && startZValid && endXValid && endYValid &&
608  endZValid && startX <= endX && startY <= endY && startZ <= endZ) {
609  m_startX = new Displacement(startX);
610  m_startY = new Displacement(startY);
611  m_startZ = new Displacement(startZ);
612  m_endX = new Displacement(endX);
613  m_endY = new Displacement(endY);
614  m_endZ = new Displacement(endZ);
615  }
616  else {
617  if(startXValid && endXValid && startX > endX) {
618  QString msg = QObject::tr("Cannot have a 3D area with inverted X coordinates of "
619  "[%1 meters] to [%2 meters]")
620  .arg(startX.meters()).arg(endX.meters());
621  throw IException(IException::Programmer, msg, _FILEINFO_);
622  }
623 
624  if(startYValid && endYValid && startY > endY) {
625  QString msg = QObject::tr("Cannot have a 3D area with inverted Y coordinates of "
626  "[%1 meters] to [%2 meters]")
627  .arg(startY.meters()).arg(endY.meters());
628  throw IException(IException::Programmer, msg, _FILEINFO_);
629  }
630 
631  if(startZValid && endZValid && startZ > endZ) {
632  QString msg = QObject::tr("Cannot have a 3D area with inverted Z coordinates of "
633  "[%1 meters] to [%2 meters]")
634  .arg(startZ.meters()).arg(endZ.meters());
635  throw IException(IException::Programmer, msg, _FILEINFO_);
636  }
637 
638  if(startXValid)
639  m_startX = new Displacement(startX);
640 
641  if(startYValid)
642  m_startY = new Displacement(startY);
643 
644  if(startZValid)
645  m_startZ = new Displacement(startZ);
646 
647  if(endXValid)
648  m_endX = new Displacement(endX);
649 
650  if(endYValid)
651  m_endY = new Displacement(endY);
652 
653  if(endZValid)
654  m_endZ = new Displacement(endZ);
655  }
656  }
657 
658 
663  if(m_startX) {
664  delete m_startX;
665  m_startX = NULL;
666  }
667 
668  if(m_startY) {
669  delete m_startY;
670  m_startY = NULL;
671  }
672 
673  if(m_startZ) {
674  delete m_startZ;
675  m_startZ = NULL;
676  }
677 
678  if(m_endX) {
679  delete m_endX;
680  m_endX = NULL;
681  }
682 
683  if(m_endY) {
684  delete m_endY;
685  m_endY = NULL;
686  }
687 
688  if(m_endZ) {
689  delete m_endZ;
690  m_endZ = NULL;
691  }
692  }
693 
694 
699  m_startX = NULL;
700  m_startY = NULL;
701  m_startZ = NULL;
702  m_endX = NULL;
703  m_endY = NULL;
704  m_endZ = NULL;
705  }
706 }
707 
Isis::Area3D::setWidth
void setWidth(const Distance &width)
Changes the width of the 3D area.
Definition: Area3D.cpp:313
Isis::Area3D::m_startX
Displacement * m_startX
The leftmost X position. Either NULL or a valid displacement.
Definition: Area3D.h:99
Isis::Area3D::m_endY
Displacement * m_endY
The bottommost Y position. Either NULL or a valid displacement.
Definition: Area3D.h:107
Isis::Area3D::setDepth
void setDepth(const Distance &depth)
Changes the depth of the 3D area.
Definition: Area3D.cpp:335
Isis::Area3D::isValid
bool isValid() const
Returns true if all of the positions of the 3D area are valid (i.e.
Definition: Area3D.cpp:489
Isis::Area3D::operator!=
bool operator!=(const Area3D &otherArea) const
Compares two areas with the != operator.
Definition: Area3D.cpp:535
Isis::Area3D::getDepth
Distance getDepth() const
Returns the depth (in the Z dimension) of the 3D area.
Definition: Area3D.cpp:184
Isis::Area3D::moveStartY
void moveStartY(const Displacement &startY)
Moves the topmost Y position of the 3D area.
Definition: Area3D.cpp:290
Isis::Area3D::setStartZ
void setStartZ(const Displacement &startZ)
Sets the frontmost Z position.
Definition: Area3D.cpp:266
Isis::Area3D::moveStartX
void moveStartX(const Displacement &startX)
Moves the leftmost X position of the 3D area.
Definition: Area3D.cpp:278
Isis::Area3D::setYDimension
void setYDimension(const Displacement &startY, const Distance &height)
Sets the Y dimension of the 3D area.
Definition: Area3D.cpp:434
Isis::Area3D::setStartY
void setStartY(const Displacement &startY)
Sets the topmost Y position.
Definition: Area3D.cpp:254
Isis::Area3D::~Area3D
virtual ~Area3D()
The destructor frees allocated memory.
Definition: Area3D.cpp:105
Isis::Area3D::setZDimension
void setZDimension(const Displacement &startZ, const Distance &depth)
Sets the Z dimension of the 3D area.
Definition: Area3D.cpp:448
Isis::Distance
Distance measurement, usually in meters.
Definition: Distance.h:34
Isis::Area3D::setHeight
void setHeight(const Distance &height)
Changes the height of the 3D area.
Definition: Area3D.cpp:324
Isis::Area3D::getStartX
Displacement getStartX() const
Returns the leftmost X position of the 3D area.
Definition: Area3D.cpp:115
Isis::Displacement
Displacement is a signed length, usually in meters.
Definition: Displacement.h:31
Isis::Distance::Meters
@ Meters
The distance is being specified in meters.
Definition: Distance.h:43
Isis::Displacement::meters
double meters() const
Get the displacement in meters.
Definition: Displacement.cpp:73
Isis::Area3D::getStartZ
Displacement getStartZ() const
Returns the frontmost Z position of the 3D area.
Definition: Area3D.cpp:145
Isis::Area3D::getWidth
Distance getWidth() const
Returns the width (in the X dimension) of the 3D area.
Definition: Area3D.cpp:160
Isis::Area3D::getEndY
Displacement getEndY() const
Returns the bottommost Y position of the 3D area.
Definition: Area3D.cpp:211
Isis::Area3D::getEndZ
Displacement getEndZ() const
Returns the backmost Z position of the 3D area.
Definition: Area3D.cpp:226
Isis::Area3D::setArea
virtual void setArea(const Displacement &startX, const Displacement &startY, const Displacement &startZ, const Displacement &endX, const Displacement &endY, const Displacement &endZ)
Sets the area.
Definition: Area3D.cpp:593
Isis::Area3D::deleteTheData
void deleteTheData()
Frees all allocated memory used by this 3D area.
Definition: Area3D.cpp:662
Isis::Area3D::moveEndY
void moveEndY(const Displacement &endY)
Moves the bottommost Y position of the 3D area.
Definition: Area3D.cpp:395
Isis::Area3D::moveEndZ
void moveEndZ(const Displacement &endZ)
Moves the backmost Z position of the 3D area.
Definition: Area3D.cpp:407
Isis::Area3D::m_endZ
Displacement * m_endZ
The backmost Z position. Either NULL or a valid displacement.
Definition: Area3D.h:109
Isis::Area3D::m_startZ
Displacement * m_startZ
The frontmost Z position. Either NULL or a valid displacement.
Definition: Area3D.h:103
Isis::Area3D::moveStartZ
void moveStartZ(const Displacement &startZ)
Moves the frontmost Z position of the 3D area.
Definition: Area3D.cpp:302
Isis::Area3D::m_endX
Displacement * m_endX
The rightmost X position. Either NULL or a valid displacement.
Definition: Area3D.h:105
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Area3D::getHeight
Distance getHeight() const
Returns the height (in the Y dimension) of the 3D area.
Definition: Area3D.cpp:172
Isis::Area3D::setEndY
void setEndY(const Displacement &endY)
Sets the bottommost Y position.
Definition: Area3D.cpp:359
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::Area3D
Represents a 3D area (a 3D "cube")
Definition: Area3D.h:29
Isis::Area3D::setEndZ
void setEndZ(const Displacement &endZ)
Sets the backmost Z position.
Definition: Area3D.cpp:371
Isis::Displacement::isValid
bool isValid() const
Test if this displacement has been initialized or not.
Definition: Displacement.cpp:141
Isis::Area3D::operator==
bool operator==(const Area3D &otherArea) const
Compares two areas with the == operator.
Definition: Area3D.cpp:517
Isis::Area3D::getEndX
Displacement getEndX() const
Returns the rightmost X position of the 3D area.
Definition: Area3D.cpp:196
Isis::Area3D::setStartX
void setStartX(const Displacement &startX)
Sets the leftmost X position.
Definition: Area3D.cpp:242
Isis::Area3D::nullTheData
void nullTheData()
Nulls all of the members used by this 3D area.
Definition: Area3D.cpp:698
Isis::Area3D::setEndX
void setEndX(const Displacement &endX)
Sets the rightmost X position.
Definition: Area3D.cpp:347
Isis::Area3D::intersect
Area3D intersect(const Area3D &otherArea) const
Returns the intersection of this 3D area with another 3D area.
Definition: Area3D.cpp:462
Isis::Area3D::moveEndX
void moveEndX(const Displacement &endX)
Moves the rightmost X position of the 3D area.
Definition: Area3D.cpp:383
Isis::Area3D::m_startY
Displacement * m_startY
The topmost Y position. Either NULL or a valid displacement.
Definition: Area3D.h:101
Isis::Area3D::operator=
Area3D & operator=(const Area3D &areaToCopy)
Assigns areaToCopy to this.
Definition: Area3D.cpp:546
Isis::Area3D::Area3D
Area3D()
The empty constructor creates an invalid 3D area.
Definition: Area3D.cpp:24
Isis::Area3D::getStartY
Displacement getStartY() const
Returns the topmost Y position of the 3D area.
Definition: Area3D.cpp:130
Isis::Area3D::setXDimension
void setXDimension(const Displacement &startX, const Distance &width)
Sets the X dimension of the 3D area.
Definition: Area3D.cpp:420
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16