-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cpp
More file actions
160 lines (128 loc) · 4.51 KB
/
Copy pathBox.cpp
File metadata and controls
160 lines (128 loc) · 4.51 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "Box.h"
#include <utility>
// Default Box Constructor
Box::Box() {
setPosition(Vector());
}
// Create a box with specifed width and length at the given position
Box::Box(float width, float length, Vector position) {
setPosition(position);
m_width = width;
m_length = length;
}
// Create a box with specifed width and length at the given position, with a given forward direction
Box::Box(float width, float length, Vector position, Vector forward) {
setPosition(position);
m_width = width;
m_length = length;
m_forward = forward;
}
// Create a box with specifed width, length, and height at the given position
Box::Box(float width, float length, float height, Vector position) {
setPosition(position);
m_width = width;
m_length = length;
m_height = height;
}
// Create a box with specifed width, length, and height at the given position, with a given forward direction
Box::Box(float width, float length, float height, Vector position, Vector forward) {
setPosition(position);
m_width = width;
m_length = length;
m_height = height;
m_forward = forward;
}
// Create boxes with specified boundaries - currently unused.
Box::Box(Vector min_bound, Vector max_bound) {
setPosition(Vector());
m_bound_min = min_bound;
m_bound_max = max_bound;
}
// Create boxes with specified boundaries at a given position - currently unused.
Box::Box(Vector min_bound, Vector max_bound, Vector position) {
setPosition(position);
m_bound_min = min_bound;
m_bound_max = max_bound;
}
// Setter: changes the value of the box's width
void Box::setWidth(float new_width) {
m_width = new_width;
}
// Setter: changes the value of the box's length
void Box::setLength(float new_length) {
m_length = new_length;
}
// Setter: changes the value of the box's height
void Box::setHeight(float new_height) {
m_height = new_height;
}
// Setter: multiplies the box's width by a given value
void Box::scaleWidth(float scalar) {
m_width *= scalar;
}
// Setter: multiplies the box's length by a given value
void Box::scaleLength(float scalar) {
m_length *= scalar;
}
// Setter: multiplies the box's height by a given value
void Box::scaleHeight(float scalar) {
m_height *= scalar;
}
// Setter: multiplies the size of the entire box by a given value
void Box::scale(float scalar) {
m_width *= scalar;
m_length *= scalar;
m_height *= scalar;
}
// Setter: sets the forward direction to a new direction. Currently unused.
void Box::setForward(Vector new_forward) {
m_forward = new_forward;
}
// Setter: sets the minimum and maximum boundaries for the box. Currently unused.
void Box::setBounds(Vector min, Vector max) {
m_bound_min = min;
m_bound_max = max;
}
// Getter: returns the box's width
float Box::getWidth() {
return m_width;
}
// Getter: returns the box's length
float Box::getLength() {
return m_length;
}
// Getter: returns the box's height
float Box::getHeight() {
return m_height;
}
// Getter: returns the box's forward direction. Currently unused.
Vector Box::getForward() {
return m_forward;
}
// Getter: returns the box's minimum boundary. Currently unused.
Vector Box::getMinBound() {
return m_bound_min;
}
// Getter: returns the box's maximum boundary. Currently unused.
Vector Box::getMaxBound() {
return m_bound_max;
}
// Box intersect test - returns true if a given ray intersects the box. Currently uses boundaries, which are unused.
// Modifies distance value to be from the ray origin to intersect point on IsectInfo if the test returns true
bool Box::intersects(const Ray& ray, IntersectInfo& IsectInfo) {
float tmin, tmax, tymin, tymax, tzmin, tzmax;
tmin = (m_bound_min.getX() - ray.getOrigin().getX()) * ray.getInverseDirection().getX();
tmax = (m_bound_max.getX() - ray.getOrigin().getX()) * ray.getInverseDirection().getX();
tymin = (m_bound_min.getY() - ray.getOrigin().getY()) * ray.getInverseDirection().getY();
tymax = (m_bound_max.getY() - ray.getOrigin().getY()) * ray.getInverseDirection().getY();
if ((tmin > tymax) || (tymin > tmax)) return false;
if (tymin > tmin) tmin = tymin;
if (tymax > tmax) tmax = tymax;
tzmin = (m_bound_min.getZ() - ray.getOrigin().getZ()) * ray.getInverseDirection().getZ();
tzmax = (m_bound_max.getZ() - ray.getOrigin().getZ()) * ray.getInverseDirection().getZ();
if ((tmin > tzmax) || (tzmin > tmax)) return false;
if (tzmin > tmin) tmin = tzmin;
if (tzmax > tmax) tmax = tzmax;
IsectInfo.t = (tmin < tmax) ? tmin : tmax;
return true;
}