matryoshka
matryoshka(V, F, VB=None, FB=None, optimize='all', R=None, c=None, cut_point=None, cut_normal=None, a_plus=None, a_minus=None, n_samples=200, n_particles=30, max_iter=40, scale_tol=0.001, warm_start=False, verbose=False, seed=None)
Generalized Matryoshka: find a similarity transform of B that nests
inside A such that A can be cut by a plane and pulled apart along
a+/a- without colliding with the inner copy. Implements the algorithm
of Jacobson (SGP 2017) on the CPU using particle swarm optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
(n,3) numpy double array
|
Vertex positions of the outer mesh A. |
required |
F
|
(m,3) numpy int array
|
Triangle indices of A. |
required |
VB
|
optional inner mesh B. If None, self-nesting is performed (B = A).
|
|
None
|
FB
|
optional inner mesh B. If None, self-nesting is performed (B = A).
|
|
None
|
optimize
|
str, optional (default 'all')
|
Which variables to optimize: * 'all' — scale, rotation, centroid, cut plane, removal directions * 'rigid' — scale, rotation, centroid (cut plane and removal directions fixed) * 'scale_only' — only the scale (everything else must be provided) |
'all'
|
R
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
c
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
cut_point
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
cut_normal
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
a_plus
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
a_minus
|
optional fixed values used
|
either as defaults for non-optimized variables or, in 'scale_only' mode,
as the entire configuration. If |
None
|
n_samples
|
int, optional (default 200)
|
Number of random surface samples drawn from B (in addition to B's vertices) for the feasibility test. |
200
|
n_particles
|
int
|
Particle swarm hyperparameters (kept small by default since each feasibility evaluation is expensive on the CPU). |
30
|
max_iter
|
int
|
Particle swarm hyperparameters (kept small by default since each feasibility evaluation is expensive on the CPU). |
30
|
scale_tol
|
float, optional (default 1e-3)
|
Binary-search tolerance for the inner scale search. |
0.001
|
warm_start
|
bool or dict, optional (default False)
|
Only relevant for |
False
|
verbose
|
bool, optional (default False)
|
Print particle-swarm progress. |
False
|
seed
|
int or None
|
Seed for the surface-sampling RNG and the particle-swarm RNG. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
dict with keys
|
s : float, the optimal scale. R : (3,3) rotation matrix applied to B. c : (3,) translation, the new centroid of T(B). B_center : (3,) original centroid of B used as the rotation pivot. cut_point : (3,) point on the cut plane. cut_normal : (3,) unit normal of the cut plane. a_plus : (3,) removal direction for A above the plane. a_minus : (3,) removal direction for A below the plane. |
Notes
This is a CPU implementation of a method originally formulated with GPU
depth peeling. It is therefore much slower than the original. Use modest
n_samples, n_particles and max_iter for interactive experimentation.
Examples:
Inspect a result interactively with polyscope. The transformed inner
copy is T(B) = c + s · R · (B − B_center); the cut plane is rendered
as a square in the normal's tangent frame, and the two removal
directions are drawn as a curve network:
import polyscope as ps
import numpy as np
import gpytoolbox as gpy
V, F = gpy.read_mesh("bunny.obj")
V = V - V.mean(0); V = V / np.max(np.abs(V))
res = gpy.matryoshka(V, F, optimize='rigid',
n_samples=80, n_particles=20, max_iter=20, seed=0)
# Inner copy.
T_B = (res['s'] * (V - res['B_center']) @ res['R'].T) + res['c']
# Cut plane as a square in the tangent frame of cut_normal.
diag = float(np.linalg.norm(V.max(0) - V.min(0)))
n = res['cut_normal']
e = np.array([1.,0.,0.]) if abs(n[0])<0.9 else np.array([0.,1.,0.])
t1 = e - np.dot(e, n) * n; t1 /= np.linalg.norm(t1)
t2 = np.cross(n, t1)
h, p0 = 0.75*diag, res['cut_point']
quad_V = np.stack([p0 + a*h*t1 + b*h*t2
for (a,b) in [(-1,-1),(1,-1),(1,1),(-1,1)]])
quad_F = np.array([[0,1,2],[0,2,3]], dtype=np.int32)
# Removal directions as a 2-edge curve network.
arrows = np.stack([p0, p0+0.5*diag*res['a_plus'],
p0, p0+0.5*diag*res['a_minus']])
edges = np.array([[0,1],[2,3]], dtype=np.int32)
ps.init()
ps.register_surface_mesh("A", V, F, transparency=0.35)
ps.register_surface_mesh("T(B)", T_B, F)
ps.register_surface_mesh("cut plane", quad_V, quad_F, transparency=0.4)
ps.register_curve_network("removal dirs", arrows, edges, radius=0.005)
ps.show()
Source code in src/gpytoolbox/matryoshka.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |