Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DIMENSION_LINE3D_H__
00009 #define __SP_DIMENSION_LINE3D_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spDimensionVector3D.hpp"
00014 #include "Base/spDimensionPoint2D.hpp"
00015
00016
00017 namespace sp
00018 {
00019 namespace dim
00020 {
00021
00022
00024 enum ELinePointRelations
00025 {
00026 LINE_RELATION_START,
00027 LINE_RELATION_END,
00028 LINE_RELATION_BETWEEN,
00029 };
00030
00031
00033 template <typename T, template <typename> class Vec> class linekd
00034 {
00035
00036 typedef Vec<T> VecT;
00037 typedef linekd<T, Vec> L;
00038
00039 public:
00040
00041 linekd()
00042 {
00043 }
00044 linekd(const VecT &RayStart, const VecT &RayEnd) :
00045 Start (RayStart ),
00046 End (RayEnd )
00047 {
00048 }
00049 linekd(const L &Other) :
00050 Start (Other.Start),
00051 End (Other.End )
00052 {
00053 }
00054 virtual ~linekd()
00055 {
00056 }
00057
00058
00059
00060 inline bool operator == (const L &Other)
00061 {
00062 return Start == Other.Start && End == Other.End;
00063 }
00064 inline bool operator != (const L &Other)
00065 {
00066 return Start != Other.Start && End != Other.End;
00067 }
00068
00069 inline L operator + (const L &Other) const
00070 {
00071 return L(Start + Other.Start, End + Other.End);
00072 }
00073 inline L& operator += (const L &Other)
00074 {
00075 Start += Other.Start; End += Other.End; return *this;
00076 }
00077
00078 inline L operator - (const L &Other) const
00079 {
00080 return L(Start - Other.Start, End - Other.End);
00081 }
00082 inline L& operator -= (const L &Other)
00083 {
00084 Start -= Other.Start; End -= Other.End; return *this;
00085 }
00086
00087 inline L operator / (const L &Other) const
00088 {
00089 return L(Start / Other.Start, End / Other.End);
00090 }
00091 inline L& operator /= (const L &Other)
00092 {
00093 Start /= Other.Start; End /= Other.End; return *this;
00094 }
00095
00096 inline L operator * (const L &Other) const
00097 {
00098 return L(Start * Other.Start, End * Other.End);
00099 }
00100 inline L& operator *= (const L &Other)
00101 {
00102 Start *= Other.Start; End *= Other.End; return *this;
00103 }
00104
00105 inline L operator - () const
00106 {
00107 return L(-Start, -End);
00108 }
00109
00110
00111
00113 inline VecT getCenter() const
00114 {
00115 return (Start + End) / 2;
00116 }
00117
00119 inline VecT getDirection() const
00120 {
00121 return End - Start;
00122 }
00123
00125 inline L getViceVersa() const
00126 {
00127 return L(End, Start);
00128 }
00129
00131 inline bool checkBoxBoxIntersection(const L &Line) const
00132 {
00133 return Start <= Line.End && End >= Line.Start;
00134 }
00135
00137 inline bool isPointInside(const VecT &Point) const
00138 {
00139 return Point.isBetweenPoints(Start, End);
00140 }
00141
00143 inline ELinePointRelations getClosestPointStraight(const VecT &Point, VecT &ClosestPoint) const
00144 {
00145 VecT Pos(Point - Start);
00146 VecT Dir(End - Start);
00147
00148 T Len(Dir.getLength());
00149 Dir *= (T(1) / Len);
00150 T Factor(Dir.dot(Pos));
00151
00152 Dir *= Factor;
00153 ClosestPoint = Start + Dir;
00154
00155 if (Factor < T(0))
00156 return LINE_RELATION_START;
00157 if (Factor > Len)
00158 return LINE_RELATION_END;
00159
00160 return LINE_RELATION_BETWEEN;
00161 }
00162
00164 inline VecT getClosestPointStraight(const VecT &Point) const
00165 {
00166 VecT ClosestPoint;
00167 getClosestPointStraight(Point, ClosestPoint);
00168 return ClosestPoint;
00169 }
00170
00172 inline ELinePointRelations getClosestPoint(const VecT &Point, VecT &ClosestPoint) const
00173 {
00174 VecT Pos(Point - Start);
00175 VecT Dir(End - Start);
00176
00177 T Len(Dir.getLength());
00178 Dir *= (T(1) / Len);
00179 T Factor(Dir.dot(Pos));
00180
00181 if (Factor < T(0))
00182 {
00183 ClosestPoint = Start;
00184 return LINE_RELATION_START;
00185 }
00186 if (Factor > Len)
00187 {
00188 ClosestPoint = End;
00189 return LINE_RELATION_END;
00190 }
00191
00192 Dir *= Factor;
00193
00194 ClosestPoint = Start + Dir;
00195 return LINE_RELATION_BETWEEN;
00196 }
00197
00199 inline VecT getClosestPoint(const VecT &Point) const
00200 {
00201 VecT ClosestPoint;
00202 getClosestPoint(Point, ClosestPoint);
00203 return ClosestPoint;
00204 }
00205
00207 inline T getPointDistance(const VecT &Point) const
00208 {
00209 return (getClosestPoint(Point) - Point).getLength();
00210 }
00211
00213 inline T getPointDistanceSq(const VecT &Point) const
00214 {
00215 return (getClosestPoint(Point) - Point).getLengthSq();
00216 }
00217
00218
00219
00220 VecT Start, End;
00221
00222 };
00223
00224 template <typename T> class line3d : public linekd<T, vector3d>
00225 {
00226
00227 public:
00228
00229 line3d() :
00230 linekd<T, vector3d>()
00231 {
00232 }
00233 line3d(const vector3d<T> &Start, const vector3d<T> &End) :
00234 linekd<T, vector3d>(Start, End)
00235 {
00236 }
00237 line3d(const linekd<T, vector3d> &Other) :
00238 linekd<T, vector3d>(Other)
00239 {
00240 }
00241 ~line3d()
00242 {
00243 }
00244
00245 };
00246
00247 template <typename T> class line2d : public linekd<T, point2d>
00248 {
00249
00250 public:
00251
00252 line2d() :
00253 linekd<T, point2d>()
00254 {
00255 }
00256 line2d(const point2d<T> &Start, const point2d<T> &End) :
00257 linekd<T, point2d>(Start, End)
00258 {
00259 }
00260 line2d(const linekd<T, point2d> &Other) :
00261 linekd<T, point2d>(Other)
00262 {
00263 }
00264 ~line2d()
00265 {
00266 }
00267
00268 };
00269
00270 typedef linekd<s32, vector3d> line3di;
00271 typedef linekd<f32, vector3d> line3df;
00272
00273 typedef linekd<s32, point2d> line2di;
00274 typedef linekd<f32, point2d> line2df;
00275
00276
00277 }
00278
00279 }
00280
00281
00282 #endif
00283
00284
00285
00286