Blobalizer
Loading...
Searching...
No Matches
BoundingBox.h
Go to the documentation of this file.
1#pragma once
2
3#include <Magnum/Magnum.h>
4#include "Utils.h"
5
9template <size_t size, class T>
12 typedef Magnum::Math::Vector<size, float> PointType;
13
16 reset();
17 }
18
22 : min(p), max(p) {}
23
29 : min(min), max(max) {
30 }
31
33 bool operator==(const BoundingBox &other) const {
34 return this->min == other.min && this->max == other.max;
35 }
36
38 bool operator!=(const BoundingBox &other) const {
39 return this->min != other.min || this->max != other.max;
40 }
41
44 PointType center() const {
45 return (this->max + this->min) * (T)0.5;
46 }
47
49 void reset() {
50 this->min[0] = +std::numeric_limits<T>::infinity();
51 this->min[1] = +std::numeric_limits<T>::infinity();
52 this->min[2] = +std::numeric_limits<T>::infinity();
53
54 this->max[0] = -std::numeric_limits<T>::infinity();
55 this->max[1] = -std::numeric_limits<T>::infinity();
56 this->max[2] = -std::numeric_limits<T>::infinity();
57 }
58
61 PointType size() const {
62 return this->max - this->min;
63 }
64
67 PointType extends() const {
68 return this->size() * (T)0.5;
69 }
70
73 void expandBy(const PointType &p) {
74 cwiseMinInplace(this->min, p);
75 cwiseMaxInplace(this->max, p);
76 }
77
80 void expandBy(const BoundingBox& other) {
81 cwiseMinInplace(this->min, other.min);
82 cwiseMaxInplace(this->max, other.max);
83 }
84
88 bool contains(const PointType& p) const {
89 return (p[0] >= this->min[0] && p[0] <= this->max[0]) &&
90 (p[1] >= this->min[1] && p[1] <= this->max[1]) &&
91 (p[2] >= this->min[2] && p[2] <= this->max[2]);
92 }
93
95 PointType min;
98};
99
BoundingBox< 1, float > BoundingBox1f
Definition BoundingBox.h:100
BoundingBox< 4, float > BoundingBox4f
Definition BoundingBox.h:103
BoundingBox< 3, float > BoundingBox3f
Definition BoundingBox.h:102
BoundingBox< 2, float > BoundingBox2f
Definition BoundingBox.h:101
void cwiseMinInplace(Magnum::Math::Vector< size, T > &a, const Magnum::Math::Vector< size, T > &b)
Calculate the component-wise minimum of the given vectors and stores it into the first given vector.
Definition Utils.h:24
void cwiseMaxInplace(Magnum::Math::Vector< size, T > &a, const Magnum::Math::Vector< size, T > &b)
Calculate the component-wise maximum of the given vectors and stores it into the first given vector.
Definition Utils.h:51
A generic 3D axis-aligned bounding box defined by its minimum and maximum points.
Definition BoundingBox.h:10
Magnum::Math::Vector< size, float > PointType
The type of points that make up this box.
Definition BoundingBox.h:12
BoundingBox(const PointType &min, const PointType &max)
Creates a new minimal bounding box containing the two points. The given minimum point has to be small...
Definition BoundingBox.h:28
bool contains(const PointType &p) const
Checks if the given point is contained within this bounding box.
Definition BoundingBox.h:88
void expandBy(const BoundingBox &other)
Minimally expand this box to encompass the given bounding box.
Definition BoundingBox.h:80
bool operator!=(const BoundingBox &other) const
Compares this bounding box to another and checks if they are not equal.
Definition BoundingBox.h:38
void reset()
Resets this bounding box to an undefined state.
Definition BoundingBox.h:49
BoundingBox()
Constructs a new empty bounding box.
Definition BoundingBox.h:15
BoundingBox(const PointType &p)
Creates a new bounding box contained the given point with zero size.
Definition BoundingBox.h:21
PointType extends() const
Returns the extends of this bounding box, this equal to half the size or the distance from the center...
Definition BoundingBox.h:67
PointType max
Component-wise maximum.
Definition BoundingBox.h:97
PointType size() const
Returns the size of this bounding box. The result of this method on an undefined bounding box will be...
Definition BoundingBox.h:61
PointType min
Component-wise minimum.
Definition BoundingBox.h:95
void expandBy(const PointType &p)
Minimally expand this box to encompass the given point.
Definition BoundingBox.h:73
bool operator==(const BoundingBox &other) const
Compares this bounding box to another and checks if they are equal.
Definition BoundingBox.h:33
PointType center() const
Returns the center of this bounding box. The result of this method on an undefined bounding box will ...
Definition BoundingBox.h:44