Add matrix of inertia (#1460)

* add matrix of inertia + test

* ran black and revert changes made by isort

* add test to cover NotImplementedError for Vertex
This commit is contained in:
Kevin Marchais
2024-01-02 14:16:56 +01:00
committed by GitHub
parent 5de4d9f1c3
commit d1a3a921e9
2 changed files with 37 additions and 0 deletions

View File

@ -631,6 +631,22 @@ class Shape(object):
return Vector(Properties.CentreOfMass())
@staticmethod
def matrixOfInertia(obj: "Shape") -> List[List[float]]:
"""
Calculates the matrix of inertia of an object.
:param obj: Compute the matrix of inertia of this object
"""
Properties = GProp_GProps()
calc_function = shape_properties_LUT[shapetype(obj.wrapped)]
if calc_function:
calc_function(obj.wrapped, Properties)
moi = Properties.MatrixOfInertia()
return [[moi.Value(i, j) for j in range(1, 4)] for i in range(1, 4)]
raise NotImplementedError
def Center(self) -> Vector:
"""
:returns: The point of the center of mass of this Shape

View File

@ -238,6 +238,27 @@ class TestCadObjects(BaseTest):
self.assertTupleAlmostEquals((0.0, 0.0, 1.0), mplane.normalAt().toTuple(), 3)
def testMatrixOfInertia(self):
"""
Tests the calculation of the matrix of inertia for a solid
"""
radius = 1.0
height = 2.0
cylinder = Solid.makeCylinder(radius=radius, height=height)
moi = Shape.matrixOfInertia(cylinder)
two_pi = 2 * math.pi
true_moi = (
two_pi * (radius ** 2 / 4 + height ** 2 / 12),
two_pi * (radius ** 2 / 4 + height ** 2 / 12),
two_pi * radius ** 2 / 2,
)
self.assertTupleAlmostEquals((moi[0][0], moi[1][1], moi[2][2]), true_moi, 3)
def testVertexMatrixOfInertiaNotImplemented(self):
with self.assertRaises(NotImplementedError):
vertex = Vertex.makeVertex(1, 1, 1)
Shape.matrixOfInertia(vertex)
def testCenterOfBoundBox(self):
pass