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__
16 //! 3D line between two points with intersection methods.
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
) {}
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
);}
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
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();
79 T t
= v
.dotProduct(c
);
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
;
101 T v
= q
.dotProduct(getVector().normalize());
102 T d
= sradius
* sradius
- (c
*c
- v
*v
);
107 outdistance
= v
- sqrt((f64
)d
);
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