-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathca_smoothing.cxx
293 lines (257 loc) · 9.98 KB
/
ca_smoothing.cxx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//--------------------------------------------------------------------------
// Software: Context Aware Smoothing
// Copyright: (C) 2012 Centro de Tecnologia da Informação Renato Archer
// Homepage: https://github.com/tfmoraes/context_aware_smoothing
// Contact: [email protected]
// License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
//--------------------------------------------------------------------------
// Este programa e software livre; voce pode redistribui-lo e/ou
// modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
// publicada pela Free Software Foundation; de acordo com a versao 2
// da Licenca.
//
// Este programa eh distribuido na expectativa de ser util, mas SEM
// QUALQUER GARANTIA; sem mesmo a garantia implicita de
// COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
// PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
// detalhes.
//--------------------------------------------------------------------------
#include "ca_smoothing.h"
vtkPolyData* ca_smoothing(vtkPolyData* pd, double T, double tmax, double bmin, int n_iter) {
const double stack_orientation[3] = { 0, 0, 1 };
vtkPolyData *tpd;
vtkIdList *vertices_staircase;
vtkDoubleArray* weights;
printf("Finding staircase artifacts\n");
printf("Number of points %lld\n", pd->GetNumberOfPoints());
vertices_staircase = find_staircase_artifacts(pd, stack_orientation, T);
printf("Calculating the Weights\n");
weights = calc_artifacts_weight(pd, vertices_staircase, tmax, bmin);
printf("Taubin Smooth\n");
tpd = taubin_smooth(pd, weights, 0.5, -0.53, n_iter);
vertices_staircase->Delete();
weights->Delete();
return tpd;
}
vtkIdList* find_staircase_artifacts(vtkPolyData* pd, const double stack_orientation[3], double T) {
/*
This function is used to find vertices at staircase artifacts, which are
those vertices whose incident faces' orientation differences are
aproximated 90°. The argument `pd' is a vtkPolydata, it have to be its
faces orientation (normal) calculated before and make sure that
BuildLinks() has been called.
*/
int nv, nf, fid;
double of_z, of_y, of_x, min_z, max_z, min_y, max_y, min_x, max_x;
double *ni;
vtkIdList *output = vtkIdList::New();
vtkIdList *idfaces = vtkIdList::New();//idfaces = vtk.vtkIdList()
nv = pd->GetNumberOfPoints(); // Number of vertices.
for (int vid=0; vid < nv; vid++){ //for vid in xrange(nv):
pd->GetPointCells(vid, idfaces); //pd.GetPointCells(vid, idfaces) # Getting faces connected to face vid.
nf = idfaces->GetNumberOfIds();
max_z = -1000;
min_z = 1000;
max_y = -1000;
min_y = 1000;
max_x = -1000;
min_x = 1000;
for (int nid=0; nid < nf; nid++) {
fid = idfaces->GetId(nid);
ni = pd->GetCellData()->GetArray("Normals")->GetTuple(fid);
of_z = 1 - abs(ni[0]*stack_orientation[0] + ni[1]*stack_orientation[1] + ni[2]*stack_orientation[2]);
of_y = 1 - abs(ni[0]*0 + ni[1]*1 + ni[2]*0);
of_x = 1 - abs(ni[0]*1 + ni[1]*0 + ni[2]*0);
if (of_z > max_z) max_z = of_z;
if (of_z < min_z) min_z = of_z;
if (of_y > max_y) max_y = of_y;
if (of_y < min_y) min_y = of_y;
if (of_x > max_x) max_x = of_x;
if (of_x < min_x) min_x = of_x;
// Getting the ones which normals dot is 90°, its vertex is added to
// output
if ((abs(max_z - min_z) >= T) || (abs(max_y - min_y) >= T) || (abs(max_x - min_x) >= T)) {
output->InsertNextId(vid);
break;
}
}
idfaces->Reset();
}
return output;
}
vtkIdList* get_near_vertices_to_v(vtkPolyData* pd, int v, double dmax){
/*
Returns all vertices with distance at most "d" to the vertice "v" with
their distances.
pd - vtkPolydata
v - the reference vertice
dmax - the maximun distance.
*/
double vi[3], vj[3], d;
int n=0, nf, fid;
MAP<int, bool> status_v;
MAP<int, bool> status_f;
std::queue <int> to_visit;
vtkIdList* near_vertices = vtkIdList::New();
vtkIdList* idfaces = vtkIdList::New();
pd->GetPoint(v, vi); // The position of vertex v
while (1) {
pd->GetPointCells(v, idfaces);
nf = idfaces->GetNumberOfIds();
for(int nid=0; nid < nf; nid++) {
fid = idfaces->GetId(nid);
if (status_f.find(fid) != status_f.end()) {
if (to_visit.empty())
break;
v = to_visit.front();
to_visit.pop();
continue;
}
status_f[fid] = true;
vtkCell* face = pd->GetCell(fid);
for(int i=0; i < 3; i++) {
int vjid = face->GetPointId(i);
if (status_v.find(vjid) == status_v.end() || !status_v[vjid]) {
pd->GetPoint(vjid, vj);
d = sqrt((vi[0] - vj[0]) * (vi[0] - vj[0])\
+ (vi[1] - vj[1]) * (vi[1] - vj[1])\
+ (vi[2] - vj[2]) * (vi[2] - vj[2]));
if (d <= dmax) {
near_vertices->InsertNextId(vjid);
to_visit.push(vjid);
}
}
status_v[vjid] = true;
}
}
n++;
if (to_visit.empty())
break;
v = to_visit.front();
to_visit.pop();
idfaces->Reset();
}
return near_vertices;
}
vtkDoubleArray* calc_artifacts_weight(vtkPolyData* pd, vtkIdList* vertices_staircase, double tmax, double bmin) {
/*
Calculate the artifact weight based on distance of each vertex to its
nearest staircase artifact vertex.
pd - vtkPolydata;
vertices_staircase - the identified staircase artifact vertices;
tmax=2 - max distance the vertex must be to its nearest artifact vertex to
considered to calculate the weight;
bmin=0.1 - The minimun weight.
*/
double vi[3], vj[3], d, value;
int viid, vjid, nnv;
int nid = vertices_staircase->GetNumberOfIds();
vtkIdList* near_vertices;
vtkDoubleArray* weights = vtkDoubleArray::New();
for (int i=0; i < pd->GetNumberOfPoints(); i++){
weights->InsertNextValue(0);
}
for (int i=0; i < nid; i++) {
viid = vertices_staircase->GetId(i);
pd->GetPoint(viid, vi);
near_vertices = get_near_vertices_to_v(pd, viid, tmax);
nnv = near_vertices->GetNumberOfIds();
for (int j=0; j < nnv; j++){
vjid = near_vertices->GetId(j);
pd->GetPoint(vjid, vj);
d = sqrt((vi[0] - vj[0]) * (vi[0] - vj[0])\
+ (vi[1] - vj[1]) * (vi[1] - vj[1])\
+ (vi[2] - vj[2]) * (vi[2] - vj[2]));
value = (1.0 - d/tmax) * (1 - bmin) + bmin;
if (value > weights->GetValue(vjid)) {
weights->SetValue(vjid, value);
}
}
near_vertices->Delete();
}
return weights;
}
Point calc_d(vtkPolyData* pd, int vid){
Point D;
int nf, fid, n=0;
double vi[3], vj[3];
std::set<int> vertices;
std::set<int>::iterator it;
vtkIdList* idfaces = vtkIdList::New();
pd->GetPointCells(vid, idfaces);
nf = idfaces->GetNumberOfIds();
for (int nid=0; nid < nf; nid++) {
fid = idfaces->GetId(nid);
vtkCell* face = pd->GetCell(fid);
for (int i=0; i < 3; i++) {
int vjid = face->GetPointId(i);
if (vjid != vid)
vertices.insert(vjid);
}
}
idfaces->Delete();
D.x = 0;
D.y = 0;
D.z = 0;
pd->GetPoint(vid, vi); // The position of vertex v
for (it=vertices.begin(); it!=vertices.end(); it++) {
pd->GetPoint(*it, vj);
D.x = D.x + (vi[0] - vj[0]);
D.y = D.y + (vi[1] - vj[1]);
D.z = D.z + (vi[2] - vj[2]);
n++;
}
D.x = D.x / n;
D.y = D.y / n;
D.z = D.z / n;
return D;
}
vtkPolyData* taubin_smooth(vtkPolyData* pd, vtkDoubleArray* weights, double l, double m, int steps){
/*
* Implementation of Taubin's smooth algorithm described in the paper "A
* Signal Processing Approach To Fair Surface Design". His benefeat is it
* avoids to
*
*/
double vi[3];
vtkPolyData* new_pd = vtkPolyData::New();
new_pd->DeepCopy(pd);
//std::vector<Point> D(pd->GetNumberOfPoints());
//D.reserve(pd->GetNumberOfPoints());
Point *D;
D = (Point*) malloc(pd->GetNumberOfPoints() * sizeof(Point));
for (int s=0; s < steps; s++) {
printf("Step %d\n", s);
for (int i=0; i < pd->GetNumberOfPoints(); i++) {
D[i] = calc_d(new_pd, i);
}
for (int i=0; i < pd->GetNumberOfPoints(); i++) {
new_pd->GetPoint(i, vi);
vi[0] = vi[0] + weights->GetValue(i)*l*D[i].x;
vi[1] = vi[1] + weights->GetValue(i)*l*D[i].y;
vi[2] = vi[2] + weights->GetValue(i)*l*D[i].z;
new_pd->GetPoints()->SetPoint(i, vi);
}
for (int i=0; i < pd->GetNumberOfPoints(); i++) {
D[i] = calc_d(new_pd, i);
}
for (int i=0; i < pd->GetNumberOfPoints(); i++) {
new_pd->GetPoint(i, vi);
vi[0] = vi[0] + weights->GetValue(i)*m*D[i].x;
vi[1] = vi[1] + weights->GetValue(i)*m*D[i].y;
vi[2] = vi[2] + weights->GetValue(i)*m*D[i].z;
new_pd->GetPoints()->SetPoint(i, vi);
}
}
free(D);
return new_pd;
}
MAP<int, std::vector<int> > get_flat_areas(vtkPolyData* pd, double stack_orientation[3], double tmax, int msize) {
/* Returns each flat area in the given *vtkPolydata pd*. To be considered a
* flat area it must have normal bellow *tmax* related to the
* *stack_orientation* and have at least *msize* triangles. The result is
* returned as hash_map.
*/
MAP<int, std::vector<int> > flat_areas;
return flat_areas;
}