File size: 2,317 Bytes
710e818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import numpy as np
import taichi as ti

ti.init()


real = ti.f32


def get_sdf_spatial_grad():
    sdf_sv_fn = "/data/xueyi/diffsim/NeuS/init_box_mesh.npy"
    if not os.path.exists(sdf_sv_fn):
        sdf_sv_fn = "/home/xueyi/diffsim/NeuS/init_box_mesh.npy"
    sdf_sv = np.load(sdf_sv_fn, allow_pickle=True)
    scene_sdf = sdf_sv
    print(sdf_sv.shape)
    
    res = sdf_sv.shape[0]
    
    sdf_grad = np.zeros((res, res, res, 3), dtype=np.float32)
    
    
    # scene_sdf = ti.field(dtype=real, shape=(self.sdf_res, self.sdf_res, self.sdf_res))
    for i_x in range(res):
        print(f"Start processing i_x : {i_x}")
        for i_y in range(res):
            print(f"Start processing i_x : {i_x}, i_y : {i_y}")
            for i_z in range(res):
                cur_grad = np.zeros((3,), dtype=np.float32)
                
                if i_x > 0 and i_x < res - 1:
                    cur_grad[0] = (scene_sdf[i_x + 1, i_y, i_z] - scene_sdf[i_x - 1, i_y, i_z]) / 2.0
                elif i_x == 0:
                    cur_grad[0] = scene_sdf[i_x + 1, i_y, i_z] - scene_sdf[i_x, i_y, i_z]
                elif i_x == res - 1:
                    cur_grad[0] = scene_sdf[i_x, i_y, i_z] - scene_sdf[i_x - 1, i_y, i_z]
                
                if i_y > 0 and i_y < res - 1:
                    cur_grad[1] = (scene_sdf[i_x, i_y + 1, i_z] - scene_sdf[i_x, i_y - 1, i_z]) / 2.0
                elif i_y == 0:
                    cur_grad[1] = scene_sdf[i_x, i_y + 1, i_z] - scene_sdf[i_x, i_y, i_z]
                elif i_y == res - 1:
                    cur_grad[1] = scene_sdf[i_x, i_y, i_z] - scene_sdf[i_x, i_y - 1, i_z]
                
                if i_z > 0 and i_z < res - 1:
                    cur_grad[2] = (scene_sdf[i_x, i_y, i_z + 1] - scene_sdf[i_x, i_y, i_z - 1]) / 2.0
                elif i_z == 0:
                    cur_grad[2] = scene_sdf[i_x, i_y, i_z + 1] - scene_sdf[i_x, i_y, i_z]
                elif i_z == res - 1:
                    cur_grad[2] = scene_sdf[i_x, i_y, i_z] - scene_sdf[i_x, i_y, i_z - 1]
                
                sdf_grad[i_x, i_y, i_z, :] = cur_grad[:]
    
    sdf_grad_sv_fn = "/home/xueyi/diffsim/NeuS/init_box_mesh_sdf_grad.npy"
    np.save(sdf_grad_sv_fn, sdf_grad)

if __name__ == '__main__':
    get_sdf_spatial_grad() #