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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698 | class OccPyRIEGL:
"""
Voxel-based occlusion mapping from RIEGL terrestrial laser scanning data.
This class handles RIEGL-specific data workflows, including reading `.rdbx`, `.rxp`, and transformation files,
optionally modeling empty pulses using preview images, performing voxel-based ray tracing, and saving the output
grids. It provides preprocessing, collinearity checks, and output saving in `.npy` and `.ply` formats.
"""
def __init__(self, config_file):
"""
Initialize an OccPyRIEGL instance for RIEGL TLS occlusion mapping.
Parameters in config file:
Must include:
- 'proj_folder' : path to RIEGL project directory with `.rdbx` and `.rxp` files
- 'riscan_folder' : path to RiSCAN PRO scan directory with transformation files
- 'vox_dim' : voxel size in meters
- 'plot_dim': grid for occlusion mapping: [minX, minY, minZ, maxX, maxY, maxZ]
Optional parameters:
- 'out_dir' : output directory (default: ./output)
- 'buffer' : spatial buffer around point cloud
- 'output_voxels' : whether to export `.ply` voxel grids
- 'model_empty_pulses' : whether to model empty pulses
- 'verbose': set logging level
- 'debug': set logging level, run extra checks and output debug files
- 'auto_dim': not implemented yet
- 'buffer': not implemented yet
- 'exclude_scan_pattern': string pattern, to exclude scans containing this pattern (in name of rdbx and transform files)
Parameters
----------
config_file : str
Path to a JSON configuration file containing processing parameters.
"""
with open(config_file) as f:
config = json.load(f)
necessary_args = ["proj_folder", "riscan_folder", "vox_dim", "plot_dim"]
missing = []
for key in necessary_args:
if key not in config:
missing.append(key)
if len(missing) > 0:
raise ValueError(f"Missing necessary arguments in config file: {missing}")
optional_args = ["model_empty_pulses", "verbose", "debug", "output_voxels", "lower_threshold", "out_dir", "auto_dim", "buffer", "exclude_scan_pattern"]
print(f"INFO: optional arguments: {optional_args}")
self.riscan_folder = config["riscan_folder"]
self.proj_folder = config["proj_folder"]
self.vox_dim = config["vox_dim"]
self.model_empty_pulses = config.get("model_empty_pulses", False)
self.verbose = config.get("verbose", False)
self.debug = config.get("debug", False)
self.output_voxels = config.get("output_voxels", False)
self.lower_threshold = config.get("lower_threshold", 0)
self.exclude_scan_pattern = config.get("exclude_scan_pattern", None)
self.out_dir = config.get("out_dir", os.path.join(os.getcwd(), "output"))
if not os.path.exists(self.out_dir):
os.makedirs(self.out_dir, exist_ok=True)
# copy config file for future reference
with open(os.path.join(self.out_dir, "config.json"), "w") as to:
json.dump(config, to)
# -- config logging
if self.debug:
logging_level = logging.DEBUG
elif self.verbose:
logging_level = logging.INFO
else:
logging_level = logging.WARNING
self.logger = logging.getLogger('occpy_logger')
self.logger.setLevel(logging_level)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging_level)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
self.logger.addHandler(console_handler)
self.logger.info(f"config:")
self.logger.info(f"{config}")
# --- prepare input
self.prepare_input()
# --- init dimensions and raytracer
if "auto_dim" in config and config["auto_dim"] is True:
if "buffer" in config:
buffer = config["buffer"]
else:
buffer = [0,0,0,0]
# TODO: implement
self.plot_dim = self.determine_grid(buffer)
else:
if "plot_dim" not in config:
raise ValueError("plot_dim must be provided if auto_dim is not set to True")
self.plot_dim = config["plot_dim"]
# ensure extents are exactly divisible by vox_dim by extending max bounds if needed.
self.plot_dim, warnings = self.align_plot_dim_to_voxel_size(self.plot_dim, self.vox_dim)
for msg in warnings:
self.logger.warning(msg)
self.PlotDim = dict(minX=self.plot_dim["minX"],
maxX=self.plot_dim["maxX"],
minY=self.plot_dim["minY"],
maxY=self.plot_dim["maxY"],
minZ=self.plot_dim["minZ"],
maxZ=self.plot_dim["maxZ"])
self.RayTr = PyRaytracer()
# Define Grid
self.grid_dim = dict(nx=int((self.PlotDim['maxX'] - self.PlotDim['minX']) / self.vox_dim),
ny=int((self.PlotDim['maxY'] - self.PlotDim['minY']) / self.vox_dim),
nz=int((self.PlotDim['maxZ'] - self.PlotDim['minZ']) / self.vox_dim))
min_bound = np.array([self.PlotDim['minX'], self.PlotDim['minY'], self.PlotDim['minZ']])
max_bound = np.array([self.PlotDim['maxX'], self.PlotDim['maxY'], self.PlotDim['maxZ']])
self.RayTr.defineGrid(min_bound, max_bound, self.grid_dim['nx'], self.grid_dim['ny'], self.grid_dim['nz'],
self.vox_dim)
def prepare_input(self):
"""
Prepare input file mappings from project folder and scan directory.
Scans the RIEGL project folder for `.rdbx`, `.rxp`, `.DAT`, and `.png` files,
and builds dictionaries that map scan IDs to each file type to prepare raytracing.
"""
# get all rdbx
self.rdbx_scans = {}
self.scan_pos_to_name = {}
rdbx_scan_list = glob.glob(os.path.join(self.riscan_folder, "project.rdb", "SCANS", "**"))
if len(rdbx_scan_list) == 0:
raise ValueError(f'No rdbx files found in riscan folder {self.riscan_folder}. Please check the path and ensure it contains RIEGL scan data with .rdbx files. Path checked: {os.path.join(self.riscan_folder, "project.rdb", "SCANS", "**")}')
for folder in rdbx_scan_list:
if "@" in folder:
# indicates deleted folder
self.logger.info(f"Skipping deleted folder {folder}")
continue
folder_name = os.path.basename(folder)
scan_pos_base = folder_name[:10]
if self.exclude_scan_pattern is not None and self.exclude_scan_pattern in folder_name:
self.logger.info(f"Excluding scan {folder_name} based on exclude_scan_pattern in config")
continue
rdbx_folders = glob.glob(os.path.join(folder, "SINGLESCANS", "**"))
rdbx_folders = [folder for folder in rdbx_folders if "residual" not in folder]
# skip deleted scans as well
rdbx_folders = [folder for folder in rdbx_folders if "@" not in folder]
# if multiple scans were left, we take latest one as this is usually the best one
# not sure how to make this customizable, should warn in documentation maybe
if len(rdbx_folders) > 1:
self.logger.debug(f"multiple rdbx folders found for position {scan_pos_base}, taking latest one ( {rdbx_folders} )")
rdbx_folders = sorted(rdbx_folders)
if len(rdbx_folders) == 0:
self.logger.warning(f"no rdbx folder found for position {scan_pos_base}, skipping.")
self.logger.debug(f"Path checked: {os.path.join(folder, 'SINGLESCANS', '**')}")
continue
rdbx_folder_final = rdbx_folders[-1]
self.scan_pos_to_name[scan_pos_base] = os.path.basename(rdbx_folder_final)
rdbx_files = glob.glob(os.path.join(rdbx_folder_final, "*.rdbx"))
rdbx_files = [file for file in rdbx_files if "residual" not in file]
if len(rdbx_files) > 1:
self.logger.warning(f"multiple rdbx files for single scan found ({scan_pos_base}), skipping.")
self.logger.debug(f"Path checked: {os.path.join(rdbx_folder_final, '*.rdbx')}")
continue
if len(rdbx_files) == 0:
self.logger.warning(f"no rdbx files for scan found ({scan_pos_base}), skipping.")
self.logger.debug(f"Path checked: {os.path.join(rdbx_folder_final, '*.rdbx')}")
continue
rdbx_final = rdbx_files[0]
self.rdbx_scans[scan_pos_base] = rdbx_final
# get transform files
# TODO: option for csv? not priority
self.transform_files = {}
for scan_pos_name in self.rdbx_scans:
transform_file = glob.glob(os.path.join(self.riscan_folder, f'{scan_pos_name}.DAT'))
if len(transform_file) > 1:
# should never happen
self.logger.warning(f"Multiple DAT files found for {scan_pos_name}, taking random one.")
self.logger.debug(f"Path checked: {os.path.join(self.riscan_folder, f'{scan_pos_name}.DAT')}")
if len(transform_file) == 0:
self.logger.warning(f"No DAT files found for {scan_pos_name}, checking if any dat files contain scan position name.")
self.logger.debug(f"Path checked: {os.path.join(self.riscan_folder, f'{scan_pos_name}.DAT')}")
# also check contains
transform_file = glob.glob(os.path.join(self.riscan_folder, f'*{scan_pos_name}*.DAT'))
if len(transform_file) == 0:
self.logger.warning(f"Cant find DAT file for scan {scan_pos_name}, will not be processed")
continue
self.logger.warning(f"Alternative path used for DAT file: {transform_file[0]}")
self.transform_files[scan_pos_name] = transform_file[0]
# get rxp's and optionally previews
self.rxp_scans = {}
if self.model_empty_pulses:
self.png_scans = {}
# look for rxp files matching rdbx files
for pos in self.rdbx_scans:
rxp_folder = os.path.join(self.proj_folder, f"{pos}.SCNPOS")
if not os.path.exists(rxp_folder):
self.logger.warning(f"rxp folder not found for position {pos}, skipping")
self.logger.debug(f"Path checked: {rxp_folder}")
continue
# search for file with exact name of rdbx scan
scan_name = self.scan_pos_to_name[pos]
rxp_file = os.path.join(rxp_folder, "scans", f"{scan_name}.rxp")
if not os.path.exists(rxp_file):
self.logger.warning(f"rxp file not found for position {pos}, skipping")
self.logger.debug(f"Path checked: {rxp_file}")
continue
self.rxp_scans[pos] = rxp_file
if self.model_empty_pulses:
png_file = rxp_file[:-4] + ".png"
if not os.path.exists(png_file):
self.logger.warning(f"preview not found for position {scan_pos_base}, skipping")
self.logger.debug(f"Path checked: {png_file}")
continue
self.png_scans[pos] = png_file
def rdbx_rxp_to_df(self, rdbx, rxp):
"""
Convert RDBX and RXP binary scan files into DataFrames with relevant fields.
Parameters
----------
rdbx : str
Path to the `.rdbx` file containing return information.
rxp : str
Path to the `.rxp` file containing pulse information.
Returns
-------
tuple of pandas.DataFrame
A tuple of two DataFrames:
- df_rdbx : Returns with coordinates and beam info.
- df_rxp : Pulses with origin and beam direction.
"""
columns_rxp = ["beam_origin_x", "beam_origin_y", "beam_origin_z", "beam_direction_x", "beam_direction_y", "beam_direction_z", "scanline", "scanline_idx", "timestamp"]
subset_rxp = {k: rxp.pulses[k] for k in columns_rxp}
df_rxp = pd.DataFrame.from_dict(subset_rxp)
columns_rdbx = ["x", "y", "z", "scanline", "scanline_idx", "reflectance", "target_index", "target_count"]
subset_rdbx = {k: rdbx.points[k] for k in columns_rdbx}
df_rdbx = pd.DataFrame.from_dict(subset_rdbx)
min_scanline = df_rdbx[["scanline"]].to_numpy().min()
if min_scanline < -1:
# scanline in rdbx is in reverse (not sure if this is due to rotation of scanner or just bug)
# shift (for vis)
df_rdbx[["scanline"]] = df_rdbx[["scanline"]] + abs(df_rdbx[["scanline"]].min())
max_scanline = df_rdbx[["scanline"]].to_numpy().max()
# then invert rxp scanline + shift
df_rxp[["scanline"]] = df_rxp[["scanline"]]*(-1)
df_rxp[["scanline"]] = df_rxp[["scanline"]] + max_scanline
if df_rxp[["scanline"]].to_numpy().max() > df_rdbx[["scanline"]].to_numpy().max():
# drop last column
df_rxp.drop(df_rxp.loc[df_rxp['scanline'] > df_rdbx["scanline"].to_numpy().max()].index, inplace=True)
return df_rdbx, df_rxp
def merge_df_rdbx_rxp(self, df_rdbx, df_rxp):
"""
Merge return and pulse DataFrames to separate hits and empty pulses.
Parameters
----------
df_rdbx : pandas.DataFrame
Return data from RDBX file.
df_rxp : pandas.DataFrame
Pulse data from RXP file.
Returns
-------
tuple of pandas.DataFrame
A tuple of two DataFrames:
- df_hit : Pulses with associated returns.
- df_empty : Pulses without any return.
"""
merged_df = df_rxp.merge(df_rdbx, how="left", on=["scanline", "scanline_idx"])
na_mask = merged_df["reflectance"].isna()
point_df = merged_df[~na_mask]
empty_pulse_df = merged_df[na_mask]
# for points, keep x,y,z, beam_origin and reflectance, timestamp
if not self.debug:
point_df = point_df.drop(["scanline", "scanline_idx", "beam_direction_x", "beam_direction_y", "beam_direction_z"], axis=1)
else:
# if debug: also keep beam_direction to check colinearity
point_df = point_df.drop(["scanline", "scanline_idx"], axis=1)
# for empty pulses, just keep beam_origin and beam_direction and timestamp
empty_pulse_df = empty_pulse_df.drop(["x", "y", "z", "reflectance"], axis=1)
return point_df, empty_pulse_df
def mask_empty_pulses_preview(self, df_empty, preview_png, max_scanline_idx, max_scanline):
"""
Mask out empty pulses that fall within occluded regions using preview image.
Parameters
----------
df_empty : pandas.DataFrame
DataFrame of empty pulses.
preview_png : str
Path to preview PNG image showing occlusion pattern.
max_scanline_idx : int
Maximum scanline index in current scan.
max_scanline : int
Maximum scanline in current scan.
Returns
-------
pandas.DataFrame
Filtered DataFrame with only empty pulses in non-occluded regions.
"""
image = cv2.imread(preview_png)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB
blue_lower = np.array([0, 0, 255])
blue_upper = np.array([0, 0, 255])
blue_mask = cv2.inRange(image, blue_lower, blue_upper)
blue_mask = blue_mask > 0
h,w = blue_mask.shape
scanline_idx_mask_idxs, scanline_mask_idxs = blue_mask.nonzero()
h_scale = (max_scanline_idx+1)/h
w_scale= (max_scanline+1)/w
scanline_idx_lower_h = np.ceil(scanline_idx_mask_idxs*h_scale).astype(int)
scanline_idx_upper_h = np.floor((scanline_idx_mask_idxs+1)*h_scale).astype(int)
scanline_lower_h = np.ceil(scanline_mask_idxs*w_scale).astype(int)
scanline_upper_h = np.floor((scanline_mask_idxs+1)*w_scale).astype(int)
img_upscaled_manual = np.zeros(shape=(max_scanline_idx+1, max_scanline+1), dtype=bool)
for i in range(len(scanline_lower_h)):
img_upscaled_manual[scanline_idx_lower_h[i]:scanline_idx_upper_h[i], scanline_lower_h[i]:scanline_upper_h[i]] = 1
# filter empty pulses df
mask = np.any(
(df_empty["scanline"].values[:, None] >= scanline_lower_h) &
(df_empty["scanline"].values[:, None] <= scanline_upper_h) &
(df_empty["scanline_idx"].values[:, None] >= scanline_idx_lower_h) &
(df_empty["scanline_idx"].values[:, None] <= scanline_idx_upper_h),
axis=1
)
# Apply the mask to keep only rows that match
df_filtered = df_empty[mask]
# also mask out the lower few lines
# on upright scans, this consistently seems to be the first 5 scanlines
# on tilts, sometimes up to 50-60
# -> filter out lowest 80 to be sure
df_filtered_lower = df_filtered[df_filtered["scanline_idx"]<max_scanline_idx-80]
if self.debug:
# write image to output folder
ofolder = os.path.join(self.out_dir, "debug")
if not os.path.exists(ofolder):
os.makedirs(ofolder)
opath = os.path.join(ofolder, f"preview_mask_{os.path.basename(preview_png)}")
self.logger.debug(f"Filtering done using preview {preview_png}, saved at {opath}")
plot_riegl_grid(df_empty, max_scanline, max_scanline_idx, blue_mask, out_path=opath)
return df_filtered_lower
def test_colinearity(self, point_df, n_points=None):
"""
Check geometric collinearity between pulse origin, beam direction, and return point.
Parameters
----------
point_df : pandas.DataFrame
DataFrame containing pulse origin, direction, and return coordinates.
n_points : int or None, optional
If specified, tests only the first `n_points` entries.
Returns
-------
int
number of points failing collinearity check
"""
def check_parallel(beam_origin, beam_direction, point, epsilon=1e-6):
vector_point_origin = point - beam_origin
return (np.dot(beam_direction, vector_point_origin))/(np.linalg.norm(vector_point_origin)*np.linalg.norm(beam_direction)) > 1 - epsilon
beam_origin = point_df[["beam_origin_x", "beam_origin_y", "beam_origin_z"]].to_numpy()
beam_direction = point_df[["beam_direction_x", "beam_direction_y", "beam_direction_z"]].to_numpy()
point = point_df[["x", "y", "z"]].to_numpy()
count = 0
if n_points is None:
n_points = len(point_df)
for i in range(n_points):
if n_points is None:
# check all points
idx = i
else:
# generate random index to check
idx = randrange(len(point_df))
if not check_parallel(beam_origin[idx,:], beam_direction[idx,:], point[idx,:]):
count += 1
return count
def do_raytracing(self):
"""
Perform voxel-based ray tracing for all scan positions.
For each scan position:
- Reads RDBX and RXP data.
- Optionally models empty pulses using preview image.
- Adds pulses to ray tracer and performs traversal.
- Clears memory after each scan.
"""
for i, scan in enumerate(self.rdbx_scans):
# read rdbx file for point data
self.logger.info(f"Processing {scan} ({i+1}/{len(self.rdbx_scans)})")
if scan not in self.transform_files:
self.logger.info(f"Transform file not found for pos {scan}, skipping.")
continue
self.logger.info(f"Reading RDBX and RXP")
# read rdbx and optionally rxp
rdbx = riegl_io.RDBFile(self.rdbx_scans[scan], transform_file=self.transform_files[scan])
if scan in self.rxp_scans:
rxp = riegl_io.RXPFile(self.rxp_scans[scan], transform_file=self.transform_files[scan])
else:
self.logger.warning(f"RXP not found for pos {scan}, skipping.")
continue
if self.debug:
matrix = rdbx.transform
self.logger.debug(f"Transformation file: {self.transform_files[scan]}")
self.logger.debug(f"Transformation matrix: {matrix}")
self.logger.info(f"Merging RDBX and RXP")
# merge rxp and rdbx
df_rdbx, df_rxp = self.rdbx_rxp_to_df(rdbx, rxp)
point_df, empty_pulse_df = self.merge_df_rdbx_rxp(df_rdbx, df_rxp)
# get max scanline and idx
max_scanline = max(df_rdbx[["scanline"]].to_numpy().max(), df_rxp[["scanline"]].to_numpy().max())
max_scanline_idx= max(df_rdbx[["scanline_idx"]].to_numpy().max(), df_rxp[["scanline_idx"]].to_numpy().max())
# test colinearity to see if rdbx and rxp merge succesfull
if self.debug:
# n_tested = len(point_df) # TODO: adapt
n_tested = 100000
self.logger.debug(f"Testing collinearity for {n_tested} points")
n = self.test_colinearity(point_df, n_points=n_tested)
if n > 0:
self.logger.warning(f"Collinearity test for {scan} returned {n} non-colinear points out of {n_tested} tested")
if self.model_empty_pulses:
self.logger.info("Reading preview and masking empty pulses")
if scan not in self.png_scans:
raise ValueError(f"Model empty pulses on but scan preview not found for {scan}, exiting.")
empty_pulse_df = self.mask_empty_pulses_preview(empty_pulse_df, self.png_scans[scan], max_scanline_idx, max_scanline)
self.logger.info("Adding point data")
x = point_df["x"].to_numpy()
y = point_df["y"].to_numpy()
z = point_df["z"].to_numpy()
gps_time = point_df["timestamp"].to_numpy()
self.logger.debug(f"Number of points in rdbx: {len(df_rdbx)}")
unique_gps = np.unique(gps_time)
self.logger.debug(f"Number of unique gps entries in point_df: {len(unique_gps)}")
self.logger.debug(f"Number of pulses in rxp: {len(df_rxp)}, number of empty pulses: {len(empty_pulse_df)}, diff: {len(df_rxp)-len(empty_pulse_df)}")
return_number = point_df["target_index"].to_numpy()
number_of_returns = point_df["target_count"].to_numpy()
sensor_x = point_df["beam_origin_x"].to_numpy()
sensor_y = point_df["beam_origin_y"].to_numpy()
sensor_z = point_df["beam_origin_z"].to_numpy()
self.RayTr.addPointData(x, y, z, sensor_x, sensor_y, sensor_z, gps_time, return_number, number_of_returns)
self.logger.info("Fixing incomplete pulses (number of returns not correct, likely due to filtered points)")
self.RayTr.cleanUpPulseDataset()
self.RayTr.getPulseDatasetReport()
self.logger.info("Perform raytracing")
tic = time.time()
self.RayTr.doRaytracing()
toc = time.time()
self.logger.info("Time elapsed for raytracing: {:.2f} seconds".format(toc - tic))
if self.model_empty_pulses:
self.logger.info("Running raytracing for empty pulses")
tic = time.time()
sensor_x = empty_pulse_df["beam_origin_x"].to_numpy()
sensor_y = empty_pulse_df["beam_origin_y"].to_numpy()
sensor_z = empty_pulse_df["beam_origin_z"].to_numpy()
direction_x = empty_pulse_df["beam_direction_x"].to_numpy()
direction_y = empty_pulse_df["beam_direction_y"].to_numpy()
direction_z = empty_pulse_df["beam_direction_z"].to_numpy()
gps_time = empty_pulse_df["timestamp"].to_numpy()
self.RayTr.addEmptyPulseData(sensor_x, sensor_y, sensor_z, direction_x, direction_y, direction_z, gps_time)
self.RayTr.doRaytracingEmptyPulses()
toc = time.time()
self.logger.info("Time elapsed for raytracing empty pulses: {:.2f} seconds".format(toc - tic))
self.RayTr.clearPulseDataset()
self.logger.info("Report on traversal:")
self.RayTr.reportOnTraversal()
return
def determine_grid(self, buffer):
# TODO: automatically derive grid from scan_positions
# Take predefined buffer in x,y,z direction (configurable)
# then find x,y,z extremes of scan positions and define grid based on this
# can have auto_dim flag in config
# if auto_dim, xyz_buf must be defined
# if not, plot_dim must be defined
# should probably be util/helper function in seperate module
raise NotImplementedError
def save_raytracing_output(self):
"""
Save ray tracing results (`Nhit`, `Nmiss`, `Nocc`, `Classification`) to disk.
Saves the voxel outputs as `.npy` arrays, and optionally writes `.ply` files
for visualization if `self.output_voxels` is True.
"""
self.logger.info("Saving output")
self.logger.info("Extracting Nhit")
tic = time.time()
self.Nhit = self.RayTr.getNhit()
self.Nhit = np.array(self.Nhit, dtype=np.int32)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
self.logger.info("Extracting Nocc")
tic = time.time()
self.Nocc = self.RayTr.getNocc()
self.Nocc = np.array(self.Nocc, dtype=np.int32)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
self.logger.info("Extracting Nmiss")
tic = time.time()
self.Nmiss = self.RayTr.getNmiss()
self.Nmiss = np.array(self.Nmiss, dtype=np.int32)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
self.logger.info("Saving Occlusion Outputs As .npy")
tic = time.time()
np.save(os.path.join(self.out_dir, "Nhit.npy"), self.Nhit)
np.save(os.path.join(self.out_dir, "Nmiss.npy"), self.Nmiss)
np.save(os.path.join(self.out_dir, "Nocc.npy"), self.Nocc)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
# Create Classification grid
self.logger.info("Classify Grid")
tic = time.time()
self.Classification = np.zeros((self.grid_dim['nx'], self.grid_dim['ny'], self.grid_dim['nz']), dtype=int)
self.Classification[np.logical_and.reduce((self.Nhit > 0, self.Nmiss >= 0, self.Nocc >= 0))] = 1 # voxels that were observed
self.Classification[np.logical_and.reduce((self.Nhit == 0, self.Nmiss > 0, self.Nocc >= 0))] = 2 # voxels that are empty
self.Classification[
np.logical_and.reduce((self.Nhit == 0, self.Nmiss == 0, self.Nocc > 0))] = 3 # voxels that are hidden (occluded)
self.Classification[np.logical_and.reduce((self.Nhit == 0, self.Nmiss == 0,
self.Nocc == 0))] = 4 # voxels that were not observed # TODO: Figure out, why this overwrites voxels that are classified as occluded! -> this was because np.logical_and only takes in 2 arrays as input, not 3! use np.logical_and.reduce() for that!
np.save(os.path.join(self.out_dir, "Classification.npy"), self.Classification)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
# write ply file
if self.output_voxels:
self.logger.info("Saving Occlusion Outputs As .ply")
self.logger.warning("Saving ply files can take a while, especially for large grids. Consider setting output_voxels to False if you only need the .npy output arrays.")
tic = time.time()
verts, faces = prepare_ply(self.vox_dim, self.plot_dim, self.Nhit)
ost.write_ply(os.path.join(self.out_dir, "Nhit.ply"), verts, ['X', 'Y', 'Z', 'data'], triangular_faces=faces)
verts, faces = prepare_ply(self.vox_dim, self.plot_dim, self.Nmiss)
ost.write_ply(os.path.join(self.out_dir, "Nmiss.ply"), verts, ['X', 'Y', 'Z', 'data'], triangular_faces=faces)
verts, faces = prepare_ply(self.vox_dim, self.plot_dim, self.Nocc)
ost.write_ply(os.path.join(self.out_dir, "Nocc.ply"), verts, ['X', 'Y', 'Z', 'data'], triangular_faces=faces)
verts, faces = prepare_ply(self.vox_dim, self.plot_dim, self.Classification)
ost.write_ply(os.path.join(self.out_dir, "Classification.ply"), verts, ['X', 'Y', 'Z', 'data'], triangular_faces=faces)
self.occl = np.zeros(shape=self.Classification.shape)
x4, y4, z4 = np.where(self.Classification == 4)
self.occl[x4, y4, z4] = self.Classification[x4, y4, z4]
verts, faces = prepare_ply(self.vox_dim, self.plot_dim, self.occl)
ost.write_ply(os.path.join(self.out_dir, "Occl.ply"), verts, ['X', 'Y', 'Z', 'data'], triangular_faces=faces)
toc = time.time()
self.logger.info("Elapsed Time: {:.2f} seconds".format(toc - tic))
@staticmethod
def align_plot_dim_to_voxel_size(plot_dim, vox_dim):
"""extend max bounds so each axis extent is divisible by vox_dim."""
adjusted = [float(v) for v in plot_dim]
messages = []
tol = 1e-9
axes = (("X", 0, 3), ("Y", 1, 4), ("Z", 2, 5))
for axis_name, min_idx, max_idx in axes:
min_bound = adjusted[min_idx]
max_bound = adjusted[max_idx]
extent = max_bound - min_bound
if extent <= 0:
raise ValueError(
f"Invalid plot_dim on axis {axis_name}: max ({max_bound}) must be greater than min ({min_bound})."
)
n_voxels = int(np.ceil((extent / vox_dim) - tol))
adjusted_extent = n_voxels * vox_dim
if not np.isclose(extent, adjusted_extent, rtol=0.0, atol=tol):
new_max = min_bound + adjusted_extent
messages.append(
f"Axis {axis_name}: extent {extent:.12g} is not divisible by vox_dim {vox_dim:.12g}. "
f"Extending max bound from {max_bound:.12g} to {new_max:.12g}."
)
adjusted[max_idx] = new_max
return adjusted, messages
|