Itasca C++ Interface
Loading...
Searching...
No Matches
cube.h
Go to the documentation of this file.
1#pragma once
8#include "extent2.h"
9
16template <class T> class Cube
17{
18public:
19 // Creators
21 Cube() { }
23 Cube(const T &x1,const T &x2,const T &y1,const T &y2,const T &z1,const T &z2)
24 :tx1_(x1), tx2_(x2), ty1_(y1), ty2_(y2), tz1_(z1), tz2_(z2) { }
26 Cube(const Vector3<T> &v111,const Vector3<T> &v222)
27 :tx1_(v111.x()), tx2_(v222.x()), ty1_(v111.y()), ty2_(v222.y()), tz1_(v111.z()), tz2_(v222.z()) { }
29PUSHWARNING
30#pragma clang diagnostic ignored "-Wdeprecated-copy"
31 Cube(const Cube<T> &r) : tx1_(r.tx1_), tx2_(r.tx2_), ty1_(r.ty1_), ty2_(r.ty2_), tz1_(r.tz1_), tz2_(r.tz2_) { }
33 explicit Cube(const Extent2<T> &r) : tx1_(r.x1()), tx2_(r.x2()), ty1_(r.y1()), ty2_(r.y2()), tz1_(0), tz2_(1) { }
38POPWARNING
39 // Accessors
41 const T &x1() const { return tx1_; }
43 const T &x2() const { return tx2_; }
45 const T &y1() const { return ty1_; }
47 const T &y2() const { return ty2_; }
49 const T &z1() const { return tz1_; }
51 const T &z2() const { return tz2_; }
53 const T &dof1(uint32 dof) const { assert(dof<3); return *(&tx1_+(2*dof)); }
55 const T &dof2(uint32 dof) const { assert(dof<3); return *(&tx2_+(2*dof)); }
57 T width() const { return (tx2_-tx1_); }
59 T height() const { return (ty2_-ty1_); }
61 T depth() const { return (tz2_-tz1_); }
63 Vector3<T> centroid() const { Vector3<T> v((tx1_+tx2_)/2,(ty1_+ty2_)/2,(tz1_+tz2_)/2); return v; }
65 T volume() const { return (width()*height()*depth()); }
67 T diagonal() const { return (size().mag()); }
70 Vector3<T> corner(uint32 x,uint32 y,uint32 z) const
71 { if (x) {
72 if (y) { if (z) return c222(); else return c221(); }
73 else { if (z) return c212(); else return c211(); }
74 } else {
75 if (y) { if (z) return c122(); else return c121(); }
76 else { if (z) return c112(); else return c111(); }
77 }
78 }
79
80 // Corner functions - returns the 8 corners of the region.
82 Vector3<T> c111() const { Vector3<T> out(tx1_,ty1_,tz1_); return out; }
84 Vector3<T> c121() const { Vector3<T> out(tx1_,ty2_,tz1_); return out; }
86 Vector3<T> c211() const { Vector3<T> out(tx2_,ty1_,tz1_); return out; }
88 Vector3<T> c221() const { Vector3<T> out(tx2_,ty2_,tz1_); return out; }
90 Vector3<T> c112() const { Vector3<T> out(tx1_,ty1_,tz2_); return out; }
92 Vector3<T> c122() const { Vector3<T> out(tx1_,ty2_,tz2_); return out; }
94 Vector3<T> c212() const { Vector3<T> out(tx2_,ty1_,tz2_); return out; }
96 Vector3<T> c222() const { Vector3<T> out(tx2_,ty2_,tz2_); return out; }
98 Vector3<T> lowerBound() const { return c111(); }
100 Vector3<T> upperBound() const { return c222(); }
102 Vector3<T> size() const { Vector3<T> out(width(),height(),depth()); return out; }
103
104 // Comparison Operators
106 bool isEmpty() const { return ( (tx1_>=tx2_) || (ty1_>=ty2_) || (tz1_>=tz2_) ); }
108 bool tolIsEmpty(const double &tol = limits<double>::epsilon() * 100) const { return ( (tx1_ >= tx2_ + tol) || (ty1_ >= ty2_ + tol) || (tz1_ >= tz2_ + tol) ); }
110 bool operator==(const Cube<T> &c) const
111 {
112 return( (tx1_==c.tx1_)&&(tx2_==c.tx2_)&&(ty1_==c.ty1_)&&(ty2_==c.ty2_)&&(tz1_==c.tz1_)&&(tz2_==c.tz2_) );
113 }
115 bool operator!=(const Cube<T> &r) const { return !operator==(r); }
117 bool operator<(const Cube<T> &c) const { return (volume() < c.volume()); }
119 bool operator>(const Cube<T> &c) const { return (volume() > c.volume()); }
121 bool isIn(const Vector3<T> &v) const
122 {
123 if ( (v.x()>=tx1_) && (v.x()<=tx2_) && (v.y()>=ty1_) && (v.y()<=ty2_) && (v.z()>=tz1_) && (v.z()<=tz2_) ) return true;
124 return false;
125 }
127 bool isIn(const Cube<T> &c) const
128 {
129 if ( (c.x1()>=tx1_) && (c.x2()<=tx2_) && (c.y1()>=ty1_) && (c.y2()<=ty2_) && (c.z1()>=tz1_) && (c.z2()<=tz2_) ) return true;
130 return false;
131 }
133 bool tolIsIn(const Vector3<T> &v,const T &tol) const
134 {
135 if ( (v.x()>=tx1_-tol) && (v.x()<=tx2_+tol) && (v.y()>=ty1_-tol) && (v.y()<=ty2_+tol) && (v.z()>=tz1_-tol) && (v.z()<=tz2_+tol) ) return true;
136 return false;
137 }
139 bool tolIsIn(const Cube<T> &c,const T &tol) const
140 {
141 if ( (c.x1()>=tx1_-tol) && (c.x2()<=tx2_+tol) && (c.y1()>=ty1_-tol) && (c.y2()<=ty2_+tol) && (c.z1()>=tz1_-tol) && (c.z2()<=tz2_+tol) ) return true;
142 return false;
143 }
145 bool intersects(const Cube<T> &c) const
146 {
147 if ((c.tx2_<tx1_) || (c.tx1_>tx2_) ||
148 (c.ty2_<ty1_) || (c.ty1_>ty2_) ||
149 (c.tz2_<tz1_) || (c.tz1_>tz2_)) return false;
150 return true;
151 }
153 bool tolIntersects(const Cube<T> &c,const T &tol) const
154 {
155 if ((c.tx2_<tx1_-tol) || (c.tx1_>tx2_+tol) ||
156 (c.ty2_<ty1_-tol) || (c.ty1_>ty2_+tol) ||
157 (c.tz2_<tz1_-tol) || (c.tz1_>tz2_+tol)) return false;
158 return true;
159 }
160
161 // Setters
162 T &rx1() { return tx1_; }
163 T &rx2() { return tx2_; }
164 T &ry1() { return ty1_; }
165 T &ry2() { return ty2_; }
166 T &rz1() { return tz1_; }
167 T &rz2() { return tz2_; }
169 T &rdof1(uint32 dof) { assert(dof<3); return *(&tx1_+(2*dof)); }
171 T &rdof2(uint32 dof) { assert(dof<3); return *(&tx2_+(2*dof)); }
172 // Setting width, height, depth assumes LL corner (c111) stays constant.
174 void width(const T &t) { tx2_ = tx1_ + t; }
176 void height(const T &t) { ty2_ = ty1_ + t; }
178 void depth(const T &t) { tz2_ = tz1_ + t; }
180 void c111(const Vector3<T> &v) { tx1_ = v.x(); ty1_ = v.y(); tz1_ = v.z(); }
182 void c121(const Vector3<T> &v) { tx1_ = v.x(); ty2_ = v.y(); tz1_ = v.z(); }
184 void c211(const Vector3<T> &v) { tx2_ = v.x(); ty1_ = v.y(); tz1_ = v.z(); }
186 void c221(const Vector3<T> &v) { tx2_ = v.x(); ty2_ = v.y(); tz1_ = v.z(); }
188 void c112(const Vector3<T> &v) { tx1_ = v.x(); ty1_ = v.y(); tz2_ = v.z(); }
190 void c122(const Vector3<T> &v) { tx1_ = v.x(); ty2_ = v.y(); tz2_ = v.z(); }
192 void c212(const Vector3<T> &v) { tx2_ = v.x(); ty1_ = v.y(); tz2_ = v.z(); }
194 void c222(const Vector3<T> &v) { tx2_ = v.x(); ty2_ = v.y(); tz2_ = v.z(); }
196 void lowerBound(const Vector3<T> &v) { c111(v); }
198 void upperBound(const Vector3<T> &v) { c222(v); }
200 void size(const Vector3<T> &v) { width(v.x()); height(v.y()); depth(v.z()); }
203 Vector3<T> bound(const Vector3<T> &v) const { return Vector3<T>(pBound(tx1_,v.x(),tx2_),pBound(ty1_,v.y(),ty2_),pBound(tz1_,v.z(),tz2_)); }
204
205 // Manipulators - unary in place
207 const Cube<T> &operator+=(const Vector3<T> &v) { tx1_+=v.x(); tx2_+=v.x(); ty1_+=v.y(); ty2_+=v.y(); tz1_+=v.z(); tz2_+=v.z(); return *this;}
209 const Cube<T> &operator-=(const Vector3<T> &v) { tx1_-=v.x(); tx2_-=v.x(); ty1_-=v.y(); ty2_-=v.y(); tz1_-=v.z(); tz2_-=v.z(); return *this;}
210
211 // binary operators
213 Cube<T> operator+(const Vector3<T> &v) const { Cube<T> out(tx1_+v.x(),tx2_+v.x(),ty1_+v.y(),ty2_+v.y(),tz1_+v.z(),tz2_+v.z()); return out; }
215 Cube<T> operator-(const Vector3<T> &v) const { Cube<T> out(tx1_-v.x(),tx2_-v.x(),ty1_-v.y(),ty2_-v.y(),tz1_-v.z(),tz2_-v.z()); return out; }
218 {
219 Cube<T> out(std::max<T>(tx1_,r.tx1_),std::min<T>(tx2_,r.tx2_),
220 std::max<T>(ty1_,r.ty1_),std::min<T>(ty2_,r.ty2_),
221 std::max<T>(tz1_,r.tz1_),std::min<T>(tz2_,r.tz2_));
222 return out;
223 }
226 {
227 tx1_ = std::min<T>(tx1_,r.tx1_);
228 ty1_ = std::min<T>(ty1_,r.ty1_);
229 tz1_ = std::min<T>(tz1_,r.tz1_);
230 tx2_ = std::max<T>(tx2_,r.tx2_);
231 ty2_ = std::max<T>(ty2_,r.ty2_);
232 tz2_ = std::max<T>(tz2_,r.tz2_);
233 return *this;
234 }
237 {
238 tx1_ = std::min<T>(tx1_,v.x());
239 ty1_ = std::min<T>(ty1_,v.y());
240 tz1_ = std::min<T>(tz1_,v.z());
241 tx2_ = std::max<T>(tx2_,v.x());
242 ty2_ = std::max<T>(ty2_,v.y());
243 tz2_ = std::max<T>(tz2_,v.z());
244 return *this;
245 }
247 const Cube<T> &expandToInclude(const T &x,const T &y,const T &z)
248 {
249 return expandToInclude(Vector3<T>(x,y,z));
250 }
253 {
254 Cube<T> out(*this);
255 out.expandToInclude(r);
256 return out;
257 }
260 {
261 Cube<T> out(*this);
262 out.expandToInclude(v);
263 return out;
264 }
266 Cube<T> expandedToInclude(const T &x,const T &y,const T &z) const
267 {
268 return expandedToInclude(Vector3<T>(x,y,z));
269 }
273 const Cube<T> &expand(const T &tol) { tx1_ -= tol; tx2_ += tol; ty1_ -= tol; ty2_ += tol; tz1_ -= tol; tz2_ += tol; return *this; }
277 Cube<T> expanded(const T &tol) const { Cube<T> out(*this); return out.expand(tol); }
280 Cube<T> biggerBy(const T &fact) const
281 {
282 Vector3<T> s(size()*fact*0.5);
283 Cube<T> out(tx1_-s.x(),tx2_+s.x(),ty1_-s.y(),ty2_+s.y(),tz1_-s.z(),tz2_+s.z());
284 return out;
285 }
287 const Cube<T> &center(const Vector3<T> &v)
288 {
289 Vector3<T> trans = v - this->centroid();
290 tx1_ += trans.x();
291 tx2_ += trans.x();
292 ty1_ += trans.y();
293 ty2_ += trans.y();
294 tz1_ += trans.z();
295 tz2_ += trans.z();
296 return *this;
297 }
299 Cube<T> center(const Vector3<T> &v) const
300 {
301 Cube<T> out(*this);
302 out.center(v);
303 return out;
304 }
305
306private:
307 T pBound(const T &min,const T &v,const T &max) const { return std::min(std::max(v,min),max); }
308 T tx1_;
309 T tx2_;
310 T ty1_;
311 T ty2_;
312 T tz1_;
313 T tz2_;
314};
315
316
317// Predefined types for standard template arguments.
318typedef Cube<double> DCube;
319typedef Cube<float> FCube;
320typedef Cube<int32> ICube;
321typedef Cube<uint32> UCube;
322
324template <class T> inline const Cube<T> &toCube(const Cube<T> &t) { return t; }
326template <class T> inline Cube<T> toCube(const Extent2<T> &t) { return Cube<T>(t.x1(),t.x2(),t.y1(),t.y2(),0.0,0.0); }
327
329// EoF
A Class representing a cartesian extent in 3D.
Definition cube.h:17
void upperBound(const Vector3< T > &v)
Sets the upper bound of the exetnt, keeping the lower bound constant. Synonym to c222().
Definition cube.h:198
Vector3< T > c211() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:86
T & ry1()
Reference access to the minimum y extent.
Definition cube.h:164
bool isEmpty() const
Returns true if the region is "inside out" in any of its three coordinates.
Definition cube.h:106
static Cube< T > nothing()
Returns an Cube with maximum negative size. Useful for calculating bounds.
Definition cube.h:35
Cube< T > expandedToInclude(const Cube< T > &r) const
Returns an extent large enough to contain this one and r.
Definition cube.h:252
Cube(const Extent2< T > &r)
Explicit contructor, from a Rectangle. The z extent is set to (0,1).
Definition cube.h:33
const Cube< T > & operator+=(const Vector3< T > &v)
Adds v to all extent boundaries, effectively translating it.
Definition cube.h:207
const Cube< T > & expand(const T &tol)
Definition cube.h:273
T width() const
Returns the size of the extent in the x-direction.
Definition cube.h:57
Cube< T > expanded(const T &tol) const
Definition cube.h:277
bool tolIsIn(const Vector3< T > &v, const T &tol) const
Returns true if Vector3 v is "inside" the extent, inclusive with an added tolerance factor.
Definition cube.h:133
const T & x2() const
Returns the upper bound of the extent in the x-direction.
Definition cube.h:43
Vector3< T > upperBound() const
Returns the upper bounding corner of the extent, maximum value of x,y, and z.
Definition cube.h:100
const T & dof1(uint32 dof) const
Returns the lower bound of degree-of-freedom dof.
Definition cube.h:53
POPWARNING const T & x1() const
Returns the lower bound of the extent in the x-direction.
Definition cube.h:41
Vector3< T > c112() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:90
bool isIn(const Cube< T > &c) const
Returns true if the Cube c falls completely inside (inclusive) the extent.
Definition cube.h:127
void c222(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:194
Cube< T > center(const Vector3< T > &v) const
Returns a rectangle centered about point v.
Definition cube.h:299
Vector3< T > c222() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:96
Cube(const T &x1, const T &x2, const T &y1, const T &y2, const T &z1, const T &z2)
Explicit constructor, by providing all six cartesian extents.
Definition cube.h:23
Vector3< T > centroid() const
Returns the geometric center of the extent as a Vector3.
Definition cube.h:63
void c112(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:188
const T & z1() const
Returns the lower bound of the extent in the y-direction.
Definition cube.h:49
Vector3< T > c121() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:84
T & rx1()
Reference access to the minimum x extent.
Definition cube.h:162
Cube< T > expandedToInclude(const T &x, const T &y, const T &z) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition cube.h:266
bool operator==(const Cube< T > &c) const
Comparison operator.
Definition cube.h:110
bool operator!=(const Cube< T > &r) const
Comparison operator.
Definition cube.h:115
const Cube< T > & operator-=(const Vector3< T > &v)
Subtracts v from all extent boundaries, effectively translating it.
Definition cube.h:209
Vector3< T > size() const
Returns the size of the extent as a Vector3(width(),height(),depth()).
Definition cube.h:102
const T & y1() const
Returns the lower bound of the extent in the y-direction.
Definition cube.h:45
const T & z2() const
Returns the upper bound of the extent in the y-direction.
Definition cube.h:51
T & rz1()
Reference access to the minimum z extent.
Definition cube.h:166
T & rz2()
Definition cube.h:167
Cube< T > biggerBy(const T &fact) const
Definition cube.h:280
T & ry2()
Reference access to the maximum y extent.
Definition cube.h:165
Cube< T > operator+(const Vector3< T > &v) const
Returns the extent created ty translating this one v units.
Definition cube.h:213
Cube< T > intersectedWith(const Cube< T > &r) const
Returns the extemt formed by the intersections of the two extents. May end up empty.
Definition cube.h:217
T & rx2()
Reference access to the maximum x extent.
Definition cube.h:163
Vector3< T > corner(uint32 x, uint32 y, uint32 z) const
Definition cube.h:70
void c211(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:184
Vector3< T > c111() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:82
void width(const T &t)
Sets the width of the extent by moving the maximum x extent, keeping the minimum constant.
Definition cube.h:174
void c111(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:180
Cube< T > expandedToInclude(const Vector3< T > &v) const
Returns an extent large enough to contain this one and the point v.
Definition cube.h:259
Cube< T > operator-(const Vector3< T > &v) const
Returns the extent created ty translating this one -v units.
Definition cube.h:215
bool tolIsEmpty(const double &tol=limits< double >::epsilon() *100) const
Returns true if the region is "inside out" with a tolerance in any of its three coordinates.
Definition cube.h:108
T depth() const
Returns the size of the extent in the z-direction.
Definition cube.h:61
Vector3< T > lowerBound() const
Returns the lower bounding corner of the extent, minimum value of x,y, and z.
Definition cube.h:98
bool operator>(const Cube< T > &c) const
Comparison operator, based on volume.
Definition cube.h:119
bool intersects(const Cube< T > &c) const
Returns true if the cube C intersects (inclusive) this extent in any way.
Definition cube.h:145
Cube()
Default constructor, no data initialization.
Definition cube.h:21
Vector3< T > c212() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:94
const Cube< T > & center(const Vector3< T > &v)
Centers this rectangle about point v.
Definition cube.h:287
void c221(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:186
void depth(const T &t)
Sets the depth of the extent by moving the maximum z extent, keeping the minimum constant.
Definition cube.h:178
const Cube< T > & expandToInclude(const T &x, const T &y, const T &z)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition cube.h:247
Vector3< T > bound(const Vector3< T > &v) const
Definition cube.h:203
void c212(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:192
bool operator<(const Cube< T > &c) const
Comparison operator, based on volume.
Definition cube.h:117
T diagonal() const
Returns the diagonal length of the extent, or the distance from the lower bound corner to the upper b...
Definition cube.h:67
Vector3< T > c221() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:88
void size(const Vector3< T > &v)
Specifies the size of the extent by moving the upper bound, keeping the lower bound constnat.
Definition cube.h:200
Cube(const Vector3< T > &v111, const Vector3< T > &v222)
Explicit constructor, by providing the lower-bound and upper-bound as two Vector3.
Definition cube.h:26
Vector3< T > c122() const
Returns one of the eith corners that characterize the extent.
Definition cube.h:92
const Cube< T > & expandToInclude(const Cube< T > &r)
Enlarges the extent to include both its original extent and r.
Definition cube.h:225
void lowerBound(const Vector3< T > &v)
Sets the lower bound of the exetnt, keeping the upper bound constant. Synonym to c111().
Definition cube.h:196
bool isIn(const Vector3< T > &v) const
Returns true if Vector3 v is "inside" the extent, inclusive.
Definition cube.h:121
const T & dof2(uint32 dof) const
Returns the upper bound of degree-of-freedom dof.
Definition cube.h:55
bool tolIntersects(const Cube< T > &c, const T &tol) const
Returns true if the cube C intersects (inclusive) this extent in any way with an added tolerance fact...
Definition cube.h:153
bool tolIsIn(const Cube< T > &c, const T &tol) const
Returns true if the Cube c falls completely inside (inclusive) the extent with an added tolerance fac...
Definition cube.h:139
T height() const
Returns the size of the extent in the y-direction.
Definition cube.h:59
void c122(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:190
T & rdof1(uint32 dof)
Reference access to lower bound of degree-of-freedom dof.
Definition cube.h:169
T volume() const
Returns the volume of the extent ( width() * height() * depth() ).
Definition cube.h:65
T & rdof2(uint32 dof)
Reference access to lower bound of degree-of-freedom dof.
Definition cube.h:171
const Cube< T > & expandToInclude(const Vector3< T > &v)
Enlarges the extent to contain v.
Definition cube.h:236
void height(const T &t)
Sets the height of the extent by moving the maximum y extent, keeping the minimum constant.
Definition cube.h:176
void c121(const Vector3< T > &v)
Sets one of the eight characteristic corners of the extent, keeping the other three extents constant.
Definition cube.h:182
const T & y2() const
Returns the upper bound of the extent in the y-direction.
Definition cube.h:47
PUSHWARNING Cube(const Cube< T > &r)
Copy constructor.
Definition cube.h:31
2D cartesian region in space.
Definition extent2.h:12
const T & y1() const
Returns the lower y-bound.
Definition extent2.h:37
const T & y2() const
Returns the upper y-bound.
Definition extent2.h:39
const T & x2() const
Returns the upper x-bound.
Definition extent2.h:35
const T & x1() const
Returns the lower x-bound.
Definition extent2.h:33
3D vector utility class.
Definition vect.h:163
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:186
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:184
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:188
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
const Cube< T > & toCube(const Cube< T > &t)
Returns a cube.
Definition cube.h:324