eLynx SDK
v3.3.0 C++ image processing API reference |
00001 //============================================================================ 00002 // Geometry.h Math.Component package 00003 //============================================================================ 00004 // Usage : 00005 //---------------------------------------------------------------------------- 00006 // Copyright (C) 2007 by eLynx project 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 // See the GNU Library General Public License for more details. 00017 //---------------------------------------------------------------------------- 00018 #ifndef __Geometry_h__ 00019 #define __Geometry_h__ 00020 00021 #include <elx/core/CoreTypes.h> 00022 #include <elx/core/CoreMacros.h> 00023 #include "MathLib.h" 00024 #include <vector> 00025 00026 namespace eLynx { 00027 namespace Math { 00028 00029 //============================================================================ 00030 // Point2<T> 00031 //============================================================================ 00032 template<typename T> 00033 class ExportedByMath Point2 00034 { 00035 public: 00036 00038 Point2() : _x(), _y() {} 00039 Point2(T iX, T iY) : _x(iX), _y(iY) {} 00040 template<typename U> 00041 Point2(U iX, U iY) : _x(T(iX)), _y(T(iY)) {} 00042 00043 bool operator==(const Point2& iPoint) const 00044 { return _x==iPoint._x && _y==iPoint._y; } 00045 00046 public: 00047 T _x,_y; 00048 }; 00049 00050 template<typename T> 00051 double elxGetDistance(const Point2<T>& iP0, const Point2<T>& iP1); 00052 00053 template<typename T> 00054 Point2<T> elxGetMiddle(const Point2<T>& iP0, const Point2<T>& iP1); 00055 00056 template <class T> 00057 Point2<T> elxGetMiddle(const Point2<T>& iP0, const Point2<T>& iP1, const Point2<T>& iP2); 00058 00059 template<typename T> 00060 bool elxComparePoints(const Point2<T>& iP0, const Point2<T>& iP1); 00061 00062 template<typename T> 00063 bool elxEqualPoints(const Point2<T>& iP0, const Point2<T>& iP1); 00064 00065 template <class T> 00066 Point2<T> elxNormalizePoint(const Point2<T>& iP); 00067 00068 template <class T> 00069 T elxDotProduct(const Point2<T>& iP0, const Point2<T>& iP1); 00070 00071 template <class T> 00072 Point2<T> elxSubstructPoints(const Point2<T>& iP0, const Point2<T>& iP1); 00073 00074 template <class T> 00075 Point2<T> elxAddPoints(const Point2<T>& iP0, const Point2<T>& iP1); 00076 00077 00078 //============================================================================ 00079 // Point3<T> 00080 //============================================================================ 00081 template<typename T> 00082 class ExportedByMath Point3 00083 { 00084 public: 00085 00087 Point3() : _x(), _y(), _z() {} 00088 Point3(T iX, T iY, T iZ) : _x(iX), _y(iY), _z(iZ) {} 00089 template<typename U> 00090 Point3(U iX, U iY, U iZ) : _x(iX), _y(iY), _z(iZ) {} 00091 bool operator==(const Point3& iPoint) const 00092 { return _x==iPoint._x && _y==iPoint._y && _z==iPoint._z; } 00093 00094 public: 00095 T _x, _y, _z; 00096 }; 00097 00098 template<typename T> 00099 double elxGetDistance(const Point3<T>& iP0, const Point3<T>& iP1); 00100 00101 template<typename T> 00102 Point3<T> elxGetMiddle(const Point3<T>& iP0, const Point3<T>& iP1); 00103 00104 template <class T> 00105 Point3<T> elxGetMiddle(const Point3<T>& iP0, const Point3<T>& iP1, const Point3<T>& iP2); 00106 00107 template<typename T> 00108 bool elxComparePoints(const Point3<T>& iP0, const Point3<T>& iP1); 00109 00110 template<typename T> 00111 bool elxEqualPoints(const Point3<T>& iP0, const Point3<T>& iP1); 00112 00113 template <class T> 00114 Point3<T> elxNormalizePoint(const Point3<T>& iP); 00115 00116 template <class T> 00117 Point3<T> elxCrossProduct(const Point3<T>& iP0, const Point3<T>& iP1); 00118 00119 template <class T> 00120 T elxDotProduct(const Point3<T>& iP0, const Point3<T>& iP1); 00121 00122 template <class T> 00123 Point3<T> elxSubstructPoints(const Point3<T>& iP0, const Point3<T>& iP1); 00124 00125 template <class T> 00126 Point3<T> elxAddPoints(const Point3<T>& iP0, const Point3<T>& iP1); 00127 00128 00131 // declare all suitable point types. 00132 00133 typedef Point2<int32> Point2i; 00134 typedef Point2<int64> Point2l; 00135 typedef Point2<float> Point2f; 00136 typedef Point2<double> Point2d; 00137 00138 typedef Point3<int32> Point3i; 00139 typedef Point3<int64> Point3l; 00140 typedef Point3<float> Point3f; 00141 typedef Point3<double> Point3d; 00142 00143 00144 typedef std::vector<Point2i> Point2iList; 00145 typedef std::vector<Point2l> Point2lList; 00146 typedef std::vector<Point2f> Point2fList; 00147 typedef std::vector<Point2d> Point2dList; 00148 00149 00150 //============================================================================ 00151 // Segment2<T> 00152 //============================================================================ 00153 template<typename T> 00154 class ExportedByMath Segment2 00155 { 00156 public: 00158 Segment2() {} 00159 Segment2(const Point2<T>& iP0, const Point2<T>& iP1) : _P0(iP0), _P1(iP1) {} 00160 template<typename U> 00161 Segment2(const Point2<U>& iP0, const Point2<U>& iP1) : _P0(iP0), _P1(iP1) {} 00162 bool operator==(const Segment2& iSegment) const 00163 { return _P0==iSegment._P0 && _P1==iSegment._P1; } 00164 00165 public: 00166 Point2<T> _P0, _P1; 00167 }; 00168 00169 //============================================================================ 00170 // Segment3<T> 00171 //============================================================================ 00172 template<typename T> 00173 class ExportedByMath Segment3 00174 { 00175 public: 00177 Segment3() {} 00178 Segment3(const Point3<T>& iP0, const Point3<T>& iP1) : _P0(iP0), _P1(iP1) {} 00179 template<typename U> 00180 Segment3(const Point3<U>& iP0, const Point3<U>& iP1) : _P0(iP0), _P1(iP1) {} 00181 bool operator==(const Segment3& iSegment) const 00182 { return _P0==iSegment._P0 && _P1==iSegment._P1; } 00183 00184 public: 00185 Point3<T> _P0, _P1; 00186 }; 00187 00190 // declare all suitable point types. 00191 typedef Segment2<int32> Segment2i; 00192 typedef Segment2<int64> Segment2l; 00193 typedef Segment2<float> Segment2f; 00194 typedef Segment2<double> Segment2d; 00195 00196 typedef Segment3<int32> Segment3i; 00197 typedef Segment3<int64> Segment3l; 00198 typedef Segment3<float> Segment3f; 00199 typedef Segment3<double> Segment3d; 00201 00202 00203 //============================================================================ 00204 // Rectangle2<T> 00205 //============================================================================ 00206 template<typename T> 00207 class ExportedByMath Rectangle2 00208 { 00209 public: 00211 Rectangle2() {} 00212 Rectangle2(const Point2<T>& iP0, const Point2<T>& iP1) : 00213 _P0(iP0), _P1(iP1) {} 00214 00215 template<typename U> 00216 Rectangle2(const Point2<U>& iP0, const Point2<U>& iP1) : 00217 _P0(iP0), _P1(iP1) {} 00218 00219 T GetWidth() const { return 1+elxAbs(_P1._x - _P0._x); } 00220 T GetHeight() const { return 1+elxAbs(_P1._y - _P0._y); } 00221 00222 bool operator==(const Rectangle2& iRectangle) const 00223 { return _P0==iRectangle._P0 && _P1==iRectangle._P1; } 00224 public: 00225 Point2<T> _P0, _P1; 00226 }; 00227 00230 // declare all suitable point types. 00231 typedef Rectangle2<int32> Rectangle2i; 00232 typedef Rectangle2<int64> Rectangle2l; 00233 typedef Rectangle2<float> Rectangle2f; 00234 typedef Rectangle2<double> Rectangle2d; 00236 00237 00238 //============================================================================ 00239 // Axix Oriented Bouding Box 2d : AOBBox2<T> 00240 //============================================================================ 00241 template<typename T> 00242 class ExportedByMath AOBBox2 00243 { 00244 public: 00246 AOBBox2() {} 00247 AOBBox2(T iX, T iY, T iW, T iH) : _x(iX), _y(iY), _w(iW), _h(iH) {} 00248 AOBBox2(const Point2<T>& iP0, const Point2<T>& iP1) 00249 { 00250 if (iP0._x > iP1._x) 00251 { _x = iP1._x, _y = iP1._y; _w = 1 + iP0._x-iP1._x; } 00252 else 00253 { _x = iP0._x, _y = iP0._y; _w = 1 + iP1._x-iP0._x; } 00254 _h = 1 + elxAbs(iP1._y - iP0._y); 00255 } 00256 Point2<T> GetPosition() const { return Point2<T>(_x,_y); } 00257 Point2<T> GetCenter() const { return Point2<T>(_x+_w/2, _y+_h/2); } 00258 bool IsInside(T iX, T iY) const { return ((GetXStart() <= iX) && (iX <=GetXEnd()) && (GetYStart() <= iY) && (iY <=GetYEnd())); } 00259 bool IsPoint() const { return ((1 == _w) && (1 == _h)); } 00260 T GetXStart() const { return _x; } 00261 T GetYStart() const { return _y; } 00262 T GetXEnd() const { return _x+_w-1; } 00263 T GetYEnd() const { return _y+_h-1; } 00264 T GetWidth() const { return _w; } 00265 T GetHeight() const { return _h; } 00266 public: 00267 T _x,_y,_w,_h; 00268 }; 00269 00272 // declare all suitable Axis Oriented Bouding Box 2d types. 00273 typedef AOBBox2<int32> AOBBox2i; 00274 typedef AOBBox2<int64> AOBBox2l; 00275 typedef AOBBox2<float> AOBBox2f; 00276 typedef AOBBox2<double> AOBBox2d; 00277 00278 00279 typedef std::vector<AOBBox2i> AOBBox2iList; 00280 typedef std::vector<AOBBox2l> AOBBox2lList; 00281 typedef std::vector<AOBBox2f> AOBBox2fList; 00282 typedef std::vector<AOBBox2d> AOBBox2dList; 00283 00284 00285 //============================================================================ 00286 // Triangle2<T> 00287 //============================================================================ 00288 template<typename T> 00289 class ExportedByMath Triangle2 00290 { 00291 public: 00293 Triangle2() {} 00294 Triangle2(const Point2<T>& iP0, const Point2<T>& iP1, const Point2<T>& iP2) : 00295 _P0(iP0), _P1(iP1), _P2(iP2) {} 00296 template<typename U> 00297 Triangle2(const Point2<U>& iP0, const Point2<U>& iP1, const Point2<U>& iP2) : 00298 _P0(iP0), _P1(iP1), _P2(iP2) {} 00299 bool operator==(const Triangle2& iTriangle) const 00300 { return _P0==iTriangle._P0 && _P1==iTriangle._P1 && _P2==iTriangle._P2; } 00301 00302 public: 00303 Point2<T> _P0, _P1, _P2; 00304 }; 00305 00306 //============================================================================ 00307 // Triangle3<T> 00308 //============================================================================ 00309 template<typename T> 00310 class ExportedByMath Triangle3 00311 { 00312 public: 00314 Triangle3() {} 00315 Triangle3(const Point3<T>& iP0, const Point3<T>& iP1, const Point3<T>& iP2) : 00316 _P0(iP0), _P1(iP1), _P2(iP2) {} 00317 template<typename U> 00318 Triangle3(const Point3<U>& iP0, const Point3<U>& iP1, const Point3<U>& iP2) : 00319 _P0(iP0), _P1(iP1), _P2(iP2) {} 00320 00321 bool operator==(const Triangle3& iTriangle) const 00322 { return _P0==iTriangle._P0 && _P1==iTriangle._P1 && _P2==iTriangle._P2; } 00323 00324 public: 00325 Point3<T> _P0, _P1, _P2; 00326 }; 00327 00330 // declare all suitable point types. 00331 typedef Triangle2<int32> Triangle2i; 00332 typedef Triangle2<int64> Triangle2l; 00333 typedef Triangle2<float> Triangle2f; 00334 typedef Triangle2<double> Triangle2d; 00335 00336 typedef Triangle3<int32> Triangle3i; 00337 typedef Triangle3<int64> Triangle3l; 00338 typedef Triangle3<float> Triangle3f; 00339 typedef Triangle3<double> Triangle3d; 00341 00342 //============================================================================ 00343 // Triangle with point indexes 00344 //============================================================================ 00345 class ExportedByMath TriangleIdx 00346 { 00347 public: 00348 TriangleIdx() {} 00349 TriangleIdx(const uint32 iP0, const uint32 iP1, const uint32 iP2) : 00350 _P0(iP0), _P1(iP1), _P2(iP2) {} 00351 00352 bool operator==(const TriangleIdx& iTriangle) const 00353 { return _P0==iTriangle._P0 && _P1==iTriangle._P1 && _P2==iTriangle._P2; } 00354 00355 public: 00356 uint32 _P0, _P1, _P2; 00357 }; 00358 00359 typedef std::vector<TriangleIdx> TriangleIdxList; 00360 00361 //============================================================================ 00362 // Trianglulation Data 00363 //============================================================================ 00364 struct ExportedByMath TriangulationData 00365 { 00366 struct Edge; 00367 struct Vertex 00368 { 00369 Point2d _position; 00370 std::vector<Edge*> _edges; 00371 }; 00372 00373 struct Triangle; 00374 00375 struct Edge 00376 { 00377 Vertex * _vertex[2]; 00378 Triangle * _triangle[2]; 00379 }; 00380 00381 struct Triangle 00382 { 00383 Vertex * _vertex[3]; 00384 Edge * _edge[3]; 00385 }; 00386 00387 std::vector<Vertex> _vertices; 00388 std::vector<Edge> _edges; 00389 std::vector<Triangle> _triangles; 00390 }; 00391 00392 00393 // Sources in Delaunay.cpp/hpp 00394 bool ExportedByMath elxTriangulate(const Point2iList& iPoints, TriangleIdxList& oTriangles); 00395 bool ExportedByMath elxTriangulate(const Point2iList& iPoints, TriangulationData& oTriangles); 00396 00397 //============================================================================ 00398 // 2d Line processing at every integer coordinates 00399 // sources in inl\Line.inl 00400 //============================================================================ 00401 template <class Operator> 00402 void ExportedByMath elxProcessLine2i( 00403 int32 iX1, int32 iY1, int32 iX2, int32 iY2, 00404 int32 iW, int32 iH, 00405 Operator& iOperator); 00406 00407 template <class Operator> 00408 void ExportedByMath elxProcessLine2i( 00409 const Math::Point2i& iP1, const Math::Point2i& iP2, 00410 int32 iW, int32 iH, 00411 Operator& iOperator); 00412 00413 void ExportedByMath elxComputeLinePoints( 00414 const Point2i& iP1, const Point2i& iP2, 00415 int32 iW, int32 iH, 00416 Point2iList& iPoints); 00417 00418 //============================================================================ 00419 // Intersections 00420 //============================================================================ 00421 template <typename T> 00422 bool ExportedByMath elxIntersectLineLine( 00423 const Point2<T>& iPoint11, const Point2<T>& iPoint12, 00424 const Point2<T>& iPoint21, const Point2<T>& iPoint22, 00425 Point2<T>& oPoint); 00426 00427 template <typename T> 00428 bool ExportedByMath elxIntersectLineLine( 00429 const T iX11, const T iY11, const T iX12, const T iY12, 00430 const T iX21, const T iY21, const T iX22, const T iY22, 00431 T& X, T& Y); 00432 00433 template <typename T, typename U> 00434 bool ExportedByMath elxIntersectLineLine( 00435 const Point2<T>& iPoint11, const Point2<T>& iPoint12, 00436 const Point2<T>& iPoint21, const Point2<T>& iPoint22, 00437 Point2<U>& oPoint); 00438 00439 template <typename T, typename U> 00440 bool ExportedByMath elxIntersectLineLine( 00441 const T iX11, const T iY11, const T iX12, const T iY12, 00442 const T iX21, const T iY21, const T iX22, const T iY22, 00443 U& X, U& Y); 00444 00445 template <typename T> 00446 bool ExportedByMath elxIntersectLineSegment( 00447 const Point2<T>& iPoint1, const Point2<T>& iPoint2, 00448 const Segment2<T>& iSegment, 00449 Point2<T>& oPoint); 00450 /* 00451 template <typename T> 00452 bool ExportedByMath elxIntersectLineRectangle( 00453 const Point2<T>& iPoint1, const Point2<T>& iPoint2, 00454 const Rectangle2<T>& iRectangle, 00455 Segment2<T>& oSegment); 00456 */ 00457 bool ExportedByMath elxIntersectLineRectangle( 00458 const Point2<int32>& iPoint1, const Point2<int32>& iPoint2, 00459 const Rectangle2<int32>& iRectangle, 00460 Segment2<int32>& oSegment); 00461 00462 template <typename T> 00463 bool ExportedByMath elxIntersectCircleRectangle( 00464 const Rectangle2<T>& iRectangle, const Point2<T>& iPoint, 00465 T iRadius); 00466 00467 //============================================================================ 00468 // Barycentric coordinates 00469 //============================================================================ 00470 template<typename T> 00471 struct BarycentricCoordinates2 00472 { 00473 // The context triangle must not be degenerate 00474 BarycentricCoordinates2(const Triangle2<T>& iTriangle); 00475 BarycentricCoordinates2( 00476 const Point2<T>& iP0, const Point2<T>& iP1, const Point2<T>& iP2); 00477 BarycentricCoordinates2(T iX0, T iY0, T iX1, T iY1, T iX2, T iY2); 00478 00479 // Swaps verticies 00480 void Swap(const uint32 iVertex1, const uint32 iVertex2); 00481 00482 // Calculates barycentric coordinates for a given point 00483 void GetCoordinates(const Point2<T>& iPoint, T& oU, T& oV, T& oW) const; 00484 void GetCoordinates(const T iX, const T iY, T& oU, T& oV, T& oW) const; 00485 00486 private: 00487 void Init(); 00488 00489 T _x0, _y0, _x1, _y1, _x2, _y2; 00490 T _a, _b, _c, _d, _e, _f, _aebd; 00491 }; 00492 00493 } // namespace Math 00494 } // namespace eLynx 00495 00496 #include "inl/Geometry.inl" 00497 00498 #endif //__Geometry_h__