1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __IRR_LINE_3D_H_INCLUDED__
\r
6 #define __IRR_LINE_3D_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
9 #include "vector3d.h"
\r
16 //! 3D line between two points with intersection methods.
\r
22 //! Default constructor
\r
23 /** line from (0,0,0) to (1,1,1) */
\r
24 line3d() : start(0,0,0), end(1,1,1) {}
\r
25 //! Constructor with two points
\r
26 line3d(T xa, T ya, T za, T xb, T yb, T zb) : start(xa, ya, za), end(xb, yb, zb) {}
\r
27 //! Constructor with two points as vectors
\r
28 line3d(const vector3d<T>& start, const vector3d<T>& end) : start(start), end(end) {}
\r
32 line3d<T> operator+(const vector3d<T>& point) const { return line3d<T>(start + point, end + point); }
\r
33 line3d<T>& operator+=(const vector3d<T>& point) { start += point; end += point; return *this; }
\r
35 line3d<T> operator-(const vector3d<T>& point) const { return line3d<T>(start - point, end - point); }
\r
36 line3d<T>& operator-=(const vector3d<T>& point) { start -= point; end -= point; return *this; }
\r
38 bool operator==(const line3d<T>& other) const
\r
39 { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
\r
40 bool operator!=(const line3d<T>& other) const
\r
41 { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
\r
44 //! Set this line to a new line going through the two points.
\r
45 void setLine(const T& xa, const T& ya, const T& za, const T& xb, const T& yb, const T& zb)
\r
46 {start.set(xa, ya, za); end.set(xb, yb, zb);}
\r
47 //! Set this line to a new line going through the two points.
\r
48 void setLine(const vector3d<T>& nstart, const vector3d<T>& nend)
\r
49 {start.set(nstart); end.set(nend);}
\r
50 //! Set this line to new line given as parameter.
\r
51 void setLine(const line3d<T>& line)
\r
52 {start.set(line.start); end.set(line.end);}
\r
54 //! Get length of line
\r
55 /** \return Length of line. */
\r
56 T getLength() const { return start.getDistanceFrom(end); }
\r
58 //! Get squared length of line
\r
59 /** \return Squared length of line. */
\r
60 T getLengthSQ() const { return start.getDistanceFromSQ(end); }
\r
62 //! Get middle of line
\r
63 /** \return Center of line. */
\r
64 vector3d<T> getMiddle() const
\r
66 return (start + end)/(T)2;
\r
69 //! Get vector of line
\r
70 /** \return vector of line. */
\r
71 vector3d<T> getVector() const
\r
76 //! Check if the given point is between start and end of the line.
\r
77 /** Assumes that the point is already somewhere on the line.
\r
78 \param point The point to test.
\r
79 \return True if point is on the line between start and end, else false.
\r
81 bool isPointBetweenStartAndEnd(const vector3d<T>& point) const
\r
83 return point.isBetweenPoints(start, end);
\r
86 //! Get the closest point on this line to a point
\r
87 /** \param point The point to compare to.
\r
88 \return The nearest point which is part of the line. */
\r
89 vector3d<T> getClosestPoint(const vector3d<T>& point) const
\r
91 vector3d<T> c = point - start;
\r
92 vector3d<T> v = end - start;
\r
93 T d = (T)v.getLength();
\r
95 T t = v.dotProduct(c);
\r
106 //! Check if the line intersects with a sphere
\r
107 /** \param sorigin: Origin of the sphere.
\r
108 \param sradius: Radius of the sphere.
\r
109 \param outdistance: The distance to the first intersection point.
\r
110 \return True if there is an intersection.
\r
111 If there is one, the distance to the first intersection point
\r
112 is stored in outdistance. */
\r
113 bool getIntersectionWithSphere(const vector3d<T>& sorigin, T sradius, f64& outdistance) const
\r
115 const vector3d<T> q = sorigin - start;
\r
116 T c = q.getLength();
\r
117 T v = q.dotProduct(getVector().normalize());
\r
118 T d = sradius * sradius - (c*c - v*v);
\r
123 outdistance = v - core::squareroot ( d );
\r
127 // member variables
\r
129 //! Start point of line
\r
131 //! End point of line
\r
135 //! Typedef for an f32 line.
\r
136 typedef line3d<f32> line3df;
\r
137 //! Typedef for an integer line.
\r
138 typedef line3d<s32> line3di;
\r
140 } // end namespace core
\r
141 } // end namespace irr
\r