mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. 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
445
446
447
448
449
450
451
452
453
454
455
use glam::{DMat4, DVec3, DVec4, Vec4Swizzles};
use mln_style::geometry::Point;

fn ray_plane_intersection(origin: DVec3, direction: DVec3, plane: DVec4) -> Option<f64> {
    let normal = plane.xyz();
    let along = direction.dot(normal);
    if along == 0.0 {
        return None;
    }
    Some((-origin.dot(normal) - plane.w) / along)
}

/// Where a point is from a plane, as `ax + by + cz + d`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PlaneSide {
    Inside,
    /// On the plane, within `rounding`.
    On,
    Outside,
}

fn plane_side(plane: DVec4, point: DVec4, rounding: f64) -> PlaneSide {
    let distance = plane.dot(point);
    if distance > rounding {
        PlaneSide::Inside
    } else if distance < -rounding {
        PlaneSide::Outside
    } else {
        PlaneSide::On
    }
}

fn plane_through(p0: DVec3, p1: DVec3, p2: DVec3) -> DVec4 {
    let n = (p0 - p1).cross(p2 - p1).normalize();
    n.extend(-n.dot(p1))
}

/// The distance from the near plane to the point of the horizon circle
/// farthest along the view direction.
fn ideal_near_far_plane_distance(horizon_plane: DVec4, near_plane: DVec4) -> f64 {
    let view = near_plane.xyz();
    let horizon_length = horizon_plane.xyz().length();
    let horizon_normal = horizon_plane.xyz() / horizon_length;
    let horizon_distance = horizon_plane.w / horizon_length;
    let circle_radius = (1.0 - horizon_distance * horizon_distance).sqrt();
    let circle_center = horizon_normal * -horizon_distance;
    // The farthest point is `circle_radius` from the center along the view's
    // projection onto the circle's plane, `projected` long, which puts it
    // `circle_radius * projected` farther along the view. The projection
    // isn't normalized: looking straight down, it is rounding noise, whose
    // direction would put the point anywhere on the circle.
    let projected = (view - horizon_normal * view.dot(horizon_normal)).length();
    view.dot(circle_center) + circle_radius * projected + near_plane.w
}

/// Moves the far corners in along the frustum's edges until the far plane
/// reaches no further than the horizon. The near corners are the first four.
fn adjust_far_plane_by_horizon_plane(corners: &mut [DVec3; 8], near_plane_indices: [usize; 3], horizon_plane: DVec4) {
    const NEAR_OFFSET: usize = 0;
    const FAR_OFFSET: usize = 4;
    let mut ray_lengths = [0.0f64; 4];
    let mut ray_directions = [DVec3::ZERO; 4];
    let mut max_distance = 0.0;
    for i in 0..4 {
        let ray = corners[i + FAR_OFFSET] - corners[i + NEAR_OFFSET];
        ray_lengths[i] = ray.length();
        ray_directions[i] = ray / ray_lengths[i];
    }
    for i in 0..4 {
        let distance = ray_plane_intersection(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 [a, b, c] = near_plane_indices;
    let near_plane = plane_through(corners[a], corners[b], corners[c]);
    // The rays make the same angle with the near plane at all four corners.
    let ideal = ideal_near_far_plane_distance(horizon_plane, near_plane);
    max_distance = f64::min(max_distance, ideal / ray_directions[0].dot(near_plane.xyz()));
    for i in 0..4 {
        corners[i + FAR_OFFSET] = corners[i + NEAR_OFFSET] + ray_directions[i] * f64::min(max_distance, ray_lengths[i]);
    }
}

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

    for point in points {
        let projected_point = (*point - origin).dot(axis);
        min = f64::min(projected_point, min);
        max = f64::max(projected_point, max);
    }

    Point::new(min, max)
}

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

/// An axis-aligned box.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Aabb {
    pub(crate) min: DVec3,
    pub(crate) max: DVec3,
}

impl Aabb {
    #[must_use]
    pub(crate) fn new(min: DVec3, max: DVec3) -> Self {
        Aabb { min, max }
    }

    #[must_use]
    pub(crate) fn closest_point(&self, point: DVec3) -> DVec3 {
        point.min(self.max).max(self.min)
    }

    /// The distance along each axis from `point` to the box, 0 on the axes
    /// where the point is within it.
    #[must_use]
    pub(crate) fn distance_xyz(&self, point: DVec3) -> DVec3 {
        (self.closest_point(point) - point).abs()
    }

