Imported more code from the old engine.
[peakengine.git] / engine / include / support / line3d.h
blobb5eff0c57db97dd5c4db5677a75cc525b952ed35
1 // Copyright (C) 2002-2007 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
5 #ifndef __IRR_LINE_3D_H_INCLUDED__
6 #define __IRR_LINE_3D_H_INCLUDED__
8 #include "irrTypes.h"
9 #include "vector3d.h"
11 namespace irr
13 namespace core
16 //! 3D line between two points with intersection methods.
17 template <class T>
18 class line3d
20 public:
22 // Constructors
24 line3d() : start(0,0,0), end(1,1,1) {}
25 line3d(T xa, T ya, T za, T xb, T yb, T zb) : start(xa, ya, za), end(xb, yb, zb) {}
26 line3d(const vector3d<T>& start, const vector3d<T>& end) : start(start), end(end) {}
28 // operators
30 line3d<T> operator+(const vector3d<T>& point) const { return line3d<T>(start + point, end + point); }
31 line3d<T>& operator+=(const vector3d<T>& point) { start += point; end += point; return *this; }
33 line3d<T> operator-(const vector3d<T>& point) const { return line3d<T>(start - point, end - point); }
34 line3d<T>& operator-=(const vector3d<T>& point) { start -= point; end -= point; return *this; }
36 bool operator==(const line3d<T>& other) const { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
37 bool operator!=(const line3d<T>& other) const { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
39 // functions
41 void setLine(const T& xa, const T& ya, const T& za, const T& xb, const T& yb, const T& zb) {start.set(xa, ya, za); end.set(xb, yb, zb);}
42 void setLine(const vector3d<T>& nstart, const vector3d<T>& nend) {start.set(nstart); end.set(nend);}
43 void setLine(const line3d<T>& line) {start.set(line.start); end.set(line.end);}
45 //! Returns length of line
46 //! \return Returns length of line.
47 T getLength() const { return start.getDistanceFrom(end); }
49 //! Returns sqared length of line
50 //! \return Returns sqared length of line.
51 T getLengthSQ() const { return start.getDistanceFromSQ(end); }
53 //! Returns middle of line
54 vector3d<T> getMiddle() const
56 return (start + end) * (T)0.5;
59 //! Returns vector of line
60 vector3d<T> getVector() const
62 return end - start;
65 //! Returns if the given point is between start and end of the
66 //! line. Assumes that the point is already somewhere on the line.
67 bool isPointBetweenStartAndEnd(const vector3d<T>& point) const
69 return point.isBetweenPoints(start, end);
72 //! Returns the closest point on this line to a point
73 vector3d<T> getClosestPoint(const vector3d<T>& point) const
75 vector3d<T> c = point - start;
76 vector3d<T> v = end - start;
77 T d = (T)v.getLength();
78 v /= d;
79 T t = v.dotProduct(c);
81 if (t < (T)0.0)
82 return start;
83 if (t > d)
84 return end;
86 v *= t;
87 return start + v;
90 //! Returns if the line intersects with a shpere
91 //! \param sorigin: Origin of the shpere.
92 //! \param sradius: Radius of the sphere.
93 //! \param outdistance: The distance to the first intersection point.
94 //! \return Returns true if there is an intersection.
95 //! If there is one, the distance to the first intersection point
96 //! is stored in outdistance.
97 bool getIntersectionWithSphere(vector3d<T> sorigin, T sradius, f64& outdistance) const
99 const vector3d<T> q = sorigin - start;
100 T c = q.getLength();
101 T v = q.dotProduct(getVector().normalize());
102 T d = sradius * sradius - (c*c - v*v);
104 if (d < 0.0)
105 return false;
107 outdistance = v - sqrt((f64)d);
108 return true;
111 // member variables
113 vector3d<T> start;
114 vector3d<T> end;
117 //! Typedef for an f32 line.
118 typedef line3d<f32> line3df;
119 //! Typedef for an integer line.
120 typedef line3d<s32> line3di;
122 } // end namespace core
123 } // end namespace irr
125 #endif