feat(sim): Add auto scale in convex decomposition. (#37)
This commit is contained in:
parent
0ca4a4894f
commit
768d1fbb1d
@ -19,6 +19,7 @@ import multiprocessing as mp
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import coacd
|
import coacd
|
||||||
|
import numpy as np
|
||||||
import trimesh
|
import trimesh
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -31,7 +32,11 @@ __all__ = [
|
|||||||
|
|
||||||
|
|
||||||
def decompose_convex_coacd(
|
def decompose_convex_coacd(
|
||||||
filename: str, outfile: str, params: dict, verbose: bool = False
|
filename: str,
|
||||||
|
outfile: str,
|
||||||
|
params: dict,
|
||||||
|
verbose: bool = False,
|
||||||
|
auto_scale: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
coacd.set_log_level("info" if verbose else "warn")
|
coacd.set_log_level("info" if verbose else "warn")
|
||||||
|
|
||||||
@ -40,6 +45,14 @@ def decompose_convex_coacd(
|
|||||||
|
|
||||||
result = coacd.run_coacd(mesh, **params)
|
result = coacd.run_coacd(mesh, **params)
|
||||||
combined = sum([trimesh.Trimesh(*m) for m in result])
|
combined = sum([trimesh.Trimesh(*m) for m in result])
|
||||||
|
|
||||||
|
# Compute collision_scale because convex decomposition usually makes the mesh larger.
|
||||||
|
if auto_scale:
|
||||||
|
convex_mesh_shape = np.ptp(combined.vertices, axis=0)
|
||||||
|
visual_mesh_shape = np.ptp(mesh.vertices, axis=0)
|
||||||
|
rescale = visual_mesh_shape / convex_mesh_shape
|
||||||
|
combined.vertices *= rescale
|
||||||
|
|
||||||
combined.export(outfile)
|
combined.export(outfile)
|
||||||
|
|
||||||
|
|
||||||
@ -57,6 +70,7 @@ def decompose_convex_mesh(
|
|||||||
pca: bool = False,
|
pca: bool = False,
|
||||||
merge: bool = True,
|
merge: bool = True,
|
||||||
seed: int = 0,
|
seed: int = 0,
|
||||||
|
auto_scale: bool = True,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Decompose a mesh into convex parts using the CoACD algorithm."""
|
"""Decompose a mesh into convex parts using the CoACD algorithm."""
|
||||||
@ -81,7 +95,7 @@ def decompose_convex_mesh(
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
decompose_convex_coacd(filename, outfile, params, verbose)
|
decompose_convex_coacd(filename, outfile, params, verbose, auto_scale)
|
||||||
if os.path.exists(outfile):
|
if os.path.exists(outfile):
|
||||||
return outfile
|
return outfile
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -91,7 +105,9 @@ def decompose_convex_mesh(
|
|||||||
if preprocess_mode != "on":
|
if preprocess_mode != "on":
|
||||||
try:
|
try:
|
||||||
params["preprocess_mode"] = "on"
|
params["preprocess_mode"] = "on"
|
||||||
decompose_convex_coacd(filename, outfile, params, verbose)
|
decompose_convex_coacd(
|
||||||
|
filename, outfile, params, verbose, auto_scale
|
||||||
|
)
|
||||||
if os.path.exists(outfile):
|
if os.path.exists(outfile):
|
||||||
return outfile
|
return outfile
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -118,6 +134,7 @@ def decompose_convex_mp(
|
|||||||
merge: bool = True,
|
merge: bool = True,
|
||||||
seed: int = 0,
|
seed: int = 0,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
|
auto_scale: bool = True,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Decompose a mesh into convex parts using the CoACD algorithm in a separate process.
|
"""Decompose a mesh into convex parts using the CoACD algorithm in a separate process.
|
||||||
|
|
||||||
@ -140,7 +157,7 @@ def decompose_convex_mp(
|
|||||||
ctx = mp.get_context("spawn")
|
ctx = mp.get_context("spawn")
|
||||||
p = ctx.Process(
|
p = ctx.Process(
|
||||||
target=decompose_convex_coacd,
|
target=decompose_convex_coacd,
|
||||||
args=(filename, outfile, params, verbose),
|
args=(filename, outfile, params, verbose, auto_scale),
|
||||||
)
|
)
|
||||||
p.start()
|
p.start()
|
||||||
p.join()
|
p.join()
|
||||||
@ -151,7 +168,7 @@ def decompose_convex_mp(
|
|||||||
params["preprocess_mode"] = "on"
|
params["preprocess_mode"] = "on"
|
||||||
p = ctx.Process(
|
p = ctx.Process(
|
||||||
target=decompose_convex_coacd,
|
target=decompose_convex_coacd,
|
||||||
args=(filename, outfile, params, verbose),
|
args=(filename, outfile, params, verbose, auto_scale),
|
||||||
)
|
)
|
||||||
p.start()
|
p.start()
|
||||||
p.join()
|
p.join()
|
||||||
|
|||||||
@ -99,9 +99,9 @@ def load_actor_from_urdf(
|
|||||||
body_type = "static" if use_static else "dynamic"
|
body_type = "static" if use_static else "dynamic"
|
||||||
builder.set_physx_body_type(body_type)
|
builder.set_physx_body_type(body_type)
|
||||||
builder.add_multiple_convex_collisions_from_file(
|
builder.add_multiple_convex_collisions_from_file(
|
||||||
collision_file if body_type == "dynamic" else visual_file,
|
collision_file,
|
||||||
material=material,
|
material=material,
|
||||||
scale=collision_scale if body_type == "dynamic" else visual_scale,
|
scale=collision_scale,
|
||||||
# decomposition="coacd",
|
# decomposition="coacd",
|
||||||
# decomposition_params=dict(
|
# decomposition_params=dict(
|
||||||
# threshold=0.05, max_convex_hull=64, verbose=False
|
# threshold=0.05, max_convex_hull=64, verbose=False
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user