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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::surface2::*;
use crate::vector2::Vector2D;
use crate::transform2::Transform2;
use crate::bounding_box2::BoundingBox2D;
use crate::ray2::Ray2D;
use crate::plane2::Plane2;
use std::sync::{RwLock, Arc};
pub struct Box2 {
bound: BoundingBox2D,
pub surface_data: Surface2Data,
}
impl Box2 {
pub fn new_default(transform: Option<Transform2>,
is_normal_flipped: Option<bool>) -> Box2 {
return Box2 {
bound: BoundingBox2D::new(Vector2D::new_default(), Vector2D::new(1.0, 1.0)),
surface_data: Surface2Data::new(transform, is_normal_flipped),
};
}
pub fn new(lower_corner: Vector2D,
upper_corner: Vector2D,
transform: Option<Transform2>,
is_normal_flipped: Option<bool>) -> Box2 {
return Box2 {
bound: BoundingBox2D::new(lower_corner, upper_corner),
surface_data: Surface2Data::new(transform, is_normal_flipped),
};
}
pub fn new_aabb(bounding_box: BoundingBox2D,
transform: Option<Transform2>,
is_normal_flipped: Option<bool>) -> Box2 {
return Box2 {
bound: bounding_box,
surface_data: Surface2Data::new(transform, is_normal_flipped),
};
}
pub fn builder() -> Builder {
return Builder::new();
}
}
impl Surface2 for Box2 {
fn closest_point_local(&self, other_point: &Vector2D) -> Vector2D {
return if self.bound.contains(&other_point) {
let planes = [Plane2::new(Vector2D::new(1.0, 0.0), self.bound.upper_corner, None, None),
Plane2::new(Vector2D::new(0.0, 1.0), self.bound.upper_corner, None, None),
Plane2::new(Vector2D::new(-1.0, 0.0), self.bound.lower_corner, None, None),
Plane2::new(Vector2D::new(0.0, -1.0), self.bound.lower_corner, None, None)];
let mut result = planes[0].closest_point(other_point);
let mut distance_squared = result.distance_squared_to(*other_point);
for i in 1..4 {
let local_result = planes[i].closest_point(other_point);
let local_distance_squared =
local_result.distance_squared_to(*other_point);
if local_distance_squared < distance_squared {
result = local_result;
distance_squared = local_distance_squared;
}
}
result
} else {
crate::vector2::clamp(&other_point, &self.bound.lower_corner, &self.bound.upper_corner)
};
}
fn bounding_box_local(&self) -> BoundingBox2D {
return self.bound.clone();
}
fn closest_intersection_local(&self, ray: &Ray2D) -> SurfaceRayIntersection2 {
let mut intersection = SurfaceRayIntersection2::new();
let bb_ray_intersection =
self.bound.closest_intersection(&ray);
intersection.is_intersecting = bb_ray_intersection.is_intersecting;
if intersection.is_intersecting {
intersection.distance = bb_ray_intersection.t_near;
intersection.point = ray.point_at(bb_ray_intersection.t_near);
intersection.normal = self.closest_normal_local(&intersection.point);
}
return intersection;
}
fn closest_normal_local(&self, other_point: &Vector2D) -> Vector2D {
let planes = [Plane2::new(Vector2D::new(1.0, 0.0), self.bound.upper_corner, None, None),
Plane2::new(Vector2D::new(0.0, 1.0), self.bound.upper_corner, None, None),
Plane2::new(Vector2D::new(-1.0, 0.0), self.bound.lower_corner, None, None),
Plane2::new(Vector2D::new(0.0, -1.0), self.bound.lower_corner, None, None)];
return if self.bound.contains(other_point) {
let mut closest_normal = planes[0].normal;
let closest_point = planes[0].closest_point(other_point);
let mut min_distance_squared = (closest_point - *other_point).length_squared();
for i in 1..4 {
let local_closest_point = planes[i].closest_point(other_point);
let local_distance_squared =
(local_closest_point - *other_point).length_squared();
if local_distance_squared < min_distance_squared {
closest_normal = planes[i].normal;
min_distance_squared = local_distance_squared;
}
}
closest_normal
} else {
let closest_point =
crate::vector2::clamp(&other_point, &self.bound.lower_corner, &self.bound.upper_corner);
let closest_point_to_input_point = *other_point - closest_point;
let mut closest_normal = planes[0].normal;
let mut max_cosine_angle = closest_normal.dot(&closest_point_to_input_point);
for i in 1..4 {
let cosine_angle = planes[i].normal.dot(&closest_point_to_input_point);
if cosine_angle > max_cosine_angle {
closest_normal = planes[i].normal;
max_cosine_angle = cosine_angle;
}
}
closest_normal
};
}
fn intersects_local(&self, ray_local: &Ray2D) -> bool {
return self.bound.intersects(ray_local);
}
fn view(&self) -> &Surface2Data {
return &self.surface_data;
}
}
pub type Box2Ptr = Arc<RwLock<Box2>>;
pub struct Builder {
_lower_corner: Vector2D,
_upper_corner: Vector2D,
_surface_data: Surface2Data,
}
impl Builder {
pub fn with_lower_corner(&mut self, pt: Vector2D) -> &mut Self {
self._lower_corner = pt;
return self;
}
pub fn with_upper_corner(&mut self, pt: Vector2D) -> &mut Self {
self._upper_corner = pt;
return self;
}
pub fn with_bounding_box(&mut self, bbox: BoundingBox2D) -> &mut Self {
self._lower_corner = bbox.lower_corner;
self._upper_corner = bbox.upper_corner;
return self;
}
pub fn build(&mut self) -> Box2 {
return Box2::new_aabb(
BoundingBox2D::new(self._lower_corner, self._upper_corner),
Some(self._surface_data.transform.clone()),
Some(self._surface_data.is_normal_flipped),
);
}
pub fn make_shared(&mut self) -> Box2Ptr {
return Box2Ptr::new(RwLock::new(self.build()));
}
pub fn new() -> Builder {
return Builder {
_lower_corner: Vector2D::new_default(),
_upper_corner: Vector2D::new(1.0, 1.0),
_surface_data: Surface2Data::new(None, None),
};
}
}
impl SurfaceBuilderBase2 for Builder {
fn view(&mut self) -> &mut Surface2Data {
return &mut self._surface_data;
}
}