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
use crate::math::*;
use core::{
    prefab::{Prefab, PrefabComponent},
    Scalar,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum HaVolume {
    /// (radius)
    Sphere(Scalar),
    /// (half extents)
    Box(Vec3),
}

impl HaVolume {
    pub fn world_space_contains(&self, matrix: &Mat4, position: Vec3) -> Option<Scalar> {
        match self {
            Self::Sphere(radius) => {
                let origin = matrix.mul_point(Vec3::zero());
                let distance = origin.distance(position);
                if distance > *radius {
                    None
                } else {
                    Some(radius - distance)
                }
            }
            Self::Box(half_extents) => {
                let points = [
                    matrix.mul_point(Vec3::new(-half_extents.x, -half_extents.y, -half_extents.z)),
                    matrix.mul_point(Vec3::new(half_extents.x, -half_extents.y, -half_extents.z)),
                    matrix.mul_point(Vec3::new(half_extents.x, half_extents.y, -half_extents.z)),
                    matrix.mul_point(Vec3::new(-half_extents.x, half_extents.y, -half_extents.z)),
                    matrix.mul_point(Vec3::new(-half_extents.x, -half_extents.y, half_extents.z)),
                    matrix.mul_point(Vec3::new(half_extents.x, -half_extents.y, half_extents.z)),
                    matrix.mul_point(Vec3::new(half_extents.x, half_extents.y, half_extents.z)),
                    matrix.mul_point(Vec3::new(-half_extents.x, half_extents.y, half_extents.z)),
                ];
                let bbox = match BoundsVolume::from_points_cloud(points.into_iter()) {
                    Some(bbox) => bbox,
                    None => return None,
                };
                let diff = position - bbox.origin;
                let half_extents = bbox.half_extents();
                let x = half_extents.x - diff.x.abs();
                let y = half_extents.y - diff.y.abs();
                let z = half_extents.z - diff.z.abs();
                if x >= 0.0 && y >= 0.0 && z >= 0.0 {
                    Some(x.max(y).max(z))
                } else {
                    None
                }
            }
        }
    }

    #[allow(clippy::many_single_char_names)]
    pub fn world_space_overlaps(
        volume_a: &Self,
        matrix_a: &Mat4,
        volume_b: &Self,
        matrix_b: &Mat4,
    ) -> Option<Scalar> {
        match (volume_a, volume_b) {
            (Self::Sphere(a), Self::Sphere(b)) => {
                let origin_a = matrix_a.mul_point(Vec3::zero());
                let origin_b = matrix_b.mul_point(Vec3::zero());
                let distance = origin_a.distance(origin_b);
                let radius = a + b;
                if distance > radius {
                    None
                } else {
                    Some(radius - distance)
                }
            }
            (Self::Box(a), Self::Sphere(b)) => {
                let points = [
                    matrix_a.mul_point(Vec3::new(-a.x, -a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, -a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, -a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, -a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, a.y, a.z)),
                ];
                let bbox = match BoundsVolume::from_points_cloud(points.into_iter()) {
                    Some(bbox) => bbox,
                    None => return None,
                };
                let origin = matrix_b.mul_point(Vec3::zero());
                let point = bbox.closest_point_with_box(origin);
                let distance = origin.distance(point);
                if distance > *b {
                    None
                } else {
                    Some(b - distance)
                }
            }
            (Self::Box(a), Self::Box(b)) => {
                let points_a = [
                    matrix_a.mul_point(Vec3::new(-a.x, -a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, -a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, a.y, -a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, -a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, -a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(a.x, a.y, a.z)),
                    matrix_a.mul_point(Vec3::new(-a.x, a.y, a.z)),
                ];
                let bbox_a = match BoundsVolume::from_points_cloud(points_a.into_iter()) {
                    Some(bbox) => bbox,
                    None => return None,
                };
                let points_b = [
                    matrix_b.mul_point(Vec3::new(-b.x, -b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, -b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, -b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, -b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, b.y, b.z)),
                ];
                let bbox_b = match BoundsVolume::from_points_cloud(points_b.into_iter()) {
                    Some(bbox) => bbox,
                    None => return None,
                };
                let diff = bbox_a.origin - bbox_b.origin;
                let extents = bbox_a.half_extents() + bbox_b.half_extents();
                let x = extents.x - diff.x.abs();
                let y = extents.y - diff.y.abs();
                let z = extents.z - diff.z.abs();
                if x >= 0.0 && y >= 0.0 && z >= 0.0 {
                    Some(x.max(y).max(z))
                } else {
                    None
                }
            }
            (Self::Sphere(a), Self::Box(b)) => {
                let points = [
                    matrix_b.mul_point(Vec3::new(-b.x, -b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, -b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, b.y, -b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, -b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, -b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(b.x, b.y, b.z)),
                    matrix_b.mul_point(Vec3::new(-b.x, b.y, b.z)),
                ];
                let bbox = match BoundsVolume::from_points_cloud(points.into_iter()) {
                    Some(bbox) => bbox,
                    None => return None,
                };
                let origin = matrix_a.mul_point(Vec3::zero());
                let point = bbox.closest_point_with_box(origin);
                let distance = origin.distance(point);
                if distance > *a {
                    None
                } else {
                    Some(a - distance)
                }
            }
        }
    }
}

impl Prefab for HaVolume {}
impl PrefabComponent for HaVolume {}