-
Notifications
You must be signed in to change notification settings - Fork 0
/
getConiferIGFs_Proposed.m
1698 lines (1463 loc) · 74.1 KB
/
getConiferIGFs_Proposed.m
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
function [igffeatureArr, slicedfeatureArrTotal, numBranches] = getConiferIGFs_Method1(inFileFullName, speciesCode, allFiles, plotOn)
igffeatureArr = zeros(size(allFiles,1),9);
slicedfeature4Div = zeros(size(allFiles,1),32);
slicedfeature7Div = zeros(size(allFiles,1),56);
slicedfeature10Div = zeros(size(allFiles,1),80);
slicedfeature13Div = zeros(size(allFiles,1),104);
numBranches = zeros(size(allFiles,1),1);
count = 1;
for file = allFiles'
fullFileName = strcat(inFileFullName,file.name); % get full file name
%-----------------------------------------------------------------%
% -- The below part of the code does LiDAR data Preprocessing --- %
% ----------------------------------------------------------------%
%Read LiDAR data
data = LoadData(fullFileName);
% Data Pre-processing and basic tree information retrieval
% e.g., max/min height, crown height etc.
stData = dataPreProcessing(data);
% Plot the conifer tree stem & also plots the complete LiDAR data
showRawLiDARdata = false; % 1 = show LiDAR data; 0 = hide LiDAR data
plotLiDARdataWithStem(stData, showRawLiDARdata, plotOn);
% ----------------------------------------------------------------%
% - The below part of the code locates and plots the conifer
% - branch tips - %
% ----------------------------------------------------------------%
plotBranchTips = false; plotConvexHull = false;
extremeLiDARDataArray = getBoundaryPoints(stData, speciesCode, plotBranchTips, plotConvexHull, plotOn);
% ----------------------------------------------------------------%
% -- The below part of the code does inital region growing starting
% ----------------------- from branche tips ----------------------%
% ----------------------------------------------------------------%
% Get connected components in the LiDAR point cloud by using 3D
[connectComponentArray,connectComponentindexArr, extremeLiDARDataArray] = ...
getConnectedComponetsOld(stData.lidarDataArray, extremeLiDARDataArray);
% ----------------------------------------------------------------%
% ---- The below part of the completes the identification of -----%
% ---- branch points not identified from region growing step -----%
% ----------------------------------------------------------------%
% Get stem point in the direction of PC1 of branch clusters.
[stemPointArr, D1] = getStemPointFromPCA(extremeLiDARDataArray,...
connectComponentArray, stData.retMaxXYZ);
% ----------------------------------------------------------------%
% ------- The below part of the code calculates the IGFs ------- %
% ----------------------------------------------------------------%
% Get features from individual branches and calculates the Internal Geometric Features
[retIGFsVec, BranchArr, nb] = getIGFFeatureValue(stData, extremeLiDARDataArray,...
stemPointArr ,D1, connectComponentArray, connectComponentindexArr, speciesCode, plotOn);
numBranches(count) = nb;
% Feature slicing (for slice-wise analysis)
SlicedFeatures = FeatureSlicing(BranchArr,stData);
slicedfeature4Div(count,:) = SlicedFeatures{1}; % 8 * 4 = 32 features
slicedfeature7Div(count,:) = SlicedFeatures{2}; % 8 * 7 = 56 features
slicedfeature10Div(count,:) = SlicedFeatures{3}; % 8 * 10 = 80 features
slicedfeature13Div(count,:) = SlicedFeatures{4}; % 8 * 13 = 104 features
% Store IGFs for the tree
igffeatureArr(count,:) = retIGFsVec;
% Print featurevalue in console and save results
printInConsole = true;
printResultInConsole(count, retIGFsVec, printInConsole);
% Print plot if plotOn is true
if(plotOn)
hold off;
pause(0.25); % To update plot
end
% Store the IGFs and IFs by slicing the by 4, 7, 10, and 13.
slicedfeatureArrTotal{1} = slicedfeature4Div;
slicedfeatureArrTotal{2} = slicedfeature7Div;
slicedfeatureArrTotal{3} = slicedfeature10Div;
slicedfeatureArrTotal{4} = slicedfeature13Div;
count = count + 1;
end
% Save IGF values in the local drive as a .csv file.
% csvwrite('csvlist_igf_pca.csv', igffeatureArr);
end
function printResultInConsole(count, retIGFarr, printInConsole)
% Print results in the matlab console
if(printInConsole)
if(count == 1)
fprintf(' avgRetSlope avgRetLen avgPntDistToline avgMaxEval avgEigRatio avgTotalPoints avgInt avgIntSd avgMedian\n')
end
disp(strcat(num2str(retIGFarr(1)), {' '}, num2str(retIGFarr(2)), {' '} , num2str(retIGFarr(3)), {' '}, num2str(retIGFarr(4)), {' '},...
num2str(retIGFarr(5)), {' '}, num2str(retIGFarr(6)), {' '}, num2str(retIGFarr(7)), {' '}, num2str(retIGFarr(8)), {' '},...
num2str(retIGFarr(9))));
end
end
function SlicedFeatures = FeatureSlicing(BranchArr,stData)
SlicedFeatures = [];
SliceNrArr = [4 7 10 13];
CrownBase = stData.minCrownHeight;
CrownTop = stData.maxtreeHeight;
CrownHeight = CrownTop - CrownBase;
TreeHeight = stData.treeHeight;
TotalPointNr = size(stData.lidarDataArray,1);
for SliceNrCounter = 1:size(SliceNrArr,2)
SliceNr = SliceNrArr(SliceNrCounter);
thresholds = [];
for i = 1:(SliceNr)
% calculate the height thresholds that will be used to
% determine which slice a branch belongs to
thresholds = [thresholds, i/SliceNr];
end
thresholds = CrownBase + thresholds*CrownHeight;
treshTable = []; labels = [];
% generate a label column which contains the slice the branch
% belongs to. It will be added to the branch feature matrix
for BranchCtr = 1:size(BranchArr,1)
treshTable = [treshTable;BranchArr(BranchCtr,9) < thresholds];
labels = [labels;min(find(treshTable(BranchCtr,:) == 1))];
end
% save the labeled branches for each different slice number
% configuration in a cell
LabeledBranches{SliceNrCounter} = [BranchArr,labels];
end
%calculate the average of each feature among branches in the same slice
for OuterLoop = 1:4
CurrentTable = LabeledBranches{OuterLoop};
AvgIgfs = zeros(SliceNrArr(OuterLoop),6);
AvgIntFeatures = zeros(SliceNrArr(OuterLoop),2);
BranchCount = [];
for SliceCtr = 1:SliceNrArr(OuterLoop)
BranchIndexPerSlice = find(CurrentTable(:,10) == SliceCtr);
BranchesPerSlice = CurrentTable(BranchIndexPerSlice,:);
% counts the number of branches per slice
% currently unused, but may be useful as an additional feature
BranchCount = [BranchCount,size(BranchesPerSlice,1)];
% table of IGFs and Intensity features of each slice
IGFs = BranchesPerSlice(:,1:6);
IntFeatures = BranchesPerSlice(:,7:8);
% averaging features for the branches in each slice
avgRetSlope = mean(IGFs(:,1));
avgRetLen = mean(IGFs(:,2))/TreeHeight;
avgPtToLineDst = mean(IGFs(:,3));
avgMaxEval = mean(IGFs(:,4))/TreeHeight;
avgEigRatio = mean(IGFs(:,5));
if(avgEigRatio>100) % to avoid large values due to division by small number.
avgEigRatio = 100;
end
avgTotalPoints = mean(IGFs(:,6))/TotalPointNr;
AvgIgfs(SliceCtr,:) = [avgRetSlope,avgRetLen,avgPtToLineDst,avgMaxEval,avgEigRatio,avgTotalPoints];
avgInt = mean(IntFeatures(:,1));
avgIntSd = mean(IntFeatures(:,2));
AvgIntFeatures(SliceCtr,:) = [avgInt,avgIntSd];
end
% save the features in a structure
% STRUCTURE :
% | IGFs Slice 1 | Intensity Features Slice 1| ... | IGFs Slice n | Intensity Features Slice n |
StructuredFeatures = [];
for j = 1:SliceNrArr(OuterLoop)
StructuredFeatures = [StructuredFeatures,AvgIgfs(j,:),AvgIntFeatures(j,:)];
StructuredFeatures(isnan(StructuredFeatures)) = 0; % substitute eventual NaN values with zero
end
SlicedFeatures{OuterLoop} = StructuredFeatures;
end
end
function extremeLiDARDataArray = getBoundaryPoints(stData, speciesCode, plotBranchTips, plotConvexHull, plotOn)
K = boundary(stData.lidarDataArray(:,1),stData.lidarDataArray(:,2),stData.lidarDataArray(:,3),1);
K = unique(K);
tempLiDARarray = stData.lidarDataArray(K,:);
% Cuttoff distances for mutiple branch tip removal
if(strcmp(speciesCode,'ar'))
cutOffDistance = 2;
elseif(strcmp(speciesCode,'la'))
cutOffDistance = 4;
elseif(strcmp(speciesCode,'pc'))
cutOffDistance = 3;
else
cutOffDistance = 2;
end
tempLiDARarray = removeRepetitions(stData, tempLiDARarray, cutOffDistance); %2:ar 0.2:la %0.5:pc 0.5:ab
% for further smoothening (remove any mutiple branch-tip candidates from the same branch)
K1 = boundary(tempLiDARarray(:,1),tempLiDARarray(:,2),tempLiDARarray(:,3),0.5);
uniqueK1 = unique(K1);
extremeLiDARDataArray = tempLiDARarray(uniqueK1,:);
% Plot branch points if both plotBranchTips = true and plotOn = true
if(and(plotBranchTips,plotOn))
plot3(tempLiDARarray(uniqueK1,1), tempLiDARarray(uniqueK1,2), ...
tempLiDARarray(uniqueK1,3), '.', 'Color', [1 0 0],'MarkerSize',20);
hold on;
end
% Plot convex hull if both plotConvexHull = true and plotOn = true
if(and(plotConvexHull,plotOn))
trisurf(K1,tempLiDARarray(:,1),tempLiDARarray(:,2),tempLiDARarray(:,3));
alpha(0.2);
end
end
function plotLiDARdataWithStem(stData, showRawLiDARParameter, plotOn)
if(plotOn)
clf; % clear figures
% plot the tree stem
plot3([stData.retMaxXYZ(1) stData.retMaxXYZ(1)],[stData.retMaxXYZ(2)...
stData.retMaxXYZ(2)],[0 stData.maxtreeHeight], '-o', 'Color', [1 0 0]);
hold on;
%To show cubic/sector grid or not ; both should not be true simulatniously
plotLiDARData(stData.lidarDataArrayComplete, false, false,...
stData.htDeduction,showRawLiDARParameter, 15,stData.retMaxXYZ)
camproj perspective; rotate3d on; axis vis3d; axis equal; axis on; view(-45, 15); grid on;
% maximum height
mht = stData.maxtreeHeight + (2 - mod(stData.maxtreeHeight,2)); % round-pff to next mutiple of 2;
% set maximum axis dimentions to be shown
axis([-stData.maxTreeWidth stData.maxTreeWidth -stData.maxTreeWidth ...
stData.maxTreeWidth 0 mht]);
% Set perspective and label fonts and view angle for the 3D plot
xlabel('X Axis','Fontname', 'Times New Roman' ,'FontSize', 14);
ylabel('Y Axis','Fontname', 'Times New Roman' ,'FontSize', 14);
zlabel('Tree height','Fontname', 'Times New Roman' ,'FontSize', 14);
set(gca,'XLim',[-6 6]);set(gca,'YLim',[-6 6]);set(gca,'ZLim',[0 mht]);
set(gca,'XTick',-6:2:6); set(gca,'YTick',-6:2:6); set(gca,'ZTick',0:2:mht)
% set(gca,'XTickLabel',['0';'';'1';' ';'2';' ';'3';' ';'4'])
end
end
function [unallotedlidarDataArr, unallotedlidarIndexArr] = getUnallocatedPointIndices(stData, connectComponentArray, connectComponentArrayIndx, stemPointArr, branchTipArray)
tmpAra =[];
for hh =1:1:size(connectComponentArray,2)
tmpAra = [tmpAra; connectComponentArrayIndx{hh}];
end
unallotedlidarDataArr = stData.lidarDataArrayComplete(find(~ismember(stData.lidarDataArrayComplete(:,7),unique(tmpAra))), :);
% allocate points to cluster of the nearest branch-line
unallotedlidarIndexArr = zeros(size(branchTipArray,1),3);
for ii = 1: 1: size(unallotedlidarDataArr,1)
distArrddss = getDist2Point22(branchTipArray(:,(1:3)), stemPointArr, repmat(unallotedlidarDataArr(ii,(1:3)),[size(branchTipArray,1) 1]) );
[mval, mdx] = min(distArrddss);
%if(mval <0.5)
unallotedlidarIndexArr(ii,:) = [ii mdx mval]; % Point LineNumber Distance of Point to the line
%end
end
unallotedlidarIndexArr = sortrows(unallotedlidarIndexArr,2);
end
function [retIGFsVec, BranchArr, numBranches] = getIGFFeatureValue(stData, branchTipArray, stemPointArr, D1, connectComponentArray, connectComponentArrayIndx, speciesCode, plotOn)
% initilaizing variables
numBranches = size(branchTipArray,1);
retIGFsArray = zeros(numBranches,9);
% Random color generation for clusters
retRand = distinguishable_colors(size(branchTipArray,1)); %rand(size(branchTipArray,1),3);
% Indices of previosly unallocated liDAR point
[unallotedlidarDataArr, unallotIndexArr] = ....
getUnallocatedPointIndices(stData, connectComponentArray, connectComponentArrayIndx, stemPointArr, branchTipArray);
% Large slopes which are very likely to be wrong.(This is a known limitation of the method)
if(strcmp(speciesCode,'ar'))
slopeThreshold = 30;
includePointDistance = 0.5; % Threshold set for ar (based on point density)
elseif(strcmp(speciesCode,'la'))
slopeThreshold = 30;
includePointDistance = 0.8;
elseif(strcmp(speciesCode,'pc')) % Threshold set for la
slopeThreshold = 100;
includePointDistance = 0.2; % Threshold set for pc
else
slopeThreshold = 30;
includePointDistance = 0.8; % Threshold set for ab
end
for bCount = 1:1:numBranches
% Add the LiDAR point obtained from region growing to the tmpGrp points
RegionGrownLiDARPoints = stData.lidarDataArray(ismember(stData.lidarDataArray(:,7), connectComponentArrayIndx{bCount}),1:3);
% distance to line is less than "includePointDistance"
unallotedLiDARPoints = unallotedlidarDataArr(unallotIndexArr(and(unallotIndexArr(:,2)==bCount, ...
(abs(unallotIndexArr(:,3))) < includePointDistance),1),(1:3));
branchPointCluster = [RegionGrownLiDARPoints; unallotedLiDARPoints; ]; % repmat(stemPointArr(jj,:),10,1)
prosConnectedComp{bCount} = branchPointCluster;
% Regression fit line through connected components.
[retSlope, lineLength, pntDistToline, maxeval, eigRatio, totalPoints] = getBestFitLine(stData,branchPointCluster, stemPointArr(bCount,:), branchTipArray(bCount,(1:3)), slopeThreshold, speciesCode, true, plotOn, [0 0.5 0]); % isPlotON
% Plot branches with slope < slopeThreshold
if(abs(retSlope) < slopeThreshold) % && stemPointArr(bCount,3) > 5
if(plotOn)
plot3(branchPointCluster(:,1),branchPointCluster(:,2),branchPointCluster(:,3),'.','MarkerSize',5,'Color',[retRand(bCount,1) retRand(bCount,2) retRand(bCount,3)]);
end
end
% get the intensity fratures (mean and standard deviation)
[avgInt,sdInt,medianZ] = GetIntensityFeatures(branchPointCluster,stData);
retIGFsArray(bCount,:) = [retSlope, lineLength, pntDistToline, maxeval,eigRatio,totalPoints,avgInt,sdInt,medianZ];
end
%Get the number of branches
numBranches = size(retIGFsArray,1);
% Remove all the rows which has 0's in at least one column
retIGFsArray = retIGFsArray(retIGFsArray(:,5)~=-1,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,1)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,2)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,3)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,4)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,5)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,6)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,7)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,8)~=0,:);
retIGFsArray = retIGFsArray(retIGFsArray(:,9)~=0,:);
% Select branches which meets the above slopeThreshold criteria
BranchArr = retIGFsArray(abs(retIGFsArray(:,1)) < slopeThreshold,:);
retIGFsVec = sum(BranchArr,1)/size(BranchArr,1);
% Perform height/desnity normalization for three of the features
retIGFsVec(2) = retIGFsVec(2)/stData.treeHeight; % Branch Length
retIGFsVec(4) = retIGFsVec(4)/stData.treeHeight; % Max eigenvalue
retIGFsVec(6) = retIGFsVec(6)/size(stData.lidarDataArray,1); % Total Points in a branch
if(retIGFsVec(5)>100) % to avoid large values due to division by small number.
retIGFsVec(5) = 100; %(eigratio)
end
end
function retEigenData = getBestEigenDirection(inputData)
[a,~,c]=pca(inputData);
pc = zeros(3,2);
for i=1:size(a,2)
pc(:,i) = a(:,i);%'*-c(i);
end
retEigPoints{1} = pc;
retEigPoints{2} = c;
retEigenData = retEigPoints;
end
function [avgInt,sdInt,medianZ] = GetIntensityFeatures(tmpGrp,stData)
tmpGrpInt = [];
branchPoints = [];
% height = (stData.maxtreeHeight-stData.minCrownHeight); % could also be implemented with mintreeheight instead of mincrownheight
% use tmpGrp xyz information to find corresponding points in lasdata
% and extrapolate intensity from there (since tmpGrp does not include the intensity value)
for i=1:size(tmpGrp,1)
target=tmpGrp(i,:);
indicesX = find ( stData.lidarDataArray(:,1) == target(1) ); % check x
indicesY = find ( stData.lidarDataArray(:,2) == target(2) ); % check y
indicesZ = find ( stData.lidarDataArray(:,3) == target(3) ); % check z
indices=[indicesX;indicesY;indicesZ];
uniqueIndices=unique(indices);
count=histc(indices,uniqueIndices); %counts the occurrence of each index. the one that appears 3 times (xyz) is the right one
targetIndex=uniqueIndices(find(count==3),1);
branchPoints = [branchPoints;stData.lidarDataArray(targetIndex,3),stData.lidarDataArray(targetIndex,6)]; % save z and intensity information
end
% calculate the mean-range of the points' z coordinate (middle point of the tree height)
BranchMax = max(branchPoints(:,1));
BranchMin = min(branchPoints(:,1));
medianZ = (BranchMax+BranchMin)/2; % will be used to identify to which height zone the branch belongs to
avgInt = mean(branchPoints(:,2)); % calculates the average intensity value for the branch
sdInt = std(branchPoints(:,2)); % calculates the intensity standard deviation value for the branch
end
function getDist = getDist2Point22(point1, point2, extPoint)
% s= [point2(1) - point1(1);point2(2) - point1(2);point2(3) - point1(3)];
% M0= extPoint;
% M1 = point1;
% m2m1 = [M1(1)-M0(1) M1(2)-M0(2) M1(3)-M0(3)];
% getDistu = norm(cross(m2m1,s),2)/norm(s,2);
% distArr = getDist2Point(branchTipArray((1:100),(1:3)), stemPointArr(1:100,:), lidarDataArraytmp(1:100,(1:3)));
getDist = zeros(size(point1,1),1);
x = extPoint; %some point
a = point1; %segment points a,b
b = point2;
v = b-a;
w = x-a;
c1 = diag(w*v');
c2 = diag(v*v');
cless = find(c1<0);
cmore= find(c2<c1);
remCind = setdiff(1:1:size(point1,1), unique([cmore; cless]));
if(~isempty(cless))
getDist(cless,1) = sqrt(sum((x(cless,:)' - a(cless,:)').^ 2))';
%return;
end
if(~isempty(cmore))
getDist(cmore,1) = sqrt(sum((x(cmore,:)' - b(cmore,:)').^ 2))';
%return;
end
if(~isempty(remCind))
b1= c1(remCind)./c2(remCind);
pb= a(remCind,:) + repmat(b1,[1 3]).*v(remCind,:);
getDist(remCind,1) = sqrt(sum((x(remCind,:)' - pb').^ 2));
end
% d_ab = norm(a-b);
% d_ax = norm(a-x);
% d_bx = norm(b-x);
% if dot(a-b,x-b)*dot(b-a,x-a)>=0
% A = [a,1;b,1;x,1];
% getDist = abs(det(A))/d_ab;
% else
% getDist = min(d_ax, d_bx);
% end
end
function isPointWithin = isWinthinArc(v1, v2, inputPoints, radius)
isPointWithin = [];
for i =1:1:size(inputPoints,1)
x = inputPoints(i,1);
y = inputPoints(i,2);
isClkwisetoV1 = -v1(1)*y + v1(2)*x > 0;
isClkwisetoV2 = -v2(1)*y + v2(2)*x > 0;
withinRad = x*x + y*y <= radius.^2;
if(not(isClkwisetoV1) && isClkwisetoV2 && withinRad)
isPointWithin = [isPointWithin i];
end
end
end
function [extremeLiDARDataArray, connectComponentArray, connectComponentindexArr] = removeSparseClusters(extremeLiDARDataArray, connectComponentArray, connectComponentindexArr)
% Remove branch clusteres with less than 10 points (as they can
% give misleading branch skeleton)
idrm = [];
for j = 1:1:size(connectComponentArray,2)
ss = connectComponentArray{j};
if(size(ss,1) > 10)
% plot3(ss(:,1), ss(:,2), ss(:,3), '*', 'Color', [rand() rand() rand()]);
else
idrm = [idrm j];
end
end
extremeLiDARDataArray = removerows(extremeLiDARDataArray,'ind',idrm);
connectComponentArray = removerows(connectComponentArray','ind',idrm)';
connectComponentindexArr = removerows(connectComponentindexArr','ind',idrm)';
end
function [connectComponentArray,connectComponentindexArr, seedPointsArray] = getConnectedComponetsOld(lidarDataArray, seedPointsArray)
outerLoopCnt= 1;
noOfIterations = 10; NumNearestPoints = 5;
%connectComponentArray = zeros(NumNearestPoints*noOfIterations,3,(compEndIndex-compStartIndex));
I =[];
for clstStartPointIndex = 1:1:size(seedPointsArray,1) %length(extremeLiDARDataArray)-50:1:length(extremeLiDARDataArray)-51+noPoints
seletedIndices = [];
cntd =1; clusteredLiDARDataArray = zeros(NumNearestPoints*noOfIterations,3);
startPoint = [seedPointsArray(clstStartPointIndex,(1:3)) seedPointsArray(clstStartPointIndex,(8))];
startPoint1 = startPoint;
% [double(startPoint(1)) double(startPoint(2)) double(startPoint(3))]
otherPoints = [lidarDataArray(:,(1:3)) lidarDataArray(:,(8)) ];
otherPoints = removerows(otherPoints,'ind',I);
for loopCnt = 1:1:noOfIterations
%(find(and(lidarDataArray(:,3) > startPoint(3)-10, lidarDataArray(:,3) < startPoint(3)+10)),(1:3));
[~,I] = pdist2(otherPoints,startPoint,'euclidean','Smallest',NumNearestPoints);
temotherPoints = [otherPoints(I,:)];
sortLiDArray = sortrows(temotherPoints,[3]); %order by 3rd column in decresing order.
a1a = otherPoints(I(2),:); b2b = startPoint1(1,(1:4));
dst = pdist([a1a;b2b],'euclidean');
if(dst<2)
clusteredLiDARDataArray(cntd:cntd+(NumNearestPoints-1),:) = otherPoints(I,1:3);
% seletedIndices = [seletedIndices; I];
cntd = cntd + NumNearestPoints;
end
startPoint = sortLiDArray(NumNearestPoints,(1:4));
otherPoints(I,:) = 0;
end
clusteredLiDARDataArray = clusteredLiDARDataArray(clusteredLiDARDataArray(:,3)~=0,:);
connectComponentArray{outerLoopCnt} = clusteredLiDARDataArray;%zeros(NumNearestPoints*noOfIterations,3,(compEndIndex-compStartIndex));
for ccCont =1:1:size(clusteredLiDARDataArray,1)
aa = clusteredLiDARDataArray(ccCont,:);
%idxfd = find(and(and(aa(:,1)==lidarDataArray(:,1) ,aa(:,2)==lidarDataArray(:,2)), aa(:,3)==lidarDataArray(:,3) ));
idx = lidarDataArray(find(and(and(aa(:,1)==lidarDataArray(:,1) ,aa(:,2)==lidarDataArray(:,2)), aa(:,3)==lidarDataArray(:,3) )),7);
seletedIndices = [seletedIndices; idx];
end
connectComponentIndicesArray{outerLoopCnt} = seletedIndices;
outerLoopCnt = outerLoopCnt + 1;
end
retConnectedComp = connectComponentArray;
[seedPointsArray, connectComponentArray, connectComponentindexArr] = removeSparseClusters(seedPointsArray, retConnectedComp, connectComponentIndicesArray);
end
function [extremeLiDARDataArray, extremeLiDARDataIndex, connectComponentArray, connectComponentindexArr] = ...
getConnectedComponets(stData, seedPointsArray, NumNearestPoints, distTheshold)
connectComponentArray = []; invalidClusterIndx = [];
seedPointsArrayIndex = seedPointsArray(:,6);
cnt = 1;
for clstStartPointIndex = 1:1:size(seedPointsArray,1) %length(extremeLiDARDataArray)-50:1:length(extremeLiDARDataArray)-51+noPoints
startPoint = seedPointsArray(clstStartPointIndex,(1:3));
startPoint = [startPoint seedPointsArray(clstStartPointIndex,(7)) seedPointsArray(clstStartPointIndex,(6))];
%startPoint = [startPoint stData.lidarDataDensityArr(seedPointsArrayIndex(clstStartPointIndex))];
otherPoints = stData.lidarDataArray(:,(1:3));
otherPoints = [otherPoints stData.lidarDataArray(:,7) stData.lidarDataArray(:,6)];
% to set the threshold
lidstartpoint = startPoint(1:3);
otherstartpoints = seedPointsArray(:,1:3);
[neigdistTheshold, ~] = pdist2(otherstartpoints, lidstartpoint,'euclidean','Smallest',2);
neigdistTheshold = mean(neigdistTheshold(2:2));
connectComponentindexArr{clstStartPointIndex} = [];
connectComponentindexArr{clstStartPointIndex} = unique(getProminalPoints(startPoint, otherPoints, neigdistTheshold*10, NumNearestPoints, 30, []));
%hold on;
%ff = connectComponentindexArr{clstStartPointIndex};
%plot3(stData.lidarDataArray(ff,1), stData.lidarDataArray(ff,2), stData.lidarDataArray(ff,3),'*','Color',[0.5 0 0]);
idx = connectComponentindexArr{clstStartPointIndex};
if(length(idx) < 4) % ignore clusters with less than 4 LiDAR points (made constant)
invalidClusterIndx = [invalidClusterIndx clstStartPointIndex];
else
newnd=[];
for nidx = 1:1:size(idx,1);
newnd = [newnd find(stData.lidarDataArray(:,6)==idx(nidx))];
end
connectComponentArray{cnt} = stData.lidarDataArray(newnd,1:3);
cnt = cnt + 1;
end
end
% Remove rows with invalid clusters identified from the above step.
extremeLiDARDataArray = removerows(seedPointsArray,'ind',invalidClusterIndx);
extremeLiDARDataIndex = removerows(seedPointsArrayIndex,'ind',invalidClusterIndx);
end
function retPointIndx = getProminalPoints(startPoint, otherPoints, distTheshold, NumNearestPoints, maxIter, retPointIndx)
[dist,Indx] = pdist2(otherPoints,startPoint,'euclidean','Smallest',NumNearestPoints);
retPointIndx = [retPointIndx;Indx];
maxIter = maxIter-1;
for i = 2:1:2
if(exp(dist(i)) < distTheshold) && maxIter > 0
retPointIndx = [retPointIndx;getProminalPoints(otherPoints(retPointIndx(size(retPointIndx,1)),:), otherPoints, distTheshold, NumNearestPoints, maxIter, retPointIndx)];
else
retPointIndx = [];
end
end
end
function retPointIndx = getProminalPointsNew(startPoint, otherPoints, distTheshold, NumNearestPoints, maxIter, retPointIndx)
[dist,Indx] = pdist2(otherPoints(:,1:4),startPoint(1,1:4),'euclidean','Smallest',NumNearestPoints);
retPointIndx = [retPointIndx; otherPoints(Indx, 5)];
otherPoints = removerows(otherPoints,'ind',Indx(1:end-1));
maxIter = maxIter-1;
for i = 2:1:2
if(dist(i) < distTheshold) && maxIter > 0
aa = find(otherPoints(:,5)==retPointIndx(end));
retPointIndx = [retPointIndx;getProminalPointsNew(otherPoints(aa,:), otherPoints, distTheshold, NumNearestPoints, maxIter, retPointIndx)];
else
retPointIndx = [];
end
end
end
function retDistWeights = calculateDistWeights(lidarDataArray, neighbourhoodSize)
[nearPoint,~] = pdist2(lidarDataArray(:,(1:3)),lidarDataArray(:,(1:3)),'euclidean','Smallest',25);
nearPoint(nearPoint>=neighbourhoodSize) = 0; % make dist 0 for points > threshold neighbourhoodDist
nearPoint = nearPoint(2:end,:); % remove first line because it is distance to point itself.
neigDensityArr = sum(nearPoint~=0,1)'; % find non zero elenents in the array for every column
retDistWeights = neigDensityArr/norm(neigDensityArr); % normalization
end
function plotConnectedComponents(connectComponentArray)
for i = 1:1:size(connectComponentArray,3);
clusteredLiDARDataArray = connectComponentArray(:,:,i);
plot3(clusteredLiDARDataArray(:,1), clusteredLiDARDataArray(:,2), clusteredLiDARDataArray(:,3),'*','Color',[0.5 0 0]);
hold on; camproj perspective; rotate3d on; view(3), axis vis3d; axis equal; axis on;
end
end
function [stemPointArr,D1] = getStemPointFromPCA(extremeLiDARDataArray, connectComponentArray,retMaxXY)
stemPointArr = zeros(size(connectComponentArray,2),3);
slopeValArr = [];
D1={};
for i = 1:1:size(connectComponentArray,2);
% Get modified connected components, after adding prospestive stem
[stemPointArr(i,:), ~, D1{i}] = getModifiedConnectedComp(connectComponentArray{i},extremeLiDARDataArray(i,(1:3)),retMaxXY);
end
end
% Plot the line
function [retSlope, lineLength, pntDistToline, maxeval, eigRatio, totalPoints] = getBestFitLine(stData, dataArray, compStartPoint, compEndPoint, slopeThreshold, speciesCode, isPlotON, plotOn, colorArr)
% Fit a line throught the cluster of points (to get branch skeleton)
[m,p,pntDistToline] = best_fit_line(dataArray(:,1),dataArray(:,2), dataArray(:,3));
% Calcuate the slope of the line (in Degrees)
retSlope = subspace(p',[p(1:2) 0]')*(180/pi);
% find the nearest point on the stem where the branch lines crosses
% as as line start point
stemBottom =[stData.retMaxXYZ(:,1:2) 0];
stemTop =[stData.retMaxXYZ(:,1:2) 100];
P3 = [m(1)+p(1)*100 m(2)+p(2)*100 m(3)+p(3)*100];
P4 = [m(1)+p(1)*-100 m(2)+p(2)*-100 m(3)+p(3)*-100];
[distToStem, nearestStemPoints] = distBtwLineSegments(stemBottom, stemTop, P3, P4);
%pt = nearestStemPoints{2};
% find the extreme point in cluster as line end point
[~, Dindx] = max(sqrt(sum((repmat(compStartPoint,size(dataArray,1),1) - dataArray).^ 2,2)));
endpnt = dataArray(Dindx,:);
%endpnt = compEndPoint; % if branch tips are sure to be end points
%hold on;
%plot3(pt(1),pt(2),pt(3),'*','Color','r', 'MarkerSize', 18);
% distance from cluster centre to branch start point(i.e. near trunk)
dst1 = pdist2(m(1,:),compStartPoint,'euclidean','Smallest',1);
% distnace of Cluster centre from the branch exterior points.
dst2 = pdist2(endpnt,m(1,:),'euclidean','Smallest',1);
% Considering that points are located at different directions around the stem.
if(endpnt(1,1)>0)
t=-dst2:0.2:dst1;
else
t= -dst1:0.2:dst2;
end
x1=m(1)+p(1)*t; y1=m(2)+p(2)*t; z1=m(3)+p(3)*t;
% jugaad fix (need to correct)
DI = max(sqrt(sum((repmat(compStartPoint,size(x1,2),1) - [x1' y1' z1']).^ 2,2)));
DIend = max(sqrt(sum((compStartPoint - endpnt).^ 2,2)));
if(DI>DIend+0.3)
t =-t; x1=m(1)+p(1)*t; y1=m(2)+p(2)*t; z1=m(3)+p(3)*t;
end
if(~strcmp(speciesCode,'pc'))
trsh = 5;
else
trsh =2;
end
% Plot line
if(abs(retSlope) < slopeThreshold && compStartPoint(3) > trsh)
if(and(isPlotON,plotOn))
%hold on
% plot3(compStartPoint(1),compStartPoint(2),compStartPoint(3),'.','Color',[0 1 0],'MarkerSize',40);
% plot3(endpnt(1),endpnt(2),endpnt(3),'.','Color',[1 0 0],'MarkerSize',40);
%plot3(m(1),m(2),m(3),'.','Color',[0 0 1],'MarkerSize',40);
lnData = [compStartPoint; endpnt];
p1 = line(lnData(:,1),lnData(:,2),lnData(:,3), 'Color',[139/256 69/256 19/256],'LineWidth', 2); alpha(0.9);
p1.Color(4) = 0.7;
%plot3(x1,y1,z1,'LineWidth',3, 'Color',colorArr);
%hold off;
end
end
lineLength = (dst1 + dst2);
% Calculate Eigen features
retEigenData = getBestEigenDirection(dataArray);
evec = retEigenData{1};
eval = retEigenData{2};
% handle cases where only one/two PC is available.
maxeval = 0; eigRatio = 1;
if(length(eval)==3)
maxeval = max(eval(2:end));
eigRatio = eval(2)/eval(3);
elseif(length(eval)==2)
maxeval = -1; %max(eval(2:end));
eigRatio = -1;
else
maxeval = -1; eigRatio =-1;
end
% total no of LiDAR point in the branch cluster
totalPoints = size(dataArray,1);
end
function dataAtt = dataPreProcessing(singleTreeLiDARdata)
% Write normalized data to a table for performance improvement
lidarDataArr = normalizeLiDARData(write2table(singleTreeLiDARdata));
%lidarDataArray = lidarDataArray(randperm(size(lidarDataArray,1),9000),:);
treeWidth = max(lidarDataArr(:,1)); treeHeight = max(lidarDataArr(:,2)); % to calculate max wisth/breadth to set plot width
dataAtt.maxTreeWidth = max(treeWidth, treeHeight) + 0.5; % Keep it as 5 if issues arise
dataAtt.mintreeHeight = min(lidarDataArr(:,3));
dataAtt.maxtreeHeight = max(lidarDataArr(:,3));
dataAtt.treeHeight = dataAtt.maxtreeHeight - dataAtt.mintreeHeight;
dataAtt.htDeduction = dataAtt.treeHeight*0.05; % to get rid of ground noise points
% Crown height
lidarDataArr = lidarDataArr(and(lidarDataArr(:,3) > min(lidarDataArr(:,3))+ dataAtt.htDeduction, lidarDataArr(:,3) < max(lidarDataArr(:,3))),:);
dataAtt.minCrownHeight = getMinCrownHeight(lidarDataArr);
dataAtt.maxCrownHeight = max(lidarDataArr(:,3));
dataAtt.crownHeight = dataAtt.maxCrownHeight - dataAtt.minCrownHeight;
%lidarDataArray = lidarDataArray(find(abs(lidarDataArray(:,2)).^2 + abs(lidarDataArray(:,1)).^2 > 0.1),:);
%lidarDataArray = lidarDataArray(find(abs(lidarDataArray(:,2)).^2 + abs(lidarDataArray(:,1)).^2 < 12),:);
% Identify the center point of the tree (in top view).
dataAtt.retMaxXYZ = findMaxHeightXY(lidarDataArr);
% Original LiDAR data Array
index = 1:1:size(lidarDataArr,1);
lidarDataDensityArr = calculateDistWeights(lidarDataArr,0.25);
lidarDataArr = [lidarDataArr index' lidarDataDensityArr];
dataAtt.lidarDataArrayComplete = lidarDataArr(:,1:8);
% Sparsified LiDAR data Array
lidarDataArrSampled = lidarDataArr(lidarDataDensityArr(:,1) >= 0.0010,:);
dataAtt.lidarDataArray = lidarDataArrSampled(:,1:8);
end
function eigDetails = getEigVetStemProximities(dataArr, extremeLiDARDataPoint, retMaxXY, plotPCAxisOn)
retEigenData = getBestEigenDirection(dataArr);
retEigPoints = retEigenData{1};
retEigvalues = retEigenData{2};
stemBottom = [retMaxXY(1);retMaxXY(2);0];
stemTop = [retMaxXY(1);retMaxXY(2);retMaxXY(3)];
eigDetails.distToStematCrossingArr = []; eigDetails.nearestStemPointsArr = []; eigDetails.crossingDistanceArr = [];
eigDetails.pcVector =[]; eigDetails.pcValue =[];
for ivCnt =1:1:size(retEigPoints,2)
pcVector = retEigPoints(:,ivCnt); eigDetails.pcVector = [eigDetails.pcVector pcVector];
pcValue = retEigvalues(ivCnt); eigDetails.pcValue = [eigDetails.pcValue pcValue];
P3 = pcVector *10; % 10 is used as a large value as there exits no trees with width > 10 m in dataset
P3(1)=P3(1)+extremeLiDARDataPoint(1); P3(2)=P3(2)+extremeLiDARDataPoint(2); P3(3) = P3(3)+extremeLiDARDataPoint(3);
P4 = pcVector *-10; % 10 is used as a large value as there exits no trees with width > 10 m
P4(1)=P4(1)+extremeLiDARDataPoint(1); P4(2)=P4(2)+extremeLiDARDataPoint(2); P4(3) = P4(3)+extremeLiDARDataPoint(3);
if(plotPCAxisOn && ivCnt ==1)
vect2= [P3';P4'];
hold on; line(vect2(:,1),vect2(:,2),vect2(:,3),'LineWidth' ,3);
end
% The function provides what is the mimimum distance between the stem line and the line formed using the PC vector direction.
[distToStem,nearestStemPoints] = distBtwLineSegments(stemBottom, stemTop, P3, P4);
crossingDistance = norm(nearestStemPoints{2}' - extremeLiDARDataPoint);
% Store details of each of the PC vectors in array
eigDetails.distToStematCrossingArr = [eigDetails.distToStematCrossingArr distToStem];
eigDetails.nearestStemPointsArr = [eigDetails.nearestStemPointsArr; nearestStemPoints{2}'];
eigDetails.crossingDistanceArr = [eigDetails.crossingDistanceArr crossingDistance];
end
end
function [point, PricComp1Slope, D1] = getModifiedConnectedComp(dataArr,extremeLiDARDataPoint,retMaxXY)
% find the closest eigen vector pointing towards the stem
eigDetailsA = getEigVetStemProximities(dataArr, extremeLiDARDataPoint, retMaxXY, false);
dx = eigDetailsA.distToStematCrossingArr;
nsa = eigDetailsA.nearestStemPointsArr;
% retEigPoints = eigDetailsA.pcVector;
% evalj = eigDetailsA.pcValue;
D1 = eigDetailsA.crossingDistanceArr;
%point =nsa(2,:);
DL = [dx(1)*D1(1) dx(2)*D1(2) dx(3)*D1(3)];
[maxD,maxDi] = min(DL);
point = 0;
slopeA =[];
if(dx(1)*D1(1)==maxD)
point = nsa(1,:);
elseif(dx(2)*D1(2)==maxD)
point = nsa(2,:);
else
point = nsa(3,:);
end
point1 = point/norm(point);
PricComp1Slope = subspace(point1',[point1(1:2) mean(dataArr(:,3))]')*(180/pi);
%point = nsa(1,:);
% c=[]; e=[];
% for v =1:1:size(retEigPoints,2)
% a = retEigPoints(:,v);
% b = [0;0;1];
% d = [0;0;-1];
% c = [c acos((a'*b)/(norm(a)*norm(b)))*(180/pi)];
% e = [e acos((a'*d)/(norm(a)*norm(d)))*(180/pi)];
% f= c-e;
% end
% [valc, indc] = min(abs(f));
%
% if(abs(f(indc))>2000000)
% DL = [D1(1) D1(2) D1(3)];
% [maxD] = min(DL);
% point = 0;
end
function lidarDataArray = normalizeLiDARData(lidarDataArray)
midxPoint = (max(lidarDataArray(:,1))- min(lidarDataArray(:,1)))/2;
midyPoint = (max(lidarDataArray(:,2))- min(lidarDataArray(:,2)))/2;
lidarDataArray(:,1) = lidarDataArray(:,1)- min(lidarDataArray(:,1));
lidarDataArray(:,2) = lidarDataArray(:,2)- min(lidarDataArray(:,2));
lidarDataArray(:,3) = lidarDataArray(:,3)- min(lidarDataArray(:,3));
lidarDataArray(:,1) = lidarDataArray(:,1)- midxPoint;
lidarDataArray(:,2) = lidarDataArray(:,2)- midyPoint;
end
function lidarBranchTips = removeRepetitions(stData, extremeLiDARDaraArray, CutoffDistBetwVect)
dstarr = pdist2(extremeLiDARDaraArray(:,1:2), stData.retMaxXYZ(1:2));
extremeLiDARDaraArray = [extremeLiDARDaraArray dstarr];
extremeLiDARDaraArray = sortrows(extremeLiDARDaraArray,[3 8]);
lidarBranchTips =[];
for j=1:1:max(extremeLiDARDaraArray(:,3))
ss = CutoffDistBetwVect*((j*1)/max(extremeLiDARDaraArray(:,3)));
CutoffDistBetwVect1 = CutoffDistBetwVect - ss; %ar =1.2; la = 1.8; % CutoffDistBetwVect -
extremeLiDARDdataBysection = extremeLiDARDaraArray(find(and(extremeLiDARDaraArray(:,3) >= j, extremeLiDARDaraArray(:,3) < j + 1)),:);
if(size(extremeLiDARDdataBysection,1) > 0)
DistBwExtrmPoints = tril(squareform(pdist(extremeLiDARDdataBysection(:,1:3),'euclidean')));
[rowIdx,~] = ind2sub(size(DistBwExtrmPoints),find(DistBwExtrmPoints < CutoffDistBetwVect1 & DistBwExtrmPoints > 0));
extremeLiDARDdataBysection = removerows(extremeLiDARDdataBysection,'ind',unique(rowIdx));
lidarBranchTips = [lidarBranchTips; extremeLiDARDdataBysection];
end
end
% lidarBranchTips = removerows(extremeLiDARDaraArray,'ind',indx);
end
function [m,p,s] = best_fit_line(x,y,z)
% x,y,z are n x 1 column vectors of the three coordinates
% of a set of n points in three dimensions. The best line,
% in the minimum mean square orthogonal distance sense,
% will pass through m and have direction cosines in p, so
% it can be expressed parametrically as x = m(1) + p(1)*t,
% y = m(2) + p(2)*t, and z = m(3)+p(3)*t, where t is the
% distance along the line from the mean point at m.
% s returns with the minimum mean square orthogonal
% distance to the line.
% RAS - March 14, 2005
[n,mx] = size(x); [ny,my] = size(y); [nz,mz] = size(z);
if (mx~=1)|(my~=1)|(mz~=1)|(ny~=n)|(nz~=n)
error('The arguments must be column vectors of the same length.')
end
m = [mean(x),mean(y),mean(z)];
w = [x-m(1),y-m(2),z-m(3)]; % Use "mean" point as base
a = (1/n)*w'*w; % 'a' is a positive definite matrix
[u,d,v] = svd(a); % 'eig' & 'svd' get same eigenvalues for this matrix
p = u(:,1)'; % Get eigenvector for largest eigenvalue
s = d(2,2)+d(3,3); % Sum the other two eigenvalues
end
function retVector = rotateVector(inputVector, teeta)
rotationMatrix = [cos(teeta) -sin(teeta) 0; sin(teeta) cos(teeta) 0; 0 0 1];
retVector = rotationMatrix*inputVector';
end
function plotLiDARData(lidarDataArray, gridOnOffParameterCylin, gridOnOffParameter,htDeduction,showRawLiDARParameter,zDiv,retMaxXYZ)
if(showRawLiDARParameter==true)
plot3(lidarDataArray(:,1), lidarDataArray(:,2), lidarDataArray(:,3),'.','MarkerSize',5,'Color',[0 0.5 0]);
end
if(gridOnOffParameterCylin)
r=max(max(lidarDataArray(:,1:2)))+1;
maxZ = max(lidarDataArray(:,3)); minZ = min(lidarDataArray(:,3));
xamples =[]; yamples =[]; %zDiv = 15;
for zStep = minZ :(maxZ - minZ)/zDiv : maxZ;
teta=-pi:0.314/4:pi;
x=r*cos(teta) + retMaxXYZ(1);
y=r*sin(teta) + retMaxXYZ(2);
plot3(x,y,zeros(1,numel(x))+zStep,'Color','k', 'LineWidth',1);
plot3(x/2,y/2,zeros(1,numel(x))+zStep,'Color','k', 'LineWidth',1);
plot3(x/4,y/4,zeros(1,numel(x))+zStep,'Color','k', 'LineWidth',1);
xamples = x(1:4:size(x,2));
yamples = y(1:4:size(y,2));
plot3(xamples,yamples,zeros(1,size(xamples,2))+zStep,'.','Color','k');
centMat = [repmat(retMaxXYZ(:,1:2),size(xamples,2)*2,1) zeros(1,size(xamples,2)*2)'+zStep ];
centMat([1:2:size(centMat,1)],:) = [xamples' yamples' zeros(1,size(xamples,2))'+zStep];
line(centMat(:,1), centMat(:,2), centMat(:,3),'Color','k','LineWidth',1);
end
for i = 1:1:size(xamples,2)
plot3([xamples(i) xamples(i)],[yamples(i) yamples(i)],[6.2 maxZ], '-', 'Color', 'k', 'LineWidth',1);
end
end
if(gridOnOffParameter)
maxX = max(lidarDataArray(:,1)); minX = min(lidarDataArray(:,1));
maxY = max(lidarDataArray(:,2)); minY = min(lidarDataArray(:,2));
maxZ = max(lidarDataArray(:,3)); minZ = min(lidarDataArray(:,3))-htDeduction;
%xDiv = 10; yDiv = 10; zDiv = 25;
x = minX:(maxX - minX)/zDiv: maxX;
y = minY:(maxY - minY)/zDiv: maxY;
z = minZ:(maxZ - minZ)/zDiv: maxZ;
[X1 Y1 Z1] = meshgrid(x([1 end]),y,z);
X1 = permute(X1,[2 1 3]); Y1 = permute(Y1,[2 1 3]); Z1 = permute(Z1,[2 1 3]);
X1(end+1,:,:) = NaN; Y1(end+1,:,:) = NaN; Z1(end+1,:,:) = NaN;
[X2 Y2 Z2] = meshgrid(x,y([1 end]),z);
X2(end+1,:,:) = NaN; Y2(end+1,:,:) = NaN; Z2(end+1,:,:) = NaN;
[X3 Y3 Z3] = meshgrid(x,y,z([1 end]));
X3 = permute(X3,[3 1 2]); Y3 = permute(Y3,[3 1 2]); Z3 = permute(Z3,[3 1 2]);
X3(end+1,:,:) = NaN; Y3(end+1,:,:) = NaN; Z3(end+1,:,:) = NaN;
%#figure('Renderer','opengl')
h = line([X1(:);X2(:);X3(:)], [Y1(:);Y2(:);Y3(:)], [Z1(:);Z2(:);Z3(:)]);
set(h, 'Color',[10/256,10/256,10/256], 'LineWidth',0.5, 'LineStyle','-')
end
% hold off;
end
function returnData = LoadData(fullFileName)
returnData = lasdata(fullFileName);
end
function retTable = write2table(lasFile)
retTable = zeros(size(lasFile.x,1),6);
retTable(:,1) = lasFile.x;
retTable(:,2) = lasFile.y;
retTable(:,3) = lasFile.z;
retTable(:,4) = get_classification(lasFile);
retTable(:,5) = lasFile.get_return_number;
retTable(:,6) = get_intensity(lasFile);
end
function [distance, dd] = distBtwLineSegments(p1, p2, p3, p4)
u = p1 - p2;
v = p3 - p4;
w = p2 - p4;
a = dot(u,u);