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
use std::ops::{AddAssign, SubAssign};
use std::vec::Vec;
use bvh::*;
use geom::*;
use collision::*;
use bounds::{BoundedBy};
use cgmath::prelude::*;
use cgmath::{Vector3, Point3, Zero, Rotation3};
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Mesh {
pub x: Vector3<f32>,
pub verts: Vec<Point3<f32>>,
pub faces: Vec<(usize, usize, usize)>,
pub bvh: BVH<AABB, usize>,
}
impl Mesh {
pub fn new() -> Self {
Mesh {
x: Vector3::zero(),
verts: Vec::new(),
faces: Vec::new(),
bvh: BVH::new(),
}
}
pub fn with_capacity(cap_verts: usize, cap_faces: usize) -> Self {
Mesh {
x: Vector3::zero(),
verts: Vec::with_capacity(cap_verts),
faces: Vec::with_capacity(cap_faces),
bvh: BVH::with_capacity(cap_faces),
}
}
pub fn push_vert(&mut self, p: Point3<f32>) -> usize {
let id = self.verts.len();
self.verts.push(p);
id
}
pub fn push_face(&mut self, f: (usize, usize, usize)) -> usize {
let a = self.verts[f.0];
let b = self.verts[f.1];
let c = self.verts[f.2];
let tri = Triangle::from((a, b, c));
let index = self.faces.len();
self.faces.push(f);
self.bvh.insert(&tri, index);
index
}
}
impl AddAssign<Vector3<f32>> for Mesh {
fn add_assign(&mut self, v: Vector3<f32>) {
self.x += v;
}
}
impl SubAssign<Vector3<f32>> for Mesh {
fn sub_assign(&mut self, v: Vector3<f32>) {
self.x -= v;
}
}
impl Shape for Mesh {
fn center(&self) -> Point3<f32> {
Point3::from_vec(self.x)
}
fn closest_point(&self, _to: Point3<f32>) -> Point3<f32> {
unimplemented!();
}
}
impl Volumetric for Mesh {
fn rotate<R: Rotation3<f32>>(mut self, rot: R) -> Mesh {
for vert in self.verts.iter_mut() {
*vert = rot.rotate_point(*vert);
}
self.bvh.clear();
for (i, &(a, b, c)) in self.faces.iter().enumerate() {
let tri = Triangle::from(
(self.verts[a], self.verts[b], self.verts[c])
);
self.bvh.insert(&tri, i);
}
self
}
}
impl<RHS> Contacts<RHS> for Mesh
where
RHS: Contacts<Triangle> + Contacts<Rectangle> + BoundedBy<AABB>
{
fn contacts<F: FnMut(Contact)>(&self, rhs: &RHS, mut callback: F) -> bool {
let mut collided = false;
self.bvh.query(&(rhs.bounds() - self.x), |&face_index| {
let (a, b, c) = self.faces[face_index];
let a = self.verts[a] + self.x;
let b = self.verts[b] + self.x;
let c = self.verts[c] + self.x;
let tri = Triangle::from((a, b, c));
rhs.contacts(&tri, |c| {
collided = true;
callback(Contact {
a: c.b,
b: c.a,
t: c.t,
n: -c.n,
});
});
});
collided
}
}
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ConvexMesh {
pub x: Vector3<f32>,
pub sum: Vector3<f32>,
pub verts: Vec<Point3<f32>>,
}
impl ConvexMesh {
pub fn new() -> Self {
ConvexMesh {
x: Vector3::zero(),
sum: Vector3::zero(),
verts: vec![],
}
}
pub fn with_capacity(cap: usize) -> Self {
ConvexMesh {
x: Vector3::zero(),
sum: Vector3::zero(),
verts: Vec::with_capacity(cap),
}
}
pub fn push(&mut self, p: Point3<f32>) {
let prev_center = self.sum / self.verts.len() as f32;
self.sum += p.to_vec();
self.verts.push(p);
let new_center = self.sum / self.verts.len() as f32;
let disp = new_center - prev_center;
self.x += disp;
}
}
impl From<Vec<Point3<f32>>> for ConvexMesh {
fn from(verts: Vec<Point3<f32>>) -> Self {
let mut sum = Vector3::zero();
for p in verts.iter() {
sum += p.to_vec();
}
ConvexMesh {
x: Vector3::zero(),
sum,
verts,
}
}
}
impl AddAssign<Vector3<f32>> for ConvexMesh {
fn add_assign(&mut self, v: Vector3<f32>) {
self.x += v;
}
}
impl SubAssign<Vector3<f32>> for ConvexMesh {
fn sub_assign(&mut self, v: Vector3<f32>) {
self.x -= v;
}
}
impl Shape for ConvexMesh {
fn center(&self) -> Point3<f32> {
Point3::from_vec(self.x + self.sum / self.verts.len() as f32)
}
fn closest_point(&self, _to: Point3<f32>) -> Point3<f32> {
unimplemented!();
}
}
impl Volumetric for ConvexMesh {
fn rotate<R: Rotation3<f32>>(mut self, rot: R) -> ConvexMesh {
let center = self.sum / self.verts.len() as f32;
for vert in self.verts.iter_mut() {
*vert = rot.rotate_point(*vert + -center) + center;
}
self
}
}
impl Convex for ConvexMesh {
fn support(&self, d: Vector3<f32>) -> Point3<f32> {
let mut best_vert = self.verts[0];
let mut best_norm = d.dot(self.verts[0].to_vec());
for vert in self.verts[1..].iter() {
let norm = d.dot(vert.to_vec());
if norm > best_norm {
best_vert = *vert;
best_norm = norm;
}
}
best_vert
}
}