vector-traits 0.6.2

Rust traits for 2D and 3D vector types.
Documentation
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2023, 2025 lacklustr@protonmail.com https://github.com/eadf

// This file is part of vector-traits.

use crate::prelude::*;

pub fn test_aabb3_from_point<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();

    // Test creation from a single point
    let point = V::new(_1, _2, _0);
    let aabb = V::Aabb::from_point(point);

    // A degenerate AABB should have min == max == point
    assert_eq!(aabb.min(), point);
    assert_eq!(aabb.max(), point);
    assert!(!aabb.is_empty());
}

pub fn test_aabb3_from_corners<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();

    // Test creation from min and max corners
    let min_corner = V::new(_0, _1, _0);
    let max_corner = V::new(_2, _3, _1);
    let aabb = V::Aabb::from_corners(min_corner, max_corner);

    // Check corners are set correctly
    assert_eq!(aabb.min(), min_corner);
    assert_eq!(aabb.max(), max_corner);
    assert!(!aabb.is_empty());

    // Test with swapped corners (should correct the ordering)
    let aabb_swapped = V::Aabb::from_corners(max_corner, min_corner);
    assert_eq!(aabb_swapped.min(), min_corner);
    assert_eq!(aabb_swapped.max(), max_corner);
}

pub fn test_aabb3_from_center_and_half_extents<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();

    // Center at (1,1,1) with half-extents (1,1,1)
    let center = V::new(_1, _1, _1);
    let half_extents = V::new(_1, _1, _1);

    let aabb = V::Aabb::from_center_and_half_extents(center, half_extents);

    // Min should be center - half_extents
    assert_eq!(aabb.min(), V::new(_0, _0, _0));
    // Max should be center + half_extents
    assert_eq!(aabb.max(), V::new(_2, _2, _2));
    assert!(!aabb.is_empty());
}

pub fn test_aabb3_from_center_and_size<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();

    // Center at (1,1,1) with size (2,2,2)
    let center = V::new(_1, _1, _1);
    let size = V::new(_2, _2, _2);

    let aabb = V::Aabb::from_center_and_size(center, size);

    // Min should be center - size/2
    assert_eq!(aabb.min(), V::new(_0, _0, _0));
    // Max should be center + size/2
    assert_eq!(aabb.max(), V::new(_2, _2, _2));
    assert!(!aabb.is_empty());

    // Test with different sizes in each dimension
    let center2 = V::new(_1, _2, _3);
    let size2 = V::new(_2, _2, _2);

    let aabb2 = V::Aabb::from_center_and_size(center2, size2);
    assert_eq!(aabb2.min(), V::new(_0, _1, _2));
    assert_eq!(aabb2.max(), V::new(_2, _3, V::Scalar::from(4.0)));
}

pub fn test_aabb3_from_points<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();
    let _4: V::Scalar = 4.0.into();

    // Create a collection of points
    let points = vec![
        V::new(_1, _1, _1),
        V::new(_2, _3, _2),
        V::new(_0, _2, _4),
        V::new(_3, _0, _2),
    ];

    let aabb = V::Aabb::from_points(points.iter());

    // AABB should encompass all points
    assert_eq!(aabb.min(), V::new(_0, _0, _1));
    assert_eq!(aabb.max(), V::new(_3, _3, _4));
    assert!(!aabb.is_empty());

    // Test with owned points
    let aabb2 = V::Aabb::from_points(points.clone());
    assert_eq!(aabb2.min(), V::new(_0, _0, _1));
    assert_eq!(aabb2.max(), V::new(_3, _3, _4));

    // Test with empty points collection
    let empty_points: Vec<V> = vec![];
    let empty_aabb = V::Aabb::from_points(empty_points.iter());
    assert!(empty_aabb.is_empty());

    let mut aabb = V::Aabb::from_points(points);
    aabb.apply(&|v: V| v * _2);
    assert_eq!(aabb.min(), V::new(_0, _0, _1 * _2));
    assert_eq!(aabb.max(), V::new(_3 * _2, _3 * _2, _4 * _2));
}

pub fn test_aabb3_extend<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();

    // Start with a simple AABB
    let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));

    // Extend with a point inside (should not change)
    aabb.add_point(V::new(_0, _1, _0));
    assert_eq!(aabb.min(), V::new(_0, _0, _0));
    assert_eq!(aabb.max(), V::new(_1, _1, _1));

    // Extend with a point outside
    aabb.add_point(V::new(_2, _3, _0));
    assert_eq!(aabb.min(), V::new(_0, _0, _0));
    assert_eq!(aabb.max(), V::new(_2, _3, _1));

    // Extend with a point that expands in negative direction
    aabb.add_point(V::new(-_1, -_1, -_1));
    assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
    assert_eq!(aabb.max(), V::new(_2, _3, _1));
}