    /// The box's corners: those at its minimum z, then those at its maximum.
    fn corners(&self) -> [DVec3; 8] {
        let (min, max) = (self.min, self.max);
        [
            DVec3::new(min.x, min.y, min.z),
            DVec3::new(max.x, min.y, min.z),
            DVec3::new(max.x, max.y, min.z),
            DVec3::new(min.x, max.y, min.z),
            DVec3::new(min.x, min.y, max.z),
            DVec3::new(max.x, min.y, max.z),
            DVec3::new(max.x, max.y, max.z),
            DVec3::new(min.x, max.y, max.z),
        ]
    }

    /// How many of the first [`corners`](Self::corners) are distinct: four
    /// for a flat box, as its corners repeat.
    fn corner_count(&self) -> usize {
        if self.min.z == self.max.z { 4 } else { 8 }
    }

    /// The quadrant `idx` of the box on its XY-plane: 0 is the minimum x and
    /// y, 1 the maximum x, 2 the maximum y and 3 both maxima.
    #[must_use]
    pub(crate) fn quadrant(&self, idx: usize) -> Aabb {
        debug_assert!(idx < 4);
        let center = (self.min + self.max) * 0.5;
        let mut min = self.min;
        let mut max = self.max;
        if idx & 1 == 0 {
            max.x = center.x;
        } else {
            min.x = center.x;
        }
        if idx & 2 == 0 {
            max.y = center.y;
        } else {
            min.y = center.y;
        }
        Aabb::new(min, max)
    }

    #[must_use]
    pub(crate) fn intersects(&self, aabb: &Aabb) -> bool {
        self.min.cmple(aabb.max).all() && aabb.min.cmple(self.max).all()
    }
}

// Named indices of `Frustum::points`.
const NEAR_TL: usize = 0;
const NEAR_TR: usize = 1;
const NEAR_BR: usize = 2;
const NEAR_BL: usize = 3;
const FAR_TL: usize = 4;
const FAR_TR: usize = 5;
const FAR_BR: usize = 6;
const FAR_BL: usize = 7;

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

#[derive(Clone, Debug)]
pub(crate) struct Frustum {
    bounds: Aabb,
    points: [DVec3; 8],
    planes: [DVec4; 6],
    /// The separating axes the box's edges along x, then y, then z make with
    /// the frustum's edges, each with the frustum's extent along it.
    projections: [Projection; 18],
}

/// The separating axes from a box's edges along x and y. A box with height
/// adds the ones from its edges along z.
const FLAT_BOX_AXES: usize = 12;

