@@ -182,7 +182,9 @@ pub fn trimesh_signed_volume_and_center_of_mass(
182182
183183#[ cfg( test) ]
184184mod test {
185- use crate :: math:: Vector ;
185+ use na:: UnitQuaternion ;
186+
187+ use crate :: math:: { Isometry , Vector } ;
186188 use crate :: {
187189 mass_properties:: MassProperties ,
188190 shape:: { Ball , Capsule , Cone , Cuboid , Cylinder , Shape } ,
@@ -283,4 +285,58 @@ mod test {
283285 ) ;
284286 }
285287 }
288+
289+ #[ test]
290+ fn inertia_tensor ( ) {
291+ let cuboid = Cuboid :: new ( Vector :: new ( 1.0 , 2.0 , 3.0 ) ) ;
292+ let density = 1.0 ;
293+
294+ // Compute mass properties with a translated and rotated cuboid.
295+ let isometry = Isometry :: new ( Vector :: new ( 5.0 , 2.0 , 3.0 ) , Vector :: new ( 0.6 , 0.7 , 0.8 ) ) ;
296+ let mprops = cuboid. mass_properties ( density) ;
297+
298+ // Compute mass properties with a manually transformed cuboid.
299+ let mut vertices = cuboid. to_trimesh ( ) . 0 ;
300+ let trimesh_origin_mprops =
301+ MassProperties :: from_trimesh ( density, & vertices, & cuboid. to_trimesh ( ) . 1 ) ;
302+ vertices. iter_mut ( ) . for_each ( |v| * v = isometry * * v) ;
303+ let trimesh_transformed_mprops =
304+ MassProperties :: from_trimesh ( density, & vertices, & cuboid. to_trimesh ( ) . 1 ) ;
305+
306+ // Compare the mass properties.
307+ assert_relative_eq ! (
308+ mprops. mass( ) ,
309+ trimesh_origin_mprops. mass( ) ,
310+ epsilon = 1.0e-4
311+ ) ;
312+ assert_relative_eq ! (
313+ mprops. mass( ) ,
314+ trimesh_transformed_mprops. mass( ) ,
315+ epsilon = 1.0e-4
316+ ) ;
317+ // compare local_com
318+ assert_relative_eq ! (
319+ isometry * mprops. local_com,
320+ trimesh_transformed_mprops. local_com,
321+ epsilon = 1.0e-4
322+ ) ;
323+ assert_relative_eq ! (
324+ isometry * trimesh_origin_mprops. local_com,
325+ trimesh_transformed_mprops. local_com,
326+ epsilon = 1.0e-4
327+ ) ;
328+ assert_relative_eq ! (
329+ trimesh_origin_mprops. principal_inertia( ) ,
330+ mprops. principal_inertia( ) ,
331+ epsilon = 1.0e-4
332+ ) ;
333+ let w1 = trimesh_origin_mprops. world_inv_inertia_sqrt ( & isometry. rotation ) ;
334+ let w2 = trimesh_transformed_mprops. world_inv_inertia_sqrt ( & UnitQuaternion :: identity ( ) ) ;
335+ assert_relative_eq ! ( w1. m11, w2. m11, epsilon = 1.0e-7 , ) ;
336+ assert_relative_eq ! ( w1. m12, w2. m12, epsilon = 1.0e-7 , ) ;
337+ assert_relative_eq ! ( w1. m13, w2. m13, epsilon = 1.0e-7 , ) ;
338+ assert_relative_eq ! ( w1. m22, w2. m22, epsilon = 1.0e-7 , ) ;
339+ assert_relative_eq ! ( w1. m23, w2. m23, epsilon = 1.0e-7 , ) ;
340+ assert_relative_eq ! ( w1. m33, w2. m33, epsilon = 1.0e-7 , ) ;
341+ }
286342}
0 commit comments