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

core/StelSphereGeometry.hpp

Go to the documentation of this file.
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 _STELSPHEREGEOMETRY_HPP_
00021 #define _STELSPHEREGEOMETRY_HPP_
00022 
00023 #include <QVector>
00024 #include <QVariant>
00025 #include <QDebug>
00026 #include <QSharedPointer>
00027 #include <QVarLengthArray>
00028 #include "VecMath.hpp"
00029 #include "OctahedronPolygon.hpp"
00030 #include "StelVertexArray.hpp"
00031 
00032 class SphericalRegion;
00033 class SphericalPolygon;
00034 class SphericalConvexPolygon;
00035 class SphericalCap;
00036 class SphericalPoint;
00037 class AllSkySphericalRegion;
00038 class EmptySphericalRegion;
00039 
00042 
00045 class SphericalRegionP : public QSharedPointer<SphericalRegion>
00046 {
00047 public:
00048     // Override the constructors of QSharedPointer
00049     SphericalRegionP() {;}
00050     SphericalRegionP(SphericalRegion* ptr) : QSharedPointer<SphericalRegion>(ptr) {;}
00051     template <class Deleter> SphericalRegionP(SphericalRegion* ptr, Deleter deleter) : QSharedPointer<SphericalRegion>(ptr, deleter) {;}
00052     SphericalRegionP(const SphericalRegionP& other) : QSharedPointer<SphericalRegion>(other) {;}
00053     SphericalRegionP(const QWeakPointer<SphericalRegion>& other) : QSharedPointer<SphericalRegion>(other) {;}
00054 
00093     static SphericalRegionP loadFromJson(QIODevice* in);
00094 
00097     static SphericalRegionP loadFromJson(const QByteArray& a);
00098 
00101     static SphericalRegionP loadFromQVariant(const QVariantMap& map);
00102     // It can only be a pure shape definition, without texture coords
00103     static SphericalRegionP loadFromQVariant(const QVariantList& list);
00104 
00106     static void serializeToJson(const QVariant& jsonObject, QIODevice* output, int indentLevel=0);
00107 
00109     static int metaTypeId;
00110 
00111 private:
00112 
00114     static int initialize();
00115 };
00116 
00117 // Allow to use SphericalRegionP with the Qt MetaType system.
00118 Q_DECLARE_METATYPE(SphericalRegionP);
00119 
00121 QDataStream& operator<<(QDataStream& out, const SphericalRegionP& region);
00123 QDataStream& operator>>(QDataStream& in, SphericalRegionP& region);
00124 
00129 class SphericalRegion
00130 {
00131 public:
00133     enum SphericalRegionType
00134     {
00135         Point = 0,
00136         Cap = 1,
00137         AllSky = 2,
00138         Polygon = 3,
00139         ConvexPolygon = 4,
00140         Empty = 5,
00141         Invalid = 6
00142     };
00143 
00144     virtual ~SphericalRegion() {;}
00145 
00146     virtual SphericalRegionType getType() const = 0;
00147 
00150     virtual OctahedronPolygon getOctahedronPolygon() const =0;
00151 
00153     virtual double getArea() const {return getOctahedronPolygon().getArea();}
00154 
00156     virtual bool isEmpty() const {return getOctahedronPolygon().isEmpty();}
00157 
00159     virtual Vec3d getPointInside() const {return getOctahedronPolygon().getPointInside();}
00160 
00162     virtual QVector<SphericalCap> getBoundingSphericalCaps() const;
00163 
00166     virtual SphericalCap getBoundingCap() const;
00167 
00174     virtual SphericalRegionP getEnlarged(double margin) const;
00175 
00177     virtual StelVertexArray getFillVertexArray() const {return getOctahedronPolygon().getFillVertexArray();}
00178 
00181     virtual StelVertexArray getOutlineVertexArray() const {return getOctahedronPolygon().getOutlineVertexArray();}
00182 
00185     virtual QVector<QVector<Vec3d > > getSimplifiedContours() const;
00186     
00188     virtual QVariantList toQVariant() const = 0;
00189 
00191     virtual void serialize(QDataStream& out) const = 0;
00192 
00195     QByteArray toJSON() const;
00196 
00199     bool contains(const SphericalRegion* r) const;
00200     bool contains(const SphericalRegionP r) const {return contains(r.data());}
00201     virtual bool contains(const Vec3d& p) const {return getOctahedronPolygon().contains(p);}
00202     virtual bool contains(const SphericalPolygon& r) const;
00203     virtual bool contains(const SphericalConvexPolygon& r) const;
00204     virtual bool contains(const SphericalCap& r) const;
00205     virtual bool contains(const SphericalPoint& r) const;
00206     virtual bool contains(const AllSkySphericalRegion& r) const;
00207     bool contains(const EmptySphericalRegion&) const {return false;}
00208 
00211     bool intersects(const SphericalRegion* r) const;
00212     bool intersects(const SphericalRegionP r) const {return intersects(r.data());}
00213     bool intersects(const Vec3d& p) const {return contains(p);}
00214     virtual bool intersects(const SphericalPolygon& r) const;
00215     virtual bool intersects(const SphericalConvexPolygon& r) const;
00216     virtual bool intersects(const SphericalCap& r) const;
00217     virtual bool intersects(const SphericalPoint& r) const;
00218     virtual bool intersects(const AllSkySphericalRegion& r) const;
00219     bool intersects(const EmptySphericalRegion&) const {return false;}
00220 
00223     SphericalRegionP getIntersection(const SphericalRegion* r) const;
00224     SphericalRegionP getIntersection(const SphericalRegionP r) const {return getIntersection(r.data());}
00225     virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
00226     virtual SphericalRegionP getIntersection(const SphericalConvexPolygon& r) const;
00227     virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
00228     virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
00229     virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& r) const;
00230     SphericalRegionP getIntersection(const EmptySphericalRegion& r) const;
00231 
00234     SphericalRegionP getUnion(const SphericalRegion* r) const;
00235     SphericalRegionP getUnion(const SphericalRegionP r) const {return getUnion(r.data());}
00236     virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
00237     virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
00238     virtual SphericalRegionP getUnion(const SphericalCap& r) const;
00239     virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
00240     SphericalRegionP getUnion(const AllSkySphericalRegion& r) const;
00241     virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
00242 
00245     SphericalRegionP getSubtraction(const SphericalRegion* r) const;
00246     SphericalRegionP getSubtraction(const SphericalRegionP r) const {return getSubtraction(r.data());}
00247     virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
00248     virtual SphericalRegionP getSubtraction(const SphericalConvexPolygon& r) const;
00249     virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
00250     virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
00251     SphericalRegionP getSubtraction(const AllSkySphericalRegion& r) const;
00252     virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
00253 
00254 private:
00255     bool containsDefault(const SphericalRegion* r) const;
00256     bool intersectsDefault(const SphericalRegion* r) const;
00257     SphericalRegionP getIntersectionDefault(const SphericalRegion* r) const;
00258     SphericalRegionP getUnionDefault(const SphericalRegion* r) const;
00259     SphericalRegionP getSubtractionDefault(const SphericalRegion* r) const;
00260 };
00261 
00262 
00267 class SphericalCap : public SphericalRegion
00268 {
00269 public:
00271     SphericalCap() : d(0) {;}
00272 
00274     SphericalCap(double x, double y, double z) : n(x,y,z), d(0) {;}
00275 
00279     SphericalCap(const Vec3d& an, double ar) : n(an), d(ar) {Q_ASSERT(d==0 || std::fabs(n.lengthSquared()-1.)<0.0000001);}
00280 
00282     SphericalCap(const SphericalCap& other) : SphericalRegion(), n(other.n), d(other.d) {;}
00283 
00284     virtual SphericalRegionType getType() const {return SphericalRegion::Cap;}
00285     virtual OctahedronPolygon getOctahedronPolygon() const;
00286 
00288     virtual double getArea() const {return 2.*M_PI*(1.-d);}
00289 
00291     virtual bool isEmpty() const {return d>=1.;}
00292 
00294     virtual Vec3d getPointInside() const {return n;}
00295 
00297     virtual SphericalCap getBoundingCap() const {return *this;}
00298 
00299     // Contain and intersect
00300     virtual bool contains(const Vec3d &v) const {Q_ASSERT(d==0 || std::fabs(v.lengthSquared()-1.)<0.0000001);return (v*n>=d);}
00301     virtual bool contains(const Vec3f &v) const {Q_ASSERT(d==0 || std::fabs(v.lengthSquared()-1.f)<0.0000001f);return (v[0]*n[0]+v[1]*n[1]+v[2]*n[2]>=d);}
00302     virtual bool contains(const SphericalConvexPolygon& r) const;
00303     virtual bool contains(const SphericalCap& h) const
00304     {
00305         const double a = n*h.n-d*h.d;
00306         return d<=h.d && ( a>=1. || (a>=0. && a*a >= (1.-d*d)*(1.-h.d*h.d)));
00307     }
00308     virtual bool contains(const AllSkySphericalRegion&) const {return d<=-1;}
00309     virtual bool intersects(const SphericalPolygon& r) const;
00310     virtual bool intersects(const SphericalConvexPolygon& r) const;
00314     virtual bool intersects(const SphericalCap& h) const
00315     {
00316         const double a = d*h.d - n*h.n;
00317         return d+h.d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d)*(1.-h.d*h.d));
00318     }
00319     virtual bool intersects(const AllSkySphericalRegion&) const {return d<=1.;}
00320 
00324     virtual QVariantList toQVariant() const;
00325 
00326     virtual void serialize(QDataStream& out) const {out << n << d;}
00327 
00329     // Methods specific to SphericalCap
00330 
00332     double getRadius() const {return std::acos(d);}
00333 
00338     inline bool intersectsHalfSpace(double hn0, double hn1, double hn2) const
00339     {
00340         const double a = n[0]*hn0+n[1]*hn1+n[2]*hn2;
00341         return d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d));
00342     }
00343 
00346     bool clipGreatCircle(Vec3d& v1, Vec3d& v2) const;
00347 
00349     bool operator==(const SphericalCap& other) const {return (n==other.n && d==other.d);}
00350 
00352     QVector<Vec3d> getClosedOutlineContour() const;
00353 
00355     bool intersectsConvexContour(const Vec3d* vertice, int nbVertice) const;
00356 
00358     bool containsTriangle(const Vec3d* vertice) const;
00359 
00361     bool intersectsTriangle(const Vec3d* vertice) const;
00362 
00364     static SphericalRegionP deserialize(QDataStream& in);
00365 
00368     static double relativeAreaOverlap(const SphericalCap& c1, const SphericalCap& c2);
00369 
00372     static double relativeDiameterOverlap(const SphericalCap& c1, const SphericalCap& c2);
00373 
00376     static bool intersectionPoints(const SphericalCap& h1, const SphericalCap& h2, Vec3d& p1, Vec3d& p2);
00377 
00379     Vec3d n;
00381     double d;
00382 };
00383 
00387 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const Vec3d& p)
00388 {
00389     return (v1[1] * v2[2] - v1[2] * v2[1])*p[0] +
00390             (v1[2] * v2[0] - v1[0] * v2[2])*p[1] +
00391             (v1[0] * v2[1] - v1[1] * v2[0])*p[2]>=-1e-17;
00392 }
00393 
00395 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
00396 {
00397     Vec3d n(v1[1]*v2[2]-v1[2]*v2[1], v2[0]*v1[2]-v2[2]*v1[0], v2[1]*v1[0]-v2[0]*v1[1]);
00398     n.normalize();
00399     const double a = n*h.n;
00400     return 0<=h.d && ( a>=1. || (a>=0. && a*a >= 1.-h.d*h.d));
00401 }
00402 
00404 inline bool sideHalfSpaceIntersects(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
00405 {
00406     Vec3d n(v1[1]*v2[2]-v1[2]*v2[1], v2[0]*v1[2]-v2[2]*v1[0], v2[1]*v1[0]-v2[0]*v1[1]);
00407     n.normalize();
00408     return  h.intersectsHalfSpace(n[0], n[1], n[2]);
00409 }
00410 
00413 class SphericalPoint : public SphericalRegion
00414 {
00415 public:
00416     SphericalPoint(const Vec3d& an) : n(an) {Q_ASSERT(std::fabs(1.-n.length())<0.0000001);}
00417     virtual ~SphericalPoint() {;}
00418 
00419     virtual SphericalRegionType getType() const {return SphericalRegion::Point;}
00420     virtual OctahedronPolygon getOctahedronPolygon() const;
00421     virtual double getArea() const {return 0.;}
00422     virtual bool isEmpty() const {return false;}
00423     virtual Vec3d getPointInside() const {return n;}
00424     virtual SphericalCap getBoundingCap() const {return SphericalCap(n, 1);}
00427     virtual QVariantList toQVariant() const;
00428     virtual void serialize(QDataStream& out) const {out << n;}
00429 
00430     // Contain and intersect
00431     virtual bool contains(const Vec3d& p) const {return n==p;}
00432     virtual bool contains(const SphericalPolygon&) const {return false;}
00433     virtual bool contains(const SphericalConvexPolygon&) const {return false;}
00434     virtual bool contains(const SphericalCap&) const {return false;}
00435     virtual bool contains(const SphericalPoint& r) const {return n==r.n;}
00436     virtual bool contains(const AllSkySphericalRegion&) const {return false;}
00437     virtual bool intersects(const SphericalPolygon&) const;
00438     virtual bool intersects(const SphericalConvexPolygon&) const;
00439     virtual bool intersects(const SphericalCap& r) const {return r.contains(n);}
00440     virtual bool intersects(const SphericalPoint& r) const {return n==r.n;}
00441     virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
00442 
00444     static SphericalRegionP deserialize(QDataStream& in);
00445 
00447     Vec3d n;
00448 };
00449 
00452 class AllSkySphericalRegion : public SphericalRegion
00453 {
00454 public:
00455     virtual ~AllSkySphericalRegion() {;}
00456 
00457     virtual SphericalRegionType getType() const {return SphericalRegion::AllSky;}
00458     virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getAllSkyOctahedronPolygon();}
00459     virtual double getArea() const {return 4.*M_PI;}
00460     virtual bool isEmpty() const {return false;}
00461     virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
00462     virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), -2);}
00465     virtual QVariantList toQVariant() const;
00466     virtual void serialize(QDataStream&) const {;}
00467 
00468     // Contain and intersect
00469     virtual bool contains(const Vec3d&) const {return true;}
00470     virtual bool contains(const SphericalPolygon&) const {return true;}
00471     virtual bool contains(const SphericalConvexPolygon&) const {return true;}
00472     virtual bool contains(const SphericalCap&) const {return true;}
00473     virtual bool contains(const SphericalPoint&) const {return true;}
00474     virtual bool contains(const AllSkySphericalRegion&) const {return true;}
00475     virtual bool intersects(const SphericalPolygon&) const {return true;}
00476     virtual bool intersects(const SphericalConvexPolygon&) const {return true;}
00477     virtual bool intersects(const SphericalCap&) const {return true;}
00478     virtual bool intersects(const SphericalPoint&) const {return true;}
00479     virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
00480 
00481     static const SphericalRegionP staticInstance;
00482 };
00483 
00486 class EmptySphericalRegion : public SphericalRegion
00487 {
00488 public:
00489     // Avoid name hiding when overloading the virtual methods.
00490     using SphericalRegion::intersects;
00491     using SphericalRegion::contains;
00492     using SphericalRegion::getIntersection;
00493     using SphericalRegion::getUnion;
00494     using SphericalRegion::getSubtraction;
00495 
00496     EmptySphericalRegion() {;}
00497     virtual ~EmptySphericalRegion() {;}
00498 
00499     virtual SphericalRegionType getType() const {return SphericalRegion::Empty;}
00500     virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getEmptyOctahedronPolygon();}
00501     virtual double getArea() const {return 0.;}
00502     virtual bool isEmpty() const {return true;}
00503     virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
00504     virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), 2);}
00507     virtual QVariantList toQVariant() const;
00508     virtual void serialize(QDataStream&) const {;}
00509 
00510     // Contain and intersect
00511     virtual bool contains(const Vec3d&) const {return false;}
00512     virtual bool contains(const SphericalPolygon&) const {return false;}
00513     virtual bool contains(const SphericalConvexPolygon&) const {return false;}
00514     virtual bool contains(const SphericalCap&) const {return false;}
00515     virtual bool contains(const SphericalPoint&) const {return false;}
00516     virtual bool contains(const AllSkySphericalRegion&) const {return false;}
00517     virtual bool intersects(const SphericalPolygon&) const {return false;}
00518     virtual bool intersects(const SphericalConvexPolygon&) const {return false;}
00519     virtual bool intersects(const SphericalCap&) const {return false;}
00520     virtual bool intersects(const SphericalPoint&) const {return false;}
00521     virtual bool intersects(const AllSkySphericalRegion&) const {return false;}
00522 
00523     static const SphericalRegionP staticInstance;
00524 };
00525 
00526 
00530 class SphericalPolygon : public SphericalRegion
00531 {
00532 public:
00533     // Avoid name hiding when overloading the virtual methods.
00534     using SphericalRegion::intersects;
00535     using SphericalRegion::contains;
00536     using SphericalRegion::getIntersection;
00537     using SphericalRegion::getUnion;
00538     using SphericalRegion::getSubtraction;
00539 
00540     SphericalPolygon() {;}
00542     SphericalPolygon(const QVector<QVector<Vec3d> >& contours) : octahedronPolygon(contours) {;}
00544     SphericalPolygon(const QVector<Vec3d>& contour) : octahedronPolygon(contour) {;}
00545     SphericalPolygon(const OctahedronPolygon& octContour) : octahedronPolygon(octContour) {;}
00546     SphericalPolygon(const QList<OctahedronPolygon>& octContours) : octahedronPolygon(octContours) {;}
00547 
00548     virtual SphericalRegionType getType() const {return SphericalRegion::Polygon;}
00549     virtual OctahedronPolygon getOctahedronPolygon() const {return octahedronPolygon;}
00550 
00555     virtual QVariantList toQVariant() const;
00556     virtual void serialize(QDataStream& out) const;
00557 
00558     virtual SphericalCap getBoundingCap() const;
00559 
00560     virtual bool contains(const Vec3d& p) const {return octahedronPolygon.contains(p);}
00561     virtual bool contains(const SphericalPolygon& r) const {return octahedronPolygon.contains(r.octahedronPolygon);}
00562     virtual bool contains(const SphericalConvexPolygon& r) const;
00563     virtual bool contains(const SphericalCap& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
00564     virtual bool contains(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
00565     virtual bool contains(const AllSkySphericalRegion& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
00566 
00567     virtual bool intersects(const SphericalPolygon& r) const {return octahedronPolygon.intersects(r.octahedronPolygon);}
00568     virtual bool intersects(const SphericalConvexPolygon& r) const;
00569     virtual bool intersects(const SphericalCap& r) const {return r.intersects(*this);}
00570     virtual bool intersects(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
00571     virtual bool intersects(const AllSkySphericalRegion&) const {return !isEmpty();}
00572 
00573     virtual SphericalRegionP getIntersection(const SphericalPoint& r) const {return contains(r.n) ? SphericalRegionP(new SphericalPoint(r)) : EmptySphericalRegion::staticInstance;}
00574     virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& ) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
00575 
00576     virtual SphericalRegionP getUnion(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
00577     virtual SphericalRegionP getUnion(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
00578 
00579     virtual SphericalRegionP getSubtraction(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
00580     virtual SphericalRegionP getSubtraction(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
00581 
00583     // Methods specific to SphericalPolygon
00587     void setContours(const QVector<QVector<Vec3d> >& contours) {octahedronPolygon = OctahedronPolygon(contours);}
00588 
00591     void setContour(const QVector<Vec3d>& contour) {octahedronPolygon = OctahedronPolygon(contour);}
00592 
00594     QVector<QVector<Vec3d> > getClosedOutlineContours() const {Q_ASSERT(0); return QVector<QVector<Vec3d> >();}
00595 
00597     static SphericalRegionP deserialize(QDataStream& in);
00598 
00600     static SphericalRegionP multiUnion(const QList<SphericalRegionP>& regions, bool optimizeByPreGrouping=false);
00601     
00603     static SphericalRegionP multiIntersection(const QList<SphericalRegionP>& regions);
00604 
00605 private:
00606     OctahedronPolygon octahedronPolygon;
00607 };
00608 
00609 
00612 class SphericalConvexPolygon : public SphericalRegion
00613 {
00614 public:
00615     // Avoid name hiding when overloading the virtual methods.
00616     using SphericalRegion::intersects;
00617     using SphericalRegion::contains;
00618 
00620     SphericalConvexPolygon() {;}
00621 
00623     SphericalConvexPolygon(const QVector<QVector<Vec3d> >& contours) {Q_ASSERT(contours.size()==1); setContour(contours.at(0));}
00625     SphericalConvexPolygon(const QVector<Vec3d>& contour) {setContour(contour);}
00627     SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2) {contour << e0 << e1 << e2; updateBoundingCap();}
00629     SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3)  {contour << e0 << e1 << e2 << e3; updateBoundingCap();}
00630 
00631     virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygon;}
00632     virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon(contour);}
00633     virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan);}
00634     virtual StelVertexArray getOutlineVertexArray() const {return StelVertexArray(contour, StelVertexArray::LineLoop);}
00635     virtual double getArea() const;
00636     virtual bool isEmpty() const {return contour.isEmpty();}
00637     virtual Vec3d getPointInside() const;
00638     virtual SphericalCap getBoundingCap() const {return cachedBoundingCap;}
00639     QVector<SphericalCap> getBoundingSphericalCaps() const;
00644     virtual QVariantList toQVariant() const;
00645     virtual void serialize(QDataStream& out) const {out << contour;}
00646 
00647     // Contain and intersect
00648     virtual bool contains(const Vec3d& p) const;
00649     virtual bool contains(const SphericalPolygon& r) const;
00650     virtual bool contains(const SphericalConvexPolygon& r) const;
00651     virtual bool contains(const SphericalCap& r) const;
00652     virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
00653     virtual bool contains(const AllSkySphericalRegion&) const {return false;}
00654     virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
00655     virtual bool intersects(const SphericalPolygon& r) const;
00656     virtual bool intersects(const SphericalConvexPolygon& r) const;
00657     virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
00658     virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
00659 
00661 //  virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
00662 //  virtual SphericalRegionP getIntersection(const SphericalConvexPolygon& r) const;
00663 //  virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
00664 //  virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
00665 //  virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& r) const;
00666 //  virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
00667 //  virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
00668 //  virtual SphericalRegionP getUnion(const SphericalCap& r) const;
00669 //  virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
00670 //  virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
00671 //  virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
00672 //  virtual SphericalRegionP getSubtraction(const SphericalConvexPolygon& r) const;
00673 //  virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
00674 //  virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
00675 //  virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
00676 
00678     // Methods specific to SphericalConvexPolygon
00682     void setContour(const QVector<Vec3d>& acontour) {contour=acontour; updateBoundingCap();}
00683 
00685     const QVector<Vec3d>& getConvexContour() const {return contour;}
00686 
00688     bool checkValid() const;
00689 
00691     static bool checkValidContour(const QVector<Vec3d>& contour);
00692 
00694     static SphericalRegionP deserialize(QDataStream& in);
00695 
00696 protected:
00698     QVector<Vec3d> contour;
00699 
00701     SphericalCap cachedBoundingCap;
00702 
00704     void updateBoundingCap();
00705 
00711     static bool areAllPointsOutsideOneSide(const Vec3d* thisContour, int nbThisContour, const Vec3d* points, int nbPoints);
00712 
00714     bool areAllPointsOutsideOneSide(const QVector<Vec3d>& points) const
00715     {
00716         return areAllPointsOutsideOneSide(contour.constData(), contour.size(), points.constData(), points.size());
00717     }
00718 
00719     bool containsConvexContour(const Vec3d* vertice, int nbVertex) const;
00720 };
00721 
00722 
00725 //class SphericalConvexPolygonSet : public SphericalRegion
00726 //{
00727 //public:
00728 //  // Avoid name hiding when overloading the virtual methods.
00729 //  using SphericalRegion::intersects;
00730 //  using SphericalRegion::contains;
00731 //
00732 //  //! Default constructor.
00733 //  SphericalConvexPolygonSet() {;}
00734 //
00735 //  //! Constructor from a list of contours.
00736 //  SphericalConvexPolygonSet(const QVector<QVector<Vec3d> >& contours);
00737 //
00738 //  virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygonSet;}
00739 //  virtual OctahedronPolygon getOctahedronPolygon() const;
00740 //  virtual StelVertexArray getFillVertexArray() const;
00741 //  virtual StelVertexArray getOutlineVertexArray() const;
00742 //  virtual double getArea() const;
00743 //  virtual bool isEmpty() const;
00744 //  virtual Vec3d getPointInside() const;
00745 //  virtual SphericalCap getBoundingCap() const {return cachedBoundingCap;}
00746 //  QVector<SphericalCap> getBoundingSphericalCaps() const;
00747 //  //! Serialize the region into a QVariant map matching the JSON format.
00748 //  //! The format is
00749 //  //! @code["CONVEX_POLYGON_SET", [[ra,dec], [ra,dec], [ra,dec], [ra,dec]], [[ra,dec], [ra,dec], [ra,dec], [ra,dec]]]@endcode
00750 //  //! where the coords from a list of closed convex contour, with each points defined by ra dec in degree in the ICRS frame.
00751 //  virtual QVariantList toQVariant() const;
00752 //  virtual void serialize(QDataStream& out) const;
00753 //
00754 //  // Contain and intersect
00755 //  virtual bool contains(const Vec3d& p) const;
00756 //  virtual bool contains(const SphericalPolygon& r) const;
00757 //  virtual bool contains(const SphericalConvexPolygon& r) const;
00758 //  virtual bool contains(const SphericalCap& r) const;
00759 //  virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
00760 //  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
00761 //  virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
00762 //  virtual bool intersects(const SphericalPolygon& r) const;
00763 //  virtual bool intersects(const SphericalConvexPolygon& r) const;
00764 //  virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
00765 //  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
00766 //
00767 //  ////////////////////////////////////////////////////////////////////
00768 //  // Methods specific to SphericalConvexPolygonSet
00769 //  ////////////////////////////////////////////////////////////////////
00770 //  //! Deserialize the region. This method must allow as fast as possible deserialization.
00771 //  static SphericalRegionP deserialize(QDataStream& in);
00772 //
00773 //protected:
00774 //  QVector<SphericalConvexPolygon> contours;
00775 //
00776 //  //! Cache the bounding cap.
00777 //  SphericalCap cachedBoundingCap;
00778 //
00779 //  //! Update the bounding cap from the vertex list.
00780 //  void updateBoundingCap();
00781 //};
00782 
00785 class SphericalTexturedPolygon : public SphericalPolygon
00786 {
00787 public:
00790     struct TextureVertex
00791     {
00792         Vec3d vertex;
00793         Vec2f texCoord;
00794     };
00795 
00796     SphericalTexturedPolygon() {;}
00798     SphericalTexturedPolygon(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
00800     SphericalTexturedPolygon(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
00801 
00803     virtual StelVertexArray getFillVertexArray() const {Q_ASSERT(0); return StelVertexArray();}
00811     virtual QVariantList toQVariant() const;
00812     virtual void serialize(QDataStream& out) const {Q_UNUSED(out); Q_ASSERT(0);}
00813 
00815     // Methods specific to SphericalTexturedPolygon
00818     void setContours(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
00819 
00822     void setContour(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
00823 
00824 private:
00827     QVector<Vec2f> textureCoords;
00828 };
00829 
00830 
00831 Q_DECLARE_TYPEINFO(SphericalTexturedPolygon::TextureVertex, Q_PRIMITIVE_TYPE);
00832 
00835 class SphericalTexturedConvexPolygon : public SphericalConvexPolygon
00836 {
00837 public:
00839     SphericalTexturedConvexPolygon() {;}
00840 
00842     SphericalTexturedConvexPolygon(const QVector<Vec3d>& contour, const QVector<Vec2f>& texCoord) {setContour(contour, texCoord);}
00843 
00846     SphericalTexturedConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3) : SphericalConvexPolygon(e0,e1,e2,e3)
00847     {
00848         textureCoords << Vec2f(0.f, 0.f) << Vec2f(1.f, 0.f) << Vec2f(1.f, 1.f) << Vec2f(0.f, 1.f);
00849     }
00850 
00853     virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan, textureCoords);}
00854 
00858     virtual void setContour(const QVector<Vec3d>& acontour, const QVector<Vec2f>& texCoord) {SphericalConvexPolygon::setContour(acontour); textureCoords=texCoord;}
00859 
00866     virtual QVariantList toQVariant() const;
00867 
00868     virtual void serialize(QDataStream& out) const {out << contour << textureCoords;}
00869 
00870 protected:
00873     QVector<Vec2f> textureCoords;
00874 };
00875 
00876 
00880 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& p3, const Vec3d& p4, bool& ok);
00881 
00885 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& nHalfSpace, bool& ok);
00886 
00887 #endif // _STELSPHEREGEOMETRY_HPP_
00888