-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRay.hpp
96 lines (73 loc) · 1.46 KB
/
Ray.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#ifndef __MATHS_RAY_HPP__
#define __MATHS_RAY_HPP__
#include "Vector3.hpp"
namespace Math
{
class Ray
{
private:
// The values are private because we want to keep the direction vector normalised
Vector3 origin_;
Vector3 direction_;
public:
Ray() : direction_(Vector3::UNIT_Z)
{
}
Ray(const Vector3& origin, const Vector3& direction) :
origin_(origin),
direction_(direction)
{
direction_.normalise();
}
Ray(const Ray& ray) :
origin_(ray.origin_),
direction_(ray.direction_)
{
}
Ray& operator = (const Ray& ray)
{
origin_ = ray.origin_;
direction_ = ray.direction_;
return *this;
}
bool operator == (const Ray& ray) const
{
return origin_ == ray.origin_ && direction_ == ray.direction_;
}
bool operator != (const Ray& ray) const
{
return origin_ != ray.origin_ || direction_ != ray.direction_;
}
Vector3 point_at(float t) const
{
return origin_ + direction_ * t;
}
Vector3 operator * (float t) const
{
return origin_ + direction_ * t;
}
friend Vector3 operator * (float t, const Ray& ray)
{
return ray.origin_ + ray.direction_ * t;
}
void origin(const Vector3& o)
{
origin_ = o;
}
const Vector3& origin() const
{
return origin_;
}
void direction(const Vector3& d)
{
direction_ = d;
direction_.normalise();
}
const Vector3& direction() const
{
return direction_;
}
};
} // namespace Math
#endif // __MATHS_RAY_HPP__