Skip to content

[Kinetic] fixed numpy.ndarray indices bug (from #86) - #450

Merged
davetcoleman merged 6 commits into
moveit:kinetic-develfrom
UTNuclearRoboticsPublic:kinetic-devel
Feb 24, 2017
Merged

davetcoleman merged 6 commits into
moveit:kinetic-develfrom
UTNuclearRoboticsPublic:kinetic-devel

Conversation

@Kukanani

@Kukanani Kukanani commented Feb 22, 2017 •

Copy link
Copy Markdown
Contributor

Description

A fix for the numpy.ndarray indices bug, the second bug described in #86.

This fix was proposed by @lifangda01 in the same issue.

I was unsure what to do about clang-format, since it seems that it's for C++ only.

Checklist

  • Required: Code is auto formatted using clang-format
  • Extended the tutorials / documentation, if necessary reference
  • Include a screenshot if changing a GUI
  • Optional: Created tests, which fail without this PR reference
  • Optional: Decide if this should be cherry-picked to other current ROS branches (Indigo, Jade, Kinetic)

Thank you!

@v4hn

v4hn commented Feb 22, 2017

Copy link
Copy Markdown
Contributor

Nice! Thanks for the pull-request. You're right, strict formatting is only required for C++ (for now...).

I just verified, pyassimp moved from an internal datatype to numpy.ndarray for these fields.

Until now the code is compatible with the pyassimp versions of ROS indigo and kinetic.
This patch breaks compatibility with the old version.

Would you mind to change it into something along the lines of

if hasattr(face, '__len__') and len(face) == 3:
    ....
elif hasattr(face, 'indices') and len(face.indices) == 3:
    ....

to allow the script to run with the old version too?

@Kukanani

Kukanani commented Feb 22, 2017 •

Copy link
Copy Markdown
Contributor Author

Full disclosure: if accepted, this would be my first successful pull request.

I added an error that is thrown if both approaches fail. Let me know if that's being "too safe." Also, these checks are being run for every face in the mesh, which is extremely slow. I think the better design would be to check only the first face to determine the pyassimp version, then run one of two operations based on the check. Something like this:

        mesh = Mesh()
        first_face = scene.meshes[0].faces[0]
        if hasattr(first_face, '__len__'):
            for face in scene.meshes[0].faces:
                if len(face) == 3:
                    triangle = MeshTriangle()
                    triangle.vertex_indices = [face[0], face[1], face[2]]
                    mesh.triangles.append(triangle)
        elif hasattr(first_face, 'indices'):
            for face in scene.meshes[0].faces:
                if len(face.indices) == 3:
                    triangle = MeshTriangle()
                    triangle.vertex_indices = [face.indices[0],
                                               face.indices[1],
                                               face.indices[2]]
                    mesh.triangles.append(triangle)
        else:
            raise MoveItCommanderException("Unable to build triangles from mesh due to mesh object structure")
        for vertex in scene.meshes[0].vertices:

@v4hn

v4hn commented Feb 23, 2017

Copy link
Copy Markdown
Contributor

Hm, apparently GitHub decided to drop the mail response I sent yesterday evening. Sorry for that.
Here it is again:


Hehe, welcome to the happy folks who file pull-requests :)

You're right, it's not very efficient to check this for every face.
Sure, you can also move the conditional outside the loop and run different
loops if you prefer that.

Sadly, the library itself does not seem to export its own version, but in this
case, it might be better to set a global indicator `PYASSIMP_VERSION`(?) at the
very beginning of the file and use that for the check.
There's a conditional there already because pyassimp > 3.0 is included via
`import pyassimp` and not via `from pyassimp import pyassimp` anymore.

I don't think the additional exception is a good idea though.
The old code just ran on when it encountered an invalid face and added an empty
`MeshTriangle`.
Now, we can definitely skip adding the empty entry, but the rest of the mesh
should still be parsed.
There's all kinds of weird mesh files out there, so we should probably extract
whatever we can. :)

@Kukanani

Copy link
Copy Markdown
Contributor Author

I agree that if we are looping over every face, then it does not make sense to raise exceptions.

However, in the code I wrote above, the exception at the end is catching the case where the first face specifically is not loadable via either method. It assumes that all the faces in the mesh have the same Python structure. If that's not the case, then I would propose keeping the loop structure as it currently is, where hasttr() is evaluated for every face, and then remove the exception.

So I would say that the two best options are 1. Use the code above, checking only the first face's structure, with the exception, or 2. Check every face for pyassimp structure, throwing no exceptions.

@v4hn

v4hn commented Feb 23, 2017

Copy link
Copy Markdown
Contributor

I agree that if we are looping over every face, then it does not make sense to raise exceptions.

Sorry, that is the current state of the pull-request though. I commented on that...

  1. Use the code above, checking only the first face's structure, with the exception
  2. Check every face for pyassimp structure, throwing no exceptions.

Both ways are fine by me. Just choose one and implement it in the request. :)

With (1.), you might want to check for len(scene.meshes) > 0 and len(scene.meshes[0].faces) > 0 before for completeness sake.

@Kukanani

Copy link
Copy Markdown
Contributor Author

Alright, I think it should be ready to go.

@v4hn v4hn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Would be great if anyone else could approve and/or (squash)merge this.

@davetcoleman davetcoleman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LTGM, thanks!

@davetcoleman
davetcoleman merged commit f1f1f43 into moveit:kinetic-devel Feb 24, 2017
@130s 130s changed the title fixed numpy.ndarray indices bug (from #86) [Kinetic] fixed numpy.ndarray indices bug (from #86) Mar 4, 2017
@cschindlbeck

Copy link
Copy Markdown
Contributor

I encountered this bug in the indigo branch as well, the same changes seem to fix that for me, pull request?

@v4hn

v4hn commented Jul 26, 2017

Copy link
Copy Markdown
Contributor

@cschindlbeck I still suspect you broke your local setup somehow, but I don't see any harm in having this
in indigo-devel too.

Feel free to file a pull-request for this.

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.

4 participants