3es  0.7
3esbounds.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESBOUNDS_H
5 #define _3ESBOUNDS_H
6 
7 #include "3es-core.h"
8 
9 #include "3esvector3.h"
10 
11 #include <limits>
12 
13 namespace tes
14 {
16  template <typename T>
17  class Bounds
18  {
19  public:
21  Bounds();
22 
25  Bounds(const Bounds<T> &other);
26 
31  template <typename Q>
32  Bounds(const Bounds<Q> &other);
33 
39  Bounds(const Vector3<T> &minExt, const Vector3<T> maxExt);
40 
43  const Vector3<T> &minimum() const;
46  const Vector3<T> &maximum() const;
47 
50  void expand(const Vector3<T> &point);
51 
54  void expand(const Bounds<T> &other);
55 
59  bool isValid() const;
60 
64  bool operator==(const Bounds<T> &other) const;
65 
69  bool operator!=(const Bounds<T> &other) const;
70 
74  Bounds<T> &operator = (const Bounds<T> &other);
75 
76  private:
77  Vector3<T> _minimum;
78  Vector3<T> _maximum;
79  };
80 
82  template class _3es_coreAPI Bounds<float>;
84  template class _3es_coreAPI Bounds<double>;
85  typedef Bounds<float> Boundsf;
86  typedef Bounds<double> Boundsd;
87 
88  template <typename T>
90  : _minimum( std::numeric_limits<T>::max())
91  , _maximum(-std::numeric_limits<T>::max())
92  {
93  }
94 
95 
96  template <typename T>
97  inline Bounds<T>::Bounds(const Bounds<T> &other)
98  {
99  *this = other;
100  }
101 
102 
103  template <typename T>
104  template <typename Q>
105  inline Bounds<T>::Bounds(const Bounds<Q> &other)
106  {
107  _minimum = Vector3<T>(other._minimum);
108  _maximum = Vector3<T>(other._maximum);
109  }
110 
111 
112  template <typename T>
113  inline Bounds<T>::Bounds(const Vector3<T> &minExt, const Vector3<T> maxExt)
114  : _minimum(minExt)
115  , _maximum(maxExt)
116  {
117  }
118 
119 
120  template <typename T>
121  inline const Vector3<T> &Bounds<T>::minimum() const
122  {
123  return _minimum;
124  }
125 
126 
127  template <typename T>
128  inline const Vector3<T> &Bounds<T>::maximum() const
129  {
130  return _maximum;
131  }
132 
133 
134  template <typename T>
135  inline void Bounds<T>::expand(const Vector3<T> &point)
136  {
137  _minimum.x = (point.x < _minimum.x) ? point.x : _minimum.x;
138  _minimum.y = (point.y < _minimum.y) ? point.y : _minimum.y;
139  _minimum.z = (point.z < _minimum.z) ? point.z : _minimum.z;
140  _maximum.x = (point.x > _maximum.x) ? point.x : _maximum.x;
141  _maximum.y = (point.y > _maximum.y) ? point.y : _maximum.y;
142  _maximum.z = (point.z > _maximum.z) ? point.z : _maximum.z;
143  }
144 
145 
146  template <typename T>
147  inline void Bounds<T>::expand(const Bounds<T> &other)
148  {
149  expand(other.minimum());
150  expand(other.maximum());
151  }
152 
153 
154  template <typename T>
155  inline bool Bounds<T>::isValid() const
156  {
157  return _minimum.x <= _maximum.x && _minimum.y <= _maximum.y && _minimum.z <= _maximum.z;
158  }
159 
160 
161  template <typename T>
162  inline bool Bounds<T>::operator==(const Bounds<T> &other) const
163  {
164  return _minimum.x == other._minimum.x && _minimum.y == other._minimum.y && _minimum.z == other._minimum.z &&
165  _maximum.x == other._maximum.x && _maximum.y == other._maximum.y && _maximum.z == other._maximum.z;
166  }
167 
168 
169  template <typename T>
170  inline bool Bounds<T>::operator!=(const Bounds<T> &other) const
171  {
172  return !operator==(other);
173  }
174 
175 
176  template <typename T>
178  {
179  _minimum = other._minimum;
180  _maximum = other._maximum;
181  return *this;
182  }
183 }
184 
185 
186 #endif // _3ESBOUNDS_H
Bounds< T > & operator=(const Bounds< T > &other)
Assignment operator.
Definition: 3esbounds.h:177
bool operator!=(const Bounds< T > &other) const
Precise inequality operator.
Definition: 3esbounds.h:170
void expand(const Vector3< T > &point)
Expand the bounding box to include point.
Definition: 3esbounds.h:135
const Vector3< T > & maximum() const
Access the maximum extents.
Definition: 3esbounds.h:128
Definition: 3esbounds.h:13
bool operator==(const Bounds< T > &other) const
Precise equality operator.
Definition: 3esbounds.h:162
Bounds()
Initialises bounds where max < min at using the limits of the type T.
Definition: 3esbounds.h:89
Represents a vector in R3.
Definition: 3esvector3.h:14
T x
Direct data member access.
Definition: 3esvector3.h:30
const Vector3< T > & minimum() const
Access the minimum extents.
Definition: 3esbounds.h:121
A simple bounding box structure.
Definition: 3esbounds.h:17
bool isValid() const
Returns true if the bounds are valid, with minimum extents less than or equal to the maximum...
Definition: 3esbounds.h:155