InertiaComputationsMesh Namespace Reference

Functions

def ComputeMassCOMMesh (mesh, density)
 
def ShiftToCenterOfMassMesh (mesh, density)
 
def ClumpTOIFromMesh (mesh, density)
 
def RotateToPrincipalDirectionsMeshTOI (mesh, toi, v1, v2, v3)
 
def ComputeInertiaFromMesh (OPT, DATA)
 

Function Documentation

◆ ClumpTOIFromMesh()

def InertiaComputationsMesh.ClumpTOIFromMesh (   mesh,
  density 
)
44 def ClumpTOIFromMesh(mesh, density):
45  # This function computes the tensor of inertia of a body bound by a triangulated surface
46  O = np.zeros([3])
47  a = 0
48  b = 0
49  c = 0
50  a_p = 0
51  b_p = 0
52  c_p = 0
53  for j in range(len(mesh.normals)):
54  x_1 = O[0]
55  y_1 = O[1]
56  z_1 = O[2]
57  x_2 = mesh.v0[j][0]
58  y_2 = mesh.v0[j][1]
59  z_2 = mesh.v0[j][2]
60  x_3 = mesh.v1[j][0]
61  y_3 = mesh.v1[j][1]
62  z_3 = mesh.v1[j][2]
63  x_4 = mesh.v2[j][0]
64  y_4 = mesh.v2[j][1]
65  z_4 = mesh.v2[j][2]
66 
67  vd = np.array( [ [x_1, y_1, z_1, 1],
68  [x_2, y_2, z_2, 1],
69  [x_3, y_3, z_3, 1],
70  [x_4, y_4, z_4, 1] ])
71  V_j = - (1./6.) * np.linalg.det(vd)
72 
73  a += V_j * ( y_1**2 + y_1*y_2 + y_2**2 + y_1*y_3 + y_2*y_3 +
74  y_3**2 + y_1*y_4 + y_2*y_4 + y_3*y_4 + y_4**2 +
75  z_1**2 + z_1*z_2 + z_2**2 + z_1*z_3 + z_2*z_3 +
76  z_3**2 + z_1*z_4 + z_2*z_4 + z_3*z_4 + z_4**2 ) / 10.
77 
78  b += V_j * ( x_1**2 + x_1*x_2 + x_2**2 + x_1*x_3 + x_2*x_3 +
79  x_3**2 + x_1*x_4 + x_2*x_4 + x_3*x_4 + x_4**2 +
80  z_1**2 + z_1*z_2 + z_2**2 + z_1*z_3 + z_2*z_3 +
81  z_3**2 + z_1*z_4 + z_2*z_4 + z_3*z_4 + z_4**2 ) / 10.
82 
83  c += V_j * ( x_1**2 + x_1*x_2 + x_2**2 + x_1*x_3 + x_2*x_3 +
84  x_3**2 + x_1*x_4 + x_2*x_4 + x_3*x_4 + x_4**2 +
85  y_1**2 + y_1*y_2 + y_2**2 + y_1*y_3 + y_2*y_3 +
86  y_3**2 + y_1*y_4 + y_2*y_4 + y_3*y_4 + y_4**2 ) / 10.
87 
88  a_p += V_j * ( 2*y_1*z_1 + y_2*z_1 + y_3*z_1 + y_4*z_1 + y_1*z_2 +
89  2*y_2*z_2 + y_3*z_2 + y_4*z_2 + y_1*z_3 + y_2*z_3 +
90  2*y_3*z_3 + y_4*z_3 + y_1*z_4 + y_2*z_4 + y_3*z_4 +
91  2*y_4*z_4) / 20.
92 
93  b_p += V_j * ( 2*x_1*z_1 + x_2*z_1 + x_3*z_1 + x_4*z_1 + x_1*z_2 +
94  2*x_2*z_2 + x_3*z_2 + x_4*z_2 + x_1*z_3 + x_2*z_3 +
95  2*x_3*z_3 + x_4*z_3 + x_1*z_4 + x_2*z_4 + x_3*z_4 +
96  2*x_4*z_4) / 20.
97 
98  c_p += V_j * ( 2*x_1*y_1 + x_2*y_1 + x_3*y_1 + x_4*y_1 + x_1*y_2 +
99  2*x_2*y_2 + x_3*y_2 + x_4*y_2 + x_1*y_3 + x_2*y_3 +
100  2*x_3*y_3 + x_4*y_3 + x_1*y_4 + x_2*y_4 + x_3*y_4 +
101  2*x_4*y_4) / 20.
102 
103  #return density * np.array([[a, -b_p, -c_p],[ -b_p, b, -a_p],[ -c_p, -a_p, c]])
104  return density * np.array([[a, -c_p, -b_p],[ -c_p, b, -a_p],[ -b_p, -a_p, c]])
105 
106 
107 
def ClumpTOIFromMesh(mesh, density)
Definition: InertiaComputationsMesh.py:44

