readme.md 1.67 KB
Newer Older
ashawkey's avatar
init  
ashawkey committed
1
2
3
4
5
6
7
# cuBVH

A CUDA Mesh BVH acceleration toolkit.

### Install

```python
ashawkey's avatar
ashawkey committed
8
9
10
pip install git+https://github.com/ashawkey/cubvh

# or locally
ashawkey's avatar
init  
ashawkey committed
11
12
13
14
15
16
17
18
19
20
21
git clone https://github.com/ashawkey/cubvh
cd cubvh
pip install .
```

### Usage

```python
import numpy as np
import trimesh
import torch
ashawkey's avatar
ashawkey committed
22

ashawkey's avatar
init  
ashawkey committed
23
24
import cubvh

ashawkey's avatar
ashawkey committed
25
### build BVH from mesh
ashawkey's avatar
init  
ashawkey committed
26
27
28
mesh = trimesh.load('example.ply')
BVH = cubvh.cuBVH(mesh.vertices, mesh.faces) # build with numpy.ndarray/torch.Tensor

ashawkey's avatar
ashawkey committed
29
### query ray-mesh intersection
ashawkey's avatar
init  
ashawkey committed
30
rays_o, rays_d = get_ray(pose, intrinsics, H, W) # [N, 3], [N, 3], query with torch.Tensor (cuda)
ashawkey's avatar
ashawkey committed
31
intersections, face_id, depth = BVH.ray_trace(rays_o, rays_d) # [N, 3], [N,], [N,]
ashawkey's avatar
init  
ashawkey committed
32

ashawkey's avatar
ashawkey committed
33
### query unsigned distance
ashawkey's avatar
init  
ashawkey committed
34
points # [N, 3]
ashawkey's avatar
ashawkey committed
35
# uvw is the barycentric corrdinates of the closest point on the closest face (None if `return_uvw` is False).
ashawkey's avatar
init  
ashawkey committed
36
distances, face_id, uvw = BVH.unsigned_distance(points, return_uvw=True) # [N], [N], [N, 3]
ashawkey's avatar
ashawkey committed
37

ashawkey's avatar
ashawkey committed
38
### query signed distance (INNER is NEGATIVE!)
ashawkey's avatar
ashawkey committed
39
40
41
42
# for watertight meshes (default)
distances, face_id, uvw = BVH.signed_distance(points, return_uvw=True, mode='watertight') # [N], [N], [N, 3]
# for non-watertight meshes:
distances, face_id, uvw = BVH.signed_distance(points, return_uvw=True, mode='raystab') # [N], [N], [N, 3]
ashawkey's avatar
init  
ashawkey committed
43
44
45
```


ashawkey's avatar
ashawkey committed
46
47
48
49
50
51
52
53
54
55
Example for a mesh normal renderer by `ray_trace`:

```bash
python test/renderer.py # default, show a dodecahedron
python test/renderer.py --mesh example.ply # show any mesh file
```

https://user-images.githubusercontent.com/25863658/183238748-7ac82808-6cd3-4bb6-867a-9c22f8e3f7dd.mp4


ashawkey's avatar
init  
ashawkey committed
56
57
58
### Acknowledgement

* Credits to [Thomas Müller](https://tom94.net/)'s amazing [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn) and [instant-ngp](https://github.com/NVlabs/instant-ngp)!