@@ -218,17 +218,16 @@ impl<const D: usize> OrientedBoundingBox<D> {
218218 DefaultAllocator : Allocator < f64 , Const < D > , Const < D > , Buffer = ArrayStorage < f64 , D , D > >
219219 + Allocator < f64 , DimDiff < Const < D > , Const < 1 > > > ,
220220 {
221- let weights = points. par_iter ( ) . map ( |_| 1. ) . collect :: < Vec < _ > > ( ) ;
222- let mat = inertia_matrix ( & weights, points) ;
221+ let mat = inertia_matrix ( points) ;
223222 let vec = inertia_vector ( mat) ;
224- let base_change = householder_reflection ( & vec) ;
225- let obb_to_aabb = base_change . try_inverse ( ) . unwrap ( ) ;
223+ let aabb_to_obb = householder_reflection ( & vec) ;
224+ let obb_to_aabb = aabb_to_obb . try_inverse ( ) . unwrap ( ) ;
226225 let mapped = points. par_iter ( ) . map ( |p| obb_to_aabb * p) ;
227226 let aabb = BoundingBox :: from_points ( mapped) ?;
228227
229228 Some ( Self {
230229 aabb,
231- aabb_to_obb : base_change ,
230+ aabb_to_obb,
232231 obb_to_aabb,
233232 } )
234233 }
@@ -276,27 +275,17 @@ impl<const D: usize> OrientedBoundingBox<D> {
276275 }
277276}
278277
279- pub ( crate ) fn inertia_matrix < const D : usize > ( weights : & [ f64 ] , points : & [ PointND < D > ] ) -> Matrix < D > {
280- let total_weight = weights. par_iter ( ) . sum :: < f64 > ( ) ;
278+ fn inertia_matrix < const D : usize > ( points : & [ PointND < D > ] ) -> Matrix < D > {
279+ let centroid: PointND < D > = points. par_iter ( ) . sum ( ) ;
280+ let centroid: PointND < D > = centroid / points. len ( ) as f64 ;
281281
282- let centroid : PointND < D > = weights
282+ points
283283 . par_iter ( )
284- . zip ( points )
285- . fold_with ( PointND :: < D > :: from_element ( 0. ) , |acc , ( w , p ) | {
286- acc + p . map ( |e| e * w )
284+ . map ( |point| {
285+ let offset = point - centroid ;
286+ offset * offset . transpose ( )
287287 } )
288- . reduce_with ( |a, b| a + b)
289- . unwrap ( )
290- / total_weight;
291-
292- weights
293- . par_iter ( )
294- . zip ( points)
295- . fold_with ( Matrix :: from_element ( 0. ) , |acc, ( w, p) | {
296- acc + ( ( p - centroid) * ( p - centroid) . transpose ( ) ) . map ( |e| e * w)
297- } )
298- . reduce_with ( |a, b| a + b)
299- . unwrap ( )
288+ . sum ( )
300289}
301290
302291pub ( crate ) fn inertia_vector < const D : usize > ( mat : Matrix < D > ) -> PointND < D >
@@ -378,31 +367,27 @@ mod tests {
378367
379368 #[ test]
380369 fn test_inertia_matrix ( ) {
381- let points = vec ! [
370+ let points = [
382371 Point2D :: from ( [ 3. , 0. ] ) ,
383372 Point2D :: from ( [ 0. , 3. ] ) ,
384373 Point2D :: from ( [ 6. , -3. ] ) ,
385374 ] ;
386375
387- let weights = vec ! [ 1. ; 3 ] ;
388-
389- let mat = inertia_matrix ( & weights, & points) ;
376+ let mat = inertia_matrix ( & points) ;
390377 let expected = Matrix2 :: new ( 18. , -18. , -18. , 18. ) ;
391378
392379 assert_ulps_eq ! ( mat, expected) ;
393380 }
394381
395382 #[ test]
396383 fn test_inertia_vector_2d ( ) {
397- let points = vec ! [
384+ let points = [
398385 Point2D :: from ( [ 3. , 0. ] ) ,
399386 Point2D :: from ( [ 0. , 3. ] ) ,
400387 Point2D :: from ( [ 6. , -3. ] ) ,
401388 ] ;
402389
403- let weights = vec ! [ 1. ; 3 ] ;
404-
405- let mat = inertia_matrix ( & weights, & points) ;
390+ let mat = inertia_matrix ( & points) ;
406391 let vec = inertia_vector ( mat) ;
407392 let vec = Point3D :: from ( [ vec. x , vec. y , 0. ] ) ;
408393 let expected = Point3D :: from ( [ 1. , -1. , 0. ] ) ;
@@ -524,20 +509,16 @@ mod tests {
524509
525510 #[ test]
526511 fn test_inertia_vector_3d ( ) {
527- let points = vec ! [
512+ let points = [
528513 Point3D :: from ( [ 3. , 0. , 0. ] ) ,
529514 Point3D :: from ( [ 0. , 3. , 3. ] ) ,
530515 Point3D :: from ( [ 6. , -3. , -3. ] ) ,
531516 ] ;
532517
533- let weights = vec ! [ 1. ; 3 ] ;
534-
535- let mat = inertia_matrix ( & weights, & points) ;
518+ let mat = inertia_matrix ( & points) ;
536519 let vec = inertia_vector ( mat) ;
537520 let expected = Point3D :: from ( [ 1. , -1. , -1. ] ) ;
538521
539- eprintln ! ( "{}" , vec) ;
540-
541522 assert_relative_eq ! ( expected. cross( & vec) . norm( ) , 0. , epsilon = 1e-15 ) ;
542523 }
543524
0 commit comments