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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use vec3f::Vec3f;
use range::Rangef;
use std::ops::Add;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Segment3f {
pub start: Vec3f,
pub end: Vec3f
}
impl Segment3f {
pub fn new(start: Vec3f, end: Vec3f) -> Segment3f {
Segment3f {
start: start,
end: end
}
}
pub fn to_vec3f(self) -> Vec3f {
self.end - self.start
}
pub fn length(self) -> f32 {
self.start.distance(self.end)
}
pub fn reversed(self) -> Segment3f {
Segment3f {
start: self.end,
end: self.start
}
}
pub fn distance_to_parametric_delta(self, distance: f32) -> f32 {
distance / self.length()
}
pub fn distance_from_parametric_delta(self, delta: f32) -> f32 {
delta * self.length()
}
pub fn point_from_parametric(self, t: f32) -> Vec3f {
self.start.lerp(self.end, t)
}
pub fn segment_from_parametric_range(self, r: Rangef) -> Segment3f {
Segment3f::new(self.point_from_parametric(r.min),
self.point_from_parametric(r.max))
}
pub fn project_segment_as_range(self, other: Segment3f) -> Rangef {
Rangef::from_sorting(self.closest_point_to_point(other.start).0,
self.closest_point_to_point(other.end).0)
}
pub fn point_distance_squared(self, point: Vec3f) -> f32 {
let ab = self.end - self.start;
let ac = point - self.start;
let bc = point - self.end;
let e = ac.dot(ab);
if e <= 0.0 {
ac.dot(ac)
}
else {
let f = ab.dot(ab);
if e >= f {
bc.dot(bc)
}
else {
ac.dot(ac) - e * e / f
}
}
}
pub fn point_distance(self, point: Vec3f) -> f32 {
self.point_distance_squared(point).sqrt()
}
pub fn closest_point_to_point(self, point: Vec3f) -> (f32, Vec3f) {
let a = self.start;
let b = self.end;
let ab = b - a;
let t = (point - a).dot(ab);
if t <= 0.0f32 {
(0.0f32, a)
} else {
let denom = ab.dot(ab);
if t >= denom {
(1.0f32, b)
} else {
let t = t / denom;
(t, a + ab * t)
}
}
}
}
impl Add<Vec3f> for Segment3f {
type Output = Segment3f;
fn add(self, v: Vec3f) -> Self::Output {
Segment3f::new(self.start + v, self.end + v)
}
}
#[cfg(test)]
mod test {
use super::*;
use range::Rangef;
use vec3f::vec3f;
fn make_seg(a: i32, b: i32) -> Segment3f {
Segment3f::new(vec3f(a, 0, 0), vec3f(b, 0, 0))
}
#[test]
fn test_to_vec3f() {
assert_eq!(Segment3f::new(vec3f(0, 0, 0),
vec3f(2, 3, 4)).to_vec3f(),
vec3f(2, 3, 4));
}
#[test]
fn test_segment_length() {
let s = Segment3f::new(vec3f(0, 0, 0),
vec3f(0, 0, 9));
assert_eq!(s.length(), 9.0);
}
#[test]
fn test_segment_reversed() {
let a = vec3f(-1.5, 0, 0);
let b = vec3f(1.0, 0, 0);
assert_eq!(Segment3f::new(a, b).reversed(),
Segment3f::new(b, a));
}
#[test]
fn test_segment_distance_conversion() {
let s = Segment3f::new(vec3f(0, 1, 0),
vec3f(0, 7, 0));
let inputs = [
(0.0f32, 0.0f32),
(6.0, 1.0),
(12.0, 2.0),
(-3.0, -0.5)];
for &(distance, delta) in inputs.iter() {
assert_eq!(s.distance_to_parametric_delta(distance), delta);
assert_eq!(s.distance_from_parametric_delta(delta), distance);
}
}
#[test]
fn test_point_from_parametric() {
let s = Segment3f::new(vec3f(0, 0, -1),
vec3f(0, 0, 3));
assert_eq!(s.point_from_parametric(0.0), vec3f(0, 0, -1));
assert_eq!(s.point_from_parametric(1.0), vec3f(0, 0, 3));
assert_eq!(s.point_from_parametric(0.5), vec3f(0, 0, 1));
}
#[test]
fn test_segment_from_parametric_range() {
let s = make_seg(0, 4);
assert_eq!(s.segment_from_parametric_range(Rangef::new(0.0, 1.0)),
make_seg(0, 4));
assert_eq!(s.segment_from_parametric_range(Rangef::new(0.25, 0.75)),
make_seg(1, 3));
}
#[test]
fn test_segment_closest_point_to_point() {
let s = Segment3f::new(vec3f(2, 0, 0),
vec3f(3, 0, 0));
assert_eq!(s.closest_point_to_point(vec3f(1, 0, 0)),
(0.0, vec3f(2, 0, 0)));
assert_eq!(s.closest_point_to_point(vec3f(4, 0, 0)),
(1.0, vec3f(3, 0, 0)));
assert_eq!(s.closest_point_to_point(vec3f(2.5, 1, 0)),
(0.5, vec3f(2.5, 0, 0)));
}
#[test]
fn test_project_segment_as_range() {
let s = make_seg(0, 4);
assert_eq!(s.project_segment_as_range(make_seg(0, 4)),
Rangef::new(0.0, 1.0));
assert_eq!(s.project_segment_as_range(make_seg(-1, 5)),
Rangef::new(0.0, 1.0));
assert_eq!(s.project_segment_as_range(make_seg(-1, -1)),
Rangef::new(0.0, 0.0));
assert_eq!(s.project_segment_as_range(make_seg(1, 3)),
Rangef::new(0.25, 0.75));
assert_eq!(s.project_segment_as_range(make_seg(3, 1)),
Rangef::new(0.25, 0.75));
}
}