pub fn test_aabb3_extend_with<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();
    let _4: V::Scalar = 4.0.into();

    // Create _2 AABBs
    let mut aabb1 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    let aabb2 = V::Aabb::from_corners(V::new(_1, _1, _1), V::new(_3, _4, _3));

    // Extend the first with the second
    aabb1.add_aabb(&aabb2);

    // Should contain both AABBs
    assert_eq!(aabb1.min(), V::new(_0, _0, _0));
    assert_eq!(aabb1.max(), V::new(_3, _4, _3));

    // Test with completely separate AABBs
    let mut aabb3 = V::Aabb::from_corners(V::new(-_3, -_3, -_3), V::new(-_1, -_1, -_1));

    aabb3.add_aabb(&aabb1);
    assert_eq!(aabb3.min(), V::new(-_3, -_3, -_3));
    assert_eq!(aabb3.max(), V::new(_3, _4, _3));
}

pub fn test_aabb3_pad<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();
    let half: V::Scalar = 0.5.into();

    // Create AABB from (0,0,0) to (2,2,2)
    let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    // Pad uniformly by 1 in each direction
    let padding = V::new(_1, _1, _1);
    aabb.pad(padding);

    // Should expand by 1 in each direction from center
    assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
    assert_eq!(aabb.max(), V::new(_3, _3, _3));

    // Test negative padding (shrink)
    let mut aabb2 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    let shrink = V::new(-half, -half, -half);
    aabb2.pad(shrink);

    // Should shrink by 0.5 in each direction toward center
    assert_eq!(aabb2.min(), V::new(half, half, half));
    assert_eq!(aabb2.max(), V::new(_1 + half, _1 + half, _1 + half));

    // Test extreme negative padding (should clamp to center)
    let mut aabb3 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    let extreme_shrink = V::new(-_2, -_2, -_2);
    aabb3.pad(extreme_shrink);

    // Should collapse to center point
    let center = V::new(_1, _1, _1);
    assert_eq!(aabb3.min(), center);
    assert_eq!(aabb3.max(), center);
    assert!(!aabb3.is_empty());
}

pub fn test_aabb3_fast_pad<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();

    // Create AABB from (0,0,0) to (2,2,2)
    let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    // Fast pad by 1 in each direction
    let padding = V::new(_1, _1, _1);
    aabb.fast_pad(padding);

    // Should directly add to max and subtract from min
    assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
    assert_eq!(aabb.max(), V::new(_3, _3, _3));

    // Test negative padding
    let mut aabb2 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_3, _3, _3));

    let shrink = V::new(-_1, -_1, -_1);
    aabb2.fast_pad(shrink);

    // Should directly subtract from max and add to min
    assert_eq!(aabb2.min(), V::new(_1, _1, _1));
    assert_eq!(aabb2.max(), V::new(_2, _2, _2));

    // Test with extreme negative padding (can invert the AABB)
    let mut aabb3 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));

    let extreme_shrink = V::new(-_2, -_2, -_2);
    aabb3.fast_pad(extreme_shrink);

    // Will produce an inverted AABB (min > max)
    assert_eq!(aabb3.min(), V::new(_2, _2, _2));
    assert_eq!(aabb3.max(), V::new(-_1, -_1, -_1));
    // This should be detected as empty by is_empty()
    assert!(aabb3.is_empty());
}

pub fn test_aabb3_is_empty<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();

    // Test a valid AABB
    let valid_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
    assert!(!valid_aabb.is_empty());

    // Test a degenerate AABB (point)
    let point_aabb = V::Aabb::from_point(V::new(_1, _1, _1));
    assert!(!point_aabb.is_empty());

    // Test an inverted AABB (from fast_pad)
    let mut inverted_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
    inverted_aabb.fast_pad(V::new(-_2, -_2, -_2));
    assert!(inverted_aabb.is_empty());

    // Test an AABB that's empty in just _1 dimension
    let partial_empty_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _0, _1));
    assert!(!partial_empty_aabb.is_empty());
}

