CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CURay.h
1 //
2 // CURay.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a 3d ray.
6 //
7 // Because math objects are intended to be on the stack, we do not provide
8 // any shared pointer support in this class.
9 //
10 // CUGL MIT License:
11 // This software is provided 'as-is', without any express or implied
12 // warranty. In no event will the authors be held liable for any damages
13 // arising from the use of this software.
14 //
15 // Permission is granted to anyone to use this software for any purpose,
16 // including commercial applications, and to alter it and redistribute it
17 // freely, subject to the following restrictions:
18 //
19 // 1. The origin of this software must not be misrepresented; you must not
20 // claim that you wrote the original software. If you use this software
21 // in a product, an acknowledgment in the product documentation would be
22 // appreciated but is not required.
23 //
24 // 2. Altered source versions must be plainly marked as such, and must not
25 // be misrepresented as being the original software.
26 //
27 // 3. This notice may not be removed or altered from any source distribution.
28 //
29 // Author: Walker White
30 // Version: 6/20/16
31 
32 #ifndef __CU_RAY_H__
33 #define __CU_RAY_H__
34 
35 #include "CUVec3.h"
36 #include "CUMat4.h"
37 
38 namespace cugl {
45 class Ray {
46 
47 #pragma mark Values
48 public:
53 
55  static const Ray X_AXIS;
57  static const Ray Y_AXIS;
59  static const Ray Z_AXIS;
60 
61 #pragma mark -
62 #pragma mark Constructors
63 public:
67  Ray() : direction(Vec3::UNIT_X) { }
68 
76  Ray (const Vec3& direction) {
78  }
79 
86  Ray (const Vec3& origin, const Vec3& direction) {
88  }
89 
95  Ray(const Ray& ray) {
96  set(ray);
97  }
98 
102  ~Ray() {}
103 
104 
105 #pragma mark -
106 #pragma mark Setters
107 
114  Ray& operator=(const Ray& ray) {
115  return set(ray);
116  }
117 
128  return set(direction);
129  }
130 
138  Ray& set(const Ray& ray) {
139  origin = ray.origin;
140  direction = ray.direction;
141  return *this;
142  }
143 
152  Ray& set(const Vec3& origin, const Vec3& direction) {
153  this->origin = origin;
154  this->direction = direction.getNormalization();
155  return *this;
156  }
157 
167  Ray& set(const Vec3& direction) {
168  origin.setZero();
169  this->direction = direction.getNormalization();
170  return *this;
171  }
172 
173 #pragma mark -
174 #pragma mark Static Arithmetic
175 
186  static Vec3* endpoint(const Ray& ray, float distance, Vec3* dst);
187 
199  static Ray* multiply(const Ray& ray, const Mat4& mat, Ray* dst);
200 
201 #pragma mark -
202 #pragma mark Arithmetic
203 
213  Vec3 getEndpoint(float distance) const {
214  return direction*distance+origin;
215  }
216 
226  Ray& multiply(const Mat4& mat) {
227  return *(multiply(*this,mat,this));
228  }
229 
230 #pragma mark -
231 #pragma mark Operators
232 
242  Ray& operator*=(const Mat4& mat) {
243  return multiply(mat);
244  }
245 
255  Vec3 operator*(float distance) const {
256  return getEndpoint(distance);
257  }
258 
268  const Ray operator*(const Mat4& mat) const {
269  Ray result(*this);
270  return result.multiply(mat);
271  }
272 
273 #pragma mark -
274 #pragma mark Comparisons
275 
285  bool operator==(const Ray& r) const {
286  return origin == r.origin && direction == r.direction;
287  }
288 
299  bool operator!=(const Ray& r) const {
300  return origin != r.origin || direction != r.direction;
301  }
302 
313  bool equals(const Ray& r, float variance=CU_MATH_EPSILON) const {
314  return (origin.equals(r.origin,variance) &&
315  direction.equals(r.direction,variance));
316  }
317 
318 #pragma mark -
319 #pragma mark Conversion Methods
320 
330  std::string toString(bool verbose = false) const;
331 
333  operator std::string() const { return toString(); }
334 };
335 
336 }
337 #endif /* __CU_RAY_H__ */
cugl::Ray::direction
Vec3 direction
Definition: CURay.h:52
cugl::Ray::operator==
bool operator==(const Ray &r) const
Definition: CURay.h:285
cugl::Ray::operator=
Ray & operator=(const Vec3 &direction)
Definition: CURay.h:127
cugl::Ray::operator=
Ray & operator=(const Ray &ray)
Definition: CURay.h:114
cugl::Ray::multiply
static Ray * multiply(const Ray &ray, const Mat4 &mat, Ray *dst)
cugl::Vec3::getNormalization
Vec3 getNormalization() const
Definition: CUVec3.h:1043
cugl::Ray::operator*
const Ray operator*(const Mat4 &mat) const
Definition: CURay.h:268
cugl::Vec3::equals
bool equals(const Vec3 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:854
cugl::Ray::X_AXIS
static const Ray X_AXIS
Definition: CURay.h:55
cugl::Ray::endpoint
static Vec3 * endpoint(const Ray &ray, float distance, Vec3 *dst)
cugl::Mat4
Definition: CUMat4.h:83
cugl::Ray::multiply
Ray & multiply(const Mat4 &mat)
Definition: CURay.h:226
cugl::Ray::Ray
Ray(const Vec3 &origin, const Vec3 &direction)
Definition: CURay.h:86
cugl::Vec3::ZERO
static const Vec3 ZERO
Definition: CUVec3.h:73
cugl::Ray::toString
std::string toString(bool verbose=false) const
cugl::Ray::getEndpoint
Vec3 getEndpoint(float distance) const
Definition: CURay.h:213
cugl::Ray::Ray
Ray(const Ray &ray)
Definition: CURay.h:95
cugl::Ray::origin
Vec3 origin
Definition: CURay.h:50
cugl::Ray::~Ray
~Ray()
Definition: CURay.h:102
cugl::Ray::set
Ray & set(const Vec3 &direction)
Definition: CURay.h:167
cugl::Ray::set
Ray & set(const Ray &ray)
Definition: CURay.h:138
cugl::Vec3::setZero
Vec3 & setZero()
Definition: CUVec3.h:188
cugl::Ray::Z_AXIS
static const Ray Z_AXIS
Definition: CURay.h:59
cugl::Ray::set
Ray & set(const Vec3 &origin, const Vec3 &direction)
Definition: CURay.h:152
cugl::Ray::Ray
Ray()
Definition: CURay.h:67
cugl::Ray::operator!=
bool operator!=(const Ray &r) const
Definition: CURay.h:299
cugl::Ray::equals
bool equals(const Ray &r, float variance=CU_MATH_EPSILON) const
Definition: CURay.h:313
cugl::Ray::Ray
Ray(const Vec3 &direction)
Definition: CURay.h:76
cugl::Ray
Definition: CURay.h:45
cugl::Ray::Y_AXIS
static const Ray Y_AXIS
Definition: CURay.h:57
cugl::Vec3
Definition: CUVec3.h:61
cugl::Ray::operator*=
Ray & operator*=(const Mat4 &mat)
Definition: CURay.h:242
cugl::Ray::operator*
Vec3 operator*(float distance) const
Definition: CURay.h:255