Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · File Structure

core/OctahedronPolygon.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2009 Fabien Chereau
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #ifndef _OCTAHEDRON_REGION_HPP_
00021 #define _OCTAHEDRON_REGION_HPP_
00022 
00023 #include <QVector>
00024 #include <QDebug>
00025 #include <QVarLengthArray>
00026 #include "VecMath.hpp"
00027 #include "StelVertexArray.hpp"
00028 
00029 struct EdgeVertex
00030 {
00031     EdgeVertex() : edgeFlag(false) {;}
00032     EdgeVertex(bool b) : edgeFlag(b) {;}
00033     EdgeVertex(const Vec3d& v, bool b) : vertex(v), edgeFlag(b) {;}
00034     Vec3d vertex;
00035     bool edgeFlag;
00036 };
00037 
00038 Q_DECLARE_TYPEINFO(EdgeVertex, Q_PRIMITIVE_TYPE);
00039 
00040 QDataStream& operator<<(QDataStream& out, const EdgeVertex&);
00041 QDataStream& operator>>(QDataStream& in, EdgeVertex&);
00042 
00043 class SubContour : public QVector<EdgeVertex>
00044 {
00045 public:
00046     // Create a SubContour from a list of vertices
00047     SubContour(const QVector<Vec3d>& vertices, bool closed=true);
00048     SubContour() {;}
00049     SubContour(int size, const EdgeVertex& v) : QVector<EdgeVertex>(size, v) {;}
00050     SubContour reversed() const;
00051     QString toJSON() const;
00052 };
00053 
00058 class OctahedronPolygon
00059 {
00060 public:
00061     OctahedronPolygon() : fillCachedVertexArray(StelVertexArray::Triangles), outlineCachedVertexArray(StelVertexArray::Lines), capN(1,0,0), capD(-2.)
00062     {sides.resize(8);}
00063 
00065     OctahedronPolygon(const SubContour& subContour);
00066     OctahedronPolygon(const QVector<QVector<Vec3d> >& contours);
00067     OctahedronPolygon(const QVector<Vec3d>& contour);
00068     OctahedronPolygon(const QList<OctahedronPolygon>& octContours);
00069 
00070     double getArea() const;
00071 
00072     Vec3d getPointInside() const;
00073 
00075     StelVertexArray getFillVertexArray() const {return fillCachedVertexArray;}
00076     StelVertexArray getOutlineVertexArray() const {return outlineCachedVertexArray;}
00077 
00078     void getBoundingCap(Vec3d& v, double& d) const {v=capN; d=capD;}
00079 
00081     void inPlaceIntersection(const OctahedronPolygon& mpoly);
00083     void inPlaceUnion(const OctahedronPolygon& mpoly);
00085     void inPlaceSubtraction(const OctahedronPolygon& mpoly);
00086 
00087     bool intersects(const OctahedronPolygon& mpoly) const;
00088     bool contains(const OctahedronPolygon& mpoly) const;
00089 
00090     bool contains(const Vec3d& p) const;
00091     bool isEmpty() const;
00092 
00093     static const OctahedronPolygon& getAllSkyOctahedronPolygon();
00094     static const OctahedronPolygon& getEmptyOctahedronPolygon() {static OctahedronPolygon poly; return poly;}
00095 
00096     static double sphericalTriangleArea(const Vec3d& v0, const Vec3d& v1, const Vec3d& v2)
00097     {
00098         const Vec3d& p1 = v0 ^ v1;
00099         const Vec3d& p2 = v1 ^ v2;
00100         const Vec3d& p3 = v2 ^ v0;
00101         return 2.*M_PI - p1.angle(p2) - p2.angle(p3) - p3.angle(p1);
00102     }
00103 
00104     QString toJson() const;
00105 
00106 private:
00107     // For unit tests
00108     friend class TestStelSphericalGeometry;
00109 
00110     friend QDataStream& operator<<(QDataStream&, const OctahedronPolygon&);
00111     friend QDataStream& operator>>(QDataStream&, OctahedronPolygon&);
00112     friend OctahedronPolygon createAllSkyOctahedronPolygon();
00113 
00115     static OctahedronPolygon createAllSkyOctahedronPolygon();
00116 
00119     void append(const OctahedronPolygon& other);
00120     void appendReversed(const OctahedronPolygon& other);
00121     void appendSubContour(const SubContour& contour);
00122 
00123     enum TessWindingRule
00124     {
00125         WindingPositive=0,  
00126         WindingAbsGeqTwo=1  
00127     };
00128 
00129     bool sideContains2D(const Vec3d& p, int sideNb) const;
00130 
00132     void tesselate(TessWindingRule rule);
00133 
00134     QVector<SubContour> tesselateOneSideLineLoop(class GLUEStesselator* tess, int sidenb) const;
00135     QVector<Vec3d> tesselateOneSideTriangles(class GLUEStesselator* tess, int sidenb) const;
00136     QVarLengthArray<QVector<SubContour>,8 > sides;
00137 
00139     void updateVertexArray();
00140     StelVertexArray fillCachedVertexArray;
00141     StelVertexArray outlineCachedVertexArray;
00142     void computeBoundingCap();
00143     Vec3d capN;
00144     double capD;
00145 
00146     static const Vec3d sideDirections[];
00147     static int getSideNumber(const Vec3d& v) {return v[0]>=0. ?  (v[1]>=0. ? (v[2]>=0.?0:1) : (v[2]>=0.?4:5))   :   (v[1]>=0. ? (v[2]>=0.?2:3) : (v[2]>=0.?6:7));}
00148     static bool isTriangleConvexPositive2D(const Vec3d& a, const Vec3d& b, const Vec3d& c);
00149     static bool triangleContains2D(const Vec3d& a, const Vec3d& b, const Vec3d& c, const Vec3d& p);
00150 
00151     static void projectOnOctahedron(QVarLengthArray<QVector<SubContour>,8 >& inSides);
00152     static void splitContourByPlan(int onLine, const SubContour& contour, QVector<SubContour> result[2]);
00153 };
00154 
00155 // Serialization routines
00156 QDataStream& operator<<(QDataStream& out, const OctahedronPolygon&);
00157 QDataStream& operator>>(QDataStream& in, OctahedronPolygon&);
00158 
00159 #endif // _OCTAHEDRON_REGION_HPP_
00160 

Generated on Wed Jun 2 13:11:13 2010 for Stellarium by  doxygen 1.5.5