-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update python mesh #492
Update python mesh #492
Changes from 5 commits
f6cf8d2
9157350
99740da
2a7f9cf
a4cc9ee
e425761
6fa0e7b
311d25a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"transform", | ||
[](Manifold &self, py::array_t<float> &mat) { | ||
auto mat_view = mat.unchecked<2>(); | ||
if (mat_view.shape(0) != 3 || mat_view.shape(1) != 4) | ||
throw std::runtime_error("Invalid matrix shape"); | ||
if (mat.ndim() != 2 || mat.shape(0) != 3 || mat.shape(1) != 4) | ||
throw std::runtime_error("Invalid matrix shape, expected (3, 4)"); | ||
glm::mat4x3 mat_glm; | ||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 4; j++) { | ||
|
@@ -122,7 +122,7 @@ PYBIND11_MODULE(manifold3d, m) { | |
"translate", | ||
[](Manifold &self, py::array_t<float> &t) { | ||
auto t_view = t.unchecked<1>(); | ||
if (t.shape(0) != 3) | ||
if (t.ndim() != 1 || t.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape"); | ||
return self.Translate(glm::vec3(t_view(0), t_view(1), t_view(2))); | ||
}, | ||
|
@@ -146,8 +146,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"scale", | ||
[](Manifold &self, py::array_t<float> &scale) { | ||
auto scale_view = scale.unchecked<1>(); | ||
if (scale_view.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape"); | ||
if (scale.ndim() != 1 || scale.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape, expected (3)"); | ||
glm::vec3 v(scale_view(0), scale_view(1), scale_view(2)); | ||
return self.Scale(v); | ||
}, | ||
|
@@ -160,8 +160,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"mirror", | ||
[](Manifold &self, py::array_t<float> &mirror) { | ||
auto v_view = mirror.unchecked<1>(); | ||
if (v_view.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape"); | ||
if (mirror.ndim() != 1 || mirror.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape, expected (3)"); | ||
glm::vec3 v(v_view(0), v_view(1), v_view(2)); | ||
return self.Mirror(v); | ||
}, | ||
|
@@ -174,8 +174,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"rotate", | ||
[](Manifold &self, py::array_t<float> &v) { | ||
auto v_view = v.unchecked<1>(); | ||
if (v_view.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape"); | ||
if (v.ndim() != 1 || v.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape, expected (3)"); | ||
return self.Rotate(v_view(0), v_view(1), v_view(2)); | ||
}, | ||
py::arg("v"), | ||
|
@@ -238,7 +238,34 @@ PYBIND11_MODULE(manifold3d, m) { | |
"\n" | ||
":param n: The number of pieces to split every edge into. Must be > " | ||
"1.") | ||
.def("to_mesh", &Manifold::GetMesh) | ||
.def( | ||
"to_mesh", | ||
[](Manifold &self, std::optional<py::array_t<uint32_t>> &normalIdx) { | ||
glm::ivec3 v(0); | ||
if (normalIdx.has_value()) { | ||
if (normalIdx.value().ndim() != 1 || | ||
normalIdx.value().shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape, expected (3)"); | ||
auto normalIdx_view = normalIdx.value().unchecked<1>(); | ||
v = glm::ivec3(normalIdx_view(0), normalIdx_view(1), | ||
normalIdx_view(2)); | ||
} | ||
return self.GetMeshGL(v); | ||
}, | ||
"The most complete output of this library, returning a MeshGL that " | ||
"is designed to easily push into a renderer, including all " | ||
"interleaved vertex properties that may have been input. It also " | ||
"includes relations to all the input meshes that form a part of " | ||
"this result and the transforms applied to each." | ||
"\n\n" | ||
":param normalIdx: If the original MeshGL inputs that formed this " | ||
"manifold had properties corresponding to normal vectors, you can " | ||
"specify which property channels these are (x, y, z), which will " | ||
"cause this output MeshGL to automatically update these normals " | ||
"according to the applied transforms and front/back side. Each " | ||
"channel must be >= 3 and < numProp, and all original MeshGLs must " | ||
"use the same channels for their normals.", | ||
py::arg("normalIdx") = py::none()) | ||
.def("num_vert", &Manifold::NumVert, | ||
"The number of vertices in the Manifold.") | ||
.def("num_edge", &Manifold::NumEdge, | ||
|
@@ -395,8 +422,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"cube", | ||
[](py::array_t<float> &size, bool center = false) { | ||
auto size_view = size.unchecked<1>(); | ||
if (size_view.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape"); | ||
if (size.ndim() != 1 || size.shape(0) != 3) | ||
throw std::runtime_error("Invalid vector shape, expected (3)"); | ||
return Manifold::Cube( | ||
glm::vec3(size_view(0), size_view(1), size_view(2)), center); | ||
}, | ||
|
@@ -459,111 +486,90 @@ PYBIND11_MODULE(manifold3d, m) { | |
"marking sets of triangles that can be looked up after " | ||
"further operations. Assign to MeshGL.runOriginalID vector"); | ||
|
||
py::class_<Mesh>(m, "Mesh") | ||
py::class_<MeshGL>(m, "MeshGL") | ||
pca006132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.def( | ||
py::init([](py::array_t<float> &vertPos, py::array_t<int> &triVerts, | ||
std::optional<py::array_t<float>> &vertNormal, | ||
std::optional<py::array_t<float>> &halfedgeTangent) { | ||
auto vertPos_view = vertPos.unchecked<2>(); | ||
auto triVerts_view = triVerts.unchecked<2>(); | ||
if (vertPos_view.shape(1) != 3) | ||
throw std::runtime_error("Invalid vert_pos shape"); | ||
if (triVerts_view.shape(1) != 3) | ||
throw std::runtime_error("Invalid tri_verts shape"); | ||
std::vector<glm::vec3> vertPos_vec(vertPos_view.shape(0)); | ||
std::vector<glm::ivec3> triVerts_vec(triVerts_view.shape(0)); | ||
for (int i = 0; i < vertPos_view.shape(0); i++) | ||
for (const int j : {0, 1, 2}) | ||
vertPos_vec[i][j] = vertPos_view(i, j); | ||
for (int i = 0; i < triVerts_view.shape(0); i++) | ||
for (const int j : {0, 1, 2}) | ||
triVerts_vec[i][j] = triVerts_view(i, j); | ||
// note that reshape requires mutable array_t, but this will not | ||
// affect the original array passed into the function | ||
py::init([](py::array_t<float> &vertProp, py::array_t<int> &triVerts, | ||
const std::optional<std::vector<uint32_t>> &mergeFromVert, | ||
pca006132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const std::optional<std::vector<uint32_t>> &mergeToVert, | ||
const std::optional<std::vector<uint32_t>> &runIndex, | ||
const std::optional<std::vector<uint32_t>> &runOriginalID, | ||
std::optional<py::array_t<float>> &runTransform, | ||
pca006132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const std::optional<std::vector<uint32_t>> &faceID, | ||
const std::optional<py::array_t<float>> &halfedgeTangent, | ||
float precision) { | ||
MeshGL out; | ||
|
||
// optional arguments | ||
if (!vertNormal.has_value() && !halfedgeTangent.has_value()) { | ||
return Mesh({vertPos_vec, triVerts_vec}); | ||
} else { | ||
auto vertNormal_view = vertNormal.value().unchecked<2>(); | ||
auto halfedgeTangent_view = | ||
halfedgeTangent.value().unchecked<2>(); | ||
if (vertNormal_view.shape(0) != 0) { | ||
if (vertNormal_view.shape(1) != 3) | ||
throw std::runtime_error("Invalid vert_normal shape"); | ||
if (vertNormal_view.shape(0) != vertPos_view.shape(0)) | ||
throw std::runtime_error( | ||
"vert_normal must have the same length as vert_pos"); | ||
} | ||
if (halfedgeTangent_view.shape(0) != 0) { | ||
if (halfedgeTangent_view.shape(1) != 4) | ||
throw std::runtime_error("Invalid halfedge_tangent shape"); | ||
if (halfedgeTangent_view.shape(0) != triVerts_view.shape(0) * 3) | ||
throw std::runtime_error( | ||
"halfedge_tangent must be three times as long as " | ||
"tri_verts"); | ||
} | ||
std::vector<glm::vec3> vertNormal_vec(vertNormal_view.shape(0)); | ||
std::vector<glm::vec4> halfedgeTangent_vec( | ||
halfedgeTangent_view.shape(0)); | ||
for (int i = 0; i < vertNormal_view.shape(0); i++) | ||
for (const int j : {0, 1, 2}) | ||
vertNormal_vec[i][j] = vertNormal_view(i, j); | ||
for (int i = 0; i < halfedgeTangent_view.shape(0); i++) | ||
for (const int j : {0, 1, 2, 3}) | ||
halfedgeTangent_vec[i][j] = halfedgeTangent_view(i, j); | ||
out.numProp = vertProp.shape(1); | ||
vertProp = vertProp.reshape({-1}); | ||
out.vertProperties = std::vector<float>( | ||
vertProp.data(), vertProp.data() + vertProp.size()); | ||
|
||
return Mesh({vertPos_vec, triVerts_vec, vertNormal_vec, | ||
halfedgeTangent_vec}); | ||
} | ||
}), | ||
py::arg("vert_pos"), py::arg("tri_verts"), | ||
py::arg("vert_normal") = py::none(), | ||
py::arg("halfedge_tangent") = py::none()) | ||
.def_property_readonly("vert_pos", | ||
[](Mesh &self) { | ||
const int numVert = self.vertPos.size(); | ||
py::array_t<float> vert_pos({numVert, 3}); | ||
auto vert_pos_view = | ||
vert_pos.mutable_unchecked<2>(); | ||
if (triVerts.ndim() != 2 || triVerts.shape(1) != 3) | ||
throw std::runtime_error( | ||
"Invalid tri_verts shape, expected (-1, 3)"); | ||
triVerts = triVerts.reshape({-1}); | ||
out.triVerts = std::vector<uint32_t>( | ||
triVerts.data(), triVerts.data() + triVerts.size()); | ||
|
||
for (int i = 0; i < numVert; ++i) | ||
for (const int j : {0, 1, 2}) | ||
vert_pos_view(i, j) = self.vertPos[i][j]; | ||
return vert_pos; | ||
}) | ||
.def_property_readonly("tri_verts", | ||
[](Mesh &self) { | ||
const int numTri = self.triVerts.size(); | ||
py::array_t<int> tri_verts({numTri, 3}); | ||
auto tri_verts_view = | ||
tri_verts.mutable_unchecked<2>(); | ||
if (mergeFromVert.has_value()) | ||
out.mergeFromVert = mergeFromVert.value(); | ||
|
||
for (int i = 0; i < numTri; ++i) | ||
for (const int j : {0, 1, 2}) | ||
tri_verts_view(i, j) = self.triVerts[i][j]; | ||
return tri_verts; | ||
}) | ||
.def_property_readonly( | ||
"vert_normal", | ||
[](Mesh &self) { | ||
const int numVert = self.vertNormal.size(); | ||
py::array_t<float> vert_normal({numVert, 3}); | ||
auto vert_normal_view = vert_normal.mutable_unchecked<2>(); | ||
if (mergeToVert.has_value()) out.mergeToVert = mergeToVert.value(); | ||
|
||
if (runIndex.has_value()) out.runIndex = runIndex.value(); | ||
|
||
if (runOriginalID.has_value()) | ||
out.runOriginalID = runOriginalID.value(); | ||
|
||
for (int i = 0; i < numVert; ++i) | ||
for (const int j : {0, 1, 2}) | ||
vert_normal_view(i, j) = self.vertNormal[i][j]; | ||
return vert_normal; | ||
}) | ||
.def_property_readonly("halfedge_tangent", [](Mesh &self) { | ||
const int numEdge = self.halfedgeTangent.size(); | ||
py::array_t<float> halfedge_tangent({numEdge, 4}); | ||
auto halfedge_tangent_view = halfedge_tangent.mutable_unchecked<2>(); | ||
if (runTransform.has_value()) { | ||
auto runTransform1 = runTransform.value(); | ||
if (runTransform1.ndim() != 3 || runTransform1.shape(1) != 4 || | ||
runTransform1.shape(2) != 3) | ||
throw std::runtime_error( | ||
"Invalid run_transform shape, expected (-1, 4, 3)"); | ||
runTransform1 = runTransform1.reshape({-1}); | ||
out.runTransform = std::vector<float>( | ||
runTransform1.data(), | ||
runTransform1.data() + runTransform1.size()); | ||
} | ||
|
||
if (faceID.has_value()) out.faceID = faceID.value(); | ||
|
||
for (int i = 0; i < numEdge; ++i) | ||
for (const int j : {0, 1, 2, 3}) | ||
halfedge_tangent_view(i, j) = self.halfedgeTangent[i][j]; | ||
return halfedge_tangent; | ||
}); | ||
if (halfedgeTangent.has_value()) { | ||
auto halfedgeTangent1 = halfedgeTangent.value(); | ||
if (halfedgeTangent1.ndim() != 3 || | ||
halfedgeTangent1.shape(1) != 3 || | ||
halfedgeTangent1.shape(2) != 4) | ||
throw std::runtime_error( | ||
"Invalid halfedge_tangent shape, expected (-1, 3, 4)"); | ||
halfedgeTangent1 = halfedgeTangent1.reshape({-1}); | ||
out.halfedgeTangent = std::vector<float>( | ||
halfedgeTangent1.data(), | ||
halfedgeTangent1.data() + halfedgeTangent1.size()); | ||
} | ||
|
||
return out; | ||
}), | ||
py::arg("vert_properties"), py::arg("tri_verts"), | ||
py::arg("merge_from_vert") = py::none(), | ||
py::arg("merge_to_vert") = py::none(), | ||
py::arg("run_index") = py::none(), | ||
py::arg("run_original_id") = py::none(), | ||
py::arg("run_transform") = py::none(), | ||
py::arg("face_id") = py::none(), | ||
py::arg("halfedge_tangent") = py::none(), py::arg("precision") = 0) | ||
.def_readonly("vert_properties", &MeshGL::vertProperties) | ||
pca006132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.def_readonly("tri_verts", &MeshGL::triVerts) | ||
elalish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.def_readonly("merge_from_vert", &MeshGL::mergeFromVert) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these automatically take the pointer, or are they copying the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found that I forgot to add opaque types. https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#making-opaque-types Now, it should auotmatically take the pointer. |
||
.def_readonly("merge_to_vert", &MeshGL::mergeToVert) | ||
.def_readonly("run_index", &MeshGL::runIndex) | ||
.def_readonly("run_original_id", &MeshGL::runOriginalID) | ||
.def_readonly("run_transform", &MeshGL::runTransform) | ||
.def_readonly("face_id", &MeshGL::faceID) | ||
.def_readonly("halfedge_tangent", &MeshGL::halfedgeTangent) | ||
.def_readonly("num_prop", &MeshGL::numProp); | ||
pca006132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
py::enum_<CrossSection::FillRule>(m, "FillRule") | ||
.value("EvenOdd", CrossSection::FillRule::EvenOdd, | ||
|
@@ -673,8 +679,8 @@ PYBIND11_MODULE(manifold3d, m) { | |
"transform", | ||
[](CrossSection self, py::array_t<float> &mat) { | ||
auto mat_view = mat.unchecked<2>(); | ||
if (mat_view.shape(0) != 2 || mat_view.shape(1) != 3) | ||
throw std::runtime_error("Invalid matrix shape"); | ||
if (mat.ndim() != 2 || mat.shape(0) != 2 || mat.shape(1) != 3) | ||
throw std::runtime_error("Invalid matrix shape, expected (2, 3)"); | ||
glm::mat3x2 mat_glm; | ||
for (int i = 0; i < 2; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to update this for properties, which will involve splitting this up and putting the rest into
Trimesh.visual
. That can happen as a follow-on though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to finish everything in this PR, to test the new APIs are working correctly. (in case we understood python incorrectly...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, fair enough. Do you want to take a stab at it? Otherwise I can work on it tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah you can work on it