00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _STELSPHEREGEOMETRY_HPP_
00021 #define _STELSPHEREGEOMETRY_HPP_
00022
00023 #include <vector>
00024 #include "VecMath.hpp"
00025
00029 namespace StelGeom
00030 {
00031
00032
00033
00034
00035 template<class T>
00036 bool intersect(const Vec3d& v, const T& o)
00037 { return contains(o, v); }
00038
00039 template<class T>
00040 bool intersect(const T& o, const Vec3d& v)
00041 { return contains(o, v); }
00042
00046 struct HalfSpace
00047 {
00048 HalfSpace() : d(0) {}
00049 HalfSpace(const Vec3d& an) : n(an), d(0) {}
00053 HalfSpace(const Vec3d& an, double ar) : n(an), d(ar) {;}
00054 HalfSpace(const HalfSpace& other) : n(other.n), d(other.d) {}
00055 bool contains(const Vec3d &v) const {return (v*n>=d);}
00056 bool operator==(const HalfSpace& other) const {return (n==other.n && d==other.d);}
00057
00059 double getArea() const {return 2.*M_PI*(1.-d);}
00061 Vec3d n;
00063 double d;
00064 };
00065
00069 class Polygon : public std::vector<Vec3d>
00070 {
00071 public:
00073 Polygon(int asize = 0) : std::vector<Vec3d>(asize) {}
00075 Polygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2);
00077 Polygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3);
00078 };
00079
00080
00081 template<class T>
00082 bool intersect(const Polygon& p, const T& o)
00083 {
00084 return intersect(o, p);
00085 }
00086
00087
00092 class ConvexS : public std::vector<HalfSpace>
00093 {
00094 public:
00096 ConvexS(const ConvexS& c) : std::vector<HalfSpace>(c) {}
00098 ConvexS(int asize = 0) : std::vector<HalfSpace>(asize) {}
00100 ConvexS(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2);
00102 ConvexS(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3);
00103
00105 bool areAllPointsOutsideOneSide(const Polygon& poly) const
00106 {
00107 for (const_iterator iter=begin();iter!=end();++iter)
00108 {
00109 bool allOutside = true;
00110 for (Polygon::const_iterator v=poly.begin();v!=poly.end()&& allOutside==true;++v)
00111 {
00112 allOutside = allOutside && !iter->contains(*v);
00113 }
00114 if (allOutside)
00115 return true;
00116 }
00117 return false;
00118 }
00119 };
00120
00125 class ConvexPolygon : public ConvexS, public Polygon
00126 {
00127 public:
00128
00130 ConvexPolygon() {}
00131
00133 ConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2):
00134 ConvexS(e0, e1, e2), Polygon(e0, e1, e2)
00135 {}
00136
00138 ConvexPolygon(const Vec3d &e0,const Vec3d &e1,const Vec3d &e2, const Vec3d &e3):
00139 ConvexS(e0, e1, e2, e3), Polygon(e0, e1, e2, e3)
00140 {}
00141
00142 bool operator==(const ConvexPolygon& other) const {
00143 return ((const Polygon&)(*this)==(const Polygon&)other);
00144 }
00145
00147 const Vec3d& operator[](const Polygon::size_type& i)const {
00148 return Polygon::operator[](i);
00149 }
00150
00152 Vec3d& operator[](Polygon::size_type& i) {
00153 return Polygon::operator[](i);
00154 }
00155
00157 double getArea() const;
00158
00160 Vec3d getBarycenter() const;
00161
00163 Polygon& asPolygon() {return static_cast<Polygon&>(*this);}
00164
00166 const Polygon& asPolygon() const {return static_cast<const Polygon&>(*this);}
00167
00169 ConvexS& asConvex() {return static_cast<ConvexS&>(*this);}
00170
00172 const ConvexS& asConvex() const {return static_cast<const ConvexS&>(*this);}
00173
00175 bool checkValid() const;
00176
00178 static ConvexPolygon fullSky();
00179 };
00180
00181
00183 inline bool intersect(const ConvexPolygon& cp1, const ConvexPolygon& cp2)
00184 {
00185 const ConvexS& c1 = cp1;
00186 const ConvexS& c2 = cp2;
00187 return !c1.areAllPointsOutsideOneSide(cp2) && !c2.areAllPointsOutsideOneSide(cp1);
00188 }
00189
00192 struct Disk : HalfSpace
00193 {
00197 Disk(const Vec3d& an, double r) : HalfSpace(an, std::cos(r))
00198 {}
00199 };
00200
00202 inline bool intersect(const ConvexS& cp1, const ConvexPolygon& cp2)
00203 {
00204 Q_ASSERT(0);
00205
00206 return false;
00207 }
00208
00209 template<class S1, class S2>
00210 class Difference
00211 {
00212 public:
00213 Difference(const S1& s1_, const S2& s2_) : s1(s1_), s2(s2_)
00214 {}
00215
00216 S1 s1;
00217 S2 s2;
00218 };
00219
00220 template<class S1, class S2, class S>
00221 inline bool intersect(const Difference<S1, S2>& d, const S& s)
00222 {
00223 return !contains(d.s2, s) && intersect(d.s1, s);
00224 }
00225
00226 template<class S1, class S2>
00227 bool intersect(const Difference<S1, S2>& d, const Vec3d& v)
00228 { return !contains(d.s2, v) && intersect(d.s1, v); }
00229
00230 template<class S1, class S2, class S>
00231 inline bool contains(const Difference<S1, S2>&d, const S& s)
00232 {
00233 return !intersect(d.s2, s) && contains(d.s1, s);
00234 }
00235
00236
00237
00238 inline bool contains(const HalfSpace& h, const Vec3d& v)
00239 {
00240 return h.contains(v);
00241 }
00242
00243 inline bool contains(const HalfSpace& h, const Polygon& poly)
00244 {
00245 for (Polygon::const_iterator iter=poly.begin();iter!=poly.end();++iter)
00246 {
00247 if (!h.contains(*iter))
00248 return false;
00249 }
00250 return true;
00251 }
00252
00253 inline bool contains(const ConvexPolygon& c, const Vec3d& v)
00254 {
00255 const ConvexS& conv = c;
00256 for (ConvexS::const_iterator iter=conv.begin();iter!=conv.end();++iter)
00257 {
00258 if (!iter->contains(v))
00259 return false;
00260 }
00261 return true;
00262 }
00263
00264 inline bool contains(const ConvexPolygon& cp1, const ConvexPolygon& cp2)
00265 {
00266 const Polygon& poly = cp2;
00267 for (Polygon::const_iterator iter=poly.begin();iter!=poly.end();++iter)
00268 {
00269 if (!contains(cp1, *iter))
00270 return false;
00271 }
00272 return true;
00273 }
00274
00278 inline bool intersect(const HalfSpace& h, const ConvexPolygon& cp)
00279 {
00280 if (contains(cp, h.n))
00281 return true;
00282 const ConvexS& c = cp;
00283 for (ConvexS::const_iterator iter=c.begin();iter!=c.end();++iter)
00284 {
00285 const double cosAlpha = h.n*iter->n;
00286 if (!(std::sqrt(1.-cosAlpha*cosAlpha) > h.d))
00287 return true;
00288 }
00289 return false;
00290 }
00291
00292
00293 inline bool intersect(const ConvexPolygon& c, const HalfSpace& h)
00294 {
00295 return intersect(h, c);
00296 }
00297
00300 bool planeIntersect2(const HalfSpace& h1, const HalfSpace& h2, Vec3d& p1, Vec3d& p2);
00301
00302 }
00303
00304 #endif // _STELSPHEREGEOMETRY_HPP_
00305