InertiaComputationsMixed Namespace Reference

Functions

def ComputeMassCOMMesh (mesh, density)
 
def ShiftToCenterOfMassMeshPebbles (mesh, pebbles, density)
 
def ClumpTOIFromMesh (mesh, density)
 
def RotateToPrincipalDirectionsMeshTOIPebbles (mesh, toi, pebbles, v1, v2, v3)
 
def RotateToPD (mesh, v1, v2, v3)
 
def ComputeInertiaMixed (OPT, DATA)
 

Function Documentation

◆ ClumpTOIFromMesh()

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

Referenced by ComputeInertiaMixed().

◆ ComputeInertiaMixed()

def InertiaComputationsMixed.ComputeInertiaMixed (   OPT,
  DATA 
)
155 def ComputeInertiaMixed(OPT, DATA):
156  # take arrays of mesh and pebbles
157  mesh = DATA['stlMesh']
158  density = DATA['density']
159  pebbles = DATA['pebbles']
160  # Compute mass, shift to center of mass
161  mass, mesh, pebbles = ShiftToCenterOfMassMeshPebbles(mesh, pebbles, density)
162  if OPT['verbose']: print("Total mass of stl: ", mass)
163 
164  # Compute tensor of inertia
165  toi = ClumpTOIFromMesh(mesh, density)
166  if OPT['verbose']: print("Tensor of inertia of stl: ", toi)
167 
168  # Compute principal directions
169  v1, v2, v3 = ComputePrincipalDirections(toi)
170  if OPT['verbose']: print("Principal directions: ", v1, v2, v3)
171 
172  # Rotate to principal directions
173  if OPT['rotateToPD']:
174  mesh, toi, pebbles, v1, v2, v3 = RotateToPrincipalDirectionsMeshTOIPebbles(mesh, toi, pebbles, v1, v2, v3)
175  if OPT['verbose']: print("Rotated principal directions: ", v1, v2, v3)
176  if OPT['verbose']: print("Rotated toi: ", toi)
177 
178  #mesh = rotate_to_pd(mesh, np.array([1,0,0]), np.array([0,0,-1]), np.array([0,1,0]))
179 
180 
181  # Save all the data
182  DATA['stlMesh'] = mesh
183  DATA['pebbles'] = pebbles
184  DATA['mass'] = mass
185  DATA['pd'] = v1, v2, v3
186  DATA['toi'] = toi
187 
188  return OPT, DATA
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet print(const Packet &a)
Definition: GenericPacketMath.h:1166
def ShiftToCenterOfMassMeshPebbles(mesh, pebbles, density)
Definition: InertiaComputationsMixed.py:34
def RotateToPrincipalDirectionsMeshTOIPebbles(mesh, toi, pebbles, v1, v2, v3)
Definition: InertiaComputationsMixed.py:110
def ComputeInertiaMixed(OPT, DATA)
Definition: InertiaComputationsMixed.py:155
def ComputePrincipalDirections(toi)
Definition: InertiaComputationsPebbles.py:49

References ClumpTOIFromMesh(), InertiaComputationsPebbles.ComputePrincipalDirections(), Eigen::internal.print(), RotateToPrincipalDirectionsMeshTOIPebbles(), and ShiftToCenterOfMassMeshPebbles().

Referenced by MClump.main().

◆ ComputeMassCOMMesh()

def InertiaComputationsMixed.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: InertiaComputationsMixed.py:12

Referenced by ShiftToCenterOfMassMeshPebbles().

◆ RotateToPD()

def InertiaComputationsMixed.RotateToPD (   mesh,
  v1,
  v2,
  v3 
)
136 def RotateToPD(mesh, v1, v2, v3):
137  # This function rotates the centered mesh to match specified principal directions v1, v2, v3 with Cartesian axes
138  e1 = np.array([1,0,0])
139  e2 = np.array([0,1,0])
140  e3 = np.array([0,0,1])
141  Q = np.array([
142  [e1@v1, e2@v1, e3@v1],
143  [e1@v2, e2@v2, e3@v2],
144  [e1@v3, e2@v3, e3@v3]])
145 
146  for j in range(len(mesh.normals)):
147  mesh.v0[j] = Q @ mesh.v0[j]
148  mesh.v1[j] = Q @ mesh.v1[j]
149  mesh.v2[j] = Q @ mesh.v2[j]
150  mesh.normals[j] = Q @ mesh.normals[j]
151 
152  return mesh
153 
154 
def RotateToPD(mesh, v1, v2, v3)
Definition: InertiaComputationsMixed.py:136

◆ RotateToPrincipalDirectionsMeshTOIPebbles()

def InertiaComputationsMixed.RotateToPrincipalDirectionsMeshTOIPebbles (   mesh,
  toi,
  pebbles,
  v1,
  v2,
  v3 
)
110 def RotateToPrincipalDirectionsMeshTOIPebbles(mesh, toi, pebbles, v1, v2, v3):
111  # This function rotates the centered mesh to match specified principal directions v1, v2, v3 with Cartesian axes
112  e1 = np.array([1,0,0])
113  e2 = np.array([0,1,0])
114  e3 = np.array([0,0,1])
115  Q = np.array([
116  [e1@v1, e2@v1, e3@v1],
117  [e1@v2, e2@v2, e3@v2],
118  [e1@v3, e2@v3, e3@v3]])
119 
120  for j in range(len(mesh.normals)):
121  mesh.v0[j] = Q @ mesh.v0[j]
122  mesh.v1[j] = Q @ mesh.v1[j]
123  mesh.v2[j] = Q @ mesh.v2[j]
124  mesh.normals[j] = Q @ mesh.normals[j]
125  toi = np.transpose(Q) @ toi @ Q
126 
127  r_pebbles = np.zeros(np.shape(pebbles))
128  for j in range(len(pebbles)):
129  r_pebbles[j][:3] = Q @ pebbles[j][:3]
130  r_pebbles[j][3] = pebbles[j][3]
131 
132  return mesh, toi, r_pebbles, e1, e2, e3
133 
134 
135 # Extra rotation of the mesh

Referenced by ComputeInertiaMixed().

◆ ShiftToCenterOfMassMeshPebbles()

def InertiaComputationsMixed.ShiftToCenterOfMassMeshPebbles (   mesh,
  pebbles,
  density 
)
34 def ShiftToCenterOfMassMeshPebbles(mesh, pebbles, density):
35  # Returns the coordinates of mesh and pebbles 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 
42  for j in range(len(pebbles)):
43  pebbles[j][:3] -= com
44 
45  return mass, mesh, pebbles
46 
47 

References ComputeMassCOMMesh().

Referenced by ComputeInertiaMixed().