00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _GFX_SEGMENT3_
00026 #define _GFX_SEGMENT3_
00027
00028 #include <gfx/gfx>
00029 #include <base/Point3>
00030 #include <base/Vector3>
00031 #include <base/Transform>
00032
00033 #include <iostream>
00034
00035
00036 namespace gfx {
00037
00038
00039 class Segment3
00040 {
00041
00042 public:
00043 Segment3() {}
00044 Segment3(const Vector3& d)
00045 : s(Point3()), e(d) {}
00046 Segment3(const Point3& s, const Point3& e)
00047 : s(s), e(e) {}
00048 Segment3(const Segment3& seg)
00049 : s(seg.s), e(seg.e) {}
00050 ~Segment3() {}
00051
00052 Segment3& operator=(const Segment3& seg)
00053 {
00054 if (&seg == this) return *this;
00055 s=seg.s;
00056 e=seg.e;
00057 return *this;
00058 }
00059
00060 bool operator==(const Segment3& seg) const
00061 {
00062 if (&seg == this) return true;
00063 return (s==seg.s) && (e==seg.e);
00064 }
00065
00066 bool operator!=(const Segment3& seg) const
00067 {
00068 if (&seg == this) return false;
00069 return (s!=seg.s) || (e!=seg.e);
00070 }
00071
00072 bool equals(const Segment3& seg, Real epsilon = consts::epsilon) const
00073 {
00074 if (&seg == this) return true;
00075 return (s.equals(seg.s,epsilon) && e.equals(seg.e,epsilon));
00076 }
00077
00078
00079 Point3& operator[](Int i) { return (i==0)?s:e; }
00080 const Point3& operator[](Int i) const { return (i==0)?s:e; }
00081
00082 Segment3& operator*=(Real s) { this->s*=s; e*=s; return *this; }
00083 Segment3& operator/=(Real s) { this->s/=s; e/=s; return *this; }
00084
00085 void transform(const base::Transform& t)
00086 {
00087 s = t*s;
00088 e = t*e;
00089 }
00090
00091 Real length() const { return (e-s).length(); }
00092
00093 Real norm() const { return (e-s).norm(); }
00094
00095 Real magnitude() const { return (e-s).magnitude(); }
00096
00097
00098
00099 bool contains(const Point3& p) const;
00100
00101
00102 Real distanceTo(const Point3& p) const;
00103
00104
00105 Real distanceTo(const Segment3& s2) const
00106 { Real ds; shortestSegmentBetween(s2,ds); return Math::sqrt(ds); }
00107
00108
00109 Point3 pointClosestTo(const Point3& p) const;
00110
00111
00112
00113 Segment3 shortestSegmentBetween(const Segment3& s2) const
00114 { Real ds; return shortestSegmentBetween(s2,ds); }
00115
00116
00117 Segment3& swapEnds() { Point3 os(s); s=e; e=os; return *this; }
00118
00119 Point3 s, e;
00120
00121 protected:
00122 Segment3 shortestSegmentBetween(const Segment3& s2, Real& ds) const;
00123
00124 friend class Triangle3;
00125 };
00126
00127
00128
00129 inline Segment3 operator*(const Segment3& seg, Real s)
00130 { Segment3 r(seg); r*=s; return r; }
00131
00132 inline Segment3 operator*(Real s, const Segment3& seg)
00133 { Segment3 r(seg); r*=s; return r; }
00134
00135 inline Segment3 operator/(const Segment3& seg, Real s)
00136 { Segment3 r(seg); r/=s; return r; }
00137
00138
00139 inline std::ostream& operator<<(std::ostream& out, const Segment3& s)
00140 { return out << "[" << s.s << "-" << s.e << "]"; }
00141
00142
00143 }
00144
00145 #endif