Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matrix of inertia #1460

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,22 @@ def _center_of_mass(shape: "Shape") -> Vector:

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
kmarchais marked this conversation as resolved.
Show resolved Hide resolved

def Center(self) -> Vector:
"""
:returns: The point of the center of mass of this Shape
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cad_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ def testFaceWrapperMakePlane(self):

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

Expand Down