Skip to content

Commit

Permalink
add box to option
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahWeiii committed Jul 12, 2024
1 parent e263eef commit d2a6c13
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 29 deletions.
91 changes: 91 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"format": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"span": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"variant": "cpp",
"vector": "cpp",
"*.tcc": "cpp",
"chrono": "cpp",
"compare": "cpp",
"concepts": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"source_location": "cpp",
"utility": "cpp",
"stop_token": "cpp"
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Here is the description of the parameters (sorted by importance).
* `-r/--resolution`: sampling resolution for Hausdorff distance calculation (1e3~1e4), default = 2000.
* `--pca`: flag to enable PCA pre-processing, default = false.
* `-k`: value of $k$ for R_v calculation, default = 0.3.
* `-am/--approximate-mode`: approximation shape type ("ch" for convex hulls, "box" for cubes), default = "ch".
* `--seed`: random seed used for sampling, default = random().

An example of changing the parameters:
Expand Down
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ int main(int argc, char *argv[])
{
params.preprocess_mode = argv[i + 1];
}
if (strcmp(argv[i], "-am") == 0 || strcmp(argv[i], "--approximate-mode") == 0)
{
params.apx_mode = argv[i + 1];
}
if (strcmp(argv[i], "-pr") == 0 || strcmp(argv[i], "--prep-resolution") == 0)
{
sscanf(argv[i + 1], "%d", &params.prep_resolution);
Expand Down
18 changes: 14 additions & 4 deletions public/coacd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
int max_convex_hull, std::string preprocess_mode,
int prep_resolution, int sample_resolution,
int mcts_nodes, int mcts_iteration, int mcts_max_depth,
bool pca, bool merge, unsigned int seed) {
bool pca, bool merge, std::string apx_mode, unsigned int seed) {

logger::info("threshold {}", threshold);
logger::info("max # convex hull {}", max_convex_hull);
Expand All @@ -27,6 +27,7 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
logger::info("mcts nodes {}", mcts_nodes);
logger::info("mcts iterations {}", mcts_iteration);
logger::info("merge {}", merge);
logger::info("approximate mode {}", apx_mode);
logger::info("seed {}", seed);

if (threshold < 0.01) {
Expand Down Expand Up @@ -56,6 +57,7 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold,
params.mcts_max_depth = mcts_max_depth;
params.pca = pca;
params.merge = merge;
params.apx_mode = apx_mode;
params.seed = seed;

Model m;
Expand Down Expand Up @@ -127,7 +129,7 @@ CoACD_MeshArray CoACD_run(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,
unsigned int seed) {
int apx_mode, unsigned int seed) {
coacd::Mesh mesh;
for (uint64_t i = 0; i < input.vertices_count; ++i) {
mesh.vertices.push_back({input.vertices_ptr[3 * i],
Expand All @@ -140,7 +142,7 @@ CoACD_MeshArray CoACD_run(CoACD_Mesh const &input, double threshold,
input.triangles_ptr[3 * i + 2]});
}

std::string pm;
std::string pm, apx;
if (preprocess_mode == preprocess_on) {
pm = "on";
} else if (preprocess_mode == preprocess_off) {
Expand All @@ -149,9 +151,17 @@ CoACD_MeshArray CoACD_run(CoACD_Mesh const &input, double threshold,
pm = "auto";
}

if (apx_mode == apx_ch) {
apx = "ch";
} else if (apx_mode == apx_box) {
apx = "box";
} else {
throw std::runtime_error("invalid approximation mode " + std::to_string(apx_mode));
}

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

CoACD_MeshArray arr;
arr.meshes_ptr = new CoACD_Mesh[meshes.size()];
Expand Down
7 changes: 5 additions & 2 deletions public/coacd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::vector<Mesh> CoACD(Mesh const &input, double threshold = 0.05,
int prep_resolution = 50, int sample_resolution = 2000,
int mcts_nodes = 20, int mcts_iteration = 150,
int mcts_max_depth = 3, bool pca = false,
bool merge = true, unsigned int seed = 0);
bool merge = true, std::string apx_mode = "ch", unsigned int seed = 0);
void set_log_level(std::string_view level);

} // namespace coacd
Expand All @@ -47,12 +47,15 @@ constexpr int preprocess_auto = 0;
constexpr int preprocess_on = 1;
constexpr int preprocess_off = 2;

constexpr int apx_ch = 0;
constexpr int apx_box = 1;

CoACD_MeshArray COACD_API CoACD_run(CoACD_Mesh const &input, double threshold,
int max_convex_hull, int preprocess_mode,
int prep_resolution, int sample_resolution,
int mcts_nodes, int mcts_iteration,
int mcts_max_depth, bool pca, bool merge,
unsigned int seed);
int apx_mode, unsigned int seed);

void COACD_API CoACD_setLogLevel(char const *level);
}
8 changes: 8 additions & 0 deletions python/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class CoACD_MeshArray(ctypes.Structure):
c_int,
c_bool,
c_bool,
c_int,
c_uint,
]
_lib.CoACD_run.restype = CoACD_MeshArray
Expand Down Expand Up @@ -84,6 +85,7 @@ def run_coacd(
mcts_max_depth: int = 3,
pca: int = False,
merge: bool = True,
apx_mode: str = "ch",
seed: int = 0,
):
vertices = np.ascontiguousarray(mesh.vertices, dtype=np.double)
Expand All @@ -110,6 +112,11 @@ def run_coacd(
else:
pm = 0

if apx_mode == "ch":
apx = 0
elif apx_mode == "box":
apx = 1

mesh_array = _lib.CoACD_run(
mesh,
threshold,
Expand All @@ -122,6 +129,7 @@ def run_coacd(
mcts_max_depth,
pca,
merge,
apx,
seed,
)

Expand Down
8 changes: 8 additions & 0 deletions python/package/bin/coacd
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ if __name__ == "__main__":
action="store_true",
help="Use PCA to align input mesh. Suitable for non-axis-aligned mesh.",
)
parser.add_argument(
"-am",
"--apx-mode",
type=str,
default="ch",
help="Approximation shape mode (ch/box).",
)
parser.add_argument("--seed", type=int, default=0, help="Random seed.")

args = parser.parse_args()
Expand Down Expand Up @@ -125,6 +132,7 @@ if __name__ == "__main__":
mcts_max_depth=args.mcts_max_depth,
pca=args.pca,
merge=not args.no_merge,
apx_mode=args.apx_mode,
seed=args.seed,
)
mesh_parts = []
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace coacd
bool merge;
int max_convex_hull;
double dmc_thres;
string apx_mode;

