| 274 | |
| 275 | |
| 276 | def createCapsule(length, radius, radial_resolution=30, cap_resolution=10): |
| 277 | nbv = np.array([max(radial_resolution, 4), max(cap_resolution, 4)]) |
| 278 | h = length |
| 279 | r = radius |
| 280 | position = 0 |
| 281 | vertices = np.zeros((nbv[0] * (2 * nbv[1]) + 2, 3)) |
| 282 | for j in range(nbv[0]): |
| 283 | phi = (2 * np.pi * j) / nbv[0] |
| 284 | for i in range(nbv[1]): |
| 285 | theta = (np.pi / 2 * i) / nbv[1] |
| 286 | vertices[position + i, :] = np.array( |
| 287 | [ |
| 288 | np.cos(theta) * np.cos(phi) * r, |
| 289 | np.cos(theta) * np.sin(phi) * r, |
| 290 | -h / 2 - np.sin(theta) * r, |
| 291 | ] |
| 292 | ) |
| 293 | vertices[position + i + nbv[1], :] = np.array( |
| 294 | [ |
| 295 | np.cos(theta) * np.cos(phi) * r, |
| 296 | np.cos(theta) * np.sin(phi) * r, |
| 297 | h / 2 + np.sin(theta) * r, |
| 298 | ] |
| 299 | ) |
| 300 | position += nbv[1] * 2 |
| 301 | vertices[-2, :] = np.array([0, 0, -h / 2 - r]) |
| 302 | vertices[-1, :] = np.array([0, 0, h / 2 + r]) |
| 303 | indexes = np.zeros((nbv[0] * (4 * (nbv[1] - 1) + 4), 3)) |
| 304 | index = 0 |
| 305 | stride = nbv[1] * 2 |
| 306 | last = nbv[0] * (2 * nbv[1]) + 1 |
| 307 | for j in range(nbv[0]): |
| 308 | j_next = (j + 1) % nbv[0] |
| 309 | indexes[index + 0] = np.array( |
| 310 | [j_next * stride + nbv[1], j_next * stride, j * stride] |
| 311 | ) |
| 312 | indexes[index + 1] = np.array( |
| 313 | [j * stride + nbv[1], j_next * stride + nbv[1], j * stride] |
| 314 | ) |
| 315 | indexes[index + 2] = np.array( |
| 316 | [j * stride + nbv[1] - 1, j_next * stride + nbv[1] - 1, last - 1] |
| 317 | ) |
| 318 | indexes[index + 3] = np.array( |
| 319 | [j_next * stride + 2 * nbv[1] - 1, j * stride + 2 * nbv[1] - 1, last] |
| 320 | ) |
| 321 | for i in range(nbv[1] - 1): |
| 322 | indexes[index + 4 + i * 4 + 0] = np.array( |
| 323 | [j_next * stride + i, j_next * stride + i + 1, j * stride + i] |
| 324 | ) |
| 325 | indexes[index + 4 + i * 4 + 1] = np.array( |
| 326 | [j_next * stride + i + 1, j * stride + i + 1, j * stride + i] |
| 327 | ) |
| 328 | indexes[index + 4 + i * 4 + 2] = np.array( |
| 329 | [ |
| 330 | j_next * stride + nbv[1] + i + 1, |
| 331 | j_next * stride + nbv[1] + i, |
| 332 | j * stride + nbv[1] + i, |
| 333 | ] |