LSSTApplications  20.0.0
LSSTDataManagementBasePackage
Box3d.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * See COPYRIGHT file at the top of the source tree.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_SPHGEOM_BOX3D_H_
24 #define LSST_SPHGEOM_BOX3D_H_
25 
29 
30 #include <iosfwd>
31 
32 #include "Interval1d.h"
33 #include "Relationship.h"
34 #include "Vector3d.h"
35 
36 
37 namespace lsst {
38 namespace sphgeom {
39 
42 class Box3d {
43 public:
44  // Factory functions
45  static Box3d empty() {
46  return Box3d();
47  }
48 
49  static Box3d full() {
50  return Box3d(Interval1d::full(),
53  }
54 
57  return Box3d(Interval1d(-1.0, 1.0),
58  Interval1d(-1.0, 1.0),
59  Interval1d(-1.0, 1.0));
60  }
61 
63  Box3d() {}
64 
66  explicit Box3d(Vector3d const & v)
67  {
68  _intervals[0] = Interval1d(v.x());
69  _intervals[1] = Interval1d(v.y());
70  _intervals[2] = Interval1d(v.z());
71  _enforceInvariants();
72  }
73 
76  Box3d(Vector3d const & v1, Vector3d const & v2)
77  {
78  _intervals[0] = Interval1d(v1.x(), v2.x());
79  _intervals[1] = Interval1d(v1.y(), v2.y());
80  _intervals[2] = Interval1d(v1.z(), v2.z());
81  _enforceInvariants();
82  }
83 
86  Box3d(Vector3d const & v, double w, double h, double d)
87  {
88  _intervals[0] = Interval1d(v.x()).dilatedBy(w);
89  _intervals[1] = Interval1d(v.y()).dilatedBy(h);
90  _intervals[2] = Interval1d(v.z()).dilatedBy(d);
91  _enforceInvariants();
92  }
93 
96  Box3d(Interval1d const & x,
97  Interval1d const & y,
98  Interval1d const & z)
99  {
100  _intervals[0] = Interval1d(x);
101  _intervals[1] = Interval1d(y);
102  _intervals[2] = Interval1d(z);
103  _enforceInvariants();
104  }
105 
107  bool operator==(Box3d const & b) const {
108  return _intervals[0] == b._intervals[0] &&
109  _intervals[1] == b._intervals[1] &&
110  _intervals[2] == b._intervals[2];
111  }
112 
113  bool operator!=(Box3d const & b) const { return !(*this == b); }
114 
116  bool operator==(Vector3d const & v) const { return *this == Box3d(v); }
117 
118  bool operator!=(Vector3d const & v) const { return !(*this == v); }
119 
121  Interval1d operator()(int i) const { return _intervals[i]; }
122 
123  Interval1d const & x() const { return _intervals[0]; }
124  Interval1d const & y() const { return _intervals[1]; }
125  Interval1d const & z() const { return _intervals[2]; }
126 
128  bool isEmpty() const {
129  return x().isEmpty();
130  }
131 
133  bool isFull() const {
134  return x().isFull() && y().isFull() && z().isFull();
135  }
136 
139  Vector3d getCenter() const {
140  return Vector3d(x().getCenter(), y().getCenter(), z().getCenter());
141  }
142 
145  double getWidth() const { return x().getSize(); }
146 
149  double getHeight() const { return y().getSize(); }
150 
153  double getDepth() const { return z().getSize(); }
154 
158  bool contains(Vector3d const & b) const {
159  return x().contains(b.x()) &&
160  y().contains(b.y()) &&
161  z().contains(b.z());
162  }
163 
164  bool contains(Box3d const & b) const {
165  return x().contains(b.x()) &&
166  y().contains(b.y()) &&
167  z().contains(b.z());
168  }
170 
174  bool isDisjointFrom(Vector3d const & b) const { return !intersects(b); }
175 
176  bool isDisjointFrom(Box3d const & b) const { return !intersects(b); }
178 
182  bool intersects(Vector3d const & b) const {
183  return x().intersects(b.x()) &&
184  y().intersects(b.y()) &&
185  z().intersects(b.z());
186  }
187 
188  bool intersects(Box3d const & b) const {
189  return x().intersects(b.x()) &&
190  y().intersects(b.y()) &&
191  z().intersects(b.z());
192  }
194 
198  bool isWithin(Vector3d const & b) const {
199  return x().isWithin(b.x()) &&
200  y().isWithin(b.y()) &&
201  z().isWithin(b.z());
202  }
203 
204  bool isWithin(Box3d const & b) const {
205  return x().isWithin(b.x()) &&
206  y().isWithin(b.y()) &&
207  z().isWithin(b.z());
208  }
210 
213  Box3d & clipTo(Vector3d const & b) {
214  _intervals[0].clipTo(b.x());
215  _intervals[1].clipTo(b.y());
216  _intervals[2].clipTo(b.z());
217  _enforceInvariants();
218  return *this;
219  }
220 
221  Box3d & clipTo(Box3d const & b) {
222  _intervals[0].clipTo(b.x());
223  _intervals[1].clipTo(b.y());
224  _intervals[2].clipTo(b.z());
225  _enforceInvariants();
226  return *this;
227  }
229 
232  Box3d clippedTo(Vector3d const & b) const {
233  return Box3d(*this).clipTo(b);
234  }
235 
236  Box3d clippedTo(Box3d const & b) const {
237  return Box3d(*this).clipTo(b);
238  }
240 
243  Box3d & expandTo(Vector3d const & b) {
244  _intervals[0].expandTo(b.x());
245  _intervals[1].expandTo(b.y());
246  _intervals[2].expandTo(b.z());
247  return *this;
248  }
249 
250  Box3d & expandTo(Box3d const & b) {
251  _intervals[0].expandTo(b.x());
252  _intervals[1].expandTo(b.y());
253  _intervals[2].expandTo(b.z());
254  return *this;
255  }
257 
261  Box3d expandedTo(Vector3d const & b) const {
262  return Box3d(*this).expandTo(b);
263  }
264 
265  Box3d expandedTo(Box3d const & b) const {
266  return Box3d(*this).expandTo(b);
267  }
269 
276  Box3d & dilateBy(double r) { return dilateBy(r, r, r); }
277  Box3d dilatedBy(double r) const { return Box3d(*this).dilateBy(r); }
278 
286  Box3d & dilateBy(double w, double h, double d) {
287  _intervals[0].dilateBy(w);
288  _intervals[1].dilateBy(h);
289  _intervals[2].dilateBy(d);
290  _enforceInvariants();
291  return *this;
292  }
293  Box3d dilatedBy(double w, double h, double d) const {
294  return Box3d(*this).dilateBy(w, h, d);
295  }
296  Box3d & erodeBy(double r) { return dilateBy(-r); }
297  Box3d erodedBy(double r) const { return dilatedBy(-r); }
298  Box3d & erodeBy(double w, double h, double d) {
299  return dilateBy(-w, -h, -d);
300  }
301  Box3d erodedBy(double w, double h, double d) const {
302  return dilatedBy(-w, -h, -d);
303  }
304 
305  Relationship relate(Vector3d const & v) const { return relate(Box3d(v)); }
306 
307  Relationship relate(Box3d const & b) const {
308  Relationship xr = x().relate(b.x());
309  Relationship yr = y().relate(b.y());
310  Relationship zr = z().relate(b.z());
311  // If the box x, y, or z intervals are disjoint, then so are the
312  // boxes. The other relationships must hold for all constituent
313  // intervals in order to hold for the boxes.
314  return ((xr & yr & zr) & (CONTAINS | WITHIN)) |
315  ((xr | yr | zr) & DISJOINT);
316  }
317 
318 
319 private:
320  void _enforceInvariants() {
321  // Make sure that all intervals are empty, or none are. This
322  // simplifies the implementation of relate and dilateBy.
323  if (x().isEmpty() || y().isEmpty() || z().isEmpty()) {
324  _intervals[0] = Interval1d();
325  _intervals[1] = Interval1d();
326  _intervals[2] = Interval1d();
327  }
328  }
329 
330  Interval1d _intervals[3];
331 };
332 
333 std::ostream & operator<<(std::ostream &, Box3d const &);
334 
335 }} // namespace lsst::sphgeom
336 
337 #endif // LSST_SPHGEOM_BOX3D_H_
lsst::sphgeom::Box3d::contains
bool contains(Vector3d const &b) const
Definition: Box3d.h:158
lsst::sphgeom::Box3d::operator()
Interval1d operator()(int i) const
The function call operator returns the i-th interval of this box.
Definition: Box3d.h:121
lsst::sphgeom::Box3d::clipTo
Box3d & clipTo(Vector3d const &b)
Definition: Box3d.h:213
lsst::sphgeom::Box3d::erodeBy
Box3d & erodeBy(double r)
Definition: Box3d.h:296
lsst::sphgeom::Box3d::full
static Box3d full()
Definition: Box3d.h:49
std::bitset
STL class.
lsst::sphgeom::Box3d::dilateBy
Box3d & dilateBy(double w, double h, double d)
dilateBy morphologically dilates or erodes the x, y, and z intervals of this box by w,...
Definition: Box3d.h:286
lsst::sphgeom::Box3d::intersects
bool intersects(Vector3d const &b) const
Definition: Box3d.h:182
lsst::sphgeom::Box3d::getWidth
double getWidth() const
getWidth returns the width (x-axis extent) of this box.
Definition: Box3d.h:145
lsst::sphgeom::Box3d::operator==
bool operator==(Box3d const &b) const
Two 3D boxes are equal if they contain the same points.
Definition: Box3d.h:107
lsst::sphgeom::Box3d::clippedTo
Box3d clippedTo(Box3d const &b) const
Definition: Box3d.h:236
lsst::sphgeom::Box3d::isFull
bool isFull() const
isFull returns true if this box contains all points in ℝ³.
Definition: Box3d.h:133
lsst::sphgeom::Box3d::relate
Relationship relate(Vector3d const &v) const
Definition: Box3d.h:305
lsst::sphgeom::Box3d::isEmpty
bool isEmpty() const
isEmpty returns true if this box does not contain any points.
Definition: Box3d.h:128
lsst::sphgeom::Box3d::dilateBy
Box3d & dilateBy(double r)
dilateBy minimally expands or shrinks this Box to include or remove all points within distance |r| of...
Definition: Box3d.h:276
lsst::sphgeom::Box3d::dilatedBy
Box3d dilatedBy(double r) const
Definition: Box3d.h:277
Relationship.h
This file provides a type alias for describing set relationships.
lsst::sphgeom::Box3d::Box3d
Box3d(Interval1d const &x, Interval1d const &y, Interval1d const &z)
This constructor creates a box spanning the given x, y, and z intervals.
Definition: Box3d.h:96
lsst::sphgeom::Box3d::Box3d
Box3d()
This constructor creates an empty 3D box.
Definition: Box3d.h:63
Interval1d.h
This file defines a class for representing intervals of ℝ.
lsst::sphgeom::Box3d::erodeBy
Box3d & erodeBy(double w, double h, double d)
Definition: Box3d.h:298
lsst::sphgeom::Interval::clipTo
Interval & clipTo(Scalar x)
Definition: Interval.h:159
lsst::sphgeom::Interval1d
Interval1d represents closed intervals of ℝ.
Definition: Interval1d.h:39
lsst::sphgeom::Interval::getSize
Scalar getSize() const
getSize returns the size (length, width) of this interval.
Definition: Interval.h:93
lsst::sphgeom::Vector3d
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition: Vector3d.h:44
lsst::sphgeom::Box3d::x
Interval1d const & x() const
Definition: Box3d.h:123
lsst::sphgeom::Box3d
Box3d represents a box in ℝ³.
Definition: Box3d.h:42
lsst::sphgeom::Box3d::relate
Relationship relate(Box3d const &b) const
Definition: Box3d.h:307
lsst::sphgeom::Box3d::expandTo
Box3d & expandTo(Box3d const &b)
Definition: Box3d.h:250
lsst::sphgeom::Box3d::isDisjointFrom
bool isDisjointFrom(Box3d const &b) const
Definition: Box3d.h:176
lsst::sphgeom::Box3d::isWithin
bool isWithin(Vector3d const &b) const
Definition: Box3d.h:198
lsst::sphgeom::Interval::isEmpty
bool isEmpty() const
isEmpty returns true if this interval does not contain any points.
Definition: Interval.h:83
lsst::sphgeom::Box3d::Box3d
Box3d(Vector3d const &v)
This constructor creates a box containing a single point.
Definition: Box3d.h:66
lsst::sphgeom::Interval::relate
Relationship relate(Scalar x) const
Definition: Interval.h:249
lsst::sphgeom::Box3d::isWithin
bool isWithin(Box3d const &b) const
Definition: Box3d.h:204
lsst::sphgeom::Box3d::getDepth
double getDepth() const
getDepth returns the depth (z-axis extent) of this box.
Definition: Box3d.h:153
lsst::sphgeom::Box3d::getCenter
Vector3d getCenter() const
getCenter returns the center of this box.
Definition: Box3d.h:139
lsst::sphgeom::Vector3d::z
double z() const
Definition: Vector3d.h:70
std::ostream
STL class.
Vector3d.h
This file declares a class for representing vectors in ℝ³.
lsst::sphgeom::Interval::contains
bool contains(Scalar x) const
Definition: Interval.h:98
lsst::sphgeom::Box3d::clippedTo
Box3d clippedTo(Vector3d const &b) const
Definition: Box3d.h:232
lsst::sphgeom::Interval::isWithin
bool isWithin(Scalar x) const
Definition: Interval.h:140
lsst::sphgeom::Box3d::z
Interval1d const & z() const
Definition: Box3d.h:125
lsst::sphgeom::Box3d::getHeight
double getHeight() const
getHeight returns the height (y-axis extent) of this box.
Definition: Box3d.h:149
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::sphgeom::Box3d::expandTo
Box3d & expandTo(Vector3d const &b)
Definition: Box3d.h:243
lsst::sphgeom::Box3d::intersects
bool intersects(Box3d const &b) const
Definition: Box3d.h:188
lsst::sphgeom::Box3d::expandedTo
Box3d expandedTo(Vector3d const &b) const
Definition: Box3d.h:261
lsst::sphgeom::Box3d::erodedBy
Box3d erodedBy(double w, double h, double d) const
Definition: Box3d.h:301
lsst::sphgeom::Box3d::Box3d
Box3d(Vector3d const &v, double w, double h, double d)
This constructor creates a box with center v, half-width w, half-height h, and half-depth d.
Definition: Box3d.h:86
lsst::sphgeom::Interval1d::isFull
bool isFull() const
isFull returns true if this interval = ℝ.
Definition: Interval1d.h:62
lsst::sphgeom::Interval1d::full
static Interval1d full()
Definition: Interval1d.h:47
lsst::sphgeom::Box3d::aroundUnitSphere
static Box3d aroundUnitSphere()
aroundUnitSphere returns a minimal Box3d containing the unit sphere.
Definition: Box3d.h:56
lsst::sphgeom::Vector3d::y
double y() const
Definition: Vector3d.h:68
lsst::sphgeom::Box3d::operator!=
bool operator!=(Box3d const &b) const
Definition: Box3d.h:113
lsst::sphgeom::Box3d::operator==
bool operator==(Vector3d const &v) const
A box is equal to a point if it contains only that point.
Definition: Box3d.h:116
w
double w
Definition: CoaddPsf.cc:69
lsst::sphgeom::Interval::dilateBy
Interval & dilateBy(Scalar x)
For positive x, dilateBy morphologically dilates this interval by [-x,x], which is equivalent to the ...
Definition: Interval.h:230
lsst::sphgeom::Box3d::empty
static Box3d empty()
Definition: Box3d.h:45
lsst::sphgeom::operator<<
std::ostream & operator<<(std::ostream &, Angle const &)
Definition: Angle.cc:34
lsst::sphgeom::Box3d::operator!=
bool operator!=(Vector3d const &v) const
Definition: Box3d.h:118
lsst::sphgeom::Interval::expandTo
Interval & expandTo(Scalar x)
Definition: Interval.h:192
lsst::sphgeom::Box3d::contains
bool contains(Box3d const &b) const
Definition: Box3d.h:164
lsst::sphgeom::Box3d::dilatedBy
Box3d dilatedBy(double w, double h, double d) const
Definition: Box3d.h:293
lsst::sphgeom::Interval::intersects
bool intersects(Scalar x) const
Definition: Interval.h:130
lsst::sphgeom::Interval::dilatedBy
Derived dilatedBy(Scalar x) const
Definition: Interval.h:239
lsst::sphgeom::Box3d::erodedBy
Box3d erodedBy(double r) const
Definition: Box3d.h:297
lsst::sphgeom::Vector3d::x
double x() const
Definition: Vector3d.h:66
lsst::sphgeom::Box3d::expandedTo
Box3d expandedTo(Box3d const &b) const
Definition: Box3d.h:265
lsst::sphgeom::Box3d::Box3d
Box3d(Vector3d const &v1, Vector3d const &v2)
This constructor creates a box spanning the intervals [v1.x(), v2.x()], [v1.y(), v2....
Definition: Box3d.h:76
lsst::sphgeom::Box3d::y
Interval1d const & y() const
Definition: Box3d.h:124
lsst::sphgeom::Box3d::isDisjointFrom
bool isDisjointFrom(Vector3d const &b) const
Definition: Box3d.h:174
lsst::sphgeom::Box3d::clipTo
Box3d & clipTo(Box3d const &b)
Definition: Box3d.h:221