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
149
150
151
152
|
import numpy as np
from itertools import chain
def compress(data, selectors):
return (d for d, s in zip(data, selectors) if s)
class Leaf(object):
def __init__(self, mesh, mesh_idx, zvalue=None):
self.zvalue = zvalue
self.mesh_idx = mesh_idx
self.lower_bound = np.array([np.min(mesh[:,:,0]), np.min(mesh[:,:,1]), np.min(mesh[:,:,2])])
self.upper_bound = np.array([np.max(mesh[:,:,0]), np.max(mesh[:,:,1]), np.max(mesh[:,:,2])])
self.size = mesh.shape[0]
class Node(object):
def __init__(self, children, zvalue=None):
self.zvalue = zvalue
lower_pts = np.array([child.lower_bound for child in children])
upper_pts = np.array([child.upper_bound for child in children])
self.lower_bound = np.array([np.min(lower_pts[:,0]), np.min(lower_pts[:,1]), np.min(lower_pts[:,2])])
self.upper_bound = np.array([np.max(upper_pts[:,0]), np.max(upper_pts[:,1]), np.max(upper_pts[:,2])])
self.size = len(children)
self.children = children
def interleave(arr):
if len(arr.shape) != 2 or arr.shape[1] != 3:
raise Exception('shape mismatch')
z = np.zeros(arr.shape[0], dtype=np.uint64)
for i in range(arr.dtype.itemsize*8):
z |= (arr[:,2] & 1 << i) << (2*i) | (arr[:,1] & 1 << i) << (2*i+1) | (arr[:,0] & 1 << i) << (2*i+2)
return z
def morton_order(mesh, bits=3):
lower_bound = np.array([np.min(mesh[:,:,0]), np.min(mesh[:,:,1]), np.min(mesh[:,:,2])])
upper_bound = np.array([np.max(mesh[:,:,0]), np.max(mesh[:,:,1]), np.max(mesh[:,:,2])])
max_value = 2**bits - 1
def quantize(x):
return np.uint64((x-lower_bound)*max_value/(upper_bound-lower_bound))
mean_positions = quantize(np.mean(mesh, axis=1))
return interleave(mean_positions)
class Solid(object):
def __init__(self, mesh, inside, outside):
if len(mesh.shape) != 3 or mesh.shape[1] != 3 or mesh.shape[2] != 3:
raise Exception('shape mismatch')
self.mesh = mesh
self.inside = inside
self.outside = outside
def __len__(self):
return self.mesh.shape[0]
class Geometry(object):
"""
Geometry object.
"""
def __init__(self):
self.solids = []
self.materials = []
def add_solid(self, solid):
self.solids.append(solid)
if solid.inside not in self.materials:
self.materials.append(solid.inside)
if solid.outside not in self.materials:
self.materials.append(solid.outside)
def build(self, bits=3):
self.mesh = np.concatenate([solid.mesh for solid in self.solids])
self.solid_index = np.concatenate([np.tile(self.solids.index(solid), len(solid)) for solid in self.solids])
self.inside_index = np.concatenate([np.tile(self.materials.index(solid.outside), len(solid)) for solid in self.solids])
self.outside_index = np.concatenate([np.tile(self.materials.index(solid.outside), len(solid)) for solid in self.solids])
zvalues = morton_order(self.mesh, bits)
order = np.array(zip(*sorted(zip(zvalues, range(zvalues.size))))[-1])
zvalues = zvalues[order]
self.mesh = self.mesh[order]
self.solid_index = self.solid_index[order]
self.inside_index = self.inside_index[order]
self.outside_index = self.outside_index[order]
leafs = []
for z in sorted(set(zvalues)):
mask = (zvalues == z)
leafs.append(Leaf(self.mesh[mask], np.where(mask)[0][0], z))
layers = []
layers.append(leafs)
while True:
zvalues = np.array([node.zvalue for node in layers[-1]])
bit_shifted_zvalues = zvalues >> 1
nodes = []
for z in sorted(set(bit_shifted_zvalues)):
mask = (bit_shifted_zvalues == z)
nodes.append(Node(list(compress(layers[-1], mask)), z))
layers.append(nodes)
if len(nodes) == 1:
break
layers.reverse()
nodes = []
for layer in layers:
for node in layer:
nodes.append(node)
self.lower_bound = np.empty((len(nodes),3))
self.upper_bound = np.empty((len(nodes),3))
self.child_map = np.empty(len(nodes))
self.child_len = np.empty(len(nodes))
self.first_leaf = -1
for i, node in enumerate(nodes):
self.lower_bound[i] = node.lower_bound
self.upper_bound[i] = node.upper_bound
self.child_len[i] = node.size
if isinstance(node, Node):
for j, child in enumerate(nodes):
if child is node.children[0]:
self.child_map[i] = j
break
if isinstance(node, Leaf):
self.child_map[i] = node.mesh_idx
if self.first_leaf == -1:
self.first_leaf = i
|