-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_plotter.py
279 lines (204 loc) · 9.54 KB
/
tree_plotter.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import annotations
import drjit as dr
import mitsuba as mi
mi.set_variant('cuda_ad_rgb')
from src.common import *
from src.quadtree import QuadTreeNode
from src.kdtree import KDTreeNode
from src.file_name_manager import FileNameManager
import numpy as np
import matplotlib.pyplot as plt
class QuadTreePlotter:
def __init__( self, fileName: str ) -> None:
"""
QuadTree data represented as numpy array fashion.
Mainly use for offline data processing.
- fileName: file name of the QuadTree data to be loaded.
"""
# Load from file
treeDataNumpy = np.load(fileName)
# Init the QuaddTreeNode and load data into
self.quadTreeNode = QuadTreeNode()
self.quadTreeNode.loadFromFile( treeDataNumpy )
def getMaxDepth( self, rootIndex: int ) -> int:
"""
Get maximum depth of a QuadTree
-
"""
leafNodeIndex = self.quadTreeNode.getAllLeafNodeIndex( mi.UInt32( rootIndex ) )
leafNodeDepth = dr.gather( mi.UInt32, self.quadTreeNode.depth, leafNodeIndex )
maxDepth = np.max(leafNodeDepth.numpy())
return maxDepth
def sampleIrradiance( self, rootIndex: mi.UInt32, position: mi.Vector2f ) -> mi.Float:
"""
Sample irradiance from a given postion. 'rootIndex' and 'position' MUST have
the same size.
- rootIndex: index of tree root (0, 1, 2, ...).
- position: sampling position.
"""
# Start searching at root node index
nodeIndex = dr.gather( mi.UInt32, self.quadTreeNode.rootNodeIndex, rootIndex )
# Test if data is within the root node bbox
rootNodeBBox = self.quadTreeNode.getBBox( nodeIndex )
isInsideRoot = rootNodeBBox.contains( position )
active = mi.Bool( isInsideRoot )
# Traverse the tree to find the corresponding leaf node
loop = mi.Loop(name= 'QuadTreeDataPlotter sample irradiance', state= lambda: (active, nodeIndex) )
while loop(active):
# If at leaf node then stop, if not then continue
isLeafNode = dr.gather(mi.Bool, self.quadTreeNode.isLeaf, nodeIndex, active )
isNotLeafNode = ~isLeafNode
active &= isNotLeafNode
# Else, check which child node it belongs to and store the node index
child_1_idx = dr.gather(mi.UInt32, self.quadTreeNode.child_1_index, index= nodeIndex, active= active)
child_2_idx = dr.gather(mi.UInt32, self.quadTreeNode.child_2_index, index= nodeIndex, active= active)
child_3_idx = dr.gather(mi.UInt32, self.quadTreeNode.child_3_index, index= nodeIndex, active= active)
child_4_idx = dr.gather(mi.UInt32, self.quadTreeNode.child_4_index, index= nodeIndex, active= active)
child_1_bbox = self.quadTreeNode.getBBox( child_1_idx )
child_1_bbox_test = child_1_bbox.contains( position )
nodeIndex[child_1_bbox_test & active] = child_1_idx
child_2_bbox = self.quadTreeNode.getBBox( child_2_idx )
child_2_bbox_test = child_2_bbox.contains( position )
nodeIndex[child_2_bbox_test & active] = child_2_idx
child_3_bbox = self.quadTreeNode.getBBox( child_3_idx )
child_3_bbox_test = child_3_bbox.contains( position )
nodeIndex[child_3_bbox_test & active] = child_3_idx
child_4_bbox = self.quadTreeNode.getBBox( child_4_idx )
child_4_bbox_test = child_4_bbox.contains( position )
nodeIndex[child_4_bbox_test & active] = child_4_idx
# Get irradiance from the corresponding leaf node index
irradiance = dr.gather( mi.Float, self.quadTreeNode.irradiance, nodeIndex, isInsideRoot )
# Compute area of the node
nodeBBox = self.quadTreeNode.getBBox( nodeIndex )
nodeBBoxExtent = (nodeBBox.max - nodeBBox.min)
nodeArea = nodeBBoxExtent.x * nodeBBoxExtent.y
# Normalized irradiance by the array size
irradiance /= nodeArea
return irradiance
def plotQuadTree( self, rootIndex: int, title: str, saveFig: bool = False ) -> None:
"""
Plot a QuadTree as heat map.
- rootIndex: index of tree root (0, 1, 2, ...).
"""
# Generate sampling position in a grid fashion
depth = self.getMaxDepth( 0 )
depth = min( depth, 10 )
numCell = pow( 2, depth )
cellSize = 1 / numCell
cellCenterOffset = cellSize / 2
x = dr.arange( mi.Float, numCell ) / numCell
y = dr.arange( mi.Float, numCell ) / numCell
x += cellCenterOffset
y += cellCenterOffset
x, y = dr.meshgrid( x, y )
samplingPosition = mi.Vector2f( x, y )
N = dr.width( samplingPosition )
# Sample QuadTree irradiance
rootIndexArray = dr.full( mi.UInt32, rootIndex, N )
irradiance = self.sampleIrradiance( rootIndexArray, samplingPosition )
irradiance_numpy = irradiance.numpy().reshape(numCell, numCell)
# Plot heatmap
plt.figure()
im = plt.imshow( irradiance_numpy, cmap='jet', interpolation='nearest', extent= [0, 1, 0, 1], origin= 'lower')
plt.xlabel( 'Normalized Φ (Phi)' )
plt.ylabel( 'Normalized Cos(θ) (Theta)' )
plt.title( title )
plt.colorbar( im )
if saveFig:
# Title contains new line character so we join into one line
figFileName = title.splitlines()
figFileName = FileNameManager.PLOT_FOLDER_PATH + figFileName[0] + ', ' + figFileName[1] + '.png'
plt.savefig(fname= figFileName, dpi=300)
# samplingPos = mi.Vector2f( 0.25, 0.25 )
# rootIndexArray = dr.full( mi.UInt32, rootIndex, 1 )
# irradiance = self.sampleIrradiance( rootIndexArray, samplingPos )
# print(irradiance)
class KDTreePlotter:
def __init__(self, fileName: str) -> None:
# Load from file
treeDataNumpy = np.load(fileName)
# Init the QuaddTreeNode and load data into
self.kdTreeNode = KDTreeNode()
self.kdTreeNode.loadFromFile( treeDataNumpy )
# Init QuadTreePlotter
self.quadTreePlotter = QuadTreePlotter( fileName )
def getSceneBBox( self ) -> Tuple[ Vec3, Vec3 ]:
pass
def findLeafNode( self, position: mi.Vector3f ) -> Tuple[ mi.UInt32, mi.Bool ]:
"""
Find the corresponding leaf node index of the given position.
- position: query position.
Return:
- nodeIndex
- isValid:
"""
# Start traversing from root node
nodeIndex = dr.zeros( mi.UInt32, dr.width( position ) )
# Test if data is within the root node bbox
rootNodeBBox = self.kdTreeNode.getBBox( 0 )
isInsideRoot = rootNodeBBox.contains( position )
active = mi.Bool( isInsideRoot )
loop = mi.Loop( name = 'KDTreePlotter.findLeafNode', state= lambda: (active, nodeIndex) )
while loop( active ):
# If at leaf node then stop. Otherwise, continue
isLeafNode = dr.gather( mi.Bool, self.kdTreeNode.isLeaf, nodeIndex, active )
# Else, check which children node it belongs to and store the node index
isNotLeafNode = ~isLeafNode
active &= isNotLeafNode
child_left_idx = dr.gather( mi.UInt32, self.kdTreeNode.child_left_index, nodeIndex, active )
child_right_idx = dr.gather( mi.UInt32, self.kdTreeNode.child_right_index, nodeIndex, active )
child_left_bbox = self.kdTreeNode.getBBox( child_left_idx )
child_left_bbox_test = child_left_bbox.contains( position )
nodeIndex[ child_left_bbox_test & active ] = child_left_idx
child_right_bbox = self.kdTreeNode.getBBox( child_right_idx )
child_right_bbox_test = child_right_bbox.contains( position )
nodeIndex[ child_right_bbox_test & active ] = child_right_idx
return nodeIndex, isInsideRoot
def plotQuadTreeAtPosition( self, position: Vec3, title: str, saveFig: bool = False ) -> None:
# Traverse the KDTree to find the corresponding rootnode
position_mi = mi.Vector3f( position )
leafNodeIndex_mi, isValid_mi = self.findLeafNode( position_mi )
leafNodeIndex = leafNodeIndex_mi.numpy()[0]
isValid = isValid_mi.numpy()[0]
# If the query position is valid (withing tree bounding box) then plot the corresponding quadtree
if isValid:
# Get the corresponding root node index
quadTreeRootIndex = dr.gather( mi.UInt32, self.kdTreeNode.quadTreeRootIndex, leafNodeIndex )
# Plot the QuadTree
self.quadTreePlotter.plotQuadTree( quadTreeRootIndex.numpy()[0], title, saveFig )
class MultiIterationTreePlotter:
def __init__(self, sceneName: str, numIteration: int) -> None:
"""
- sceneName: the scene name of the file. Not the actual file path.
- numIteration: total number of iteration
"""
self.sceneName = sceneName
self.kdTreePlotters: List[ KDTreePlotter ] = []
# Initialize KDTreePlotter for refinement each iteration
for i in range( numIteration ):
fileName = FileNameManager.generateTreeDataFileName( i, withNpzEnding= True )
kdTreePlotter = KDTreePlotter( fileName )
self.kdTreePlotters.append( kdTreePlotter )
def plotQuadTreeAtPosition( self, position: Vec3, saveFig: bool = False ) -> None:
"""
Plot a QuadTree of every iteration of a given world position.
- position: world position. Will be use to traverse a KDTree to find a corresponding QuadTree.
"""
# Plot the refinement map at every iteration
for index, kdTreePlotter in enumerate( self.kdTreePlotters ):
title = self.sceneName + ' Irradiance Map\nworld_pos= [{0}, {1}, {2}], Refinement Iteration= {3}' \
.format( position[0], position[1], position[2], index )
kdTreePlotter.plotQuadTreeAtPosition( position, title, saveFig )
# Show plot
plt.show()
if __name__ == '__main__':
# sceneName = 'veach-bidir'
sceneName = 'torus'
# sceneName = 'cornell-box-empty'
iteration = 5
FileNameManager.setSceneName( sceneName )
multiIterationTreePlotter = MultiIterationTreePlotter( sceneName, iteration )
# multiIterationTreePlotter.plotQuadTreeAtPosition( [0, 0, 0], saveFig= True )
# multiIterationTreePlotter.plotQuadTreeAtPosition( [-2.38, 1.9, 0.4617], saveFig= True ) # veach-bidir behind egg
# multiIterationTreePlotter.plotQuadTreeAtPosition( [-1.749760, 0.157591, 3.000237], saveFig= True ) # veach-bidir: This position is in mid air
multiIterationTreePlotter.plotQuadTreeAtPosition( [-6.7, 1.5, -1.828], saveFig= True ) # torus, bright spot in the shadow