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(Vec3d v, bool b) : vertex(v), edgeFlag(b) {;}
00034     Vec3d vertex;
00035     bool edgeFlag;
00036 };
00037 
00038 QDataStream& operator<<(QDataStream& out, const EdgeVertex&);
00039 QDataStream& operator>>(QDataStream& in, EdgeVertex&);
00040 
00041 class SubContour : public QVector<EdgeVertex>
00042 {
00043 public:
00044     // Create a SubContour from a list of vertices
00045     SubContour(const QVector<Vec3d>& vertices, bool closed=true);
00046     SubContour() {;}
00047     SubContour(int size, const EdgeVertex& v) : QVector<EdgeVertex>(size, v) {;}
00048     SubContour reversed() const;
00049     QString toJSON() const;
00050 };
00051 
00056 class OctahedronPolygon
00057 {
00058 public:
00059     OctahedronPolygon() : fillCachedVertexArray(StelVertexArray::Triangles), outlineCachedVertexArray(StelVertexArray::Lines), capN(1,0,0), capD(-2.)
00060     {sides.resize(8);}
00061 
00063     OctahedronPolygon(const SubContour& subContour);
00064     OctahedronPolygon(const QVector<QVector<Vec3d> >& contours);
00065     OctahedronPolygon(const QVector<Vec3d>& contour);
00066     OctahedronPolygon(const QList<OctahedronPolygon>& octContours);
00067 
00068     double getArea() const;
00069 
00070     Vec3d getPointInside() const;
00071 
00073     StelVertexArray getFillVertexArray() const {return fillCachedVertexArray;}
00074     StelVertexArray getOutlineVertexArray() const {return outlineCachedVertexArray;}
00075 
00076     void getBoundingCap(Vec3d& v, double& d) const {v=capN; d=capD;}
00077 
00079     void inPlaceIntersection(const OctahedronPolygon& mpoly);
00081     void inPlaceUnion(const OctahedronPolygon& mpoly);
00083     void inPlaceSubtraction(const OctahedronPolygon& mpoly);
00084 
00085     bool intersects(const OctahedronPolygon& mpoly) const;
00086     bool contains(const OctahedronPolygon& mpoly) const;
00087 
00088     bool contains(const Vec3d& p) const;
00089     bool isEmpty() const;
00090 
00091     static const OctahedronPolygon& getAllSkyOctahedronPolygon();
00092     static const OctahedronPolygon& getEmptyOctahedronPolygon() {static OctahedronPolygon poly; return poly;}
00093 
00094     static double sphericalTriangleArea(const Vec3d& v0, const Vec3d& v1, const Vec3d& v2)
00095     {
00096         const Vec3d& p1 = v0 ^ v1;
00097         const Vec3d& p2 = v1 ^ v2;
00098         const Vec3d& p3 = v2 ^ v0;
00099         return 2.*M_PI - p1.angle(p2) - p2.angle(p3) - p3.angle(p1);
00100     }
00101 
00102     QString toJson() const;
00103 
00104 private:
00105     // For unit tests
00106     friend class TestStelSphericalGeometry;
00107 
00108     friend QDataStream& operator<<(QDataStream&, const OctahedronPolygon&);
00109     friend QDataStream& operator>>(QDataStream&, OctahedronPolygon&);
00110     friend OctahedronPolygon createAllSkyOctahedronPolygon();
00111 
00113     static OctahedronPolygon createAllSkyOctahedronPolygon();
00114 
00117     void append(const OctahedronPolygon& other);
00118     void appendReversed(const OctahedronPolygon& other);
00119     void appendSubContour(const SubContour& contour);
00120 
00121     enum TessWindingRule
00122     {
00123         WindingPositive=0,  
00124         WindingAbsGeqTwo=1  
00125     };
00126 
00127     bool sideContains2D(const Vec3d& p, int sideNb) const;
00128 
00130     void tesselate(TessWindingRule rule);
00131 
00132     QVector<SubContour> tesselateOneSideLineLoop(class GLUEStesselator* tess, int sidenb) const;
00133     QVector<Vec3d> tesselateOneSideTriangles(class GLUEStesselator* tess, int sidenb) const;
00134     QVarLengthArray<QVector<SubContour>,8 > sides;
00135 
00137     void updateVertexArray();
00138     StelVertexArray fillCachedVertexArray;
00139     StelVertexArray outlineCachedVertexArray;
00140     void computeBoundingCap();
00141     Vec3d capN;
00142     double capD;
00143 
00144     static const Vec3d sideDirections[];
00145     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));}
00146     static bool isTriangleConvexPositive2D(const Vec3d& a, const Vec3d& b, const Vec3d& c);
00147     static bool triangleContains2D(const Vec3d& a, const Vec3d& b, const Vec3d& c, const Vec3d& p);
00148 
00149     static void projectOnOctahedron(QVarLengthArray<QVector<SubContour>,8 >& inSides);
00150     static void splitContourByPlan(int onLine, const SubContour& contour, QVector<SubContour> result[2]);
00151 };
00152 
00153 // Serialization routines
00154 QDataStream& operator<<(QDataStream& out, const OctahedronPolygon&);
00155 QDataStream& operator>>(QDataStream& in, OctahedronPolygon&);
00156 
00157 #endif // _OCTAHEDRON_REGION_HPP_
00158 

Generated on Mon Mar 22 09:55:37 2010 for Stellarium by  doxygen 1.5.5