Itasca C++ Interface
extent2.h
1 #pragma once
8 #include "vect.h"
9 
12 template <class T> class Extent2 {
13 public:
14  // Creators
16 #ifdef _DEBUG
17  Extent2() { tx1_ = tx2_ = ty1_ = ty2_ = initVal<T>(); }
18 #else
19  Extent2() {}
20 #endif
22  Extent2(const T &x1,const T &x2,const T &y1,const T &y2) : tx1_(x1), tx2_(x2), ty1_(y1), ty2_(y2) { }
24  Extent2(const Vector2<T> &v11,const Vector2<T> &v22) : tx1_(v11.x()), tx2_(v22.x()), ty1_(v11.y()), ty2_(v22.y()) { }
26  Extent2(const Extent2<T> &r) : tx1_(r.tx1_), tx2_(r.tx2_), ty1_(r.ty1_), ty2_(r.ty2_) { }
27  constexpr const Extent2 &operator=(const Extent2 &e) { tx1_=e.tx1_; tx2_=e.tx2_; ty1_=e.ty1_; ty2_=e.ty2_; return *this; }
30  limits<T>::max(),-limits<T>::max()); return r; }
31  // Accessors
33  const T &x1() const { return tx1_; }
35  const T &x2() const { return tx2_; }
37  const T &y1() const { return ty1_; }
39  const T &y2() const { return ty2_; }
41  const T &dof1(uint32 u) const {
42  assert(u<2);
43  if (u)
44  return ty1_;
45  return tx1_;
46  }
48  const T &dof2(uint32 u) const {
49  assert(u<2);
50  if (u)
51  return ty2_;
52  return tx2_;
53  }
55  T width() const { return (tx2_-tx1_); }
57  T height() const { return (ty2_-ty1_); }
59  Vector2<T> centroid() const { Vector2<T> out((tx1_+tx2_)/2,(ty1_+ty2_)/2); return out; }
61  Vector2<T> c11() const { Vector2<T> out(tx1_,ty1_); return out; }
63  Vector2<T> c12() const { Vector2<T> out(tx1_,ty2_); return out; }
65  Vector2<T> c21() const { Vector2<T> out(tx2_,ty1_); return out; }
67  Vector2<T> c22() const { Vector2<T> out(tx2_,ty2_); return out; }
69  Vector2<T> lowerBound() const { return c11(); }
71  Vector2<T> upperBound() const { return c22(); }
73  Vector2<T> size() const { Vector2<T> out(width(),height()); return out; }
75  T area() const { return (width()*height()); }
77  T volume() const { return area(); }
79  T diagonal() const { return size().mag(); }
81  bool isEmpty() const { return( (tx1_>=tx2_) || (ty1_>=ty2_) ); }
83  bool tolIsEmpty(const double &tol = limits<double>::epsilon() * 100) const { return ( (tx1_ + tol >= tx2_) || (ty1_ + tol >= ty2_) ); }
84 
85  // Comparison operators
87  bool operator==(const Extent2<T> &r) const { return ( (tx1_==r.tx1_)&&(tx2_==r.tx2_)&&(ty1_==r.ty1_)&&(ty2_==r.ty2_) ); }
89  bool operator!=(const Extent2<T> &r) const { return !operator==(r); }
91  bool operator<(const Extent2<T> &r) const { return (area() < r.area()); }
93  bool operator>(const Extent2<T> &r) const { return (area() > r.area()); }
95  bool isIn(const Vector2<T> &v) const { return ((v.x()>=tx1_)&&(v.x()<=tx2_)&&(v.y()>=ty1_)&&(v.y()<=ty2_)); }
97  bool isIn(const Extent2<T> &r) const {
98  if ( (r.tx1_>=tx1_) && (r.tx2_<=tx2_) && (r.ty1_>=ty1_) && (r.ty2_<=ty2_) ) return true;
99  return false;
100  }
102  bool tolIsIn(const Vector2<T> &v,const T &tol) const {
103  return ((v.x()>=tx1_-tol)&&(v.x()<=tx2_+tol)&&(v.y()>=ty1_-tol)&&(v.y()<=ty2_+tol));
104  }
106  bool tolIsIn(const Extent2<T> &r,const T &tol) const {
107  if ( (r.tx1_>=tx1_-tol) && (r.tx2_<=tx2_+tol) && (r.ty1_>=ty1_-tol) && (r.ty2_<=ty2_+tol) ) return true;
108  return false;
109  }
111  bool intersects(const Extent2<T> &r) const {
112  if ((r.tx2_<tx1_) || (r.tx1_>tx2_) ||
113  (r.ty2_<ty1_) || (r.ty1_>ty2_)) return false;
114  return true;
115  }
117  bool tolIntersects(const Extent2<T> &r,const T &tol) const {
118  if ((r.tx2_<tx1_-tol) || (r.tx1_>tx2_+tol) ||
119  (r.ty2_<ty1_-tol) || (r.ty1_>ty2_+tol)) return false;
120  return true;
121  }
122 
123  // Setters
125  T &rx1() { return tx1_; }
127  T &rx2() { return tx2_; }
129  T &ry1() { return ty1_; }
131  T &ry2() { return ty2_; }
133  T &rdof1(uint32 u) {
134  assert(u<2);
135  if (u)
136  return ty1_;
137  return tx1_;
138  }
140  T &rdof2(uint32 u) {
141  assert(u<2);
142  if (u)
143  return ty2_;
144  return tx2_;
145  }
146  // Changing width or height assumes LL corner (c11) stays constant.
148  void width(const T &t) { tx2_ = tx1_ + t; }
150  void height(const T &t) { ty2_ = ty1_ + t; }
152  void c11(const Vector2<T> &v) { rx1() = v.x(); ry1() = v.y(); }
154  void c12(const Vector2<T> &v) { rx1() = v.x(); ry2() = v.y(); }
156  void c21(const Vector2<T> &v) { rx2() = v.x(); ry1() = v.y(); }
158  void c22(const Vector2<T> &v) { rx2() = v.x(); ry2() = v.y(); }
160  void lowerBound(const Vector2<T> &v) { c11(v); }
162  void upperBound(const Vector2<T> &v) { c22(v); }
164  void size(const Vector2<T> &v) { width(v.x()); height(v.y()); }
166  Vector2<T> bound(const Vector2<T> &v) const { return Vector2<T>(pbound(tx1_,v.x(),tx2_),pbound(ty1_,v.y(),ty2_)); }
167 
168  // Manipulators - unary in place
170  const Extent2<T> &operator+=(const Vector2<T> &v) { tx1_+=v.x(); tx2_+=v.x(); ty1_+=v.y(); ty2_+=v.y(); return *this; }
172  const Extent2<T> &operator-=(const Vector2<T> &v) { tx1_-=v.x(); tx2_-=v.x(); ty1_-=v.y(); ty2_-=v.y(); return *this; }
173 
174  // Binary operators
176  Extent2<T> operator+(const Vector2<T> &v) const { Extent2<T> out(tx1_+v.x(),tx2_+v.x(),ty1_+v.y(),ty2_+v.y()); return out; }
178  Extent2<T> operator-(const Vector2<T> &v) const { Extent2<T> out(tx1_-v.x(),tx2_-v.x(),ty1_-v.y(),ty2_-v.y()); return out; }
182  Extent2<T> out(std::max<T>(tx1_,r.tx1_),std::min<T>(tx2_,r.tx2_),std::max<T>(ty1_,r.ty1_),std::min<T>(ty2_,r.ty2_));
183  return out;
184  }
187  tx1_ = std::min<T>(tx1_,r.tx1_);
188  tx2_ = std::max<T>(tx2_,r.tx2_);
189  ty1_ = std::min<T>(ty1_,r.ty1_);
190  ty2_ = std::max<T>(ty2_,r.ty2_);
191  return *this;
192  }
194  const Extent2<T> &expandToInclude(const Vector2<T> &v) {
195  tx1_ = std::min<T>(tx1_,v.x());
196  tx2_ = std::max<T>(tx2_,v.x());
197  ty1_ = std::min<T>(ty1_,v.y());
198  ty2_ = std::max<T>(ty2_,v.y());
199  return *this;
200  }
203  Extent2<T> out(*this);
204  out.expandToInclude(r);
205  return out;
206  }
208  Extent2<T> expandedToInclude(const Vector2<T> &v) const {
209  Extent2<T> out(*this);
210  out.expandToInclude(v);
211  return out;
212  }
215  const Extent2<T> &expand(const T &tol) { tx1_ -= tol; tx2_ += tol; ty1_ -= tol; ty2_ += tol; return *this; }
218  Extent2<T> expanded(const T &tol) const { Extent2<T> out(*this); return out.expand(tol); }
220  const Extent2<T> &center(const Vector2<T> &v) {
221  Vector2<T> trans = v - this->centroid();
222  tx1_ += trans.x();
223  tx2_ += trans.x();
224  ty1_ += trans.y();
225  ty2_ += trans.y();
226  return *this;
227  }
229  Extent2<T> center(const Vector2<T> &v) const {
230  Extent2<T> out(*this);
231  out.center(v);
232  return out;
233  }
236  Extent2<T> biggerBy(const T &fact) const {
237  auto s = size()*0.5;
238  s += s*fact;
239  auto c = centroid();
240  Extent2<T> out(c-s,c+s);
241  return out;
242  }
243 
244 private:
245  T pbound(const T &min,const T &v,const T &max) const { return std::min(std::max(v,min),max); }
246  T tx1_;
247  T tx2_;
248  T ty1_;
249  T ty2_;
250 };
251 
252 typedef Extent2<double> DExtent2;
253 typedef Extent2<float> FExtent2;
254 typedef Extent2<int32> IExtent2;
255 typedef Extent2<uint32> UExtent2;
256 typedef Extent2<int64> I64Extent2;
257 typedef Extent2<uint64> U64Extent2;
258 
260 // EoF
2D cartesian region in space.
Definition: extent2.h:12
Extent2< T > biggerBy(const T &fact) const
Definition: extent2.h:236
T volume() const
Returns the volume of the extent assuming unit depth ( width() * height()).
Definition: extent2.h:77
bool intersects(const Extent2< T > &r) const
Returns true if Extent2 r intersects (inclusive) the Extent2.
Definition: extent2.h:111
const T & y1() const
Returns the lower y-bound.
Definition: extent2.h:37
bool operator!=(const Extent2< T > &r) const
Comparison operator, no tolerance applied.
Definition: extent2.h:89
T & rdof1(uint32 u)
Reference access to lower bound of degree-of-freedom dof.
Definition: extent2.h:133
Vector2< T > upperBound() const
Returns the upper bound of the Extent2 (maximum x and y corner).
Definition: extent2.h:71
Vector2< T > bound(const Vector2< T > &v) const
Forces the point v to fall within the Extent2, by clamping the x and y values to fall within it's ext...
Definition: extent2.h:166
Extent2< T > center(const Vector2< T > &v) const
Returns a Extent2 centered about point v.
Definition: extent2.h:229
static constexpr Extent2< T > nothing()
Static function returning object with maximum negative size, useful for calculating bounds.
Definition: extent2.h:29
const T & dof1(uint32 u) const
Returns the lower bound of degree-of-freedom dof.
Definition: extent2.h:41
Vector2< T > c22() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:67
Vector2< T > c12() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:63
const T & dof2(uint32 u) const
Returns the upper bound of degree-of-freedom dof.
Definition: extent2.h:48
T diagonal() const
Returns the length of the diagonal from the lower bound to the upper bound.
Definition: extent2.h:79
Extent2< T > expandedToInclude(const Extent2< T > &r) const
Returns a Extent2 expanded to completely include r.
Definition: extent2.h:202
const Extent2< T > & operator-=(const Vector2< T > &v)
In place subtraction operator, offsets the exent by -v.
Definition: extent2.h:172
void lowerBound(const Vector2< T > &v)
Sets the lower bound of the Extent2, leaving the upper bound alone.
Definition: extent2.h:160
Extent2(const Vector2< T > &v11, const Vector2< T > &v22)
Explicit constructor, given the lower and upper bounds as two Vector2.
Definition: extent2.h:24
const Extent2< T > & expandToInclude(const Extent2< T > &r)
Expands the extent of this Extent2 as necessary to completely include r.
Definition: extent2.h:186
Vector2< T > size() const
Returns the size of the Extent2, the width() and height() encoded as a Vector2.
Definition: extent2.h:73
Vector2< T > lowerBound() const
Returns the lower bound of the Extent2 (minimum x and y corner).
Definition: extent2.h:69
T & rx1()
Access reference to the minimum x extent.
Definition: extent2.h:125
void upperBound(const Vector2< T > &v)
Sets the upper bound of the Extent2, leaving the lower bound alone.
Definition: extent2.h:162
Extent2< T > intersectedWith(const Extent2< T > &r) const
Definition: extent2.h:181
T & ry2()
Access reference to the maximum y extent.
Definition: extent2.h:131
bool tolIntersects(const Extent2< T > &r, const T &tol) const
Returns true if Extent2 r intersects (inclusive) the Extent2 with an added tolerance factor.
Definition: extent2.h:117
void c11(const Vector2< T > &v)
Sets one of the four characteristic corners of the extent, leaving the other two values alone.
Definition: extent2.h:152
const Extent2< T > & expandToInclude(const Vector2< T > &v)
Expands the extent of this Extent2 as necessary to completely include point v.
Definition: extent2.h:194
const T & x1() const
Returns the lower x-bound.
Definition: extent2.h:33
Extent2(const Extent2< T > &r)
Copy constructor.
Definition: extent2.h:26
const Extent2< T > & expand(const T &tol)
Definition: extent2.h:215
T width() const
Returns the size of the x-extent (x2-x1)
Definition: extent2.h:55
const T & y2() const
Returns the upper y-bound.
Definition: extent2.h:39
bool isIn(const Extent2< T > &r) const
Returns true Extent2 v is inside (inclusive) the Extent2.
Definition: extent2.h:97
bool tolIsIn(const Vector2< T > &v, const T &tol) const
Returns true if point v is inside (inclusive) the Extent2 with an added tolerance factor.
Definition: extent2.h:102
T & rx2()
Access reference to the maximum x extent.
Definition: extent2.h:127
bool operator<(const Extent2< T > &r) const
Comparison operator, using area() as a metric.
Definition: extent2.h:91
Vector2< T > c11() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:61
bool tolIsEmpty(const double &tol=limits< double >::epsilon() *100) const
Returns true if the area of the Extent2 is <= 0 with a tolerance.
Definition: extent2.h:83
void size(const Vector2< T > &v)
Sets the size of the Extent2 by moving the maximum x and y extents, leaving the minimum alone.
Definition: extent2.h:164
void height(const T &t)
Sets the height of the Extent2 by moving the maximum y extent, leaving the minimum alone.
Definition: extent2.h:150
const Extent2< T > & center(const Vector2< T > &v)
Centers this Extent2 about point v.
Definition: extent2.h:220
Extent2< T > expandedToInclude(const Vector2< T > &v) const
Returns a Extent2 expanded to include point v..
Definition: extent2.h:208
bool operator==(const Extent2< T > &r) const
Comparison operator, no tolerance applied.
Definition: extent2.h:87
Vector2< T > centroid() const
Returns the centroid of the Extent2 as a Vector2.
Definition: extent2.h:59
void c22(const Vector2< T > &v)
Sets one of the four characteristic corners of the extent, leaving the other two values alone.
Definition: extent2.h:158
bool isIn(const Vector2< T > &v) const
Returns true if point v is inside (inclusive) the Extent2.
Definition: extent2.h:95
Vector2< T > c21() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:65
Extent2(const T &x1, const T &x2, const T &y1, const T &y2)
Explicit constructor, given the x and y extents.
Definition: extent2.h:22
const Extent2< T > & operator+=(const Vector2< T > &v)
In place addition operator, offsets the exent by v.
Definition: extent2.h:170
Extent2()
Default constructor, no data initialization.
Definition: extent2.h:19
bool tolIsIn(const Extent2< T > &r, const T &tol) const
Returns true Extent2 v is inside (inclusive) the Extent2 with an added tolerance factor.
Definition: extent2.h:106
T & ry1()
Access reference to the minimum y extent.
Definition: extent2.h:129
void c21(const Vector2< T > &v)
Sets one of the four characteristic corners of the extent, leaving the other two values alone.
Definition: extent2.h:156
void width(const T &t)
Sets the width of the Extent2 by moving the maximum x extent, leaving the minimum alone.
Definition: extent2.h:148
T height() const
Return sthe size of the y-extent (y2-y1)
Definition: extent2.h:57
void c12(const Vector2< T > &v)
Sets one of the four characteristic corners of the extent, leaving the other two values alone.
Definition: extent2.h:154
T area() const
Returns the width()*height() - could be negative.
Definition: extent2.h:75
Extent2< T > operator-(const Vector2< T > &v) const
Binary subtraction operator, returns a Extent2 offset by -v.
Definition: extent2.h:178
bool isEmpty() const
Returns true if the area of the Extent2 is <= 0.
Definition: extent2.h:81
T & rdof2(uint32 u)
Reference access to lower bound of degree-of-freedom dof.
Definition: extent2.h:140
bool operator>(const Extent2< T > &r) const
Comparison operator, using area() as a metric.
Definition: extent2.h:93
Extent2< T > expanded(const T &tol) const
Definition: extent2.h:218
Extent2< T > operator+(const Vector2< T > &v) const
Binary addition operator, returns a Extent2 offset by v.
Definition: extent2.h:176
const T & x2() const
Returns the upper x-bound.
Definition: extent2.h:35
debug checked shorthand for std::numeric_limits<T>::
Definition: limit.h:25
2D and 3D vector utility classes.