Irrlicht 3D Engine: line3d.h Source File

| Irrlicht 3D Engine |

line3d.h
Go to the documentation of this file.

    00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
    00002 // This file is part of the "Irrlicht Engine".
    00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
    00004
    00005 #ifndef __IRR_LINE_3D_H_INCLUDED__
    00006 #define __IRR_LINE_3D_H_INCLUDED__
    00007
    00008 #include "irrTypes.h"
    00009 #include "vector3d.h"
    00010
    00011 namespace irr
    00012 {
    00013 namespace core
    00014 {
    00015
    00017 template <class T>
    00018 class line3d
    00019 {
    00020     public:
    00021
    00023
    00024         line3d() : start(0,0,0), end(1,1,1) {}
    00026         line3d(T xa, T ya, T za, T xb, T yb, T zb) : start(xa, ya, za), end(xb, yb, zb) {}
    00028         line3d(const vector3d<T>& start, const vector3d<T>& end) : start(start), end(end) {}
    00029
    00030         // operators
    00031
    00032         line3d<T> operator+(const vector3d<T>& point) const { return line3d<T>(start + point, end + point); }
    00033         line3d<T>& operator+=(const vector3d<T>& point) { start += point; end += point; return *this; }
    00034
    00035         line3d<T> operator-(const vector3d<T>& point) const { return line3d<T>(start - point, end - point); }
    00036         line3d<T>& operator-=(const vector3d<T>& point) { start -= point; end -= point; return *this; }
    00037
    00038         bool operator==(const line3d<T>& other) const
    00039         { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
    00040         bool operator!=(const line3d<T>& other) const
    00041         { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
    00042
    00043         // functions
    00045         void setLine(const T& xa, const T& ya, const T& za, const T& xb, const T& yb, const T& zb)
    00046         {start.set(xa, ya, za); end.set(xb, yb, zb);}
    00048         void setLine(const vector3d<T>& nstart, const vector3d<T>& nend)
    00049         {start.set(nstart); end.set(nend);}
    00051         void setLine(const line3d<T>& line)
    00052         {start.set(line.start); end.set(line.end);}
    00053
    00055
    00056         T getLength() const { return start.getDistanceFrom(end); }
    00057
    00059
    00060         T getLengthSQ() const { return start.getDistanceFromSQ(end); }
    00061
    00063
    00064         vector3d<T> getMiddle() const
    00065         {
    00066             return (start + end)/(T)2;
    00067         }
    00068
    00070
    00071         vector3d<T> getVector() const
    00072         {
    00073             return end - start;
    00074         }
    00075
    00077
    00081         bool isPointBetweenStartAndEnd(const vector3d<T>& point) const
    00082         {
    00083             return point.isBetweenPoints(start, end);
    00084         }
    00085
    00087
    00089         vector3d<T> getClosestPoint(const vector3d<T>& point) const
    00090         {
    00091             vector3d<T> c = point - start;
    00092             vector3d<T> v = end - start;
    00093             T d = (T)v.getLength();
    00094             v /= d;
    00095             T t = v.dotProduct(c);
    00096
    00097             if (t < (T)0.0)
    00098                 return start;
    00099             if (t > d)
    00100                 return end;
    00101
    00102             v *= t;
    00103             return start + v;
    00104         }
    00105
    00107
    00113         bool getIntersectionWithSphere(vector3d<T> sorigin, T sradius, f64& outdistance) const
    00114         {
    00115             const vector3d<T> q = sorigin - start;
    00116             T c = q.getLength();
    00117             T v = q.dotProduct(getVector().normalize());
    00118             T d = sradius * sradius - (c*c - v*v);
    00119
    00120             if (d < 0.0)
    00121                 return false;
    00122
    00123             outdistance = v - core::squareroot ( d );
    00124             return true;
    00125         }
    00126
    00127         // member variables
    00128
    00130         vector3d<T> start;
    00132         vector3d<T> end;
    00133 };
    00134
    00136     typedef line3d<f32> line3df;
    00138     typedef line3d<s32> line3di;
    00139
    00140 } // end namespace core
    00141 } // end namespace irr
    00142
    00143 #endif
    00144