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
use crate::math::Vector;
#[cfg(feature = "alloc")]
use crate::{math::Vector3, query::details::NormalConstraints};
// NOTE: ideally, the normal cone should take into account the point where the normal cone is
// considered. But as long as we assume that the triangles are one-way we can get away with
// just relying on the normal directions.
// Taking the point into account would be technically doable (and desirable if we wanted
// to define, e.g., a one-way mesh) but requires:
// 1. To make sure the edge pseudo-normals are given in the correct edge order.
// 2. To have access to the contact feature.
// We can have access to both during the narrow-phase, but leave that as a future
// potential improvements.
// NOTE: this isn’t equal to the "true" normal cones since concave edges will have pseudo-normals
// still pointing outward (instead of inward or being empty).
/// The pseudo-normals of a triangle providing approximations of its feature’s normal cones.
#[derive(Clone, Debug)]
pub struct TrianglePseudoNormals {
/// The triangle’s face normal.
pub face: Vector,
// TODO: if we switch this to providing pseudo-normals in a specific order
// (e.g. in the same order as the triangle’s edges), then we should
// think of fixing that order in the heightfield
// triangle_pseudo_normals code.
/// The edges pseudo-normals, in no particular order.
pub edges: [Vector; 3],
}
#[cfg(feature = "alloc")]
impl NormalConstraints for TrianglePseudoNormals {
/// Projects the given direction to it is contained in the polygonal
/// cone defined `self`.
fn project_local_normal_mut(&self, dir: &mut Vector) -> bool {
// Find the closest pseudo-normal.
let dots = Vector3::new(
dir.dot(self.edges[0]),
dir.dot(self.edges[1]),
dir.dot(self.edges[2]),
);
let closest_edge = self.edges[dots.max_position()];
crate::shape::pseudo_normals::project_into_cone(self.face, closest_edge, dir)
}
}
#[cfg(test)]
#[cfg(all(feature = "dim3", feature = "alloc"))]
mod test {
use super::NormalConstraints;
use crate::math::{Real, Vector};
use crate::shape::TrianglePseudoNormals;
fn bisector(v1: Vector, v2: Vector) -> Vector {
(v1 + v2).normalize()
}
fn bisector_y(v: Vector) -> Vector {
bisector(v, Vector::Y)
}
#[test]
fn trivial_pseudo_normals_projection() {
let pn = TrianglePseudoNormals {
face: Vector::Y,
edges: [Vector::Y; 3],
};
assert_eq!(
pn.project_local_normal(Vector::new(1.0, 1.0, 1.0)),
Some(Vector::Y)
);
assert!(pn.project_local_normal(-Vector::Y).is_none());
}
#[test]
fn edge_pseudo_normals_projection_strictly_positive() {
let bisector = |v1: Vector, v2: Vector| (v1 + v2).normalize();
let bisector_y = |v: Vector| bisector(v, Vector::Y);
// The normal cones for this test will be fully contained in the +Y half-space.
let cones_ref_dir = [
-Vector::Z,
-Vector::X,
Vector::new(1.0, 0.0, 1.0).normalize(),
];
let cones_ends = cones_ref_dir.map(bisector_y);
let cones_axes = cones_ends.map(bisector_y);
let pn = TrianglePseudoNormals {
face: Vector::Y,
edges: cones_axes.map(|v| v.normalize()),
};
for i in 0..3 {
assert!(pn
.project_local_normal(cones_ends[i])
.unwrap()
.abs_diff_eq(cones_ends[i], 1.0e-5));
assert_eq!(pn.project_local_normal(cones_axes[i]), Some(cones_axes[i]));
// Guaranteed to be inside the normal cone of edge i.
let subdivs = 100;
for k in 1..100 {
let v = Vector::Y
.lerp(cones_ends[i], k as Real / (subdivs as Real))
.normalize();
assert_eq!(pn.project_local_normal(v).unwrap(), v);
}
// Guaranteed to be outside the normal cone of edge i.
for k in 1..subdivs {
let v = cones_ref_dir[i]
.lerp(cones_ends[i], k as Real / (subdivs as Real))
.normalize();
assert!(pn
.project_local_normal(v)
.unwrap()
.abs_diff_eq(cones_ends[i], 1.0e-5));
}
// Guaranteed to be outside the normal cone, and in the -Y half-space.
for k in 1..subdivs {
let v = cones_ref_dir[i]
.lerp(-Vector::Y, k as Real / (subdivs as Real))
.normalize();
assert!(pn.project_local_normal(v).is_none(),);
}
}
}
#[test]
fn edge_pseudo_normals_projection_negative() {
// The normal cones for this test will be fully contained in the +Y half-space.
let cones_ref_dir = [
-Vector::Z,
-Vector::X,
Vector::new(1.0, 0.0, 1.0).normalize(),
];
let cones_ends = cones_ref_dir.map(|v| bisector(v, -Vector::Y));
let cones_axes = [
bisector(bisector_y(cones_ref_dir[0]), cones_ref_dir[0]),
bisector(bisector_y(cones_ref_dir[1]), cones_ref_dir[1]),
bisector(bisector_y(cones_ref_dir[2]), cones_ref_dir[2]),
];
let pn = TrianglePseudoNormals {
face: Vector::Y,
edges: cones_axes.map(|v| v.normalize()),
};
for i in 0..3 {
assert_eq!(pn.project_local_normal(cones_axes[i]), Some(cones_axes[i]));
// Guaranteed to be inside the normal cone of edge i.
let subdivs = 100;
for k in 1..subdivs {
let v = Vector::Y
.lerp(cones_ends[i], k as Real / (subdivs as Real))
.normalize();
assert_eq!(pn.project_local_normal(v).unwrap(), v);
}
// Guaranteed to be outside the normal cone of edge i.
// Since it is additionally guaranteed to be in the -Y half-space, we should get None.
for k in 1..subdivs {
let v = (-Vector::Y)
.lerp(cones_ends[i], k as Real / (subdivs as Real))
.normalize();
assert!(pn.project_local_normal(v).is_none());
}
}
}
}