mln-rs-util 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use geometry::Point;

use crate::mat3::{vec3_add, vec3_cross, vec3_dot, vec3_length, vec3_normalize, vec3_scale, vec3_sub};
use crate::mat4::{Mat4, matrix};
use crate::vectors::{Vec3, Vec3i, Vec4};

fn to_vec3(v: &Vec4) -> Vec3 {
    [v[0], v[1], v[2]]
}

fn vec4_dot(a: &Vec4, b: &Vec4) -> f64 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
}

fn ray_plane_intersection(origin: &Vec3, direction: &Vec3, plane: &Vec4) -> Option<f64> {
    let normal: Vec3 = [plane[0], plane[1], plane[2]];
    let along = vec3_dot(direction, &normal);
    if along == 0.0 {
        return None;
    }
    Some((-vec3_dot(origin, &normal) - plane[3]) / along)
}

fn plane_through(p0: &Vec3, p1: &Vec3, p2: &Vec3) -> Vec4 {
    let n = vec3_normalize(&vec3_cross(&vec3_sub(p0, p1), &vec3_sub(p2, p1)));
    [n[0], n[1], n[2], -vec3_dot(&n, p1)]
}

/// The distance from the near plane to the point of the horizon circle
/// farthest along the view direction, if the camera is not looking straight
/// down.
fn ideal_near_far_plane_distance(horizon_plane: &Vec4, near_plane: &Vec4) -> Option<f64> {
    let view: Vec3 = [near_plane[0], near_plane[1], near_plane[2]];
    let horizon_length = vec3_length(&[horizon_plane[0], horizon_plane[1], horizon_plane[2]]);
    let horizon_normal = vec3_scale(&[horizon_plane[0], horizon_plane[1], horizon_plane[2]], 1.0 / horizon_length);
    let horizon_distance = horizon_plane[3] / horizon_length;
    let projected_view = vec3_sub(&view, &vec3_scale(&horizon_normal, vec3_dot(&view, &horizon_normal)));
    let projected_length = vec3_length(&projected_view);
    if projected_length <= 0.0 {
        return None;
    }
    let circle_radius = (1.0 - horizon_distance * horizon_distance).sqrt();
    let circle_center = vec3_scale(&horizon_normal, -horizon_distance);
    let farthest = vec3_add(&circle_center, &vec3_scale(&projected_view, circle_radius / projected_length));
    Some(vec3_dot(&view, &farthest) + near_plane[3])
}

/// Moves the far corners in along the frustum's edges until the far plane
/// reaches no further than the horizon; port of GL JS
/// `adjustFarPlaneByHorizonPlane`. The near corners are the first four.
fn adjust_far_plane_by_horizon_plane(corners: &mut [Vec4; 8], near_plane_indices: &Vec3i, horizon_plane: &Vec4) {
    const NEAR_OFFSET: usize = 0;
    const FAR_OFFSET: usize = 4;
    let mut ray_lengths = [0.0f64; 4];
    let mut ray_directions: [Vec3; 4] = [[0.0; 3]; 4];
    let mut max_distance = 0.0;
    for i in 0..4 {
        let ray = vec3_sub(&to_vec3(&corners[i + FAR_OFFSET]), &to_vec3(&corners[i + NEAR_OFFSET]));
        ray_lengths[i] = vec3_length(&ray);
        ray_directions[i] = vec3_scale(&ray, 1.0 / ray_lengths[i]);
    }
    for i in 0..4 {
        let distance = ray_plane_intersection(&to_vec3(&corners[i + NEAR_OFFSET]), &ray_directions[i], horizon_plane);
        // Rays parallel to the horizon or pointing away from it keep their
        // length.
        let length = match distance {
            Some(distance) if distance >= 0.0 => distance,
            _ => ray_lengths[i],
        };
        max_distance = f64::max(max_distance, length);
    }
    let near_plane = plane_through(
        &to_vec3(&corners[near_plane_indices[0] as usize]),
        &to_vec3(&corners[near_plane_indices[1] as usize]),
        &to_vec3(&corners[near_plane_indices[2] as usize]),
    );
    if let Some(ideal) = ideal_near_far_plane_distance(horizon_plane, &near_plane) {
        // The rays make the same angle with the near plane at all four
        // corners.
        max_distance = f64::min(
            max_distance,
            ideal / vec3_dot(&ray_directions[0], &[near_plane[0], near_plane[1], near_plane[2]]),
        );
    }
    for i in 0..4 {
        let far = vec3_add(
            &to_vec3(&corners[i + NEAR_OFFSET]),
            &vec3_scale(&ray_directions[i], f64::min(max_distance, ray_lengths[i])),
        );
        corners[i + FAR_OFFSET] = [far[0], far[1], far[2], 1.0];
    }
}