◆ ComputeInertiaFromMesh()

def InertiaComputationsMesh.ComputeInertiaFromMesh (   OPT,
  DATA 
)
127 def ComputeInertiaFromMesh(OPT, DATA):
128  # take array of pebbles
129  mesh = DATA['stlMesh']
130  density = DATA['density']
131  # Compute mass, shift to center of mass
132  mass, mesh = shiftToCenterOfMassMesh(mesh, density)
133  if OPT['verbose']: print("Total mass of stl: ", mass)
134 
135  # Compute tensor of inertia
136  toi = clump_toi_from_mesh(mesh, density)
137  if OPT['verbose']: print("Tensor of inertia of stl: ", toi)
138 
139  # Compute principal directions
140  v1, v2, v3 = compute_principal_directions(toi)
141  if OPT['verbose']: print("Principal directions: ", v1, v2, v3)
142 
143  # Rotate to principal directions
144  if OPT['rotateToPD']:
145  mesh, toi, v1, v2, v3 = rotate_to_principal_directions_mesh_toi(mesh, toi, v1, v2, v3)
146  if OPT['verbose']: print("Rotated principal directions: ", v1, v2, v3)
147  if OPT['verbose']: print("Rotated toi: ", toi)
148 
149  # Save all the data
150  DATA['stlMesh'] = mesh
151  DATA['mass'] = mass
152  DATA['pd'] = v1, v2, v3
153  DATA['toi'] = toi
154 
155  return OPT, DATA
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet print(const Packet &a)
Definition: GenericPacketMath.h:1166
def ComputeInertiaFromMesh(OPT, DATA)
Definition: InertiaComputationsMesh.py:127

References Eigen::internal.print().

◆ ComputeMassCOMMesh()

def InertiaComputationsMesh.ComputeMassCOMMesh (   mesh,
  density 
)
12 def ComputeMassCOMMesh(mesh, density):
13  # Computes center of mass of the body bound by the triangulated surface "mesh"
14  # Returns mass, coordinates of the center of mass
15  O = np.zeros(3)
16  com = 0
17  vol = 0
18  for j in range(len(mesh.normals)):
19  p1 = mesh.v0[j]
20  p2 = mesh.v1[j]
21  p3 = mesh.v2[j]
22  vd = np.vstack((np.hstack((O,1)),
23  np.hstack((p1,1)),
24  np.hstack((p2,1)),
25  np.hstack((p3,1))))
26  V_j = -(1./6.) * np.linalg.det(vd)
27  c_j = (O + p1 + p2 + p3)/4.
28  com += V_j * c_j
29  vol += V_j
30  com /= vol
31  return density*vol, com
32 
33 
def ComputeMassCOMMesh(mesh, density)
Definition: InertiaComputationsMesh.py:12

◆ RotateToPrincipalDirectionsMeshTOI()

def InertiaComputationsMesh.RotateToPrincipalDirectionsMeshTOI (   mesh,
  toi,
  v1,
  v2,
  v3 
)
108 def RotateToPrincipalDirectionsMeshTOI(mesh, toi, v1, v2, v3):
109  # This function rotates the centered mesh to match specified principal directions v1, v2, v3 with Cartesian axes
110  e1 = np.array([1,0,0])
111  e2 = np.array([0,1,0])
112  e3 = np.array([0,0,1])
113  Q = np.array([
114  [e1@v1, e2@v1, e3@v1],
115  [e1@v2, e2@v2, e3@v2],
116  [e1@v3, e2@v3, e3@v3]])
117 
118  for j in range(len(mesh.normals)):
119  mesh.v0[j] = Q @ mesh.v0[j]
120  mesh.v1[j] = Q @ mesh.v1[j]
121  mesh.v2[j] = Q @ mesh.v2[j]
122  mesh.normals[j] = Q @ mesh.normals[j]
123  toi = np.transpose(Q) @ toi @ Q
124  return mesh, toi, e1, e2, e3
125 
126 
def RotateToPrincipalDirectionsMeshTOI(mesh, toi, v1, v2, v3)
Definition: InertiaComputationsMesh.py:108

◆ ShiftToCenterOfMassMesh()

def InertiaComputationsMesh.ShiftToCenterOfMassMesh (   mesh,
  density 
)
34 def ShiftToCenterOfMassMesh(mesh, density):
35  # Returns the coordinates of "mesh" shifted such that the center of mass is at zero.
36  mass, com = computeMassComMesh(mesh, density)
37  for j in range(len(mesh.normals)):
38  mesh.v0[j] -= com
39  mesh.v1[j] -= com
40  mesh.v2[j] -= com
41  return mass, mesh
42 
43 
def ShiftToCenterOfMassMesh(mesh, density)
Definition: InertiaComputationsMesh.py:34