-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathriegl2raycloud.py
More file actions
282 lines (230 loc) · 9.28 KB
/
riegl2raycloud.py
File metadata and controls
282 lines (230 loc) · 9.28 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
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
280
281
282
import os
import argparse
import glob
import csv
import json
import time
import matplotlib.pyplot as plt
import pdal
import open3d as o3d
import numpy as np
from boundingrectangle import boundingrectangle
from tile import tile_from_corner_points
OUT_DIR = "out"
def read_csv(file):
pos_dict = {}
with open(file) as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
pos_dict[row[0]] = [float(i) for i in row[1:]]
return pos_dict
def read_dat(folder):
pos_dict = {}
for datfile in glob.glob(os.path.join(folder, "*.DAT")):
scanpos = os.path.splitext(os.path.basename(datfile))[0]
with open(datfile) as f:
mat = []
for line in f.readlines():
mat_row = [float(el) for el in line.split(' ')]
mat.append(mat_row)
pos_dict[scanpos] = np.array(mat)
return pos_dict
def plot_dat_positions(positions, visualisation: bool = False, out_dir = None):
plt.scatter([scanposfromdat(el)[0] for el in positions.values()], [scanposfromdat(el)[1] for el in positions.values()])
for txt in positions:
plt.annotate(txt, (scanposfromdat(positions[txt])[0], scanposfromdat(positions[txt])[1] + 1))
if out_dir:
if not os.path.exists(out_dir):
os.makedirs(out_dir)
plt.savefig(os.path.join(out_dir, "PlotScanpositions.png"))
if visualisation:
plt.show()
return
def get_pipeline(rxp_file):
cmds = []
read = {"type":"readers.rxp",
"filename": rxp_file,
"sync_to_pps": "false",
"reflectance_as_intensity": "false"}
cmds.append(read)
# option to add PDAL filters here
dmp = json.dumps(cmds)
pipeline = pdal.Pipeline(dmp)
return pipeline
def transform_rxp(rxp_array, matrix):
"""
Returns numpy array with xyz's of rxp file transformed using .dat matrix
Parameters
----------
rxp_array
array of tuples containing rxp data points
matrix
4x4 transformation matrix
"""
xyz = rxp_array[['X', 'Y', 'Z']]
# need to do this to convert tuples to arrays, there might be a faster way with .view or something
xyz_np = np.array(xyz.tolist())
# append extra row of ones to make transform work
extra_dim = np.ones((xyz_np.shape[0],1))
xyz_np = np.hstack((xyz_np, extra_dim))
# transpose
xyz_np = np.transpose(xyz_np)
#perform transformation
xyz_np = np.matmul(matrix, xyz_np)
#retranspose to get nx4 array again
xyz_np = np.transpose(xyz_np)
# remove final column of 1's
xyz_np = xyz_np[:,:-1]
return xyz_np
def read_rxps(project, pos_dict):
"""
Reads all rxp files in a project folder
Returns generator with scanpos, points
Parameters
----------
project
.RISCAN folder
pos_dict
dictionary with .DAT matrices
"""
for scanpos in sorted(os.listdir(os.path.join(project, 'SCANS'))): # TODO: temp slice for laptop memory reasons
if scanpos not in pos_dict:
print(f"Can't read rxp {scanpos} as not present in pos_dict, make sure .DAT files are generated before running (skipping)")
continue
rxp = glob.glob(os.path.join(project, 'SCANS', scanpos, '**/*_*.rxp'))
# remove all the residual files
rxp = [el for el in rxp if not 'residual' in el]
if len(rxp) != 1:
print(f"Error for scanpos {scanpos}: rxp file not found (list = {rxp})")
continue
# get and execute pdal pipeline
pipeline = get_pipeline(rxp[0])
pipeline.execute()
# transform using DAT files into 1 coordinate system
points = transform_rxp(pipeline.arrays[0], pos_dict[scanpos])
yield scanpos, points
del points # don't know if this actually does something but anyways
def scanposfromdat(matrix):
return matrix[:-1,-1]
def appendray(points, scanpos, time) -> o3d.t.geometry.PointCloud:
"""
Returns o3d Pointcloud with scanposition coordinates saved in normal field and appended time field
Parameters
----------
points
np array of points
scanpos
np array of xyz of scanpos
time
time value of scan pos
"""
device = o3d.core.Device("CPU:0")
dtype_f32 = o3d.core.float32
dtype_f64 = o3d.core.float64
pcd = o3d.t.geometry.PointCloud(device)
pcd.point.positions = o3d.core.Tensor(points, dtype_f32, device)
n_points = len(points)
# append scanpos
scanpos = np.repeat([scanpos], n_points, axis=0)
pcd.point.normals = o3d.core.Tensor( scanpos, dtype_f32, device)
# append time
time = np.repeat([[time]], n_points, axis=0)
pcd.point.time = o3d.core.Tensor( time, dtype_f64, device)
return pcd
def pc2rc(pos_dict, out_dir, args):
"""
Converts pointclouds into rayclouds, and merges into one big .ply conforming to raycloudtools formatting
Also crops the pointcloud to bounding rectangle with buffer
Parameters
----------
pos_dict
dictionary with scanner positions: {"ScanPosXX" -> [x, y, z]}
out_dir
path to output directory
args
"""
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# get bounding rectangle of scanpositions
positions = [scanposfromdat(el)[:2] for el in pos_dict.values()]
if len(positions) < 4:
print("Less then 4 positions found, not cropping with bounding rectangle")
else:
area, bbox_xy_corners = boundingrectangle(positions, buffer=args.edgebuffer, out_dir=out_dir)
# first create 03d pointcloud, then get bounding box from this pointcloud
# no direct way of getting bbox in current o3d
# also can't use inf because o3d complains, so use LARGE_Z (update when trees get larger then 1 million metres!)
LARGE_Z = 1000000
top_corners = np.hstack((bbox_xy_corners, np.asarray([[LARGE_Z]]*4)))
bot_corners = np.hstack((bbox_xy_corners, np.asarray([[-LARGE_Z]]*4)))
corners_pc = o3d.t.geometry.PointCloud(o3d.core.Tensor(np.vstack((top_corners, bot_corners))))
crop_bbox = corners_pc.get_oriented_bounding_box()
merged = None
# call generator function for rxps
for scanpos, points in read_rxps(args.project, pos_dict):
print(f"Processing position {scanpos}")
pcd = appendray(points, scanposfromdat(pos_dict[scanpos]), 1)
# crop using bbox
print("Cropping")
pcd = pcd.crop(crop_bbox)
# downsampling is done before merging, as otherwise normals (aka rays) are averaged in voxel
if (args.resolution):
print("Downsampling")
pcd = pcd.voxel_down_sample(args.resolution)
print("Merging")
# ugly code but for some reason o3d errors when appending to empty pointcloud
if merged is None:
# copy constructor
merged = o3d.t.geometry.PointCloud(pcd)
else:
merged = merged.append(pcd)
if(args.debug):
# for debugging: also write single point clouds
pos_dir = out_dir + "/pos/"
if not os.path.exists(pos_dir):
os.makedirs(pos_dir)
filename = pos_dir+scanpos+"_raycloud.ply"
o3d.t.io.write_point_cloud( filename, pcd, write_ascii=False, compressed=False, print_progress=False)
del pcd, points
print("Tiling merged point cloud")
# Tile based on corners forming rectangle
tile_out_dir = os.path.join(out_dir, "tiled")
if not os.path.exists(tile_out_dir):
os.makedirs(tile_out_dir)
tiles = tile_from_corner_points(bbox_xy_corners, merged, size=args.tilesize, buffer=args.tilebuffer, exact_size=args.exact_tiles, visualization=False, out_dir=tile_out_dir)
for i, tile in enumerate(tiles):
o3d.t.io.write_point_cloud(os.path.join(tile_out_dir,"Tile"+str(i)+".ply"), tile, write_ascii=False, compressed=False, print_progress=False)
#write merged pointcloud
print("Writing merged pointcloud")
o3d.t.io.write_point_cloud(os.path.join(out_dir, "merged_raycloud.ply"), merged, write_ascii=False, compressed=False, print_progress=False)
return
# @profile
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--project", type=str, required=True)
parser.add_argument("--debug", action='store_true')
parser.add_argument("-r", "--resolution", type=float)
# tile related
parser.add_argument("-b", "--edgebuffer", type=int, default=5)
parser.add_argument("-t", "--tilebuffer", type=int, default=2)
parser.add_argument("-s", "--tilesize", type=int, default=20)
parser.add_argument("--exact_tiles", action='store_true')
print("")
args = parser.parse_args()
if not os.path.exists(args.project):
print("couldnt find folder")
os._exit(1)
pos_dict = read_dat(args.project)
# get output folder from project name
if (args.project.endswith("/")):
args.project = args.project[:len(args.project) -1]
out_dir = os.path.join("out", os.path.splitext(os.path.basename(args.project))[0])
print("Converting raycloud")
plot_dat_positions(pos_dict, out_dir=out_dir)
t = time.process_time()
pc2rc(pos_dict, out_dir, args)
t2 = time.process_time()
print(f"Converted pointclouds to rayclouds in {(t2 - t):.2f} seconds")
return
if __name__ == "__main__":
main()