fn project_points_to_axis<const N: usize>(points: &[Vec3; N], origin: &Vec3, axis: &Vec3) -> Point<f64> {
    let mut min = f64::MAX;
    let mut max = -f64::MAX;

    for point in points {
        let projected_point = vec3_dot(&vec3_sub(point, origin), axis);
        min = f64::min(projected_point, min);
        max = f64::max(projected_point, max);
    }

    Point::new(min, max)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum IntersectionResult {
    Separate,
    Intersects,
    Contains,
}

/// The derived `PartialEq` is the C++ operator==: `min == aabb.min && max ==
/// aabb.max` (`std::array` element-wise ==).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AABB {
    pub(crate) min: Vec3,
    pub(crate) max: Vec3,
}

impl AABB {
    #[must_use]
    pub fn new(min: Vec3, max: Vec3) -> Self {
        AABB { min, max }
    }

    #[must_use]
    pub(crate) fn closest_point(&self, point: &Vec3) -> Vec3 {
        [
            f64::max(f64::min(self.max[0], point[0]), self.min[0]),
            f64::max(f64::min(self.max[1], point[1]), self.min[1]),
            f64::max(f64::min(self.max[2], point[2]), self.min[2]),
        ]
    }

    /// Computes the shortest manhattan distance to the provided point
    #[must_use]
    pub fn distance_xyz(&self, point: &Vec3) -> Vec3 {
        let mut vec = vec3_sub(&self.closest_point(point), point);

        vec[0] = vec[0].abs();
        vec[1] = vec[1].abs();
        vec[2] = vec[2].abs();

        vec
    }

    /// Creates an aabb covering one quadrant of the aabb on XZ-plane.
    #[must_use]
    pub fn quadrant(&self, idx: i32) -> AABB {
        debug_assert!((0..4).contains(&idx));
        let mut quadrant_min = self.min;
        let mut quadrant_max = self.max;
        let x_center = f64::midpoint(self.max[0], self.min[0]);
        let y_center = f64::midpoint(self.max[1], self.min[1]);

        // This aabb is split into 4 quadrants. For each axis define in which side
        // of the split "idx" is The result for indices 0, 1, 2, 3 is: { 0, 0 }, {
        // 1, 0 }, { 0, 1 }, { 1, 1 }
        let x_split: [i32; 4] = [0, 1, 0, 1];
        let y_split: [i32; 4] = [0, 0, 1, 1];
        let idx = idx as usize;

        quadrant_min[0] = if x_split[idx] != 0 { x_center } else { quadrant_min[0] };
        quadrant_max[0] = if x_split[idx] != 0 { quadrant_max[0] } else { x_center };

        quadrant_min[1] = if y_split[idx] != 0 { y_center } else { quadrant_min[1] };
        quadrant_max[1] = if y_split[idx] != 0 { quadrant_max[1] } else { y_center };

        AABB::new(quadrant_min, quadrant_max)
    }

    #[must_use]
    pub(crate) fn intersects(&self, aabb: &AABB) -> bool {
        if self.min[0] > aabb.max[0] || aabb.min[0] > self.max[0] {
            return false;
        }
        if self.min[1] > aabb.max[1] || aabb.min[1] > self.max[1] {
            return false;
        }
        if self.min[2] > aabb.max[2] || aabb.min[2] > self.max[2] {
            return false;
        }
        true
    }
}

// Named index values for frustum::points array
const NEAR_TL: i32 = 0;
const NEAR_TR: i32 = 1;
const NEAR_BR: i32 = 2;
const NEAR_BL: i32 = 3;
const FAR_TL: i32 = 4;
const FAR_TR: i32 = 5;
const FAR_BR: i32 = 6;
const FAR_BL: i32 = 7;

#[derive(Clone, Copy, Debug, Default)]
struct Projection {
    axis: Vec3,
    projection: Point<f64>,
}

#[derive(Clone, Debug)]
pub struct Frustum {
    bounds: AABB,
    points: [Vec3; 8],
    planes: [Vec4; 6],
    projections: [Projection; 12],
}

