LSST Applications g0f08755f38+c959026f09,g12f32b3c4e+3caaee4b9e,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a0ca8cf93+2f198f2aa8,g28da252d5a+7e025d8f94,g2bbee38e9b+c076ca1133,g2bc492864f+c076ca1133,g3156d2b45e+41e33cbcdc,g347aa1857d+c076ca1133,g35bb328faa+a8ce1bb630,g3a166c0a6a+c076ca1133,g3e281a1b8c+b162652f75,g414038480c+6cfc39b08c,g41af890bb2+d4c324d475,g5fbc88fb19+17cd334064,g6b1c1869cb+878204452a,g781aacb6e4+a8ce1bb630,g80478fca09+a0d7f63753,g82479be7b0+05b6a2221e,g8554a5dd34+785477f506,g858d7b2824+c959026f09,g89c8672015+ff349045bf,g9125e01d80+a8ce1bb630,g9726552aa6+3a8748fa0c,ga5288a1d22+59736ba80c,gb58c049af0+d64f4d3760,gc28159a63d+c076ca1133,gcf0d15dbbd+efb7d8614d,gd7358e8bfb+4147a68bd7,gda3e153d99+c959026f09,gda6a2b7d83+efb7d8614d,gdaeeff99f8+1711a396fd,ge2409df99d+5ddbaf9a99,ge79ae78c31+c076ca1133,gf0baf85859+50e8a91c63,gf3967379c6+f3639e9197,gfb92a5be7c+c959026f09,w.2024.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
Box.h
Go to the documentation of this file.
1/*
2 * Developed for the LSST Data Management System.
3 * This product includes software developed by the LSST Project
4 * (https://www.lsst.org).
5 * See the COPYRIGHT file at the top-level directory of this distribution
6 * for details of code ownership.
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 GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef LSST_GEOM_BOX_H
23#define LSST_GEOM_BOX_H
24
25#include <vector>
26
27#include "boost/format.hpp"
28#include "ndarray.h"
29
30#include "lsst/geom/Point.h"
31#include "lsst/geom/Extent.h"
32#include "lsst/geom/Interval.h"
33
34namespace lsst {
35namespace geom {
36
37class Box2D;
38
55class Box2I final {
56public:
57 typedef Point2I Point;
59 typedef int Element;
60
62
64
66 Box2I() noexcept : _minimum(0), _dimensions(0) {}
67
76 Box2I(Point2I const& minimum, Point2I const& maximum, bool invert = true);
77
90 Box2I(Point2I const& corner, Extent2I const& dimensions, bool invert = true);
91
98 Box2I(Interval const & x, Interval const & y) :
99 Box2I(Point(x.getMin(), y.getMin()), Point(x.getMax(), y.getMax()), false)
100 {}
101
119 explicit Box2I(Box2D const& other, EdgeHandlingEnum edgeHandling = EXPAND);
120
122 Box2I(Box2I const&) noexcept = default;
123 Box2I(Box2I&&) noexcept = default;
124 ~Box2I() noexcept = default;
125
139 static Box2I makeCenteredBox(Point2D const& center, Extent const& size);
140
141 void swap(Box2I& other) noexcept {
142 _minimum.swap(other._minimum);
143 _dimensions.swap(other._dimensions);
144 }
145
147 Box2I& operator=(Box2I const&) noexcept = default;
148 Box2I& operator=(Box2I&&) noexcept = default;
149
156 Point2I const getMin() const noexcept { return _minimum; }
157 int getMinX() const noexcept { return _minimum.getX(); }
158 int getMinY() const noexcept { return _minimum.getY(); }
159
160 Point2I const getMax() const noexcept { return _minimum + _dimensions - Extent2I(1); }
161 int getMaxX() const noexcept { return _minimum.getX() + _dimensions.getX() - 1; }
162 int getMaxY() const noexcept { return _minimum.getY() + _dimensions.getY() - 1; }
164
171 Point2I const getBegin() const noexcept { return _minimum; }
172 int getBeginX() const noexcept { return _minimum.getX(); }
173 int getBeginY() const noexcept { return _minimum.getY(); }
174
175 Point2I const getEnd() const noexcept { return _minimum + _dimensions; }
176 int getEndX() const noexcept { return _minimum.getX() + _dimensions.getX(); }
177 int getEndY() const noexcept { return _minimum.getY() + _dimensions.getY(); }
179
186 Extent2I const getDimensions() const noexcept { return _dimensions; }
187 int getWidth() const noexcept { return _dimensions.getX(); }
188 int getHeight() const noexcept { return _dimensions.getY(); }
189 int getArea() const { return getWidth() * getHeight(); }
191
198 Point2D const getCenter() const noexcept;
199 double getCenterX() const noexcept { return this->getCenter().getX(); }
200 double getCenterY() const noexcept { return this->getCenter().getY(); }
202
204
208
210 ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
211
213 bool isEmpty() const noexcept { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
214
216
217 bool contains(Point2I const& point) const noexcept;
218 bool contains(Element x, Element y) const noexcept { return contains(Point2I(x, y)); }
220
226 bool contains(Box2I const& other) const noexcept;
227
229
234 bool overlaps(Box2I const& other) const noexcept;
235 bool intersects(Box2I const& other) const noexcept { return overlaps(other); }
237
241 bool isDisjointFrom(Box2I const & other) const noexcept;
242
249 void grow(int buffer) { grow(Extent2I(buffer)); }
250
257 void grow(Extent2I const& buffer);
258
260 void shift(Extent2I const& offset);
261
263 void flipLR(int xExtent);
264
266 void flipTB(int yExtent);
267
269 void include(Point2I const& point);
270
272 void include(Box2I const& other);
273
280 void clip(Box2I const& other) noexcept;
281
294 Box2I dilatedBy(Extent const & buffer) const;
295 Box2I dilatedBy(Element buffer) const {
296 return dilatedBy(Extent(buffer, buffer));
297 }
299
312 Box2I erodedBy(Extent const & buffer) const { return dilatedBy(-buffer); }
313 Box2I erodedBy(Element buffer) const { return dilatedBy(-buffer); }
315
322 Box2I shiftedBy(Extent const & offset) const;
323
330
337
339
347 Box2I expandedTo(Point const & other) const;
348 Box2I expandedTo(Box2I const & other) const;
350
358 Box2I clippedTo(Box2I const & other) const noexcept;
359
365 bool operator==(Box2I const& other) const noexcept;
366
372 bool operator!=(Box2I const& other) const noexcept;
373
375 std::size_t hash_value() const noexcept;
376
383 std::vector<Point2I> getCorners() const;
384
385 std::string toString() const {
386 return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
387 }
388
389private:
390 Point2I _minimum;
391 Extent2I _dimensions;
392};
393
413class Box2D final {
414public:
415 typedef Point2D Point;
417 typedef double Element;
418
420
425 static double const EPSILON;
426
428 static double const INVALID;
429
431 Box2D() noexcept;
432
443 Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true) noexcept;
444
455 Box2D(Point2D const& corner, Extent2D const& dimensions, bool invert = true) noexcept;
456
463 Box2D(Interval const & x, Interval const & y) :
464 Box2D(Point(x.getMin(), y.getMin()), Point(x.getMax(), y.getMax()), false)
465 {}
466
476 explicit Box2D(Box2I const& other) noexcept;
477
479 Box2D(Box2D const&) noexcept = default;
480 Box2D(Box2D&&) noexcept = default;
481
482 ~Box2D() noexcept = default;
483
495 // It's hard to guarantee postconditions (especially size) with non-finite inputs
496 static Box2D makeCenteredBox(Point2D const& center, Extent const& size) noexcept;
497
498 void swap(Box2D& other) noexcept {
499 _minimum.swap(other._minimum);
500 _maximum.swap(other._maximum);
501 }
502
504 Box2D& operator=(Box2D const&) noexcept = default;
505 Box2D& operator=(Box2D&&) noexcept = default;
506
513 Point2D const getMin() const noexcept { return _minimum; }
514 double getMinX() const noexcept { return _minimum.getX(); }
515 double getMinY() const noexcept { return _minimum.getY(); }
516
517 Point2D const getMax() const noexcept { return _maximum; }
518 double getMaxX() const noexcept { return _maximum.getX(); }
519 double getMaxY() const noexcept { return _maximum.getY(); }
521
528 Extent2D const getDimensions() const noexcept { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
529 double getWidth() const noexcept { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
530 double getHeight() const noexcept { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
531 double getArea() const noexcept {
532 Extent2D dim(getDimensions());
533 return dim.getX() * dim.getY();
534 }
536
538
542
549 Point2D const getCenter() const noexcept {
550 return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5);
551 }
552 double getCenterX() const noexcept { return (_minimum.getX() + _maximum.getX()) * 0.5; }
553 double getCenterY() const noexcept { return (_minimum.getY() + _maximum.getY()) * 0.5; }
555
557 bool isEmpty() const noexcept { return _minimum.getX() != _minimum.getX(); }
558
560
561 bool contains(Point2D const& point) const noexcept;
562 bool contains(Element x, Element y) const noexcept { return contains(Point2D(x, y)); }
564
570 bool contains(Box2D const& other) const;
571
573
578 bool overlaps(Box2D const& other) const noexcept;
579 bool intersects(Box2D const& other) const noexcept { return overlaps(other); }
581
582
586 bool isDisjointFrom(Box2D const & other) const noexcept;
587
594 void grow(double buffer) { grow(Extent2D(buffer)); }
595
602 void grow(Extent2D const& buffer);
603
605 void shift(Extent2D const& offset);
606
608 void flipLR(float xExtent);
609
611 void flipTB(float yExtent);
612
620 void include(Point2D const& point) noexcept;
621
623 void include(Box2D const& other) noexcept;
624
631 void clip(Box2D const& other) noexcept;
632
634
649 Box2D dilatedBy(Extent const & buffer) const;
650 Box2D dilatedBy(Element buffer) const {
651 return dilatedBy(Extent(buffer, buffer));
652 }
654
656
671 Box2D erodedBy(Extent const & buffer) const { return dilatedBy(-buffer); }
672 Box2D erodedBy(Element buffer) const { return dilatedBy(-buffer); }
674
685 Box2D shiftedBy(Extent const & offset) const;
686
696
706
717 Box2D expandedTo(Point const & other) const;
718
725 Box2D expandedTo(Box2D const & other) const;
726
734 Box2D clippedTo(Box2D const & other) const;
735
741 bool operator==(Box2D const& other) const noexcept;
742
748 bool operator!=(Box2D const& other) const noexcept;
749
751 std::size_t hash_value() const noexcept;
752
759 std::vector<Point2D> getCorners() const;
760
761 std::string toString() const {
762 return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
763 }
764
765private:
766 void _tweakMax(int n) noexcept {
767 if (_maximum[n] < 0.0) {
768 _maximum[n] *= (1.0 - EPSILON);
769 } else if (_maximum[n] > 0.0) {
770 _maximum[n] *= (1.0 + EPSILON);
771 } else {
772 _maximum[n] = EPSILON;
773 }
774 }
775 Point2D _minimum;
776 Point2D _maximum;
777};
778
779typedef Box2D BoxD;
780typedef Box2I BoxI;
781
782std::ostream& operator<<(std::ostream& os, Box2I const& box);
783
784std::ostream& operator<<(std::ostream& os, Box2D const& box);
785
786} // namespace geom
787} // namespace lsst
788
789namespace std {
790template <>
794 size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
795};
796
797template <>
801 size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
802};
803} // namespace std
804
805#endif
int y
Definition SpanSet.cc:48
A floating-point coordinate rectangle geometry.
Definition Box.h:413
double getMaxY() const noexcept
Definition Box.h:519
Box2D(Box2D const &) noexcept=default
Standard copy constructor.
Point2D const getMax() const noexcept
Definition Box.h:517
double getMaxX() const noexcept
Definition Box.h:518
Box2D dilatedBy(Extent const &buffer) const
Increase the size of the box by the given amount(s) on all sides (returning a new object).
Definition Box.cc:438
bool operator==(Box2D const &other) const noexcept
Compare two boxes for equality.
Definition Box.cc:476
double Element
Definition Box.h:417
Extent2D const getDimensions() const noexcept
1-d interval accessors
Definition Box.h:528
Box2D dilatedBy(Element buffer) const
Return true if the box contains no points.
Definition Box.h:650
bool operator!=(Box2D const &other) const noexcept
Compare two boxes for equality.
Definition Box.cc:481
void shift(Extent2D const &offset)
Shift the position of the box by the given offset.
Definition Box.cc:350
double getMinY() const noexcept
Definition Box.h:515
void clip(Box2D const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition Box.cc:416
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition Box.cc:486
double getWidth() const noexcept
1-d interval accessors
Definition Box.h:529
static Box2D makeCenteredBox(Point2D const &center, Extent const &size) noexcept
Create a box centered on a particular point.
Definition Box.cc:316
Box2D erodedBy(Extent const &buffer) const
Decrease the size of the box by the given amount(s) on all sides (returning a new object).
Definition Box.h:671
Box2D() noexcept
Construct an empty box.
Definition Box.cc:274
Point2D const getCenter() const noexcept
Return true if the box contains no points.
Definition Box.h:549
Box2D & operator=(Box2D &&) noexcept=default
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition Box.h:557
Box2D & operator=(Box2D const &) noexcept=default
Standard assignment operator.
Point2D const getMin() const noexcept
Definition Box.h:513
double getMinX() const noexcept
Definition Box.h:514
bool isDisjointFrom(Box2D const &other) const noexcept
Return true if there are no points in both this and other.
Definition Box.cc:339
Interval getY() const
1-d interval accessors
Definition Box.h:540
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition Box.h:594
Box2D reflectedAboutY(Element y) const
Reflect the box about a horizontal line (returning a new object).
Definition Box.cc:453
void flipLR(float xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition Box.cc:356
Extent2D Extent
Definition Box.h:416
void flipTB(float yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition Box.cc:368
Interval getX() const
1-d interval accessors
Definition Box.h:539
Box2D reflectedAboutX(Element x) const
Reflect the box about a vertical line (returning a new object).
Definition Box.cc:448
double getCenterX() const noexcept
Return true if the box contains no points.
Definition Box.h:552
void include(Point2D const &point) noexcept
Expand this to ensure that this->contains(point).
Definition Box.cc:380
bool contains(Point2D const &point) const noexcept
Return true if the box contains the point.
Definition Box.cc:322
Box2D erodedBy(Element buffer) const
Return true if the box contains no points.
Definition Box.h:672
bool overlaps(Box2D const &other) const noexcept
Return true if any points in other are also in this.
Definition Box.cc:332
bool contains(Element x, Element y) const noexcept
Return true if the box contains no points.
Definition Box.h:562
bool intersects(Box2D const &other) const noexcept
Return true if the box contains no points.
Definition Box.h:579
double getArea() const noexcept
1-d interval accessors
Definition Box.h:531
Box2D clippedTo(Box2D const &other) const
Shrink a box to ensure that it is contained by other (returning a new object).
Definition Box.cc:471
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount.
Definition Box.h:425
Point2D Point
Definition Box.h:415
Box2D expandedTo(Point const &other) const
Expand a box to ensure that contains(other) is true (returning a new object).
Definition Box.cc:458
std::vector< Point2D > getCorners() const
Get the corner points.
Definition Box.cc:496
Box2D shiftedBy(Extent const &offset) const
Shift the position of the box by the given offset (returning a new object).
Definition Box.cc:443
double getHeight() const noexcept
1-d interval accessors
Definition Box.h:530
std::string toString() const
Return true if the box contains no points.
Definition Box.h:761
static double const INVALID
Value used to specify undefined coordinate values.
Definition Box.h:428
double getCenterY() const noexcept
Return true if the box contains no points.
Definition Box.h:553
Box2D(Box2D &&) noexcept=default
An integer coordinate rectangle.
Definition Box.h:55
int getBeginX() const noexcept
Definition Box.h:172
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition Box.cc:189
Extent2I Extent
Definition Box.h:58
int getMinY() const noexcept
Definition Box.h:158
bool isDisjointFrom(Box2I const &other) const noexcept
Return true if there are no points in both this and other.
Definition Box.cc:126
bool overlaps(Box2I const &other) const noexcept
Return true if any points in other are also in this.
Definition Box.cc:122
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition Box.cc:152
int getHeight() const noexcept
Definition Box.h:188
Box2I(Interval const &x, Interval const &y)
Construct a box from a pair of intervals.
Definition Box.h:98
Box2I expandedTo(Point const &other) const
Expand the box to ensure that contains(other) is true (returning a new object).
Definition Box.cc:233
Point2I const getMin() const noexcept
Definition Box.h:156
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition Box.h:213
Point2I const getMax() const noexcept
Definition Box.h:160
Box2I erodedBy(Element buffer) const
1-d interval accessors
Definition Box.h:313
int getEndY() const noexcept
Definition Box.h:177
Box2I clippedTo(Box2I const &other) const noexcept
Shrink an interval to ensure that it is contained by other (returning a new object).
Definition Box.cc:243
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition Box.h:249
Box2I erodedBy(Extent const &buffer) const
Decrease the size of the box by the given amount(s) on all sides (returning a new object).
Definition Box.h:312
Point2I const getBegin() const noexcept
Definition Box.h:171
Box2I dilatedBy(Extent const &buffer) const
Increase the size of the box by the given amount(s) on all sides (returning a new object).
Definition Box.cc:213
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition Box.cc:256
int getMinX() const noexcept
Definition Box.h:157
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition Box.cc:97
Point2I Point
Definition Box.h:57
int getWidth() const noexcept
Definition Box.h:187
int getArea() const
Definition Box.h:189
int getBeginY() const noexcept
Definition Box.h:173
int getMaxX() const noexcept
Definition Box.h:161
Interval getX() const
1-d interval accessors
Definition Box.h:205
std::vector< Point2I > getCorners() const
Get the corner points.
Definition Box.cc:261
Box2I reflectedAboutX(Element x) const
Reflect the box about a vertical line (returning a new object).
Definition Box.cc:223
bool contains(Point2I const &point) const noexcept
Return true if the box contains the point.
Definition Box.cc:114
Box2I & operator=(Box2I &&) noexcept=default
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition Box.cc:134
bool contains(Element x, Element y) const noexcept
1-d interval accessors
Definition Box.h:218
std::string toString() const
1-d interval accessors
Definition Box.h:385
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition Box.cc:145
Box2I & operator=(Box2I const &) noexcept=default
Standard assignment operator.
ndarray::View< boost::fusion::vector2< ndarray::index::Range, ndarray::index::Range > > getSlices() const
Return slices to extract the box's region from an ndarray::Array.
Definition Box.cc:109
int getMaxY() const noexcept
Definition Box.h:162
double getCenterY() const noexcept
1-d interval accessors
Definition Box.h:200
Box2I(Box2I const &) noexcept=default
Standard copy constructor.
Box2I reflectedAboutY(Element y) const
Reflect the box about a horizontal line (returning a new object).
Definition Box.cc:228
Box2I(Box2I &&) noexcept=default
bool operator!=(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition Box.cc:252
Point2I const getEnd() const noexcept
Definition Box.h:175
bool operator==(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition Box.cc:248
Interval getY() const
1-d interval accessors
Definition Box.h:206
Extent2I const getDimensions() const noexcept
Definition Box.h:186
Box2I() noexcept
Construct an empty box.
Definition Box.h:66
double getCenterX() const noexcept
1-d interval accessors
Definition Box.h:199
Box2I dilatedBy(Element buffer) const
1-d interval accessors
Definition Box.h:295
bool intersects(Box2I const &other) const noexcept
1-d interval accessors
Definition Box.h:235
Box2I shiftedBy(Extent const &offset) const
Shift the position of the box by the given offset (returning a new object).
Definition Box.cc:218
Point2D const getCenter() const noexcept
1-d interval accessors
Definition Box.cc:93
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition Box.cc:138
int getEndX() const noexcept
Definition Box.h:176
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
A floating-point coordinate rectangle geometry.
Definition Interval.h:413
static IntervalD fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
Definition Interval.cc:226
A 1-d integer coordinate range.
Definition Interval.h:50
static IntervalI fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
Definition Interval.cc:57
std::string toString() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition Point.h:143
void swap(Point &other) noexcept
Definition Point.h:202
Extent< double, 2 > Extent2D
Definition Extent.h:400
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
Box2D BoxD
Definition Box.h:779
Point< double, 2 > Point2D
Definition Point.h:324
Extent< int, 2 > Extent2I
Definition Extent.h:397
Box2I BoxI
Definition Box.h:780
Point< int, 2 > Point2I
Definition Point.h:321
STL namespace.
decltype(sizeof(void *)) size_t
Definition doctest.h:524
size_t operator()(argument_type const &x) const noexcept
Definition Box.h:801
size_t operator()(argument_type const &x) const noexcept
Definition Box.h:794