-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointsRefine.py
658 lines (602 loc) · 30.2 KB
/
pointsRefine.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
# -%%
import matplotlib.pyplot as plt
import numpy as np
import open3d as o3d
import trimesh as tm
import os
from pathlib import Path
import subprocess
import convexPoints2HRepresentation
import torch
debug = True
pfad = "/files/Code/convexNeuralVolumeData"
def loadPoints(x_block,y_block,z_block):
my_file = Path(pfad+"/blocks/{}x{}y{}z.npy".format(x_block,y_block,z_block))
if my_file.is_file():
points = np.load(my_file)
return points
def loadEmptyPoints(x_block,y_block,z_block):
my_file = Path(pfad+"/emptyBlocks/e_{}x{}y{}z.npy".format(x_block,y_block,z_block))
if my_file.is_file():
points = np.load(my_file)
return points
def checkEmptyPoints(x_block,y_block,z_block):
my_file = Path(pfad+"/emptyBlocks/e_{}x{}y{}z.npy".format(x_block,y_block,z_block))
if my_file.is_file():
return True
return False
def appendPoints(x_block,y_block,z_block, points):
my_file = Path(pfad+"/blocks/{}x{}y{}z.npy".format(x_block,y_block,z_block))
if my_file.is_file():
#load existing Points and append new ones
oldpoints = np.load(my_file)
np.save(my_file,np.concatenate([oldpoints,points],0))
else:
np.save(my_file,points)
import os
if len(os.listdir(pfad+"/blocks")) == 0:
print("creating Blocks")
import laspy
with laspy.open(pfad+"/out_final_resample_first_return_only.las") as input_las:
for i, points in enumerate(input_las.chunk_iterator(100000)):
print("points",i*100000, "to", (i+1)*100000, "of", input_las.header.point_records_count)
intensity = np.array(points.intensity)
pointa = np.concatenate([np.array([points.X, points.Y, points.Z]).T/100.,
np.array([points.red, points.green, points.blue]).T/float(2**16)],1)[intensity>10000]
pointDicts = {}
for p in pointa:
block = (int(p[0]//35),int(p[1]//35),int(p[2]//35))
if block in pointDicts:
pointDicts[block].append(p)
else:
pointDicts[block] = [p]
for key in pointDicts.keys():
appendPoints(key[0],key[1],key[2], np.stack(pointDicts[key],0))
heights = {}
minBound__ = np.ones(3) * 9999999
maxBound__ = np.ones(3) * 0
for name in os.listdir(pfad+"/blocks"):
x = int(name.split("x")[0])
y = int(name.split("x")[1].split("y")[0])
z = int(name.split("x")[1].split("y")[1].split("z")[0])
if x < minBound__[0]:
minBound__[0] = x
if y < minBound__[1]:
minBound__[1] = y
if z < minBound__[2]:
minBound__[2] = z
if x > maxBound__[0]:
maxBound__[0] = x
if y > maxBound__[1]:
maxBound__[1] = y
if z > maxBound__[2]:
maxBound__[2] = z
if (x,y) in heights:
heights[(x,y)] = [min(z,heights[(x,y)][0]),max(z,heights[(x,y)][1])]
else:
heights[(x,y)] = [z,z]
np.save(pfad+"/heights.npy", np.array(list(heights.keys())))
np.save(pfad+"/heightsValues.npy", np.array(list(heights.values())))
def appendEmptyPoints(x_block,y_block,z_block, points):
my_file = Path(pfad+"/emptyBlocks/e_{}x{}y{}z.npy".format(x_block,y_block,z_block))
if my_file.is_file():
#load existing Points and append new ones
oldpoints = np.load(my_file)
np.save(my_file,np.concatenate([oldpoints,points],0))
else:
np.save(my_file,points)
def createVoxelPointsXYDict(voxels):
idx1 = np.argsort(voxels[:,2])
idx2 = np.argsort(voxels[idx1][:,1],kind='mergesort')
idx3 = np.argsort(voxels[idx1][idx2][:,0],kind='mergesort')
voxels=voxels[idx1][idx2][idx3]
voxelIdx, firstIndex, counts = np.unique(voxels[:,:2],axis=0, return_index=True, return_counts=True)
voxelmap = {}
index = 0
for x in range(int(voxelIdx.max(axis=0)[0]+1)):
row = {}
voxelmap[x] = row
for y in range(int(voxelIdx.max(axis=0)[1]+1)):
column = {"min":0,"max":0,"voxels":[]}
row[y] = column
if index == len(voxelIdx):
continue
if voxelIdx[index][0] == x and voxelIdx[index][1] == y:
minZ = 999999
maxZ = 0
for voIdx in range(counts[index]):
column["voxels"].append(voxels[firstIndex[index] + voIdx])
if column["voxels"][-1][2] < minZ:
minZ = column["voxels"][-1][2]
if column["voxels"][-1][2] > maxZ:
maxZ = column["voxels"][-1][2]
column["max"] = maxZ
column["min"] = minZ
index += 1
return voxelmap, voxelIdx
def fillVoxelColumns(voxelmap, voxelIdx):
twoDMap = np.zeros(voxelIdx.max(axis=0)+1)
twoDMap[np.swapaxes(voxelIdx,1,0)[0],np.swapaxes(voxelIdx,1,0)[1]] += 1
fillstart = np.stack((1-twoDMap).nonzero(),-1)
#create fillColumns
newVoxels = []
x_max, y_max = voxelIdx.max(axis=0)
notfilledyet = []
tempFillmap = {}
for i in range(len(fillstart)):
x,y = fillstart[i]
#check n8 neighbourhood
z_values = []
for x_check in [max(0,x-1),x,min(x_max,x+1)]:
for y_check in [max(0,y-1),y,min(y_max,y+1)]:
if len(voxelmap[x_check][y_check]["voxels"]) > 0:
for voxel in voxelmap[x_check][y_check]["voxels"]:
if voxel[2] not in z_values:
z_values.append(voxel[2])
for z_value in z_values:
newVoxels.append([x,y,z_value])
if (x,y) in tempFillmap:
tempFillmap[(x,y)].append(z_value)
else:
tempFillmap[(x,y)] = [z_value]
if len(z_values) == 0:
notfilledyet.append([x,y])
#fill columns again:
for z in range(3):
for coord in notfilledyet:
if (coord[0],coord[1]) not in tempFillmap:
z_vals = []
for x_check in [max(0,coord[0]-1),coord[0],min(x_max,coord[0]+1)]:
for y_check in [max(0,coord[1]-1),coord[1],min(y_max,coord[1]+1)]:
if (x_check,y_check) in tempFillmap:
for z_value in tempFillmap[(x_check,y_check)]:
if z_value not in z_vals:
z_vals.append(z_value)
newVoxels.append([coord[0],coord[1],z_value])
tempFillmap[(coord[0],coord[1])] = z_vals
newVoxels = np.array(newVoxels)
return newVoxels
def fillVoxelMinimalDown(voxelmap, voxelIdx):
x_max, y_max = voxelIdx.max(axis=0)
newVoxels = []
for x in voxelmap.keys():
for y in voxelmap[0].keys():
if len(voxelmap[x][y]["voxels"]) > 0:
#we have voxels (so it will get filled by "fillColumns")
z_start = voxelmap[x][y]["min"]
#now do like the inverse column fill
#check neighbourhood
z_min = z_start
rangeNr = 11
for x_check in range(rangeNr):#[max(0,x-1),x,min(x_max,x+1)]:
for y_check in range(rangeNr):#[max(0,y-1),y,min(y_max,y+1)]:
x_check = min(max(0,x-x_check+rangeNr//2),x_max)
y_check = min(max(0,y-y_check+rangeNr//2),y_max)
if voxelmap[x_check][y_check]["min"] < z_min:
z_min = voxelmap[x_check][y_check]["min"]
for i in range(int(z_start-z_min)):
newVoxels.append([x,y,z_start-1-i])
newVoxels = np.array(newVoxels)
return newVoxels
def translateOldVoxels2New(oldNewVoxels):
if len(oldNewVoxels.shape) == 1:
oldNewVoxels = oldNewVoxels[None,:]
newVoxels = np.zeros((oldNewVoxels.shape[0]*8,oldNewVoxels.shape[1]))
for i in range(len(oldNewVoxels)):
x_old,y_old,z_old = oldNewVoxels[i]
newVoxels[i*8] = np.array((x_old*2,y_old*2,z_old*2))
newVoxels[i*8+1] = np.array((x_old*2,y_old*2,z_old*2+1))
newVoxels[i*8+2] = np.array((x_old*2,y_old*2+1,z_old*2))
newVoxels[i*8+3] = np.array((x_old*2,y_old*2+1,z_old*2+1))
newVoxels[i*8+4] = np.array((x_old*2+1,y_old*2+1,z_old*2))
newVoxels[i*8+5] = np.array((x_old*2+1,y_old*2+1,z_old*2+1))
newVoxels[i*8+6] = np.array((x_old*2+1,y_old*2,z_old*2))
newVoxels[i*8+7] = np.array((x_old*2+1,y_old*2,z_old*2+1))
return newVoxels
def fillVoxelsFurther(newVoxels, oldvoxels):
'''Fills a neighbour Voxel if there is a filled neighbour in the old scale "newFillMap" and a neighbour in the new map'''
secondVoxels = []
x_max, y_max = voxelIdx.max(axis=0)
for i in range(len(newVoxels)):
x,y,z = newVoxels[i,:]
for x_check in [max(0,x-1),x,min(x_max,x+1)]:
for y_check in [max(0,y-1),y,min(y_max,y+1)]:
if len(voxelmap[x_check][y_check]["voxels"]) == 0: #not self occupied
hasNeighbourInNewMap = False
hasNeighbourInOldMap = False
nrOfNeighbours = 0
for x_check2 in [max(0,x_check-1),x,min(x_max,x_check+1)]:
for y_check2 in [max(0,y_check-1),y,min(y_max,y_check+1)]:
if len(voxelmap[x_check2][y_check2]["voxels"]) > 0: #has a real neighbour
for vx in voxelmap[x_check2][y_check2]["voxels"]:
if vx[2] == z:
secondVoxels.append([x_check,y_check,z])
nrOfNeighbours += 1
return secondVoxels
def fillVoxelsFurther2(newVoxels):
'''Fills a neighbour Voxel if there are at least 4 neighbours present'''
secondVoxels = []
voxelmap,voxelIdx = createVoxelPointsXYDict(newVoxels)
x_max, y_max = voxelIdx.max(axis=0)
for i in range(len(newVoxels)):
x,y,z = newVoxels[i,:]
for x_check in [max(0,x-1),x,min(x_max,x+1)]:
for y_check in [max(0,y-1),y,min(y_max,y+1)]:
if len(voxelmap[x_check][y_check]["voxels"]) == 0: #not self occupied
#start checking n8 for 4 or more neighbours
nrOfNeighbours = 0
for x_check2 in [max(0,x_check-1),x,min(x_max,x_check+1)]:
for y_check2 in [max(0,y_check-1),y,min(y_max,y_check+1)]:
if len(voxelmap[x_check2][y_check2]["voxels"]) > 0: #has a real neighbour
for vx in voxelmap[x_check2][y_check2]["voxels"]:
if vx[2] == z:
nrOfNeighbours += 1
if nrOfNeighbours > 2:
secondVoxels.append([x_check,y_check,z])
return secondVoxels
def voxelGrid2Voxels(voxelgrid):
voxels = []
for i in voxelgrid.get_voxels():
voxels.append(i.grid_index)
try:
voxels = np.stack(voxels)
except: print("no voxels in voxelgrid!")
return voxels
def voxelGrid2Colors(voxelgrid):
voxels = []
for i in voxelgrid.get_voxels():
voxels.append(i.color)
voxels = np.stack(voxels)
return voxels
def makeVoxelDict(array):
vdict = {}
for i in range(len(array)):
vdict[(array[i][0],array[i][1],array[i][2])] = {"color":[0,0,0]}
return vdict
#meshing and creating surfaceVoxels
def vertexKey(n1,n2):
nbs = [n1,n2]
return tuple(np.array(nbs).mean(axis=0))
def meshIt_(voxelFinal):
vertices = {}
vertexList = []
faces = []
face_normals = []
face_colors = []
surfaceVoxels = {}
for key in voxelFinal:
normalDirection = np.array([0,0,0])
voxelThere = False
surfaceDirections = []
if tuple((key[0]-1,key[1],key[2])) not in voxelFinal:
normalDirection[0] -= 1
voxelThere = True
surfaceDirections.append(tuple((key[0]-1,key[1],key[2])))
if tuple((key[0]+1,key[1],key[2])) not in voxelFinal:
normalDirection[0] += 1
voxelThere = True
surfaceDirections.append(tuple((key[0]+1,key[1],key[2])))
if tuple((key[0],key[1]-1,key[2])) not in voxelFinal:
normalDirection[1] -= 1
voxelThere = True
surfaceDirections.append(tuple((key[0],key[1]-1,key[2])))
if tuple((key[0],key[1]+1,key[2])) not in voxelFinal:
normalDirection[1] += 1
voxelThere = True
surfaceDirections.append(tuple((key[0],key[1]+1,key[2])))
if tuple((key[0],key[1],key[2]-1)) not in voxelFinal:
normalDirection[2] -= 1
voxelThere = True
surfaceDirections.append(tuple((key[0],key[1],key[2]-1)))
if tuple((key[0],key[1],key[2]+1)) not in voxelFinal:
normalDirection[2] += 1
voxelThere = True
surfaceDirections.append(tuple((key[0],key[1],key[2]+1)))
if voxelThere:
if abs(normalDirection).sum() > 0:
surfaceVoxels[key] = {"normal":normalDirection}
selfKey = np.array(key)
for direction in surfaceDirections:
#two triangle-faces per direction
dirKey = np.array(direction)
face_normals.append(dirKey-selfKey)
face_normals.append(dirKey-selfKey)
face_colors.append(voxelFinal[key]["color"])
face_colors.append(voxelFinal[key]["color"])
variableList = []
fixedIdx = -1
fixedDirection = 0
for i in range(3):
if face_normals[-1][i] == 0:
variableList.append(i)
else:
fixedIdx = i
fixedDirection = face_normals[-1][i]
#face1vertices
vtx1nbr = np.array(key)
vtx1nbr[fixedIdx] += fixedDirection
vtx1nbr[variableList[0]] += 1
vtx1nbr[variableList[1]] += 1
vtx2nbr = np.array(key)
vtx2nbr[fixedIdx] += fixedDirection
vtx2nbr[variableList[0]] += -1
vtx2nbr[variableList[1]] += 1
vtx3nbr = np.array(key)
vtx3nbr[fixedIdx] += fixedDirection
vtx3nbr[variableList[0]] += -1
vtx3nbr[variableList[1]] += -1
#face2vertices
Bvtx1nbr = np.array(key)
Bvtx1nbr[fixedIdx] += fixedDirection
Bvtx1nbr[variableList[0]] += -1
Bvtx1nbr[variableList[1]] += -1
Bvtx2nbr = np.array(key)
Bvtx2nbr[fixedIdx] += fixedDirection
Bvtx2nbr[variableList[0]] += 1
Bvtx2nbr[variableList[1]] += -1
Bvtx3nbr = np.array(key)
Bvtx3nbr[fixedIdx] += fixedDirection
Bvtx3nbr[variableList[0]] += 1
Bvtx3nbr[variableList[1]] += 1
vtxIdx = []
#getVertexIdx
for vtxNbr in [vtx1nbr,vtx2nbr,vtx3nbr,Bvtx1nbr,Bvtx2nbr,Bvtx3nbr]:
vtxKey = vertexKey(list(vtxNbr),list(key))
if vtxKey not in vertices:
#create vtx
vertices[vtxKey] = len(vertexList)
vertexList.append(list(vtxKey))
vtxIdx.append(vertices[vtxKey])
#bugfix if face-normals get ignored
if face_normals[-1][0] > 0: #x dir
faces.append([vtxIdx[0],vtxIdx[1],vtxIdx[2]])
faces.append([vtxIdx[3],vtxIdx[4],vtxIdx[5]])
elif face_normals[-1][0] < 0: #x dir
faces.append([vtxIdx[2],vtxIdx[1],vtxIdx[0]])
faces.append([vtxIdx[5],vtxIdx[4],vtxIdx[3]])
elif face_normals[-1][1] < 0: #y dir
faces.append([vtxIdx[0],vtxIdx[1],vtxIdx[2]])
faces.append([vtxIdx[3],vtxIdx[4],vtxIdx[5]])
elif face_normals[-1][1] > 0: #y dir
faces.append([vtxIdx[2],vtxIdx[1],vtxIdx[0]])
faces.append([vtxIdx[5],vtxIdx[4],vtxIdx[3]])
elif face_normals[-1][2] > 0: #z dir
faces.append([vtxIdx[0],vtxIdx[1],vtxIdx[2]])
faces.append([vtxIdx[3],vtxIdx[4],vtxIdx[5]])
elif face_normals[-1][2] < 0: #z dir
faces.append([vtxIdx[2],vtxIdx[1],vtxIdx[0]])
faces.append([vtxIdx[5],vtxIdx[4],vtxIdx[3]])
else:
continue
voxelPoints = np.array(list(surfaceVoxels.keys()))
bounds = voxelPoints.max(axis=0)
voxelNormals = np.zeros_like(voxelPoints)
for i,key in enumerate(surfaceVoxels.keys()):
voxelNormals[i] = surfaceVoxels[key]["normal"]
return vertexList,faces,face_normals,face_colors,surfaceVoxels,bounds
times = 0
cl = None
pcd = None
###############################################
breakAll = False
partSize = 6
# -%%
for x_mid in range(int(maxBound__[0]-minBound__[0])//partSize):
x_mid = x_mid*partSize+int(minBound__[0])+partSize//2
for y_mid in range(int(maxBound__[1]-minBound__[1])//partSize):
y_mid = y_mid*partSize+int(minBound__[1])+partSize//2
if True:
print("Doing ",x_mid,y_mid)
alreadyRefined = False
points = None
for x in range(-partSize//2+x_mid,partSize//2+x_mid):
for y in range(-partSize//2+y_mid,partSize//2+y_mid):
for z in range(int(minBound__[2]),int(maxBound__[2]+1)):
try:
if checkEmptyPoints(x,y,z):
alreadyRefined = True
if alreadyRefined:
continue
points_ = loadPoints(x,y,z)
if points_ is not None:
try:
points = np.concatenate([points,points_],0)
except:
points = points_
except: pass # no points to load here
if points is not None:
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points[:,:3])
pcd.colors = o3d.utility.Vector3dVector(points[:,3:])
cl, ind = pcd.remove_statistical_outlier(nb_neighbors=20,std_ratio=2.0)
cl, ind = cl.remove_radius_outlier(nb_points=2, radius=20.05)
else:
continue
if len(cl.points) == 0:
#no real points make all empty
for x in range(-partSize//2+x_mid,partSize//2+x_mid):
for y in range(-partSize//2+y_mid,partSize//2+y_mid):
for z in range(int(minBound__[2]),int(maxBound__[2]+1)):
points_ = loadPoints(x,y,z)
if points_ is not None:
appendEmptyPoints(x,y,z, np.array([[x*35.,y*35.,z*35.]]))
continue
if alreadyRefined:
continue
voxelSize = 32.0
voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(cl,
voxel_size=voxelSize)
oldVoxels = []
rangemax = 2
for i in range(rangemax):
voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(cl,
voxel_size=voxelSize)
voxels = voxelGrid2Voxels(voxel_grid)
#print(voxels.max(axis=0))
voxelmap,voxelIdx = createVoxelPointsXYDict(voxels)
newVoxels = fillVoxelColumns(voxelmap,voxelIdx)
if len(oldVoxels) > 0:
try:
newVoxels = np.concatenate((newVoxels,oldVoxels))
except:
if len(newVoxels) > len(oldVoxels):
pass
else:
newVoxels = oldVoxels
#secondVoxels = fillVoxelsFurther2(newVoxels)
#newVoxels = np.concatenate((newVoxels,secondVoxels))
newVoxels = np.unique(newVoxels,axis=0)
if i < rangemax-1:
if len(newVoxels) > 0:
newVoxels = translateOldVoxels2New(newVoxels)
oldVoxels = newVoxels
voxelSize = voxelSize * 0.5
voxelSize = voxelSize*2
try:
voxelmap,voxelIdx = createVoxelPointsXYDict(np.concatenate((voxels,newVoxels),0))
except:
if len(voxels) > 0: voxelmap,voxelIdx = createVoxelPointsXYDict(voxels)
if len(newVoxels) > 0: voxelmap,voxelIdx = createVoxelPointsXYDict(newVoxels)
newFill = fillVoxelMinimalDown(voxelmap, voxelIdx)
try:
newVoxels = np.concatenate((voxels,newFill,newVoxels),0)
except:
if len(voxels) > 0 and len(newFill) > 0: newVoxels = np.concatenate((voxels,newFill),0)
elif len(newVoxels) > 0 and len(newFill) > 0: newVoxels = np.concatenate((newFill,newVoxels),0)
elif len(voxels) > 0 and len(newVoxels) > 0: newVoxels = np.concatenate((voxels,newVoxels),0)
elif len(voxels) > 0: newVoxels = voxels
else: pass
try:
voxelFinal = makeVoxelDict(np.concatenate((newVoxels,voxels),0))
except:
if len(voxels) > 0: voxelFinal = makeVoxelDict(voxels)
if len(newVoxels) > 0: voxelFinal = makeVoxelDict(newVoxels)
try:
vertexList,faces,face_normals,face_colors,surfaceVoxels,bounds = meshIt_(voxelFinal)
except:
print("could not mesh this")
#continue
meshedMesh = tm.Trimesh(vertices=vertexList, faces=faces, face_normals=None, vertex_normals=None, face_colors=face_colors, vertex_colors=None, face_attributes=None, vertex_attributes=None, metadata=None, process=True, validate=False, use_embree=True, initial_cache=None, visual=None)
meshedMesh.export(pfad+"/bla.obj")
print("exported")
inputfile = pfad+"/bla.obj"
outputfile = pfad+"/bla_vhacd2.obj"
resolution = 200000 #maximum number of voxels generated during the voxelization stage
depth = 32 #maximum number of clipping stages. During each split stage, all the model parts (with a concavity higher than the user defined threshold) are clipped according the "best" clipping plane 20 1-32
concavity =0.05 #maximum concavity 0.0025 0.0-1.0
planeDownsampling = 2 #controls the granularity of the search for the "best" clipping plane 4 1-16
convexhullDownsampling = 1 #controls the precision of the convex-hull generation process during the clipping plane selection stage 4 1-16
alpha = 0.05 #controls the bias toward clipping along symmetry planes 0.05 0.0-1.0
beta = 0.05 #controls the bias toward clipping along revolution axes 0.05 0.0-1.0
gamma = 0.005 #maximum allowed concavity during the merge stage 0.00125 0.0-1.0
pca = 0 #enable/disable normalizing the mesh before applying the convex decomposition 0 0-1
mode = 0 #voxel-based approximate convex decomposition, 1: tetrahedron-based approximate convex decomposition 0 0-1
maxNumVerticesPerCH = 16 #controls the maximum number of triangles per convex-hull 64 4-1024
minVolumePerCH = 0.000001 #controls the adaptive sampling of the generated convex-hulls 0.0001 0.0-0.01
vhacdPath = pfad+"/v-hacd/src/build/test/testVHACD"
subprocess.call("{} --input '{}' --output '{}' --resolution {} --depth {} --concavity {} --planeDownsampling {} --convexhullDownsampling {} --alpha {} --beta {} --gamma {} --pca {} --mode {} --maxNumVerticesPerCH {} --minVolumePerCH {}".format(vhacdPath,
inputfile,outputfile,resolution,depth,concavity, planeDownsampling,convexhullDownsampling,alpha,beta,gamma,pca,mode,maxNumVerticesPerCH,minVolumePerCH), shell=True, stdout=subprocess.PIPE)
mesh2 = tm.load(pfad+"/bla_vhacd2.obj")
meshes = mesh2.split()
vertexListMinBounds = np.array(vertexList).min(axis=0)
vertexListMaxBounds = np.array(vertexList).max(axis=0)
notEmpty = set(voxelFinal.keys())
empty = set()
for x in range(int((vertexListMaxBounds+vertexListMinBounds)[0])):
for y in range(int((vertexListMaxBounds+vertexListMinBounds)[1])):
for z in range(int((vertexListMaxBounds+vertexListMinBounds)[2])):
if (x,y,z) not in notEmpty:
empty.add((x,y,z))
empty = np.array(list(empty),dtype=np.float)
#bring vertices from grid-coordinates back to pointcloud-coordinates
minBound = np.asarray(cl.points).min(axis=0)
maxBound = np.asarray(cl.points).max(axis=0)
zeroed = np.array(mesh2.vertices)+vertexListMinBounds
if len(empty) > 0:
empty_z = empty+vertexListMinBounds
zeroed = zeroed/(vertexListMaxBounds-vertexListMinBounds)
if len(empty) > 0:
empty_z = empty_z/(vertexListMaxBounds-vertexListMinBounds)
mesh2.vertices = zeroed*(maxBound-minBound)+minBound
if len(empty) > 0:
empty_z = empty_z*(maxBound-minBound)+minBound
if len(meshes) == 0:
continue
meshes = mesh2.split()
points = torch.zeros((len(meshes),16,3))
centers = []
for i in range(len(meshes)):
verticesNr = len(meshes[i].vertices)
points[i][:verticesNr] = torch.tensor(meshes[i].vertices - meshes[i].bounds.mean(axis=0))
Hrep = convexPoints2HRepresentation.step_withoutNaN(convexPoints2HRepresentation.startPlanes, points.cuda())
rHrep = Hrep.clone()
rHrep[:,0]=Hrep[:,3]
rHrep[:,1]=Hrep[:,5]
rHrep[:,3]=Hrep[:,0]
rHrep[:,4]=Hrep[:,1]
rHrep[:,5]= Hrep[:,4]
if debug:
#create points for every mesh
meshesPoints = []
for i,mesh in enumerate(meshes):
pointcloudPoints = mesh.sample(2000)
pointcloudMesh = o3d.geometry.PointCloud(points=o3d.utility.Vector3dVector(pointcloudPoints))
colors = np.ones_like(pointcloudPoints).astype(np.float64)
colors[:,2] = colors[:,2]*np.random.rand()
colors[:,1] = colors[:,1]*np.random.rand()
colors[:,0] = colors[:,0]*np.random.rand()
pointcloudMesh.colors = o3d.utility.Vector3dVector(colors)
meshesPoints.append(pointcloudMesh)
#visualize empty
pointcloudEmpty = o3d.geometry.PointCloud(points=o3d.utility.Vector3dVector(empty_z))
colors = np.ones_like(empty_z).astype(np.float64)
colors[:,2] = colors[:,2]*np.random.rand()
colors[:,1] = colors[:,1]*np.random.rand()
colors[:,0] = colors[:,0]*np.random.rand()
pointcloudEmpty.colors = o3d.utility.Vector3dVector(colors)
import linAlgHelper
#create pointcloud
allpoints = None
for i in range(len(meshes)):
pointstemp = (torch.rand((10000,3))*2-1.)*(meshes[i].bounds[1]-meshes[i].bounds[0])[None,:]# + meshes[i].bounds.mean(axis=0)
boundsTest = linAlgHelper.getPointDistances2PlaneNormal(pointstemp[None,:,:].cuda().double(), Hrep[i][None,:,:].cuda().double())[0]
near = boundsTest>0
completeNear = near.sum(dim=1)==near.shape[1]
pointstemp = pointstemp[completeNear] + meshes[i].bounds.mean(axis=0)
try:
allpoints = torch.cat((allpoints,pointstemp),dim=0)
except:
allpoints = pointstemp
starts = []
for m in meshes:
starts.append(m.bounds.mean(axis=0))
#write empty Blocks (up to 27000 per 30*30*30 volume)
if len(empty) > 0:
pointDicts = {}
for p in empty_z:
block = (int(p[0]//5),int(p[1]//35),int(p[2]//35))
if block in pointDicts:
pointDicts[block].append(p)
else:
pointDicts[block] = [p]
for key in pointDicts.keys():
appendEmptyPoints(key[0],key[1],key[2], np.stack(pointDicts[key],0))
#write startVolumes
for i in range(len(rHrep)):
my_file = Path(pfad+"/neuralVolumes/{}x{}y{}z_{}.npy".format(int(starts[i][0]//35),
int(starts[i][1]//35),
int(starts[i][2]//35),i))
np.save(my_file,np.concatenate([starts[i][None,:],rHrep[i].cpu()],0))
#save refined Points
refPoints = np.concatenate([np.asarray(cl.points),np.asarray(cl.colors)],1)
pointDicts = {}
for p in refPoints:
block = (int(p[0]//35),int(p[1]//35),int(p[2]//35))
if block in pointDicts:
pointDicts[block].append(p)
else:
pointDicts[block] = [p]
for key in pointDicts.keys():
my_file = Path(pfad+"/blocks/{}x{}y{}z.npy".format(key[0],key[1],key[2]))
np.save(my_file,np.stack(pointDicts[key],0))
#- %%