-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_NE.m
169 lines (140 loc) · 4.96 KB
/
update_NE.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
function [N, E, check_update] = update_NE(vec, p1, N, E, EL, x, y)
check_update = 0;
check = 1;
Y = round(N);
place = 0;
mid_point = p1+(vec./2);
for a = 1:length(N)
if abs(norm(mid_point - N(a,:))) <= 80
temp = N(a,:);
place = a;
check = 0;
break;
end
end
% New node on intersectionpoint
if check
temp = round(mid_point);
Lia = ismember(Y, temp, 'rows');
if nnz(Lia)
N = Y;
return
end
temp_E = E';
num_nodes = length(N);
update_check = 0;
x(x==num_nodes) = num_nodes+1;
y(y==num_nodes) = num_nodes+1;
temp_E(temp_E==num_nodes) = num_nodes+1;
[tf, index_x] = ismember(x, temp_E, 'rows');
if ~tf
[tf, index_x] = ismember(flip(x), temp_E, 'rows');
end
if tf
x_2 = x(2);
temp_E(index_x,2) = num_nodes;
temp_E = [temp_E; [num_nodes x_2]];
update_check = 1;
%else
%viz_struct(N./10, E, 2, 2);
%fprintf("HM");
end
[tf, index_y] = ismember(y, temp_E, 'rows');
if ~tf
[tf, index_y] = ismember(flip(y), temp_E, 'rows');
end
if tf
y_2 = y(2);
temp_E(index_y,2) = num_nodes;
temp_E = [temp_E; [num_nodes y_2]];
update_check = 1;
%else
%viz_struct(N./10, E, 2, 2);
%fprintf("HM");
end
if update_check
temp_E = temp_E';
E = temp_E;
% Insert new node as the second to last node
temp = [N(1:num_nodes-1, :); mid_point; N(num_nodes, :)];
N = temp;
check_update = 1;
end
else % connect to closest node
E = E';
if (place == x(1)) || (place == x(2)) && (place == y(1)) || (place == y(2))
E = E';
elseif (place == x(1)) || (place == x(2))
% Find index of y-connection
[tf, index] = ismember(y, E, 'rows');
if ~tf
[tf, index] = ismember(flip(y), E, 'rows');
if ~tf
E = E';
return
end
end
% Found connection index
connection = E(index, :);
connection_1 = [connection(1) place]; % new connection
[tf_1, ~] = ismember(connection_1, E, 'rows'); % x_y
[tf_2, ~] = ismember(flip(connection_1), E, 'rows'); % y_x
if ~(tf_1 || tf_2) % Not previously connected
E(index, :) = connection_1;
end
connection_2 = [connection(2) place];
[tf_1, ~] = ismember(connection_2, E, 'rows');
[tf_2, ~] = ismember(flip(connection_2), E, 'rows');
if ~(tf_1 || tf_2)
E = [E; connection_2];
end
check_update = 1;
E = E';
elseif place == y(1) || place == y(2)
% Find index of x-connection
[tf, index] = ismember(x, E, 'rows');
if ~tf
[tf, index] = ismember(flip(x), E, 'rows');
if ~tf
E = E';
return
end
end
% Found connection index
connection = E(index, :);
connection_1 = [connection(1) place]; % new connection
[tf_1, ~] = ismember(connection_1, E, 'rows'); % x_y
[tf_2, ~] = ismember(flip(connection_1), E, 'rows'); % y_x
if ~(tf_1 || tf_2) % Not previously connected
E(index, :) = connection_1;
end
connection_2 = [connection(2) place];
[tf_1, ~] = ismember(connection_2, E, 'rows');
[tf_2, ~] = ismember(flip(connection_2), E, 'rows');
if ~(tf_1 || tf_2)
E = [E; connection_2];
end
check_update = 1;
E = E';
else
% Remove y-connection
[tf, index_y] = ismember(y, E, 'rows');
if ~tf
[tf, index_y] = ismember(flip(y), E, 'rows');
end
if tf
E(index_y,:) = [];
end
% Remove x-connection
[tf, index_x] = ismember(x, E, 'rows');
if ~tf
[tf, index_x] = ismember(flip(x), E, 'rows');
end
if tf
E(index_x,:) = [];
end
check_update = 1;
E = E';
end
end
end