python_unit/geometric_shapes.py
Go to the documentation of this file.
1 import unittest
2 from test_case import TestCase
3 import hppfcl
4 import numpy as np
5 
6 
8  def test_capsule(self):
9  capsule = hppfcl.Capsule(1.0, 2.0)
10  self.assertIsInstance(capsule, hppfcl.Capsule)
11  self.assertIsInstance(capsule, hppfcl.ShapeBase)
12  self.assertIsInstance(capsule, hppfcl.CollisionGeometry)
13  self.assertEqual(capsule.getNodeType(), hppfcl.NODE_TYPE.GEOM_CAPSULE)
14  self.assertEqual(capsule.radius, 1.0)
15  self.assertEqual(capsule.halfLength, 1.0)
16  capsule.radius = 3.0
17  capsule.halfLength = 4.0
18  self.assertEqual(capsule.radius, 3.0)
19  self.assertEqual(capsule.halfLength, 4.0)
20  com = capsule.computeCOM()
21  self.assertApprox(com, np.zeros(3))
22  V = capsule.computeVolume()
23  V_cylinder = capsule.radius * capsule.radius * np.pi * 2.0 * capsule.halfLength
24  V_sphere = 4.0 * np.pi / 3 * capsule.radius**3
25  V_ref = V_cylinder + V_sphere
26  self.assertApprox(V, V_ref)
27  I0 = capsule.computeMomentofInertia()
28  Iz_cylinder = V_cylinder * capsule.radius**2 / 2.0
29  Iz_sphere = 0.4 * V_sphere * capsule.radius * capsule.radius
30  Iz_ref = Iz_cylinder + Iz_sphere
31  Ix_cylinder = (
32  V_cylinder * (3 * capsule.radius**2 + 4 * capsule.halfLength**2) / 12.0
33  )
34  V_hemi = 0.5 * V_sphere # volume of hemisphere
35  I0x_hemi = 0.5 * Iz_sphere # inertia of hemisphere w.r.t. origin
36  com_hemi = 3.0 * capsule.radius / 8.0 # CoM of hemisphere w.r.t. origin
37  Icx_hemi = (
38  I0x_hemi - V_hemi * com_hemi * com_hemi
39  ) # inertia of hemisphere w.r.t. CoM
40  Ix_hemi = (
41  Icx_hemi + V_hemi * (capsule.halfLength + com_hemi) ** 2
42  ) # inertia of hemisphere w.r.t. tip of cylinder
43  Ix_ref = Ix_cylinder + 2 * Ix_hemi # total inertia of capsule
44  I0_ref = np.diag([Ix_ref, Ix_ref, Iz_ref])
45  self.assertApprox(I0, I0_ref)
46  Ic = capsule.computeMomentofInertiaRelatedToCOM()
47  self.assertApprox(Ic, I0_ref)
48 
49  def test_box1(self):
50  box = hppfcl.Box(np.array([1.0, 2.0, 3.0]))
51  self.assertIsInstance(box, hppfcl.Box)
52  self.assertIsInstance(box, hppfcl.ShapeBase)
53  self.assertIsInstance(box, hppfcl.CollisionGeometry)
54  self.assertEqual(box.getNodeType(), hppfcl.NODE_TYPE.GEOM_BOX)
55  self.assertTrue(np.array_equal(box.halfSide, np.array([0.5, 1.0, 1.5])))
56  box.halfSide = np.array([4.0, 5.0, 6.0])
57  self.assertTrue(np.array_equal(box.halfSide, np.array([4.0, 5.0, 6.0])))
58  com = box.computeCOM()
59  self.assertApprox(com, np.zeros(3))
60  V = box.computeVolume()
61  x = float(2 * box.halfSide[0])
62  y = float(2 * box.halfSide[1])
63  z = float(2 * box.halfSide[2])
64  V_ref = x * y * z
65  self.assertApprox(V, V_ref)
66  I0 = box.computeMomentofInertia()
67  Ix = V_ref * (y * y + z * z) / 12.0
68  Iy = V_ref * (x * x + z * z) / 12.0
69  Iz = V_ref * (y * y + x * x) / 12.0
70  I0_ref = np.diag([Ix, Iy, Iz])
71  self.assertApprox(I0, I0_ref)
72  Ic = box.computeMomentofInertiaRelatedToCOM()
73  self.assertApprox(Ic, I0_ref)
74 
75  def test_box2(self):
76  box = hppfcl.Box(1.0, 2.0, 3)
77  self.assertIsInstance(box, hppfcl.Box)
78  self.assertIsInstance(box, hppfcl.ShapeBase)
79  self.assertIsInstance(box, hppfcl.CollisionGeometry)
80  self.assertEqual(box.getNodeType(), hppfcl.NODE_TYPE.GEOM_BOX)
81  self.assertEqual(box.halfSide[0], 0.5)
82  self.assertEqual(box.halfSide[1], 1.0)
83  self.assertEqual(box.halfSide[2], 1.5)
84 
85  def test_sphere(self):
86  sphere = hppfcl.Sphere(1.0)
87  self.assertIsInstance(sphere, hppfcl.Sphere)
88  self.assertIsInstance(sphere, hppfcl.ShapeBase)
89  self.assertIsInstance(sphere, hppfcl.CollisionGeometry)
90  self.assertEqual(sphere.getNodeType(), hppfcl.NODE_TYPE.GEOM_SPHERE)
91  self.assertEqual(sphere.radius, 1.0)
92  sphere.radius = 2.0
93  self.assertEqual(sphere.radius, 2.0)
94  com = sphere.computeCOM()
95  self.assertApprox(com, np.zeros(3))
96  V = sphere.computeVolume()
97  V_ref = 4.0 * np.pi / 3 * sphere.radius**3
98  self.assertApprox(V, V_ref)
99  I0 = sphere.computeMomentofInertia()
100  I0_ref = 0.4 * V_ref * sphere.radius * sphere.radius * np.identity(3)
101  self.assertApprox(I0, I0_ref)
102  Ic = sphere.computeMomentofInertiaRelatedToCOM()
103  self.assertApprox(Ic, I0_ref)
104 
105  def test_cylinder(self):
106  cylinder = hppfcl.Cylinder(1.0, 2.0)
107  self.assertIsInstance(cylinder, hppfcl.Cylinder)
108  self.assertIsInstance(cylinder, hppfcl.ShapeBase)
109  self.assertIsInstance(cylinder, hppfcl.CollisionGeometry)
110  self.assertEqual(cylinder.getNodeType(), hppfcl.NODE_TYPE.GEOM_CYLINDER)
111  self.assertEqual(cylinder.radius, 1.0)
112  self.assertEqual(cylinder.halfLength, 1.0)
113  cylinder.radius = 3.0
114  cylinder.halfLength = 4.0
115  self.assertEqual(cylinder.radius, 3.0)
116  self.assertEqual(cylinder.halfLength, 4.0)
117  com = cylinder.computeCOM()
118  self.assertApprox(com, np.zeros(3))
119  V = cylinder.computeVolume()
120  V_ref = cylinder.radius * cylinder.radius * np.pi * 2.0 * cylinder.halfLength
121  self.assertApprox(V, V_ref)
122  I0 = cylinder.computeMomentofInertia()
123  Ix_ref = (
124  V_ref * (3 * cylinder.radius**2 + 4 * cylinder.halfLength**2) / 12.0
125  )
126  Iz_ref = V_ref * cylinder.radius**2 / 2.0
127  I0_ref = np.diag([Ix_ref, Ix_ref, Iz_ref])
128  self.assertApprox(I0, I0_ref)
129  Ic = cylinder.computeMomentofInertiaRelatedToCOM()
130  self.assertApprox(Ic, I0_ref)
131 
132  def test_cone(self):
133  cone = hppfcl.Cone(1.0, 2.0)
134  self.assertIsInstance(cone, hppfcl.Cone)
135  self.assertIsInstance(cone, hppfcl.ShapeBase)
136  self.assertIsInstance(cone, hppfcl.CollisionGeometry)
137  self.assertEqual(cone.getNodeType(), hppfcl.NODE_TYPE.GEOM_CONE)
138  self.assertEqual(cone.radius, 1.0)
139  self.assertEqual(cone.halfLength, 1.0)
140  cone.radius = 3.0
141  cone.halfLength = 4.0
142  self.assertEqual(cone.radius, 3.0)
143  self.assertEqual(cone.halfLength, 4.0)
144  com = cone.computeCOM()
145  self.assertApprox(com, np.array([0.0, 0.0, -0.5 * cone.halfLength]))
146  V = cone.computeVolume()
147  V_ref = np.pi * cone.radius**2 * 2.0 * cone.halfLength / 3.0
148  self.assertApprox(V, V_ref)
149  I0 = cone.computeMomentofInertia()
150  Ix_ref = V_ref * (3.0 / 20.0 * cone.radius**2 + 0.4 * cone.halfLength**2)
151  Iz_ref = 0.3 * V_ref * cone.radius**2
152  I0_ref = np.diag([Ix_ref, Ix_ref, Iz_ref])
153  self.assertApprox(I0, I0_ref)
154  Ic = cone.computeMomentofInertiaRelatedToCOM()
155  Icx_ref = V_ref * 3.0 / 20.0 * (cone.radius**2 + cone.halfLength**2)
156  Ic_ref = np.diag([Icx_ref, Icx_ref, Iz_ref])
157  self.assertApprox(Ic, Ic_ref)
158 
159  def test_convex(self):
160  verts = hppfcl.StdVec_Vec3f()
161  faces = hppfcl.StdVec_Triangle()
162  verts.extend(
163  [
164  np.array([0, 0, 0]),
165  np.array([0, 1, 0]),
166  np.array([1, 0, 0]),
167  ]
168  )
169  faces.append(hppfcl.Triangle(0, 1, 2))
170  convex = hppfcl.Convex(verts, faces)
171 
172  verts.append(np.array([0, 0, 1]))
173  try:
174  convexHull = hppfcl.Convex.convexHull(verts, False, None)
175  qhullAvailable = True
176  except Exception as e:
177  self.assertEqual(
178  str(e), "Library built without qhull. Cannot build object of this type."
179  )
180  qhullAvailable = False
181 
182  if qhullAvailable:
183  convexHull = hppfcl.Convex.convexHull(verts, False, "")
184  convexHull = hppfcl.Convex.convexHull(verts, True, "")
185 
186  try:
187  convexHull = hppfcl.Convex.convexHull(verts[:3], False, None)
188  except Exception as e:
189  self.assertIn(
190  str(e), "You shouldn't use this function with less than 4 points."
191  )
192 
193 
194 if __name__ == "__main__":
195  unittest.main()
const char * str()
def assertApprox(self, a, b, epsilon=1e-6)
Definition: test_case.py:6


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:01