rohan-sawhney / fcpw

fast distance and ray intersection queries
MIT License
286 stars 31 forks source link

Unexpected results for scene intersection. #14

Open HTDerekLiu opened 5 hours ago

HTDerekLiu commented 5 hours ago

Hi Rohan,

It is me again! I found that some queries for scene.intersect gave me unexpected results. In this example, I created two rays shooting from one side of the cube (yellow points) to the other side (purple points):

Screenshot 2024-10-18 at 4 25 40 PM

But one of them (upper pair) returns a hit, the other one (bottom pair) does not. I wonder whether you know where I may do wrong?

I extracted a self-contained script here for your reference

import fcpw
import polyscope as ps
import numpy as np

V = np.array([[ 1.6,  1.6,  1.6],
    [14.4,  1.6,  1.6],
    [ 1.6, 14.4,  1.6],
    [14.4, 14.4,  1.6],
    [ 1.6,  1.6, 14.4],
    [14.4,  1.6, 14.4],
    [ 1.6, 14.4, 14.4],
    [14.4, 14.4, 14.4]])
F = np.array([[2, 1, 0],
        [1, 2, 3],
        [4, 2, 0],
        [2, 4, 6],
        [1, 4, 0],
        [4, 1, 5],
        [6, 5, 7],
        [5, 6, 4],
        [3, 6, 7],
        [6, 3, 2],
        [5, 3, 7],
        [3, 5, 1]])

scene = fcpw.scene_3D()
scene.set_object_count(1)
scene.set_object_vertices(V, 0)
scene.set_object_triangles(F, 0)
aggregate_type = fcpw.aggregate_type.bvh_surface_area
build_vectorized_bvh = True
scene.build(aggregate_type, build_vectorized_bvh)

# ray casting 
origins = np.array([[1, 8, 11.5], 
                    [1, 4.5, 11.5]], dtype=float)
dirs = np.array([[1., 0, 0], 
                 [1., 0, 0]], dtype=float)
distance_bounds = np.array([2.0, 2.0], dtype=float)

interactions = fcpw.interaction_3D_list()
scene.intersect(origins, dirs, distance_bounds, interactions, False)

print(interactions[0].primitive_index)
print(interactions[1].primitive_index)

ps.init()
ps.register_surface_mesh("mesh", V, F)
ps.register_point_cloud("origin", origins)
ps.register_point_cloud("end point", origins + dirs * distance_bounds[:,None])
ps.show()
rohan-sawhney commented 1 hour ago

Thanks for reporting this! Seems to be a precision issue---the intersection point should lie along the edge of the nearest triangle. I'll debug it tomorrow :)