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

[SofaPython] ADD: Bindings for BoundingBox #736

Merged

Conversation

marques-bruno
Copy link
Member

This PR contains the code required to retrieve a component's bounding box in Python, and to manually call for a BaseObject's computeBBox method.

This was necessary in my case to create a RegularGrid from a mesh, since contrary to the sparseGrid, the regularGrid does not look at the node's MechanicalObject to create the grid.
An example scene in this PR shows my specific use case.

Concerning the bindings, I made the BoundingBox's minBBox and maxBBox read only since this should be computed by the component, and not set externally IMHO.

Could be useful to bind the different methods of BoundingBox too, but this is not covered in this PR


This PR:

  • builds with SUCCESS for all platforms on the CI.
  • does not generate new warnings.
  • does not generate new unit test failures.
  • does not generate new scene test failures.
  • does not break API compatibility.
  • is more than 1 week old (or has fast-merge label).

Reviewers will merge only if all these checks are true.

@marques-bruno marques-bruno added enhancement About a possible enhancement pr: status to review To notify reviewers to review this pull-request labels Jul 30, 2018
@marques-bruno marques-bruno self-assigned this Jul 30, 2018
@@ -270,6 +276,7 @@ SP_CLASS_METHOD_DOC(BaseObject, getCategories,
SP_CLASS_METHOD_DOC(BaseObject, getTarget,
"Returns the target (plugin) that contains the current object.")
SP_CLASS_METHOD(BaseObject,getAsACreateObjectParameter)
SP_CLASS_METHOD(BaseObject,computeBBox)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to use SP_CLASS_METHOD_DOC() to add a docstring so we can query the object from interactive python environemnt (like Ipython notebook) and we can generate the doc out of code.

import math
import numpy as np

class CreateGrid(Sofa.PythonScriptController):
Copy link
Contributor

Choose a reason for hiding this comment

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

Here I don't understand why you use a PythonScriptController with embedded arg parsing to create a Scene.
Cannot you do something like the following ?

def createGrid(parentNode, nx, ny, nz, mesh, name): 
        """
        create a grid...blahblah....
        """" 
        ## Add some checking on the values eg:
        
        m = parentNode.createObject('MeshObjLoader', name="loader", filename=mesh)
        model = parentNode.createObject('OglModel', name="model", src=m.getPathName())
        model.init()
        model.computeBBox()
        
        root.createObject('RegularGrid', name="grid", nx=nx, ny=ny, nz=nz,
                                                            min=model.bbox.minBBox, max=model.bbox.maxBBox)
        root.createObject('MechanicalObject', name="DOFs")
        ## ....        

def createScene(root)!
           try :
                  sys.argv[0]
           except :
                  sys.argv = sys.argv[1].split()
           else:
                  sys.argv = ['-h']
   
           parser = argparse.ArgumentParser(
                description='Loads a 3D model a generates a regular grid using its bounding box')
            parser.add_argument(
                '-x','--gridx', dest='nx', type=int, default=self.nx, help='')
            parser.add_argument(
                '-y','--gridy', dest='ny', type=int, default=self.ny, help='')
            parser.add_argument(
                '-z', '--gridz', dest='nz', type=int, default=self.nz, help='')
            parser.add_argument(
                '-m', '--mesh', dest='mesh', type=str, default=self.mesh, help='')
            parser.add_argument(
                '-o', '--output', dest='name', type=str, default="", help='')
            args = parser.parse_args(argv)
 
            createGrid(root, nx, ny, nz, mesh, name)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review!
Sure thing, no need for a PythonScriptController here.. I initially copy-pasted the relevant code from a scene of mine that needs a script controller. I'll change it to what you say.

topoExporter.createObject('TetrahedronSetTopologyModifier')
topoExporter.createObject('TetrahedronSetTopologyAlgorithms', template="Vec3d")
topoExporter.createObject('TetrahedronSetGeometryAlgorithms', template="Vec3d", drawTetrahedra="0")
topoExporter.createObject('Hexa2TetraTopologicalMapping', name="default28", input="@../grid", output="@Container")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is element of style but I recommend the systematic use of grid.getPathName() instead of hard coded link "@../grid" into string because this make the scene component relocatable in much easier way.


SP_CLASS_ATTR_SET(BoundingBox, minBBox)(PyObject */*self*/, PyObject * /*args*/, void*)
{
SP_MESSAGE_ERROR("BoundingBox attributes are read-only")
Copy link
Contributor

Choose a reason for hiding this comment

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

What is -1 for ? Is the the standard way to return a problem in a setter ?
If this is the case I think there is no need to send a message in addition to PyErro_BadArgument.
And if you want more details in the exception risen then it is best to use PyErr_SetString to set the message associated with the exxception.

In case the exception is not caught by python the message will be finally be printed by sofa using the sofa msg api.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got inspiration from DataFileName, that returns -1.
I will adjust

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if it is the standard way or the best way to do it, but that's how it is in every SP_CLASS_ATTR_SET methods that I found in Sofa.. :/

@damienmarchal
Copy link
Contributor

Hello Bruno,

Thanks for this PR, it is good to see more binding.

I dropped some comments on it. It is more style issue than fundamental ones.

@guparan guparan added pr: status wip Development in the pull-request is still in progress and removed pr: status to review To notify reviewers to review this pull-request labels Aug 1, 2018
@marques-bruno
Copy link
Member Author

Reviews taken into account in last commit. Tell me if you have any other comments I should take into account.

@marques-bruno marques-bruno added pr: status to review To notify reviewers to review this pull-request and removed pr: status wip Development in the pull-request is still in progress labels Aug 1, 2018
@jnbrunet
Copy link
Contributor

jnbrunet commented Aug 1, 2018

Looks good to me!

@marques-bruno marques-bruno added pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels Aug 2, 2018
@damienmarchal
Copy link
Contributor

[ci-build][with-scene-tests]

@damienmarchal damienmarchal merged commit c0bb4d3 into sofa-framework:master Aug 6, 2018
@marques-bruno marques-bruno deleted the bindings_BoundingBox branch August 7, 2018 11:38
@guparan guparan added this to the v18.12 milestone Oct 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement About a possible enhancement pr: status ready Approved a pull-request, ready to be squashed
Projects
No open projects
SofaPython
  
Awaiting triage
Development

Successfully merging this pull request may close these issues.

None yet

4 participants