def test_translate_inertia(translation): shape = PlatonicFamily()("Cube") # Choose a volume > 1 to test the volume scaling, but don't choose one # that's too large because the uncentered polyhedral calculation has # massive error without fixing that. shape.volume = 2 shape.center = (0, 0, 0) translated_shape = ConvexPolyhedron(shape.vertices + translation) translated_inertia = translate_inertia_tensor( translation, shape.inertia_tensor, shape.volume ) mc_tensor = compute_inertia_mc(translated_shape.vertices, 1e4) assert np.allclose( translated_inertia, translated_shape._compute_inertia_tensor(False), atol=2e-1, rtol=2e-1, ) assert np.allclose( mc_tensor, translated_shape._compute_inertia_tensor(False), atol=2e-1, rtol=2e-1 ) assert np.allclose(mc_tensor, translated_inertia, atol=1e-2, rtol=1e-2) assert np.allclose(mc_tensor, translated_shape.inertia_tensor, atol=1e-2, rtol=1e-2)
def testfun(translation): translated_convex_cube = ConvexPolyhedron(convex_cube.vertices + translation) translated_inertia = translate_inertia_tensor( translation, convex_cube_inertia_tensor, convex_cube_volume) mc_tensor = compute_inertia_mc(translated_convex_cube.vertices, convex_cube_volume, 1e4) uncentered_inertia_tensor = translated_convex_cube._compute_inertia_tensor( False) assert np.allclose( translated_inertia, uncentered_inertia_tensor, atol=2e-1, rtol=2e-1, ) assert np.allclose(mc_tensor, uncentered_inertia_tensor, atol=2e-1, rtol=2e-1) assert np.allclose(mc_tensor, translated_inertia, atol=1e-2, rtol=1e-2) assert np.allclose(mc_tensor, translated_convex_cube.inertia_tensor, atol=1e-2, rtol=1e-2)
def test_moment_inertia_damasceno_shapes(shape): # These shapes pass the test for a sufficiently high number of samples, but # the number is too high to be worth running them regularly. bad_shapes = [ "Augmented Truncated Dodecahedron", "Deltoidal Hexecontahedron", "Disdyakis Triacontahedron", "Truncated Dodecahedron", "Truncated Icosidodecahedron", "Metabiaugmented Truncated Dodecahedron", "Pentagonal Hexecontahedron", "Paragyrate Diminished Rhombicosidodecahedron", "Square Cupola", "Triaugmented Truncated Dodecahedron", "Parabiaugmented Truncated Dodecahedron", ] if shape["name"] in ["RESERVED", "Sphere"] + bad_shapes: return np.random.seed(0) poly = ConvexPolyhedron(shape["vertices"]) num_samples = 1000 accept = False # Loop over different sampling rates to minimize the test runtime. while num_samples < 1e8: try: coxeter_result = poly.inertia_tensor mc_result = compute_inertia_mc(shape["vertices"], num_samples) assert np.allclose(coxeter_result, mc_result, atol=1e-1) accept = True break except AssertionError: num_samples *= 10 continue if not accept: raise AssertionError( "The test failed for shape {}.\nMC Result: " "\n{}\ncoxeter result: \n{}".format( shape["name"], mc_result, coxeter_result ) )