-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraht_torch.py
1247 lines (819 loc) · 34.5 KB
/
raht_torch.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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on May 25, 2021
Modified on Jul 20, 2024
This code is derived by the implementation of 3DAC. See https://fatpeter.github.io/ for more details.
It is an python version of RAHT based on https://github.com/digitalivp/RAHT/tree/reorder.
The original C implementation is more readable.
"""
from argparse import ArgumentParser
import numpy as np
import torch
from plyfile import PlyData
class LaplaceDist:
def __init__(self, loc, b_m, Q):
self.loc = loc
self.b_m = b_m
self.Q = Q
def laplace_prob(self, x):
return 1 / (2*self.b_m) * np.exp(-np.abs(x - self.loc) / self.b_m)
def get_probability(self, k):
Q_b = self.Q / self.b_m
if k != 0:
return 0.5 * np.exp(-np.abs(k) * Q_b) * (np.exp(0.5 * Q_b) - np.exp(-0.5 * Q_b))
else:
return 1 - np.exp(-0.5 * Q_b)
# morton coding
# convert voxlized and deduplicated point cloud to morton code
def copyAsort(V):
# input
# V: np.array (n,3), input vertices
# output
# W: np.array (n,), weight
# val: np.array (n,), zyx val of vertices
# reord: np.array (n,), idx ord after sort
V=V.astype(np.uint64)
# w of leaf node sets to 1
W=np.ones(V.shape[0])
# encode zyx (pos) to bin
vx, vy, vz= V[:,2], V[:,1], V[:,0]
val = ((0x000001 & vx) ) + ((0x000001 & vy)<< 1) + ((0x000001 & vz)<< 2) + \
((0x000002 & vx)<< 2) + ((0x000002 & vy)<< 3) + ((0x000002 & vz)<< 4) + \
((0x000004 & vx)<< 4) + ((0x000004 & vy)<< 5) + ((0x000004 & vz)<< 6) + \
((0x000008 & vx)<< 6) + ((0x000008 & vy)<< 7) + ((0x000008 & vz)<< 8) + \
((0x000010 & vx)<< 8) + ((0x000010 & vy)<< 9) + ((0x000010 & vz)<<10) + \
((0x000020 & vx)<<10) + ((0x000020 & vy)<<11) + ((0x000020 & vz)<<12) + \
((0x000040 & vx)<<12) + ((0x000040 & vy)<<13) + ((0x000040 & vz)<<14) + \
((0x000080 & vx)<<14) + ((0x000080 & vy)<<15) + ((0x000080 & vz)<<16) + \
((0x000100 & vx)<<16) + ((0x000100 & vy)<<17) + ((0x000100 & vz)<<18) + \
((0x000200 & vx)<<18) + ((0x000200 & vy)<<19) + ((0x000200 & vz)<<20) + \
((0x000400 & vx)<<20) + ((0x000400 & vy)<<21) + ((0x000400 & vz)<<22) + \
((0x000800 & vx)<<22) + ((0x000800 & vy)<<23) + ((0x000800 & vz)<<24) + \
((0x001000 & vx)<<24) + ((0x001000 & vy)<<25) + ((0x001000 & vz)<<26) + \
((0x002000 & vx)<<26) + ((0x002000 & vy)<<27) + ((0x002000 & vz)<<28) + \
((0x004000 & vx)<<28) + ((0x004000 & vy)<<29) + ((0x004000 & vz)<<30) + \
((0x008000 & vx)<<30) + ((0x008000 & vy)<<31) + ((0x008000 & vz)<<32) + \
((0x010000 & vx)<<32) + ((0x010000 & vy)<<33) + ((0x010000 & vz)<<34) + \
((0x020000 & vx)<<34) + ((0x020000 & vy)<<35) + ((0x020000 & vz)<<36) + \
((0x040000 & vx)<<36) + ((0x040000 & vy)<<37) + ((0x040000 & vz)<<38) + \
((0x080000 & vx)<<38) + ((0x080000 & vy)<<39) + ((0x080000 & vz)<<40)
# + \
# ((0x100000 & vx)<<40) + ((0x100000 & vy)<<41) + ((0x100000 & vz)<<42) + \
# ((0x200000 & vx)<<42) + ((0x200000 & vy)<<43) + ((0x200000 & vz)<<44) + \
# ((0x400000 & vx)<<44) + ((0x400000 & vy)<<45) + ((0x400000 & vz)<<46) + \
# ((0x800000 & vx)<<46) + ((0x800000 & vy)<<47) + ((0x800000 & vz)<<48)
reord=np.argsort(val)
val=np.sort(val)
val = val.astype(np.uint64)
return W, val, reord
# morton decoding
# convert morton code to point cloud
def val2V(val, factor):
'''
Parameters
----------
val : morton code
factor : shift morton code for deocoding
Returns
-------
V_re : point cloud
'''
if factor>2 or factor<0:
print('error')
return
val = val<<factor
V_re = np.zeros((val.shape[0],3))
V_re[:,2] = (0x000001 & val) + \
(0x000002 & (val>> 2)) + \
(0x000004 & (val>> 4)) + \
(0x000008 & (val>> 6)) + \
(0x000010 & (val>> 8)) + \
(0x000020 & (val>>10)) + \
(0x000040 & (val>>12)) + \
(0x000080 & (val>>14)) + \
(0x000100 & (val>>16)) + \
(0x000200 & (val>>18)) + \
(0x000400 & (val>>20)) + \
(0x000800 & (val>>22)) + \
(0x001000 & (val>>24)) + \
(0x002000 & (val>>26)) + \
(0x004000 & (val>>28)) + \
(0x008000 & (val>>30)) + \
(0x010000 & (val>>32)) + \
(0x020000 & (val>>34)) + \
(0x040000 & (val>>36)) + \
(0x080000 & (val>>38)) + \
(0x100000 & (val>>40))
# + \
# (0x200000 & (val>>42)) + \
# (0x400000 & (val>>44)) + \
# (0x800000 & (val>>46))
V_re[:,1] = (0x000001 & (val>> 1)) + \
(0x000002 & (val>> 3)) + \
(0x000004 & (val>> 5)) + \
(0x000008 & (val>> 7)) + \
(0x000010 & (val>> 9)) + \
(0x000020 & (val>>11)) + \
(0x000040 & (val>>13)) + \
(0x000080 & (val>>15)) + \
(0x000100 & (val>>17)) + \
(0x000200 & (val>>19)) + \
(0x000400 & (val>>21)) + \
(0x000800 & (val>>23)) + \
(0x001000 & (val>>25)) + \
(0x002000 & (val>>27)) + \
(0x004000 & (val>>29)) + \
(0x008000 & (val>>31)) + \
(0x010000 & (val>>33)) + \
(0x020000 & (val>>35)) + \
(0x040000 & (val>>37)) + \
(0x080000 & (val>>39)) + \
(0x100000 & (val>>41))
# + \
# (0x200000 & (val>>43)) + \
# (0x400000 & (val>>45)) + \
# (0x800000 & (val>>47))
V_re[:,0] = (0x000001 & (val>> 2)) + \
(0x000002 & (val>> 4)) + \
(0x000004 & (val>> 6)) + \
(0x000008 & (val>> 8)) + \
(0x000010 & (val>>10)) + \
(0x000020 & (val>>12)) + \
(0x000040 & (val>>14)) + \
(0x000080 & (val>>16)) + \
(0x000100 & (val>>18)) + \
(0x000200 & (val>>20)) + \
(0x000400 & (val>>22)) + \
(0x000800 & (val>>24)) + \
(0x001000 & (val>>26)) + \
(0x002000 & (val>>28)) + \
(0x004000 & (val>>30)) + \
(0x008000 & (val>>32)) + \
(0x010000 & (val>>34)) + \
(0x020000 & (val>>36)) + \
(0x040000 & (val>>38)) + \
(0x080000 & (val>>40)) + \
(0x100000 & (val>>42))
# + \
# (0x200000 & (val>>44)) + \
# (0x400000 & (val>>46)) + \
# (0x800000 & (val>>48))
if factor == 1:
V_re[:,2]/=2
if factor == 2:
V_re[:,1]/=2
V_re[:,2]/=2
return V_re
def transform_batched(a0, a1, C0, C1):
# input
# a0, a1: float, weight
# C0, C1: np.array (n,), att of vertices
# output
# v0, v1: np.array (n,), trans att of vertices
trans_matrix=np.array([[a0, a1],
[-a1, a0]])
trans_matrix=trans_matrix.transpose((2,0,1))
V=np.matmul(trans_matrix, np.concatenate((C0,C1),1))
return V[:,0], V[:,1]
def transform_batched_torch(a0, a1, C0, C1):
# print(a0.shape)
t0 = torch.tensor(a0[:,None]).cuda().float()
t1 = torch.tensor(a1[:,None]).cuda().float()
V0 = t0*C0+t1*C1
V1 = -t1*C0+t0*C1
# temp1 = a0[:,None]
# temp2 = a1[:,None]
# trans_matrix = np.concatenate((temp1, temp2, -temp2, temp1),1)
# trans_matrix = trans_matrix.reshape(-1,2,2)
# trans_matrix = torch.tensor(trans_matrix).to(C0.get_device()).float()
# print('trans_matrix.shape', trans_matrix.shape)
# print('trans_matrix.shape', C0.shape)
# print('torch.cat((C0,C1),1).shape', torch.cat((C0,C1),1).shape)
# V=torch.matmul(trans_matrix, torch.cat((C0,C1),1))
return V0, V1
def itransform_batched(a0, a1, CT0, CT1):
# input
# a0, a1: float, weight
# CT0, CT1: np.array (n,), trans att of vertices
# output
# c0, c1: np.array (n,), att of vertices
trans_matrix=np.array([[a0, -a1],
[a1, a0]])
trans_matrix=trans_matrix.transpose((2,0,1))
C=np.matmul(trans_matrix, np.concatenate((CT0,CT1),1))
return C[:,0], C[:,1]
def itransform_batched_torch(a0, a1, CT0, CT1):
# input
# a0, a1: float, weight
# CT0, CT1: np.array (n,), trans att of vertices
# output
# c0, c1: np.array (n,), att of vertices
# trans_matrix=np.array([[a0, -a1],
# [a1, a0]])
# trans_matrix=trans_matrix.transpose((2,0,1))
# C=np.matmul(trans_matrix, np.concatenate((CT0,CT1),1))
# return C[:,0], C[:,1]
t0 = torch.tensor(a0[:,None]).cuda().float()
t1 = torch.tensor(a1[:,None]).cuda().float()
V0 = t0*CT0-t1*CT1
V1 = t1*CT0+t0*CT1
return V0, V1
def haar3D(inV, inC, depth):
'''
Parameters
----------
inV : point cloud geometry(pre-voxlized and deduplicated)
inC : attributes
depth : depth level of geometry(octree)
Returns
-------
res : transformed coefficients and side information
'''
import copy
inC = copy.deepcopy(inC)
# N,NN number of points
# K, dims (3) of geometry
N, K = inC.shape
NN = N
# depth of RAHT tree (without leaf node level)
depth *= 3
# print('depth', depth)
# low_freq coeffs for transmitting coeffs (high_freq)
# low_freq = np.zeros(inC.shape)
wT = np.zeros((N, )).astype(np.uint64)
valT = np.zeros((N, )).astype(np.uint64)
posT = np.zeros((N, )).astype(np.int64)
# position of coeffs
node_xyz = np.zeros((N, 3))-1
depth_CT = np.zeros((N, ))-1
# morton coding
# return weight, morton code, map from inV to val
w, val, TMP = copyAsort(inV)
# pos, order from transformed coeffes to morton sorted attributes
pos = np.arange(N)
C = inC[TMP].astype(np.float64)
# low_freq for each depth
iCT_low=[]
# parent idx for each depth
iparent=[]
# weight for each depth
iW=[]
# node position for each depth
iPos=[]
for d in range(depth):
# print('-'*10, 'd:', d, '-'*10)
# num of nodes for current depth
S = N
# 1D example (trans val 1 and 4, merge 2 and 3)
# 01234567
# idx: 0, 1, 2, 3
# val: 1, 2, 3, 4
# merge two leaf nodes or not
# mask: False, True, False, False
# combine two neighbors or transmit
# combine idx: 1
# trans idx: 0, 3
# merge two leaf nodes or not
temp=val.astype(np.uint64)&0xFFFFFFFFFFFFFFFE
mask=temp[:-1]==temp[1:]
mask=np.concatenate((mask,[False]))
# 2 types of idx for current level of RAHT tree
# combine two neighbors or transmit
comb_idx_array=np.where(mask==True)[0]
trans_idx_array=np.where(mask==False)[0]
trans_idx_array=np.setdiff1d(trans_idx_array, comb_idx_array+1)
# print('comb_idx_array.shape', comb_idx_array.shape)
# print('trans_idx_array.shape', trans_idx_array.shape)
# 2 types of idx for next level of RAHT tree
# idxT_array, idx of low-freq for next depth level
# maskT == False for trans (not merge two leaf nodes)
# maskT == True for comb (merge two leaf nodes)
# maskT: False, True, False (1D example)
idxT_array=np.setdiff1d(np.arange(S), comb_idx_array+1)
maskT=mask[idxT_array]
# 2 types of weight for next level of RAHT tree
# wT[N] = wT[M] (not merge two leaf nodes)
# wT[M] = w[i] + w[j] (merge two leaf nodes)
# print(w.shape)
# print(wT.shape)
# print(wT[np.where(maskT==True)[0]].shape)
# print((w[comb_idx_array]+w[comb_idx_array+1]).shape)
wT[np.where(maskT==False)[0]] = w[trans_idx_array]
wT[np.where(maskT==True)[0]] = w[comb_idx_array]+w[comb_idx_array+1]
# pos is used to connect C and val/w (current level)
# posT is used to connect C and val/w (next level)
# pos: 0, 1, 2, 3
# posT: 0, 1, 3, *
posT[np.where(maskT==False)[0]] = pos[trans_idx_array]
posT[np.where(maskT==True)[0]] = pos[comb_idx_array]
# transform attr to coeff
left_node_array, right_node_array = comb_idx_array, comb_idx_array+1
a = np.sqrt((w[left_node_array])+(w[right_node_array]))
C[pos[left_node_array]], C[pos[right_node_array]] = transform_batched(np.sqrt((w[left_node_array]))/a,
np.sqrt((w[right_node_array]))/a,
C[pos[left_node_array],None],
C[pos[right_node_array],None])
# collect side information for current depth
parent=np.arange(S)
parent_t=np.zeros(S)
parent_t[right_node_array]=1
parent_t = parent_t.cumsum()
parent = parent-parent_t
# collected but not used in paper
iparent.append(parent.astype(int))
# High-freq nodes do not exist in the leaf level, thus collect information from the next depth.
# collect side information after transform for next depth
iCT_low.append(C[pos[idxT_array]])
num_nodes = N-comb_idx_array.shape[0]
iW.append(wT[:num_nodes]+0)
Pos_t = val2V(val, d%3)[idxT_array]
if d%3 == 0:
Pos_t[:,2]=Pos_t[:,2]//2
if d%3 == 1:
Pos_t[:,1]=Pos_t[:,1]//2
if d%3 == 2:
Pos_t[:,0]=Pos_t[:,0]//2
iPos.append(Pos_t)
# collect side information of high_freq nodes for next depth
# tree node feature extraction without considering low-freq nodes
# low_freq[pos[right_node_array]]=C[pos[left_node_array]]
node_xyz[pos[right_node_array]] = val2V(val[right_node_array], d%3)
if d%3 == 0:
node_xyz[pos[right_node_array],2]=node_xyz[pos[right_node_array],2]//2
if d%3 == 1:
node_xyz[pos[right_node_array],1]=node_xyz[pos[right_node_array],1]//2
if d%3 == 2:
node_xyz[pos[right_node_array],0]=node_xyz[pos[right_node_array],0]//2
depth_CT[pos[trans_idx_array]] = d
depth_CT[pos[left_node_array]], depth_CT[pos[right_node_array]] = d, d
# end of information collection
# valT, morton code for the next depth
valT = (val >> 1)[idxT_array]
# num of leaf nodes for next level
N_T=N
N=N-comb_idx_array.shape[0]
# move pos,w of high-freq nodes in the end
# pos: 0, 1, 2, 3
# posT: 0, 1, 3, *
# posT: 0, 1, 3, 2
# transpose
N_idx_array=np.arange(N_T, N, -1)-NN-1
wT[N_idx_array]=wT[np.where(maskT==True)[0]]
posT[N_idx_array]=pos[comb_idx_array+1]
# move transposed pos,w of high-freq nodes in the end
pos[N:S] = posT[N:S]
w[N:S] = wT[N:S]
val, valT = valT, val
pos, posT = posT, pos
w, wT = wT, w
outW=np.zeros(w.shape)
outW[pos]=w
# print('iCT_low[-1].shape', iCT_low[-1].shape)
# print('low_freq.shape', low_freq.shape)
# low_freq[0] = iCT_low[-1]
res = {'CT':C,
'w':outW,
'depth_CT':depth_CT,
'node_xyz':node_xyz,
# 'low_freq':low_freq,
'iCT_low':iCT_low,
'iW':iW,
'iPos':iPos,
'iparent':iparent,
}
return res
def haar3D_torch(inC, depth, w, val, TMP):
'''
Parameters
----------
inV : point cloud geometry(pre-voxlized and deduplicated)
inC : attributes
depth : depth level of geometry(octree)
Returns
-------
res : transformed coefficients and side information
'''
N, K = inC.shape
NN = N
# depth of RAHT tree (without leaf node level)
depth *= 3
# print('depth', depth)
wT = np.zeros((N, )).astype(np.uint64)
valT = np.zeros((N, )).astype(np.uint64)
posT = np.zeros((N, )).astype(np.int64)
# position of coeffs
node_xyz = np.zeros((N, 3))-1
# depth_CT = np.zeros((N, ))-1
# morton coding
# return weight, morton code, map from inV to val
# w, val, TMP = copyAsort(inV)
pos = np.arange(N)
C = inC[torch.tensor(TMP)]
# .astype(torch.float64)
# parent idx for each depth
# iparent=[]
# weight for each depth
# iW=[]
# node position for each depth
# iPos=[]
for d in range(depth):
S = N
# merge two leaf nodes or not
temp=val.astype(np.uint64)&0xFFFFFFFFFFFFFFFE
mask=temp[:-1]==temp[1:]
mask=np.concatenate((mask,[False]))
# 2 types of idx for current level of RAHT tree
# combine two neighbors or transmit
comb_idx_array=np.where(mask==True)[0]
trans_idx_array=np.where(mask==False)[0]
trans_idx_array=np.setdiff1d(trans_idx_array, comb_idx_array+1)
# print('comb_idx_array.shape', comb_idx_array.shape)
# print('trans_idx_array.shape', trans_idx_array.shape)
# 2 types of idx for next level of RAHT tree
# idxT_array, idx of low-freq for next depth level
# maskT == False for trans (not merge two leaf nodes)
# maskT == True for comb (merge two leaf nodes)
# maskT: False, True, False (1D example)
idxT_array=np.setdiff1d(np.arange(S), comb_idx_array+1)
maskT=mask[idxT_array]
# 2 types of weight for next level of RAHT tree
# wT[N] = wT[M] (not merge two leaf nodes)
# wT[M] = w[i] + w[j] (merge two leaf nodes)
wT[np.where(maskT==False)[0]] = w[trans_idx_array]
wT[np.where(maskT==True)[0]] = w[comb_idx_array]+w[comb_idx_array+1]
# pos is used to connect C and val/w (current level)
# posT is used to connect C and val/w (next level)
# pos: 0, 1, 2, 3
# posT: 0, 1, 3, *
posT[np.where(maskT==False)[0]] = pos[trans_idx_array]
posT[np.where(maskT==True)[0]] = pos[comb_idx_array]
# transform attr to coeff
left_node_array, right_node_array = comb_idx_array, comb_idx_array+1
a = np.sqrt((w[left_node_array])+(w[right_node_array]))
C[pos[left_node_array]], C[pos[right_node_array]] = transform_batched_torch(np.sqrt((w[left_node_array]))/a,
np.sqrt((w[right_node_array]))/a,
C[pos[left_node_array],None],
C[pos[right_node_array],None])
# collect side information for current depth
parent=np.arange(S)
parent_t=np.zeros(S)
parent_t[right_node_array]=1
parent_t = parent_t.cumsum()
parent = parent-parent_t
# collected but not used in paper
# iparent.append(parent.astype(int))
# High-freq nodes do not exist in the leaf level, thus collect information from the next depth.
# collect side information after transform for next depth
# iCT_low.append(C[pos[idxT_array]].cpu().numpy())
# num_nodes = N-comb_idx_array.shape[0]
# iW.append(wT[:num_nodes]+0)
Pos_t = val2V(val, d%3)[idxT_array]
if d%3 == 0:
Pos_t[:,2]=Pos_t[:,2]//2
if d%3 == 1:
Pos_t[:,1]=Pos_t[:,1]//2
if d%3 == 2:
Pos_t[:,0]=Pos_t[:,0]//2
# iPos.append(Pos_t)
# collect side information of high_freq nodes for next depth
# tree node feature extraction without considering low-freq nodes
# low_freq[pos[right_node_array]]=C[pos[left_node_array]].cpu().numpy()
node_xyz[pos[right_node_array]] = val2V(val[right_node_array], d%3)
if d%3 == 0:
node_xyz[pos[right_node_array],2]=node_xyz[pos[right_node_array],2]//2
if d%3 == 1:
node_xyz[pos[right_node_array],1]=node_xyz[pos[right_node_array],1]//2
if d%3 == 2:
node_xyz[pos[right_node_array],0]=node_xyz[pos[right_node_array],0]//2
# depth_CT[pos[trans_idx_array]] = d
# depth_CT[pos[left_node_array]], depth_CT[pos[right_node_array]] = d, d
# end of information collection
# valT, morton code for the next depth
valT = (val >> 1)[idxT_array]
# num of leaf nodes for next level
N_T=N
N=N-comb_idx_array.shape[0]
# move pos,w of high-freq nodes in the end
# pos: 0, 1, 2, 3
# posT: 0, 1, 3, *
# posT: 0, 1, 3, 2
# transpose
N_idx_array=np.arange(N_T, N, -1)-NN-1
wT[N_idx_array]=wT[np.where(maskT==True)[0]]
posT[N_idx_array]=pos[comb_idx_array+1]
# move transposed pos,w of high-freq nodes in the end
pos[N:S] = posT[N:S]
w[N:S] = wT[N:S]
val, valT = valT, val
pos, posT = posT, pos
w, wT = wT, w
return C
def get_RAHT_tree(inV, depth):
'''
Parameters
----------
inV : point cloud geometry(pre-voxlized and deduplicated)
depth : depth level of geometry(octree)
Returns
-------
res : tree without low- and high-freq coeffs
'''
# N,NN number of points
# K, dims (3) of geometry
N, _ = inV.shape
NN = N
depth *= 3
wT = np.zeros((N, ))
valT = np.zeros((N, )).astype(np.uint64)
posT = np.zeros((N, )).astype(np.uint64)
# morton code and weight for each depth level
iVAL = np.zeros((depth, N)).astype(np.uint64)
iW = np.zeros((depth, N))
# M, num of nodes for current depth level
M = N
# num of nodes for each depth level
iM = np.zeros((depth, )).astype(np.uint64)
w, val, reord = copyAsort(inV)
pos = np.arange(N).astype(np.uint64)
# construct RAHT tree from bottom to top, similar to RAHT encoding
# obtain iVAL, iW, iM for RAHT decoding
for d in range(depth):
iVAL[d,:M] = val[:M]
iW[d,:M] = w[:M]
iM[d]= M
M = 0
S = N
temp=val.astype(np.uint64)&0xFFFFFFFFFFFFFFFE
mask=temp[:-1]==temp[1:]
mask=np.concatenate((mask,[False]))
comb_idx_array=np.where(mask==True)[0]
trans_idx_array=np.where(mask==False)[0]
trans_idx_array=np.setdiff1d(trans_idx_array, comb_idx_array+1)
idxT_array=np.setdiff1d(np.arange(S), comb_idx_array+1)
maskT=mask[idxT_array]
wT[np.where(maskT==False)[0]] = w[trans_idx_array]
posT[np.where(maskT==False)[0]] = pos[trans_idx_array]
wT[np.where(maskT==True)[0]] = w[comb_idx_array]+w[comb_idx_array+1]
posT[np.where(maskT==True)[0]] = pos[comb_idx_array]
valT = (val >> 1)[idxT_array]
N_T=N
N=N-comb_idx_array.shape[0]
M=N
N_idx_array=np.arange(N_T, N, -1)-NN-1
wT[N_idx_array]=wT[np.where(maskT==True)[0]]
posT[N_idx_array]=pos[comb_idx_array+1]
pos[N:S] = posT[N:S]
w[N:S] = wT[N:S]
val, valT = valT, val
pos, posT = posT, pos
w, wT = wT, w
# input attributes, morton sorted attributes, coeffs
# inC, C, CT
# inC and C are connected by reorder
# C and CT are connected by pos
res = {'reord':reord,
'pos':pos,
'iVAL':iVAL,
'iW':iW,
'iM':iM,
}
return res
def inv_haar3D(inV, inCT, depth):
'''
Parameters
----------
inV : point cloud geometry(pre-voxlized and deduplicated)
inCT : transformed coeffs (high-freq coeffs)
depth : depth level of geometry(octree)
Returns
-------
res : rec attributes
'''
# N,NN number of points
# K, dims (3) of geometry
N, K = inCT.shape
NN = N
depth *= 3
CT = np.zeros((N, K))
C = np.zeros((N, K))
outC = np.zeros((N, K))
res_tree = get_RAHT_tree(inV, depth)
reord, pos, iVAL, iW, iM = \
res_tree['reord'], res_tree['pos'], res_tree['iVAL'], res_tree['iW'], res_tree['iM']
CT = inCT[pos]
C = np.zeros(CT.shape)
# RAHT decoding from top to bottom
d = depth
while d:
d = d-1
S = iM[d]
M = iM[d-1] if d else NN
val, w = iVAL[d, :int(S)], iW[d, :int(S)]
M = 0
N = S
# get idx, similar to encoding
temp=val.astype(np.uint64)&0xFFFFFFFFFFFFFFFE
mask=temp[:-1]==temp[1:]
mask=np.concatenate((mask,[False]))
comb_idx_array=np.where(mask==True)[0]
trans_idx_array=np.where(mask==False)[0]
trans_idx_array=np.setdiff1d(trans_idx_array, comb_idx_array+1)
idxT_array=np.setdiff1d(np.arange(S), comb_idx_array+1)
maskT=mask[idxT_array.astype(int)]
# transmit low-freq
C[trans_idx_array] = CT[np.where(maskT==False)[0]]
# decode low_freq and high_freq to two low_freq coeffs
# N_idx_array, idx of high_freq
N_T=N
N=N-comb_idx_array.shape[0]
N_idx_array=np.arange(N_T, N, -1)-NN-1
left_node_array, right_node_array = comb_idx_array, comb_idx_array+1
a = np.sqrt((w[left_node_array])+(w[right_node_array]))
C[left_node_array], C[right_node_array] = itransform_batched(np.sqrt((w[left_node_array]))/a,
np.sqrt((w[right_node_array]))/a,
CT[np.where(maskT==True)[0]][:,None],
CT[N_idx_array.astype(int)][:,None])
CT[:S] = C[:S]
outC[reord] = C
return outC
def inv_haar3D_torch(inCT, depth, res_tree):
'''
Parameters
----------
inV : point cloud geometry(pre-voxlized and deduplicated)
inCT : transformed coeffs (high-freq coeffs)
depth : depth level of geometry(octree)
Returns
-------
res : rec attributes
'''
# N,NN number of points
# K, dims (3) of geometry
N, K = inCT.shape
NN = N