/////////////// MCTS Config ///////////////
int mcts_iteration;
Expand All @@ -55,6 +56,7 @@ namespace coacd
pca = false;
merge = true;
dmc_thres = 0.55;
apx_mode = "ch";

mcts_iteration = 150;
mcts_max_depth = 3;
Expand Down
1 change: 1 addition & 0 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace coacd
logger::info("\tPCA (ON/OFF): {}", params.pca);
logger::info("\tk for Rv: {}", params.rv_k);
logger::info("\tHausdorff Sampling Resolution: {}", params.resolution);
logger::info("\tApproximation Mode (ch/box): {}", params.apx_mode);
logger::info("\tRandom Seed: {}", params.seed);
}

Expand Down
28 changes: 14 additions & 14 deletions src/mcts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace coacd
ori_mesh_area = MeshArea(initial_part);
ori_mesh_volume = MeshVolume(initial_part);
Model ch;
initial_part.ComputeCH(ch);
initial_part.ComputeAPX(ch);
ori_meshCH_volume = MeshVolume(ch);
current_cost = 0; // accumulated score
}
Expand All @@ -72,7 +72,7 @@ namespace coacd
ori_mesh_area = MeshArea(initial_part);
ori_mesh_volume = MeshVolume(initial_part);
Model ch;
initial_part.ComputeCH(ch);
initial_part.ComputeAPX(ch);
ori_meshCH_volume = MeshVolume(ch);
current_cost = 0;
}
Expand Down Expand Up @@ -153,8 +153,8 @@ namespace coacd
_current_parts.push_back(current_parts[i]);
}
}
pos.ComputeCH(posCH);
neg.ComputeCH(negCH);
pos.ComputeAPX(posCH);
neg.ComputeAPX(negCH);
double cost_pos = ComputeRv(pos, posCH, params.rv_k);
double cost_neg = ComputeRv(neg, negCH, params.rv_k);
Part part_pos(params, pos);
Expand Down Expand Up @@ -269,7 +269,7 @@ namespace coacd
double max_cost;

Model ch;
m.ComputeCH(ch);
m.ComputeAPX(ch);

Model pos, neg, posCH, negCH;
flag = Clip(m, pos, neg, first_plane, tmp);
Expand All @@ -278,8 +278,8 @@ namespace coacd
final_cost = INF;
return false;
}
pos.ComputeCH(posCH);
neg.ComputeCH(negCH);
pos.ComputeAPX(posCH);
neg.ComputeAPX(negCH);
double pos_cost = ComputeRv(pos, posCH, params.rv_k);
double neg_cost = ComputeRv(neg, negCH, params.rv_k);
scores.push_back(pos_cost);
Expand All @@ -303,8 +303,8 @@ namespace coacd
final_cost = INF;
return false;
}
_pos.ComputeCH(_posCH);
_neg.ComputeCH(_negCH);
_pos.ComputeAPX(_posCH);
_neg.ComputeAPX(_negCH);
double _pos_cost = ComputeRv(_pos, _posCH, params.rv_k);
double _neg_cost = ComputeRv(_neg, _negCH, params.rv_k);

Expand Down Expand Up @@ -653,8 +653,8 @@ namespace coacd
if (pos.points.size() <= 0 || neg.points.size() <= 0)
continue;

pos.ComputeCH(posCH);
neg.ComputeCH(negCH);
pos.ComputeAPX(posCH);
neg.ComputeAPX(negCH);

H = ComputeTotalRv(m, pos, posCH, neg, negCH, params.rv_k, planes[i]);
}
Expand Down Expand Up @@ -740,8 +740,8 @@ namespace coacd
_current_parts.push_back(current_state.current_parts[i]);
}
}
pos.ComputeCH(posCH);
neg.ComputeCH(negCH);
pos.ComputeAPX(posCH);
neg.ComputeAPX(negCH);
double cost_pos = ComputeRv(pos, posCH, params.rv_k);
double cost_neg = ComputeRv(neg, negCH, params.rv_k);

Expand Down Expand Up @@ -849,7 +849,7 @@ namespace coacd
{
int computation_budget = params.mcts_iteration;
Model initial_mesh = node->get_state()->current_parts[0].current_mesh, initial_ch;
initial_mesh.ComputeCH(initial_ch);
initial_mesh.ComputeAPX(initial_ch);
double cost = ComputeRv(initial_mesh, initial_ch, params.rv_k) / params.mcts_max_depth;
vector<Plane> current_path;

Expand Down
Loading

0 comments on commit d2a6c13

Please sign in to comment.