-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
203 lines (164 loc) · 3.39 KB
/
main.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
#include "he_mesh.h"
#include "sim.h"
#include "timer.h"
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;
MeshData plane(int rows,int cols)
{
#define LIN_IDX(_i,_j) ((_i)*cols+(_j))
// n*n vert
float3* p=new float3[rows*cols];
for(int i=0;i<rows;++i)
{
for(int j=0;j<cols;++j)
{
p[LIN_IDX(i,j)]=float3(i,j,0.f);
}
}
int* vi=new int[2*(rows-1)*(cols-1)*3];
int* pvi=vi;
for(int i=0;i<rows-1;++i)
{
for(int j=0;j<cols-1;++j)
{
*pvi++=LIN_IDX(i,j);
*pvi++=LIN_IDX(i+1,j);
*pvi++=LIN_IDX(i,j+1);
*pvi++=LIN_IDX(i+1,j);
*pvi++=LIN_IDX(i+1,j+1);
*pvi++=LIN_IDX(i,j+1);
}
}
return MeshData(2*(rows-1)*(cols-1),rows*cols,rows*cols,0,p,0,0,vi);
}
MeshData mesh1()
{
// 3 /\
// 2 /__\ 1
// \ /
// 0 \/
float3* p=new float3[4];
int* vi=new int[2*3];
p[0]=float3(0.f,-1.f,0.f);
p[1]=float3(1.f,0.f,0.f);
p[2]=float3(-1.f,0.f,0.f);
p[3]=float3(0.f,1.f,0.f);
// counter-clockwise
vi[0]=0;vi[1]=1;vi[2]=2;
vi[3]=2;vi[4]=1;vi[5]=3;
return MeshData(2,4,4,0,p,0,0,vi);
}
MeshData mesh2()
{
// 2 ____ 1
// \ /
// \/0
// /\
// 3 /__\ 4
float3* p=new float3[5];
int* vi=new int[2*3];
p[0]=float3(0.f,0.f,0.f);
p[1]=float3(1.f,1.f,0.f);
p[2]=float3(-1.f,1.f,0.f);
p[3]=float3(-1.f,-1.f,0.f);
p[4]=float3(1.f,-1.f,0.f);
// counter-clockwise
vi[0]=0;vi[1]=1;vi[2]=2;
vi[3]=0;vi[4]=3;vi[5]=4;
return MeshData(2,5,5,0,p,0,0,vi);
}
MeshData mesh3()
{
// 2 ____ 1
// \ /
// 0 \/_/|6
// /\ \|5
// 3 /__\ 4
float3* p=new float3[7];
int* vi=new int[3*3];
p[0]=float3(0.f,0.f,0.f);
p[1]=float3(1.f,1.f,0.f);
p[2]=float3(-1.f,1.f,0.f);
p[3]=float3(-1.f,-1.f,0.f);
p[4]=float3(1.f,-1.f,0.f);
p[5]=float3(2.f,-1.f,0.f);
p[6]=float3(2.f,1.f,0.f);
// counter-clockwise
vi[0]=0;vi[1]=1;vi[2]=2;
vi[3]=0;vi[4]=3;vi[5]=4;
vi[6]=0;vi[7]=5;vi[8]=6;
return MeshData(3,7,7,0,p,0,0,vi);
}
void print_usage()
{
printf("meshsim input_file [-faces %%d] [-ratio %%f]\n");
}
// TRY
// boundary in half edge
// forward energy
int main(int argc,char** argv)
{
string input_file=argv[1];
int faces=0;
float ratio=0.5f;
he_mesh mesh;
if(argc<2)
{
print_usage();
return 0;
}
for(int i=2;i<argc;++i)
{
if(!strcmp(argv[i],"-faces"))
{
if(i+1<argc)
{
faces=atoi(argv[++i]);
}
}
else if(!strcmp(argv[i],"-ratio"))
{
if(i+1<argc)
{
ratio=atof(argv[++i]);
if(ratio<=0.f||ratio>=1.f)
ratio=0.5f;
}
}
}
//MeshData mesh_data=mesh2();
//MeshData mesh_data=plane(1000,1000);
MeshData mesh_data;
string fext=input_file.substr(input_file.length()-4,4);
if(fext==".off")
mesh_data=MeshLoader::off(input_file.c_str());
else if(fext==".obj")
mesh_data=MeshLoader::obj(input_file.c_str());
else
{
printf("cannot load file format: %s\n",input_file.c_str());
return 0;
}
tic();
if(!mesh.construct(mesh_data))
return 0;
toc("convert to half-edge mesh");
//mesh.dumpOFF("input.off");
mesh_data.clear();
if(faces==0||faces>=mesh.faces.size())
{
faces=ratio*mesh.faces.size();
}
printf("input faces: %d, target faces: %d\n",mesh.faces.size(),faces);
MeshSim mesh_simer;
tic();
mesh_simer.simplify(&mesh,faces);//mesh.faces.size()*0.01);
toc("simplify");
char fsuffix[50];
sprintf(fsuffix,"_%d.off",mesh.faces.size());
string output_file=input_file.substr(0,input_file.length()-4)+fsuffix;
mesh.dumpOFF(output_file.c_str());
return 0;
}