ARGoS 3
A parallel, multi-engine simulator for swarm robotics
ray3.h
Go to the documentation of this file.
1
7#ifndef RAY3_H
8#define RAY3_H
9
10namespace argos {
11 class CRay3;
12 class CPlane;
13}
14
15#include <argos3/core/utility/math/vector3.h>
16
17namespace argos {
18
19 class CRay3 {
20
21 public:
22
24 }
25
26 CRay3(const CVector3& c_start,
27 const CVector3& c_end) :
28 m_cStart(c_start), m_cEnd(c_end) {
29 }
30
31 CRay3(const CVector3& c_start,
32 const CVector3& c_direction,
33 Real f_length) {
34 Set(c_start, c_direction, f_length);
35 }
36
37 inline CVector3& GetStart() {
38 return m_cStart;
39 }
40
41 inline const CVector3& GetStart() const {
42 return m_cStart;
43 }
44
45 inline CVector3& GetEnd() {
46 return m_cEnd;
47 }
48
49 inline const CVector3& GetEnd() const {
50 return m_cEnd;
51 }
52
53 inline void SetStart(const CVector3& c_start) {
54 m_cStart = c_start;
55 }
56
57 inline void SetEnd(const CVector3& c_end) {
58 m_cEnd = c_end;
59 }
60
61 inline void SetLength(Real f_length) {
62 CVector3 c_direction;
63 GetDirection(c_direction);
64 Set(m_cStart, c_direction, f_length);
65 }
66
67 inline void Set(const CVector3& c_start, const CVector3& c_end) {
68 m_cStart = c_start;
69 m_cEnd = c_end;
70 }
71
72 inline void Set(const CVector3& c_start, const CVector3& c_direction, Real f_length) {
73 m_cStart = c_start;
74 /* Same as, but faster than
75 m_cEnd = m_cStart + f_length * c_direction; */
76 m_cEnd = m_cStart;
77 m_cEnd += f_length * c_direction;
78 }
79
80 inline void GetDirection(CVector3& c_buffer) const {
81 /* Same as, but faster than
82 c_buffer = (m_cEnd - m_cStart).Normalize(); */
83 c_buffer = m_cEnd;
84 c_buffer -= m_cStart;
85 c_buffer.Normalize();
86 }
87
88 inline void GetInverseDirection(CVector3& c_buffer) const {
89 /* Same as, but faster than
90 c_buffer = (m_cEnd - m_cStart).Normalize(); */
91 c_buffer = m_cStart;
92 c_buffer -= m_cEnd;
93 c_buffer.Normalize();
94 }
95
96 inline Real GetLength() const {
97 return (m_cEnd - m_cStart).Length();
98 }
99
100 inline CVector3& ToVector(CVector3& c_buffer) const {
101 /* Same as, but faster than
102 c_buffer = m_cEnd - m_cStart; */
103 c_buffer = m_cEnd;
104 c_buffer -= m_cStart;
105 return c_buffer;
106 }
107
108 /* Returns the point on the line corresponding to f_t */
109 inline void GetPoint(CVector3& c_point,
110 Real f_t) const {
111 c_point.SetX(m_cStart.GetX() + f_t * (m_cEnd.GetX() - m_cStart.GetX()));
112 c_point.SetY(m_cStart.GetY() + f_t * (m_cEnd.GetY() - m_cStart.GetY()));
113 c_point.SetZ(m_cStart.GetZ() + f_t * (m_cEnd.GetZ() - m_cStart.GetZ()));
114 }
115
116 /* Returns the distance from the ray3 start to the point on the line corresponding to f_t */
117 inline Real GetDistance(Real f_t) const {
118 return ::sqrt(Square(f_t * (m_cEnd.GetX() - m_cStart.GetX())) +
119 Square(f_t * (m_cEnd.GetY() - m_cStart.GetY())) +
120 Square(f_t * (m_cEnd.GetZ() - m_cStart.GetZ())));
121 }
122
123 /*
124 * Calculates the ray-plane intersection point.
125 * @param c_plane The plane whose intersection with this ray must be calculated
126 * @param c_point The resulting intersection point is returned here
127 * @return true if intersection occurred, false if ray and plane are parallel or ray lies on plane
128 */
129 bool Intersects(const CPlane& c_plane,
130 CVector3& c_point) const;
131
138 inline friend std::ostream& operator<<(std::ostream& c_os,
139 const CRay3& c_ray) {
140 c_os << c_ray.GetStart() << " -> " << c_ray.GetEnd();
141 return c_os;
142 }
143
144 private:
145
146 CVector3 m_cStart;
147 CVector3 m_cEnd;
148
149 };
150
151}
152
153#endif
float Real
Collects all ARGoS code.
Definition datatypes.h:39
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
T Square(const T &t_v)
Returns the square of the value of the passed argument.
Definition general.h:128
void SetEnd(const CVector3 &c_end)
Definition ray3.h:57
void Set(const CVector3 &c_start, const CVector3 &c_direction, Real f_length)
Definition ray3.h:72
CRay3(const CVector3 &c_start, const CVector3 &c_end)
Definition ray3.h:26
Real GetLength() const
Definition ray3.h:96
const CVector3 & GetEnd() const
Definition ray3.h:49
CVector3 & GetStart()
Definition ray3.h:37
Real GetDistance(Real f_t) const
Definition ray3.h:117
CVector3 & GetEnd()
Definition ray3.h:45
void GetDirection(CVector3 &c_buffer) const
Definition ray3.h:80
void Set(const CVector3 &c_start, const CVector3 &c_end)
Definition ray3.h:67
bool Intersects(const CPlane &c_plane, CVector3 &c_point) const
Definition ray3.cpp:15
const CVector3 & GetStart() const
Definition ray3.h:41
void GetPoint(CVector3 &c_point, Real f_t) const
Definition ray3.h:109
void GetInverseDirection(CVector3 &c_buffer) const
Definition ray3.h:88
CRay3(const CVector3 &c_start, const CVector3 &c_direction, Real f_length)
Definition ray3.h:31
CVector3 & ToVector(CVector3 &c_buffer) const
Definition ray3.h:100
friend std::ostream & operator<<(std::ostream &c_os, const CRay3 &c_ray)
Serializes the contents of the passed ray onto a stream.
Definition ray3.h:138
void SetLength(Real f_length)
Definition ray3.h:61
void SetStart(const CVector3 &c_start)
Definition ray3.h:53
A 3D vector class.
Definition vector3.h:31
void SetY(const Real f_y)
Sets the y coordinate of this vector.
Definition vector3.h:129
Real GetX() const
Returns the x coordinate of this vector.
Definition vector3.h:105
void SetX(const Real f_x)
Sets the x coordinate of this vector.
Definition vector3.h:113
CVector3 & Normalize()
Normalizes this vector.
Definition vector3.h:237
void SetZ(const Real f_z)
Sets the z coordinate of this vector.
Definition vector3.h:145
Real GetY() const
Returns the y coordinate of this vector.
Definition vector3.h:121
Real GetZ() const
Returns the z coordinate of this vector.
Definition vector3.h:137