|
18 | 18 | }, |
19 | 19 | "outputs": [], |
20 | 20 | "source": [ |
21 | | - "import time\n", |
22 | | - "\n", |
23 | 21 | "import numpy as np\n", |
24 | 22 | "from pydrake.all import (\n", |
25 | 23 | " AddMultibodyPlantSceneGraph,\n", |
|
31 | 29 | ")\n", |
32 | 30 | "\n", |
33 | 31 | "from manipulation import running_as_notebook\n", |
34 | | - "from manipulation.scenarios import AddShape" |
| 32 | + "from manipulation.scenarios import AddShape\n" |
35 | 33 | ] |
36 | 34 | }, |
37 | 35 | { |
|
41 | 39 | "outputs": [], |
42 | 40 | "source": [ |
43 | 41 | "# Start the visualizer.\n", |
44 | | - "meshcat = StartMeshcat()" |
| 42 | + "meshcat = StartMeshcat()\n" |
45 | 43 | ] |
46 | 44 | }, |
47 | 45 | { |
|
69 | 67 | "\n", |
70 | 68 | " meshcat.Delete()\n", |
71 | 69 | " meshcat.Set2dRenderMode(xmin=0, xmax=1, ymin=0, ymax=1)\n", |
| 70 | + " meshcat.StartRecording(set_visualizations_while_recording=False)\n", |
72 | 71 | "\n", |
73 | 72 | " start = np.empty((N, 3))\n", |
74 | 73 | " end = np.empty((N, 3))\n", |
75 | | - " id = 0\n", |
| 74 | + " last_plotted_index = 0\n", |
| 75 | + " plot_count = 1\n", |
76 | 76 | " for n in range(1, N):\n", |
77 | 77 | " q_sample = rng.random((1, 2))[0]\n", |
78 | 78 | " distance_sq = np.sum((Q[:n] - q_sample) ** 2, axis=1)\n", |
79 | 79 | " closest = np.argmin(distance_sq)\n", |
80 | 80 | " distance = np.sqrt(distance_sq[closest])\n", |
81 | 81 | " if distance > 0.1:\n", |
82 | 82 | " q_sample = Q[closest] + (0.1 / distance) * (q_sample - Q[closest])\n", |
83 | | - " start[n] = [Q[closest, 0], 0, Q[closest, 1]]\n", |
84 | | - " end[n] = [q_sample[0], 0, q_sample[1]]\n", |
| 83 | + " start[n - 1] = [Q[closest, 0], 0, Q[closest, 1]]\n", |
| 84 | + " end[n - 1] = [q_sample[0], 0, q_sample[1]]\n", |
85 | 85 | " if (n < 1000 and n % 100 == 1) or n % 1000 == 1:\n", |
86 | | - " meshcat.SetLineSegments(\"rrt\", start[: n + 1].T, end[: n + 1].T)\n", |
87 | | - " time.sleep(0.1) # sleep to slow things down.\n", |
| 86 | + " meshcat.SetLineSegments(\n", |
| 87 | + " f\"rrt/{last_plotted_index}\",\n", |
| 88 | + " start[last_plotted_index:n].T,\n", |
| 89 | + " end[last_plotted_index:n].T,\n", |
| 90 | + " )\n", |
| 91 | + " meshcat.SetProperty(\n", |
| 92 | + " f\"rrt/{last_plotted_index}\", \"visible\", False, 0\n", |
| 93 | + " )\n", |
| 94 | + " meshcat.SetProperty(\n", |
| 95 | + " f\"rrt/{last_plotted_index}\", \"visible\", True, 0.25 * plot_count\n", |
| 96 | + " )\n", |
| 97 | + " plot_count = plot_count + 1\n", |
| 98 | + " last_plotted_index = n\n", |
88 | 99 | " Q[n] = q_sample\n", |
89 | 100 | "\n", |
| 101 | + " meshcat.PublishRecording()\n", |
| 102 | + "\n", |
90 | 103 | "\n", |
91 | | - "basic_rrt()" |
| 104 | + "basic_rrt()\n" |
92 | 105 | ] |
93 | 106 | }, |
94 | 107 | { |
|
173 | 186 | " meshcat.Set2dRenderMode(xmin=0, xmax=1, ymin=0, ymax=1)\n", |
174 | 187 | "\n", |
175 | 188 | " visualizer = MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)\n", |
| 189 | + " meshcat.StartRecording(set_visualizations_while_recording=False)\n", |
176 | 190 | "\n", |
177 | 191 | " diagram = builder.Build()\n", |
178 | 192 | " context = diagram.CreateDefaultContext()\n", |
|
193 | 207 | "\n", |
194 | 208 | " max_length = thickness / 4\n", |
195 | 209 | " n = 1\n", |
| 210 | + " last_plotted_index = 0\n", |
| 211 | + " plot_count = 1\n", |
196 | 212 | " while n < N:\n", |
197 | 213 | " q_sample = rng.random((1, 2))[0]\n", |
198 | 214 | " distance_sq = np.sum((Q[:n] - q_sample) ** 2, axis=1)\n", |
|
207 | 223 | " ):\n", |
208 | 224 | " # Then the sample point is in collision...\n", |
209 | 225 | " continue\n", |
210 | | - " start[n] = [Q[closest, 0], 0, Q[closest, 1]]\n", |
211 | | - " end[n] = [q_sample[0], 0, q_sample[1]]\n", |
| 226 | + " start[n - 1] = [Q[closest, 0], 0, Q[closest, 1]]\n", |
| 227 | + " end[n - 1] = [q_sample[0], 0, q_sample[1]]\n", |
212 | 228 | " if (n < 1000 and n % 100 == 1) or n % 1000 == 1:\n", |
213 | | - " meshcat.SetLineSegments(\"rrt\", start[: n + 1].T, end[: n + 1].T)\n", |
214 | | - " time.sleep(0.1) # sleep to slow things down.\n", |
215 | | - "\n", |
| 229 | + " meshcat.SetLineSegments(\n", |
| 230 | + " f\"rrt/{last_plotted_index}\",\n", |
| 231 | + " start[last_plotted_index:n].T,\n", |
| 232 | + " end[last_plotted_index:n].T,\n", |
| 233 | + " )\n", |
| 234 | + " meshcat.SetProperty(\n", |
| 235 | + " f\"rrt/{last_plotted_index}\", \"visible\", False, 0\n", |
| 236 | + " )\n", |
| 237 | + " meshcat.SetProperty(\n", |
| 238 | + " f\"rrt/{last_plotted_index}\", \"visible\", True, 0.25 * plot_count\n", |
| 239 | + " )\n", |
| 240 | + " plot_count = plot_count + 1\n", |
| 241 | + " last_plotted_index = n\n", |
216 | 242 | " Q[n] = q_sample\n", |
217 | 243 | " n += 1\n", |
| 244 | + " meshcat.PublishRecording()\n", |
218 | 245 | "\n", |
219 | 246 | "\n", |
220 | | - "rrt_bugtrap()" |
| 247 | + "rrt_bugtrap()\n" |
221 | 248 | ] |
222 | 249 | }, |
223 | 250 | { |
|
249 | 276 | "name": "python", |
250 | 277 | "nbconvert_exporter": "python", |
251 | 278 | "pygments_lexer": "ipython3", |
252 | | - "version": "3.8.10" |
| 279 | + "version": "3.11.2" |
253 | 280 | }, |
254 | 281 | "vscode": { |
255 | 282 | "interpreter": { |
|
0 commit comments