Stellarium 0.11.4
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., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, 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 
00031 struct EdgeVertex
00032 {
00033     EdgeVertex() : edgeFlag(false) {;}
00034     EdgeVertex(bool b) : edgeFlag(b) {;}
00035     EdgeVertex(const Vec3d& v, bool b) : vertex(v), edgeFlag(b) {;}
00037     Vec3d vertex;
00039     bool edgeFlag;
00040 };
00041 
00042 Q_DECLARE_TYPEINFO(EdgeVertex, Q_PRIMITIVE_TYPE);
00043 
00044 QDataStream& operator<<(QDataStream& out, const EdgeVertex&);
00045 QDataStream& operator>>(QDataStream& in, EdgeVertex&);
00046 
00047 class SubContour : public QVector<EdgeVertex>
00048 {
00049 public:
00050     // Create a SubContour from a list of vertices
00051     SubContour(const QVector<Vec3d>& vertices, bool closed=true);
00052     SubContour() {;}
00053     SubContour(int size, const EdgeVertex& v) : QVector<EdgeVertex>(size, v) {;}
00054     SubContour reversed() const;
00055     QString toJSON() const;
00056 };
00057 
00062 class OctahedronPolygon
00063 {
00064 public:
00065     OctahedronPolygon() : fillCachedVertexArray(StelVertexArray::Triangles), outlineCachedVertexArray(StelVertexArray::Lines), capN(1,0,0), capD(-2.)
00066     {sides.resize(8);}
00067 
00069     OctahedronPolygon(const SubContour& subContour);
00070     OctahedronPolygon(const QVector<QVector<Vec3d> >& contours);
00071     OctahedronPolygon(const QVector<Vec3d>& contour);
00072     OctahedronPolygon(const QList<OctahedronPolygon>& octContours);
00073 
00074     double getArea() const;
00075 
00076     Vec3d getPointInside() const;
00077 
00079     StelVertexArray getFillVertexArray() const {return fillCachedVertexArray;}
00080     StelVertexArray getOutlineVertexArray() const {return outlineCachedVertexArray;}
00081 
00082     void getBoundingCap(Vec3d& v, double& d) const {v=capN; d=capD;}
00083 
00085     void inPlaceIntersection(const OctahedronPolygon& mpoly);
00087     void inPlaceUnion(const OctahedronPolygon& mpoly);
00089     void inPlaceSubtraction(const OctahedronPolygon& mpoly);
00090 
00091     bool intersects(const OctahedronPolygon& mpoly) const;
00092     bool contains(const OctahedronPolygon& mpoly) const;
00093 
00094     bool contains(const Vec3d& p) const;
00095     bool isEmpty() const;
00096 
00097     static const OctahedronPolygon& getAllSkyOctahedronPolygon();
00098     static const OctahedronPolygon& getEmptyOctahedronPolygon() {static OctahedronPolygon poly; return poly;}
00099 
00100     static double sphericalTriangleArea(const Vec3d& v0, const Vec3d& v1, const Vec3d& v2)
00101     {
00102         const Vec3d& p1 = v0 ^ v1;
00103         const Vec3d& p2 = v1 ^ v2;
00104         const Vec3d& p3 = v2 ^ v0;
00105         return 2.*M_PI - p1.angle(p2) - p2.angle(p3) - p3.angle(p1);
00106     }
00107 
00108     QString toJson() const;
00109 
00110 private:
00111     // For unit tests
00112     friend class TestStelSphericalGeometry;
00113 
00114     friend QDataStream& operator<<(QDataStream&, const OctahedronPolygon&);
00115     friend QDataStream& operator>>(QDataStream&, OctahedronPolygon&);
00116     friend OctahedronPolygon createAllSkyOctahedronPolygon();
00117 
00119     static OctahedronPolygon createAllSkyOctahedronPolygon();
00120 
00123     void append(const OctahedronPolygon& other);
00124     void appendReversed(const OctahedronPolygon& other);
00125     void appendSubContour(const SubContour& contour);
00126 
00127     enum TessWindingRule
00128     {
00129         WindingPositive=0,  
00130         WindingAbsGeqTwo=1  
00131     };
00132 
00133     bool sideContains2D(const Vec3d& p, int sideNb) const;
00134 
00136     void tesselate(TessWindingRule rule);
00137 
00138     QVector<SubContour> tesselateOneSideLineLoop(struct GLUEStesselator* tess, int sidenb) const;
00139     QVector<Vec3d> tesselateOneSideTriangles(struct GLUEStesselator* tess, int sidenb) const;
00140     QVarLengthArray<QVector<SubContour>,8 > sides;
00141 
00143     void updateVertexArray();
00144     StelVertexArray fillCachedVertexArray;
00145     StelVertexArray outlineCachedVertexArray;
00146     void computeBoundingCap();
00147     Vec3d capN;
00148     double capD;
00149 
00150     static const Vec3d sideDirections[];
00151     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));}
00152     static bool isTriangleConvexPositive2D(const Vec3d& a, const Vec3d& b, const Vec3d& c);
00153     static bool triangleContains2D(const Vec3d& a, const Vec3d& b, const Vec3d& c, const Vec3d& p);
00154 
00155     static void projectOnOctahedron(QVarLengthArray<QVector<SubContour>,8 >& inSides);
00156     static void splitContourByPlan(int onLine, const SubContour& contour, QVector<SubContour> result[2]);
00157 };
00158 
00159 // Serialization routines
00160 QDataStream& operator<<(QDataStream& out, const OctahedronPolygon&);
00161 QDataStream& operator>>(QDataStream& in, OctahedronPolygon&);
00162 
00163 #endif // _OCTAHEDRON_REGION_HPP_
00164 
Generated on Sat Aug 25 22:13:29 2012 for Stellarium by  doxygen 1.6.3