impl Frustum {
    #[must_use]
    pub(crate) fn new(points: &[Vec3; 8], planes: &[Vec4; 6]) -> Self {
        let points = *points;
        let planes = *planes;

        let x_bounds = project_points_to_axis(&points, &[0.0, 0.0, 0.0], &[1.0, 0.0, 0.0]);
        let y_bounds = project_points_to_axis(&points, &[0.0, 0.0, 0.0], &[0.0, 1.0, 0.0]);
        let z_bounds = project_points_to_axis(&points, &[0.0, 0.0, 0.0], &[0.0, 0.0, 1.0]);

        let bounds = AABB::new([x_bounds.x, y_bounds.x, z_bounds.x], [x_bounds.y, y_bounds.y, z_bounds.y]);

        // Precompute a set of separating axis candidates for precise intersection
        // tests. Remaining axes not covered in basic intersection tests are: axis[]
        // = (edges of aabb) x (edges of frustum)
        let p = |i: i32| &points[i as usize];
        let frustum_edges: [Vec3; 6] = [
            vec3_sub(p(NEAR_BR), p(NEAR_BL)),
            vec3_sub(p(NEAR_TL), p(NEAR_BL)),
            vec3_sub(p(FAR_TL), p(NEAR_TL)),
            vec3_sub(p(FAR_TR), p(NEAR_TR)),
            vec3_sub(p(FAR_BR), p(NEAR_BR)),
            vec3_sub(p(FAR_BL), p(NEAR_BL)),
        ];

        let mut projections = [Projection::default(); 12];
        for (i, edge) in frustum_edges.iter().enumerate() {
            // Cross product [1, 0, 0] x [a, b, c] == [0, -c, b]
            // Cross product [0, 1, 0] x [a, b, c] == [c, 0, -a]
            let axis0: Vec3 = [0.0, -edge[2], edge[1]];
            let axis1: Vec3 = [edge[2], 0.0, -edge[0]];

            projections[i * 2] =
                Projection { axis: axis0, projection: project_points_to_axis(&points, &points[0], &axis0) };
            projections[i * 2 + 1] =
                Projection { axis: axis1, projection: project_points_to_axis(&points, &points[0], &axis1) };
        }

        Frustum { bounds, points, planes, projections }
    }

    /// With `horizon_plane`, the far plane is pulled in to the horizon so
    /// nothing behind it counts as visible.
    #[must_use]
    pub fn from_inv_proj_matrix(
        inv_proj: &Mat4,
        world_size: f64,
        zoom: f64,
        flipped_y: bool,
        horizon_plane: Option<&Vec4>,
    ) -> Frustum {
        // Define frustum corner points in normalized clip space
        let mut corner_coords: [Vec4; 8] = [
            [-1.0, 1.0, -1.0, 1.0],
            [1.0, 1.0, -1.0, 1.0],
            [1.0, -1.0, -1.0, 1.0],
            [-1.0, -1.0, -1.0, 1.0],
            [-1.0, 1.0, 1.0, 1.0],
            [1.0, 1.0, 1.0, 1.0],
            [1.0, -1.0, 1.0, 1.0],
            [-1.0, -1.0, 1.0, 1.0],
        ];

        let scale = 2.0f64.powf(zoom);

        // Transform points to tile space
        for coord in &mut corner_coords {
            let a = *coord;
            matrix::transform_mat4(coord, &a, inv_proj);
            // `component *= 1.0 / coord[3] / worldSize * scale` for each
            // component in order: coord[3] is the last one written.
            for i in 0..4 {
                coord[i] *= 1.0 / coord[3] / world_size * scale;
            }
        }

        let mut frustum_plane_point_indices: [Vec3i; 6] = [
            [NEAR_BL, NEAR_BR, FAR_BR],  // bottom
            [NEAR_TL, NEAR_BL, FAR_BL],  // left
            [NEAR_BR, NEAR_TR, FAR_TR],  // right
            [NEAR_TL, FAR_TL, FAR_TR],   // top
            [NEAR_TL, NEAR_TR, NEAR_BR], // near
            [FAR_BR, FAR_TR, FAR_TL],    // far
        ];

        if flipped_y {
            for tri in &mut frustum_plane_point_indices {
                tri.swap(1, 2);
            }
        }

        if let Some(horizon_plane) = horizon_plane {
            adjust_far_plane_by_horizon_plane(&mut corner_coords, &frustum_plane_point_indices[4], horizon_plane);
        }

        let mut frustum_planes: [Vec4; 6] = [[0.0; 4]; 6];

        for (i, indices) in frustum_plane_point_indices.iter().enumerate() {
            frustum_planes[i] = plane_through(
                &to_vec3(&corner_coords[indices[0] as usize]),
                &to_vec3(&corner_coords[indices[1] as usize]),
                &to_vec3(&corner_coords[indices[2] as usize]),
            );
        }

        let mut frustum_points: [Vec3; 8] = [[0.0; 3]; 8];

        for i in 0..corner_coords.len() {
            frustum_points[i] = to_vec3(&corner_coords[i]);
        }

        Frustum::new(&frustum_points, &frustum_planes)
    }

