Stellarium 0.15.2
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 "OctahedronPolygon.hpp"
24 #include "StelVertexArray.hpp"
25 #include "VecMath.hpp"
26 
27 #include <QVector>
28 #include <QVariant>
29 #include <QDebug>
30 #include <QSharedPointer>
31 #include <QVarLengthArray>
32 #include <QDataStream>
33 
34 #include <stdio.h>
35 
36 class SphericalRegion;
37 class SphericalPolygon;
39 class SphericalCap;
40 class SphericalPoint;
43 
46 
49 class SphericalRegionP : public QSharedPointer<SphericalRegion>
50 {
51 public:
52  // Override the constructors of QSharedPointer
53  SphericalRegionP() {;}
54  SphericalRegionP(SphericalRegion* ptr) : QSharedPointer<SphericalRegion>(ptr) {;}
55  template <class Deleter> SphericalRegionP(SphericalRegion* ptr, Deleter deleter) : QSharedPointer<SphericalRegion>(ptr, deleter) {;}
56  SphericalRegionP(const SphericalRegionP& other) : QSharedPointer<SphericalRegion>(other) {;}
57  SphericalRegionP(const QWeakPointer<SphericalRegion>& other) : QSharedPointer<SphericalRegion>(other) {;}
58 
97  static SphericalRegionP loadFromJson(QIODevice* in);
98 
101  static SphericalRegionP loadFromJson(const QByteArray& a);
102 
105  static SphericalRegionP loadFromQVariant(const QVariantMap& map);
106  // It can only be a pure shape definition, without texture coords
107  static SphericalRegionP loadFromQVariant(const QVariantList& list);
108 
110  static void serializeToJson(const QVariant& jsonObject, QIODevice* output, int indentLevel=0);
111 
113  static int metaTypeId;
114 
115 private:
116 
118  static int initialize();
119 };
120 
121 // Allow to use SphericalRegionP with the Qt MetaType system.
122 Q_DECLARE_METATYPE(SphericalRegionP)
123 
124 QDataStream& operator<<(QDataStream& out, const SphericalRegionP& region);
127 QDataStream& operator>>(QDataStream& in, SphericalRegionP& region);
128 
134 {
135 public:
138  {
139  Point = 0,
140  Cap = 1,
141  AllSky = 2,
142  Polygon = 3,
143  ConvexPolygon = 4,
144  Empty = 5,
145  Invalid = 6
146  };
147 
148  virtual ~SphericalRegion() {;}
149 
150  virtual SphericalRegionType getType() const = 0;
151 
154  virtual OctahedronPolygon getOctahedronPolygon() const =0;
155 
157  virtual double getArea() const {return getOctahedronPolygon().getArea();}
158 
160  virtual bool isEmpty() const {return getOctahedronPolygon().isEmpty();}
161 
163  virtual Vec3d getPointInside() const {return getOctahedronPolygon().getPointInside();}
164 
166  virtual QVector<SphericalCap> getBoundingSphericalCaps() const;
167 
170  virtual SphericalCap getBoundingCap() const;
171 
178  virtual SphericalRegionP getEnlarged(double margin) const;
179 
181  virtual StelVertexArray getFillVertexArray() const {return getOctahedronPolygon().getFillVertexArray();}
182 
185  virtual StelVertexArray getOutlineVertexArray() const {return getOctahedronPolygon().getOutlineVertexArray();}
186 
189  virtual QVector<QVector<Vec3d > > getSimplifiedContours() const;
190 
192  virtual QVariantList toQVariant() const = 0;
193 
195  virtual void serialize(QDataStream& out) const = 0;
196 
199  QByteArray toJSON() const;
200 
203  bool contains(const SphericalRegion* r) const;
204  bool contains(const SphericalRegionP r) const {return contains(r.data());}
205  virtual bool contains(const Vec3d& p) const {return getOctahedronPolygon().contains(p);}
206  virtual bool contains(const SphericalPolygon& r) const;
207  virtual bool contains(const SphericalConvexPolygon& r) const;
208  virtual bool contains(const SphericalCap& r) const;
209  virtual bool contains(const SphericalPoint& r) const;
210  virtual bool contains(const AllSkySphericalRegion& r) const;
211  bool contains(const EmptySphericalRegion&) const {return false;}
212 
215  bool intersects(const SphericalRegion* r) const;
216  bool intersects(const SphericalRegionP r) const {return intersects(r.data());}
217  bool intersects(const Vec3d& p) const {return contains(p);}
218  virtual bool intersects(const SphericalPolygon& r) const;
219  virtual bool intersects(const SphericalConvexPolygon& r) const;
220  virtual bool intersects(const SphericalCap& r) const;
221  virtual bool intersects(const SphericalPoint& r) const;
222  virtual bool intersects(const AllSkySphericalRegion& r) const;
223  bool intersects(const EmptySphericalRegion&) const {return false;}
224 
227  SphericalRegionP getIntersection(const SphericalRegion* r) const;
228  SphericalRegionP getIntersection(const SphericalRegionP r) const {return getIntersection(r.data());}
229  virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
230  virtual SphericalRegionP getIntersection(const SphericalConvexPolygon& r) const;
231  virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
232  virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
233  virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& r) const;
234  SphericalRegionP getIntersection(const EmptySphericalRegion& r) const;
235 
238  SphericalRegionP getUnion(const SphericalRegion* r) const;
239  SphericalRegionP getUnion(const SphericalRegionP r) const {return getUnion(r.data());}
240  virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
241  virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
242  virtual SphericalRegionP getUnion(const SphericalCap& r) const;
243  virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
244  SphericalRegionP getUnion(const AllSkySphericalRegion& r) const;
245  virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
246 
249  SphericalRegionP getSubtraction(const SphericalRegion* r) const;
250  SphericalRegionP getSubtraction(const SphericalRegionP r) const {return getSubtraction(r.data());}
251  virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
252  virtual SphericalRegionP getSubtraction(const SphericalConvexPolygon& r) const;
253  virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
254  virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
255  SphericalRegionP getSubtraction(const AllSkySphericalRegion& r) const;
256  virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
257 
258 private:
259  bool containsDefault(const SphericalRegion* r) const;
260  bool intersectsDefault(const SphericalRegion* r) const;
261  SphericalRegionP getIntersectionDefault(const SphericalRegion* r) const;
262  SphericalRegionP getUnionDefault(const SphericalRegion* r) const;
263  SphericalRegionP getSubtractionDefault(const SphericalRegion* r) const;
264 };
265 
266 
272 {
274 public:
276  SphericalCap() : d(0) {;}
277 
279  SphericalCap(double x, double y, double z) : n(x,y,z), d(0) {;}
280 
284  SphericalCap(const Vec3d& an, double ar) : n(an), d(ar) {//n.normalize();
285  Q_ASSERT(d==0 || std::fabs(n.lengthSquared()-1.)<0.0000001);}
286  // FIXME: GZ reports 2013-03-02: apparently the Q_ASSERT is here because n should be normalized at this point, but
287  // for efficiency n.normalize() should not be called at this point.
288  // However, when zooming in a bit in Hammer-Aitoff and Mercator projections, this Assertion fires.
289  // Atmosphere must be active
290  // It may have to do with DSO texture rendering.
291  // found at r5863.
292  // n.normalize() prevents this for now, but may cost performance.
293  // AARGH - activating n.normalize() inhibits mouse-identification/selection of stars!
294  // May be compiler dependent (seen on Win/MinGW), AW cannot confirm it on Linux.
295 
297  SphericalCap(const SphericalCap& other) : SphericalRegion(), n(other.n), d(other.d) {;}
298 
299  virtual SphericalRegionType getType() const {return SphericalRegion::Cap;}
300  virtual OctahedronPolygon getOctahedronPolygon() const;
301 
303  virtual double getArea() const {return 2.*M_PI*(1.-d);}
304 
306  virtual bool isEmpty() const {return d>=1.;}
307 
309  virtual Vec3d getPointInside() const {return n;}
310 
312  virtual SphericalCap getBoundingCap() const {return *this;}
313 
314  // Contain and intersect
315  virtual bool contains(const Vec3d &v) const {Q_ASSERT(d==0 || std::fabs(v.lengthSquared()-1.)<0.0000002);return (v*n>=d);}
316  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);}
317  virtual bool contains(const SphericalConvexPolygon& r) const;
318  virtual bool contains(const SphericalCap& h) const
319  {
320  const double a = n*h.n-d*h.d;
321  return d<=h.d && ( a>=1. || (a>=0. && a*a >= (1.-d*d)*(1.-h.d*h.d)));
322  }
323  virtual bool contains(const AllSkySphericalRegion&) const {return d<=-1;}
324  virtual bool intersects(const SphericalPolygon& r) const;
325  virtual bool intersects(const SphericalConvexPolygon& r) const;
329  virtual bool intersects(const SphericalCap& h) const
330  {
331  const double a = d*h.d - n*h.n;
332  return d+h.d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d)*(1.-h.d*h.d));
333  }
334  virtual bool intersects(const AllSkySphericalRegion&) const {return d<=1.;}
335 
339  virtual QVariantList toQVariant() const;
340 
341  virtual void serialize(QDataStream& out) const {out << n << d;}
342 
344  // Methods specific to SphericalCap
345 
347  double getRadius() const {return std::acos(d);}
348 
353  inline bool intersectsHalfSpace(double hn0, double hn1, double hn2) const
354  {
355  const double a = n[0]*hn0+n[1]*hn1+n[2]*hn2;
356  return d<=0. || a<=0. || (a<=1. && a*a <= (1.-d*d));
357  }
358 
361  bool clipGreatCircle(Vec3d& v1, Vec3d& v2) const;
362 
364  bool operator==(const SphericalCap& other) const {return (n==other.n && d==other.d);}
365 
367  QVector<Vec3d> getClosedOutlineContour() const;
368 
370  bool intersectsConvexContour(const Vec3d* vertice, int nbVertice) const;
371 
373  bool containsTriangle(const Vec3d* vertice) const;
374 
376  bool intersectsTriangle(const Vec3d* vertice) const;
377 
379  static SphericalRegionP deserialize(QDataStream& in);
380 
383  static double relativeAreaOverlap(const SphericalCap& c1, const SphericalCap& c2);
384 
387  static double relativeDiameterOverlap(const SphericalCap& c1, const SphericalCap& c2);
388 
391  static bool intersectionPoints(const SphericalCap& h1, const SphericalCap& h2, Vec3d& p1, Vec3d& p2);
392 
396  double d;
397 };
398 
402 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const Vec3d& p)
403 {
404  return (v1[1] * v2[2] - v1[2] * v2[1])*p[0] +
405  (v1[2] * v2[0] - v1[0] * v2[2])*p[1] +
406  (v1[0] * v2[1] - v1[1] * v2[0])*p[2]>=-1e-17;
407 }
408 
410 inline bool sideHalfSpaceContains(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
411 {
412  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]);
413  n.normalize();
414  const double a = n*h.n;
415  return 0<=h.d && ( a>=1. || (a>=0. && a*a >= 1.-h.d*h.d));
416 }
417 
419 inline bool sideHalfSpaceIntersects(const Vec3d& v1, const Vec3d& v2, const SphericalCap& h)
420 {
421  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]);
422  n.normalize();
423  return h.intersectsHalfSpace(n[0], n[1], n[2]);
424 }
425 
429 {
430 public:
431  SphericalPoint(const Vec3d& an) : n(an) {Q_ASSERT(std::fabs(1.-n.length())<0.0000001);}
432  virtual ~SphericalPoint() {;}
433 
434  virtual SphericalRegionType getType() const {return SphericalRegion::Point;}
435  virtual OctahedronPolygon getOctahedronPolygon() const;
436  virtual double getArea() const {return 0.;}
437  virtual bool isEmpty() const {return false;}
438  virtual Vec3d getPointInside() const {return n;}
439  virtual SphericalCap getBoundingCap() const {return SphericalCap(n, 1);}
442  virtual QVariantList toQVariant() const;
443  virtual void serialize(QDataStream& out) const {out << n;}
444 
445  // Contain and intersect
446  virtual bool contains(const Vec3d& p) const {return n==p;}
447  virtual bool contains(const SphericalPolygon&) const {return false;}
448  virtual bool contains(const SphericalConvexPolygon&) const {return false;}
449  virtual bool contains(const SphericalCap&) const {return false;}
450  virtual bool contains(const SphericalPoint& r) const {return n==r.n;}
451  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
452  virtual bool intersects(const SphericalPolygon&) const;
453  virtual bool intersects(const SphericalConvexPolygon&) const;
454  virtual bool intersects(const SphericalCap& r) const {return r.contains(n);}
455  virtual bool intersects(const SphericalPoint& r) const {return n==r.n;}
456  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
457 
459  static SphericalRegionP deserialize(QDataStream& in);
460 
463 };
464 
468 {
469 public:
470  virtual ~AllSkySphericalRegion() {;}
471 
472  virtual SphericalRegionType getType() const {return SphericalRegion::AllSky;}
473  virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getAllSkyOctahedronPolygon();}
474  virtual double getArea() const {return 4.*M_PI;}
475  virtual bool isEmpty() const {return false;}
476  virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
477  virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), -2);}
480  virtual QVariantList toQVariant() const;
481  virtual void serialize(QDataStream&) const {;}
482 
483  // Contain and intersect
484  virtual bool contains(const Vec3d&) const {return true;}
485  virtual bool contains(const SphericalPolygon&) const {return true;}
486  virtual bool contains(const SphericalConvexPolygon&) const {return true;}
487  virtual bool contains(const SphericalCap&) const {return true;}
488  virtual bool contains(const SphericalPoint&) const {return true;}
489  virtual bool contains(const AllSkySphericalRegion&) const {return true;}
490  virtual bool intersects(const SphericalPolygon&) const {return true;}
491  virtual bool intersects(const SphericalConvexPolygon&) const {return true;}
492  virtual bool intersects(const SphericalCap&) const {return true;}
493  virtual bool intersects(const SphericalPoint&) const {return true;}
494  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
495 
496  static const SphericalRegionP staticInstance;
497 };
498 
502 {
503 public:
504  // Avoid name hiding when overloading the virtual methods.
510 
512  virtual ~EmptySphericalRegion() {;}
513 
514  virtual SphericalRegionType getType() const {return SphericalRegion::Empty;}
515  virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon::getEmptyOctahedronPolygon();}
516  virtual double getArea() const {return 0.;}
517  virtual bool isEmpty() const {return true;}
518  virtual Vec3d getPointInside() const {return Vec3d(1,0,0);}
519  virtual SphericalCap getBoundingCap() const {return SphericalCap(Vec3d(1,0,0), 2);}
522  virtual QVariantList toQVariant() const;
523  virtual void serialize(QDataStream&) const {;}
524 
525  // Contain and intersect
526  virtual bool contains(const Vec3d&) const {return false;}
527  virtual bool contains(const SphericalPolygon&) const {return false;}
528  virtual bool contains(const SphericalConvexPolygon&) const {return false;}
529  virtual bool contains(const SphericalCap&) const {return false;}
530  virtual bool contains(const SphericalPoint&) const {return false;}
531  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
532  virtual bool intersects(const SphericalPolygon&) const {return false;}
533  virtual bool intersects(const SphericalConvexPolygon&) const {return false;}
534  virtual bool intersects(const SphericalCap&) const {return false;}
535  virtual bool intersects(const SphericalPoint&) const {return false;}
536  virtual bool intersects(const AllSkySphericalRegion&) const {return false;}
537 
538  static const SphericalRegionP staticInstance;
539 };
540 
541 
546 {
547 public:
548  // Avoid name hiding when overloading the virtual methods.
554 
555  SphericalPolygon() {;}
557  SphericalPolygon(const QVector<QVector<Vec3d> >& contours) : octahedronPolygon(contours) {;}
559  SphericalPolygon(const QVector<Vec3d>& contour) : octahedronPolygon(contour) {;}
560  SphericalPolygon(const OctahedronPolygon& octContour) : octahedronPolygon(octContour) {;}
561  SphericalPolygon(const QList<OctahedronPolygon>& octContours) : octahedronPolygon(octContours) {;}
562 
563  virtual SphericalRegionType getType() const {return SphericalRegion::Polygon;}
564  virtual OctahedronPolygon getOctahedronPolygon() const {return octahedronPolygon;}
565 
570  virtual QVariantList toQVariant() const;
571  virtual void serialize(QDataStream& out) const;
572 
573  virtual SphericalCap getBoundingCap() const;
574 
575  virtual bool contains(const Vec3d& p) const {return octahedronPolygon.contains(p);}
576  virtual bool contains(const SphericalPolygon& r) const {return octahedronPolygon.contains(r.octahedronPolygon);}
577  virtual bool contains(const SphericalConvexPolygon& r) const;
578  virtual bool contains(const SphericalCap& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
579  virtual bool contains(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
580  virtual bool contains(const AllSkySphericalRegion& r) const {return octahedronPolygon.contains(r.getOctahedronPolygon());}
581 
582  virtual bool intersects(const SphericalPolygon& r) const {return octahedronPolygon.intersects(r.octahedronPolygon);}
583  virtual bool intersects(const SphericalConvexPolygon& r) const;
584  virtual bool intersects(const SphericalCap& r) const {return r.intersects(*this);}
585  virtual bool intersects(const SphericalPoint& r) const {return octahedronPolygon.contains(r.n);}
586  virtual bool intersects(const AllSkySphericalRegion&) const {return !isEmpty();}
587 
588  virtual SphericalRegionP getIntersection(const SphericalPoint& r) const {return contains(r.n) ? SphericalRegionP(new SphericalPoint(r)) : EmptySphericalRegion::staticInstance;}
589  virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& ) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
590 
591  virtual SphericalRegionP getUnion(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
592  virtual SphericalRegionP getUnion(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
593 
594  virtual SphericalRegionP getSubtraction(const SphericalPoint&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
595  virtual SphericalRegionP getSubtraction(const EmptySphericalRegion&) const {return SphericalRegionP(new SphericalPolygon(octahedronPolygon));}
596 
598  // Methods specific to SphericalPolygon
602  void setContours(const QVector<QVector<Vec3d> >& contours) {octahedronPolygon = OctahedronPolygon(contours);}
603 
606  void setContour(const QVector<Vec3d>& contour) {octahedronPolygon = OctahedronPolygon(contour);}
607 
609  QVector<QVector<Vec3d> > getClosedOutlineContours() const {Q_ASSERT(0); return QVector<QVector<Vec3d> >();}
610 
612  static SphericalRegionP deserialize(QDataStream& in);
613 
615  static SphericalRegionP multiUnion(const QList<SphericalRegionP>& regions, bool optimizeByPreGrouping=false);
616 
618  static SphericalRegionP multiIntersection(const QList<SphericalRegionP>& regions);
619 
620 private:
621  OctahedronPolygon octahedronPolygon;
622 };
623 
624 
628 {
629 public:
630  // Avoid name hiding when overloading the virtual methods.
633 
636 
638  SphericalConvexPolygon(const QVector<QVector<Vec3d> >& contours) {Q_ASSERT(contours.size()==1); setContour(contours.at(0));}
640  SphericalConvexPolygon(const QVector<Vec3d>& contour) {setContour(contour);}
642  SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2) {contour << e0 << e1 << e2; updateBoundingCap();}
644  SphericalConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3) {contour << e0 << e1 << e2 << e3; updateBoundingCap();}
645 
646  virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygon;}
647  virtual OctahedronPolygon getOctahedronPolygon() const {return OctahedronPolygon(contour);}
648  virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan);}
649  virtual StelVertexArray getOutlineVertexArray() const {return StelVertexArray(contour, StelVertexArray::LineLoop);}
650  virtual double getArea() const;
651  virtual bool isEmpty() const {return contour.isEmpty();}
652  virtual Vec3d getPointInside() const;
653  virtual SphericalCap getBoundingCap() const {return cachedBoundingCap;}
654  QVector<SphericalCap> getBoundingSphericalCaps() const;
659  virtual QVariantList toQVariant() const;
660  virtual void serialize(QDataStream& out) const {out << contour;}
661 
662  // Contain and intersect
663  virtual bool contains(const Vec3d& p) const;
664  virtual bool contains(const SphericalPolygon& r) const;
665  virtual bool contains(const SphericalConvexPolygon& r) const;
666  virtual bool contains(const SphericalCap& r) const;
667  virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
668  virtual bool contains(const AllSkySphericalRegion&) const {return false;}
669  virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
670  virtual bool intersects(const SphericalPolygon& r) const;
671  virtual bool intersects(const SphericalConvexPolygon& r) const;
672  virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
673  virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
674 
676 // virtual SphericalRegionP getIntersection(const SphericalPolygon& r) const;
677 // virtual SphericalRegionP getIntersection(const SphericalConvexPolygon& r) const;
678 // virtual SphericalRegionP getIntersection(const SphericalCap& r) const;
679 // virtual SphericalRegionP getIntersection(const SphericalPoint& r) const;
680 // virtual SphericalRegionP getIntersection(const AllSkySphericalRegion& r) const;
681 // virtual SphericalRegionP getUnion(const SphericalPolygon& r) const;
682 // virtual SphericalRegionP getUnion(const SphericalConvexPolygon& r) const;
683 // virtual SphericalRegionP getUnion(const SphericalCap& r) const;
684 // virtual SphericalRegionP getUnion(const SphericalPoint& r) const;
685 // virtual SphericalRegionP getUnion(const EmptySphericalRegion& r) const;
686 // virtual SphericalRegionP getSubtraction(const SphericalPolygon& r) const;
687 // virtual SphericalRegionP getSubtraction(const SphericalConvexPolygon& r) const;
688 // virtual SphericalRegionP getSubtraction(const SphericalCap& r) const;
689 // virtual SphericalRegionP getSubtraction(const SphericalPoint& r) const;
690 // virtual SphericalRegionP getSubtraction(const EmptySphericalRegion& r) const;
691 
693  // Methods specific to SphericalConvexPolygon
697  void setContour(const QVector<Vec3d>& acontour) {contour=acontour; updateBoundingCap();}
698 
700  const QVector<Vec3d>& getConvexContour() const {return contour;}
701 
703  bool checkValid() const;
704 
706  static bool checkValidContour(const QVector<Vec3d>& contour);
707 
709  static SphericalRegionP deserialize(QDataStream& in);
710 
711 protected:
713  QVector<Vec3d> contour;
714 
717 
719  void updateBoundingCap();
720 
726  static bool areAllPointsOutsideOneSide(const Vec3d* thisContour, int nbThisContour, const Vec3d* points, int nbPoints);
727 
729  bool areAllPointsOutsideOneSide(const QVector<Vec3d>& points) const
730  {
731  return areAllPointsOutsideOneSide(contour.constData(), contour.size(), points.constData(), points.size());
732  }
733 
734  bool containsConvexContour(const Vec3d* vertice, int nbVertex) const;
735 };
736 
737 
738 // ! @class SphericalConvexPolygonSet
739 // ! A special case of SphericalPolygon for which the polygon is composed of disjoint convex polygons.
740 //class SphericalConvexPolygonSet : public SphericalRegion
741 //{
742 //public:
743 // // Avoid name hiding when overloading the virtual methods.
744 // using SphericalRegion::intersects;
745 // using SphericalRegion::contains;
746 //
747 // //! Default constructor.
748 // SphericalConvexPolygonSet() {;}
749 //
750 // //! Constructor from a list of contours.
751 // SphericalConvexPolygonSet(const QVector<QVector<Vec3d> >& contours);
752 //
753 // virtual SphericalRegionType getType() const {return SphericalRegion::ConvexPolygonSet;}
754 // virtual OctahedronPolygon getOctahedronPolygon() const;
755 // virtual StelVertexArray getFillVertexArray() const;
756 // virtual StelVertexArray getOutlineVertexArray() const;
757 // virtual double getArea() const;
758 // virtual bool isEmpty() const;
759 // virtual Vec3d getPointInside() const;
760 // virtual SphericalCap getBoundingCap() const {return cachedBoundingCap;}
761 // QVector<SphericalCap> getBoundingSphericalCaps() const;
762 // //! Serialize the region into a QVariant map matching the JSON format.
763 // //! The format is
764 // //! @code["CONVEX_POLYGON_SET", [[ra,dec], [ra,dec], [ra,dec], [ra,dec]], [[ra,dec], [ra,dec], [ra,dec], [ra,dec]]]@endcode
765 // //! where the coords from a list of closed convex contour, with each points defined by ra dec in degree in the ICRS frame.
766 // virtual QVariantList toQVariant() const;
767 // virtual void serialize(QDataStream& out) const;
768 //
769 // // Contain and intersect
770 // virtual bool contains(const Vec3d& p) const;
771 // virtual bool contains(const SphericalPolygon& r) const;
772 // virtual bool contains(const SphericalConvexPolygon& r) const;
773 // virtual bool contains(const SphericalCap& r) const;
774 // virtual bool contains(const SphericalPoint& r) const {return contains(r.n);}
775 // virtual bool contains(const AllSkySphericalRegion&) const {return false;}
776 // virtual bool intersects(const SphericalCap& r) const {if (!cachedBoundingCap.intersects(r)) return false; return r.intersects(*this);}
777 // virtual bool intersects(const SphericalPolygon& r) const;
778 // virtual bool intersects(const SphericalConvexPolygon& r) const;
779 // virtual bool intersects(const SphericalPoint& r) const {return contains(r.n);}
780 // virtual bool intersects(const AllSkySphericalRegion&) const {return true;}
781 //
782 // ////////////////////////////////////////////////////////////////////
783 // // Methods specific to SphericalConvexPolygonSet
784 // ////////////////////////////////////////////////////////////////////
785 // //! Deserialize the region. This method must allow as fast as possible deserialization.
786 // static SphericalRegionP deserialize(QDataStream& in);
787 //
788 //protected:
789 // QVector<SphericalConvexPolygon> contours;
790 //
791 // //! Cache the bounding cap.
792 // SphericalCap cachedBoundingCap;
793 //
794 // //! Update the bounding cap from the vertex list.
795 // void updateBoundingCap();
796 //};
797 
801 {
802 public:
806  {
807  Vec3d vertex;
808  Vec2f texCoord;
809  };
810 
813  SphericalTexturedPolygon(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
815  SphericalTexturedPolygon(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
816 
818  virtual StelVertexArray getFillVertexArray() const {Q_ASSERT(0); return StelVertexArray();}
826  virtual QVariantList toQVariant() const;
827  virtual void serialize(QDataStream& out) const {Q_UNUSED(out); Q_ASSERT(0);}
828 
830  // Methods specific to SphericalTexturedPolygon
833  void setContours(const QVector<QVector<TextureVertex> >& contours) {Q_UNUSED(contours); Q_ASSERT(0);}
834 
837  void setContour(const QVector<TextureVertex>& contour) {Q_UNUSED(contour); Q_ASSERT(0);}
838 
839 private:
842  QVector<Vec2f> textureCoords;
843 };
844 
845 
846 Q_DECLARE_TYPEINFO(SphericalTexturedPolygon::TextureVertex, Q_PRIMITIVE_TYPE);
847 
851 {
852 public:
855 
857  SphericalTexturedConvexPolygon(const QVector<Vec3d>& contour, const QVector<Vec2f>& texCoord) {setContour(contour, texCoord);}
858 
861  SphericalTexturedConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3) : SphericalConvexPolygon(e0,e1,e2,e3)
862  {
863  textureCoords << Vec2f(0.f, 0.f) << Vec2f(1.f, 0.f) << Vec2f(1.f, 1.f) << Vec2f(0.f, 1.f);
864  }
865 
868  virtual StelVertexArray getFillVertexArray() const {return StelVertexArray(contour, StelVertexArray::TriangleFan, textureCoords);}
869 
873  virtual void setContour(const QVector<Vec3d>& acontour, const QVector<Vec2f>& texCoord) {SphericalConvexPolygon::setContour(acontour); textureCoords=texCoord;}
874 
881  virtual QVariantList toQVariant() const;
882 
883  virtual void serialize(QDataStream& out) const {out << contour << textureCoords;}
884 
885 protected:
888  QVector<Vec2f> textureCoords;
889 };
890 
891 
895 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& p3, const Vec3d& p4, bool& ok);
896 
900 Vec3d greatCircleIntersection(const Vec3d& p1, const Vec3d& p2, const Vec3d& nHalfSpace, bool& ok);
901 
902 #endif // _STELSPHEREGEOMETRY_HPP_
903 
SphericalConvexPolygon(const Vec3d &e0, const Vec3d &e1, const Vec3d &e2, const Vec3d &e3)
Special constructor for quads.
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 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.
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.
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.
virtual bool isEmpty() const
Return true if the region is empty.
A shared pointer on a SphericalRegion.
static int metaTypeId
The meta type ID associated to a SphericalRegionP.
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.
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.
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.
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&#39;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.
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...
bool contains(const SphericalRegion *r) const
Returns whether a SphericalRegion is contained into this region.
A special case of SphericalPolygon for which the polygon is convex.
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 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 SphericalRegionP loadFromQVariant(const QVariantMap &map)
Create a SphericalRegion from the given QVariantMap with a format matching the JSON file parsed in lo...
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.
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.
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.
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...
virtual OctahedronPolygon getOctahedronPolygon() const
Return the octahedron contour representation of the polygon.
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 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.
virtual double getArea() const
Return the area of the region in steradians.
SphericalPolygon(const QVector< QVector< Vec3d > > &contours)
Constructor from a list of contours.
void setContour(const QVector< Vec3d > &contour)
Set a single contour defining the SphericalPolygon.
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.
SphericalCap()
Construct a SphericalCap with a 90 deg aperture and an undefined direction.
virtual void setContour(const QVector< Vec3d > &acontour, const QVector< Vec2f > &texCoord)
Set a single contour defining the SphericalPolygon.
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 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.