Itasca C++ Interface
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
extent2.h
1 #pragma once
2 
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
21  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_) { }
29  limits<T>::max(),-limits<T>::max()); return r; }
30  // Accessors
32  const T &x1() const { return tx1_; }
34  const T &x2() const { return tx2_; }
36  const T &y1() const { return ty1_; }
38  const T &y2() const { return ty2_; }
40  const T &dof1(UInt u) const {
41  assert(u<2);
42  if (u)
43  return ty1_;
44  return tx1_;
45  }
47  const T &dof2(UInt u) const {
48  assert(u<2);
49  if (u)
50  return ty2_;
51  return tx2_;
52  }
54  T width() const { return (tx2_-tx1_); }
56  T height() const { return (ty2_-ty1_); }
58  Vector2<T> centroid() const { Vector2<T> out((tx1_+tx2_)/2,(ty1_+ty2_)/2); return out; }
60  Vector2<T> c11() const { Vector2<T> out(tx1_,ty1_); return out; }
62  Vector2<T> c12() const { Vector2<T> out(tx1_,ty2_); return out; }
64  Vector2<T> c21() const { Vector2<T> out(tx2_,ty1_); return out; }
66  Vector2<T> c22() const { Vector2<T> out(tx2_,ty2_); return out; }
68  Vector2<T> lowerBound() const { return c11(); }
70  Vector2<T> upperBound() const { return c22(); }
72  Vector2<T> size() const { Vector2<T> out(width(),height()); return out; }
74  T area() const { return (width()*height()); }
76  T volume() const { return area(); }
78  T diagonal() const { return size().mag(); }
80  bool isEmpty() const { return( (tx1_>=tx2_) || (ty1_>=ty2_) ); }
82  bool tolIsEmpty(const double &tol = limits<double>::epsilon() * 100) const { return ( (tx1_ + tol >= tx2_) || (ty1_ + tol >= ty2_) ); }
83 
84  // Comparison operators
86  bool operator==(const Extent2<T> &r) const { return ( (tx1_==r.tx1_)&&(tx2_==r.tx2_)&&(ty1_==r.ty1_)&&(ty2_==r.ty2_) ); }
88  bool operator!=(const Extent2<T> &r) const { return !operator==(r); }
90  bool operator<(const Extent2<T> &r) const { return (area() < r.area()); }
92  bool operator>(const Extent2<T> &r) const { return (area() > r.area()); }
94  bool isIn(const Vector2<T> &v) const { return ((v.x()>=tx1_)&&(v.x()<=tx2_)&&(v.y()>=ty1_)&&(v.y()<=ty2_)); }
96  bool isIn(const Extent2<T> &r) const {
97  if ( (r.tx1_>=tx1_) && (r.tx2_<=tx2_) && (r.ty1_>=ty1_) && (r.ty2_<=ty2_) ) return true;
98  return false;
99  }
101  bool tolIsIn(const Vector2<T> &v,const T &tol) const {
102  return ((v.x()>=tx1_-tol)&&(v.x()<=tx2_+tol)&&(v.y()>=ty1_-tol)&&(v.y()<=ty2_+tol));
103  }
105  bool tolIsIn(const Extent2<T> &r,const T &tol) const {
106  if ( (r.tx1_>=tx1_-tol) && (r.tx2_<=tx2_+tol) && (r.ty1_>=ty1_-tol) && (r.ty2_<=ty2_+tol) ) return true;
107  return false;
108  }
110  bool intersects(const Extent2<T> &r) const {
111  if ((r.tx2_<tx1_) || (r.tx1_>tx2_) ||
112  (r.ty2_<ty1_) || (r.ty1_>ty2_)) return false;
113  return true;
114  }
116  bool tolIntersects(const Extent2<T> &r,const T &tol) const {
117  if ((r.tx2_<tx1_-tol) || (r.tx1_>tx2_+tol) ||
118  (r.ty2_<ty1_-tol) || (r.ty1_>ty2_+tol)) return false;
119  return true;
120  }
121 
122  // Setters
124  T &rx1() { return tx1_; }
126  T &rx2() { return tx2_; }
128  T &ry1() { return ty1_; }
130  T &ry2() { return ty2_; }
132  T &rdof1(UInt u) {
133  assert(u<2);
134  if (u)
135  return ty1_;
136  return tx1_;
137  }
139  T &rdof2(UInt u) {
140  assert(u<2);
141  if (u)
142  return ty2_;
143  return tx2_;
144  }
145  // Changing width or height assumes LL corner (c11) stays constant.
147  void width(const T &t) { tx2_ = tx1_ + t; }
149  void height(const T &t) { ty2_ = ty1_ + t; }
151  void c11(const Vector2<T> &v) { rx1() = v.x(); ry1() = v.y(); }
153  void c12(const Vector2<T> &v) { rx1() = v.x(); ry2() = v.y(); }
155  void c21(const Vector2<T> &v) { rx2() = v.x(); ry1() = v.y(); }
157  void c22(const Vector2<T> &v) { rx2() = v.x(); ry2() = v.y(); }
159  void lowerBound(const Vector2<T> &v) { c11(v); }
161  void upperBound(const Vector2<T> &v) { c22(v); }
163  void size(const Vector2<T> &v) { width(v.x()); height(v.y()); }
165  Vector2<T> bound(const Vector2<T> &v) const { return Vector2<T>(pbound(tx1_,v.x(),tx2_),pbound(ty1_,v.y(),ty2_)); }
166 
167  // Manipulators - unary in place
169  const Extent2<T> &operator+=(const Vector2<T> &v) { tx1_+=v.x(); tx2_+=v.x(); ty1_+=v.y(); ty2_+=v.y(); return *this; }
171  const Extent2<T> &operator-=(const Vector2<T> &v) { tx1_-=v.x(); tx2_-=v.x(); ty1_-=v.y(); ty2_-=v.y(); return *this; }
172 
173  // Binary operators
175  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; }
177  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; }
181  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_));
182  return out;
183  }
186  tx1_ = std::min<T>(tx1_,r.tx1_);
187  tx2_ = std::max<T>(tx2_,r.tx2_);
188  ty1_ = std::min<T>(ty1_,r.ty1_);
189  ty2_ = std::max<T>(ty2_,r.ty2_);
190  return *this;
191  }
194  tx1_ = std::min<T>(tx1_,v.x());
195  tx2_ = std::max<T>(tx2_,v.x());
196  ty1_ = std::min<T>(ty1_,v.y());
197  ty2_ = std::max<T>(ty2_,v.y());
198  return *this;
199  }
202  Extent2<T> out(*this);
203  out.expandToInclude(r);
204  return out;
205  }
208  Extent2<T> out(*this);
209  out.expandToInclude(v);
210  return out;
211  }
214  const Extent2<T> &expand(const T &tol) { tx1_ -= tol; tx2_ += tol; ty1_ -= tol; ty2_ += tol; return *this; }
217  Extent2<T> expanded(const T &tol) const { Extent2<T> out(*this); return out.expand(tol); }
219  const Extent2<T> &center(const Vector2<T> &v) {
220  Vector2<T> trans = v - this->centroid();
221  tx1_ += trans.x();
222  tx2_ += trans.x();
223  ty1_ += trans.y();
224  ty2_ += trans.y();
225  return *this;
226  }
228  Extent2<T> center(const Vector2<T> &v) const {
229  Extent2<T> out(*this);
230  out.center(v);
231  return out;
232  }
235  Extent2<T> biggerBy(const T &fact) const {
236  auto s = size()*0.5;
237  s += s*fact;
238  auto c = centroid();
239  Extent2<T> out(c-s,c+s);
240  return out;
241  }
242 
243 private:
244  T pbound(const T &min,const T &v,const T &max) const { return std::min(std::max(v,min),max); }
245  T tx1_;
246  T tx2_;
247  T ty1_;
248  T ty2_;
249 };
250 
255 
257 // EoF
Vector2< T > upperBound() const
Returns the upper bound of the Extent2 (maximum x and y corner).
Definition: extent2.h:70
const Extent2< T > & expandToInclude(const Vector2< T > &v)
Expands the extent of this Extent2 as necessary to completely include point v.
Definition: extent2.h:193
const Extent2< T > & expandToInclude(const Extent2< T > &r)
Expands the extent of this Extent2 as necessary to completely include r.
Definition: extent2.h:185
const Extent2< T > & operator+=(const Vector2< T > &v)
In place addition operator, offsets the exent by v.
Definition: extent2.h:169
const Extent2< T > & expand(const T &tol)
Definition: extent2.h:214
static constexpr Extent2< T > nothing()
Static function returning object with maximum negative size, useful for calculating bounds.
Definition: extent2.h:28
const T & y1() const
Returns the lower y-bound.
Definition: extent2.h:36
bool operator!=(const Extent2< T > &r) const
Comparison operator, no tolerance applied.
Definition: extent2.h:88
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:165
Extent2< T > center(const Vector2< T > &v) const
Returns a Extent2 centered about point v.
Definition: extent2.h:228
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:105
const Extent2< T > & center(const Vector2< T > &v)
Centers this Extent2 about point v.
Definition: extent2.h:219
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:163
T & rx2()
Access reference to the maximum x extent.
Definition: extent2.h:126
T diagonal() const
Returns the length of the diagonal from the lower bound to the upper bound.
Definition: extent2.h:78
T & rdof1(UInt u)
Reference access to lower bound of degree-of-freedom dof.
Definition: extent2.h:132
Extent2< T > expandedToInclude(const Extent2< T > &r) const
Returns a Extent2 expanded to completely include r.
Definition: extent2.h:201
2D and 3D vector utility classes.
const T & dof2(UInt u) const
Returns the upper bound of degree-of-freedom dof.
Definition: extent2.h:47
bool operator<(const Extent2< T > &r) const
Comparison operator, using area() as a metric.
Definition: extent2.h:90
Extent2()
Default constructor, no data initialization.
Definition: extent2.h:19
bool isIn(const Vector2< T > &v) const
Returns true if point v is inside (inclusive) the Extent2.
Definition: extent2.h:94
T width() const
Returns the size of the x-extent (x2-x1)
Definition: extent2.h:54
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:101
Vector2< T > lowerBound() const
Returns the lower bound of the Extent2 (minimum x and y corner).
Definition: extent2.h:68
T & ry1()
Access reference to the minimum y extent.
Definition: extent2.h:128
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:155
Vector2< T > c11() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:60
Extent2(const Extent2< T > &r)
Copy constructor.
Definition: extent2.h:26
T & ry2()
Access reference to the maximum y extent.
Definition: extent2.h:130
debug checked shorthand for std::numeric_limits<T>::
Definition: limit.h:25
unsigned int UInt
unsigned 32 bit
Definition: basedef.h:31
Extent2< T > biggerBy(const T &fact) const
Definition: extent2.h:235
Extent2< T > expanded(const T &tol) const
Definition: extent2.h:217
Vector2< T > c21() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:64
Extent2< Double > DExtent2
Definition: extent2.h:251
Extent2(const Vector2< T > &v11, const Vector2< T > &v22)
Explicit constructor, given the lower and upper bounds as two Vector2.
Definition: extent2.h:24
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:157
const T & dof1(UInt u) const
Returns the lower bound of degree-of-freedom dof.
Definition: extent2.h:40
const T & y() const
Y component access.
Definition: vect.h:56
bool operator==(const Extent2< T > &r) const
Comparison operator, no tolerance applied.
Definition: extent2.h:86
void height(const T &t)
Sets the height of the Extent2 by moving the maximum y extent, leaving the minimum alone.
Definition: extent2.h:149
Extent2< T > operator+(const Vector2< T > &v) const
Binary addition operator, returns a Extent2 offset by v.
Definition: extent2.h:175
2D vector utility class.
Definition: vect.h:31
Vector2< T > c22() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:66
bool intersects(const Extent2< T > &r) const
Returns true if Extent2 r intersects (inclusive) the Extent2.
Definition: extent2.h:110
Extent2< UInt > UExtent2
Definition: extent2.h:254
T & rx1()
Access reference to the minimum x extent.
Definition: extent2.h:124
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:153
Vector2< T > c12() const
Returns one of the four characteristic corners of the 2D extent.
Definition: extent2.h:62
const Extent2< T > & operator-=(const Vector2< T > &v)
In place subtraction operator, offsets the exent by -v.
Definition: extent2.h:171
bool operator>(const Extent2< T > &r) const
Comparison operator, using area() as a metric.
Definition: extent2.h:92
const T & y2() const
Returns the upper y-bound.
Definition: extent2.h:38
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:116
void upperBound(const Vector2< T > &v)
Sets the upper bound of the Extent2, leaving the lower bound alone.
Definition: extent2.h:161
bool isEmpty() const
Returns true if the area of the Extent2 is <= 0.
Definition: extent2.h:80
bool isIn(const Extent2< T > &r) const
Returns true Extent2 v is inside (inclusive) the Extent2.
Definition: extent2.h:96
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:151
2D cartesian region in space.
Definition: extent2.h:12
Extent2< T > intersectedWith(const Extent2< T > &r) const
Definition: extent2.h:180
const T & x() const
X component access.
Definition: vect.h:54
T area() const
Returns the width()*height() - could be negative.
Definition: extent2.h:74
Extent2< T > operator-(const Vector2< T > &v) const
Binary subtraction operator, returns a Extent2 offset by -v.
Definition: extent2.h:177
Vector2< T > centroid() const
Returns the centroid of the Extent2 as a Vector2.
Definition: extent2.h:58
T & rdof2(UInt u)
Reference access to lower bound of degree-of-freedom dof.
Definition: extent2.h:139
T volume() const
Returns the volume of the extent assuming unit depth ( width() * height()).
Definition: extent2.h:76
const T & x2() const
Returns the upper x-bound.
Definition: extent2.h:34
Extent2< Float > FExtent2
Definition: extent2.h:252
void width(const T &t)
Sets the width of the Extent2 by moving the maximum x extent, leaving the minimum alone.
Definition: extent2.h:147
Vector2< T > size() const
Returns the size of the Extent2, the width() and height() encoded as a Vector2.
Definition: extent2.h:72
void lowerBound(const Vector2< T > &v)
Sets the lower bound of the Extent2, leaving the upper bound alone.
Definition: extent2.h:159
T height() const
Return sthe size of the y-extent (y2-y1)
Definition: extent2.h:56
Extent2< Int > IExtent2
Definition: extent2.h:253
const T & x1() const
Returns the lower x-bound.
Definition: extent2.h:32
Extent2< T > expandedToInclude(const Vector2< T > &v) const
Returns a Extent2 expanded to include point v..
Definition: extent2.h:207
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:82