]> git.lizzy.rs Git - irrlicht.git/blob - include/line3d.h
Add back LightManager
[irrlicht.git] / include / line3d.h
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
4 \r
5 #ifndef __IRR_LINE_3D_H_INCLUDED__\r
6 #define __IRR_LINE_3D_H_INCLUDED__\r
7 \r
8 #include "irrTypes.h"\r
9 #include "vector3d.h"\r
10 \r
11 namespace irr\r
12 {\r
13 namespace core\r
14 {\r
15 \r
16 //! 3D line between two points with intersection methods.\r
17 template <class T>\r
18 class line3d\r
19 {\r
20         public:\r
21 \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
29 \r
30                 // operators\r
31 \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
34 \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
37 \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
42 \r
43                 // functions\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
53 \r
54                 //! Get length of line\r
55                 /** \return Length of line. */\r
56                 T getLength() const { return start.getDistanceFrom(end); }\r
57 \r
58                 //! Get squared length of line\r
59                 /** \return Squared length of line. */\r
60                 T getLengthSQ() const { return start.getDistanceFromSQ(end); }\r
61 \r
62                 //! Get middle of line\r
63                 /** \return Center of line. */\r
64                 vector3d<T> getMiddle() const\r
65                 {\r
66                         return (start + end)/(T)2;\r
67                 }\r
68 \r
69                 //! Get vector of line\r
70                 /** \return vector of line. */\r
71                 vector3d<T> getVector() const\r
72                 {\r
73                         return end - start;\r
74                 }\r
75 \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
80                 */\r
81                 bool isPointBetweenStartAndEnd(const vector3d<T>& point) const\r
82                 {\r
83                         return point.isBetweenPoints(start, end);\r
84                 }\r
85 \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
90                 {\r
91                         vector3d<T> c = point - start;\r
92                         vector3d<T> v = end - start;\r
93                         T d = (T)v.getLength();\r
94                         v /= d;\r
95                         T t = v.dotProduct(c);\r
96 \r
97                         if (t < (T)0.0)\r
98                                 return start;\r
99                         if (t > d)\r
100                                 return end;\r
101 \r
102                         v *= t;\r
103                         return start + v;\r
104                 }\r
105 \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
114                 {\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
119 \r
120                         if (d < 0.0)\r
121                                 return false;\r
122 \r
123                         outdistance = v - core::squareroot ( d );\r
124                         return true;\r
125                 }\r
126 \r
127                 // member variables\r
128 \r
129                 //! Start point of line\r
130                 vector3d<T> start;\r
131                 //! End point of line\r
132                 vector3d<T> end;\r
133 };\r
134 \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
139 \r
140 } // end namespace core\r
141 } // end namespace irr\r
142 \r
143 #endif\r
144 \r