Check object (poly) intersect



  • I have a script (python):

    • Adds a sphere
    • Checks the spheres bounding box
    • Determines how many cubes would need to be drawn along X,Y,Z to stack cubes that cover the sphere

    I am trying to figure out how to remove all cubes that do not intersect with the Sphere?

    Thank you for any guidance.

    34cdecd4-ab05-470f-90af-f57416dd8690-image.png



  • Hi,

    I am not sure what you want to do on the long run (intersection testing in its most general form for arbitrary polygon objects via ray casting is quite run time complex and Python therefor not really the ideal language to do this), but if you are willing to make some concessions you could use signed distance functions (which would restrict you to objects which you can describe with these).

    In sort of python pseudo code for the simplest form of your case it would be something like this:

    def sdf_sphere(p, q, r):
        return (q - p).length() - r
    
    for box in my_boxes:
        if sdf_sphere(box.position, my_sphere.position, my_sphere.radius) > 0.:
            box.hide()
    

    You could increase the complexity by not just testing the origin of the box and figuring out the closest point of your box to the origin of the spehere and test for that point, by adding more primitive types, and – this is probably the most important one – by adding an underlying data structure to speed things up like an Octree.

    Cheers
    zipit



  • Thank you, this works well for the immediate issues. I am happy to learn about signed distance functions.

    FYI, I am trying to make a plugin that will chunk out pieces of the object (sphere) to be used on a CNC mill; the mill is limited in size for the size of material it can work with. Therefore, if this breaks up the surface of the object within these cubes, then it will bool the surface from each one that is intersecting. Those will be milled and then physically recombined. Kind of a long exercise to learn python in c4d.

    If you have other suggestions (I'm sure there are better solutions) -- they would be much appreciated.



  • @froslie said in Check object (poly) intersect:

    If you have other suggestions (I'm sure there are better solutions) -- they would be much appreciated.

    Well, not really. If you want to intersection test two polygon objects you have to either ray cast or describe them as primitives. SDF are just one (fun) way to describe objects, but in terms of limitations other methods are not better.

    If I understand your comments on the goals correctly your boxes will be always boxes, but your sphere might be something else, right? So basically you want to voxelize an object? Cinema4D has voxels since R20 but they are not yet fully implemented in Python. When you try to access the return value of VolumeObject.GetVolume() Cinema will crash. Maybe there is some undocumented Python functionality there to read the returned VolumeInterface only the devs can tell you about?

    So you would have to voxelize your object yourself. One solution could be to ray march your object with c4d.utils.GeRayCollider (Link). But I am not sure if this is the right thing for you to do when you want to learn Python, unless you already have a solid understanding of computer graphics.

    Cheers
    zipit



  • Hello,

    some information on collision checks between geometries can be found in this thread: Turn object into a point selection bounding box

    Regarding Volume Interface: if you have problems using the Volume object, please report this in a new thread. There are some issues with the API, that are fixed in the next version.

    best wishes,
    Sebastian



  • Btw, if you just want to check if a position is inside a polygon object, you can simply shoot a ray from that position into any direction you want, and intersect it with the polygon mesh. If the number of found intersections is odd, you're inside; if it's even, you're outside. Only works reliably with closed meshes, of course.



  • @fwilleke80 as i mention in the other post, the problem (at least with our raycolider) is that if your ray hit an edge, it will hit 2 polygons at the same time. If it hit a point, it will hit <number of polygons attached to that point> times.

    It can be good enough but you have to be careful if you really want something solid.

    Cheers
    Manuel.


Log in to reply