pub fn test_aabb3_comprehensive<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let _3: V::Scalar = 3.0.into();
    let _4: V::Scalar = 4.0.into();
    let _5: V::Scalar = 5.0.into();
    let _6: V::Scalar = 6.0.into();
    let _7: V::Scalar = 7.0.into();
    // Create an AABB and test multiple operations in sequence

    // 1. Start with a point
    let mut aabb = V::Aabb::from_point(V::new(_0, _0, _0));
    assert!(!aabb.is_empty());

    // 2. Extend with points
    aabb.add_point(V::new(_1, _1, _1));
    assert_eq!(aabb.min(), V::new(_0, _0, _0));
    assert_eq!(aabb.max(), V::new(_1, _1, _1));
    assert!(!aabb.is_empty());

    aabb.add_point(V::new(-_1, _2, _3));
    assert_eq!(aabb.min(), V::new(-_1, _0, _0));
    assert_eq!(aabb.max(), V::new(_1, _2, _3));

    // 3. Extend with another AABB
    let other_aabb = V::Aabb::from_corners(V::new(_2, -_2, -_1), V::new(_5, _0, _4));
    aabb.add_aabb(&other_aabb);
    assert_eq!(aabb.min(), V::new(-_1, -_2, -_1));
    assert_eq!(aabb.max(), V::new(_5, _2, _4));

    // 4. Pad the AABB
    aabb.pad(V::new(_1, _1, _1));
    assert_eq!(aabb.min(), V::new(-_2, -_3, -_2));
    assert_eq!(aabb.max(), V::new(_6, _3, _5));

    // 5. Create a new AABB from these points
    let points = [
        V::new(-_2, -_3, -_2),
        V::new(_6, _3, _5),
        V::new(_0, _0, _0),
    ];
    let new_aabb = V::Aabb::from_points(points.iter());
    assert_eq!(new_aabb.min(), V::new(-_2, -_3, -_2));
    assert_eq!(new_aabb.max(), V::new(_6, _3, _5));

    // Test that the new AABB equals our manually extended and padded _1
    assert_eq!(new_aabb.min(), aabb.min());
    assert_eq!(new_aabb.max(), aabb.max());
}

pub fn test_aabb3_contains_point_inclusive<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();
    let half: V::Scalar = 0.5.into();
    let neg_one: V::Scalar = (-1.0).into();

    // Create an AABB from (0,0,0) to (2,2,2)
    let aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));

    // Test points inside the AABB
    assert!(aabb.contains_point_inclusive(V::new(_1, _1, _1)));
    assert!(aabb.contains_point_inclusive(V::new(half, half, half)));
    assert!(aabb.contains_point_inclusive(V::new(_1, half, _2)));

    // Test points on the boundary
    assert!(aabb.contains_point_inclusive(V::new(_0, _0, _0))); // min corner
    assert!(aabb.contains_point_inclusive(V::new(_2, _2, _2))); // max corner
    assert!(aabb.contains_point_inclusive(V::new(_0, _1, _2))); // on face
    assert!(aabb.contains_point_inclusive(V::new(_2, _0, _1))); // on face
    assert!(aabb.contains_point_inclusive(V::new(_1, _2, _0))); // on face
    assert!(aabb.contains_point_inclusive(V::new(_0, _0, _1))); // on edge
    assert!(aabb.contains_point_inclusive(V::new(_1, _2, _2))); // on edge

    // Test points outside the AABB
    assert!(!aabb.contains_point_inclusive(V::new(neg_one, _1, _1)));
    assert!(!aabb.contains_point_inclusive(V::new(_1, neg_one, _1)));
    assert!(!aabb.contains_point_inclusive(V::new(_1, _1, neg_one)));
    assert!(!aabb.contains_point_inclusive(V::new(_2 + half, _1, _1)));
    assert!(!aabb.contains_point_inclusive(V::new(_1, _2 + half, _1)));
    assert!(!aabb.contains_point_inclusive(V::new(_1, _1, _2 + half)));

    // Test with a point AABB
    let point_aabb = V::Aabb::from_point(V::new(_1, _1, _1));
    assert!(point_aabb.contains_point_inclusive(V::new(_1, _1, _1)));
    assert!(!point_aabb.contains_point_inclusive(V::new(_1 + half, _1, _1)));

    // Test with an empty AABB
    let empty_aabb = V::Aabb::default();
    assert!(!empty_aabb.contains_point_inclusive(V::new(_0, _0, _0)));
    assert!(!empty_aabb.contains_point_inclusive(V::new(_1, _1, _1)));

    // Test with an inverted AABB
    let inverted_aabb = V::Aabb::from_corners(V::new(_1, _1, _1), V::new(_0, _0, _0));
    assert!(inverted_aabb.contains_point_inclusive(V::new(half, half, half)));
    assert!(inverted_aabb.contains_point_inclusive(V::new(_0, _0, _0)));
    assert!(inverted_aabb.contains_point_inclusive(V::new(_1, _1, _1)));
    assert!(!inverted_aabb.contains_point_inclusive(V::new(_2, _1, _1)));
}

pub fn test_extents<V: GenericVector3>() {
    let _0: V::Scalar = 0.0.into();
    let _1: V::Scalar = 1.0.into();
    let _2: V::Scalar = 2.0.into();

    // Create an AABB from (0,0,0) to (2,2,2)
    let aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
    let (min, max, delta) = aabb.extents();
    let aabb2 = V::Aabb::from_corners(min, min + delta / _2);
    let aabb3 = V::Aabb::from_corners(min, min + delta / _2);
    assert_eq!(aabb2, aabb3);
    assert_ne!(aabb, aabb3);

    assert!(!aabb2.contains_point_inclusive(max));
    assert!(aabb.contains_aabb_inclusive(&aabb2));

    let empty_aabb = V::Aabb::default();
    let empty_aabb2 = V::Aabb::default();
    assert_eq!(empty_aabb, empty_aabb2);
    assert_ne!(aabb2, empty_aabb2);
}