impl Frustum {
    #[must_use]
    pub(crate) fn new(points: [DVec3; 8], planes: [DVec4; 6]) -> Self {
        let x_bounds = project_points_to_axis(&points, DVec3::ZERO, DVec3::X);
        let y_bounds = project_points_to_axis(&points, DVec3::ZERO, DVec3::Y);
        let z_bounds = project_points_to_axis(&points, DVec3::ZERO, DVec3::Z);

        let bounds =
            Aabb::new(DVec3::new(x_bounds.x, y_bounds.x, z_bounds.x), DVec3::new(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 = points;
        let frustum_edges = [
            p[NEAR_BR] - p[NEAR_BL],
            p[NEAR_TL] - p[NEAR_BL],
            p[FAR_TL] - p[NEAR_TL],
            p[FAR_TR] - p[NEAR_TR],
            p[FAR_BR] - p[NEAR_BR],
            p[FAR_BL] - p[NEAR_BL],
        ];

        let mut projections = [Projection::default(); 18];
        for (i, box_edge) in [DVec3::X, DVec3::Y, DVec3::Z].into_iter().enumerate() {
            for (j, edge) in frustum_edges.iter().enumerate() {
                let axis = box_edge.cross(*edge);
                projections[i * frustum_edges.len() + j] =
                    Projection { axis, projection: project_points_to_axis(&points, points[0], axis) };
            }
        }

        Frustum { bounds, points, planes, projections }
    }

    /// The frustum that `inv_proj` takes clip space's cube to, in the tile
    /// units of `zoom`. With `horizon_plane`, the far plane is pulled in to
    /// the horizon so nothing behind it counts as visible.
    #[must_use]
    pub(crate) fn from_inv_proj_matrix(
        inv_proj: &DMat4,
        world_size: f64,
        zoom: f64,
        flipped_y: bool,
        horizon_plane: Option<DVec4>,
    ) -> Frustum {
        let corners = Self::clip_corners(inv_proj).map(|corner| corner / world_size * 2.0f64.powf(zoom));
        Self::from_corners(corners, flipped_y, horizon_plane)
    }

    /// The frustum that `inv_proj` takes clip space's cube to, with x and y
    /// in the tile units of `zoom` and z left in the units `inv_proj` gives
    /// it, meters for a map's projection matrix. Boxes
    /// tested against it with [`intersects_box`](Self::intersects_box) take
    /// the same units.
    #[must_use]
    pub(crate) fn from_inv_proj_matrix_metres(inv_proj: &DMat4, world_size: f64, zoom: f64) -> Frustum {
        let scale = 2.0f64.powf(zoom);
        let corners = Self::clip_corners(inv_proj).map(|corner| {
            let xy = corner / world_size * scale;
            DVec3::new(xy.x, xy.y, corner.z)
        });
        Self::from_corners(corners, false, None)
    }

    /// The corners of clip space's cube through `inv_proj`.
    fn clip_corners(inv_proj: &DMat4) -> [DVec3; 8] {
        // The frustum's corners in normalized clip space.
        let clip_corners = [
            DVec3::new(-1.0, 1.0, -1.0),
            DVec3::new(1.0, 1.0, -1.0),
            DVec3::new(1.0, -1.0, -1.0),
            DVec3::new(-1.0, -1.0, -1.0),
            DVec3::new(-1.0, 1.0, 1.0),
            DVec3::new(1.0, 1.0, 1.0),
            DVec3::new(1.0, -1.0, 1.0),
            DVec3::new(-1.0, -1.0, 1.0),
        ];
        clip_corners.map(|corner| inv_proj.project_point3(corner))
    }

    fn from_corners(mut corners: [DVec3; 8], flipped_y: bool, horizon_plane: Option<DVec4>) -> Frustum {
        let mut frustum_plane_point_indices = [
            [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 corners, frustum_plane_point_indices[4], horizon_plane);
        }

        let planes = frustum_plane_point_indices.map(|[a, b, c]| plane_through(corners[a], corners[b], corners[c]));
        Frustum::new(corners, planes)
    }

    /// A conservative intersection test by the separating axis theorem: it
    /// may report an intersection where there is none (in under 1% of cases)
    /// for speed. A box that only touches the frustum is separate from it,
    /// and one that touches it from inside is within it.
    #[must_use]
    pub(crate) fn intersects(&self, aabb: &Aabb) -> IntersectionResult {
        // Each frustum plane together with 3 major axes define the separating
        // axes.
        if !self.bounds.intersects(aabb) {
            return IntersectionResult::Separate;
        }

        let corners = aabb.corners();
        let aabb_points = corners.map(|corner| corner.extend(1.0));
        let aabb_points = &aabb_points[..aabb.corner_count()];

        let mut fully_inside = true;

        // A plane is placed as precisely as the coordinates of the points it
        // passes through, which along each axis are as large as the frustum's.
        let magnitude = self.points.iter().fold(DVec3::ZERO, |magnitude, point| magnitude.max(point.abs()));
        for plane in &self.planes {
            let rounding = 1e-12 * plane.xyz().abs().dot(magnitude);
            let sides = aabb_points.iter().map(|point| plane_side(*plane, *point, rounding));
            let (inside, outside) = sides.fold((false, false), |(inside, outside), side| {
                (inside || side == PlaneSide::Inside, outside || side == PlaneSide::Outside)
            });
            if !inside {
                // Separating axis found, no intersection
                return IntersectionResult::Separate;
            }
            if outside {
                fully_inside = false;
            }
        }

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

    /// A precise intersection test by the separating axis theorem. With
    /// `edge_cases_only`, it tests only the axes that
    /// [`intersects`](Self::intersects) doesn't.
    #[must_use]
    pub(crate) 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 corners = aabb.corners();
        let aabb_points = &corners[..aabb.corner_count()];
        let axes = if aabb.min.z == aabb.max.z { FLAT_BOX_AXES } else { self.projections.len() };

        // 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[..axes] {
            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
    }

    /// A conservative intersection test of a box of any height: the box is
    /// separate if all of it is behind one plane, or outside the frustum's
    /// bounds along an axis. It may report an intersection where there is
    /// none. A box that only touches a plane from outside intersects it.
    #[must_use]
    pub(crate) fn intersects_box(&self, aabb: &Aabb) -> IntersectionResult {
        let mut fully_inside = true;
        for plane in &self.planes {
            // The plane's distance to the box's corners nearest and farthest
            // along its normal, from the plane's offset, summed x, y, then z.
            let mut distance_min = plane.w;
            let mut distance_max = plane.w;
            for axis in 0..3 {
                let (near, far) =
                    if plane[axis] > 0.0 { (aabb.min[axis], aabb.max[axis]) } else { (aabb.max[axis], aabb.min[axis]) };
                distance_min += plane[axis] * near;
                distance_max += plane[axis] * far;
            }
            if distance_max < 0.0 {
                return IntersectionResult::Separate;
            }
            if distance_min < 0.0 {
                fully_inside = false;
            }
        }
        if fully_inside {
            return IntersectionResult::Contains;
        }
        if !self.bounds.intersects(aabb) {
            return IntersectionResult::Separate;
        }
        IntersectionResult::Intersects
    }

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

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

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

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