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 size = 0) : std::vector<Vec3d>(size) {}
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 size = 0) : std::vector<HalfSpace>(size) {}
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 };
00174
00175
00177 inline bool intersect(const ConvexPolygon& cp1, const ConvexPolygon& cp2)
00178 {
00179 const ConvexS& c1 = cp1;
00180 const ConvexS& c2 = cp2;
00181 return !c1.areAllPointsOutsideOneSide(cp2) && !c2.areAllPointsOutsideOneSide(cp1);
00182 }
00183
00186 struct Disk : HalfSpace
00187 {
00191 Disk(const Vec3d& n, double r) : HalfSpace(n, std::cos(r))
00192 {}
00193 };
00194
00196 inline bool intersect(const ConvexS& cp1, const ConvexPolygon& cp2)
00197 {
00198 Q_ASSERT(0);
00199
00200 return false;
00201 }
00202
00203 template<class S1, class S2>
00204 class Difference
00205 {
00206 public:
00207 Difference(const S1& s1_, const S2& s2_) : s1(s1_), s2(s2_)
00208 {}
00209
00210 S1 s1;
00211 S2 s2;
00212 };
00213
00214 template<class S1, class S2, class S>
00215 inline bool intersect(const Difference<S1, S2>& d, const S& s)
00216 {
00217 return !contains(d.s2, s) && intersect(d.s1, s);
00218 }
00219
00220 template<class S1, class S2>
00221 bool intersect(const Difference<S1, S2>& d, const Vec3d& v)
00222 { return !contains(d.s2, v) && intersect(d.s1, v); }
00223
00224 template<class S1, class S2, class S>
00225 inline bool contains(const Difference<S1, S2>&d, const S& s)
00226 {
00227 return !intersect(d.s2, s) && contains(d.s1, s);
00228 }
00229
00230
00231
00232 inline bool contains(const HalfSpace& h, const Vec3d& v)
00233 {
00234 return h.contains(v);
00235 }
00236
00237 inline bool contains(const HalfSpace& h, const Polygon& poly)
00238 {
00239 for (Polygon::const_iterator iter=poly.begin();iter!=poly.end();++iter)
00240 {
00241 if (!h.contains(*iter))
00242 return false;
00243 }
00244 return true;
00245 }
00246
00247 inline bool contains(const ConvexPolygon& c, const Vec3d& v)
00248 {
00249 const ConvexS& conv = c;
00250 for (ConvexS::const_iterator iter=conv.begin();iter!=conv.end();++iter)
00251 {
00252 if (!iter->contains(v))
00253 return false;
00254 }
00255 return true;
00256 }
00257
00258 inline bool contains(const ConvexPolygon& cp1, const ConvexPolygon& cp2)
00259 {
00260 const Polygon& poly = cp2;
00261 for (Polygon::const_iterator iter=poly.begin();iter!=poly.end();++iter)
00262 {
00263 if (!contains(cp1, *iter))
00264 return false;
00265 }
00266 return true;
00267 }
00268
00272 inline bool intersect(const HalfSpace& h, const ConvexPolygon& cp)
00273 {
00274 if (contains(cp, h.n))
00275 return true;
00276 const ConvexS& c = cp;
00277 for (ConvexS::const_iterator iter=c.begin();iter!=c.end();++iter)
00278 {
00279 const double cosAlpha = h.n*iter->n;
00280 if (!(std::sqrt(1.-cosAlpha*cosAlpha) > h.d))
00281 return true;
00282 }
00283 return false;
00284 }
00285
00286
00287 inline bool intersect(const ConvexPolygon& c, const HalfSpace& h)
00288 {
00289 return intersect(h, c);
00290 }
00291
00294 bool planeIntersect2(const HalfSpace& h1, const HalfSpace& h2, Vec3d& p1, Vec3d& p2);
00295
00296 }
00297
00298 #endif // _STELSPHEREGEOMETRY_HPP_
00299