-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershape.cpp
328 lines (216 loc) · 7.5 KB
/
powershape.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <iostream>
#include <math.h>
#include "sdefs.h"
#include <cstring>
/*
* Power Crust software, by Nina Amenta, Sunghee Choi and Ravi Krishna Kolluri.
* Copyright (c) 2000 by the University of Texas
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee under the GNU Public License is hereby granted,
* provided that this entire notice is included in all copies of any software
* which is or includes a copy or modification of this software and in all copies
* of the supporting documentation for such software.
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*/
powershape pshape; // a global variable
char inFileName[32]="input",outFileName[32]="output";
bool plotGraph=true;
double nt=0,rt=0;
bool outputUnlabeled=false;
void powershape::addEdge(int i,int j) {
if(find(verts[i].neighbors.begin(),verts[i].neighbors.end(),j)==
verts[i].neighbors.end()){
verts[i].neighbors.push_back(j);
verts[j].neighbors.push_back(i);
}
}
void powershape::readInput(ifstream& inFile) {
int nverts,nfaces;
inFile>>nverts;
inFile>>nfaces;
double x,y,z,r,d;
int stat;
polelabel pl;
vector<int> findices;
int nfverts;
int temp;
for(int i=0;i<nverts;i++) {
inFile>>x;
inFile>>y;
inFile>>z;
inFile>>r;
inFile>>stat;
inFile>>d;
if(stat==0)
pl=UNLABELED;
else
if(stat==1)
pl=EXT;
else
pl=INT;
verts.push_back(vertex(x,y,z,r,pl,d));
}
// now for the faces
for(int i=0;i<nfaces;i++) {
findices.clear();
inFile>>nfverts;
for(int j=0;j<nfverts;j++) {
inFile>>temp;
findices.push_back(temp);
}
// actually add edges to the graph
for(int j=0;j<findices.size()-1;j++)
addEdge(findices[j],findices[j+1]);
if(findices.size()>2)
addEdge(findices[0],findices[findices.size()-1]);
}
}
void powershape::noiseRemove() {
int numPoints=0;
for(int i=0;i<verts.size();i++)
if(verts[i].distance<= noiseThreshold){
verts[i].status=REMOVED;
numPoints++;
}
cout<<"Number of points removed by the noise criterion\t"<<numPoints<<"\n";
}
int compare(const void *n1,const void *n2){
//int compare(int index1,int index2) {
int index1=*((int *)n1);
int index2=*((int *)n2);
if(index1 >= pshape.verts.size() || index2 >= pshape.verts.size()){
cout<<"Error!..\n"<<index1<<"\t"<<index2<<"\n";
return 0;
}
if(pshape.verts[index1].radius > pshape.verts[index2].radius) return -1;
if(pshape.verts[index1].radius < pshape.verts[index2].radius) return 1;
return 0;
}
int compareDistance(const void *n1,const void *n2){
//int compare(int index1,int index2) {
int index1=*((int *)n1);
int index2=*((int *)n2);
if(index1 >= pshape.verts.size() || index2 >= pshape.verts.size()){
cout<<"Error!..\n"<<index1<<"\t"<<index2<<"\n";
return 0;
}
if(pshape.verts[index1].distance > pshape.verts[index2].distance) return 1;
if(pshape.verts[index1].distance < pshape.verts[index2].distance) return -1;
return 0;
}
void powershape::outputPlot(ofstream& outFile) {
int *pqueue=(int*)malloc(verts.size()*sizeof(int));
for(int i=0;i<verts.size();i++) pqueue[i]=i;
qsort(pqueue,verts.size(),sizeof(int),compareDistance);
for(int i=0;i<verts.size();i++) {
outFile<<i<<"\t"<<verts[pqueue[i]].distance<<"\n";
}
}
double powershape::distance(int i,int j) {
return (sqrt(SQ(verts[i].x-verts[j].x) +
SQ(verts[i].y-verts[j].y) +
SQ(verts[i].z-verts[j].z)));
}
void powershape::redRemove() {
int *pqueue;
int currnode,currneighbor;
int numPoints=0;
pqueue=(int *)malloc(verts.size()*sizeof(int));
for(int i=0;i<verts.size();i++) pqueue[i]=i;
qsort(pqueue,verts.size(),sizeof(int),compare);
for(int i=0;i<verts.size();i++) {
currnode=pqueue[i];
if(verts[currnode].status!=PRESENT)
continue;
verts[currnode].status=FIXED;
for(int j=0;j<verts[currnode].neighbors.size();j++) {
currneighbor=verts[currnode].neighbors[j];
if(verts[currneighbor].status==REMOVED || verts[currneighbor].status==FIXED)
continue;
if((distance(currnode,currneighbor)-
(sqrt(verts[currnode].radius)-sqrt(verts[currneighbor].radius)))<
redThreshold){
verts[currneighbor].status=REMOVED;
numPoints++;
// add the neighbors to the original graph
for(int k=0;k<verts[currneighbor].neighbors.size();k++)
addEdge(verts[currneighbor].neighbors[k],currnode);
}
}
verts[currnode].neighbors.clear();
}
cout<<"Number of points removed by the redundancy criterion\t"<<numPoints<<"\n";
}
void powershape::outputPoles(ofstream& outFile) {
outFile.precision(12);
int numverts=0;
for(int i=0;i<verts.size();i++)
if(verts[i].status!=REMOVED &&
(verts[i].label!=UNLABELED || outputUnlabeled))
numverts++;
outFile<<numverts<<"\n";
for(int i=0;i<verts.size();i++)
if(verts[i].status!=REMOVED &&
(verts[i].label!=UNLABELED || outputUnlabeled))
outFile<<verts[i].x<<"\t"
<<verts[i].y<<"\t"
<<verts[i].z<<"\t"
<<verts[i].radius<<"\t"
<<verts[i].label<<"\t"
<<verts[i].distance<<"\n";
}
/* parse the command line */
int parseCommand(int argc,char **argv) {
for(int i=1;i<argc;) {
// cout<<"Argv is\t"<<argv[i]<<"\n";
if(argv[i][0]!='-')
return 0;
switch(argv[i][1]) {
case 'o':strcpy(outFileName,argv[i+1]);
i=i+2;
break;
case 'i':strcpy(inFileName,argv[i+1]);
i=i+2;
break;
case 'n':nt=atof(argv[i+1]);
i=i+2;
break;
case 'r':rt=atof(argv[i+1]);
i=i+2;
break;
case 'p':plotGraph=atoi(argv[i+1]);
i=i+2;
break;
case 'u':outputUnlabeled = atoi(argv[i+1]);
i+=2;
break;
default:return 0;
}
}
return 1;
}
int mainPowershape (int argc,char** argv) {
if(!parseCommand(argc,argv)) {
cout<<"Error in arguments..\n";
exit(0);
}
ifstream inFile(inFileName);
ofstream outFile(outFileName);
pshape.readInput(inFile);
cout<<"Number of vertices in the input File \t"<< pshape.verts.size()<<"\n";
cout<<"The input file is\t"<<inFileName<<"\n";
cout<<"The output file is\t"<<outFileName<<"\n";
cout<<"The noise threshold is\t "<<nt<<"\n";
cout<<"The redundancy threshold is\t"<<rt<<"\n";
ofstream plotLog("distancelog");
pshape.noiseThreshold=nt;
pshape.redThreshold=rt;
if(plotGraph)
pshape.outputPlot(plotLog);
pshape.noiseRemove();
pshape.redRemove();
pshape.outputPoles(outFile);
}