Tools2D.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 #ifndef BASE_TOOLS2D_H
00025 #define BASE_TOOLS2D_H
00026
00027
00028 #include <math.h>
00029 #include <stdio.h>
00030 #include <list>
00031 #include <vector>
00032
00033 #include "Vector3D.h"
00034
00035 namespace Base {
00036
00037 class Vector2D;
00038 class BoundBox2D;
00039 class Line2D;
00040 class Polygon2D;
00041
00045 class BaseExport Vector2D
00046 {
00047 public:
00048 float fX, fY;
00049
00050 inline Vector2D (void);
00051 inline Vector2D (float x, float y);
00052 inline Vector2D (double x, double y);
00053 inline Vector2D (const Vector2D &rclVct);
00054
00055
00056 inline float Length (void) const;
00057
00058
00059 inline Vector2D& operator= (const Vector2D &rclVct);
00060 inline float operator* (const Vector2D &rclVct) const;
00061 inline bool operator== (const Vector2D &rclVct) const;
00062 inline Vector2D operator+ (const Vector2D &rclVct) const;
00063 inline Vector2D operator- (const Vector2D &rclVct) const;
00064
00065 inline void Set (float fPX, float fPY);
00066 inline void Scale (float fS);
00067 inline void Normalize (void);
00068 float GetAngle (const Vector2D &rclVect) const;
00069 void ProjToLine (const Vector2D &rclPt, const Vector2D &rclLine);
00070 };
00071
00077 class BaseExport BoundBox2D
00078 {
00079 public:
00080 float fMinX, fMinY, fMaxX, fMaxY;
00081
00082 inline BoundBox2D (void);
00083 inline BoundBox2D (const BoundBox2D &rclBB);
00084 inline BoundBox2D (float fX1, float fY1, float fX2, float fY2);
00085 inline bool IsValid (void);
00086
00087
00088 inline BoundBox2D& operator= (const BoundBox2D& rclBB);
00089 inline bool operator== (const BoundBox2D& rclBB) const;
00090 bool operator|| (const Line2D &rclLine) const;
00091 bool operator|| (const BoundBox2D &rclBB) const;
00092 bool operator|| (const Polygon2D &rclPoly) const;
00093 inline void operator &= (const Vector2D &rclVct);
00094
00095 void SetVoid (void) { fMinX = fMinY = FLOAT_MAX; fMaxX = fMaxY = -FLOAT_MAX; }
00096
00097
00098 bool Contains (const Vector2D &rclV) const;
00099 };
00100
00106 class BaseExport Line2D
00107 {
00108 public:
00109 Vector2D clV1, clV2;
00110
00111 Line2D (void) {}
00112 inline Line2D (const Line2D &rclLine);
00113 inline Line2D (const Vector2D &rclV1, const Vector2D &rclV2);
00114
00115
00116 inline float Length (void) const;
00117 BoundBox2D CalcBoundBox (void) const;
00118
00119
00120 inline Line2D& operator= (const Line2D& rclLine);
00121 inline bool operator== (const Line2D& rclLine) const;
00122
00123
00124 inline bool Contains (const Vector2D &rclV) const;
00125 bool Intersect (const Line2D& rclLine, Vector2D &rclV) const;
00126 bool IntersectAndContain (const Line2D& rclLine, Vector2D &rclV) const;
00127 Vector2D FromPos (float fDistance) const;
00128 };
00129
00135 class BaseExport Polygon2D
00136 {
00137 public:
00138 Polygon2D (void) {}
00139 inline Polygon2D (const Polygon2D &rclPoly);
00140 virtual ~Polygon2D () {}
00141
00142 inline Polygon2D& operator = (const Polygon2D &rclP);
00143
00144
00145 inline size_t GetCtVectors (void) const;
00146 inline bool Add (const Vector2D &rclVct);
00147 inline Vector2D& operator[] (size_t ulNdx) const;
00148 inline Vector2D& At (size_t ulNdx) const;
00149 inline bool Delete (size_t ulNdx);
00150 inline void DeleteAll (void);
00151
00152
00153 BoundBox2D CalcBoundBox (void) const;
00154 bool Contains (const Vector2D &rclV) const;
00155 void Intersect (const Polygon2D &rclPolygon, std::list<Polygon2D> &rclResultPolygonList) const;
00156
00157 private:
00158 std::vector<Vector2D> _aclVct;
00159 };
00160
00163 inline void BoundBox2D::operator &= (const Vector2D &rclVct)
00164 {
00165 fMinX = std::min<float>(fMinX, rclVct.fX);
00166 fMinY = std::min<float>(fMinY, rclVct.fY);
00167 fMaxX = std::max<float>(fMaxX, rclVct.fX);
00168 fMaxY = std::max<float>(fMaxY, rclVct.fY);
00169 }
00170
00171 inline Vector2D::Vector2D (void)
00172 : fX(0.0f), fY(0.0f)
00173 {
00174 }
00175
00176 inline Vector2D::Vector2D (float x, float y)
00177 : fX (x), fY (y)
00178 {
00179 }
00180
00181 inline Vector2D::Vector2D (double x, double y)
00182 : fX(float(x)),
00183 fY(float(y))
00184 {
00185 }
00186
00187 inline Vector2D::Vector2D (const Vector2D &rclVct)
00188 : fX(rclVct.fX), fY(rclVct.fY)
00189 {
00190 }
00191
00192 inline float Vector2D::Length (void) const
00193 {
00194 return (float)sqrt ((fX * fX) + (fY * fY));
00195 }
00196
00197 inline Vector2D& Vector2D::operator= (const Vector2D &rclVct)
00198 {
00199 fX = rclVct.fX;
00200 fY = rclVct.fY;
00201 return *this;
00202 }
00203
00204 inline bool Vector2D::operator== (const Vector2D &rclVct) const
00205 {
00206 return (fX == rclVct.fX) && (fY == rclVct.fY);
00207 }
00208
00209 inline Vector2D Vector2D::operator+ (const Vector2D &rclVct) const
00210 {
00211 return Vector2D(fX + rclVct.fX, fY + rclVct.fY);
00212 }
00213
00214 inline Vector2D Vector2D::operator- (const Vector2D &rclVct) const
00215 {
00216 return Vector2D(fX - rclVct.fX, fY - rclVct.fY);
00217 }
00218
00219 inline float Vector2D::operator* (const Vector2D &rclVct) const
00220 {
00221 return (fX * rclVct.fX) + (fY * rclVct.fY);
00222 }
00223
00224 inline void Vector2D::Scale (float fS)
00225 {
00226 fX *= fS;
00227 fY *= fS;
00228 }
00229
00230 inline void Vector2D::Normalize (void)
00231 {
00232 float fLen = Length();
00233 if (fLen != 0.0f)
00234 {
00235 fX /= fLen;
00236 fY /= fLen;
00237 }
00238 }
00239
00240 inline void Vector2D::Set (float fPX, float fPY)
00241 {
00242 fX = fPX;
00243 fY = fPY;
00244 }
00245
00246 inline Polygon2D::Polygon2D (const Polygon2D &rclPoly)
00247 {
00248 *this = rclPoly;
00249 }
00250
00251 inline Polygon2D& Polygon2D::operator = (const Polygon2D &rclP)
00252 {
00253 _aclVct = rclP._aclVct;
00254 return *this;
00255 }
00256
00257 inline void Polygon2D::DeleteAll (void)
00258 {
00259 _aclVct.clear();
00260 }
00261
00262 inline size_t Polygon2D::GetCtVectors (void) const
00263 {
00264 return _aclVct.size ();
00265 }
00266
00267 inline bool Polygon2D::Add (const Vector2D &rclVct)
00268 {
00269 _aclVct.push_back (rclVct);
00270 return true;
00271 }
00272
00273 inline bool Polygon2D::Delete (size_t ulNdx)
00274 {
00275 if ( ulNdx < _aclVct.size() )
00276 {
00277 std::vector<Vector2D>::iterator it = _aclVct.begin() + ulNdx;
00278 _aclVct.erase ( it );
00279 return true;
00280 }
00281
00282 return false;
00283 }
00284
00285 inline Vector2D& Polygon2D::operator[] (size_t ulNdx) const
00286 {
00287 return (Vector2D&) _aclVct[ulNdx];
00288 }
00289
00290 inline Vector2D& Polygon2D::At (size_t ulNdx) const
00291 {
00292 return (Vector2D&) _aclVct[ulNdx];
00293 }
00294
00295
00296 inline Line2D::Line2D (const Line2D &rclLine)
00297 : clV1 (rclLine.clV1),
00298 clV2 (rclLine.clV2)
00299 {
00300 }
00301
00302 inline Line2D::Line2D (const Vector2D &rclV1, const Vector2D &rclV2)
00303 : clV1 (rclV1), clV2 (rclV2)
00304 {
00305 }
00306
00307 inline float Line2D::Length (void) const
00308 {
00309 return (clV2 - clV1).Length ();
00310 }
00311
00312 inline Line2D& Line2D::operator= (const Line2D& rclLine)
00313 {
00314 clV1 = rclLine.clV1;
00315 clV2 = rclLine.clV2;
00316 return *this;
00317 }
00318
00319 inline bool Line2D::operator== (const Line2D& rclLine) const
00320 {
00321 return (clV1 == rclLine.clV1) && (clV2 == rclLine.clV2);
00322 }
00323
00324 inline bool Line2D::Contains (const Vector2D &rclV) const
00325 {
00326 return CalcBoundBox ().Contains (rclV);
00327 }
00328
00329 inline BoundBox2D::BoundBox2D (void)
00330 {
00331 fMinX = fMinY = FLOAT_MAX;
00332 fMaxX = fMaxY = - FLOAT_MAX;
00333 }
00334
00335 inline BoundBox2D::BoundBox2D (const BoundBox2D &rclBB)
00336 : fMinX (rclBB.fMinX),
00337 fMinY (rclBB.fMinY),
00338 fMaxX (rclBB.fMaxX),
00339 fMaxY (rclBB.fMaxY)
00340 {
00341 }
00342
00343 inline BoundBox2D::BoundBox2D (float fX1, float fY1, float fX2, float fY2)
00344 {
00345 fMinX = std::min<float>( fX1, fX2 );
00346 fMaxX = std::max<float>( fX1, fX2 );
00347 fMinY = std::min<float>( fY1, fY2 );
00348 fMaxY = std::max<float>( fY1, fY2 );
00349 }
00350
00351 inline bool BoundBox2D::IsValid (void)
00352 {
00353 return (fMaxX >= fMinX) && (fMaxY >= fMinY);
00354 }
00355
00356 inline BoundBox2D& BoundBox2D::operator= (const BoundBox2D& rclBB)
00357 {
00358 fMinX = rclBB.fMinX;
00359 fMinY = rclBB.fMinY;
00360 fMaxX = rclBB.fMaxX;
00361 fMaxY = rclBB.fMaxY;
00362 return *this;
00363 }
00364
00365 inline bool BoundBox2D::operator== (const BoundBox2D& rclBB) const
00366 {
00367 return
00368 (fMinX == rclBB.fMinX) &&
00369 (fMinY == rclBB.fMinY) &&
00370 (fMaxX == rclBB.fMaxX) &&
00371 (fMaxY == rclBB.fMaxY);
00372 }
00373
00374 }
00375
00376 #endif // BASE_TOOLS2D_H
00377
00378