forked from THUHoloLab/TextureDeblur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnifyDuplicatedVertices.m
154 lines (138 loc) · 4.46 KB
/
UnifyDuplicatedVertices.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
function [Vtx Tris] = UnifyDuplicatedVertices(Vtx, Tris)
% % 将X排序,按次序判断冗余
% [Y, SortIdx] = sort(Vtx(:,1));
%
% VtxIdxMap = zeros(size(Vtx,1),1);
% VtxOld = Vtx;
%
% Vtx(1,:) = VtxOld(SortIdx(1),:);
% VtxIdxMap(SortIdx(1)) = 1;
% Index = 1;
% for i = 2:size(SortIdx,1)
% for j = Index : -1 : 1
% if VtxOld(SortIdx(i),1) ~= Vtx(j,1)
% Index = Index + 1;
% Vtx(Index,:) = VtxOld(SortIdx(i),:);
% break;
% end
% if VtxOld(SortIdx(i),1) == Vtx(j,1) && ...
% VtxOld(SortIdx(i),2) == Vtx(j,2) && ...
% VtxOld(SortIdx(i),3) == Vtx(j,3)
% break;
% end
% end
% VtxIdxMap(SortIdx(i)) = Index;
% end
% Vtx = Vtx(1:Index,:);
%
% for i = 1:size(Tris,1)
% Tris(i,:) = [ VtxIdxMap(Tris(i,1)) VtxIdxMap(Tris(i,2)) VtxIdxMap(Tris(i,3)) ];
% end
% 分块计算去除冗余
VtxNum = size(Vtx,1);
[Vtx Index VtxIdx] = Unify_cube(zeros(VtxNum,3), 0, zeros(VtxNum,1), Vtx, (1:VtxNum)');
Vtx = Vtx(1:Index,:);
for i = 1:size(Tris,1)
Tris(i,:) = [ VtxIdx(Tris(i,1)) VtxIdx(Tris(i,2)) VtxIdx(Tris(i,3)) ];
end
% % 直接计算去除冗余
% [Vtx Tris] = UnifyDuplicatedVertices_simple(Vtx, Tris);
function [GlobalVtx GlobalIndex GlobalVtxIdx] = Unify_cube(GlobalVtx, GlobalIndex, GlobalVtxIdx, Vtx, VtxIdx)
if size(Vtx,1) < 300
Start = GlobalIndex+1;
for i = 1:size(Vtx,1)
a = 0;
for j = Start : GlobalIndex
if sum(Vtx(i,:) == GlobalVtx(j,:)) == 3
a = j;
break;
end
end
if a == 0
GlobalIndex = GlobalIndex + 1;
GlobalVtx(GlobalIndex,:) = Vtx(i,:);
GlobalVtxIdx(VtxIdx(i)) = GlobalIndex;
else
GlobalVtxIdx(VtxIdx(i)) = a;
end
end
return;
end
Border = [ min(Vtx(:,1)) , max(Vtx(:,1)) ;
min(Vtx(:,2)) , max(Vtx(:,2)) ;
min(Vtx(:,3)) , max(Vtx(:,3)) ] ;
Length = [ Border(1,2) - Border(1,1) , ...
Border(2,2) - Border(2,1) , ...
Border(3,2) - Border(3,1) ] ;
[C Direction] = max( Length );
MeanBorder = Border(Direction,1) + Length(Direction)/2;
Border1 = Border;
Border1(Direction,:) = [ Border(Direction,1) , MeanBorder ];
Border2 = Border;
Border2(Direction,:) = [ MeanBorder , Border(Direction,2) ];
Index1 = 0;
Index2 = 0;
PerSectNum1 = 100000; % 分段读入时每段读入的个数
PrepTotalNum1 = PerSectNum1; % 已申请的总数
Vtx1 = zeros(PrepTotalNum1, 3);
VtxIdx1 = zeros(PrepTotalNum1, 1);
PerSectNum2 = 100000; % 分段读入时每段读入的个数
PrepTotalNum2 = PerSectNum2; % 已申请的总数
Vtx2 = zeros(PrepTotalNum2, 3);
VtxIdx2 = zeros(PrepTotalNum2, 1);
for i = 1:size(Vtx,1)
if Vtx(i,Direction) <= MeanBorder
if PrepTotalNum1 == Index1
PrepTotalNum1 = PrepTotalNum1 + PerSectNum1;
Vtx1(Index1+1:PrepTotalNum1, :) = zeros(PerSectNum1, 3);
VtxIdx1(Index1+1:PrepTotalNum1, :) = zeros(PerSectNum1, 1);
end
Index1 = Index1 + 1;
Vtx1(Index1,:) = Vtx(i,:);
VtxIdx1(Index1,:) = VtxIdx(i,:);
else
if PrepTotalNum2 == Index2
PrepTotalNum2 = PrepTotalNum2 + PerSectNum2;
Vtx2(Index2+1:PrepTotalNum2, :) = zeros(PerSectNum2, 3);
VtxIdx2(Index2+1:PrepTotalNum2, :) = zeros(PerSectNum2, 1);
end
Index2 = Index2 + 1;
Vtx2(Index2,:) = Vtx(i,:);
VtxIdx2(Index2,:) = VtxIdx(i,:);
end
end
Vtx1 = Vtx1(1:Index1, :);
VtxIdx1 = VtxIdx1(1:Index1, :);
Vtx2 = Vtx2(1:Index2, :);
VtxIdx2 = VtxIdx2(1:Index2, :);
if Index1 > 0
[GlobalVtx GlobalIndex GlobalVtxIdx] = Unify_cube(GlobalVtx, GlobalIndex, GlobalVtxIdx, Vtx1, VtxIdx1);
end
if Index2 > 0
[GlobalVtx GlobalIndex GlobalVtxIdx] = Unify_cube(GlobalVtx, GlobalIndex, GlobalVtxIdx, Vtx2, VtxIdx2);
end
function [Vtx Tris] = UnifyDuplicatedVertices_simple(Vtx, Tris)
% 直接计算去除冗余
VtxIdx = 1:size(Vtx,1);
VtxOld = Vtx;
Index = 0;
for i = 1:size(VtxOld,1)
a = 0;
for j = 1:Index
if sum(VtxOld(i,:) == Vtx(j,:)) == 3
a = j;
break;
end
end
if a == 0
Index = Index + 1;
Vtx(Index,:) = VtxOld(i,:);
VtxIdx(i) = Index;
else
VtxIdx(i) = a;
end
end
Vtx = Vtx(1:Index,:);
for i = 1:size(Tris,1)
Tris(i,:) = [ VtxIdx(Tris(i,1)) VtxIdx(Tris(i,2)) VtxIdx(Tris(i,3)) ];
end