-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallGrowing.m
212 lines (155 loc) · 7.29 KB
/
wallGrowing.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
function [ newPointLable,newClustersTagAndPCAndCenters ] =...
wallGrowing( xyz,pointLable,angleThreshold,...
distanceBetweenClusters,NumOfNeighbors,frameThickness,...
frameExtraLength,maxDistanceForNanPoint,...
minArea,distanceBetweenPoints,wallRecVoxelSize,dialateRadius)
%closeClustersList=connectedClusters(xyz,pointLable,connectedDistance,connectedK,distanceBetweenPoints);
tmpIdx=~isnan(pointLable);
idxOfWalls=find(tmpIdx);
numOfPixels=floor((1/wallRecVoxelSize)*dialateRadius);
closeClustersList=seperatePointsToDiffrentWalls(xyz,xyz(tmpIdx,:),wallRecVoxelSize,numOfPixels);
[~,numOfConnectedClusters]=size(closeClustersList);
newPointLable=pointLable;
%%
connectedClustersList={};
%parfor
%get connected components
parfor i=1:numOfConnectedClusters
clusterIdx=closeClustersList{i};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%print group before merge
% figure;
% tmpLable=newPointLable(clusterIdx);
% tmpCloud=pointCloud(xyz(clusterIdx,:));
% tmpColored = colorPointCloud(tmpCloud,tmpLable);
% pcshow(tmpColored);
% xlabel('x');ylabel('y');zlabel('z');
% title('merged walls');
% daspect([1 1 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
connectedClustersList{i} =connectWalls( xyz(clusterIdx,:),newPointLable(clusterIdx),...
angleThreshold,distanceBetweenClusters,NumOfNeighbors,distanceBetweenPoints);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%print group after merge
% figure;
% tmpGroups=connectedClustersList{i};
% tmpGroups=tmpGroups{1};
% tmpIdx=find(ismember(newPointLable,tmpGroups));
% newPointLable(tmpIdx)=20;
%
% tmpLable=newPointLable(clusterIdx);
% tmpCloud=pointCloud(xyz(clusterIdx,:));
% tmpColored = colorPointCloud(tmpCloud,tmpLable);
% pcshow(tmpColored);
%
% xlabel('x');ylabel('y');zlabel('z');
% title('merged walls');
% daspect([1 1 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%% change tags
[~,sizeOfconnectedClustersList]=size(connectedClustersList);
for connectedClustersNum=1:sizeOfconnectedClustersList
groupsToUnite=connectedClustersList{connectedClustersNum};
[~,sizeOfgroupsToUnite]=size(groupsToUnite);
for GroupsToUniteNum=1:sizeOfgroupsToUnite
%num of diffrent groups to join
[~,numOfNewGroups]=size(groupsToUnite{GroupsToUniteNum});
%loop and change tags and for each cell unite the groups
for i=1:numOfNewGroups
%get the array of groups to join
arrayOfUniteGroups=groupsToUnite{GroupsToUniteNum}{i};
%get the size of the group to join
[sizeOfGroups,~]=size(arrayOfUniteGroups);
%if theere is no need to join
if sizeOfGroups==1
continue
end
%
for groupNum=2:sizeOfGroups
%get the idx that we need to change
indexToChange=newPointLable==arrayOfUniteGroups(groupNum);
newPointLable(indexToChange)=arrayOfUniteGroups(1);
end
end
end
end
%% remove small walls
newPointLable = AreaCalcAndRemove(...
xyz,newPointLable ,minArea );
%% save normals and..
clustersTagAndPCAndCenters = PCAOfClustersAndCenter(xyz,newPointLable);
newClustersTagAndPCAndCenters = clustersTagAndPCAndCenters;
%% add nan to walls.
%get all the clusters
allClusters=clustersTagAndPCAndCenters{1};
%get all the egenvectors
clusterEgenVectors=clustersTagAndPCAndCenters{2};
%get size of allClusters
[sizeOfAllClusters,~]=size(allClusters);
for i=1:sizeOfAllClusters
currentClusterNum=allClusters(i);
%get all the nan points
nanIdx=find(isnan(newPointLable));
%get the cluster points
currentClusterPointsIdx=find(newPointLable==currentClusterNum);
%project all cluster point on new axis
clusterProjection=xyz(currentClusterPointsIdx,:)*clusterEgenVectors(:,:,i);
%up direction
%up=[0 0 1];
%upProjection=up*clusterEgenVectors(:,:,i);
%project all nan point on new axis
nanPointsProjection=xyz(nanIdx,:)*clusterEgenVectors(:,:,i);
% get min max
minOfCluster=min(clusterProjection);
maxOfCluster=max(clusterProjection);
%get the points in between
boolGoodIdx=nanPointsProjection(:,1)<maxOfCluster(1)+frameExtraLength & nanPointsProjection(:,2)<maxOfCluster(2)+frameExtraLength;
boolGoodIdx=boolGoodIdx & nanPointsProjection(:,1)>minOfCluster(1)-frameExtraLength & nanPointsProjection(:,2)>minOfCluster(2)-frameExtraLength;
boolGoodIdx=boolGoodIdx & nanPointsProjection(:,3)>minOfCluster(3)-frameThickness & nanPointsProjection(:,3)<maxOfCluster(3)+frameThickness;
goodIdx=find(boolGoodIdx);
%we need to search for all goodIdx points that close enogh to the
%cluster
%check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% figure;
% idxOfXYZ=nanIdx(goodIdx);
% pcshow(xyz(idxOfXYZ,:),[1 0 0]);
% xlabel('x');ylabel('y');zlabel('z');
% title('merged walls');
% daspect([1 1 1]);
% hold on;
% pcshow(xyz(currentClusterPointsIdx,:),[0 1 0]);
%close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tmp_Idx=[];
[numberOfnan,~]=size(goodIdx);
Mdl = KDTreeSearcher(xyz(currentClusterPointsIdx,:));
for goodIdxNumber=1:numberOfnan
%nanIdx is points in xyz with nan lable
%goodIdx is nan points that is close to the cluster
idxOfXYZ=nanIdx(goodIdx(goodIdxNumber));
[~ , D] = knnsearch(Mdl,xyz(idxOfXYZ,:),'k',1);
[minDist,~] = min(D);
if minDist<maxDistanceForNanPoint
newPointLable(idxOfXYZ)=currentClusterNum;
%needed for check
tmp_Idx=[tmp_Idx,idxOfXYZ];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% figure;
% idxOfXYZ=nanIdx(goodIdx(ismember(goodIdx,tmp_Idx) ));
% pcshow(xyz(idxOfXYZ,:),[1 0 0]);
% xlabel('x');ylabel('y');zlabel('z');
% title('merged walls');
% daspect([1 1 1]);
% hold on;
% pcshow(xyz(currentClusterPointsIdx,:),[0 1 0]);
%
% close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end