Stellarium 0.13.1
StelSphereGeometry.hpp
Go to the documentation of this file.
1 /*
2  * Stellarium
3  * Copyright (C) 2009 Fabien Chereau
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELSPHEREGEOMETRY_HPP_
21 #define _STELSPHEREGEOMETRY_HPP_
22 
23 #include "config.h"
24 
25 #include "OctahedronPolygon.hpp"
26 #include "StelVertexArray.hpp"
27 #include "VecMath.hpp"
28 
29 #include <QVector>
30 #include <QVariant>
31 #include <QDebug>
32 #include <QSharedPointer>
33 #include <QVarLengthArray>
34 #include <QDataStream>
35 
36 #include <stdio.h>
37 
38 class SphericalRegion;
39 class SphericalPolygon;
41 class SphericalCap;
42 class SphericalPoint;
45 
48 
51 class SphericalRegionP : public QSharedPointer<SphericalRegion>
52 {
53 public:
54  // Override the constructors of QSharedPointer
55  SphericalRegionP() {;}
56  SphericalRegionP(SphericalRegion* ptr) : QSharedPointer<SphericalRegion>(ptr) {;}
57  template <class Deleter> SphericalRegionP(SphericalRegion* ptr, Deleter deleter) : QSharedPointer<SphericalRegion>(ptr, deleter) {;}
58  SphericalRegionP(const SphericalRegionP& other) : QSharedPointer<SphericalRegion>(other) {;}
59  SphericalRegionP(const QWeakPointer<SphericalRegion>& other) : QSharedPointer<SphericalRegion>(other) {;}
60 
99  static SphericalRegionP loadFromJson(QIODevice* in);
100 
103  static SphericalRegionP loadFromJson(const QByteArray& a);
104 
107  static SphericalRegionP loadFromQVariant(const QVariantMap& map);
108  // It can only be a pure shape definition, without texture coords
109  static SphericalRegionP loadFromQVariant(const QVariantList& list);
110 
112  static void serializeToJson(const QVariant& jsonObject, QIODevice* output, int indentLevel=0);
113 
115  static int metaTypeId;
116 
117 private:
118 
120  static int initialize();
121 };
122 
123 // Allow to use SphericalRegionP with the Qt MetaType system.
124 Q_DECLARE_METATYPE(SphericalRegionP);
125 
127 QDataStream& operator<<(QDataStream& out, const SphericalRegionP& region);
129 QDataStream& operator>>(QDataStream& in, SphericalRegionP& region);
130 
136 {
137 public:
140  {
141  Point = 0,
142  Cap = 1,
143  AllSky = 2,
144  Polygon = 3,
145  ConvexPolygon = 4,
146  Empty = 5,
147  Invalid = 6
148  };
149 
150  virtual ~SphericalRegion() {;}
151 
152  virtual SphericalRegionType getType() const = 0;
153 
156  virtual OctahedronPolygon getOctahedronPolygon() const =0;
157 
159  virtual double getArea() const {return getOctahedronPolygon().getArea();}
160 
162  virtual bool isEmpty() const {return getOctahedronPolygon().isEmpty();}
163 
165  virtual Vec3d getPointInside() const {return getOctahedronPolygon().getPointInside();}
166 
168  virtual QVector<SphericalCap> getBoundingSphericalCaps() const;
169 
172  virtual SphericalCap getBoundingCap() const;
173 
180  virtual SphericalRegionP getEnlarged(double margin) const;
181 
184 
187  virtual StelVertexArray getOutlineVertexArray() const {return getOctahedronPolygon().getOutlineVertexArray();}
188 
191  virtual QVector<QVector<Vec3d > > getSimplifiedContours() const;
192 
194  virtual QVariantList toQVariant() const = 0;
195 
197  virtual void serialize(QDataStream& out) const = 0;
198 
201  QByteArray toJSON() const;
202 
205  bool contains(const SphericalRegion* r) const;
206  bool contains(const SphericalRegionP r) const {return contains(r.data());}
207  virtual bool contains(const Vec3d& p) const {return getOctahedronPolygon().contains(p);}
208  virtual bool contains(const SphericalPolygon& r) const;
209  virtual bool contains(const SphericalConvexPolygon& r) const;
210  virtual bool contains(const SphericalCap& r) const;
211  virtual bool contains(const SphericalPoint& r) const;
212  virtual bool contains(const AllSkySphericalRegion& r) const;
213  bool contains(const EmptySphericalRegion&) const {return false;}
214 
217  bool intersects(const SphericalRegion* r) const;
218  bool intersects(const SphericalRegionP r) const {return intersects(r.data());}
219  bool intersects(const Vec3d& p) const {return contains(p);}
220  virtual bool intersects(const SphericalPolygon& r) const;
221  virtual bool intersects(const SphericalConvexPolygon& r) const;
222  virtual bool intersects(const SphericalCap& r) const;
223  virtual bool intersects(const SphericalPoint& r) const;
224  virtual bool intersects(const AllSkySphericalRegion& r) const;
225  bool intersects(const EmptySphericalRegion&) const {return false;}
226 
230  SphericalRegionP getIntersection(const SphericalRegionP r) const {return getIntersection(r.data());}
231  virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
233  virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
234  virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
237 
240  SphericalRegionP getUnion(const SphericalRegion* r) const;
241  SphericalRegionP getUnion(const SphericalRegionP r) const {return getUnion(r.data());}
242  virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
243  virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
244  virtual SphericalRegionP getUnion(const SphericalCap& r) const;
245  virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
247  virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
248 
252  SphericalRegionP getSubtraction(const SphericalRegionP r) const {return getSubtraction(r.data());}
253  virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
255  virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
256  virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
258  virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
259 
260 private:
261  bool containsDefault(const SphericalRegion* r) const;
262  bool intersectsDefault(const SphericalRegion* r) const;
263  SphericalRegionP getIntersectionDefault(const SphericalRegion* r) const;
264  SphericalRegionP getUnionDefault(const SphericalRegion* r) const;
265  SphericalRegionP getSubtractionDefault(const SphericalRegion* r) const;
266 };
267 
268 
274 {
276 public:
278  SphericalCap() : d(0) {;}
279 
281  SphericalCap(double x, double y, double z) : n(x,y,z), d(0) {;}
282 
286  SphericalCap(const Vec3d& an, double ar) : n(an), d(ar) {//n.normalize();
287  Q_ASSERT(d==0 || std::fabs(n.lengthSquared()-1.)<0.0000001);}
288  // FIXME: GZ reports 2013-03-02: apparently the Q_ASSERT is here because n should be normalized at this point, but
289  // for efficiency n.normalize() should not be called at this point.
290  // However, when zooming in a bit in Hammer-Aitoff and Mercator projections, this Assertion fires.
291  // Atmosphere must be active
292  // It may have to do with DSO texture rendering.
293  // found at r5863.
294  // n.normalize() prevents this for now, but may cost performance.
295  // AARGH - activating n.normalize() inhibits mouse-identification/selection of stars!
296  // May be compiler dependent (seen on Win/MinGW), AW cannot confirm it on Linux.
297 
299  SphericalCap(const SphericalCap& other) : SphericalRegion(), n(other.n), d(other.d) {;}
300 
301  virtual SphericalRegionType getType() const {return SphericalRegion::Cap;}
302  virtual OctahedronPolygon getOctahedronPolygon() const;
303 
305  virtual double getArea() const {return 2.*M_PI*(1.-d);}
306 
308  virtual bool isEmpty() const {return d>=1.;}
309 
311  virtual Vec3d getPointInside() const {return n;}
312 
314  virtual SphericalCap getBoundingCap() const {return *this;}
315 
316  // Contain and intersect
317  virtual bool contains(const Vec3d &v) const {Q_ASSERT(d==0 || std::fabs(v.lengthSquared()-1.)<0.0000002);return (v*n>=d);}
318  virtual bool contains(const Vec3f &v) const {Q_ASSERT(d==0 || std::fabs(v.lengthSquared()-1.f)<0.000002f);return (v[0]*n[0]+v[1]*n[1]+v[2]*n[2]>=d);}
319  virtual bool contains(const SphericalConvexPolygon& r) const;
320  virtual bool contains(const SphericalCap& h) const
321  {
322  const double a = n*h.n-d*h.d;
323  return d<=h.d && ( a>=1. || (a>=0. && a*a >= (1.-d*d)*(1.-h.d*h.d)));
324  }
325  virtual bool contains(const AllSkySphericalRegion&) const {return d<=-1;}
326  virtual bool intersects(const SphericalPolygon& r) const;
327  virtual bool intersects(const SphericalConvexPolygon& r) const;
331  virtual bool intersects(const SphericalCap& h) const
332  {
333  const double a = d*h.d - n*h.n;
334  return d+h.d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d)*(1.-h.d*h.d));
335  }
336  virtual bool intersects(const AllSkySphericalRegion&) const {return d<=1.;}
337 
341  virtual QVariantList toQVariant() const;
342 
343  virtual void serialize(QDataStream& out) const {out << n << d;}
344 
346  // Methods specific to SphericalCap
347 
349  double getRadius() const {return std::acos(d);}
350 
355  inline bool intersectsHalfSpace(double hn0, double hn1, double hn2) const
356  {
357  const double a = n[0]*hn0+n[1]*hn1+n[2]*hn2;
358  return d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d));
359  }
360 
363  bool clipGreatCircle(Vec3d& v1, Vec3d& v2) const;
364 
366  bool operator==(const SphericalCap& other) const {return (n==other.n && d==other.d);}
367 
369  QVector<Vec3d> getClosedOutlineContour() const;
370 
372  bool intersectsConvexContour(const Vec3d* vertice, int nbVertice) const;
373 
375  bool containsTriangle(const Vec3d* vertice) const;
376 
378  bool intersectsTriangle(const Vec3d* vertice) const;
379 
381  static SphericalRegionP deserialize(QDataStream& in);
382 
385  static double relativeAreaOverlap(const SphericalCap& c1, const SphericalCap& c2);
386 
389  static double relativeDiameterOverlap(const SphericalCap& c1, const SphericalCap& c2);
390 
393  static bool intersectionPoints(const SphericalCap& h1, const SphericalCap& h2, Vec3d& p1, Vec3d& p2);
394 
398  double d;
399 };
400 
404 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const Vec3d& p)
405 {
406  return (v1[1] * v2[2] - v1[2] * v2[1])*p[0] +
407  (v1[2] * v2[0] - v1[0] * v2[2])*p[1] +
408  (v1[0] * v2[1] - v1[1] * v2[0])*p[2]>=-1e-17;
409 }
410 
412 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
413 {
414  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]);
415  n.normalize();
416  const double a = n*h.n;
417  return 0<=h.d && ( a>=1. || (a>=0. && a*a >= 1.-h.d*h.d));
418 }
419 
421 inline bool sideHalfSpaceIntersects(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
422 {
423  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]);
424  n.normalize();
425  return h.intersectsHalfSpace(n[0], n[1], n[2]);
426 }
427 
431 {
432 public:
433  SphericalPoint(const Vec3d& an) : n(an) {Q_ASSERT(std::fabs(1.-n.length())<0.0000001);}
434  virtual ~SphericalPoint() {;}
435 
436  virtual SphericalRegionType getType() const {return SphericalRegion::Point;}
437  virtual OctahedronPolygon getOctahedronPolygon() const;
438  virtual double getArea() const {return 0.;}
439  virtual bool isEmpty() const {return false;}
440  virtual Vec3d getPointInside() const {return n;}
441  virtual SphericalCap getBoundingCap() const {return SphericalCap(n, 1);}
444  virtual QVariantList toQVariant() const;
445  virtual void serialize(QDataStream& out) const {out << n;}
446 
447  // Contain and intersect
448  virtual bool contains(const Vec3d& p) const {return n==p;}
449  virtual bool contains(const SphericalPolygon&) const {return false;}
450  virtual bool contains(const SphericalConvexPolygon&) const {return false;}
451  virtual bool contains(const SphericalCap&) const {return false;}
452  virtual bool contains(const SphericalPoint& r) const {return n==r.n;}
453  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
454  virtual bool intersects(const SphericalPolygon&) const;
455  virtual bool intersects(const SphericalConvexPolygon&) const;
456  virtual bool intersects(const SphericalCap& r) const {return r.contains(n);}
457  virtual bool intersects(const SphericalPoint& r) const {return n==r.n;}
458  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
459 
461  static SphericalRegionP deserialize(QDataStream& in);
462 
465 };
466 
470 {
471 public:
472  virtual ~AllSkySphericalRegion() {;}
473 
474  virtual SphericalRegionType getType() const {return SphericalRegion::AllSky;}
475  virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getAllSkyOctahedronPolygon();}
476  virtual double getArea() const {return 4.*M_PI;}
477  virtual bool isEmpty() const {return false;}
478  virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
479  virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), -2);}
482  virtual QVariantList toQVariant() const;
483  virtual void serialize(QDataStream&) const {;}
484 
485  // Contain and intersect
486  virtual bool contains(const Vec3d&) const {return true;}
487  virtual bool contains(const SphericalPolygon&) const {return true;}
488  virtual bool contains(const SphericalConvexPolygon&) const {return true;}
489  virtual bool contains(const SphericalCap&) const {return true;}
490  virtual bool contains(const SphericalPoint&) const {return true;}
491  virtual bool contains(const AllSkySphericalRegion&) const {return true;}
492  virtual bool intersects(const SphericalPolygon&) const {return true;}
493  virtual bool intersects(const SphericalConvexPolygon&) const {return true;}
494  virtual bool intersects(const SphericalCap&) const {return true;}
495  virtual bool intersects(const SphericalPoint&) const {return true;}
496  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
497 
498  static const SphericalRegionP staticInstance;
499 };
500 
504 {
505 public:
506  // Avoid name hiding when overloading the virtual methods.
512 
514  virtual ~EmptySphericalRegion() {;}
515 
516  virtual SphericalRegionType getType() const {return SphericalRegion::Empty;}
517  virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getEmptyOctahedronPolygon();}
518  virtual double getArea() const {return 0.;}
519  virtual bool isEmpty() const {return true;}
520  virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
521  virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), 2);}
524  virtual QVariantList toQVariant() const;
525  virtual void serialize(QDataStream&) const {;}
526 
527  // Contain and intersect
528  virtual bool contains(const Vec3d&) const {return false;}
529  virtual bool contains(const SphericalPolygon&) const {return false;}
530  virtual bool contains(const SphericalConvexPolygon&) const {return false;}
531  virtual bool contains(const SphericalCap&) const {return false;}
532  virtual bool contains(const SphericalPoint&) const {return false;}
533  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
534  virtual bool intersects(const SphericalPolygon&) const {return false;}
535  virtual bool intersects(const SphericalConvexPolygon&) const {return false;}
536  virtual bool intersects(const SphericalCap&) const {return false;}
537  virtual bool intersects(const SphericalPoint&) const {return false;}
538  virtual bool intersects(const AllSkySphericalRegion&) const {return false;}
539 
540  static const SphericalRegionP staticInstance;
541 };
542 
543 
548 {
549 public:
550  // Avoid name hiding when overloading the virtual methods.
556 
557  SphericalPolygon() {;}
559  SphericalPolygon(const QVector<QVector<Vec3d> >& contours) : octahedronPolygon(contours) {;}
561  SphericalPolygon(const QVector<Vec3d>& contour) : octahedronPolygon(contour) {;}
562  SphericalPolygon(const OctahedronPolygon& octContour) : octahedronPolygon(octContour) {;}
563  SphericalPolygon(const QList<OctahedronPolygon>& octContours) : octahedronPolygon(octContours) {;}
564 
565  virtual SphericalRegionType getType() const {return SphericalRegion::Polygon;}
566  virtual OctahedronPolygon getOctahedronPolygon() const {return octahedronPolygon;}
567 
572  virtual QVariantList toQVariant() const;
573  virtual void serialize(QDataStream& out) const;
574 
575  virtual SphericalCap getBoundingCap() const;
576 
577  virtual bool contains(const Vec3d& p) const {return octahedronPolygon.contains(p);}
578  virtual bool contains(const SphericalPolygon& r) const {return octahedronPolygon.contains(r.octahedronPolygon);}
579  virtual bool contains(const SphericalConvexPolygon& r) const;
580  virtual bool contains(const SphericalCap& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
581  virtual bool contains(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
582  virtual bool contains(const AllSkySphericalRegion& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
583 
584  virtual bool intersects(const SphericalPolygon& r) const {return octahedronPolygon.intersects(r.octahedronPolygon);}
585  virtual bool intersects(const SphericalConvexPolygon& r) const;
586  virtual bool intersects(const SphericalCap& r) const {return r.intersects(*this);}
587  virtual bool intersects(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
588  virtual bool intersects(const AllSkySphericalRegion&) const {return !isEmpty();}
589 
590  virtual SphericalRegionP getIntersection(const SphericalPoint& r) const {return contains(r.n) ? SphericalRegionP(new SphericalPoint(r)) : EmptySphericalRegion::staticInstance;}
591  virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& ) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
592 
593  virtual SphericalRegionP getUnion(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
594  virtual SphericalRegionP getUnion(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
595 
596  virtual SphericalRegionP getSubtraction(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
597  virtual SphericalRegionP getSubtraction(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
598 
600  // Methods specific to SphericalPolygon
604  void setContours(const QVector<QVector<Vec3d> >& contours) {octahedronPolygon = OctahedronPolygon(contours);}
605 
608  void setContour(const QVector<Vec3d>& contour) {octahedronPolygon = OctahedronPolygon(contour);}
609 
611  QVector<QVector<Vec3d> > getClosedOutlineContours() const {Q_ASSERT(0); return QVector<QVector<Vec3d> >();}
612 
614  static SphericalRegionP deserialize(QDataStream& in);
615 
617  static SphericalRegionP multiUnion(const QList<SphericalRegionP>& regions, bool optimizeByPreGrouping=false);
618 
620  static SphericalRegionP multiIntersection(const QList<SphericalRegionP>& regions);
621 
622 private:
623  OctahedronPolygon octahedronPolygon;
624 };
625 
626 
630 {
631 public:
632  // Avoid name hiding when overloading the virtual methods.
635 
638 
640  SphericalConvexPolygon(const QVector<QVector<Vec3d> >& contours) {Q_ASSERT(contours.size()==1); setContour(contours.at(0));}
642  SphericalConvexPolygon(const QVector<Vec3d>& contour) {setContour(contour);}
644  SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2) {contour << e0 << e1 << e2; updateBoundingCap();}
646  SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3) {contour << e0 << e1 << e2 << e3; updateBoundingCap();}
647 
648  virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygon;}
650  virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan);}
651  virtual StelVertexArray getOutlineVertexArray() const {return StelVertexArray(contour, StelVertexArray::LineLoop);}
652  virtual double getArea() const;
653  virtual bool isEmpty() const {return contour.isEmpty();}
654  virtual Vec3d getPointInside() const;
656  QVector<SphericalCap> getBoundingSphericalCaps() const;
661  virtual QVariantList toQVariant() const;
662  virtual void serialize(QDataStream& out) const {out << contour;}
663 
664  // Contain and intersect
665  virtual bool contains(const Vec3d& p) const;
666  virtual bool contains(const SphericalPolygon& r) const;
667  virtual bool contains(const SphericalConvexPolygon& r) const;
668  virtual bool contains(const SphericalCap& r) const;
669  virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
670  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
671  virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
672  virtual bool intersects(const SphericalPolygon& r) const;
673  virtual bool intersects(const SphericalConvexPolygon& r) const;
674  virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
675  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
676 
678 // virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
679 // virtual SphericalRegionP getIntersection(const SphericalConvexPolygon& r) const;
680 // virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
681 // virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
682 // virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& r) const;
683 // virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
684 // virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
685 // virtual SphericalRegionP getUnion(const SphericalCap& r) const;
686 // virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
687 // virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
688 // virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
689 // virtual SphericalRegionP getSubtraction(const SphericalConvexPolygon& r) const;
690 // virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
691 // virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
692 // virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
693 
695  // Methods specific to SphericalConvexPolygon
699  void setContour(const QVector<Vec3d>& acontour) {contour=acontour; updateBoundingCap();}
700 
702  const QVector<Vec3d>& getConvexContour() const {return contour;}
703 
705  bool checkValid() const;
706 
708  static bool checkValidContour(const QVector<Vec3d>& contour);
709 
711  static SphericalRegionP deserialize(QDataStream& in);
712 
713 protected:
715  QVector<Vec3d> contour;
716 
719 
721  void updateBoundingCap();
722 
728  static bool areAllPointsOutsideOneSide(const Vec3d* thisContour, int nbThisContour, const Vec3d* points, int nbPoints);
729 
731  bool areAllPointsOutsideOneSide(const QVector<Vec3d>& points) const
732  {
733  return areAllPointsOutsideOneSide(contour.constData(), contour.size(), points.constData(), points.size());
734  }
735 
736  bool containsConvexContour(const Vec3d* vertice, int nbVertex) const;
737 };
738 
739 
740 // ! @class SphericalConvexPolygonSet
741 // ! A special case of SphericalPolygon for which the polygon is composed of disjoint convex polygons.
742 //class SphericalConvexPolygonSet : public SphericalRegion
743 //{
744 //public:
745 // // Avoid name hiding when overloading the virtual methods.
746 // using SphericalRegion::intersects;
747 // using SphericalRegion::contains;
748 //
749 // //! Default constructor.
750 // SphericalConvexPolygonSet() {;}
751 //
752 // //! Constructor from a list of contours.
753 // SphericalConvexPolygonSet(const QVector<QVector<Vec3d> >& contours);
754 //
755 // virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygonSet;}
756 // virtual OctahedronPolygon getOctahedronPolygon() const;
757 // virtual StelVertexArray getFillVertexArray() const;
758 // virtual StelVertexArray getOutlineVertexArray() const;
759 // virtual double getArea() const;
760 // virtual bool isEmpty() const;
761 // virtual Vec3d getPointInside() const;
762 // virtual SphericalCap getBoundingCap() const {return cachedBoundingCap;}
763 // QVector<SphericalCap> getBoundingSphericalCaps() const;
764 // //! Serialize the region into a QVariant map matching the JSON format.
765 // //! The format is
766 // //! @code["CONVEX_POLYGON_SET", [[ra,dec], [ra,dec], [ra,dec], [ra,dec]], [[ra,dec], [ra,dec], [ra,dec], [ra,dec]]]@endcode
767 // //! where the coords from a list of closed convex contour, with each points defined by ra dec in degree in the ICRS frame.
768 // virtual QVariantList toQVariant() const;
769 // virtual void serialize(QDataStream& out) const;
770 //
771 // // Contain and intersect
772 // virtual bool contains(const Vec3d& p) const;
773 // virtual bool contains(const SphericalPolygon& r) const;
774 // virtual bool contains(const SphericalConvexPolygon& r) const;
775 // virtual bool contains(const SphericalCap& r) const;
776 // virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
777 // virtual bool contains(const AllSkySphericalRegion&) const {return false;}
778 // virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
779 // virtual bool intersects(const SphericalPolygon& r) const;
780 // virtual bool intersects(const SphericalConvexPolygon& r) const;
781 // virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
782 // virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
783 //
784 // ////////////////////////////////////////////////////////////////////
785 // // Methods specific to SphericalConvexPolygonSet
786 // ////////////////////////////////////////////////////////////////////
787 // //! Deserialize the region. This method must allow as fast as possible deserialization.
788 // static SphericalRegionP deserialize(QDataStream& in);
789 //
790 //protected:
791 // QVector<SphericalConvexPolygon> contours;
792 //
793 // //! Cache the bounding cap.
794 // SphericalCap cachedBoundingCap;
795 //
796 // //! Update the bounding cap from the vertex list.
797 // void updateBoundingCap();
798 //};
799 
803 {
804 public:
808  {
809  Vec3d vertex;
810  Vec2f texCoord;
811  };
812 
815  SphericalTexturedPolygon(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
817  SphericalTexturedPolygon(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
818 
820  virtual StelVertexArray getFillVertexArray() const {Q_ASSERT(0); return StelVertexArray();}
828  virtual QVariantList toQVariant() const;
829  virtual void serialize(QDataStream& out) const {Q_UNUSED(out); Q_ASSERT(0);}
830 
832  // Methods specific to SphericalTexturedPolygon
835  void setContours(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
836 
839  void setContour(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
840 
841 private:
844  QVector<Vec2f> textureCoords;
845 };
846 
847 
848 Q_DECLARE_TYPEINFO(SphericalTexturedPolygon::TextureVertex, Q_PRIMITIVE_TYPE);
849 
853 {
854 public:
857 
859  SphericalTexturedConvexPolygon(const QVector<Vec3d>& contour, const QVector<Vec2f>& texCoord) {setContour(contour, texCoord);}
860 
863  SphericalTexturedConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3) : SphericalConvexPolygon(e0,e1,e2,e3)
864  {
865  textureCoords << Vec2f(0.f, 0.f) << Vec2f(1.f, 0.f) << Vec2f(1.f, 1.f) << Vec2f(0.f, 1.f);
866  }
867 
870  virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan, textureCoords);}
871 
875  virtual void setContour(const QVector<Vec3d>& acontour, const QVector<Vec2f>& texCoord) {SphericalConvexPolygon::setContour(acontour); textureCoords=texCoord;}
876 
883  virtual QVariantList toQVariant() const;
884 
885  virtual void serialize(QDataStream& out) const {out << contour << textureCoords;}
886 
887 protected:
890  QVector<Vec2f> textureCoords;
891 };
892 
893 
897 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& p3, const Vec3d& p4, bool& ok);
898 
902 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& nHalfSpace, bool& ok);
903 
904 #endif // _STELSPHEREGEOMETRY_HPP_
905 
SphericalConvexPolygon(const Vec3d &e0, const Vec3d &e1, const Vec3d &e2, const Vec3d &e3)
Special constructor for quads.
bool containsTriangle(const Vec3d *vertice) const
Return whether the cap contains the passed triangle.
static SphericalRegionP deserialize(QDataStream &in)
Deserialize the region. This method must allow as fast as possible deserialization.
SphericalCap cachedBoundingCap
Cache the bounding cap.
Special SphericalRegion for the whole sphere.
bool areAllPointsOutsideOneSide(const QVector< Vec3d > &points) const
Computes whether the passed points are all outside of at least one SphericalCap defining the polygon ...
virtual SphericalRegionP getEnlarged(double margin) const
Return an enlarged version of this SphericalRegion so that any point distant of more than the given m...
virtual StelVertexArray getFillVertexArray() const
Return an openGL compatible array to be displayed using vertex arrays.
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.
virtual Vec3d getPointInside() const
Return a point located inside the region.
bool clipGreatCircle(Vec3d &v1, Vec3d &v2) const
Clip the passed great circle connecting points v1 and v2.
static bool areAllPointsOutsideOneSide(const Vec3d *thisContour, int nbThisContour, const Vec3d *points, int nbPoints)
Computes whether the passed points are all outside of at least one SphericalCap defining the polygon ...
virtual double getArea() const
Get the area of the intersection of the halfspace on the sphere in steradian.
double d
The cos of cone radius.
virtual StelVertexArray getFillVertexArray() const
Return an openGL compatible array to be displayed using vertex arrays.
SphericalConvexPolygon()
Default constructor.
SphericalConvexPolygon(const Vec3d &e0, const Vec3d &e1, const Vec3d &e2)
Special constructor for triangle.
virtual Vec3d getPointInside() const
Return a point located inside the SphericalCap.
SphericalRegionP getIntersection(const SphericalRegion *r) const
Return a new SphericalRegion consisting of the intersection of this and the given region...
SphericalRegionType
define types for all supported regions.
double getRadius() const
Return the radiusof the cap in radian.
virtual double getArea() const
Return the area of the region in steradians.
QVector< Vec3d > contour
A list of vertices of the convex contour.
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
SphericalConvexPolygon(const QVector< QVector< Vec3d > > &contours)
Constructor from a list of contours.
SphericalTexturedPolygon(const QVector< TextureVertex > &contour)
Constructor from one contour.
virtual bool isEmpty() const
Return true if the region is empty.
bool checkValid() const
Check if the polygon is valid, i.e. it has no side >180.
virtual bool isEmpty() const
Return true if the region is empty.
A shared pointer on a SphericalRegion.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
static int metaTypeId
The meta type ID associated to a SphericalRegionP.
static SphericalRegionP multiIntersection(const QList< SphericalRegionP > &regions)
Create a new SphericalRegionP which is the intersection of all the passed ones.
virtual void serialize(QDataStream &out) const =0
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
virtual void serialize(QDataStream &) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
A SphericalCap is defined by a direction and an aperture.
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
static void serializeToJson(const QVariant &jsonObject, QIODevice *output, int indentLevel=0)
Method registered to JSON serializer.
static SphericalRegionP loadFromJson(QIODevice *in)
Create a SphericalRegion from the given input JSON stream.
virtual Vec3d getPointInside() const
Return a point located inside the region.
static bool intersectionPoints(const SphericalCap &h1, const SphericalCap &h2, Vec3d &p1, Vec3d &p2)
Compute the intersection of 2 halfspaces on the sphere (usually on 2 points) and return it in p1 and ...
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
QByteArray toJSON() const
Output a JSON string representing the polygon.
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
Special SphericalRegion for — UMM, WHAT EXACTLY?
SphericalRegionP getUnion(const SphericalRegion *r) const
Return a new SphericalRegion consisting of the union of this and the given region.
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
static SphericalRegionP multiUnion(const QList< SphericalRegionP > &regions, bool optimizeByPreGrouping=false)
Create a new SphericalRegionP which is the union of all the passed ones.
Manage a non-convex polygon which can extends on more than 180 deg.
virtual bool isEmpty() const
Return true if the region is empty.
virtual bool isEmpty() const
Return true if the region is empty.
StelVertexArray getFillVertexArray() const
Returns the list of triangles resulting from tesselating the contours.
virtual StelVertexArray getOutlineVertexArray() const
Get the outline of the contours defining the SphericalPolygon.
Vec3d n
The direction unit vector. Only if d==0, this vector doesn't need to be unit.
Class defining default implementations for some spherical geometry methods.
Special SphericalRegion for a point on the sphere.
virtual double getArea() const
Return the area of the region in steradians.
virtual bool intersects(const SphericalCap &h) const
Returns whether a SphericalCap intersects with this one.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
static SphericalRegionP deserialize(QDataStream &in)
Deserialize the region. This method must allow as fast as possible deserialization.
SphericalCap(double x, double y, double z)
Construct a SphericalCap from its direction and assumes a 90 deg aperture.
virtual Vec3d getPointInside() const
Return a point located inside the region.
SphericalRegionP getSubtraction(const SphericalRegion *r) const
Return a new SphericalRegion consisting of the subtraction of the given region from this...
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.
bool contains(const SphericalRegion *r) const
Returns whether a SphericalRegion is contained into this region.
static double relativeDiameterOverlap(const SphericalCap &c1, const SphericalCap &c2)
Return the relative overlap between the diameter of the 2 caps, i.e: min(intersectionDistance/c1.diameter, intersectionDistance/c2.diameter)
A special case of SphericalPolygon for which the polygon is convex.
QDataStream & operator<<(QDataStream &out, const SphericalRegionP &region)
Serialize the passed SphericalRegionP into a binary blob.
virtual bool isEmpty() const
Return true if the region is empty.
void setContours(const QVector< QVector< TextureVertex > > &contours)
Set the contours defining the SphericalPolygon.
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.
virtual QVector< SphericalCap > getBoundingSphericalCaps() const
Return the list of SphericalCap bounding the ConvexPolygon.
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
void setContour(const QVector< TextureVertex > &contour)
Set a single contour defining the SphericalPolygon.
static bool checkValidContour(const QVector< Vec3d > &contour)
Check if the passed contour is convex and valid, i.e. it has no side >180.
static SphericalRegionP loadFromQVariant(const QVariantMap &map)
Create a SphericalRegion from the given QVariantMap with a format matching the JSON file parsed in lo...
static SphericalRegionP deserialize(QDataStream &in)
Deserialize the region. This method must allow as fast as possible deserialization.
virtual Vec3d getPointInside() const
Return a point located inside the region.
virtual Vec3d getPointInside() const
Return a point located inside the region.
Vec3d n
The unit vector of the point direction.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
Vec3d greatCircleIntersection(const Vec3d &p1, const Vec3d &p2, const Vec3d &p3, const Vec3d &p4, bool &ok)
Compute the intersection of 2 great circles segments.
void setContour(const QVector< Vec3d > &acontour)
Set a single contour defining the SphericalPolygon.
QVector< SphericalCap > getBoundingSphericalCaps() const
Return the list of SphericalCap bounding the ConvexPolygon.
virtual OctahedronPolygon getOctahedronPolygon() const =0
Return the octahedron contour representation of the polygon.
SphericalTexturedConvexPolygon(const Vec3d &e0, const Vec3d &e1, const Vec3d &e2, const Vec3d &e3)
Special constructor for quads.
Abstract class defining a region of the sphere.
virtual void serialize(QDataStream &) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
QVector< Vec3d > getClosedOutlineContour() const
Return the list of closed contours defining the polygon boundaries.
bool intersects(const SphericalRegion *r) const
Returns whether a SphericalRegion intersects with this region.
SphericalPolygon(const QVector< Vec3d > &contour)
Constructor from one contour.
SphericalCap(const SphericalCap &other)
Copy constructor.
An extension of SphericalPolygon with addition of texture coordinates.
QVector< QVector< Vec3d > > getClosedOutlineContours() const
Return the list of closed contours defining the polygon boundaries.
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.
virtual SphericalCap getBoundingCap() const
Return itself.
bool sideHalfSpaceIntersects(const Vec3d &v1, const Vec3d &v2, const SphericalCap &h)
Return whether the halfspace defined by the vectors v1 and v2 intersects the SphericalCap h...
static SphericalRegionP deserialize(QDataStream &in)
Deserialize the region. This method must allow as fast as possible deserialization.
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
QDataStream & operator>>(QDataStream &in, SphericalRegionP &region)
Load the SphericalRegionP from a binary blob.
bool intersectsConvexContour(const Vec3d *vertice, int nbVertice) const
Return whether the cap intersect with a convex contour defined by nbVertice.
SphericalConvexPolygon(const QVector< Vec3d > &contour)
Constructor from one contour.
bool intersectsHalfSpace(double hn0, double hn1, double hn2) const
Returns whether a HalfSpace (like a SphericalCap with d=0) intersects with this SphericalCap.
SphericalTexturedPolygon(const QVector< QVector< TextureVertex > > &contours)
Constructor from a list of contours.
SphericalCap(const Vec3d &an, double ar)
Construct a SphericalCap from its direction and aperture.
bool sideHalfSpaceContains(const Vec3d &v1, const Vec3d &v2, const Vec3d &p)
Return whether the halfspace defined by the vectors v1^v2 and with aperture 90 deg contains the point...
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
SphericalTexturedConvexPolygon(const QVector< Vec3d > &contour, const QVector< Vec2f > &texCoord)
Constructor from one contour.
SphericalTexturedConvexPolygon()
Default constructor.
virtual double getArea() const
Return the area of the region in steradians.
const QVector< Vec3d > & getConvexContour() const
Get the single contour defining the SphericalConvexPolygon.
bool intersectsTriangle(const Vec3d *vertice) const
Return whether the cap intersect with the passed triangle.
virtual double getArea() const
Return the area of the region in steradians.
SphericalPolygon(const QVector< QVector< Vec3d > > &contours)
Constructor from a list of contours.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
void setContour(const QVector< Vec3d > &contour)
Set a single contour defining the SphericalPolygon.
virtual QVector< QVector< Vec3d > > getSimplifiedContours() const
Get the contours defining the SphericalPolygon when combined using a positive winding rule...
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
A container for 3D vertex + associated texture coordinates.
bool operator==(const SphericalCap &other) const
Comparison operator.
virtual bool isEmpty() const
Return true if the region is empty.
virtual StelVertexArray getFillVertexArray() const
Return an openGL compatible array of texture coords to be used using vertex arrays.
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
SphericalCap()
Construct a SphericalCap with a 90 deg aperture and an undefined direction.
static double relativeAreaOverlap(const SphericalCap &c1, const SphericalCap &c2)
Return the relative overlap between the areas of the 2 caps, i.e: min(intersectionArea/c1.area, intersectionArea/c2.area)
virtual void setContour(const QVector< Vec3d > &acontour, const QVector< Vec2f > &texCoord)
Set a single contour defining the SphericalPolygon.
virtual QVariantList toQVariant() const
Serialize the region into a QVariant map matching the JSON format.
virtual double getArea() const
Return the area of the region in steradians.
virtual StelVertexArray getFillVertexArray() const
Return an openGL compatible array to be displayed using vertex arrays.
void setContours(const QVector< QVector< Vec3d > > &contours)
Set the contours defining the SphericalPolygon.
virtual StelVertexArray getOutlineVertexArray() const
Get the outline of the contours defining the SphericalPolygon.
virtual QVariantList toQVariant() const =0
Serialize the region into a QVariant list matching the JSON format.
void updateBoundingCap()
Update the bounding cap from the vertex list.
virtual void serialize(QDataStream &out) const
Serialize the region. This method must allow as fast as possible serialization and work with deserial...
Extension of SphericalConvexPolygon for textured polygon.
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.
QVector< Vec2f > textureCoords
A list of uv textures coordinates corresponding to the triangle vertices.
virtual SphericalCap getBoundingCap() const
Return a bounding SphericalCap.