-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.m
96 lines (80 loc) · 2.3 KB
/
mesh.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
function [Xcleaned, tri] = mesh(X, xL, xR, xColor)
% scan we are working on
% scandir = 'C:\\Users\\Yuki\\Documents\\MATLAB\\117\\proj\\teapot\\set_9'
% X = Xold;
% xL = xLold;
% xR = xRold;
% xColor = xColorold;
% threshold for pruning neighbors
nbrthresh = 10;
trithresh = 5;
% load in results of reconstruct
% load([scandir 'scandata.mat']);
X(1,:) = -X(1,:);
X(3,:) = -X(3,:);
%
% cleaning step 1: remove points outside known bounding box
% goodpoints = find( (X(1,:)>0) & (X(1,:)<20) & (X(2,:)>0) & (X(2,:)<25) & (X(3,:)>0) & (X(3,:)<20) );
goodpoints = find( ((X(3,:) > -1100)));
fprintf('dropping %2.2f %% of points from scan',100*(1 - (length(goodpoints)/size(X,2))));
X = X(:,goodpoints);
xR = xR(:,goodpoints);
xL = xL(:,goodpoints);
xColor = xColor(:,goodpoints);
%%
%% cleaning step 2: remove points whose neighbors are far away
%%
fprintf('filtering right image neighbors\n');
[tri,pterrR] = nbr_error(xR,X);
fprintf('filtering left image neighbors\n');
[tri,pterrL] = nbr_error(xL,X);
goodpoints = find((pterrR<nbrthresh) & (pterrL<nbrthresh));
fprintf('dropping %2.2f %% of points from scan\n',100*(1-(length(goodpoints)/size(X,2))));
X = X(:,goodpoints);
xR = xR(:,goodpoints);
xL = xL(:,goodpoints);
xColor = xColor(:,goodpoints);
%%
%
% cleaning step 3: remove triangles which have long edges
%
[tri,terr] = tri_error(xL,X);
subt = find(terr<trithresh);
tri = tri(subt,:);
%
% render intermediate results
%
figure(1); clf;
h = trisurf(tri,X(1,:),X(2,:),X(3,:));
set(h,'edgecolor','none')
axis image; axis vis3d;
camorbit(120,0); camlight left;
camorbit(120,0); camlight left;
lighting phong;
set(gca,'projection','perspective')
set(gcf,'renderer','opengl')
set(h,'facevertexcdata',xColor'/255);
%
% cleaning step 4: simple smoothing
%
Y = nbr_smooth(tri,X,1);
% Y(1,:) = -Y(1,:);
% Y(3,:) = -Y(3,:);
%%
% visualize results of smooth with
% mesh edges visible
figure(2); clf;
h = trisurf(tri,Y(1,:),Y(2,:),Y(3,:));
set(h,'edgecolor','flat')
axis image; axis vis3d;
camorbit(120,0); camlight left;
camorbit(120,0); camlight left;
lighting flat;
set(gca,'projection','perspective')
set(gcf,'renderer','opengl')
set(h,'facevertexcdata',xColor'/255);
material dull
% flip the tri coordinates around to reorientate the normals to the correct
% direction
tri = tri(:,[2 1 3]);
fprintf(' ============ MESH.M DONE! ============\n');