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
use num::Zero;
use alga::general::Real;
use na::{Matrix1, Point2};
use na;
use ncollide::utils;
use ncollide::procedural::Polyline;
use ncollide::transformation;
use ncollide::shape::ConvexHull2;
use volumetric::Volumetric;
use math::{Point, AngularInertia};
pub fn convex_polyline_area_and_center_of_mass_unchecked<N: Real>(convex_polyline: &Polyline<Point<N>>)
-> (N, Point<N>) {
let geometric_center = utils::center(convex_polyline.coords());
let mut res = Point::origin();
let mut areasum = N::zero();
let mut iterpeek = convex_polyline.coords().iter().peekable();
let firstelement = *iterpeek.peek().unwrap();
while let Some(elem) = iterpeek.next() {
let area = utils::triangle_area(elem, iterpeek.peek().unwrap_or(&firstelement), &geometric_center);
let center = utils::triangle_center(elem, iterpeek.peek().unwrap_or(&firstelement), &geometric_center);
res = res + center.coords * area;
areasum = areasum + area;
}
if areasum.is_zero() {
(areasum, geometric_center)
}
else {
(areasum, res / areasum)
}
}
pub fn convex_polyline_mass_properties_unchecked<N: Real>(convex_polyline: &Polyline<Point<N>>,
density: N)
-> (N, Point<N>, N) {
let (area, com) = convex_polyline_area_and_center_of_mass_unchecked(convex_polyline);
if area.is_zero() {
return (na::zero(), com, na::zero());
}
let mut itot = N::zero();
let factor: N = na::convert(0.5 * 1.0/3.0);
let mut iterpeek = convex_polyline.coords().iter().peekable();
let firstelement = *iterpeek.peek().unwrap();
while let Some (elem) = iterpeek.next() {
let area = utils::triangle_area(&com, elem, iterpeek.peek().unwrap_or(&firstelement));
let e1 = *elem - com;
let e2 = **(iterpeek.peek().unwrap_or(&firstelement)) - com;
let ex1 = e1[0];
let ey1 = e1[1];
let ex2 = e2[0];
let ey2 = e2[1];
let intx2 = ex1 * ex1 + ex2 * ex1 + ex2 * ex2;
let inty2 = ey1 * ey1 + ey2 * ey1 + ey2 * ey2;
let ipart = factor * (intx2 + inty2);
itot = itot + ipart * area;
}
(area * density, com, itot * density)
}
pub fn convex_polyline_area_unchecked<N: Real>(convex_polyline: &Polyline<Point<N>>) -> N {
let geometric_center = utils::center(convex_polyline.coords());
let mut areasum = N::zero();
let mut iterpeek = convex_polyline.coords().iter().peekable();
let firstelement = *iterpeek.peek().unwrap();
while let Some(elem) = iterpeek.next() {
let area = utils::triangle_area(elem, iterpeek.peek().unwrap_or(&firstelement), &geometric_center);
areasum = areasum + area;
}
areasum
}
pub fn convex_hull_area<N: Real>(points: &[Point<N>]) -> N {
let convex_polyline = transformation::convex_hull2(points);
convex_polyline_area_unchecked(&convex_polyline)
}
pub fn convex_hull_volume<N: Real>(points: &[Point<N>]) -> N {
convex_hull_area(points)
}
pub fn convex_hull_center_of_mass<N: Real>(points: &[Point<N>]) -> Point<N> {
let convex_polyline = transformation::convex_hull2(points);
convex_polyline_area_and_center_of_mass_unchecked(&convex_polyline).1
}
pub fn convex_hull_unit_angular_inertia<N: Real>(points: &[Point<N>]) -> AngularInertia<N> {
let convex_polyline = transformation::convex_hull2(points);
let (area, _, i): (_, _, N) = convex_polyline_mass_properties_unchecked(&convex_polyline, na::one());
let mut tensor = AngularInertia::zero();
tensor[(0, 0)] = i * (N::one() / area);
tensor
}
impl<N: Real> Volumetric<N, Point2<N>, Matrix1<N>> for ConvexHull2<N> {
fn area(&self) -> N {
convex_hull_area(self.points())
}
fn volume(&self) -> N {
convex_hull_volume(self.points())
}
fn center_of_mass(&self) -> Point2<N> {
convex_hull_center_of_mass(self.points())
}
fn unit_angular_inertia(&self) -> Matrix1<N> {
convex_hull_unit_angular_inertia(self.points())
}
fn mass_properties(&self, density: N) -> (N, Point2<N>, Matrix1<N>) {
let convex_polyline = transformation::convex_hull2(self.points());
let (r1, r2, r3) = convex_polyline_mass_properties_unchecked(&convex_polyline, density);
(r1, r2, Matrix1::<N>::new(r3))
}
}
#[cfg(test)]
mod test {
#![allow(unused_imports)]
use na::{Vector2, Vector3, Matrix1, Point2};
use na;
use ncollide::shape::{ConvexHull2, ConvexHull3, Cuboid};
use ncollide::procedural;
use volumetric::Volumetric;
#[test]
#[cfg(feature = "dim3")]
fn test_inertia_tensor3() {
let excentricity = 10.0;
let mut shape = procedural::cuboid(&Vector3::new(2.0f64 - 0.08, 2.0 - 0.08, 2.0 - 0.08));
for c in shape.coords.iter_mut() {
c.x = c.x + excentricity;
c.y = c.y + excentricity;
c.z = c.z + excentricity;
}
let convex = ConvexHull3::new(shape.coords);
let cuboid = Cuboid::new(Vector3::new(0.96f64, 0.96, 0.96));
let actual = convex.unit_angular_inertia();
let expected = cuboid.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia tensors do not match: actual {:?}, expected: {:?}.", actual, expected));
let (actual_m, _, actual_i) = convex.mass_properties(2.37689);
let (expected_m, _, expected_i) = cuboid.mass_properties(2.37689);
assert!(relative_eq!(&actual, &expected),
format!("Unit inertia tensors do not match: actual {:?}, expected: {:?}.", actual, expected));
assert!(relative_eq!(actual_i, expected_i),
format!("Inertia tensors do not match: actual {:?}, expected: {:?}.", actual_i, expected_i));
assert!(relative_eq!(actual_m, expected_m),
format!("Masses do not match: actual {}, expected: {}.", actual_m, expected_m));
}
#[test]
#[cfg(feature = "dim2")]
fn test_inertia_tensor2() {
let a = 3.8f32;
let half_a = a / 2.0;
let real_moi = a.powf(2.0) / 6.0;
let expected = Matrix1::new(real_moi);
let cube = Cuboid::new(Vector2::new(half_a, half_a));
let actual = cube.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia values do not match: actual {:?}, expected: {:?}.", actual, expected));
let geom = {
let points = vec![ Point2::new(half_a, half_a), Point2::new(-half_a, half_a),
Point2::new(-half_a, -half_a), Point2::new(half_a, -half_a) ];
ConvexHull2::new(points)
};
let actual = geom.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia values do not match: actual {:?}, expected: {:?}.", actual, expected));
let a = 2.3f32;
let b = 6.7f32;
let half_a = a / 2.0;
let half_b = b / 2.0;
let real_moi = (1.0 / 12.0) * (a.powf(2.0) + b.powf(2.0));
let expected = Matrix1::new(real_moi);
let cube = Cuboid::new(Vector2::new(half_a, half_b));
let actual = cube.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia values do not match: actual {:?}, expected: {:?}.", actual, expected));
let geom = {
let points = vec![ Point2::new(half_a, half_b), Point2::new(-half_a, half_b),
Point2::new(-half_a, -half_b), Point2::new(half_a, -half_b) ];
ConvexHull2::new(points)
};
let actual = geom.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia values do not match: actual {:?}, expected: {:?}.", actual, expected));
let b = 6.7f32;
let h = 3.8f32;
let a = 2.3f32;
let c_x = a + b / 3.0;
let c_y = h / 3.0;
let area = b * h / 2.0;
let real_moi = (b.powf(3.0) * h - b.powf(2.0) * h * a + b * h * a.powf(2.0) + b * h.powf(3.0)) / (36.0 * area);
let expected = Matrix1::new(real_moi);
let geom = {
let points = vec![ Point2::new(0.0 - c_x, 0.0 - c_y), Point2::new(b - c_x, 0.0 - c_y),
Point2::new(a - c_x, h - c_y) ];
ConvexHull2::new(points)
};
let actual = geom.unit_angular_inertia();
assert!(relative_eq!(actual, expected),
format!("Inertia values do not match: actual {:?}, expected: {:?}.", actual, expected));
}
}