Skip to content

Commit c262106

Browse files
committed
Change exclusive scan code to use thrust.
1 parent e8381fb commit c262106

File tree

10 files changed

+391
-608
lines changed

10 files changed

+391
-608
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@
88
"editor.codeActionsOnSave": {
99
"source.organizeImports": "always",
1010
}
11+
},
12+
"[c][cxx][cuda]": {
13+
"editor.formatOnSave": true,
14+
"editor.formatOnPaste": false,
15+
"editor.formatOnType": false,
16+
"editor.defaultFormatter": "xaver.clang-format"
1117
}
1218
}

README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,45 @@ pip install git+https://github.com/tatsy/torchmcubes.git
2525
See [mcubes.py](./mcubes.py) for more details.
2626

2727
```python
28+
import time
2829
import numpy as np
2930

3031
import torch
3132
from torchmcubes import marching_cubes, grid_interp
3233

3334
# Grid data
3435
N = 128
35-
x, y, z = np.mgrid[:N, :N, :N]
36-
x = (x / N).astype('float32')
37-
y = (y / N).astype('float32')
38-
z = (z / N).astype('float32')
36+
xs = np.linspace(-1.0, 1.0, N, endpoint=True, dtype="float32")
37+
ys = np.linspace(-1.0, 1.0, N, endpoint=True, dtype="float32")
38+
zs = np.linspace(-1.0, 1.0, N, endpoint=True, dtype="float32")
39+
zs, ys, xs = np.meshgrid(zs, ys, xs)
3940

4041
# Implicit function (metaball)
41-
f0 = (x - 0.35) ** 2 + (y - 0.35) ** 2 + (z - 0.35) ** 2
42-
f1 = (x - 0.65) ** 2 + (y - 0.65) ** 2 + (z - 0.65) ** 2
43-
u = 1.0 / f0 + 1.0 / f1
44-
rgb = np.stack((x, y, z), axis=-1)
45-
rgb = np.transpose(rgb, axes=(3, 2, 1, 0)).copy()
42+
f0 = (xs - 0.35)**2 + (ys - 0.35)**2 + (zs - 0.35)**2
43+
f1 = (xs + 0.35)**2 + (ys + 0.35)**2 + (zs + 0.35)**2
44+
u = 4.0 / (f0 + 1.0e-6) + 4.0 / (f1 + 1.0e-6)
4645

47-
# Test
48-
u = torch.from_numpy(u).cuda()
49-
rgb = torch.from_numpy(rgb).cuda()
50-
verts, faces = marching_cubes(u, 15.0)
51-
colrs = grid_interp(rgb, verts)
52-
53-
verts = verts.cpu().numpy()
54-
faces = faces.cpu().numpy()
55-
colrs = colrs.cpu().numpy()
46+
rgb = np.stack((xs, ys, zs), axis=-1) * 0.5 + 0.5
47+
rgb = np.transpose(rgb, axes=(3, 2, 1, 0))
48+
rgb = np.ascontiguousarray(rgb)
5649

57-
# Use Open3D for visualization (optional)
58-
import open3d as o3d
50+
# Test
51+
u = torch.from_numpy(u)
52+
rgb = torch.from_numpy(rgb)
53+
u = u.cuda()
54+
rgb = rgb.cuda()
5955

60-
mesh = o3d.geometry.TriangleMesh()
61-
mesh.vertices = o3d.utility.Vector3dVector(verts)
62-
mesh.triangles = o3d.utility.Vector3iVector(faces)
63-
mesh.vertex_colors = o3d.utility.Vector3dVector(colrs)
64-
wire = o3d.geometry.LineSet.create_from_triangle_mesh(mesh)
65-
o3d.visualization.draw_geometries([mesh, wire], window_name='Marching cubes (CUDA)')
56+
t_start = time.time()
57+
verts, faces = marching_cubes(u, 15.0)
58+
colors = grid_interp(rgb, verts)
59+
t_end = time.time()
60+
print(f"verts: {verts.size(0)}, faces: {faces.size(0)}, time: {t_end - t_start:.2f}s")
61+
62+
verts = verts.detach().cpu().numpy()
63+
faces = faces.detach().cpu().numpy()
64+
colors = colors.detach().cpu().numpy()
65+
verts = (verts / (N - 1)) * 2.0 - 1.0 # Get back to the original space
66+
visualize(verts, faces, colors)
6667
```
6768

6869
## Screen shot

cxx/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ if (CMAKE_CUDA_COMPILER)
1111
helper_math.h
1212
macros.h
1313
mcubes_cuda.cu
14-
grid_interp_cuda.cu
15-
pscan.h
16-
pscan.cu)
14+
grid_interp_cuda.cu)
1715
endif()
1816

1917
target_link_libraries(${BUILD_TARGET} PRIVATE

cxx/helper_math.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,6 @@
2828
typedef unsigned int uint;
2929
typedef unsigned short ushort;
3030

31-
#ifndef __CUDACC__
32-
#include <math.h>
33-
34-
////////////////////////////////////////////////////////////////////////////////
35-
// host implementations of CUDA functions
36-
////////////////////////////////////////////////////////////////////////////////
37-
38-
inline float fminf(float a, float b) {
39-
return a < b ? a : b;
40-
}
41-
42-
inline float fmaxf(float a, float b) {
43-
return a > b ? a : b;
44-
}
45-
46-
inline int max(int a, int b) {
47-
return a > b ? a : b;
48-
}
49-
50-
inline int min(int a, int b) {
51-
return a < b ? a : b;
52-
}
53-
54-
inline float rsqrtf(float x) {
55-
return 1.0f / sqrtf(x);
56-
}
57-
#endif
58-
5931
////////////////////////////////////////////////////////////////////////////////
6032
// constructors
6133
////////////////////////////////////////////////////////////////////////////////

cxx/mcubes_cpu.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ XYZ VertexInterp(float isolevel, XYZ p1, XYZ p2, float valp1, float valp2) {
6969
float mu;
7070
XYZ p;
7171

72-
if (std::abs(isolevel - valp1) < 0.00001) return (p1);
73-
74-
if (std::abs(isolevel - valp2) < 0.00001) return (p2);
75-
76-
if (std::abs(valp1 - valp2) < 0.00001) return (p1);
72+
if (std::abs(isolevel - valp1) < 1.0e-4f) return p1;
73+
if (std::abs(isolevel - valp2) < 1.0e-4f) return p2;
74+
if (std::abs(valp1 - valp2) < 1.0e-4f) return p1;
7775

7876
mu = (isolevel - valp1) / (valp2 - valp1);
7977
p.x = p1.x + mu * (p2.x - p1.x);

cxx/mcubes_cuda.cu

Lines changed: 322 additions & 353 deletions
Large diffs are not rendered by default.

cxx/pscan.cu

Lines changed: 0 additions & 171 deletions
This file was deleted.

cxx/pscan.h

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)