Skip to content

Commit d1a3a92

Browse files
authored
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
1 parent 5de4d9f commit d1a3a92

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

cadquery/occ_impl/shapes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,22 @@ def _center_of_mass(shape: "Shape") -> Vector:
631631

632632
return Vector(Properties.CentreOfMass())
633633

634+
@staticmethod
635+
def matrixOfInertia(obj: "Shape") -> List[List[float]]:
636+
"""
637+
Calculates the matrix of inertia of an object.
638+
:param obj: Compute the matrix of inertia of this object
639+
"""
640+
Properties = GProp_GProps()
641+
calc_function = shape_properties_LUT[shapetype(obj.wrapped)]
642+
643+
if calc_function:
644+
calc_function(obj.wrapped, Properties)
645+
moi = Properties.MatrixOfInertia()
646+
return [[moi.Value(i, j) for j in range(1, 4)] for i in range(1, 4)]
647+
648+
raise NotImplementedError
649+
634650
def Center(self) -> Vector:
635651
"""
636652
:returns: The point of the center of mass of this Shape

tests/test_cad_objects.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,27 @@ def testFaceWrapperMakePlane(self):
238238

239239
self.assertTupleAlmostEquals((0.0, 0.0, 1.0), mplane.normalAt().toTuple(), 3)
240240

241+
def testMatrixOfInertia(self):
242+
"""
243+
Tests the calculation of the matrix of inertia for a solid
244+
"""
245+
radius = 1.0
246+
height = 2.0
247+
cylinder = Solid.makeCylinder(radius=radius, height=height)
248+
moi = Shape.matrixOfInertia(cylinder)
249+
two_pi = 2 * math.pi
250+
true_moi = (
251+
two_pi * (radius ** 2 / 4 + height ** 2 / 12),
252+
two_pi * (radius ** 2 / 4 + height ** 2 / 12),
253+
two_pi * radius ** 2 / 2,
254+
)
255+
self.assertTupleAlmostEquals((moi[0][0], moi[1][1], moi[2][2]), true_moi, 3)
256+
257+
def testVertexMatrixOfInertiaNotImplemented(self):
258+
with self.assertRaises(NotImplementedError):
259+
vertex = Vertex.makeVertex(1, 1, 1)
260+
Shape.matrixOfInertia(vertex)
261+
241262
def testCenterOfBoundBox(self):
242263
pass
243264

0 commit comments

Comments
 (0)