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_translate_inertia(translation): shape = PlatonicFamily.get_shape("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 test_inertia_tensor(r, center): sphere = Sphere(r) assert np.all(sphere.inertia_tensor >= 0) volume = 4 / 3 * np.pi * r**3 expected = [2 / 5 * volume * r**2] * 3 np.testing.assert_allclose(np.diag(sphere.inertia_tensor), expected) sphere.center = center expected = translate_inertia_tensor(center, np.diag(expected), volume) np.testing.assert_allclose(sphere.inertia_tensor, expected)
def test_inertia_tensor(a, b, c, center): # First just test a sphere. ellipsoid = Ellipsoid(a, a, a) assert np.all(ellipsoid.inertia_tensor >= 0) volume = ellipsoid.volume expected = [2 / 5 * volume * a ** 2] * 3 np.testing.assert_allclose(np.diag(ellipsoid.inertia_tensor), expected) ellipsoid.center = center expected = translate_inertia_tensor(center, np.diag(expected), volume) np.testing.assert_allclose(ellipsoid.inertia_tensor, expected)