-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbindings.cpp
More file actions
150 lines (137 loc) · 5.61 KB
/
bindings.cpp
File metadata and controls
150 lines (137 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/eigen.h>
#include <omp.h>
#include "pipeline.h" // now declares the new signature
#include "Component.h"
#include "Config.h"
namespace py = pybind11;
static inline int to_int_or_neg1(const py::handle& h) {
if (h.is_none()) return -1;
return py::cast<int>(h);
}
// tree: { key: {"left": L, "right": R}, ... }
static std::vector<NodeRecord> nodes_from_py_dict(const py::dict& tree) {
std::vector<int> keys;
keys.reserve(tree.size());
for (auto&& kv : tree) {
keys.push_back(py::cast<int>(kv.first));
}
std::sort(keys.begin(), keys.end());
std::vector<NodeRecord> nodes;
nodes.reserve(keys.size());
for (int k : keys) {
py::dict rec = py::cast<py::dict>(tree[py::int_(k)]);
NodeRecord n{};
n.id = k;
n.left = to_int_or_neg1(rec["left"]);
n.right = to_int_or_neg1(rec["right"]);
nodes.push_back(n);
}
return nodes;
}
// ---------------------------------------------------------------------------
// 1. Wrapper for the “file‑based” call (optional – keep it if you still
// have a file‑loading overload of pipeline(); otherwise delete it).
// ---------------------------------------------------------------------------
static std::pair<UVParts, std::vector<UVParts>>
pipeline_file_py(const std::string& tree_filename,
const std::string& mesh_filename,
const std::string& configPath,
double threshold)
{
ConfigManager::instance().loadFromFile(configPath);
std::pair<UVParts, std::vector<UVParts>> out;
std::vector<UVParts> parts;
UVParts final_part = pipeline(tree_filename, mesh_filename, threshold, parts);
out = {final_part, std::move(parts)};
return out;
}
// ---------------------------------------------------------------------------
// 2. Matrix‑based wrapper – no globals, no helpers, just forward.
// ---------------------------------------------------------------------------
static std::pair<UVParts, std::vector<UVParts>>
pipeline_numpy_py(const Eigen::Ref<const Eigen::MatrixXd>& V,
const Eigen::Ref<const Eigen::MatrixXi>& F,
const py::dict& tree_dict,
const std::string& configPath,
double threshold,
bool pack_final_mesh)
{
std::pair<UVParts, std::vector<UVParts>> out;
std::vector<NodeRecord> nodes = nodes_from_py_dict(tree_dict);
std::vector<UVParts> parts;
UVParts final_part = pipeline(V, F, nodes, configPath, threshold, pack_final_mesh, parts);
out = {final_part, std::move(parts)};
return out;
}
int omp_threads_used() {
int used = 0;
#pragma omp parallel
{
#pragma omp single
used = omp_get_num_threads();
}
return used;
}
// ---------------------------------------------------------------------------
// 3. Module definition.
// ---------------------------------------------------------------------------
PYBIND11_MODULE(_core, m)
{
m.doc() = "UV‑unwrapping pipeline bindings";
py::class_<Component>(m, "Component")
.def(py::init<>())
.def_readwrite("V", &Component::V)
.def_readwrite("F", &Component::F)
.def_readwrite("UV", &Component::UV)
.def_readwrite("distortion", &Component::distortion)
.def_readwrite("index", &Component::index)
.def_readwrite("faces", &Component::faces)
.def("save_mesh", &Component::save_mesh)
.def("__add__", &Component::operator+);
// ------------ UVParts ---------------------------------------------------
py::class_<UVParts>(m, "UVParts")
.def(py::init<>())
.def(py::init<const std::vector<Component>&>())
.def(py::init<int>())
.def_readwrite("components", &UVParts::components)
.def_readwrite("distortion", &UVParts::distortion)
.def_readwrite("num_components", &UVParts::num_components)
.def_property_readonly(
"hierarchy_json",
[](const UVParts& p) { return p.hierarchy.to_json().dump(); }
)
.def("getUV", &UVParts::getUV)
.def("getNumFaces", &UVParts::getNumFaces)
.def("to_components",&UVParts::to_components)
.def("__add__", &UVParts::operator+)
.def("__eq__", &UVParts::operator==)
.def("__ne__", &UVParts::operator!=);
// ------------ Pipeline --------------------------------------------------
// Keep the file‑based front end only if you still have that overload.
m.def("pipeline",
&pipeline_file_py,
py::arg("tree_filename"),
py::arg("mesh_filename"),
py::arg("configPath"),
py::arg("threshold") = 1.25,
py::call_guard<py::gil_scoped_release>(),
R"pbdoc(
Run the UV‑unwrapping pipeline that loads the mesh from disk.
Returns (final_part, individual_parts).
)pbdoc");
m.def("pipeline_numpy",
&pipeline_numpy_py,
py::arg("V"),
py::arg("F"),
py::arg("tree_dict"),
py::arg("configPath"),
py::arg("threshold") = 1.25,
py::arg("pack_final_mesh") = false,
R"pbdoc(
Run the UV‑unwrapping pipeline directly on numpy arrays V (Nx3) and F (Mx3).
Returns (final_part, individual_parts).
)pbdoc");
m.def("omp_threads_used", &omp_threads_used, "Return OpenMP team size for a dummy region");
}