-
Notifications
You must be signed in to change notification settings - Fork 11
/
PointCloudData.cpp
1463 lines (1402 loc) · 47 KB
/
PointCloudData.cpp
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 "stdafx.h"
#include "PointCloudData.h"
PointCloudData::PointCloudData()
{
m_PointSumNumber = 0;
b_OneStepMode = false;
b_AllPoint = false;
}
PointCloudData::~PointCloudData()
{
}
//获取点云数量
int PointCloudData::GetPointSumNumber()
{
return m_PointSumNumber;
}
//设置点云数量
void PointCloudData::SetPointSumNumber(int number)
{
m_PointSumNumber = number;
}
//获取点云数据
PCPoint* PointCloudData::GetPointCloud()
{
return m_PointCloud;
}
//初始化点云数据
void PointCloudData::InitPointCloud(PCPoint* pointCloud)
{
m_PointCloud = (PCPoint*)new PCPoint[m_PointSumNumber];//点云数据
m_OriginPointCloud = (PCPoint*)new PCPoint[m_PointSumNumber];//单位化后的点云数据
for (int num = 0; num < m_PointSumNumber; num++)
{
//初始化点云坐标和颜色
for (int i = 0; i < 3; i++)
{
m_PointCloud[num].m_Coordinate[i] = pointCloud[num].m_Coordinate[i];
m_PointCloud[num].m_Color[i] = pointCloud[num].m_Color[i];
}
m_PointCloud[num].m_ID = num;
}
PointMoveToOrigin(pointCloud);//将模型置于显示中心
for (int num = 0; num < m_PointSumNumber; num++)
{
for (int j = 0; j < 3; j++)
{
m_OriginPointCloud[num].m_Coordinate[j] = pointCloud[num].m_Coordinate[j];
m_OriginPointCloud[num].m_Color[j] = pointCloud[num].m_Color[j];
}
//初始化ID
m_OriginPointCloud[num].m_ID = num;
}
//初始化PCL kd树
cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>());
cloud->width = m_PointSumNumber;//初始化 PCL 点云
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
int cloudCount = 0;
for (int num = 0; num < m_PointSumNumber; num++)
{
cloud->points[cloudCount].x = m_OriginPointCloud[num].m_Coordinate[0];
cloud->points[cloudCount].y = m_OriginPointCloud[num].m_Coordinate[1];
cloud->points[cloudCount].z = m_OriginPointCloud[num].m_Coordinate[2];
cloudCount++;
}
kdtree.setInputCloud(cloud);
}
//获取移动到原点后的点云数据
PCPoint* PointCloudData::GetOriginPointCloud()
{
return m_OriginPointCloud;
}
//从文件中读取点云数据
bool PointCloudData::ReadPointCloudFile(char* fileName)
{
m_MaxX = -10000000.0;//给定它一个非常小的初始值
m_MaxY = -10000000.0;//给定它一个非常小的初始值
m_MaxZ = -10000000.0;//给定它一个非常小的初始值
m_MinX = DBL_MAX;//给定它最大值
m_MinY = DBL_MAX;//给定它最大值
m_MinZ = DBL_MAX;//给定它最大值
char str[256];
int i = 0;
FILE *fp;
PCPoint sumPoint;
PCPoint* tempPointData;//点云数据临时存储器
//获取点云模型点数量
if (fopen_s(&fp, fileName, "r") == 0)
{
while (fscanf_s(fp, "%s", str, 256) != EOF)
{
fscanf_s(fp, "%s", str, 256);
fscanf_s(fp, "%s", str, 256);
m_PointSumNumber++;
}
fclose(fp);
}
//初始化临时点云数据
tempPointData = (PCPoint*) new PCPoint[m_PointSumNumber];
sumPoint.m_Coordinate[0] = 0.0;
sumPoint.m_Coordinate[1] = 0.0;
sumPoint.m_Coordinate[2] = 0.0;
if (fopen_s(&fp, fileName, "r") == 0)
{
while (fscanf_s(fp, "%s", str, 256) != EOF)
{
//将文件中读取数据点的信息存入数组中
double v[3];
int c[3];
int index;
v[2] = (double)atof(str);
fscanf_s(fp, "%s", str, 256);
v[0] = (double)atof(str);
fscanf_s(fp, "%s", str, 256);
v[1] = (double)atof(str);
c[0] = 0.0; c[1] = 0.0; c[2] = 0.0;//设置初始颜色为黑色
index = i++;
for (int j = 0; j<3; j++)//把文件中的点存入数组RangeData中
{
tempPointData[index].m_Coordinate[j] = v[j];
tempPointData[index].m_Color[j] = c[j];
sumPoint.m_Coordinate[j] += v[j];
}
if (v[0] > m_MaxX)
{
m_MaxX = v[0];//得到最大的x坐标值
}
if (v[1] > m_MaxY)
{
m_MaxY = v[1];//得到最大的y坐标值
}
if (v[2] > m_MaxZ)
{
m_MaxZ = v[2];//得到最大的z坐标值
}
if (v[0] < m_MinX)
{
m_MinX = v[0];//得到最小的x坐标值
}
if (v[1] < m_MinY)
{
m_MinY = v[1];//得到最小的y坐标值
}
if (v[2] < m_MinZ)
{
m_MinZ = v[2];//得到最小的z坐标值
}
}
fclose(fp);
//获取模型的中心点
m_MidPoint.m_Coordinate[0] = sumPoint.m_Coordinate[0] / m_PointSumNumber;
m_MidPoint.m_Coordinate[1] = sumPoint.m_Coordinate[1] / m_PointSumNumber;
m_MidPoint.m_Coordinate[2] = sumPoint.m_Coordinate[2] / m_PointSumNumber;
InitPointCloud(tempPointData);//初始化点云数据
delete[] tempPointData;//释放内存
return 1;//文件打得开则返回1
}
else
{
return 0;//文件打不开则返回0
}
}
void PointCloudData::PointMoveToOrigin(PCPoint* & pointCloud)//将模型置于显示中心
{
m_MaxX -= m_MidPoint.m_Coordinate[0];
for (int num = 0; num < m_PointSumNumber; num++)
{
pointCloud[num].m_Coordinate[0] -= m_MidPoint.m_Coordinate[0];
pointCloud[num].m_Coordinate[1] -= m_MidPoint.m_Coordinate[1];
pointCloud[num].m_Coordinate[2] -= m_MidPoint.m_Coordinate[2];
pointCloud[num].m_Coordinate[0] = (pointCloud[num].m_Coordinate[0])
/ (m_MaxX);
pointCloud[num].m_Coordinate[1] = (pointCloud[num].m_Coordinate[1])
/ (m_MaxX);
pointCloud[num].m_Coordinate[2] = (pointCloud[num].m_Coordinate[2])
/ (m_MaxX);
}
}
//带云参数的PCL kdtree k近邻搜索
void PointCloudData::PCLCoefficientsKDtreeNKSearch(PCPoint lpoint, int Knum, pcl::PointCloud<pcl::PointXYZ>::Ptr coeCloud
, pcl::KdTreeFLANN<pcl::PointXYZ> coeKdtree)
{
pcl::PointXYZ searchPoint;//初始化 搜索点
searchPoint.x = lpoint.m_Coordinate[0];
searchPoint.y = lpoint.m_Coordinate[1];
searchPoint.z = lpoint.m_Coordinate[2];
std::vector<int> pointIdxNKNSearch(Knum);
std::vector<float> pointNKNSquaredDistance(Knum);
PCPoint Ptemp;
if (coeKdtree.nearestKSearch(searchPoint, Knum, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
{
for (size_t i = 0; i < pointIdxNKNSearch.size(); ++i)
{
Ptemp.m_Coordinate[0] = coeCloud->points[pointIdxNKNSearch[i]].x;
Ptemp.m_Coordinate[1] = coeCloud->points[pointIdxNKNSearch[i]].y;
Ptemp.m_Coordinate[2] = coeCloud->points[pointIdxNKNSearch[i]].z;
RkNearestPoints.push_back(Ptemp);
}
}
}
//PCL KDtree k近邻搜索
void PointCloudData::PCLKDtreeNKSearch(PCPoint lpoint, int Knum)
{
pcl::PointXYZ searchPoint;//初始化 搜索点
searchPoint.x = lpoint.m_Coordinate[0];
searchPoint.y = lpoint.m_Coordinate[1];
searchPoint.z = lpoint.m_Coordinate[2];
std::vector<int> pointIdxNKNSearch(Knum);
std::vector<float> pointNKNSquaredDistance(Knum);
PCPoint Ptemp;
if (kdtree.nearestKSearch(searchPoint, Knum, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
{
for (size_t i = 0; i < pointIdxNKNSearch.size(); ++i)
{
Ptemp.m_Coordinate[0] = cloud->points[pointIdxNKNSearch[i]].x;
Ptemp.m_Coordinate[1] = cloud->points[pointIdxNKNSearch[i]].y;
Ptemp.m_Coordinate[2] = cloud->points[pointIdxNKNSearch[i]].z;
RkNearestPoints.push_back(Ptemp);
}
}
}
//边界检测
bool PointCloudData::BoundaryCheck(PCPoint lpoint)
{
double array[BCNum][3], Y[3];
double array_ML[BCNum][3];
double A, B, C;
A = B = C = 0.0;
ZeroMemory(array, sizeof(array));
ZeroMemory(Y, sizeof(Y));
PCLKDtreeNKSearch(lpoint, BCNum);
for (int i = 0; i < BCNum; i++)
{
array_ML[i][0] = array[i][0] = RkNearestPoints[i].m_Coordinate[0];
array_ML[i][1] = array[i][1] = RkNearestPoints[i].m_Coordinate[1];
array_ML[i][2] = array[i][2] = RkNearestPoints[i].m_Coordinate[2];
}
RkNearestPoints.clear();
double *Matrix[3], *IMatrix[3];
for (int i = 0; i < 3; i++)
{
Matrix[i] = new double[3];
IMatrix[i] = new double[3];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
*(Matrix[i] + j) = 0.0;//全部赋值为0.0
}
}
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < BCNum; i++)
{
*(Matrix[0] + j) += array[i][0] * array[i][j];
*(Matrix[1] + j) += array[i][1] * array[i][j];
*(Matrix[2] + j) += array[i][2] * array[i][j];
Y[j] -= array[i][j];
}
}
double d = Determinant(Matrix, 3);
Inverse(Matrix, IMatrix, 3, d);
for (int i = 0; i < 3; i++)
{
A += *(IMatrix[0] + i)*Y[i];
B += *(IMatrix[1] + i)*Y[i];
C += *(IMatrix[2] + i)*Y[i];
}
for (int i = 0; i < 3; i++)
{
delete[] Matrix[i];
delete[] IMatrix[i];
}
for (int i = 0; i<BCNum; i++){
double t = (A*array[i][0] + B*array[i][1] + C*array[i][2] + 1) / (A*A + B*B + C*C);
array[i][0] = array[i][0] - A*t;
array[i][1] = array[i][1] - B*t;
array[i][2] = array[i][2] - C*t;
}
double t = (A*lpoint.m_Coordinate[0] + B*lpoint.m_Coordinate[1] + C*lpoint.m_Coordinate[2] + 1) / (A*A + B*B + C*C);
lpoint.m_Coordinate[0] = lpoint.m_Coordinate[0] - A*t;
lpoint.m_Coordinate[1] = lpoint.m_Coordinate[1] - B*t;
lpoint.m_Coordinate[2] = lpoint.m_Coordinate[2] - C*t;
PCPoint* pt3d = new PCPoint[BCNum];
// Point* pt2d = new Point[10];
PCPoint averagePoint; // average point.
averagePoint.m_Coordinate[0] = 0;
averagePoint.m_Coordinate[1] = 0;
averagePoint.m_Coordinate[2] = 0;
for (int i = 0; i < BCNum; i++)
{
pt3d[i].m_Coordinate[0] = array[i][0];
pt3d[i].m_Coordinate[1] = array[i][1];
pt3d[i].m_Coordinate[2] = array[i][2];
averagePoint.m_Coordinate[0] += array[i][0];//get averagePoint
averagePoint.m_Coordinate[1] += array[i][1];
averagePoint.m_Coordinate[2] += array[i][2];
}
averagePoint.m_Coordinate[0] = averagePoint.m_Coordinate[0] / BCNum;
averagePoint.m_Coordinate[1] = averagePoint.m_Coordinate[1] / BCNum;
averagePoint.m_Coordinate[2] = averagePoint.m_Coordinate[2] / BCNum;
int side1 = 0;
int side2 = 0;
PCPoint s;
s.m_Coordinate[0] = averagePoint.m_Coordinate[0] - lpoint.m_Coordinate[0];
s.m_Coordinate[1] = averagePoint.m_Coordinate[1] - lpoint.m_Coordinate[1];
s.m_Coordinate[2] = averagePoint.m_Coordinate[2] - lpoint.m_Coordinate[2];
for (int i = 0; i < BCNum; i++)
{
double t = s.m_Coordinate[0] * (pt3d[i].m_Coordinate[0] - lpoint.m_Coordinate[0])
+ s.m_Coordinate[1] * (pt3d[i].m_Coordinate[1] - lpoint.m_Coordinate[1])
+ s.m_Coordinate[2] * (pt3d[i].m_Coordinate[2] - lpoint.m_Coordinate[2]);
if (t > 0)
{
side1++;
}
else
{
side2++;
}
}
if (abs(((double)side1 - (double)side2) / BCNum) > Threadshold)
{
return true;
}
return false;
}
//进行孔洞修补
bool PointCloudData::HoleRepair()
{
//先清空数组
vector<PCPoint> m_SelectedPoint;//被选中的约束点
vector<PCPoint> newSelectedPoint;//不含附加约束点
vector<PCPoint> m_SelectedBoundaryPoint;//被选中的边界点
//新的修补点集合
vector<PCPoint> repairedPoint;
//获取选中的边界点集合
for (int num = 0; num < m_PointSumNumber; num++)
{
if (m_OriginPointCloud[num].b_Selected &&
m_OriginPointCloud[num].b_BoundaryPoint)
{
m_SelectedBoundaryPoint.push_back(m_OriginPointCloud[num]);
}
}
//将选中的边界点向周围进行扩充,扩充的点即为约束点,每个点向其周围扩充
//新的knum个点
int knum = 15;
for (int num = 0; num < m_SelectedBoundaryPoint.size(); num++)
{
PCLKDtreeNKSearch(m_SelectedBoundaryPoint[num], knum);
for (int i = 1; i < knum; i++)
{
if (NotIn(RkNearestPoints[i], m_SelectedBoundaryPoint,m_SelectedPoint))
{
m_SelectedPoint.push_back(RkNearestPoints[i]);
newSelectedPoint.push_back(RkNearestPoints[i]);
}
}
RkNearestPoints.clear();
}
gProgress = 10;//用于显示进度条
//获取隐式曲面方程的系数
double* coefficients = GetRBFCoefficients(m_SelectedPoint);
//test begin
for (int i = 0; i < m_SelectedPoint.size(); i++)
{
m_TestSeletedPointCloud.push_back(m_SelectedPoint[i]);
}
//test end
gProgress = 30;//用于显示进度条
//插入新的孔洞修补点,病对修补点进行位置的调整
bool repairingResult = InsertRepairPoint(coefficients, repairedPoint, m_SelectedBoundaryPoint, m_SelectedPoint);
gProgress = 90;//用于显示进度条
//将所有的数据重置到初始的未选中状态
for (int num = 0; num < m_PointSumNumber; num++)
{
m_OriginPointCloud[num].b_Selected = false;
}
gProgress = 95;//用于显示进度条
//进行张量投票特征点检测
//TensorVoting(repairedPoint);
//向点云中插入新的修补点
for (int num = 0; num < repairedPoint.size(); num++)
{
/*if (num % 2 == 0 && repairedPoint[num].b_TVPoint == false && num != 0)
{
continue;
}*/
m_AddedPoingCLoud.push_back(repairedPoint[num]);
}
gProgress = -1;//销毁进度条
if (repairingResult)
{
return true;
}
else
{
return false;
}
}
//是否在选中的边界集合中
bool PointCloudData::NotIn(PCPoint lpoint, vector<PCPoint> m_SelectedBoundaryPoint, vector<PCPoint> m_SelectedPoint)
{
for (int num = 0; num < m_SelectedBoundaryPoint.size(); num++)
{
if (fabs(lpoint.m_Coordinate[0] - m_SelectedBoundaryPoint[num].m_Coordinate[0]) < 0.0001
&& fabs(lpoint.m_Coordinate[1] - m_SelectedBoundaryPoint[num].m_Coordinate[1]) < 0.0001
&& fabs(lpoint.m_Coordinate[2] - m_SelectedBoundaryPoint[num].m_Coordinate[2]) < 0.0001)
{
return false;
}
}
for (int num = 0; num < m_SelectedPoint.size(); num++)
{
if (fabs(lpoint.m_Coordinate[0] - m_SelectedPoint[num].m_Coordinate[0]) < 0.0001
&& fabs(lpoint.m_Coordinate[1] - m_SelectedPoint[num].m_Coordinate[1]) < 0.0001
&& fabs(lpoint.m_Coordinate[2] - m_SelectedPoint[num].m_Coordinate[2]) < 0.0001)
{
return false;
}
}
return true;
}
//获取径向基函数的系数
double* PointCloudData::GetRBFCoefficients(vector<PCPoint>& m_SelectedPoint)
{
//定义附加约束点
int RBFNum = m_SelectedPoint.size();
PCPoint* additionalPoint = new PCPoint[RBFNum];
//利用选择的约束点获取附加约束点
for (int num = 0; num < m_SelectedPoint.size(); num++)
{
//获取每个数据点的法矢量值及坐标值
additionalPoint[num].m_Normal = GetNormalVector(m_SelectedPoint[num]);
additionalPoint[num].m_Coordinate[0] = m_SelectedPoint[num].m_Coordinate[0];
additionalPoint[num].m_Coordinate[1] = m_SelectedPoint[num].m_Coordinate[1];
additionalPoint[num].m_Coordinate[2] = m_SelectedPoint[num].m_Coordinate[2];
}
//进行法矢量的方向矫正
RectifyNormals(additionalPoint, RBFNum);
for (int num = 0; num < RBFNum; num++)
{
//获取附加约束点的值
GetAdditionalPoint(&additionalPoint[num]);
//为了方便直接将得到的附加约束点加入约束点集合中
m_SelectedPoint.push_back(additionalPoint[num]);
}
//RBF矩阵的大小
int newRBFNum = 2 * RBFNum;
//初始化RBF元素值
double** RBFValue = new double*[newRBFNum];
for (int num = 0; num < newRBFNum; num++)
{
RBFValue[num] = new double[newRBFNum];
}
for (int i = 0; i < newRBFNum; i++)
{
for (int j = 0; j < newRBFNum; j++)
{
RBFValue[i][j] = GetRBFValue(GetTwoPointsDistance(m_SelectedPoint[i],m_SelectedPoint[j]));
}
}
//初始化RBF矩阵
double** RBFMatrix = new double*[newRBFNum + 4];
for (int num = 0; num < newRBFNum + 4; num++)
{
RBFMatrix[num] = new double[newRBFNum + 5];
}
for (int i = 0; i < newRBFNum; i++)
{
for (int j = 0; j < newRBFNum; j++)
{
RBFMatrix[i][j] = RBFValue[i][j];
}
}
for (int num = 0; num < newRBFNum; num++)
{
RBFMatrix[num][newRBFNum] = 1;
RBFMatrix[num][newRBFNum + 1] = m_SelectedPoint[num].m_Coordinate[0];
RBFMatrix[num][newRBFNum + 2] = m_SelectedPoint[num].m_Coordinate[1];
RBFMatrix[num][newRBFNum + 3] = m_SelectedPoint[num].m_Coordinate[2];
}
for (int num = 0; num < newRBFNum; num++)
{
RBFMatrix[newRBFNum][num] = 1;
RBFMatrix[newRBFNum + 1][num] = m_SelectedPoint[num].m_Coordinate[0];
RBFMatrix[newRBFNum + 2][num] = m_SelectedPoint[num].m_Coordinate[1];
RBFMatrix[newRBFNum + 3][num] = m_SelectedPoint[num].m_Coordinate[2];
}
for (int i = newRBFNum; i < newRBFNum + 4; i++)
{
for (int j = newRBFNum; j < newRBFNum + 4; j++)
{
RBFMatrix[i][j] = 0;
}
}
for (int num = 0; num < RBFNum; num++)
{
RBFMatrix[num][newRBFNum + 4] = 0;
}
for (int num = RBFNum; num < newRBFNum; num++)
{
RBFMatrix[num][newRBFNum + 4] = 1;
}
for (int num = newRBFNum; num < newRBFNum + 4; num++)
{
RBFMatrix[num][newRBFNum + 4] = 0;
}
double* coefficients = new double[newRBFNum + 4];
//利用列主元高斯消去法对矩阵进行计算,得到隐式曲面方程的系数
colunmPrincipleGauss(newRBFNum + 4, RBFMatrix);
//获取隐式曲面方程的系数
for (int num = 0; num < newRBFNum + 4; num++)
{
coefficients[num] = RBFMatrix[num][newRBFNum + 4];
}
return coefficients;
}
//法向矫正
void PointCloudData::RectifyNormals(PCPoint* additionalPoint, int RBFNum)
{
for (int num = 0; num < RBFNum; num++)
{
if (additionalPoint[0].m_Normal[0] * additionalPoint[num].m_Normal[0]
+ additionalPoint[0].m_Normal[1] * additionalPoint[num].m_Normal[1]
+ additionalPoint[0].m_Normal[2] * additionalPoint[num].m_Normal[2]
< 0)
{
additionalPoint[num].m_Normal[0] = -additionalPoint[num].m_Normal[0];
additionalPoint[num].m_Normal[1] = -additionalPoint[num].m_Normal[1];
additionalPoint[num].m_Normal[2] = -additionalPoint[num].m_Normal[2];
}
}
for (int num = 0; num < RBFNum; num++)
{
int knum = 10;
PCLKDtreeNKSearch(additionalPoint[num],knum);
for (int i = 1; i < knum; i++)
{
for (int j = 0; j < RBFNum; j++)
{
if (fabs(RkNearestPoints[i].m_Coordinate[0] - additionalPoint[j].m_Coordinate[0]) < 0.0001
&& fabs(RkNearestPoints[i].m_Coordinate[1] - additionalPoint[j].m_Coordinate[1]) < 0.0001
&& fabs(RkNearestPoints[i].m_Coordinate[2] - additionalPoint[j].m_Coordinate[2]) < 0.0001)
{//找到该近邻点
if (additionalPoint[num].m_Normal[0] * additionalPoint[j].m_Normal[0]
+ additionalPoint[num].m_Normal[1] * additionalPoint[j].m_Normal[1]
+ additionalPoint[num].m_Normal[2] * additionalPoint[j].m_Normal[2]
< 0)
{//如果两法矢量之间的夹角大于90度,则改变法矢量的方向
additionalPoint[j].m_Normal[0] = -additionalPoint[j].m_Normal[0];
additionalPoint[j].m_Normal[1] = -additionalPoint[j].m_Normal[1];
additionalPoint[j].m_Normal[2] = -additionalPoint[j].m_Normal[2];
}
break;
}
}
}
RkNearestPoints.clear();
}
}
//获取数据点的法矢量
double* PointCloudData::GetNormalVector(PCPoint lpoint)
{
/*double* normalVector = new double[3];//定义面法向量
//以下法向量求法由行列式推理得到
//lpoint(x1,y1,z1)
//nearest(x2,y2,z2)
//nearest2(x3,y3,z3)
//o(0,0,0),a(a1,a2,a3),b(b1,b2,b3)
//x=a2b3-a3b2,y=a3b1-a1b3,z=a1b2-a2b1
PCLKDtreeNKSearch(lpoint,10);
double a1, a2, a3, b1, b2, b3;
a1 = RkNearestPoints[3].m_Coordinate[0] - lpoint.m_Coordinate[0];
a2 = RkNearestPoints[3].m_Coordinate[1] - lpoint.m_Coordinate[1];
a3 = RkNearestPoints[3].m_Coordinate[2] - lpoint.m_Coordinate[2];
b1 = RkNearestPoints[5].m_Coordinate[0] - lpoint.m_Coordinate[0];
b2 = RkNearestPoints[5].m_Coordinate[1] - lpoint.m_Coordinate[1];
b3 = RkNearestPoints[5].m_Coordinate[2] - lpoint.m_Coordinate[2];
normalVector[0] = a2 * b3 - a3 * b2;
normalVector[1] = a3 * b1 - a1 * b3;
normalVector[2] = a1 * b2 - a2 * b1;
RkNearestPoints.clear();
return normalVector;*/
//利用数据点拟合其最小二乘平面,用最小二乘平面的法矢量代替点的法矢量
double* normalVector = new double[3];
double array[NORMAL_VECTOR_NUM][3], Y[3];
double array_ML[NORMAL_VECTOR_NUM][3];
double A, B, C;
A = B = C = 0.0;
ZeroMemory(array, sizeof(array));
ZeroMemory(Y, sizeof(Y));
PCLKDtreeNKSearch(lpoint, NORMAL_VECTOR_NUM);
for (int i = 0; i < NORMAL_VECTOR_NUM; i++)
{
array_ML[i][0] = array[i][0] = RkNearestPoints[i].m_Coordinate[0];
array_ML[i][1] = array[i][1] = RkNearestPoints[i].m_Coordinate[1];
array_ML[i][2] = array[i][2] = RkNearestPoints[i].m_Coordinate[2];
}
RkNearestPoints.clear();
double *Matrix[3], *IMatrix[3];
for (int i = 0; i < 3; i++)
{
Matrix[i] = new double[3];
IMatrix[i] = new double[3];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
*(Matrix[i] + j) = 0.0;//全部赋值为0.0
}
}
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < NORMAL_VECTOR_NUM; i++)
{
*(Matrix[0] + j) += array[i][0] * array[i][j];
*(Matrix[1] + j) += array[i][1] * array[i][j];
*(Matrix[2] + j) += array[i][2] * array[i][j];
Y[j] -= array[i][j];
}
}
double d = Determinant(Matrix, 3);
Inverse(Matrix, IMatrix, 3, d);
for (int i = 0; i < 3; i++)
{
A += *(IMatrix[0] + i)*Y[i];
B += *(IMatrix[1] + i)*Y[i];
C += *(IMatrix[2] + i)*Y[i];
}
normalVector[0] = A;
normalVector[1] = B;
normalVector[2] = C;
return normalVector;
}
//求得张量投票算法的特征点法矢量
double* PointCloudData::GetTVNormalVector(PCPoint lpoint, pcl::PointCloud<pcl::PointXYZ>::Ptr coeCloud
, pcl::KdTreeFLANN<pcl::PointXYZ> coeKdtree)
{
double* normalVector = new double[3];
double array[NORMAL_VECTOR_NUM][3], Y[3];
double array_ML[NORMAL_VECTOR_NUM][3];
double A, B, C;
A = B = C = 0.0;
ZeroMemory(array, sizeof(array));
ZeroMemory(Y, sizeof(Y));
PCLCoefficientsKDtreeNKSearch(lpoint, NORMAL_VECTOR_NUM, coeCloud, coeKdtree);
for (int i = 0; i < NORMAL_VECTOR_NUM; i++)
{
array_ML[i][0] = array[i][0] = RkNearestPoints[i].m_Coordinate[0];
array_ML[i][1] = array[i][1] = RkNearestPoints[i].m_Coordinate[1];
array_ML[i][2] = array[i][2] = RkNearestPoints[i].m_Coordinate[2];
}
RkNearestPoints.clear();
double *Matrix[3], *IMatrix[3];
for (int i = 0; i < 3; i++)
{
Matrix[i] = new double[3];
IMatrix[i] = new double[3];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
*(Matrix[i] + j) = 0.0;//全部赋值为0.0
}
}
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < NORMAL_VECTOR_NUM; i++)
{
*(Matrix[0] + j) += array[i][0] * array[i][j];
*(Matrix[1] + j) += array[i][1] * array[i][j];
*(Matrix[2] + j) += array[i][2] * array[i][j];
Y[j] -= array[i][j];
}
}
double d = Determinant(Matrix, 3);
Inverse(Matrix, IMatrix, 3, d);
for (int i = 0; i < 3; i++)
{
A += *(IMatrix[0] + i)*Y[i];
B += *(IMatrix[1] + i)*Y[i];
C += *(IMatrix[2] + i)*Y[i];
}
normalVector[0] = A;
normalVector[1] = B;
normalVector[2] = C;
return normalVector;
}
//获取附加约束点的值
void PointCloudData::GetAdditionalPoint(PCPoint* lpoint)
{
double l = 0.1;
double pNormal[3];
double temp[3];
for (int num = 0; num < 3; num++)
{
pNormal[num] = lpoint->m_Normal[num];
}
temp[0] = l * pNormal[0] / sqrt(pNormal[0] * pNormal[0]
+ pNormal[1] * pNormal[1] + pNormal[2] * pNormal[2]) + lpoint->m_Coordinate[0];
temp[1] = l * pNormal[1] / sqrt(pNormal[0] * pNormal[0]
+ pNormal[1] * pNormal[1] + pNormal[2] * pNormal[2]) + lpoint->m_Coordinate[1];
temp[2] = l * pNormal[2] / sqrt(pNormal[0] * pNormal[0]
+ pNormal[1] * pNormal[1] + pNormal[2] * pNormal[2]) + lpoint->m_Coordinate[2];
lpoint->m_Coordinate[0] = temp[0];
lpoint->m_Coordinate[1] = temp[1];
lpoint->m_Coordinate[2] = temp[2];
}
//获取两个数据点之间的距离
double PointCloudData::GetTwoPointsDistance(PCPoint p, PCPoint q)
{
double result = sqrt((p.m_Coordinate[0] - q.m_Coordinate[0]) * (p.m_Coordinate[0] - q.m_Coordinate[0])
+ (p.m_Coordinate[1] - q.m_Coordinate[1]) * (p.m_Coordinate[1] - q.m_Coordinate[1])
+ (p.m_Coordinate[2] - q.m_Coordinate[2]) * (p.m_Coordinate[2] - q.m_Coordinate[2]));
return result;
}
//插入新的修补点
bool PointCloudData::InsertRepairPoint(double* coefficients, vector<PCPoint> & repairedPoint, vector<PCPoint> m_SelectedBoundaryPoint, vector<PCPoint> m_SelectedPoint)
{
PCPoint midPoint;//备选约束点的中心点,不包括附加约束点
for (int num = 0; num < m_SelectedBoundaryPoint.size(); num++)
{
midPoint.m_Coordinate[0] += m_SelectedBoundaryPoint[num].m_Coordinate[0];
midPoint.m_Coordinate[1] += m_SelectedBoundaryPoint[num].m_Coordinate[1];
midPoint.m_Coordinate[2] += m_SelectedBoundaryPoint[num].m_Coordinate[2];
}
//获得中心点
midPoint.m_Coordinate[0] /= (m_SelectedBoundaryPoint.size());
midPoint.m_Coordinate[1] /= (m_SelectedBoundaryPoint.size());
midPoint.m_Coordinate[2] /= (m_SelectedBoundaryPoint.size());
//获取随机两点之间的距离
PCLKDtreeNKSearch(m_SelectedBoundaryPoint[0], 2);
double dp = GetTwoPointsDistance(m_SelectedBoundaryPoint[0], RkNearestPoints[1]);
RkNearestPoints.clear();
//中点
repairedPoint.push_back(midPoint);
//依据隐式曲面方程调整孔洞修补点的位置
RepairedPointDisjust(coefficients, repairedPoint[repairedPoint.size() - 1], m_SelectedPoint);
//进行插点操作
for (int num = 0; num < m_SelectedBoundaryPoint.size(); num++)
{
if (gProgress > 0)
{
gProgress = 30 + 60 * ((double)num / (double)m_SelectedBoundaryPoint.size());//用于显示进度条
}
if (true)
{
//获取两点之间的距离
double distance = GetTwoPointsDistance(midPoint, m_SelectedBoundaryPoint[num]);
//获取插入点的个数
int pointNum = distance / dp;
for (int i = 1; i <= pointNum; i++)
{
double l = ((double)i / (double)pointNum) * distance;
PCPoint tempPoint = GetInsertPoint(midPoint, m_SelectedBoundaryPoint[num], l);
repairedPoint.push_back(tempPoint);
if (i != 1 && GetTwoPointsDistance(repairedPoint[repairedPoint.size() - 1], repairedPoint[repairedPoint.size() - 2]) > 4.5 * dp)
{
//表示该孔洞不能用径向基函数拟合,因此不能自动修补
repairedPoint.clear();
return false;
}
else
{
//依据隐式曲面方程调整孔洞修补点的位置
RepairedPointDisjust(coefficients, repairedPoint[repairedPoint.size() - 1], m_SelectedPoint);
}
}
}
}
return true;
}
//获取新插入的修补点
PCPoint PointCloudData::GetInsertPoint(PCPoint p, PCPoint q, double l)
{
//获取两点的方向向量
double normal[3];
normal[0] = q.m_Coordinate[0] - p.m_Coordinate[0];
normal[1] = q.m_Coordinate[1] - p.m_Coordinate[1];
normal[2] = q.m_Coordinate[2] - p.m_Coordinate[2];
//新插入的修补点
PCPoint repairedPoint;
repairedPoint.m_Coordinate[0] = l * normal[0] / sqrt(normal[0] * normal[0]
+ normal[1] * normal[1] + normal[2] * normal[2]) + p.m_Coordinate[0];
repairedPoint.m_Coordinate[1] = l * normal[1] / sqrt(normal[0] * normal[0]
+ normal[1] * normal[1] + normal[2] * normal[2]) + p.m_Coordinate[1];
repairedPoint.m_Coordinate[2] = l * normal[2] / sqrt(normal[0] * normal[0]
+ normal[1] * normal[1] + normal[2] * normal[2]) + p.m_Coordinate[2];
//设为
repairedPoint.m_Color[0] = 1.0;
repairedPoint.m_Color[1] = 0.0;
repairedPoint.m_Color[2] = 1.0;
return repairedPoint;
}
//获取修补点集合
vector<PCPoint>* PointCloudData::GetNewAddedPoint()
{
return &m_AddedPoingCLoud;
}
/*
//获取约束点集合
vector<PCPoint>* PointCloudData::GetSelectedPoint()
{
return &m_SelectedPoint;
}*/
//调整孔洞修补点
void PointCloudData::RepairedPointDisjust(double* coefficients, PCPoint& repairedPoint, vector<PCPoint> m_SelectedPoint)
{
double lastFr = DBL_MAX;//设为最大的double值
while (true)
{
double frPoint[3];
double frX = 0;
double frY = 0;
double frZ = 0;
for (int i = 0; i < m_SelectedPoint.size(); i++)
{
frX += coefficients[i] * (repairedPoint.m_Coordinate[0] - m_SelectedPoint[i].m_Coordinate[0])
* GetTwoPointsDistance(repairedPoint, m_SelectedPoint[i]);
frY += coefficients[i] * (repairedPoint.m_Coordinate[1] - m_SelectedPoint[i].m_Coordinate[1])
* GetTwoPointsDistance(repairedPoint, m_SelectedPoint[i]);
frZ += coefficients[i] * (repairedPoint.m_Coordinate[2] - m_SelectedPoint[i].m_Coordinate[2])
* GetTwoPointsDistance(repairedPoint, m_SelectedPoint[i]);
}
frX = 3 * frX + coefficients[m_SelectedPoint.size() + 1];
frY = 3 * frY + coefficients[m_SelectedPoint.size() + 2];
frZ = 3 * frZ + coefficients[m_SelectedPoint.size() + 3];
frPoint[0] = frX;
frPoint[1] = frY;
frPoint[2] = frZ;
double Fr = 0;
for (int i = 0; i < m_SelectedPoint.size(); i++)
{
Fr += coefficients[i] * GetRBFValue(GetTwoPointsDistance(repairedPoint, m_SelectedPoint[i]));
}
Fr = Fr + coefficients[m_SelectedPoint.size()] + repairedPoint.m_Coordinate[0] * coefficients[m_SelectedPoint.size() + 1]
+ repairedPoint.m_Coordinate[1] * coefficients[m_SelectedPoint.size() + 2]
+ repairedPoint.m_Coordinate[2] * coefficients[m_SelectedPoint.size() + 3];
double tempFr = Fr / (frPoint[0] * frPoint[0]
+ frPoint[1] * frPoint[1] + frPoint[2] * frPoint[2]);
if (fabs(Fr) < fabs(lastFr))
{
lastFr = Fr;
repairedPoint.m_Coordinate[0] = repairedPoint.m_Coordinate[0] - tempFr * frPoint[0];
repairedPoint.m_Coordinate[1] = repairedPoint.m_Coordinate[1] - tempFr * frPoint[1];
repairedPoint.m_Coordinate[2] = repairedPoint.m_Coordinate[2] - tempFr * frPoint[2];
}
else
{
break;
}
}
}
//进行边界聚类
void PointCloudData::BoundaryClustering()
{
//获取选中的边界点集合
vector<PCPoint> m_SelectedBoundaryPoint;
for (int num = 0; num < m_PointSumNumber; num++)
{
if (m_OriginPointCloud[num].b_Selected &&
m_OriginPointCloud[num].b_BoundaryPoint)
{
m_SelectedBoundaryPoint.push_back(m_OriginPointCloud[num]);
}
}
//利用PCL中的聚类函数对得到的边界点进行聚类操作
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr boundarycloud(new pcl::PointCloud<pcl::PointXYZ>);
for (int num = 0; num < m_PointSumNumber; num++)
{//获取边界点集合
if (m_OriginPointCloud[num].b_BoundaryPoint)
{
m_SelectedBoundaryPoint.push_back(m_OriginPointCloud[num]);
}
m_OriginPointCloud[num].m_Color[0] = 0.0;//全部设为蓝色
m_OriginPointCloud[num].m_Color[1] = 0.0;//全部设为蓝色
m_OriginPointCloud[num].m_Color[2] = 1.0;//全部设为蓝色
}
boundarycloud->width = m_SelectedBoundaryPoint.size();//初始化 PCL 点云
boundarycloud->height = 1;
boundarycloud->points.resize(boundarycloud->width * boundarycloud->height);
//初始化点云
int cloudCount = 0;
for (int num = 0; num < m_SelectedBoundaryPoint.size(); num++)
{
boundarycloud->points[cloudCount].x = m_SelectedBoundaryPoint[num].m_Coordinate[0];
boundarycloud->points[cloudCount].y = m_SelectedBoundaryPoint[num].m_Coordinate[1];
boundarycloud->points[cloudCount].z = m_SelectedBoundaryPoint[num].m_Coordinate[2];
cloudCount++;
}
tree->setInputCloud(boundarycloud);
//初始化聚类参数并进行聚类
vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.setClusterTolerance(m_clusterDistace);
ec.setMinClusterSize(m_clusterNum);
ec.setMaxClusterSize(5000);
ec.setSearchMethod(tree);
ec.setInputCloud(boundarycloud);
ec.extract(cluster_indices);
//提取聚类完成的各个点云集合
m_CloudClusterNum = cluster_indices.size();
pcl::PointCloud<pcl::PointXYZ>::Ptr* cloud_cluster; //(new pcl::PointCloud<pcl::PointXYZ>);
cloud_cluster = new pcl::PointCloud<pcl::PointXYZ>::Ptr[m_CloudClusterNum];
for (int num = 0; num < m_CloudClusterNum; num++)
{
cloud_cluster[num] = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
}
int i = 0;
for (vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin();
it != cluster_indices.end(); i++, it++)
{
for (vector<int>::const_iterator pit = it->indices.begin();
pit != it->indices.end(); pit++)
{
cloud_cluster[i]->points.push_back(boundarycloud->points[*pit]);
}
cloud_cluster[i]->width = cloud_cluster[i]->points.size();
cloud_cluster[i]->height = 1;
cloud_cluster[i]->is_dense = true;
}
//将得到的聚类划分好的点云集合存储起来
m_BoundaryPointClusters = new vector<PCPoint>[m_CloudClusterNum];
for (int i = 0; i < m_CloudClusterNum; i++)
{
for (int num = 0; num < cloud_cluster[i]->points.size(); num++)
{
PCPoint tempPoint;
tempPoint.m_Color[0] = ((double)i / double(m_CloudClusterNum)) * 1.0;
tempPoint.m_Color[1] = 1.0 - ((double)i / double(m_CloudClusterNum)) * 1.0;
tempPoint.m_Color[2] = 0.5;
tempPoint.m_Coordinate[0] = cloud_cluster[i]->points[num].x;
tempPoint.m_Coordinate[1] = cloud_cluster[i]->points[num].y;
tempPoint.m_Coordinate[2] = cloud_cluster[i]->points[num].z;
m_BoundaryPointClusters[i].push_back(tempPoint);
}
}
}
//获取边界的聚类数量
int PointCloudData::GetCloudClusterNum()
{
return m_CloudClusterNum;
}
//获取边界聚类集合
vector<PCPoint>* PointCloudData::GetBoundaryPointClusters()
{
return m_BoundaryPointClusters;
}
//自动进行孔洞修补
void PointCloudData::AutomaticHoleRepair()
{
m_AddedPoingCLoud.clear();
//对聚类出的孔洞边界集合分别进行孔洞修补的工作
//先判断出不进行修补的孔洞
//存储判断距离