-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpoint_cloud_renderer.py
More file actions
168 lines (146 loc) · 5.39 KB
/
Copy pathpoint_cloud_renderer.py
File metadata and controls
168 lines (146 loc) · 5.39 KB
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
import numpy as np
import sys
import os
from plyfile import PlyData
import mitsuba as mi
class XMLTemplates:
HEAD = """
<scene version="0.6.0">
<integrator type="path">
<integer name="maxDepth" value="-1"/>
</integrator>
<sensor type="perspective">
<float name="farClip" value="100"/>
<float name="nearClip" value="0.1"/>
<transform name="toWorld">
<lookat origin="3,3,3" target="0,0,0" up="0,0,1"/>
</transform>
<float name="fov" value="25"/>
<sampler type="independent">
<integer name="sampleCount" value="256"/>
</sampler>
<film type="hdrfilm">
<integer name="width" value="1920"/>
<integer name="height" value="1080"/>
<rfilter type="gaussian"/>
</film>
</sensor>
<bsdf type="roughplastic" id="surfaceMaterial">
<string name="distribution" value="ggx"/>
<float name="alpha" value="0.05"/>
<float name="intIOR" value="1.46"/>
<rgb name="diffuseReflectance" value="1,1,1"/> <!-- default 0.5 -->
</bsdf>
"""
BALL_SEGMENT = """
<shape type="sphere">
<float name="radius" value="0.015"/>
<transform name="toWorld">
<translate x="{}" y="{}" z="{}"/>
</transform>
<bsdf type="diffuse">
<rgb name="reflectance" value="{},{},{}"/>
</bsdf>
</shape>
"""
TAIL = """
<shape type="rectangle">
<ref name="bsdf" id="surfaceMaterial"/>
<transform name="toWorld">
<scale x="10" y="10" z="1"/>
<translate x="0" y="0" z="-0.5"/>
</transform>
</shape>
<shape type="rectangle">
<transform name="toWorld">
<scale x="10" y="10" z="1"/>
<lookat origin="-4,4,20" target="0,0,0" up="0,0,1"/>
</transform>
<emitter type="area">
<rgb name="radiance" value="6,6,6"/>
</emitter>
</shape>
</scene>
"""
class PointCloudRenderer:
POINTS_PER_OBJECT = 2048
XML_HEAD = XMLTemplates.HEAD
XML_BALL_SEGMENT = XMLTemplates.BALL_SEGMENT
XML_TAIL = XMLTemplates.TAIL
def __init__(self, file_path):
self.file_path = file_path
self.folder, full_filename = os.path.split(file_path)
self.folder = self.folder or '.'
self.filename, _ = os.path.splitext(full_filename)
@staticmethod
def compute_color(x, y, z):
vec = np.clip(np.array([x, y, z]), 0.001, 1.0)
vec /= np.linalg.norm(vec)
return vec
@staticmethod
def standardize_point_cloud(pcl, points_per_object=POINTS_PER_OBJECT):
pt_indices = np.random.choice(pcl.shape[0], points_per_object, replace=False)
pcl = pcl[pt_indices]
center = np.mean(pcl, axis=0)
scale = np.amax(pcl - np.amin(pcl, axis=0))
return ((pcl - center) / scale).astype(np.float32)
def load_point_cloud(self):
file_extension = os.path.splitext(self.file_path)[1]
if file_extension == '.npy':
return np.load(self.file_path, allow_pickle=True)
elif file_extension == '.npz':
return np.load(self.file_path)['pred']
elif file_extension == '.ply':
ply_data = PlyData.read(self.file_path)
return np.column_stack(ply_data['vertex'][t] for t in ('x', 'y', 'z'))
else:
raise ValueError('Unsupported file format.')
def generate_xml_content(self, pcl):
xml_segments = [self.XML_HEAD]
for point in pcl:
color = self.compute_color(
point[0] + 0.5, point[1] + 0.5, point[2] + 0.5 - 0.0125)
xml_segments.append(self.XML_BALL_SEGMENT.format(
point[0], point[1], point[2], *color))
xml_segments.append(self.XML_TAIL)
return ''.join(xml_segments)
@staticmethod
def save_xml_content_to_file(output_file_path, xml_content):
xml_file_path = f'{output_file_path}.xml'
with open(xml_file_path, 'w') as f:
f.write(xml_content)
return xml_file_path
@staticmethod
def render_scene(xml_file_path):
mi.set_variant('scalar_rgb')
scene = mi.load_file(xml_file_path)
img = mi.render(scene)
return img
@staticmethod
def save_scene(output_file_path, rendered_scene):
mi.util.write_bitmap(f'{output_file_path}.png', rendered_scene)
def process(self):
pcl_data = self.load_point_cloud()
if len(pcl_data.shape) < 3:
pcl_data = pcl_data[np.newaxis, :, :]
for index, pcl in enumerate(pcl_data):
pcl = self.standardize_point_cloud(pcl)
pcl = pcl[:, [2, 0, 1]]
pcl[:, 0] *= -1
pcl[:, 2] += 0.0125
output_filename = f'{self.filename}_{index:02d}'
output_file_path = f'{self.folder}/{output_filename}'
print(f'Processing {output_filename}...')
xml_content = self.generate_xml_content(pcl)
xml_file_path = self.save_xml_content_to_file(output_file_path, xml_content)
rendered_scene = self.render_scene(xml_file_path)
self.save_scene(output_file_path, rendered_scene)
print(f'Finished processing {output_filename}.')
def main(argv):
if len(argv) < 2:
print('Filename not provided as argument.')
return
renderer = PointCloudRenderer(argv[1])
renderer.process()
if __name__ == '__main__':
main(sys.argv)