ashawkey / raytracing

A CUDA Mesh RayTracer with BVH acceleration, with python bindings and a GUI.
MIT License
95 stars 16 forks source link

How to get the face id? #3

Closed yihua7 closed 2 years ago

yihua7 commented 2 years ago

Thanks a lot for the excellent code!!! I'm asking if we could get the intersected face id from the code, which is really helpful in lots of cases.

ashawkey commented 2 years ago

The face id can be retrieved from here in the CUDA side, but it is not passed to the python side. You can add another buffer to output this value.

yihua7 commented 2 years ago

I use a int64_t array face_idx to retrieve the p.first as this:

    // face normal is written to directions.
    if (p.first >= 0) {
        normals[i] = triangles[p.first].normal();
        face_idx[i] = p.first;
    } else {
        normals[i].setZero();
        face_idx[0] = -1;
    }

However, when I visualize the intersected face gathered by face_id, the results are inconsistent to the intersections. The latter is correct. snapshot00 The mesh model is from renderer.py. The yellow points are intersections and faces are picked by face_idx discussed above. The input rays_o and rays_d are:

    rays_o = torch.tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]).float()
    rays_d = torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).float()

Is there something wrong in my implementation? Thanks for your reply.

yihua7 commented 2 years ago

The problem can be solved by adding an additional attribute to the structure Triangle : int64_t idx; Then initialize it here:

        for (size_t i = 0; i < n_triangles; i++) {
            triangles_cpu[i] = {vertices.row(triangles(i, 0)), vertices.row(triangles(i, 1)), vertices.row(triangles(i, 2)), (int64_t) i};
        }

And the buffer is set as:

    if (p.first >= 0) {
        normals[i] = triangles[p.first].normal();
        face_idx[i] = triangles[p.first].idx;
    } else {
        normals[i].setZero();
        face_idx[0] = -1;
    }

It turns out that p.first is not the index for the input faces.