00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_POLYGON_H
00027 #define NL_POLYGON_H
00028
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/matrix.h"
00031 #include "nel/misc/stream.h"
00032 #include "nel/misc/vector_2f.h"
00033 #include <vector>
00034
00035
00036 namespace NLMISC
00037 {
00038
00039 using NLMISC::CVector;
00040 using NLMISC::CPlane;
00041 using NLMISC::CMatrix;
00042
00043
00044 class CTriangle;
00045
00046
00047 class CBSPNode2v;
00048
00049
00056 class CPolygon
00057 {
00058 public:
00059 std::vector<CVector> Vertices;
00060
00061 public:
00062
00064 CPolygon() {}
00066 CPolygon(const CVector &a, const CVector &b, const CVector &c);
00067
00068 sint getNumVertices() const {return Vertices.size();}
00069
00071 void clip(const CPlane *planes, uint nPlanes);
00073 void clip(const std::vector<CPlane> &planes);
00074
00076 void serial(NLMISC::IStream &f) throw(NLMISC::EStream);
00077
00091 bool toConvexPolygons (std::list<CPolygon>& outputPolygons, const CMatrix& basis) const;
00092
00103 bool chain (const std::vector<CPolygon> &other, const CMatrix& basis);
00104
00106 void getBestTriplet(uint &index0, uint &index1, uint &index2);
00107
00112 void buildBasis(CMatrix &dest);
00113
00114
00115 void toConvexPolygonsLocalAndBSP (std::vector<CVector> &localVertices, CBSPNode2v &root, const CMatrix &basis) const;
00116 static bool toConvexPolygonsEdgeIntersect (const CVector2f& a0, const CVector2f& a1, const CVector2f& b0, const CVector2f& b1);
00117 static bool toConvexPolygonsLeft (const std::vector<CVector> &vertex, uint a, uint b, uint c);
00118 static bool toConvexPolygonsLeftOn (const std::vector<CVector> &vertex, uint a, uint b, uint c);
00119 static bool toConvexPolygonsInCone (const std::vector<CVector> &vertex, uint a, uint b);
00120 static bool toConvexPolygonsDiagonal (const std::vector<CVector> &vertex, const CBSPNode2v &bsp, uint a, uint b);
00121 };
00122
00126 class CPolygon2D
00127 {
00128 public:
00129 typedef std::vector<CVector2f> TVec2fVect;
00130 TVec2fVect Vertices;
00131 public:
00133 CPolygon2D() {}
00134
00138 CPolygon2D(const CPolygon &src, const CMatrix &projMat = CMatrix::Identity);
00139
00143 CPolygon2D(const CTriangle &tri, const CMatrix &projMat = CMatrix::Identity);
00144
00146 bool isConvex();
00147
00152 void buildConvexHull(CPolygon2D &dest) const;
00153
00155 void getBestTriplet(uint &index0, uint &index1, uint &index2);
00156
00158 void serial(NLMISC::IStream &f) throw(NLMISC::EStream);
00159
00160 typedef std::pair<sint, sint> TRaster;
00161 typedef std::vector<TRaster> TRasterVect;
00162
00169 void computeBorders(TRasterVect &borders, sint &minimumY);
00170
00172 bool intersect(const CPolygon2D &other) const;
00173
00175 bool contains(const CVector2f &p) const;
00176
00180 bool getNonNullSeg(uint &seg) const;
00181
00183 void getLineEquation(uint index, float &a, float &b, float &c) const;
00184
00185 private:
00187 float sumDPAgainstLine(float a, float b, float c) const;
00188
00190 const CVector2f &getSegRef0(uint index) const
00191 {
00192 nlassert(index < Vertices.size()); return Vertices[index];
00193 }
00194 const CVector2f &getSegRef1(uint index) const
00195 {
00196 nlassert(index < Vertices.size());
00197 return index == Vertices.size() - 1 ?
00198 Vertices[0] :
00199 Vertices[index + 1];
00200 }
00201
00202
00203
00204 };
00205
00206
00207 }
00208
00209
00210 #endif // NL_POLYGON_H
00211
00212