1
1
# pylint: disable=invalid-name, missing-docstring, line-too-long, no-member
2
2
3
3
from collections import deque
4
- from typing import Dict , Set
4
+ from typing import Dict , Set , Tuple
5
5
6
6
import numpy as np
7
7
from cloudvolume import CloudVolume
@@ -40,7 +40,7 @@ def _get_hierarchy(cg: ChunkedGraph, node_id: NODE_ID) -> Dict:
40
40
41
41
def _get_skipped_and_missing_leaf_nodes (
42
42
node_children : Dict , mesh_fragments : Dict
43
- ) -> Set :
43
+ ) -> Tuple [ Set , Set ] :
44
44
"""
45
45
Returns nodes with only one child and leaves (l2ids).
46
46
Nodes with one child do not have a mesh fragment, because it would be identical to child fragment.
@@ -56,6 +56,26 @@ def _get_skipped_and_missing_leaf_nodes(
56
56
return skipped , leaves
57
57
58
58
59
+ def _get_node_coords_and_layers_map (
60
+ cg : ChunkedGraph , node_children : Dict
61
+ ) -> Tuple [Dict , Dict ]:
62
+ node_ids = np .fromiter (node_children .keys (), dtype = NODE_ID )
63
+ node_coords = {}
64
+ node_layers = cg .get_chunk_layers (node_ids )
65
+ for layer in set (node_layers ):
66
+ layer_mask = node_layers == layer
67
+ coords = cg .get_chunk_coordinates_multiple (node_ids [layer_mask ])
68
+ _node_coords = dict (zip (node_ids [layer_mask ], coords ))
69
+ node_coords .update (_node_coords )
70
+ return node_coords , dict (zip (node_ids , node_layers ))
71
+
72
+
73
+ def _normalize_coordinates (coords , layer , bfs_depth , max_layer ):
74
+ node_depth = max_layer - layer
75
+ depth_diff = node_depth - bfs_depth
76
+ return coords // 2 ** depth_diff
77
+
78
+
59
79
def build_octree (
60
80
cg : ChunkedGraph , node_id : NODE_ID , node_children : Dict , mesh_fragments : Dict
61
81
):
@@ -71,6 +91,7 @@ def build_octree(
71
91
requested/rendered.
72
92
"""
73
93
node_ids = np .fromiter (mesh_fragments .keys (), dtype = NODE_ID )
94
+ node_coords_d , node_layers_d = _get_node_coords_and_layers_map (cg , node_children )
74
95
skipped , leaves = _get_skipped_and_missing_leaf_nodes (node_children , mesh_fragments )
75
96
76
97
OCTREE_NODE_SIZE = 5
@@ -88,13 +109,24 @@ def build_octree(
88
109
89
110
while len (que ) > 0 :
90
111
row_counter -= 1
91
- current_node , current_depth = que .popleft ()
112
+ current_node , depth = que .popleft ()
92
113
children = node_children [current_node ]
93
-
114
+ node_layer = node_layers_d [current_node ]
115
+ node_coords = node_coords_d [current_node ]
116
+
117
+ # node_coords = _normalize_coordinates(
118
+ # coords=node_coords,
119
+ # layer=node_layer,
120
+ # bfs_depth=depth,
121
+ # max_layer=cg.meta.layer_count,
122
+ # )
123
+
124
+ x , y , z = node_coords
125
+ # x, y, z = node_coords * np.array(cg.meta.graph_config.CHUNK_SIZE, dtype=int)
94
126
offset = OCTREE_NODE_SIZE * row_counter
95
- octree [offset + 0 ] = 1.25 ** current_depth * cg . meta . graph_config . CHUNK_SIZE [ 0 ]
96
- octree [offset + 1 ] = 1.25 ** current_depth * cg . meta . graph_config . CHUNK_SIZE [ 1 ]
97
- octree [offset + 2 ] = 1.25 ** current_depth * cg . meta . graph_config . CHUNK_SIZE [ 2 ]
127
+ octree [offset + 0 ] = x
128
+ octree [offset + 1 ] = y
129
+ octree [offset + 2 ] = z
98
130
99
131
rows_used += children .size
100
132
start = ROW_TOTAL - rows_used
@@ -104,7 +136,6 @@ def build_octree(
104
136
octree [offset + 4 ] = end_empty
105
137
106
138
octree_node_ids [row_counter ] = current_node
107
-
108
139
try :
109
140
if children .size == 1 :
110
141
# map to child fragment
@@ -116,7 +147,7 @@ def build_octree(
116
147
octree [offset + 4 ] |= 1 << 31
117
148
118
149
for child in children :
119
- que .append ((child , current_depth + 1 ))
150
+ que .append ((child , depth + 1 ))
120
151
return octree , octree_node_ids , octree_fragments
121
152
122
153
@@ -139,16 +170,13 @@ def get_manifest(cg: ChunkedGraph, node_id: NODE_ID) -> Dict:
139
170
fragments_d .update (_fragments_d )
140
171
141
172
octree , node_ids , fragments = build_octree (cg , node_id , node_children , fragments_d )
142
-
143
173
max_layer = min (cg .get_chunk_layer (node_id ) + 1 , cg .meta .layer_count )
144
- lods = 4 ** np .arange (max_layer - 2 , dtype = np .dtype ("<f4" ))
145
- fragments = normalize_fragments (fragments )
146
174
147
175
response = {
148
176
"chunkShape" : np .array (cg .meta .graph_config .CHUNK_SIZE , dtype = np .dtype ("<f4" )),
149
177
"chunkGridSpatialOrigin" : np .array ([0 , 0 , 0 ], dtype = np .dtype ("<f4" )),
150
- "lodScales" : lods ,
151
- "fragments" : fragments ,
178
+ "lodScales" : 2 ** np . arange ( max_layer , dtype = np . dtype ( "<f4" )) * 1 ,
179
+ "fragments" : normalize_fragments ( fragments ) ,
152
180
"octree" : octree ,
153
181
}
154
182
return node_ids , response
0 commit comments