Saga3D API Documentation  1.0-RC4
line3d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __IRR_LINE_3D_H_INCLUDED__
6 #define __IRR_LINE_3D_H_INCLUDED__
7 
8 #include <glm/vec3.hpp>
9 
10 namespace saga
11 {
12 namespace core
13 {
14 
16 template <class T>
17 class line3d
18 {
19  public:
20 
22 
23  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) {}
27  line3d(const glm::vec3& start, const glm::vec3& end) : start(start), end(end) {}
28 
29  // operators
30 
31  line3d<T> operator+(const glm::vec3& point) const { return line3d<T>(start + point, end + point); }
32  line3d<T>& operator+=(const glm::vec3& point) { start += point; end += point; return *this; }
33 
34  line3d<T> operator-(const glm::vec3& point) const { return line3d<T>(start - point, end - point); }
35  line3d<T>& operator-=(const glm::vec3& point) { start -= point; end -= point; return *this; }
36 
37  bool operator==(const line3d<T>& other) const
38  { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
39  bool operator!=(const line3d<T>& other) const
40  { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
41 
42  // functions
44  void setLine(const T& xa, const T& ya, const T& za, const T& xb, const T& yb, const T& zb)
45  { start = {xa, ya, za}; end = {xb, yb, zb}; }
47  void setLine(const glm::vec3& nstart, const glm::vec3& nend)
48  { start = nstart; end = nend; }
50  void setLine(const line3d<T>& line)
51  { start = line.start; end = line.end; }
52 
54 
55  T getLength() const { return glm::distance(start, end); }
56 
58 
59  T getLengthSQ() const { auto d = glm::distance(start, end); return d * d; }
60 
62 
63  glm::vec3 getMiddle() const
64  {
65  return (start + end)/(T)2;
66  }
67 
69 
70  glm::vec3 getVector() const
71  {
72  return end - start;
73  }
74 
76 
80  bool isPointBetweenStartAndEnd(const glm::vec3& point) const
81  {
82  return isBetweenPoints(point, start, end);
83  }
84 
86 
88  glm::vec3 getClosestPoint(const glm::vec3& point) const
89  {
90  glm::vec3 c = point - start;
91  glm::vec3 v = end - start;
92  T d = (T) glm::length(v);
93  v /= d;
94  T t = glm::dot(v, c);
95 
96  if (t < (T)0.0)
97  return start;
98  if (t > d)
99  return end;
100 
101  v *= t;
102  return start + v;
103  }
104 
106 
112  bool getIntersectionWithSphere(const glm::vec3& sorigin, T sradius, double& outdistance) const
113  {
114  const glm::vec3 q = sorigin - start;
115  T c = glm::length(q);
116  T v = glm::dot(q, glm::normalize(getVector()));
117  T d = sradius * sradius - (c*c - v*v);
118 
119  if (d < 0.0)
120  return false;
121 
122  outdistance = v - core::squareroot (d);
123  return true;
124  }
125 
126  // member variables
127 
129  glm::vec3 start;
131  glm::vec3 end;
132 };
133 
138 
139 } // namespace core
140 } // namespace saga
141 
142 #endif
143 
saga::core::line3d::getClosestPoint
glm::vec3 getClosestPoint(const glm::vec3 &point) const
Get the closest point on this line to a point.
Definition: line3d.h:88
saga::core::line3d::getLength
T getLength() const
Get length of line.
Definition: line3d.h:55
saga::core::squareroot
REALINLINE float squareroot(const float f)
Definition: irrMath.h:545
saga::core::line3d::operator!=
bool operator!=(const line3d< T > &other) const
Definition: line3d.h:39
saga::core::line3d
3D line between two points with intersection methods.
Definition: line3d.h:17
saga::core::line3d::line3d
line3d(const glm::vec3 &start, const glm::vec3 &end)
Constructor with two points as vectors.
Definition: line3d.h:27
saga::core::isBetweenPoints
bool isBetweenPoints(const glm::vec3 &point, const glm::vec3 &begin, const glm::vec3 &end)
Definition: irrMath.h:65
saga::core::line3d::end
glm::vec3 end
End point of line.
Definition: line3d.h:131
saga::core::line3d::getVector
glm::vec3 getVector() const
Get vector of line.
Definition: line3d.h:70
saga::core::line3d::setLine
void setLine(const line3d< T > &line)
Set this line to new line given as parameter.
Definition: line3d.h:50
saga::core::line3d::start
glm::vec3 start
Start point of line.
Definition: line3d.h:129
saga::core::line3d::setLine
void setLine(const T &xa, const T &ya, const T &za, const T &xb, const T &yb, const T &zb)
Set this line to a new line going through the two points.
Definition: line3d.h:44
saga::core::line3d::line3d
line3d()
Default constructor.
Definition: line3d.h:23
saga::core::line3d::operator-
line3d< T > operator-(const glm::vec3 &point) const
Definition: line3d.h:34
saga::core::line3d::isPointBetweenStartAndEnd
bool isPointBetweenStartAndEnd(const glm::vec3 &point) const
Check if the given point is between start and end of the line.
Definition: line3d.h:80
saga::core::line3d::line3d
line3d(T xa, T ya, T za, T xb, T yb, T zb)
Constructor with two points.
Definition: line3d.h:25
saga::core::line3d::getIntersectionWithSphere
bool getIntersectionWithSphere(const glm::vec3 &sorigin, T sradius, double &outdistance) const
Check if the line intersects with a sphere.
Definition: line3d.h:112
saga::core::line3d::operator-=
line3d< T > & operator-=(const glm::vec3 &point)
Definition: line3d.h:35
saga::core::line3d::operator+
line3d< T > operator+(const glm::vec3 &point) const
Definition: line3d.h:31
saga::core::line3d::operator+=
line3d< T > & operator+=(const glm::vec3 &point)
Definition: line3d.h:32
saga::core::line3d::getLengthSQ
T getLengthSQ() const
Get squared length of line.
Definition: line3d.h:59
saga::core::line3d::operator==
bool operator==(const line3d< T > &other) const
Definition: line3d.h:37
saga::core::line3df
line3d< float > line3df
Typedef for an float line.
Definition: line3d.h:135
saga::core::line3d::getMiddle
glm::vec3 getMiddle() const
Get middle of line.
Definition: line3d.h:63
saga::core::line3di
line3d< std::int32_t > line3di
Typedef for an integer line.
Definition: line3d.h:137
saga
Definition: aabbox3d.h:11
saga::core::line3d::setLine
void setLine(const glm::vec3 &nstart, const glm::vec3 &nend)
Set this line to a new line going through the two points.
Definition: line3d.h:47