00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef VEE_BOUNDING_SQUARE_H
00021 #define VEE_BOUNDING_SQUARE_H
00022
00023 #include <vee_vector2.h>
00024 #include <vee_matrix4.h>
00025
00026 class VEE_BoundingSquare
00027 {
00028 public:
00029 VEE_BoundingSquare() {}
00030 VEE_BoundingSquare(const VEE_Vector2 * lowHigh)
00031 : m_low(*lowHigh), m_high(*lowHigh) {}
00032 VEE_BoundingSquare(const VEE_Vector2 & low, const VEE_Vector2 & high)
00033 : m_low(low), m_high(high) {}
00034 ~VEE_BoundingSquare() {}
00035
00036 void scale(float v) { m_low = m_low * v; m_high = m_high * v; }
00037 inline void scale(const VEE_Vector2 &v);
00038 void move(const VEE_Vector2 &v) { m_low += v; m_high += v; }
00039
00040 void clear() { m_low = m_high = VEE_Vector2ZERO; }
00041 void clear(const VEE_Vector2 &v) { m_low = m_high = v; }
00042
00043 inline void expand(const VEE_Vector2 &v);
00044 inline void expand(const VEE_Vector2 &v, float radius);
00045 inline void expand(const VEE_BoundingSquare &b);
00046
00047 VEE_Vector2 & low() { return m_low; }
00048 const VEE_Vector2 & low() const { return m_low; }
00049 VEE_Vector2 & high() { return m_high; }
00050 const VEE_Vector2 & high() const { return m_high; }
00051
00052 void set(float lx, float ly, float hx, float hy)
00053 { m_low.make(lx, ly); m_high.make(hx, hy); }
00054
00055 void set(const VEE_Vector2 &low, const VEE_Vector2 &high)
00056 { m_low = low; m_high = high; }
00057
00058 void set(const VEE_Vector2 &point)
00059 { m_low = m_high = point; }
00060
00061 void set(const VEE_Vector2 &point, float radius)
00062 { m_low = point - radius; m_high = point + radius; }
00063
00064 inline VEE_Vector2 center() const { return (m_low + m_high) * 0.5; }
00065 inline VEE_Vector2 span() const { return m_high - m_low; }
00066 inline VEE_Vector2 topCenter() const;
00067
00068 inline bool intersects(const VEE_BoundingSquare &) const;
00069 inline bool contains(const VEE_Vector2 &) const;
00070 inline bool contains(const VEE_BoundingSquare &b) const;
00071
00072 inline VEE_Vector2 clamp(const VEE_Vector2 &) const;
00073
00074 protected:
00075 VEE_Vector2 m_low, m_high;
00076 };
00077
00078 inline void VEE_BoundingSquare::expand(const VEE_Vector2 &v)
00079 {
00080 if(v[0] < m_low[0]) m_low[0] = v[0];
00081 if(v[1] < m_low[1]) m_low[1] = v[1];
00082
00083 if(v[0] > m_high[0]) m_high[0] = v[0];
00084 if(v[1] > m_high[1]) m_high[1] = v[1];
00085 }
00086
00087 inline void VEE_BoundingSquare::expand(const VEE_Vector2 &v, float radius)
00088 {
00089 expand(v - VEE_Vector2(radius, radius));
00090 expand(v + VEE_Vector2(radius, radius));
00091 }
00092
00093 inline void VEE_BoundingSquare::expand(const VEE_BoundingSquare &b)
00094 {
00095 if(b.m_low[0] < m_low[0]) m_low[0] = b.m_low[0];
00096 if(b.m_low[1] < m_low[1]) m_low[1] = b.m_low[1];
00097
00098 if(b.m_high[0] > m_high[0]) m_high[0] = b.m_high[0];
00099 if(b.m_high[1] > m_high[1]) m_high[1] = b.m_high[1];
00100 }
00101
00102 void VEE_BoundingSquare::scale(const VEE_Vector2 &v)
00103 {
00104 m_low[0] *= v[0];
00105 m_low[1] *= v[1];
00106
00107 m_high[0] *= v[0];
00108 m_high[1] *= v[1];
00109 }
00110
00111 VEE_Vector2 VEE_BoundingSquare::topCenter() const
00112 {
00113 return VEE_Vector2((m_low.x + m_high.x) * 0.5, m_high.y);
00114 }
00115
00116 bool VEE_BoundingSquare::intersects(const VEE_BoundingSquare &b) const
00117 {
00118 for(int i=0; i < 2; i++) {
00119 if(b.m_high[i] < m_low[i] || b.m_low[i] > m_high[i])
00120 return false;
00121 }
00122
00123 return true;
00124 }
00125
00126 inline bool VEE_BoundingSquare::contains(const VEE_Vector2 &v) const
00127 {
00128 return ((v[0] >= m_low[0]) && (v[0] <= m_high[0]) &&
00129 (v[1] >= m_low[1]) && (v[1] <= m_high[1]));
00130 }
00131
00132 inline bool VEE_BoundingSquare::contains(const VEE_BoundingSquare &b) const
00133 {
00134 return ((b.m_low[0] >= m_low[0]) && (b.m_high[0] <= m_high[0]) &&
00135 (b.m_low[1] >= m_low[1]) && (b.m_high[1] <= m_high[1]));
00136 }
00137
00138 inline VEE_Vector2 VEE_BoundingSquare::clamp(const VEE_Vector2 &v) const
00139 {
00140 int i;
00141 VEE_Vector2 r(v);
00142
00143 for(i=0; i < 2; i++)
00144 if(r[i] < m_low[i]) r[i] = m_low[i];
00145
00146 for(i=0; i < 2; i++)
00147 if(r[i] > m_high[i]) r[i] = m_high[i];
00148
00149 return r;
00150 }
00151
00154 template <class M>
00155 inline VEE_BoundingSquare operator * (const M &m, const VEE_BoundingSquare &b)
00156 {
00157 VEE_Vector2 tmp, low = b.low(), high = b.high();
00158
00159 VEE_BoundingSquare square;
00160 square.set(m * low);
00161
00162 for(uint x = 0; x < 2; x++) {
00163 for(uint y = 0; y < 2; y++) {
00164 tmp.x = x ? low.x : high.x;
00165 tmp.y = y ? low.y : high.y;
00166
00167 square.expand(m * tmp);
00168 }
00169 }
00170
00171 return square;
00172 }
00173
00174 #endif
00175