reachable-workspace.py
Go to the documentation of this file.
1 import sys
2 import time
3 from pathlib import Path
4 
5 import meshcat.geometry as g
6 import numpy as np
7 import pinocchio as pin
8 from pinocchio.visualize import MeshcatVisualizer
9 
10 # Load the URDF model.
11 pinocchio_model_dir = Path(__file__).parent.parent / "models"
12 
13 model_path = pinocchio_model_dir / "example-robot-data/robots"
14 mesh_dir = pinocchio_model_dir
15 
16 urdf_filename = "panda.urdf"
17 urdf_model_path = model_path / "panda_description/urdf" / urdf_filename
18 
19 robot, collision_model, visual_model = pin.buildModelsFromUrdf(
20  urdf_model_path, mesh_dir
21 )
22 data = robot.createData()
23 
24 # Start a new MeshCat server and client.
25 viz = MeshcatVisualizer(robot, collision_model, visual_model)
26 try:
27  viz.initViewer(open=True)
28 except ImportError as err:
29  print(
30  "Error while initializing the viewer. "
31  "It seems you should install Python meshcat"
32  )
33  print(err)
34  sys.exit(0)
35 
36 # # Load the robot in the viewer.
37 viz.loadViewerModel()
38 # Display a robot configuration.
39 q0 = (robot.upperPositionLimit.T + robot.lowerPositionLimit.T) / 2
40 horizon = 0.2 # seconds
41 frame = robot.getFrameId(
42  robot.frames[-1].name
43 ) # for example the last frame of the robot
44 n_samples = 5
45 facet_dims = 2
46 
47 # To have convex hull computation or just the points of the reachable workspace and then
48 # compute it with cgal.
49 convex = True
50 
51 if convex:
52  verts, faces = pin.reachableWorkspaceHull(
53  robot, q0, horizon, frame, n_samples, facet_dims
54  )
55  verts = verts.T
56 
57 else:
58  try:
59  from CGAL.CGAL_Alpha_wrap_3 import * # noqa: F403
60  from CGAL.CGAL_Kernel import * # noqa: F403
61  from CGAL.CGAL_Mesh_3 import * # noqa: F403
62  from CGAL.CGAL_Polyhedron_3 import Polyhedron_3
63  except ModuleNotFoundError:
64  print("To compute non convex Polytope CGAL library needs to be installed.")
65  sys.exit(0)
66 
68  return v.x(), v.y(), v.z()
69 
71  p1 = he
72  p2 = p1.next()
73  p3 = p2.next()
74  return [
75  vertex_to_tuple(p1.vertex().point()),
76  vertex_to_tuple(p2.vertex().point()),
77  vertex_to_tuple(p3.vertex().point()),
78  ]
79 
80  def alpha_shape_with_cgal(coords, alpha=None):
81  """
82  Compute the alpha shape of a set of points. (Code thanks to A. Skuric)
83  Retrieved from http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/
84 
85  :param coords : Coordinates of points
86  :param alpha: List of alpha values to influence the gooeyness of the border.
87  Smaller numbers don't fall inward as much as larger numbers.
88  Too large, and you lose everything!
89  :return: Shapely.MultiPolygons which is the hull of the input set of points
90  """
91  if alpha is None:
92  bbox_diag = np.linalg.norm(np.max(coords, 0) - np.min(coords, 0))
93  alpha_value = bbox_diag / 5
94  else:
95  alpha_value = np.mean(alpha)
96  # Convert to CGAL point
97  points = [Point_3(pt[0], pt[1], pt[2]) for pt in coords] # noqa: F405
98  # Compute alpha shape
99  Q = Polyhedron_3()
100  _a = alpha_wrap_3(points, alpha_value, 0.01, Q) # noqa: F405
101 
102  alpha_shape_vertices = np.array(
103  [vertex_to_tuple(vertex.point()) for vertex in Q.vertices()]
104  )
105  alpha_shape_faces = np.array(
106  [np.array(halfedge_to_triangle(face.halfedge())) for face in Q.facets()]
107  )
108 
109  return alpha_shape_vertices, alpha_shape_faces
110 
111  verts = pin.reachableWorkspace(robot, q0, horizon, frame, n_samples, facet_dims)
112  verts = verts.T
113 
114  alpha = 0.2
115  verts, faces = alpha_shape_with_cgal(verts, alpha)
116  verts = faces.reshape(-1, 3)
117  faces = np.arange(len(verts)).reshape(-1, 3)
118 
119 print("------------------- Display Vertex")
120 
121 
122 # meshcat triangulated mesh
123 poly = g.TriangularMeshGeometry(vertices=verts, faces=faces)
124 viz.viewer["poly"].set_object(
125  poly, g.MeshBasicMaterial(color=0x000000, wireframe=True, linewidth=12, opacity=0.2)
126 )
127 
128 while True:
129  viz.display(q0)
130  viz.viewer["poly"].set_object(
131  poly,
132  g.MeshBasicMaterial(color=0x000000, wireframe=True, linewidth=2, opacity=0.2),
133  )
134 
135  time.sleep(1e-2)
CGAL_Kernel
CGAL_Mesh_3
reachable-workspace.halfedge_to_triangle
def halfedge_to_triangle(he)
Definition: reachable-workspace.py:70
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
reachable-workspace.alpha_shape_with_cgal
def alpha_shape_with_cgal(coords, alpha=None)
Definition: reachable-workspace.py:80
CGAL_Alpha_wrap_3
reachable-workspace.vertex_to_tuple
def vertex_to_tuple(v)
Definition: reachable-workspace.py:67
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:576


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:32