question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vectorized/parallel version of IntersectWithLine for ray casting

See original GitHub issue

Hi @marcomusy, I was checking on your example here and I was wondering whether it is possible to apply ray casting with multiple rays at once, without a for loop. For example if I have an origin point at p1: [0,0,0] and then multiple rays at different directions pnts: [[0,0,1], [0.25, 1, 0.5], [0.5, 1, 0.25]], how to get intersection points with a surface at once.

I was checking also whether I could maybe use intersectWithLine() with numpy.vectorize() or numpy.apply_along_axis(), but I am not sure how to do it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ttsesmcommented, Jun 6, 2020

Yes, it should be easy to vectorize it. But for now I think that the accelerated solution from trimesh should be fine for my needs.

Once I find some free time I will try to port it to pycuda. I think it would be a nice opportunity to get my hands dirty with cuda programming.

Thanks for your time 😃

1reaction
marcomusycommented, Jun 5, 2020

thanks for sharing this findings… my guess is that if you really need one or more orders of magnitude increase in speed you should go for the pycuda implementation, although just looking at the c++ Möller–Trumbore intersection algorithm implementation it seems to me that it could be vectorizable in numpy…

this is not vectorized, just a translation of the above Möller–Trumbore algo:


import numpy as np
from vtkplotter import *

def rayIntersectsTriangle(rayOrigin, 
                          rayVector, 
                          inTriangle):
    EPSILON = 0.0000001
    vertex0, vertex1, vertex2  = inTriangle
    edge1 = vertex1 - vertex0
    edge2 = vertex2 - vertex0
    h = np.cross(rayVector, edge2)
    a = np.dot(edge1, h)
    if abs(a) < EPSILON:
        return False   # This ray is parallel to this triangle.
    f = 1.0/a
    s = rayOrigin - vertex0
    u = f * np.dot(s, h)
    if u < 0.0 or u > 1.0:
        return False
    q = np.cross(s, edge1)
    v = f * np.dot(rayVector, q)
    if v < 0.0 or u + v > 1.0:
        return False
    #At this stage we can compute t to find out where the intersection point is on the line.
    t = f * np.dot(edge2, q)
    if t > EPSILON: # ray intersection
        return rayOrigin + rayVector * t
    else: # This means that there is a line intersection but not a ray intersection.
        return False

s = Sphere().alpha(0.1)
h = Hyperboloid(res=10).smoothWSinc().x(0.2).alpha(0.1).lw(0.1)

sp = s.points()
hp = h.points()
hfaces= h.faces() 
orig = np.array([0,0,0])

tri = hp[hfaces[187]]

tcenter = np.mean(tri, axis=0)
#tcenter= np.array([0,0,1]) # not hitting

inters_pt = rayIntersectsTriangle(orig, tcenter, tri)

atri = Points(tri)
ray = Line(orig, tcenter*2, c='blue')
hit = None
if inters_pt is not False:
    hit = Point(inters_pt)

show(h, atri, ray, hit, axes=1)

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Need a fast ray-box intersection that handles if a ray is parallel ...
This is a C++ implementation. Slab is the space between a pair of parallel planes.Test if ray R(t) = p + t *...
Read more >
Ray-Plane and Ray-Disk Intersection - Scratchapixel
If the ray intersects this plane, all we have to do is to compute the intersection point, then compute the distance from this...
Read more >
How do you check for intersection between a line segment ...
Let r = (cos θ, sin θ). Then any point on the ray through p is representable as p + t r (for...
Read more >
Ray tracing in Parallel - ScalaBlitz
Ray tracing is a way of rendering an image by simulating light ... For that, we need to be able to compute the...
Read more >
Ray-casting algorithm - Rosetta Code
Where the function ray_intersects_segment return true if the horizontal ray starting from the point P intersects the side (segment), false ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found