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

python bindings: mirror and Mesh with 2 args #459

Merged
merged 2 commits into from
Jun 27, 2023

Conversation

axel-angel
Copy link
Contributor

Hello there,

I have noticed those two things are missing in the python bindings. I checked other methods and didn't see anything obvious more to add so far, it's a small PR.

  • The mirror() function seems to be not exposed to Python.
  • The Mesh() constructor in C++ has 2 optional arguments but the Python bindings force it (with those .unchecked it just refused the call). I'm sorry I did a copy paste, that looked like the easiest and simplest way to achieve it. Let me know if you have an idea how to make this better.

I tested with my Python code. Hope this helps!

@google-cla
Copy link

google-cla bot commented Jun 18, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@codecov
Copy link

codecov bot commented Jun 18, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (6ae9dee) 89.94% compared to head (b39ff1f) 89.94%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #459   +/-   ##
=======================================
  Coverage   89.94%   89.94%           
=======================================
  Files          35       35           
  Lines        4286     4286           
=======================================
  Hits         3855     3855           
  Misses        431      431           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@elalish
Copy link
Owner

elalish commented Jun 19, 2023

@pca006132 It looks like the existing Mesh constructor is intended to have option arguments, but @axel-angel says they're not working that way. I'm not the best with Python, but I assume there must be some canonical way to do optional arguments so extra functions are not necessary?

@elalish
Copy link
Owner

elalish commented Jun 19, 2023

Have you managed to sign the CLA yet? The formatting should be taken care of if you run clang-format.

@axel-angel
Copy link
Contributor Author

axel-angel commented Jun 20, 2023

Have you managed to sign the CLA yet?

Yes I did sign.

The formatting should be taken care of if you run clang-format

I copy pasted the code from above, so the format is the same (unless I mixed tabs vs spaces)?

but I assume there must be some canonical way to do optional arguments

As said above, it comes from those "unchecked" calls. Maybe we can check for null and only do unchecked for those with the rest of the processing? I could check if it works on my side. Let me know if you have thoughts.

ValueError: array has incorrect number of dimensions: 1; expected 2

@pca006132
Copy link
Collaborator

The canonical way for optional argument is

py::arg("fillrule") = CrossSection::FillRule::Positive,

It should be simple to fix.

@axel-angel
Copy link
Contributor Author

Thanks to a post in pybind/pybind11#1953 (comment) I was able to properly make the argument optional. I also formatted using clang-format. 👌

@pca006132
Copy link
Collaborator

I think we can just have empty array as the optional argument. That can simplify the code a bit.

@axel-angel
Copy link
Contributor Author

I couldn't find how to create empty arrays of specific dimensions. Any hint?

          py::arg("vert_normal") = py::array_t<float>(shape={0, 3}),
          py::arg("halfedge_tangent") = py::array_t<float>(shape={0, 3}))

this fails

manifold/bindings/python/pymanifold.cpp:516:55: error: ‘shape’ was not declared in this scope; did you mean ‘unshare’?
  516 |           py::arg("vert_normal") = py::array_t<float>(shape={0, 3}),
      |                                                       ^~~~~
      |                                                       unshare

@pca006132
Copy link
Collaborator

           py::arg("vert_normal") = std::vector<glm::vec3>(),
           py::arg("halfedge_tangent") = std::vector<glm::vec4>())

@axel-angel
Copy link
Contributor Author

axel-angel commented Jun 21, 2023

@pca006132 Unfortunately it fails at auto vertNormal_view = vertNormal.unchecked<2>(); line in the original code:

ValueError: array has incorrect number of dimensions: 1; expected 2

I wish I had a clue how to fix this.

@pca006132
Copy link
Collaborator

try std::vector<std::vector<float>>? not on my computer now so cannot test this

@axel-angel
Copy link
Contributor Author

axel-angel commented Jun 21, 2023

@pca006132 Thanks. Still the same. I think it's expecting a real (allocated?) array because in python I was able to provide an empty as follows:

        m = pymanifold.Mesh(vertices.astype(np.float32),
                            faces.astype(np.int32),
                            # FIXME: Manifold forces Python binding to send arrays here
                            np.empty(shape=(0,0)), np.empty(shape=(0,0))
                            )

See np.empty. No idea how to reproduce that in C++

@pca006132
Copy link
Collaborator

No idea, a simple fix would be to provide a basic python script wrapper, similar to what we do with js.

@axel-angel
Copy link
Contributor Author

Any update? I'm proposing the following: (1) merging as is, (2) separating the change for mirror() into new PR so we can merge it and keep the Mesh() only here, so we can think about it (3) anyone can make that wrapper? I have no idea how to do it

@pca006132
Copy link
Collaborator

yeah I think we can just merge this

@pca006132 pca006132 merged commit 37db0d0 into elalish:master Jun 27, 2023
21 checks passed
jmicahc pushed a commit to SovereignShop/manifold that referenced this pull request Jul 4, 2023
* python bindings: Add mirror

* python bindings: fix Mesh constructor optional arguments
@elalish elalish mentioned this pull request Nov 3, 2023
cartesian-theatrics pushed a commit to SovereignShop/manifold that referenced this pull request Mar 11, 2024
* python bindings: Add mirror

* python bindings: fix Mesh constructor optional arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants