Spaces:
Sleeping
Sleeping
File size: 4,398 Bytes
66c9c8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
import unittest
import numpy as np
import warp as wp
from warp.tests.unittest_utils import *
wp.init()
@wp.kernel
def bvh_query_aabb(bvh_id: wp.uint64, lower: wp.vec3, upper: wp.vec3, bounds_intersected: wp.array(dtype=int)):
query = wp.bvh_query_aabb(bvh_id, lower, upper)
bounds_nr = int(0)
while wp.bvh_query_next(query, bounds_nr):
bounds_intersected[bounds_nr] = 1
@wp.kernel
def bvh_query_ray(bvh_id: wp.uint64, start: wp.vec3, dir: wp.vec3, bounds_intersected: wp.array(dtype=int)):
query = wp.bvh_query_ray(bvh_id, start, dir)
bounds_nr = int(0)
while wp.bvh_query_next(query, bounds_nr):
bounds_intersected[bounds_nr] = 1
def aabb_overlap(a_lower, a_upper, b_lower, b_upper):
if (
a_lower[0] > b_upper[0]
or a_lower[1] > b_upper[1]
or a_lower[2] > b_upper[2]
or a_upper[0] < b_lower[0]
or a_upper[1] < b_lower[1]
or a_upper[2] < b_lower[2]
):
return 0
else:
return 1
def intersect_ray_aabb(start, dir, lower, upper):
l1 = (lower[0] - start[0]) * dir[0]
l2 = (upper[0] - start[0]) * dir[0]
lmin = min(l1, l2)
lmax = max(l1, l2)
l1 = (lower[1] - start[1]) * dir[1]
l2 = (upper[1] - start[1]) * dir[1]
lmin = max(min(l1, l2), lmin)
lmax = min(max(l1, l2), lmax)
l1 = (lower[2] - start[2]) * dir[2]
l2 = (upper[2] - start[2]) * dir[2]
lmin = max(min(l1, l2), lmin)
lmax = min(max(l1, l2), lmax)
if lmax >= 0.0 and lmax >= lmin:
return 1
else:
return 0
def test_bvh(test, type, device):
rng = np.random.default_rng(123)
num_bounds = 100
lowers = rng.random(size=(num_bounds, 3)) * 5.0
uppers = lowers + rng.random(size=(num_bounds, 3)) * 5.0
device_lowers = wp.array(lowers, dtype=wp.vec3, device=device)
device_uppers = wp.array(uppers, dtype=wp.vec3, device=device)
bvh = wp.Bvh(device_lowers, device_uppers)
bounds_intersected = wp.zeros(shape=(num_bounds), dtype=int, device=device)
query_lower = wp.vec3(2.0, 2.0, 2.0)
query_upper = wp.vec3(8.0, 8.0, 8.0)
query_start = wp.vec3(0.0, 0.0, 0.0)
query_dir = wp.normalize(wp.vec3(1.0, 1.0, 1.0))
for test_case in range(2):
if type == "AABB":
wp.launch(
kernel=bvh_query_aabb,
dim=1,
inputs=[bvh.id, query_lower, query_upper, bounds_intersected],
device=device,
)
else:
wp.launch(
kernel=bvh_query_ray, dim=1, inputs=[bvh.id, query_start, query_dir, bounds_intersected], device=device
)
device_intersected = bounds_intersected.numpy()
for i in range(num_bounds):
lower = lowers[i]
upper = uppers[i]
if type == "AABB":
host_intersected = aabb_overlap(lower, upper, query_lower, query_upper)
else:
host_intersected = intersect_ray_aabb(query_start, query_dir, lower, upper)
test.assertEqual(host_intersected, device_intersected[i])
if test_case == 0:
lowers = rng.random(size=(num_bounds, 3)) * 5.0
uppers = lowers + rng.random(size=(num_bounds, 3)) * 5.0
wp.copy(device_lowers, wp.array(lowers, dtype=wp.vec3, device=device))
wp.copy(device_uppers, wp.array(uppers, dtype=wp.vec3, device=device))
bvh.refit()
bounds_intersected.zero_()
def test_bvh_query_aabb(test, device):
test_bvh(test, "AABB", device)
def test_bvh_query_ray(test, device):
test_bvh(test, "ray", device)
devices = get_test_devices()
class TestBvh(unittest.TestCase):
pass
add_function_test(TestBvh, "test_bvh_aabb", test_bvh_query_aabb, devices=devices)
add_function_test(TestBvh, "test_bvh_ray", test_bvh_query_ray, devices=devices)
if __name__ == "__main__":
wp.build.clear_kernel_cache()
unittest.main(verbosity=2)
|