Skip to content
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

Merged
merged 8 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bindings/python/examples/run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sys
import importlib
import trimesh
import numpy as np
from time import time

if __name__ == "__main__":
Expand All @@ -32,10 +33,13 @@
module = importlib.import_module(f)
t0 = time()
model = module.run()
mesh = model.to_mesh()
mesh = model.to_meshgl()
if export_models:
vertices = np.reshape(mesh.vert_properties, (-1, mesh.num_prop))
if mesh.num_prop > 3:
vertices = np.hsplit(vertices, 3)
meshOut = trimesh.Trimesh(
vertices=mesh.vert_pos, faces=mesh.tri_verts)
vertices=vertices, faces=np.reshape(mesh.tri_verts, (-1, 3)))
Copy link
Owner Author

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.

Copy link
Collaborator

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...)

Copy link
Owner Author

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.

Copy link
Collaborator

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

trimesh.exchange.export.export_mesh(meshOut, f'{f}.glb', 'glb')
print(f'Exported model to {f}.glb')
t1 = time()
Expand Down
214 changes: 123 additions & 91 deletions bindings/python/manifold3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,33 @@ 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_meshgl",
[](Manifold &self, std::optional<py::array_t<uint32_t>> &normalIdx) {
glm::ivec3 v(0);
if (normalIdx.has_value()) {
auto normalIdx_view = normalIdx.value().unchecked<1>();
if (normalIdx_view.shape(0) != 3)
throw std::runtime_error("Invalid vector shape");
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,
Expand Down Expand Up @@ -459,111 +485,117 @@ 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")
.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>();
py::init([](const py::array_t<float> &vertProp,
const py::array_t<int> &triVerts,
const std::optional<py::array_t<uint32_t>> &mergeFromVert,
const std::optional<py::array_t<uint32_t>> &mergeToVert,
const std::optional<py::array_t<uint32_t>> &runIndex,
const std::optional<py::array_t<uint32_t>> &runOriginalID,
const std::optional<py::array_t<float>> &runTransform,
const std::optional<py::array_t<uint32_t>> &faceID,
const std::optional<py::array_t<float>> &halfedgeTangent,
float precision) {
MeshGL out;

auto vertProp_view = vertProp.unchecked<2>();
auto triVerts_view = triVerts.unchecked<2>();
if (vertPos_view.shape(1) != 3)
throw std::runtime_error("Invalid vert_pos shape");
const uint32_t numProp = vertProp_view.shape(1);
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 < vertProp_view.shape(0); i++)
for (int j = 0; j < numProp; ++j)
out.vertProperties.push_back(vertProp_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);
out.triVerts.push_back(triVerts_view(i, j));

// 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 (mergeFromVert.has_value()) {
auto mergeFrom_view = mergeFromVert.value().unchecked<1>();
for (int i = 0; i < mergeFrom_view.shape(0); ++i) {
out.mergeFromVert.push_back(mergeFrom_view(i));
}
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");
}

if (mergeToVert.has_value()) {
auto mergeTo_view = mergeToVert.value().unchecked<1>();
for (int i = 0; i < mergeTo_view.shape(0); ++i) {
out.mergeToVert.push_back(mergeTo_view(i));
}
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);
}

return Mesh({vertPos_vec, triVerts_vec, vertNormal_vec,
halfedgeTangent_vec});
if (runIndex.has_value()) {
auto runIndex_view = runIndex.value().unchecked<1>();
for (int i = 0; i < runIndex_view.shape(0); ++i) {
out.runIndex.push_back(runIndex_view(i));
}
}
}),
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>();

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 (runOriginalID.has_value()) {
auto runOriginalID_view = runOriginalID.value().unchecked<1>();
for (int i = 0; i < runOriginalID_view.shape(0); ++i) {
out.runOriginalID.push_back(runOriginalID_view(i));
}
}

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 (runTransform.has_value()) {
auto runTransform_view = runTransform.value().unchecked<3>();
if (runTransform_view.shape(1) != 4 ||
runTransform_view.shape(2) != 3)
throw std::runtime_error("Invalid run_transform shape");
for (int i = 0; i < runTransform_view.shape(0); ++i) {
for (const int col : {0, 1, 2, 3}) {
for (const int row : {0, 1, 2}) {
out.runTransform.push_back(runTransform_view(i, col, row));
}
}
}
}

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 (faceID.has_value()) {
auto faceID_view = faceID.value().unchecked<1>();
for (int i = 0; i < faceID_view.shape(0); ++i) {
out.faceID.push_back(faceID_view(i));
}
}

if (halfedgeTangent.has_value()) {
auto halfedgeTangent_view =
halfedgeTangent.value().unchecked<3>();
if (halfedgeTangent_view.shape(1) != 3 ||
halfedgeTangent_view.shape(2) != 4)
throw std::runtime_error("Invalid halfedge_tangent shape");
for (int i = 0; i < halfedgeTangent_view.shape(0); ++i) {
for (const int j : {0, 1, 2}) {
for (const int k : {0, 1, 2, 3}) {
out.halfedgeTangent.push_back(
halfedgeTangent_view(i, j, k));
}
}
}
}

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;
});
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)
.def_readonly("tri_verts", &MeshGL::triVerts)
.def_readonly("merge_from_vert", &MeshGL::mergeFromVert)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these automatically take the pointer, or are they copying the std::vector instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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);

py::enum_<CrossSection::FillRule>(m, "FillRule")
.value("EvenOdd", CrossSection::FillRule::EvenOdd,
Expand Down