TCMathBox.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _TC_MATH_BOX_H_
00037 #define _TC_MATH_BOX_H_
00038
00039 #include "TCMathCoordUtil.h"
00040 #include "TCMathUtil.h"
00041
00042 #include <limits>
00043 #include <vector>
00044
00045 namespace TC
00046 {
00047 namespace Math
00048 {
00053 template <class COORD_TYPE>
00054 class Box
00055 {
00056 private:
00057 typedef COORD_TYPE CoordType;
00058 typedef typename COORD_TYPE::DataType DataType;
00059
00060 public:
00062 Box()
00063 {
00064 }
00065
00067 Box(const Box& box)
00068 :m_min(box.m_min),
00069 m_max(box.m_max)
00070 {
00071 }
00072
00079 Box(const COORD_TYPE& min, const COORD_TYPE& max)
00080 :m_min(min),
00081 m_max(max)
00082 {
00083 }
00084
00086 Box& operator=(const Box& box)
00087 {
00088 m_min = box.m_min;
00089 m_max = box.m_max;
00090 return *this;
00091 }
00092
00094 const COORD_TYPE& GetMin() const {return m_min;}
00096 const COORD_TYPE& GetMax() const {return m_max;}
00097
00099 void SetMin(const COORD_TYPE& top) {m_min=top;}
00101 void SetMax(const COORD_TYPE& bottom) {m_max=bottom;}
00102
00104 COORD_TYPE& operator[](uint32 i){ return (&m_min)[i]; }
00106 const COORD_TYPE& operator[](uint32 i) const { return (&m_min)[i]; }
00107
00109 bool operator==(const Box& box) const { return m_min==box.m_min && m_max==box.m_max; }
00111 bool operator!=(const Box& box) const { return m_min!=box.m_min || m_max!=box.m_max; }
00112
00113
00115 DataType GetWidth() const { return m_max[0] - m_min[0]; }
00117 DataType GetHeight() const { return m_max[1] - m_min[1]; }
00119 DataType GetDepth() const { return m_max[2] - m_min[2]; }
00120
00122 DataType GetLongest() const
00123 {
00124 COORD_TYPE diag = GetDiagonal();
00125 return Util::Max(diag[0], diag[1], diag[2]);
00126 }
00127
00129 DataType GetShortest() const
00130 {
00131 COORD_TYPE diag = GetDiagonal();
00132 return Util::Min(diag[0], diag[1], diag[2]);
00133 }
00134
00135 double GetDiameter() const
00136 {
00137 return Distance(m_max, m_min);
00138 }
00139 double GetRadius() const
00140 {
00141 return GetDiameter()/static_cast<DataType>(2);
00142 }
00143
00145 COORD_TYPE GetDiagonal() const {return m_max - m_min;}
00146
00148 COORD_TYPE GetCenter() const {return (m_max + m_min)/static_cast<DataType>(2);}
00149
00151 bool IsEmpty() const
00152 {
00153 return !((m_min.Length2() + m_max.Length2()) > 0);
00154 }
00155
00159 bool Contains(const COORD_TYPE& p) const
00160 {
00161 for (uint32 i=0; i<COORD_TYPE::NUM_COMPONENTS; i++)
00162 {
00163 bool inside = m_min[i] <= p[i] && p[i] < m_max[i];
00164 if (!inside)
00165 {
00166 return false;
00167 }
00168
00169 return true;
00170 }
00171 }
00172
00176 bool Contains(const Box& r) const
00177 {
00178 return Contains(r.m_min) && Contains(r.m_max);
00179 }
00180
00188 static Box Unite(const Box& box1, const Box& box2)
00189 {
00190 if (box1.IsEmpty())
00191 {
00192 return box2;
00193 }
00194 else if (box2.IsEmpty())
00195 {
00196 return box1;
00197 }
00198
00199 COORD_TYPE min = Min(box1.GetMin(), box2.GetMin());
00200 COORD_TYPE max = Max(box1.GetMax(), box2.GetMax());
00201 return Box(min, max);
00202 }
00203
00210 static Box BoundingBox(const std::vector<COORD_TYPE>& coords)
00211 {
00212 COORD_TYPE c_min(std::numeric_limits<DataType>::max());
00213 COORD_TYPE c_max(std::numeric_limits<DataType>::min());
00214
00215 typename std::vector<COORD_TYPE>::const_iterator c_it;
00216 for (c_it= coords.begin(); c_it!=coords.end(); c_it++)
00217 {
00218 c_min = Min(c_min, *c_it);
00219 c_max = Max(c_max, *c_it);
00220 }
00221
00222 return Box(c_min, c_max);
00223 }
00224 private:
00225 COORD_TYPE m_min;
00226 COORD_TYPE m_max;
00227 };
00228 }
00229 }
00230 #endif
00231