-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot3d.m
175 lines (160 loc) · 8.35 KB
/
plot3d.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
clear
close all
x = [-0.0462986306678939 -0.0445983132232155 -0.0460856399293174...
-0.0446382692456549 -0.0458980695176541 -0.0443595257664412...
-0.0450679335552641 -0.0613715343296299 -0.0458964238522471...
-0.0437554801684153 -0.0443629635713894 -0.0456586210666779...
-0.0490490980413487 -0.0465216924030692 -0.0451932363489714...
-0.0314237762655256 -0.0433998967348189 -0.0433836174102179...
-0.0311954950799321 -0.0443078893622584 -0.0443274489623910...
-0.0460016967757873 -0.0452526312219531 -0.0425952812752832...
-0.0426384020470851 -0.0433363555205429 -0.0447829061061852...
-0.0446815894175926 -0.0463431722783344 -0.0450522993274295...
-0.0420359641015271 -0.0460057389993592 -0.0430210352683455...
-0.0454509690499881 -0.0427265384501126 -0.0449425770884506...
-0.0465841966057391 -0.0445158634214035 -0.0427149269037689...
-0.0450083408418727 -0.0450380948035519 -0.0347562061152129...
-0.0435720641379072 -0.0511648563544511 -0.0425923632505494...
-0.0455844621004480 -0.0448736612539232 -0.0460905074164467...
-0.0446374475528109 -0.0447967162909612 -0.0439941567248151...
-0.0324944879191538 -0.0448130142236959 -0.0443561954643423...
-0.0466759428690460]';
y = [-0.0229185175978028 0.0222657633603937 0.00855031245162668...
-0.0409891899171168 -8.12251486412893e-5 -0.0522080105990298...
0.00751315163346284 -0.0990298252722104 0.0109233233154777...
-0.0718435273190361 -0.0479943495826301 -0.0130665935039823...
-0.0739759079657459 0.0153045196690851 -0.0455448537567502...
-0.0326375761853029 -0.110589568923239 -0.0913090142610235...
-0.0246267126536141 -0.0464524059278825 -0.0484716616874548...
0.00634859682496403 -0.0141102004349724 -0.0943052664524440...
-0.0986980480530366 -0.0221729380522960 -0.0516652598230434...
-0.0405832055094145 0.0107350404466709 -0.0214044735615335...
-0.118944465218712 0.0195600130053933 -0.0896382147275844...
0.0217307018327242 -0.105632381022837 -0.0341615191348370...
0.0222427074569520 -0.0453493599339101 -0.0928782474781229...
-0.0466252174370394 -0.0217819717514396 -0.122628033714009...
-0.0583003283968122 0.0125120084805672 -0.0942532057036884...
0.0197974303443588 -0.00142630888945058 0.0248700538613969...
-0.0207638425149549 -0.00135403904590127 -0.0535669643056640...
-0.0245667553884627 0.0210247549515988 -0.0479862750615108...
0.0177994927532551]';
z = [0.550254097564056 0.402316910772993 0.504926835261579 0.539730081334147...
0.516838951083883 0.517307369454682 0.506254089704606 0.456511752590412...
0.467146146078343 0.565553956236266 0.467476321874593 0.437058713421461...
0.443895269246945 0.495205606664267 0.547629557374719 0.360702422088232...
0.565924128422473 0.550996798635872 0.348057373384285 0.496989418938309...
0.578542110147525 0.507333923274695 0.497493879576203 0.500149385789978...
0.541489453887043 0.476029296385583 0.592635239394528 0.576813264885865...
0.467685638233636 0.479026073311360 0.504967848796559 0.488967064932650...
0.572148104243473 0.428318415683607 0.576395693542315 0.567558220329879...
0.484445401046769 0.547368917684610 0.567076714690426 0.584577801520786...
0.478878011378689 0.512016867882223 0.460227867457421 0.432056080639678...
0.500619063865377 0.466027627935290 0.518100883697252 0.480754818128370...
0.476768204746585 0.518041538004767 0.524500624165487 0.347329287871492...
0.438188314283623 0.467473810403554 0.493918299622636]';
% x = [-0.221938599791474 -0.227557663574298 -0.220317451719907...
% -0.211535695675013 -0.202884062484904 -0.209353710715222...
% -0.220297387658410 -0.189395483651475 -0.210956769119828...
% -0.188372999978252 -0.207399563582716 -0.204514108614224...
% -0.206575864131113 -0.202873916108036 -0.234255600126977...
% -0.208264527729494 -0.223754279278132 -0.189504487657586...
% -0.209414226751488 -0.220387092511711 -0.190788399549382...
% -0.197334717350073 -0.178959497329902 -0.200240224954247...
% -0.210861713257268 -0.211781270239224 -0.208864721609214...
% -0.209013189073219 -0.216798846026252 -0.222199255921393...
% -0.209194410289008 -0.207367792177556 -0.197483473580174...
% -0.209073999268204 -0.210150069102169 -0.219814628901363...
% -0.219215973409575 -0.204486658947074 -0.226514140775914...
% -0.218040825535686 -0.211831138721557 -0.188352215648861...
% -0.218780751707179 -0.222132754341131 -0.180304066831478...
% -0.208632438008694 -0.206381016341985 -0.220226794531900...
% -0.209029546541053 -0.216663734402877 -0.197459882489285]';
%
% y = [-0.0953752036938286 -0.139369709532030 -0.0531562511399783...
% -0.0627372695035152 -0.0186423450301216 -0.00398485333719822...
% -0.0816672853583729 0.0389471942985086 -0.00365572831401574...
% 0.0391481705867117 -0.0499914847730831 -0.0232237913562524...
% -0.0149133652797682 -0.0186699099179992 -0.118509407190110...
% -0.0668569061683134 -0.0910796571242715 0.0389989672403096...
% 0.00347992917496971 -0.0530040476189822 0.0448795691758552...
% 0.0435845528436319 -0.0232858186969612 -0.0285032561314500...
% -0.0742664830684661 -0.0148266112888143 0.00886011581500649...
% 0.00367624820003665 -0.0840184885916197 -0.112299917414776...
% -0.0583499606728689 -0.0496199495956523 0.0348845906470756...
% 0.0104204382142519 -0.0994880520856263 -0.0287467589924344...
% -0.0820745945665867 -0.0232296700181449 -0.0918512024011799...
% -0.0809706788263380 -0.0148166306084670 0.0391659520867431...
% -0.115421766518772 -0.112305676207449 -0.0232805258624663...
% -0.0146447070881230 -0.0576402799201216 -0.0293048999721725...
% 0.0104036641571169 -0.0354408663926108 0.0348624816256254]';
%
% z = [0.548181707945499 0.564944993651095 0.561661375928860 0.493459920315726...
% 0.460752340486443 0.526032273152536 0.569901355532906 0.401439467649828...
% 0.525807028222445 0.386737899692677 0.470873507017913 0.469053270650378...
% 0.508019402061946 0.460805485476076 0.625686494254413 0.459924539216132...
% 0.583491260416431 0.400913845338598 0.515720271963356 0.561727656289902...
% 0.454105606021020 0.456356294853774 0.351848202461016 0.439892212686545...
% 0.502446606627988 0.543085943121043 0.507606457949670 0.515519637220041...
% 0.526164108372284 0.537918947674570 0.484236002596450 0.472043506879205...
% 0.468843249925364 0.505130387817829 0.483177996269533 0.562506778520702...
% 0.535921178078892 0.469035401809234 0.583582651463903 0.534543165002289...
% 0.543315214201475 0.386765155048255 0.518285034574592 0.537698622973065...
% 0.351862253215371 0.507188850101729 0.482989831373111 0.563472019355520...
% 0.505175040366383 0.542665356141289 0.468842380153187]';
A = [x,y,z];
n = length(x);
plt = true
while true
% 1. Select a random subset of the original data. Call this subset the hypothetical inliers.
sample_idx = datasample(1:n,round(n/5),'Replace',false);
x1 = x(sample_idx);
y1 = y(sample_idx);
z1 = z(sample_idx);
% 2. A model is fitted to the set of hypothetical inliers.
[c,d] = fitPlane(x1,y1,z1);
% 3. All other data are then tested against the fitted model.
% Those points that fit the estimated model well, according to some
% model-specific loss function, are considered as part of the consensus set.
% average distance
l = abs(A*c-d);
num_fit = sum(l<0.001);
% 4. The estimated model is reasonably good if sufficiently many points
% have been classified as part of the consensus set.
if (num_fit > 0.50*n)
if (plt == true)
[x_p, y_p] = meshgrid(linspace(min(x1), max(x1)), linspace(min(y1),max(y1)));
f = @(x,y) (d - c(1)*x-c(2)*y)/c(3);
z_p = f(x_p,y_p);
plot3(x_p,y_p,z_p)
axis('square')
hold on
scatter3(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
% axis([-0.1,0.1,-0.1,0.1,-0.1,0.1])
end
return
end
% 5. Afterwards, the model may be improved by reestimating it using all
% members of the consensus set.
% skip
% plot the plane
end
function [c,d] = fitPlane(x1,y1,z1)
x_1 = x1-mean(x1);
y_1 = y1-mean(y1);
z_1 = z1-mean(z1);
% Use SVD and take the first two eigenvector (the principal components)
B = [x_1,y_1,z_1]';
[U,~,~] = svd(B);
% eigenvectorsl
p1 = U(:,1);
p2 = U(:,2);
% normal vectors
p3 = cross(p1,p2); % the normal vector of our plane
p3 = p3/norm(p3);
% find d from the average point
d = p3(1)*mean(x1) + p3(2)*mean(y1) + p3(3)*mean(z1);
c = p3;
end