    /// Performs conservative intersection test using separating axis theorem.
    /// Some accuracy is traded for better performance. False positive rate is < 1%
    #[must_use]
    pub fn intersects(&self, aabb: &AABB) -> IntersectionResult {
        // Execute separating axis test between two convex objects to find intersections
        // Each frustum plane together with 3 major axes define the separating axes
        // This implementation is conservative as it's not checking all possible axes.
        // False positive rate is ~0.5% of all cases (see intersectsPrecise).
        // Note: test only 4 points as both min and max points have zero elevation
        debug_assert!(aabb.min[2] == 0.0 && aabb.max[2] == 0.0);

        if !self.bounds.intersects(aabb) {
            return IntersectionResult::Separate;
        }

        let aabb_points: [Vec4; 4] = [
            [aabb.min[0], aabb.min[1], 0.0, 1.0],
            [aabb.max[0], aabb.min[1], 0.0, 1.0],
            [aabb.max[0], aabb.max[1], 0.0, 1.0],
            [aabb.min[0], aabb.max[1], 0.0, 1.0],
        ];

        let mut fully_inside = true;

        let epsilon = 1e-10;

        for plane in &self.planes {
            let mut points_inside: usize = 0;

            points_inside += usize::from(vec4_dot(plane, &aabb_points[0]) >= -epsilon);
            points_inside += usize::from(vec4_dot(plane, &aabb_points[1]) >= -epsilon);
            points_inside += usize::from(vec4_dot(plane, &aabb_points[2]) >= -epsilon);
            points_inside += usize::from(vec4_dot(plane, &aabb_points[3]) >= -epsilon);

            if points_inside == 0 {
                // Separating axis found, no intersection
                return IntersectionResult::Separate;
            }

            if points_inside != aabb_points.len() {
                fully_inside = false;
            }
        }

        if fully_inside { IntersectionResult::Contains } else { IntersectionResult::Intersects }
    }

    /// Performs precise intersection test using separating axis theorem.
    /// It is possible run only edge cases that were not covered in `intersects()`
    /// (C++ default: `edgeCasesOnly = false`).
    #[must_use]
    pub fn intersects_precise(&self, aabb: &AABB, edge_cases_only: bool) -> IntersectionResult {
        if !edge_cases_only {
            let result = self.intersects(aabb);

            if result == IntersectionResult::Separate {
                return result;
            }
        }

        let aabb_points: [Vec3; 4] = [
            [aabb.min[0], aabb.min[1], 0.0],
            [aabb.max[0], aabb.min[1], 0.0],
            [aabb.max[0], aabb.max[1], 0.0],
            [aabb.min[0], aabb.max[1], 0.0],
        ];

        // For a precise SAT-test all edge cases needs to be covered
        // Projections of the frustum on separating axis candidates have been precomputed already
        for proj in &self.projections {
            let projected_aabb = project_points_to_axis(&aabb_points, &self.points[0], &proj.axis);
            let projected_frustum = &proj.projection;

            if projected_frustum.y < projected_aabb.x || projected_frustum.x > projected_aabb.y {
                return IntersectionResult::Separate;
            }
        }

        IntersectionResult::Intersects
    }

    #[must_use]
    pub fn get_points(&self) -> &[Vec3; 8] {
        &self.points
    }

    #[must_use]
    pub fn get_planes(&self) -> &[Vec4; 6] {
        &self.planes
    }

    /// This frustum with its far plane `distance` farther from `camera`, GL
    /// JS's `pushFrustumFarPlane`. Each far corner moves along its ray from
    /// the camera to the moved plane, so the frustum stays a pyramid.
    #[must_use]
    pub fn with_far_plane_pushed(&self, distance: f64, camera: &Vec3) -> Frustum {
        const FAR: usize = 5;
        let far = self.planes[FAR];
        let normal: Vec3 = [far[0], far[1], far[2]];
        let mut points = self.points;
        for point in &mut points[FAR_TL as usize..=FAR_BL as usize] {
            let ray = vec3_sub(point, camera);
            // The plane's normal points back into the frustum.
            let ray_dot_normal = vec3_dot(&ray, &normal);
            if ray_dot_normal < -1e-9 {
                *point = vec3_add(point, &vec3_scale(&ray, distance / -ray_dot_normal));
            }
        }
        let mut planes = self.planes;
        planes[FAR][3] += distance;
        Frustum::new(&points, &planes)
    }
}

#[cfg(test)]
#[path = "bounding_volumes_test.rs"]
mod bounding_volumes_test;