-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathclean_up.cu
More file actions
1293 lines (1136 loc) · 42.5 KB
/
clean_up.cu
File metadata and controls
1293 lines (1136 loc) · 42.5 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
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
#include "cumesh.h"
#include "dtypes.cuh"
#include "shared.h"
#include <cub/cub.cuh>
#if defined(CUDART_VERSION) && (CUDART_VERSION < 12040)
#include <thrust/sort.h>
#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
#endif
namespace cumesh {
#if defined(CUDART_VERSION) && (CUDART_VERSION < 12040)
// Marks faces as 1 (keep) or 0 (remove) by comparing adjacent sorted faces
__global__ void mark_duplicates_from_indices_kernel(
const int* sorted_indices,
const int3* faces,
uint8_t* mask_original,
int n
) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= n) return;
int current_original_idx = sorted_indices[idx];
// The first element in the sorted list is always unique
if (idx == 0) {
mask_original[current_original_idx] = 1;
return;
}
// Compare with the previous element in the sorted list
int prev_original_idx = sorted_indices[idx - 1];
int3 curr_f = faces[current_original_idx];
int3 prev_f = faces[prev_original_idx];
// If identical to previous, it's a duplicate -> mark 0 (remove)
// Otherwise -> mark 1 (keep)
bool is_duplicate = (curr_f.x == prev_f.x && curr_f.y == prev_f.y && curr_f.z == prev_f.z);
mask_original[current_original_idx] = is_duplicate ? 0 : 1;
}
// Comparator for Thrust to sort indices based on face values
struct FaceComparator {
const int3* faces;
FaceComparator(const int3* f) : faces(f) {}
__device__ bool operator()(int i, int j) const {
const int3& a = faces[i];
const int3& b = faces[j];
if (a.x != b.x) return a.x < b.x;
if (a.y != b.y) return a.y < b.y;
return a.z < b.z;
}
};
#endif
static __global__ void copy_vec3f_to_float3_kernel(
const Vec3f* vec3f,
const size_t N,
float3* output
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= N) return;
output[tid] = make_float3(vec3f[tid].x, vec3f[tid].y, vec3f[tid].z);
}
template<typename T, typename U>
static __global__ void copy_T_to_T3_kernel(
const T* input,
const size_t N,
U* output
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= N) return;
output[tid] = { input[3 * tid], input[3 * tid + 1], input[3 * tid + 2] };
}
void CuMesh::remove_faces(torch::Tensor& face_mask) {
size_t F = this->faces.size;
size_t temp_storage_bytes = 0;
int *cu_new_num_faces;
int3 *cu_new_faces;
CUDA_CHECK(cudaMalloc(&cu_new_num_faces, sizeof(int)));
CUDA_CHECK(cudaMalloc(&cu_new_faces, F * sizeof(int3)));
CUDA_CHECK(cub::DeviceSelect::Flagged(
nullptr, temp_storage_bytes,
this->faces.ptr, face_mask.data_ptr<bool>(), cu_new_faces, cu_new_num_faces,
F
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSelect::Flagged(
this->cub_temp_storage.ptr, temp_storage_bytes,
this->faces.ptr, face_mask.data_ptr<bool>(), cu_new_faces, cu_new_num_faces,
F
));
int new_num_faces;
CUDA_CHECK(cudaMemcpy(&new_num_faces, cu_new_num_faces, sizeof(int), cudaMemcpyDeviceToHost));
this->faces.resize(new_num_faces);
CUDA_CHECK(cudaMemcpy(this->faces.ptr, cu_new_faces, new_num_faces * sizeof(int3), cudaMemcpyDeviceToDevice));
CUDA_CHECK(cudaFree(cu_new_num_faces));
CUDA_CHECK(cudaFree(cu_new_faces));
this->remove_unreferenced_vertices();
}
void CuMesh::_remove_faces(uint8_t* face_mask) {
size_t F = this->faces.size;
size_t temp_storage_bytes = 0;
int *cu_new_num_faces;
int3 *cu_new_faces;
CUDA_CHECK(cudaMalloc(&cu_new_num_faces, sizeof(int)));
CUDA_CHECK(cudaMalloc(&cu_new_faces, F * sizeof(int3)));
CUDA_CHECK(cub::DeviceSelect::Flagged(
nullptr, temp_storage_bytes,
this->faces.ptr, face_mask, cu_new_faces, cu_new_num_faces,
F
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSelect::Flagged(
this->cub_temp_storage.ptr, temp_storage_bytes,
this->faces.ptr, face_mask, cu_new_faces, cu_new_num_faces,
F
));
int new_num_faces;
CUDA_CHECK(cudaMemcpy(&new_num_faces, cu_new_num_faces, sizeof(int), cudaMemcpyDeviceToHost));
this->faces.resize(new_num_faces);
CUDA_CHECK(cudaMemcpy(this->faces.ptr, cu_new_faces, new_num_faces * sizeof(int3), cudaMemcpyDeviceToDevice));
CUDA_CHECK(cudaFree(cu_new_num_faces));
CUDA_CHECK(cudaFree(cu_new_faces));
this->remove_unreferenced_vertices();
}
static __global__ void set_vertex_is_referenced(
const int3* faces,
const size_t F,
int* vertex_is_referenced
) {
const int fid = blockIdx.x * blockDim.x + threadIdx.x;
if (fid >= F) return;
int3 face = faces[fid];
vertex_is_referenced[face.x] = 1;
vertex_is_referenced[face.y] = 1;
vertex_is_referenced[face.z] = 1;
}
static __global__ void compress_vertices_kernel(
const int* vertices_map,
const float3* old_vertices,
const int V,
float3* new_vertices
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= V) return;
int new_id = vertices_map[tid];
int is_kept = vertices_map[tid + 1] == new_id + 1;
if (is_kept) {
new_vertices[new_id] = old_vertices[tid];
}
}
static __global__ void remap_faces_kernel(
const int* vertices_map,
const int F,
int3* faces
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= F) return;
faces[tid].x = vertices_map[faces[tid].x];
faces[tid].y = vertices_map[faces[tid].y];
faces[tid].z = vertices_map[faces[tid].z];
}
void CuMesh::remove_unreferenced_vertices() {
size_t V = this->vertices.size;
size_t F = this->faces.size;
// Mark referenced vertices
int* cu_vertex_is_referenced;
CUDA_CHECK(cudaMalloc(&cu_vertex_is_referenced, (V+1) * sizeof(int)));
CUDA_CHECK(cudaMemset(cu_vertex_is_referenced, 0, (V+1) * sizeof(int)));
set_vertex_is_referenced<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->faces.ptr,
F,
cu_vertex_is_referenced
);
CUDA_CHECK(cudaGetLastError());
// Get vertices map
size_t temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceScan::ExclusiveSum(
nullptr, temp_storage_bytes,
cu_vertex_is_referenced,cu_vertex_is_referenced, V+1
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceScan::ExclusiveSum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_vertex_is_referenced,cu_vertex_is_referenced, V+1
));
int new_num_vertices;
CUDA_CHECK(cudaMemcpy(&new_num_vertices, cu_vertex_is_referenced + V, sizeof(int), cudaMemcpyDeviceToHost));
// Compress vertices
this->temp_storage.resize(new_num_vertices * sizeof(float3));
compress_vertices_kernel<<<(V+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_vertex_is_referenced,
this->vertices.ptr,
V,
reinterpret_cast<float3*>(this->temp_storage.ptr)
);
CUDA_CHECK(cudaGetLastError());
swap_buffers(this->temp_storage, this->vertices);
// Update faces
remap_faces_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_vertex_is_referenced,
F,
this->faces.ptr
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_vertex_is_referenced));
// Delete all cached info since mesh has changed
this->clear_cache();
}
static __global__ void sort_faces_kernel(
int3* faces,
const size_t F
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= F) return;
int3 face = faces[tid];
int tmp;
// bubble sort 3 elements (x, y, z)
if (face.x > face.y) { tmp = face.x; face.x = face.y; face.y = tmp; }
if (face.y > face.z) { tmp = face.y; face.y = face.z; face.z = tmp; }
if (face.x > face.y) { tmp = face.x; face.x = face.y; face.y = tmp; }
faces[tid] = face;
}
static __global__ void select_first_in_each_group_kernel(
const int3* faces,
const size_t F,
uint8_t* face_mask
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= F) return;
if (tid == 0) {
face_mask[tid] = 1;
} else {
int3 face = faces[tid];
int3 prev_face = faces[tid-1];
if (face.x == prev_face.x && face.y == prev_face.y && face.z == prev_face.z) {
face_mask[tid] = 0;
} else {
face_mask[tid] = 1;
}
}
}
struct int3_decomposer
{
__host__ __device__ ::cuda::std::tuple<int&, int&, int&> operator()(int3& key) const
{
return {key.x, key.y, key.z};
}
};
void CuMesh::remove_duplicate_faces() {
size_t F = this->faces.size;
// Create a temporary sorted copy of faces for duplicate detection
// Do NOT modify the original faces to preserve vertex order and normals
int3 *cu_sorted_faces;
CUDA_CHECK(cudaMalloc(&cu_sorted_faces, F * sizeof(int3)));
CUDA_CHECK(cudaMemcpy(cu_sorted_faces, this->faces.ptr, F * sizeof(int3), cudaMemcpyDeviceToDevice));
// Sort vertices within each face (in the temporary copy)
sort_faces_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_sorted_faces,
F
);
CUDA_CHECK(cudaGetLastError());
#if defined(CUDART_VERSION) && (CUDART_VERSION < 12040)
// CUDA < 12.4: use Thrust implementation
int *cu_sorted_face_indices;
CUDA_CHECK(cudaMalloc(&cu_sorted_face_indices, F * sizeof(int)));
thrust::sequence(thrust::device,
thrust::device_pointer_cast(cu_sorted_face_indices),
thrust::device_pointer_cast(cu_sorted_face_indices + F));
thrust::sort(thrust::device,
thrust::device_pointer_cast(cu_sorted_face_indices),
thrust::device_pointer_cast(cu_sorted_face_indices + F),
FaceComparator(cu_sorted_faces));
uint8_t* cu_face_mask_original;
CUDA_CHECK(cudaMalloc(&cu_face_mask_original, F * sizeof(uint8_t)));
mark_duplicates_from_indices_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_sorted_face_indices,
cu_sorted_faces,
cu_face_mask_original,
(int)F
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_sorted_faces));
CUDA_CHECK(cudaFree(cu_sorted_face_indices));
this->_remove_faces(cu_face_mask_original);
CUDA_CHECK(cudaFree(cu_face_mask_original));
#else
// CUDA >= 12.4: keep existing CUB behavior
size_t temp_storage_bytes = 0;
int *cu_sorted_face_indices;
CUDA_CHECK(cudaMalloc(&cu_sorted_face_indices, F * sizeof(int)));
arange_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(cu_sorted_face_indices, F);
CUDA_CHECK(cudaGetLastError());
int *cu_sorted_indices_output;
int3 *cu_sorted_faces_output;
CUDA_CHECK(cudaMalloc(&cu_sorted_indices_output, F * sizeof(int)));
CUDA_CHECK(cudaMalloc(&cu_sorted_faces_output, F * sizeof(int3)));
CUDA_CHECK(cub::DeviceRadixSort::SortPairs(
nullptr, temp_storage_bytes,
cu_sorted_faces, cu_sorted_faces_output,
cu_sorted_face_indices, cu_sorted_indices_output,
F,
int3_decomposer{}
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceRadixSort::SortPairs(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_sorted_faces, cu_sorted_faces_output,
cu_sorted_face_indices, cu_sorted_indices_output,
F,
int3_decomposer{}
));
CUDA_CHECK(cudaFree(cu_sorted_faces));
CUDA_CHECK(cudaFree(cu_sorted_face_indices));
uint8_t* cu_face_mask_sorted;
CUDA_CHECK(cudaMalloc(&cu_face_mask_sorted, F * sizeof(uint8_t)));
select_first_in_each_group_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_sorted_faces_output,
F,
cu_face_mask_sorted
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_sorted_faces_output));
uint8_t* cu_face_mask_original;
CUDA_CHECK(cudaMalloc(&cu_face_mask_original, F * sizeof(uint8_t)));
scatter_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_sorted_indices_output,
cu_face_mask_sorted,
F,
cu_face_mask_original
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_face_mask_sorted));
CUDA_CHECK(cudaFree(cu_sorted_indices_output));
this->_remove_faces(cu_face_mask_original);
CUDA_CHECK(cudaFree(cu_face_mask_original));
#endif
}
static __global__ void mark_degenerate_faces_kernel(
const float3* vertices,
const int3* faces,
const float abs_thresh,
const float rel_thresh,
const size_t F,
uint8_t* face_mask
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= F) return;
int3 face = faces[tid];
// 1. Check if any vertex is duplicated
if (face.x == face.y || face.y == face.z || face.z == face.x) {
face_mask[tid] = 0;
return;
}
// 2. Check if slim or zero area
Vec3f v0 = Vec3f(vertices[face.x]);
Vec3f v1 = Vec3f(vertices[face.y]);
Vec3f v2 = Vec3f(vertices[face.z]);
Vec3f e0 = v1 - v0;
Vec3f e1 = v2 - v1;
Vec3f e2 = v0 - v2;
float max_edge_len = fmaxf(fmaxf(e0.norm(), e1.norm()), e2.norm());
float area = e0.cross(e1).norm() / 2.0f;
float thresh = fminf(rel_thresh * max_edge_len * max_edge_len, abs_thresh);
if (area < thresh) {
face_mask[tid] = 0;
return;
}
face_mask[tid] = 1;
}
void CuMesh::remove_degenerate_faces(float abs_thresh, float rel_thresh) {
size_t F = this->faces.size;
uint8_t* cu_face_mask;
CUDA_CHECK(cudaMalloc(&cu_face_mask, F * sizeof(uint8_t)));
mark_degenerate_faces_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->vertices.ptr,
this->faces.ptr,
abs_thresh, rel_thresh,
F,
cu_face_mask
);
CUDA_CHECK(cudaGetLastError());
this->_remove_faces(cu_face_mask);
CUDA_CHECK(cudaFree(cu_face_mask));
}
static __global__ void compute_loop_boundary_lengths(
const float3* vertices,
const uint64_t* edges,
const int* loop_boundaries,
const size_t E,
float* loop_boundary_lengths
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= E) return;
uint64_t edge = edges[loop_boundaries[tid]];
int e0 = int(edge & 0xFFFFFFFF);
int e1 = int(edge >> 32);
Vec3f v0 = Vec3f(vertices[e0]);
Vec3f v1 = Vec3f(vertices[e1]);
loop_boundary_lengths[tid] = (v1 - v0).norm();
}
static __global__ void compute_loop_boundary_midpoints(
const float3* vertices,
const uint64_t* edges,
const int* loop_boundaries,
const size_t E,
Vec3f* loop_boundary_midpoints
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= E) return;
uint64_t edge = edges[loop_boundaries[tid]];
int e0 = int(edge & 0xFFFFFFFF);
int e1 = int(edge >> 32);
Vec3f v0 = Vec3f(vertices[e0]);
Vec3f v1 = Vec3f(vertices[e1]);
loop_boundary_midpoints[tid] = (v0 + v1) * 0.5f;
}
static __global__ void connect_new_vertices_kernel(
const uint64_t* edges,
const int* loop_boundaries,
const int* loop_bound_loop_ids,
const size_t L,
const size_t V,
int3* faces
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= L) return;
int loop_id = loop_bound_loop_ids[tid];
int loop_boundary = loop_boundaries[tid];
uint64_t e = edges[loop_boundary];
int e0 = int(e & 0xFFFFFFFF);
int e1 = int(e >> 32);
int new_v_id = loop_id + V;
faces[tid] = {e0, e1, new_v_id};
}
struct LessThanOp {
__device__ bool operator()(float a, float b) const {
return a < b;
}
};
void CuMesh::fill_holes(float max_hole_perimeter) {
if (this->loop_boundaries.is_empty() || this->loop_boundaries_offset.is_empty()) {
this->get_boundary_loops();
}
size_t V = this->vertices.size;
size_t F = this->faces.size;
size_t L = this->num_bound_loops;
size_t E = this->loop_boundaries.size;
// Early return if no boundary loops
if (L == 0 || E == 0) {
return;
}
// Compute loop boundary lengths
float* cu_loop_boundary_lengths;
CUDA_CHECK(cudaMalloc(&cu_loop_boundary_lengths, E * sizeof(float)));
compute_loop_boundary_lengths<<<(E+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->vertices.ptr,
this->edges.ptr,
this->loop_boundaries.ptr,
E,
cu_loop_boundary_lengths
);
CUDA_CHECK(cudaGetLastError());
// Segment sum
size_t temp_storage_bytes = 0;
float *cu_bound_loop_perimeters;
CUDA_CHECK(cudaMalloc(&cu_bound_loop_perimeters, L * sizeof(float)));
CUDA_CHECK(cub::DeviceSegmentedReduce::Sum(
nullptr, temp_storage_bytes,
cu_loop_boundary_lengths, cu_bound_loop_perimeters,
L,
this->loop_boundaries_offset.ptr,
this->loop_boundaries_offset.ptr + 1
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSegmentedReduce::Sum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_loop_boundary_lengths, cu_bound_loop_perimeters,
L,
this->loop_boundaries_offset.ptr,
this->loop_boundaries_offset.ptr + 1
));
CUDA_CHECK(cudaFree(cu_loop_boundary_lengths));
// Mask small loops
uint8_t* cu_bound_loop_mask;
CUDA_CHECK(cudaMalloc(&cu_bound_loop_mask, L * sizeof(uint8_t)));
compare_kernel<<<(L+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_bound_loop_perimeters,
max_hole_perimeter,
L,
LessThanOp(),
cu_bound_loop_mask
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_bound_loop_perimeters));
// Compress bound loops size
int* cu_bound_loops_cnt;
CUDA_CHECK(cudaMalloc(&cu_bound_loops_cnt, L * sizeof(int)));
diff_kernel<<<(L+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->loop_boundaries_offset.ptr,
L,
cu_bound_loops_cnt
);
CUDA_CHECK(cudaGetLastError());
int *cu_new_loop_boundaries_cnt, *cu_new_num_bound_loops;
CUDA_CHECK(cudaMalloc(&cu_new_loop_boundaries_cnt, (L+1) * sizeof(int)));
CUDA_CHECK(cudaMalloc(&cu_new_num_bound_loops, sizeof(int)));
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceSelect::Flagged(
nullptr, temp_storage_bytes,
cu_bound_loops_cnt, cu_bound_loop_mask, cu_new_loop_boundaries_cnt, cu_new_num_bound_loops,
L
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSelect::Flagged(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_bound_loops_cnt, cu_bound_loop_mask, cu_new_loop_boundaries_cnt, cu_new_num_bound_loops,
L
));
int new_num_bound_loops;
CUDA_CHECK(cudaMemcpy(&new_num_bound_loops, cu_new_num_bound_loops, sizeof(int), cudaMemcpyDeviceToHost));
CUDA_CHECK(cudaFree(cu_bound_loops_cnt));
CUDA_CHECK(cudaFree(cu_new_num_bound_loops));
if (new_num_bound_loops == 0) {
CUDA_CHECK(cudaFree(cu_new_loop_boundaries_cnt));
CUDA_CHECK(cudaFree(cu_bound_loop_mask));
return;
}
// Get loop ids of loop boundaries
int* cu_loop_bound_loop_ids;
CUDA_CHECK(cudaMalloc(&cu_loop_bound_loop_ids, E * sizeof(int)));
CUDA_CHECK(cudaMemset(cu_loop_bound_loop_ids, 0, E * sizeof(int)));
if (L > 1) {
set_flag_kernel<<<(L-1+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->loop_boundaries_offset.ptr + 1, L - 1,
cu_loop_bound_loop_ids
);
CUDA_CHECK(cudaGetLastError());
}
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceScan::InclusiveSum(
nullptr, temp_storage_bytes,
cu_loop_bound_loop_ids,cu_loop_bound_loop_ids,
E
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceScan::InclusiveSum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_loop_bound_loop_ids,cu_loop_bound_loop_ids,
E
));
// Mask loop boundaries
uint8_t* cu_loop_boundary_mask;
CUDA_CHECK(cudaMalloc(&cu_loop_boundary_mask, E * sizeof(uint8_t)));
index_kernel<<<(E+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_bound_loop_mask,
cu_loop_bound_loop_ids,
E,
cu_loop_boundary_mask
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_bound_loop_mask));
CUDA_CHECK(cudaFree(cu_loop_bound_loop_ids));
// Compress loop boundaries
int *cu_new_loop_boundaries, *cu_new_num_loop_boundaries;
CUDA_CHECK(cudaMalloc(&cu_new_loop_boundaries, E * sizeof(int)));
CUDA_CHECK(cudaMalloc(&cu_new_num_loop_boundaries, sizeof(int)));
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceSelect::Flagged(
nullptr, temp_storage_bytes,
this->loop_boundaries.ptr, cu_loop_boundary_mask, cu_new_loop_boundaries, cu_new_num_loop_boundaries,
E
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSelect::Flagged(
this->cub_temp_storage.ptr, temp_storage_bytes,
this->loop_boundaries.ptr, cu_loop_boundary_mask, cu_new_loop_boundaries, cu_new_num_loop_boundaries,
E
));
int new_num_loop_boundaries;
CUDA_CHECK(cudaMemcpy(&new_num_loop_boundaries, cu_new_num_loop_boundaries, sizeof(int), cudaMemcpyDeviceToHost));
CUDA_CHECK(cudaFree(cu_new_num_loop_boundaries));
CUDA_CHECK(cudaFree(cu_loop_boundary_mask));
// Reconstruct new bound loops
int* cu_new_loop_boundaries_offset;
CUDA_CHECK(cudaMalloc(&cu_new_loop_boundaries_offset, (new_num_loop_boundaries+1) * sizeof(int)));
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceScan::ExclusiveSum(
nullptr, temp_storage_bytes,
cu_new_loop_boundaries_cnt, cu_new_loop_boundaries_offset,
new_num_bound_loops + 1
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceScan::ExclusiveSum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_new_loop_boundaries_cnt, cu_new_loop_boundaries_offset,
new_num_bound_loops + 1
));
int* cu_new_loop_bound_loop_ids;
CUDA_CHECK(cudaMalloc(&cu_new_loop_bound_loop_ids, new_num_loop_boundaries * sizeof(int)));
CUDA_CHECK(cudaMemset(cu_new_loop_bound_loop_ids, 0, new_num_loop_boundaries * sizeof(int)));
if (new_num_bound_loops > 1) {
set_flag_kernel<<<(new_num_bound_loops-1+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_new_loop_boundaries_offset+1, new_num_bound_loops-1,
cu_new_loop_bound_loop_ids
);
CUDA_CHECK(cudaGetLastError());
}
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceScan::InclusiveSum(
nullptr, temp_storage_bytes,
cu_new_loop_bound_loop_ids,cu_new_loop_bound_loop_ids,
new_num_loop_boundaries
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceScan::InclusiveSum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_new_loop_bound_loop_ids,cu_new_loop_bound_loop_ids,
new_num_loop_boundaries
));
// Calculate new vertex positions as average of loop vertices
Vec3f* cu_new_loop_bound_centers;
CUDA_CHECK(cudaMalloc(&cu_new_loop_bound_centers, new_num_loop_boundaries * sizeof(Vec3f)));
compute_loop_boundary_midpoints<<<(new_num_loop_boundaries+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->vertices.ptr,
this->edges.ptr,
cu_new_loop_boundaries,
new_num_loop_boundaries,
cu_new_loop_bound_centers
);
CUDA_CHECK(cudaGetLastError());
Vec3f* cu_new_vertices;
CUDA_CHECK(cudaMalloc(&cu_new_vertices, new_num_bound_loops * sizeof(Vec3f)));
temp_storage_bytes = 0;
CUDA_CHECK(cub::DeviceSegmentedReduce::Sum(
nullptr, temp_storage_bytes,
cu_new_loop_bound_centers, cu_new_vertices,
new_num_bound_loops,
cu_new_loop_boundaries_offset,
cu_new_loop_boundaries_offset + 1
));
this->cub_temp_storage.resize(temp_storage_bytes);
CUDA_CHECK(cub::DeviceSegmentedReduce::Sum(
this->cub_temp_storage.ptr, temp_storage_bytes,
cu_new_loop_bound_centers, cu_new_vertices,
new_num_bound_loops,
cu_new_loop_boundaries_offset,
cu_new_loop_boundaries_offset + 1
));
CUDA_CHECK(cudaFree(cu_new_loop_bound_centers));
CUDA_CHECK(cudaFree(cu_new_loop_boundaries_offset));
inplace_div_kernel<<<(new_num_bound_loops+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_new_vertices,
cu_new_loop_boundaries_cnt,
new_num_bound_loops
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_new_loop_boundaries_cnt));
// Update mesh
this->vertices.extend(new_num_bound_loops);
this->faces.extend(new_num_loop_boundaries);
copy_vec3f_to_float3_kernel<<<(new_num_bound_loops+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_new_vertices,
new_num_bound_loops,
this->vertices.ptr + V
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_new_vertices));
connect_new_vertices_kernel<<<(new_num_loop_boundaries+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->edges.ptr,
cu_new_loop_boundaries,
cu_new_loop_bound_loop_ids,
new_num_loop_boundaries,
V,
this->faces.ptr + F
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_new_loop_boundaries));
CUDA_CHECK(cudaFree(cu_new_loop_bound_loop_ids));
// Delete all cached info since mesh has changed
this->clear_cache();
}
static __global__ void construct_vertex_adj_pairs_kernel(
const int2* manifold_face_adj,
const int3* faces,
int2* vertex_adj_pairs,
const size_t M
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= M) return;
const int2 adj_faces = manifold_face_adj[tid];
const int3 face1 = faces[adj_faces.x];
const int3 face2 = faces[adj_faces.y];
const int v1[3] = {face1.x, face1.y, face1.z};
int shared_local_indices1[2] = {0, 0};
int shared_local_indices2[2] = {0, 0};
int found_count = 0;
for (int i = 0; i < 3; ++i) {
if (v1[i] == face2.x) {
shared_local_indices1[found_count] = i;
shared_local_indices2[found_count] = 0;
found_count++;
} else if (v1[i] == face2.y) {
shared_local_indices1[found_count] = i;
shared_local_indices2[found_count] = 1;
found_count++;
} else if (v1[i] == face2.z) {
shared_local_indices1[found_count] = i;
shared_local_indices2[found_count] = 2;
found_count++;
}
if (found_count == 2) {
break;
}
}
// Only process if we found exactly 2 shared vertices (valid manifold edge)
if (found_count == 2) {
vertex_adj_pairs[2 * tid + 0] = make_int2(
3 * adj_faces.x + shared_local_indices1[0],
3 * adj_faces.y + shared_local_indices2[0]
);
vertex_adj_pairs[2 * tid + 1] = make_int2(
3 * adj_faces.x + shared_local_indices1[1],
3 * adj_faces.y + shared_local_indices2[1]
);
} else {
// Invalid edge, set to identity mapping
vertex_adj_pairs[2 * tid + 0] = make_int2(3 * adj_faces.x, 3 * adj_faces.x);
vertex_adj_pairs[2 * tid + 1] = make_int2(3 * adj_faces.y, 3 * adj_faces.y);
}
}
static __global__ void index_vertice_kernel(
const int* vertex_ids,
const int3* faces,
const float3* vertices,
const size_t V,
float3* new_vertices
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= V) return;
const int vid = vertex_ids[tid];
const int3 face = faces[vid / 3];
const int f[3] = {face.x, face.y, face.z};
new_vertices[tid] = vertices[f[vid % 3]];
}
void CuMesh::repair_non_manifold_edges(){
// Always recompute manifold_face_adj to ensure it's up to date
// especially after operations like simplify() that modify the mesh
this->get_manifold_face_adjacency();
size_t F = this->faces.size;
size_t M = this->manifold_face_adj.size;
// Construct vertex adjacency pairs with manifold edges
int2* cu_vertex_adj_pairs;
CUDA_CHECK(cudaMalloc(&cu_vertex_adj_pairs, 2*M*sizeof(int2)));
construct_vertex_adj_pairs_kernel<<<(M+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->manifold_face_adj.ptr,
this->faces.ptr,
cu_vertex_adj_pairs,
M
);
CUDA_CHECK(cudaGetLastError());
// Iterative Hook and Compress
int* cu_vertex_ids;
CUDA_CHECK(cudaMalloc(&cu_vertex_ids, 3 * F * sizeof(int)));
arange_kernel<<<(3*F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(cu_vertex_ids, 3 * F);
CUDA_CHECK(cudaGetLastError());
int* cu_end_flag; int h_end_flag;
CUDA_CHECK(cudaMalloc(&cu_end_flag, sizeof(int)));
do {
h_end_flag = 1;
CUDA_CHECK(cudaMemcpy(cu_end_flag, &h_end_flag, sizeof(int), cudaMemcpyHostToDevice));
// Hook
hook_edges_kernel<<<(2*M+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_vertex_adj_pairs,
2 * M,
cu_vertex_ids,
cu_end_flag
);
CUDA_CHECK(cudaGetLastError());
// Compress
compress_components_kernel<<<(3*F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_vertex_ids,
3 * F
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaMemcpy(&h_end_flag, cu_end_flag, sizeof(int), cudaMemcpyDeviceToHost));
} while (h_end_flag == 0);
CUDA_CHECK(cudaFree(cu_end_flag));
CUDA_CHECK(cudaFree(cu_vertex_adj_pairs));
// Construct new faces
int* cu_new_vertices_ids;
CUDA_CHECK(cudaMalloc(&cu_new_vertices_ids, 3 * F * sizeof(int)));
int new_V = compress_ids(cu_vertex_ids, 3 * F, this->cub_temp_storage, cu_new_vertices_ids);
float3* cu_new_vertices;
CUDA_CHECK(cudaMalloc(&cu_new_vertices, new_V * sizeof(float3)));
index_vertice_kernel<<<(new_V+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
cu_new_vertices_ids,
this->faces.ptr,
this->vertices.ptr,
new_V,
cu_new_vertices
);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_new_vertices_ids));
this->vertices.resize(new_V);
CUDA_CHECK(cudaMemcpy(this->vertices.ptr, cu_new_vertices, new_V * sizeof(float3), cudaMemcpyDeviceToDevice));
CUDA_CHECK(cudaFree(cu_new_vertices));
this->faces.resize(F);
copy_T_to_T3_kernel<<<(F+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(cu_vertex_ids, F, this->faces.ptr);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaFree(cu_vertex_ids));
// Delete all cached info since mesh has changed
this->clear_cache();
}
/**
* Mark faces to remove for non-manifold edges
* For each non-manifold edge (shared by >2 faces), only keep the first 2 faces
*
* @param edge2face: edge to face adjacency
* @param edge2face_offset: edge to face adjacency offset
* @param edge2face_cnt: number of faces per edge
* @param E: number of edges
* @param face_keep_mask: output mask (1 = keep, 0 = remove)
*/
static __global__ void mark_non_manifold_faces_kernel(
const int* edge2face,
const int* edge2face_offset,
const int* edge2face_cnt,
const size_t E,
uint8_t* face_keep_mask
) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid >= E) return;
// Only process non-manifold edges (cnt > 2)
int cnt = edge2face_cnt[tid];
if (cnt <= 2) return;
// Mark faces beyond the first 2 for removal
int start = edge2face_offset[tid];
for (int i = 2; i < cnt; i++) {
int face_idx = edge2face[start + i];
face_keep_mask[face_idx] = 0;
}
}
void CuMesh::remove_non_manifold_faces() {
// Get edge-face adjacency information
if (this->edge2face.is_empty() || this->edge2face_offset.is_empty()) {
this->get_edge_face_adjacency();
}
size_t F = this->faces.size;
size_t E = this->edges.size;
if (F == 0 || E == 0) return;
// Initialize face mask (1 = keep all faces initially)
uint8_t* cu_face_keep_mask;
CUDA_CHECK(cudaMalloc(&cu_face_keep_mask, F * sizeof(uint8_t)));
CUDA_CHECK(cudaMemset(cu_face_keep_mask, 1, F * sizeof(uint8_t)));
// Mark faces on non-manifold edges for removal
mark_non_manifold_faces_kernel<<<(E+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(
this->edge2face.ptr,
this->edge2face_offset.ptr,
this->edge2face_cnt.ptr,
E,
cu_face_keep_mask
);
CUDA_CHECK(cudaGetLastError());
// Remove marked faces
this->_remove_faces(cu_face_keep_mask);
CUDA_CHECK(cudaFree(cu_face_keep_mask));
// Clear cache since mesh has changed
this->clear_cache();
}
struct GreaterThanOrEqualToOp {
__device__ __forceinline__ bool operator()(const float& a, const float& b) const {
return a >= b;
}
};
void CuMesh::remove_small_connected_components(float min_area) {
if (this->conn_comp_ids.is_empty()) {
this->get_connected_components();
}
if (this->face_areas.is_empty()) {
this->compute_face_areas();
}
size_t F = this->faces.size;
if (F == 0) return;