Skip to content

Commit

Permalink
add ch extrude (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahWeiii committed Jul 20, 2024
1 parent 6c3917e commit 38ab9e4
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 4 deletions.
8 changes: 8 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ int main(int argc, char *argv[])
{
sscanf(argv[i + 1], "%d", &params.max_ch_vertex);
}
if (strcmp(argv[i], "-ex") == 0 || strcmp(argv[i], "--extrude") == 0)
{
params.extrude = true;
}
if (strcmp(argv[i], "-em") == 0 || strcmp(argv[i], "--extrude-margin") == 0)
{
sscanf(argv[i + 1], "%le", &params.extrude_margin);
}
if (strcmp(argv[i], "--pca") == 0)
{
params.pca = true;
Expand Down
9 changes: 8 additions & 1 deletion public/coacd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
int prep_resolution, int sample_resolution,
int mcts_nodes, int mcts_iteration, int mcts_max_depth,
bool pca, bool merge, bool decimate, int max_ch_vertex,
bool extrude, double extrude_margin,
std::string apx_mode, unsigned int seed) {

logger::info("threshold {}", threshold);
Expand All @@ -30,6 +31,8 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
logger::info("merge {}", merge);
logger::info("decimate {}", decimate);
logger::info("max_ch_vertex {}", max_ch_vertex);
logger::info("extrude {}", extrude);
logger::info("extrude margin {}", extrude_margin);
logger::info("approximate mode {}", apx_mode);
logger::info("seed {}", seed);

Expand Down Expand Up @@ -62,6 +65,8 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
params.merge = merge;
params.decimate = decimate;
params.max_ch_vertex = max_ch_vertex;
params.extrude = extrude;
params.extrude_margin = extrude_margin;
params.apx_mode = apx_mode;
params.seed = seed;

Expand Down Expand Up @@ -135,6 +140,7 @@ CoACD_MeshArray CoACD_run(CoACD_Mesh const &input, double threshold,
int mcts_nodes, int mcts_iteration,
int mcts_max_depth, bool pca, bool merge,
bool decimate, int max_ch_vertex,
bool extrude, double extrude_margin,
int apx_mode, unsigned int seed) {
coacd::Mesh mesh;
for (uint64_t i = 0; i < input.vertices_count; ++i) {
Expand Down Expand Up @@ -167,7 +173,8 @@ CoACD_MeshArray CoACD_run(CoACD_Mesh const &input, double threshold,

auto meshes = coacd::CoACD(mesh, threshold, max_convex_hull, pm,
prep_resolution, sample_resolution, mcts_nodes,
mcts_iteration, mcts_max_depth, pca, merge, decimate, max_ch_vertex, apx, seed);
mcts_iteration, mcts_max_depth, pca, merge, decimate, max_ch_vertex,
extrude, extrude_margin, apx, seed);

CoACD_MeshArray arr;
arr.meshes_ptr = new CoACD_Mesh[meshes.size()];
Expand Down
1 change: 1 addition & 0 deletions public/coacd.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CoACD_MeshArray COACD_API CoACD_run(CoACD_Mesh const &input, double threshold,
int mcts_nodes, int mcts_iteration,
int mcts_max_depth, bool pca, bool merge,
bool decimate, int max_ch_vertex,
bool extrude, double extrude_margin,
int apx_mode, unsigned int seed);

void COACD_API CoACD_setLogLevel(char const *level);
Expand Down
6 changes: 6 additions & 0 deletions python/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CoACD_MeshArray(ctypes.Structure):
c_bool,
c_bool,
c_int,
c_bool,
c_double,
c_int,
c_uint,
]
Expand Down Expand Up @@ -89,6 +91,8 @@ def run_coacd(
merge: bool = True,
decimate: bool = False,
max_ch_vertex: int = 256,
extrude: bool = False,
extrude_margin: float = 0.01,
apx_mode: str = "ch",
seed: int = 0,
):
Expand Down Expand Up @@ -135,6 +139,8 @@ def run_coacd(
merge,
decimate,
max_ch_vertex,
extrude,
extrude_margin,
apx,
seed,
)
Expand Down
15 changes: 15 additions & 0 deletions python/package/bin/coacd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ if __name__ == "__main__":
default=256,
help="max # vertices per convex hull, works only when decimate is enabled",
)
parser.add_argument(
"-ex",
"--extrude",
action="store_true",
help="If extrude is enabled, extrude the neighboring convex hulls along the overlap face (other faces are unchanged).",
)
parser.add_argument(
"-em",
"--extrude-margin",
type=float,
default=0.01,
help="extrude margin, works only when extrude is enabled",
)
parser.add_argument(
"-c",
"--max-convex-hull",
Expand Down Expand Up @@ -147,6 +160,8 @@ if __name__ == "__main__":
merge=not args.no_merge,
decimate=args.decimate,
max_ch_vertex=args.max_ch_vertex,
extrude=args.extrude,
extrude_margin=args.extrude_margin,
apx_mode=args.apx_mode,
seed=args.seed,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def build_extension(self, ext):

setup(
name="coacd",
version="1.0.4",
version="1.0.5",
author_email="[email protected]",
keywords="collision convex decomposition",
description="Approximate Convex Decomposition for 3D Meshes with Collision-Aware Concavity and Tree Search",
Expand Down
4 changes: 4 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace coacd
string apx_mode;
bool decimate;
int max_ch_vertex;
bool extrude;
double extrude_margin;

/////////////// MCTS Config ///////////////
int mcts_iteration;
Expand All @@ -61,6 +63,8 @@ namespace coacd
apx_mode = "ch";
decimate = false;
max_ch_vertex = 256;
extrude = false;
extrude_margin = 0.01;

mcts_iteration = 150;
mcts_max_depth = 3;
Expand Down
2 changes: 2 additions & 0 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace coacd
logger::info("\tMerge Postprocess (on/off): {}", params.merge);
logger::info("\tDecimate Postprocess (on/off): {}", params.decimate);
logger::info("\tMax Convex Hull Vertex: {}", params.max_ch_vertex);
logger::info("\tExtrude Postprocess (on/off): {}", params.extrude);
logger::info("\tExtrude Margin: {}", params.extrude_margin);
logger::info("\tPCA (ON/OFF): {}", params.pca);
logger::info("\tk for Rv: {}", params.rv_k);
logger::info("\tHausdorff Sampling Resolution: {}", params.resolution);
Expand Down
55 changes: 55 additions & 0 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,58 @@ namespace coacd
return h;
}

void ExtrudeCH(Model &ch, Plane overlap_plane, Params &params, double margin)
{
vec3d normal = {overlap_plane.a, overlap_plane.b, overlap_plane.c};

// decide the extrude direction by other points of the ch
int side = 0;
for (int i = 0; i < (int)ch.points.size(); i++)
{
vec3d p = ch.points[i];
side += overlap_plane.Side(p, 1e-4);
}
side = side > 0 ? 1 : -1;

for (int i = 0; i < (int)ch.points.size(); i++)
{
if (overlap_plane.Side(ch.points[i], 1e-4) == 0)
ch.points[i] = {ch.points[i][0] - side * margin * normal[0],
ch.points[i][1] - side * margin * normal[1],
ch.points[i][2] - side * margin * normal[2]};
}

Model tmp;
ch.ComputeAPX(tmp, params.apx_mode, true);
ch = tmp;
}

void ExtrudeConvexHulls(vector<Model> &cvxs, Params &params, double eps)
{
logger::info(" - Extrude Convex Hulls");
for (int i = 0; i < (int)cvxs.size(); i++)
{
Model cvx = cvxs[i];
for (int j = 0; j < (int)cvxs.size(); j++)
{
Model convex1 = cvxs[i], convex2 = cvxs[j];
Plane overlap_plane;

double dist = MeshDist(convex1, convex2);
bool flag = ComputeOverlapFace(convex1, convex2, overlap_plane);

// only extrude the convex hulls along the normal of overlap plane
if (dist < eps && flag)
{
ExtrudeCH(convex1, overlap_plane, params, params.extrude_margin);
cvxs[i] = convex1;
ExtrudeCH(convex2, overlap_plane, params, params.extrude_margin);
cvxs[j] = convex2;
}
}
}
}

vector<Model> Compute(Model &mesh, Params &params)
{
vector<Model> InputParts = {mesh};
Expand Down Expand Up @@ -467,6 +519,9 @@ namespace coacd
if (params.decimate)
DecimateConvexHulls(parts, params);

if (params.extrude)
ExtrudeConvexHulls(parts, params);

#ifdef _OPENMP
end = omp_get_wtime();
logger::info("Compute Time: {}s", double(end - start));
Expand Down
6 changes: 4 additions & 2 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ namespace coacd
extern thread_local std::mt19937 random_engine;

void DecimateCH(Model &ch, int tgt_pts, string apx_mode);
void DecimateConvexHulls(vector<Model> &cvxs, Params &params);
void DecimateConvexHulls(vector<Model> &cvxs, Params &params);
void MergeCH(Model &ch1, Model &ch2, Model &ch, Params &params);
double MergeConvexHulls(Model &m, vector<Model> &meshs, vector<Model> &cvxs, Params &params, double epsilon = 0.02, double threshold = 0.01);
vector<Model> Compute(Model &mesh, Params &params);
void ExtrudeCH(Model &ch, Plane overlap_plane, Params &params, double margin = 0.01);
void ExtrudeConvexHulls(vector<Model> &cvxs, Params &params, double eps = 1e-4);
vector<Model> Compute(Model &mesh, Params &params);
bool IsManifold(Model &input);

inline void addNeighbor(map<pair<int, int>, pair<int, int>> &edge_map, pair<int, int> &edge, vector<int> &neighbors, int idx)
Expand Down

0 comments on commit 38ab9e4

Please sign in to comment.