Open3D icon indicating copy to clipboard operation
Open3D copied to clipboard

Finding intersection of 3d objects and lines

Open carlosimoes opened this issue 5 years ago • 17 comments

Finding intersection of 3d objects and lines I'm working on a project that takes a point cloud from a .ply file and import to a open3D visualizer and I wanted to find whether a ray(line) intersects with a 3d object (it can also be a bounding box) and count how many of the lines intersect with a particular object.

imagem

In Open3D can I do this ?

carlosimoes avatar Jan 16 '21 11:01 carlosimoes

@carlosimos are you drawing the lines from the camera itself?

Zumbalamambo avatar Jan 16 '21 12:01 Zumbalamambo

This is just an example of what i want to do, not done yet with Open3D, but the lines should start from the camera, like ray casting @Zumbalamambo

carlosimoes avatar Jan 16 '21 12:01 carlosimoes

@carlosimoes, I saw the message you put in my other repository. I have notifications on so you could actually have tagged me in a comment on this issue and I would have found it. I'm going to close the unrelated issues and we can discuss here.

The Line3D and Ray3D classes will perform intersection tests against Axis Aligned Bounding Boxes as they are written. Testing against a sphere is also very simple; you can use the Projection method to find the closest point between your sphere center and the line/ray and check if the distance between them is less than the sphere radius.

Cylinder tests are a little more complicated.

The only caveat is that this functionality is C++ and I am not sure if bindings for these features have been made for Python. So if you're using Open3D in C++ then you can take advantage of these features.

mattj23 avatar Jan 18 '21 19:01 mattj23

Hi @mattj23 , Once again I'm sorry for posting in the other repository I didn't remember to tag you here. For this project, I'm using python... Is there any tutorial that I can follow? And where can I find the code in C++? because maybe I will write it in python so I can use it in my project. My objective is to draw several rays like in the image given the starting point and the direction and find in which Axis Aligned Bounding Boxes it intersects.

carlosimoes avatar Jan 18 '21 21:01 carlosimoes

@carlosimoes, I don't know much about the process of creating python bindings for C++ constructs. Looking at the Open3D/cpp/pybind folder I don't think anyone has creating bindings for Line3D and its subclasses.

The C++ header file can be found here: https://github.com/intel-isl/Open3D/blob/master/cpp/open3d/geometry/Line3D.h

The implementations are here: https://github.com/intel-isl/Open3D/blob/master/cpp/open3d/geometry/Line3D.cpp

If you are willing to undertake the creation of bindings for the Line3D constructs I can at least help you understand how the things I wrote work and potentially aid in that process. If you're just looking to re-implement the algorithms in Python they should be fairly straightforward, but the main advantage that the C++ implementation has is that it will run very quickly. If you're only testing a small number of rays against a small number of boxes a Python implementation should be fine. However, the naive case scales exponentially so as you add rays and boxes it will get out of control quickly, at which point you will probably want to create some sort of BVH or spatial partitioning tree to make it reasonable again.

What kind of tutorial are you looking for?

mattj23 avatar Jan 18 '21 23:01 mattj23

Thanks for the implementation and header links.

When I talked about the tutorial I was referring to a document that was more explicit in the function description or some examples like in http://www.open3d.org/docs/release/tutorial/geometry/index.html to gain more knowledge and understanding of how to use them. Initially, I think I will have a few rays and boxes but they may increase as the dataset in use can change. In terms of code, I think I will re-implement the algorithms in Python since I'm not very comfortable with bindings In your opinion where should I start? since neither rays are implemented for python, right? Creating some sort of BVH, spatial partitioning tree or the simplest case?

carlosimoes avatar Jan 19 '21 01:01 carlosimoes

In your opinion where should I start? since neither rays are implemented for python, right? Creating some sort of BVH, spatial partitioning tree or the simplest case?

Hmm, where are you starting from? Have you used Open3D before? Managed to load your PLY files? Create your AABBs?

If you're starting from nothing, I'd try to follow a basic tutorial to get a sample scene loaded, then create the AABBs.

If you're asking how you should start with the ray intersections, I'd not worry about a BVH or a tree just yet. Create a ray class in Python and implement the slab method just to make sure you can get it to work.

The base implementation is here, and the version for a ray adds an additional check to verify that any intersection occurs on the positive side of the ray origin.

You can also look more directly at the original implementation by Tavian Barnes which I based this code off of.

