11# pylint: disable=invalid-name, missing-docstring, line-too-long, no-member
22
33from collections import deque
4- from typing import Dict , Set
4+ from typing import Dict , Set , Tuple
55
66import numpy as np
77from cloudvolume import CloudVolume
@@ -40,7 +40,7 @@ def _get_hierarchy(cg: ChunkedGraph, node_id: NODE_ID) -> Dict:
4040
4141def _get_skipped_and_missing_leaf_nodes (
4242 node_children : Dict , mesh_fragments : Dict
43- ) -> Set :
43+ ) -> Tuple [ Set , Set ] :
4444 """
4545 Returns nodes with only one child and leaves (l2ids).
4646 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(
5656 return skipped , leaves
5757
5858
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+
5979def build_octree (
6080 cg : ChunkedGraph , node_id : NODE_ID , node_children : Dict , mesh_fragments : Dict
6181):
@@ -71,6 +91,7 @@ def build_octree(
7191 requested/rendered.
7292 """
7393 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 )
7495 skipped , leaves = _get_skipped_and_missing_leaf_nodes (node_children , mesh_fragments )
7596
7697 OCTREE_NODE_SIZE = 5
@@ -88,13 +109,24 @@ def build_octree(
88109
89110 while len (que ) > 0 :
90111 row_counter -= 1
91- current_node , current_depth = que .popleft ()
112+ current_node , depth = que .popleft ()
92113 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)
94126 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
98130
99131 rows_used += children .size
100132 start = ROW_TOTAL - rows_used
@@ -104,7 +136,6 @@ def build_octree(
104136 octree [offset + 4 ] = end_empty
105137
106138 octree_node_ids [row_counter ] = current_node
107-
108139 try :
109140 if children .size == 1 :
110141 # map to child fragment
@@ -116,7 +147,7 @@ def build_octree(
116147 octree [offset + 4 ] |= 1 << 31
117148
118149 for child in children :
119- que .append ((child , current_depth + 1 ))
150+ que .append ((child , depth + 1 ))
120151 return octree , octree_node_ids , octree_fragments
121152
122153
@@ -139,16 +170,13 @@ def get_manifest(cg: ChunkedGraph, node_id: NODE_ID) -> Dict:
139170 fragments_d .update (_fragments_d )
140171
141172 octree , node_ids , fragments = build_octree (cg , node_id , node_children , fragments_d )
142-
143173 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 )
146174
147175 response = {
148176 "chunkShape" : np .array (cg .meta .graph_config .CHUNK_SIZE , dtype = np .dtype ("<f4" )),
149177 "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 ) ,
152180 "octree" : octree ,
153181 }
154182 return node_ids , response
0 commit comments