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

adding python set_properties #504

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@
"Verts"
],
"javascript.format.semicolons": "insert",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none",
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ node test/manifold_test.js
### Python

The CMake script will build the python binding `manifold3d` automatically. To
use the extension, please add `$BUILD_DIR/tools` to your `PYTHONPATH`, where
use the extension, please add `$BUILD_DIR/bindings/python` to your `PYTHONPATH`, where
`$BUILD_DIR` is the build directory for CMake. Examples using the python binding
can be found in `bindings/python/examples`. To see exported samples, run:
```
Expand Down
39 changes: 39 additions & 0 deletions bindings/python/examples/sponge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from manifold3d import Manifold


def fractal(holes, hole, w, position, depth, maxDepth):
w /= 3
holes.append(hole.scale([w, w, 1.0]).translate([position[0], position[1], 0.0]))
if depth == maxDepth:
return
offsets = [
[-w, -w],
[-w, 0.0],
[-w, w],
[0.0, w],
[w, w],
[w, 0.0],
[w, -w],
[0.0, -w],
]
for offset in offsets:
fractal(holes, hole, w, position + offset, depth + 1, maxDepth)


def posColors(newProp, pos):
for i in [0, 1, 2]:
newProp[i] = (1 - pos[i]) / 2


def run(n=1):
result = Manifold.cube([1, 1, 1], True)
holes = []
fractal(holes, result, 1.0, [0.0, 0.0], 1, n)

hole = Manifold.compose(holes)

result -= hole
result -= hole.rotate([90, 0, 0])
result -= hole.rotate([0, 90, 0])

return result.trim_by_plane([1, 1, 1], 0).setProperties(3, posColors).scale(100)
32 changes: 32 additions & 0 deletions bindings/python/manifold3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ PYBIND11_MODULE(manifold3d, m) {
"the user to choose their function with discretion."
"\n\n"
":param warpFunc: A function that modifies a given vertex position.")
.def(
"set_properties",
[](Manifold &self, int newNumProp,
const std::function<void(py::array_t<float> &, Float3,
const py::array_t<float> &)> &f) {
const int oldNumProp = self.NumProp();
return self.SetProperties(newNumProp, [newNumProp, oldNumProp, &f](
float *newProps,
glm::vec3 v,
const float *oldProps) {
f(py::array(newNumProp, newProps), std::make_tuple(v.x, v.y, v.z),
py::array(oldNumProp, oldProps));
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'm getting a compile error here that I don't understand. Am I missing something?

});
},
py::arg("new_num_prop"), py::arg("f"),
"Create a new copy of this manifold with updated vertex properties "
"by supplying a function that takes the existing position and "
"properties as input. You may specify any number of output "
"properties, allowing creation and removal of channels. Note: "
"undefined behavior will result if you read past the number of input "
"properties or write past the number of output properties."
"\n\n"
":param numProp: The new number of properties per vertex."
":param propFunc: A function that modifies the properties of a given "
"vertex.")
.def(
"refine", [](Manifold &self, int n) { return self.Refine(n); },
py::arg("n"),
Expand Down Expand Up @@ -428,6 +453,13 @@ PYBIND11_MODULE(manifold3d, m) {
.def_static(
"from_mesh", [](const MeshGL &mesh) { return Manifold(mesh); },
py::arg("mesh"))
.def_static(
"compose",
[](const std::vector<Manifold> &list) {
return Manifold::Compose(list);
},
"combine several manifolds into one without checking for "
"intersections.")
.def_static(
"tetrahedron", []() { return Manifold::Tetrahedron(); },
"Constructs a tetrahedron centered at the origin with one vertex at "
Expand Down
Loading