Calculate inertia tensors

The inertia tensor is defined as an integral of the following tensor over the body region

vars = {x, y, z};
r2 = IdentityMatrix[3] Tr[#] - # &@Outer[Times, vars, vars];
r2 // MatrixForm

enter image description here

It is very simple to do with integration over a region

Integrate[r2, vars ∈ region]

It can be wrapped in the following function

inertiaTensor[reg_, assum_: {}] := 
  Module[{x, y, z, d = RegionEmbeddingDimension[reg], r2, vars},
   vars = {x, y, z};
   r2 = IdentityMatrix[3] Tr[#] - # &@Outer[Times, vars, vars];
   If[d == 2, r2 = r2 /. z -> 0; vars = {x, y}];
   If[d == 1, r2 = r2 /. {x -> 0, y -> 0}; vars = {z}];
   Integrate[r2, vars ∈ reg, Assumptions -> assum]/
     Integrate[1, vars ∈ reg, Assumptions -> assum] // 
    Simplify];

I assume that the body have a unit mass. I also assume that 2D bodies lie in xy plane and 1D bodies lie on the axes z.

Now we can prepare the following demonstration which corresponds to known list of moments of inertia

gr3d = Graphics3D[{PointSize[0.03], Thickness[0.03], 
     FaceForm[Opacity[0.5]], Blue, #, Gray, Thickness[0.01], 
     Line[{-#, #} & /@ IdentityMatrix[3]], Black, 
     Text @@@ {{x, {1.1, 0, 0}}, {y, {0, 1.1, 0}}, {z, {0, 0, 1.1}}}},
     ImageSize -> 150, PlotRange -> 1, Boxed -> False, 
    SphericalRegion -> False, ViewAngle -> Pi/10] &;
gr2d = gr3d@{FaceForm[Opacity[1]], Texture[#], EdgeForm[None], 
         Polygon[{{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0}}, 
          VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 
             1}}]} &@Rasterize[#, Background -> None] &@
    Graphics[{EdgeForm[Blue], FaceForm[{Opacity[0.5], Blue}], 
      Blue, #}, PlotRange -> 1.01, ImageSize -> 150, 
     Background -> Transparent] &;

Manipulate[
   Row@{type[[1]], 
     MatrixForm[m inertiaTensor @@ type[[2 ;;]]]}, {type, 
    Thread[#[[All, 2 ;;]] -> #[[All, 1]]]}, 
   Initialization :> {type = #[[1, 2 ;;]]}] &@{{"Point", 
   gr3d@Point[{0, 0, 1}], Point[{0, 0, r}], r > 0},
  {"Rod", gr3d@Line[{{0, 0, -1/2}, {0, 0, 1/2}}], 
   Interval[{-a/2, a/2}], a > 0},
  {"Circle", gr2d@Circle[], Circle[{0, 0}, r], r > 0},
  {"Disk", gr2d@Disk[], Disk[{0, 0}, r], r > 0},
  {"Cylinder", gr3d@Cylinder[{{0, 0, -1/2}, {0, 0, 1/2}}, 1/2], 
   Cylinder[{{0, 0, -h/2}, {0, 0, h/2}}, r], {r > 0, h > 0}},
  {"Tetrahedron", gr3d@Tetrahedron[#], Tetrahedron[s #], s > 0} &@
   PolyhedronData["Tetrahedron", "VertexCoordinates"],
  {"Sphere", gr3d@Sphere[], Sphere[{0, 0, 0}, r], r > 0},
  {"Ball", gr3d@Ball[], Ball[{0, 0, 0}, r], r > 0},
  {"Cone", gr3d@Cone[{{0, 0, 2/3}, {0, 0, 0}}, 1/2], 
   Cone[{{0, 0, h}, {0, 0, 0}}, r], {r > 0, h > 0}},
  {"Ellipsoid", gr3d@Ellipsoid[{0, 0, 0}, {0.7, 0.5, 0.3}], 
   Ellipsoid[{0, 0, 0}, {a, b, c}], {a > 0, b > 0, c > 0}},
  {"Rectangle", gr2d@Rectangle[-{1, 1}/2, {1, 1}/2], 
   Rectangle[-{a, a}/2, {a, a}/2], a > 0},
  {"Cuboid", gr3d@Cuboid[-{0.4, 0.3, 0.2}, {0.4, 0.3, 0.2}], 
   Cuboid[-{a, b, c}/2, {a, b, c}/2], {a > 0, b > 0, c > 0}}}

enter image description here


In Mathematica 10.4, MomentOfInertia is now built-in. So we can compute inertia tensor for named, arbitrary and formula regions. Some examples:

MomentOfInertia[Ball[]]

(* {{(8 Pi)/15, 0, 0}, {0, (8 Pi)/15, 0}, {0, 0, (8 Pi)/15}} *)

reg = DelaunayMesh[RandomReal[1, {20, 3}]]

Mathematica graphics

MomentOfInertia[reg]
(* {{0.0227787, 0.085264, 0.0937136}, {0.085264, 0.0226137, 
  0.0801547}, {0.0937136, 0.0801547, 0.0183785}} *)