From there if you're finding that your code runs too slow you can think about implementing some sort of acceleration structure. I wouldn't bother until you know it's a problem.

Also, consider trying to use the C++ version of Open3D. It can be difficult to build and link depending on your platform, but if you're on Linux it might be worth a shot. It has a lot more features than are exposed to Python.

mattj23 avatar Jan 19 '21 02:01 mattj23

Hmm, where are you starting from? Have you used Open3D before? Managed to load your PLY files? Create your AABBs? If you're starting from nothing, I'd try to follow a basic tutorial to get a sample scene loaded, then create the AABBs

I already have: -Creation and loading of the PLY files -Remove the outliers from the object -Creation of the AABBs for the object inliers -The direction and the starting point for the ray -Visualization of the AABBs and objects [Also it's my first time working with the Open3D]

If you're asking how you should start with the ray intersections, I'd not worry about a BVH or a tree just yet. Create a ray class in Python and implement the slab method just to make sure you can get it to work. The base implementation is here, and the version for a ray adds an additional check to verify that any intersection occurs on the positive side of the ray origin. You can also look more directly at the original implementation by Tavian Barnes which I based this code off of.

For the ray class in Python do you have any suggestion of material that I can base? And thank you very much for the links to do the intersection!! I will use them and then I can show you the results!

Also, consider trying to use the C++ version of Open3D. It can be difficult to build and link depending on your platform, but if you're on Linux it might be worth a shot. It has a lot more features than are exposed to Python.

The problem is that all my project has been made in python ...

carlosimoes avatar Jan 19 '21 12:01 carlosimoes

Ah, you're very far along. This should be straight forward.

Yes, the easiest way to represent the ray will be with two 3d vectors. In the c++ code you'll see them accessed internally by calls to origin() and direction().

So your python class will probably want to be at least six floats at baseline, the x, y, z of the ray origin and the x, y, z of the ray direction. You will notice that the slab method also precomputes inverses of the ray directions since those are the computationally heaviest part of the method in c++.

You'll want to verify that python divide by zeros produce inf, since the slab method uses that trick in IEEE math to perform the check efficiently.

mattj23 avatar Jan 19 '21 13:01 mattj23

@mattj23 I have already implemented the slab method (https://tavianator.com/2015/ray_box_nan.html). I just wanted to ask if you still have the tests that you used to verification of the method? Or you just run the basics one (outside-in, in-outside and the margins)?

carlosimoes avatar Jan 27 '21 20:01 carlosimoes

The actual tests are in this file:

https://github.com/intel-isl/Open3D/blob/master/cpp/tests/geometry/Line3D.cpp

They're a little difficult to decipher because of the testing framework.

mattj23 avatar Jan 28 '21 16:01 mattj23

@carlosimoes Hi Carlos, I was also working on the same problem in python and have tried different approaches but without good results yet. Were you able to obtain the intersections of lines and objects in python?

cferns avatar Jun 24 '21 18:06 cferns

@cferns yes I have finish the project. Basically what I did was with the starting point and the vector detect if the ray intersects a bounding box, then I can describe which of the BBox is intersect by the rays.

carlosimoes avatar Jun 25 '21 09:06 carlosimoes

@carlosimoes Thanks for the reply. May I know what algorithm/method you found effective to obtain the intersection point of the ray and the box?

cferns avatar Jul 01 '21 20:07 cferns

As i already said before to mattj23, I implemented the slab method (https://tavianator.com/2015/ray_box_nan.html). And for the tests I made it worked fine! Did you test this algoritm? And if yes which tests failed? Maybe i can test the experiment and tell you my results @cferns.

carlosimoes avatar Jul 09 '21 11:07 carlosimoes

@carlosimoes I ended up converting the box to scipy.spatial.ConvexHull, then checking to see - for every point along the ray if the point lies inside or outside the convex hull (https://stackoverflow.com/questions/16750618/whats-an-efficient-way-to-find-if-a-point-lies-in-the-convex-hull-of-a-point-cl). Then iteratively finding a point approximately on the convex hull.

cferns avatar Jul 16 '21 23:07 cferns

We have added RayCasting support to Open3D in this release (0.14). Please checkout this tutorial for creating a scene and performing ray intersection tests. http://www.open3d.org/docs/release/tutorial/geometry/ray_casting.html

sanskar107 avatar Dec 03 '21 13:12 sanskar107