rnd-team-dev / plotoptix

Data visualisation and ray tracing in Python based on OptiX 7.7 framework.
https://rnd.team/plotoptix
Other
499 stars 26 forks source link

Recovering the hit primitive ID #39

Closed vitalitylearning2021 closed 1 year ago

vitalitylearning2021 commented 1 year ago

Good day, I'm using the following code:

import numpy as np
from plotoptix import TkOptiX
from plotoptix import NpOptiX
import matplotlib.pyplot as plt

nu = 5
nv = 5

def displayResults(rt):

    print("Launch finished.")

    hitPositionsData = rt._hit_pos
    xHitPositions = hitPositionsData[:, :, 0]
    yHitPositions = hitPositionsData[:, :, 1]
    zHitPositions = hitPositionsData[:, :, 2]
    dHitPositions = hitPositionsData[:, :, 3]

    hitTriangle   = rt._geo_id[:, :, 0].reshape(rt._height, rt._width)

    print("Shape of rays array is {}.".format(xHitPositions.shape))

    print(xHitPositions)

    xHitPositions = xHitPositions[hitTriangle < 0xFFFFFFFF]
    yHitPositions = yHitPositions[hitTriangle < 0xFFFFFFFF]

    #hitTriangle[hitTriangle >= 100] = 0

    print("Shape of hitting rays array is {}.".format(xHitPositions.shape))

    print(rt._geo_id[:, :, 0].reshape(rt._height, rt._width))

    plt.plot(xHitPositions, yHitPositions, 'bo')
    plt.show()

    plt.imshow(dHitPositions)
    plt.colorbar()
    plt.show()

    plt.imshow(hitTriangle)
    plt.colorbar()
    plt.show()

    plt.draw()

verticesTriangle1   = np.array([[-2, -2, 0], [2, -2, 0], [-2, 2, 0]])
verticesTriangle2   = np.array([[ 2, -2, 0], [2,  2, 0], [-2, 2, 0]])
faceTriangle1       = np.array([0, 1, 2])
faceTriangle2       = np.array([0, 1, 2])

rt                  = NpOptiX(on_rt_accum_done = displayResults, width = nu, height = nv)

rt.set_mesh("Mesh1", verticesTriangle1, faceTriangle1)
rt.set_mesh("Mesh2", verticesTriangle2, faceTriangle2)

u                   = np.linspace(-2, 2, nu)
v                   = np.linspace(-2, 2, nv)
V, U                = np.meshgrid(v, u)
W                   = np.full((nu, nv), -1)
originsTexture      = np.stack((U, V, W, np.zeros((nu, nv)))).T

rt.set_texture_2d("origins", originsTexture)

cx                  = np.zeros((nu, nv))
cy                  = np.zeros((nu, nv))
cz                  = np.ones((nu, nv))
r                   = np.full((nu, nv), 200)
directionsTexture   = np.stack((cx, cy, cz, r)).T

rt.set_texture_2d("directions", directionsTexture)

rt.setup_camera("custom_cam", cam_type = "CustomProjXYZtoDir", textures=["origins", "directions"])

rt.set_param(max_accumulation_frames = 1)

rt.start()

#rt.close()

By rt._geo_id[:, :, 0], I would like to infer the ID of the primitive hit by the rays. However, the output seems to be:

[[4294967295 4294967295 4294967295 4294967295 4294967295]
 [         1          1 1073741825 1073741825 4294967295]
 [2147483649 2147483649 1073741825          2 4294967295]
 [2147483649 2147483649 2147483650 1073741826 4294967295]
 [2147483649 2147483650 1073741826 1073741826 4294967295]]

The large numbers at the borders of the matrix mean hit miss. However, I have only two primitives in the mesh and I do not understand why do I have more IDs than primitives.

robertsulej commented 1 year ago

Please, try rt._geo_id[:, :, 1], face id's are stored there. rt._geo_id[:, :, 0] contains id's of entire meshes and the nearest vertex id's, as described here.

vitalitylearning2021 commented 1 year ago

Thank you very much for your answer. That closes the issue.