triro 1.3.0
A Python Ray-Mesh Intersector in OptiX
Loading...
Searching...
No Matches
ops.py
Go to the documentation of this file.
1import os
2import torch
3from typing import Tuple
4from jaxtyping import Float32, Bool, Int32
5import importlib
6import torch.utils.cpp_extension
7from triro.ray.ray_optix import OptixAccelStructureWrapper
8
9triro_module = None
10
11
13 """
14 Get the triro module by compiling it if necessary and importing it.
15
16 Returns:
17 The triro module.
18 """
19 global triro_module
20 if triro_module is not None:
21 return triro_module
22 # compile module
23 # source file
24 source_files = ['base.cpp', 'binding.cpp', 'ray.cpp']
25 # optix install location
26 optix_install_dir = os.environ['OptiX_INSTALL_DIR']
27 # include optix
28 cflags = [f'-I{optix_install_dir}/include']
29 # link with cuda lib
30 ldflags = ['-lcuda']
31 # full source path
32 source_paths = [os.path.join(os.path.dirname(__file__), fn) for fn in source_files]
33 # for windows
34 if os.name == 'nt':
35 cflags += ['/DNOMINMAX'] # avoid conflict in windows headers
36 ldflags = ['/DLL', 'cuda.lib', 'cudart.lib', 'Advapi32.lib']
37 torch.utils.cpp_extension.load(
38 name="triro",
39 sources=source_paths,
40 extra_cflags=cflags,
41 extra_ldflags=ldflags,
42 with_cuda=True,
43 verbose=False,
44 )
45 triro_module = importlib.import_module("triro")
46 return triro_module
47
48
50 """
51 Initialize OptiX.
52 """
53 get_module().initOptix()
54
55
57 """
58 Create an OptiX context.
59 """
60 get_module().createOptixContext()
61
62
64 """
65 Create an OptiX module.
66 """
67 get_module().createOptixModule()
68
69
71 """
72 Create OptiX pipelines.
73 """
74 get_module().createOptixPipelines()
75
76
78 """
79 Build the SBTs (Shader Binding Tables).
80 """
81 get_module().buildSBT()
82
83
85 accel_structure: OptixAccelStructureWrapper,
86 origins: Float32[torch.Tensor, "*b 3"],
87 dirs: Float32[torch.Tensor, "*b 3"],
88) -> Bool[torch.Tensor, "*b"]:
89 """
90 Check if any ray intersects with the acceleration structure.
91
92 Args:
93 accel_structure: The acceleration structure.
94 origins: The origins of the rays.
95 dirs: The directions of the rays.
96
97 Returns:
98 A boolean tensor indicating if each ray intersects with the acceleration structure.
99 """
100 return get_module().intersectsAny(accel_structure._inner, origins, dirs)
101
102
104 accel_structure: OptixAccelStructureWrapper,
105 origins: Float32[torch.Tensor, "*b 3"],
106 dirs: Float32[torch.Tensor, "*b 3"],
107) -> Int32[torch.Tensor, "*b"]:
108 """
109 Find the index of the first intersection for each ray.
110
111 Args:
112 accel_structure: The acceleration structure.
113 origins: The origins of the rays.
114 dirs: The directions of the rays.
115
116 Returns:
117 An integer tensor indicating the index of the first intersection for each ray.
118 """
119 return get_module().intersectsFirst(accel_structure._inner, origins, dirs)
120
121
123 accel_structure: OptixAccelStructureWrapper,
124 origins: Float32[torch.Tensor, "*b 3"],
125 dirs: Float32[torch.Tensor, "*b 3"],
126) -> Tuple[
127 Bool[torch.Tensor, "*b"], # hit
128 Bool[torch.Tensor, "*b"], # front
129 Int32[torch.Tensor, "*b"], # triangle index
130 Float32[torch.Tensor, "*b 3"], # intersect location
131 Float32[torch.Tensor, "*b 2"], # uv
132]:
133 """
134 Find the closest intersection for each ray.
135
136 Args:
137 accel_structure: The acceleration structure.
138 origins: The origins of the rays.
139 dirs: The directions of the rays.
140
141 Returns:
142 A tuple containing the following tensors:
143 - A boolean tensor indicating if each ray hits an object.
144 - A boolean tensor indicating if each ray hits the front face of an object.
145 - An integer tensor indicating the index of the triangle that each ray intersects with.
146 - A float tensor indicating the location of the intersection for each ray.
147 - A float tensor indicating the UV coordinates of the intersection for each ray.
148 """
149 return get_module().intersectsClosest(accel_structure._inner, origins, dirs)
150
151
153 accel_structure: OptixAccelStructureWrapper,
154 origins: Float32[torch.Tensor, "*b 3"],
155 dirs: Float32[torch.Tensor, "*b 3"],
156) -> Int32[torch.Tensor, "*b"]:
157 """
158 Count the number of intersections for each ray.
159
160 Args:
161 accel_structure: The acceleration structure.
162 origins: The origins of the rays.
163 dirs: The directions of the rays.
164
165 Returns:
166 An integer tensor indicating the number of intersections for each ray.
167 """
168 return get_module().intersectsCount(accel_structure._inner, origins, dirs)
169
170
172 accel_structure: OptixAccelStructureWrapper,
173 origins: Float32[torch.Tensor, "*b 3"],
174 dirs: Float32[torch.Tensor, "*b 3"],
175) -> Tuple[
176 Float32[torch.Tensor, "h 3"], Int32[torch.Tensor, "h"], Int32[torch.Tensor, "h"]
177]:
178 """
179 Find the location of intersections for each ray.
180
181 Args:
182 accel_structure: The acceleration structure.
183 origins: The origins of the rays.
184 dirs: The directions of the rays.
185
186 Returns:
187 A tuple containing the following tensors:
188 - A float tensor indicating the location of the intersection for each ray.
189 - The index of the ray that had the intersection.
190 - An integer tensor indicating the index of the instance that each ray intersects with.
191 """
192 return get_module().intersectsLocation(accel_structure._inner, origins, dirs)
get_module()
Definition ops.py:12
Int32[torch.Tensor, "*b"] intersects_count(OptixAccelStructureWrapper accel_structure, Float32[torch.Tensor, "*b 3"] origins, Float32[torch.Tensor, "*b 3"] dirs)
Definition ops.py:156
create_optix_module()
Definition ops.py:63
create_optix_context()
Definition ops.py:56
create_optix_pipelines()
Definition ops.py:70
Tuple[ Bool[torch.Tensor, "*b"], # hit Bool[torch.Tensor, "*b"], # front Int32[torch.Tensor, "*b"], # triangle index Float32[torch.Tensor, "*b 3"], # intersect location Float32[torch.Tensor, "*b 2"], # uv] intersects_closest(OptixAccelStructureWrapper accel_structure, Float32[torch.Tensor, "*b 3"] origins, Float32[torch.Tensor, "*b 3"] dirs)
Definition ops.py:132
init_optix()
Definition ops.py:49
Int32[torch.Tensor, "*b"] intersects_first(OptixAccelStructureWrapper accel_structure, Float32[torch.Tensor, "*b 3"] origins, Float32[torch.Tensor, "*b 3"] dirs)
Definition ops.py:107
build_sbts()
Definition ops.py:77
Tuple[ Float32[torch.Tensor, "h 3"], Int32[torch.Tensor, "h"], Int32[torch.Tensor, "h"]] intersects_location(OptixAccelStructureWrapper accel_structure, Float32[torch.Tensor, "*b 3"] origins, Float32[torch.Tensor, "*b 3"] dirs)
Definition ops.py:177
Bool[torch.Tensor, "*b"] intersects_any(OptixAccelStructureWrapper accel_structure, Float32[torch.Tensor, "*b 3"] origins, Float32[torch.Tensor, "*b 3"] dirs)
Definition ops.py:88