-
Notifications
You must be signed in to change notification settings - Fork 12
/
Mesh.cpp
463 lines (422 loc) · 9.61 KB
/
Mesh.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "includes/Mesh.h"
#include "includes/CompFab.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#ifdef _WIN32
#define NOMINMAX //Stop errors with std::max
#include <windows.h>
#endif
#include <stdio.h>
#include <cstdlib>
#include <utility>
#include <map>
#include <sstream>
#include <string.h>
//#include "util.h"
// typedef double CompFab::precision_type;
// typedef float CompFab::precision_type;
///@brief is a point inside a box
bool ptInBox(const CompFab::Vec3 & mn,
const CompFab::Vec3 mx, const CompFab::Vec3 & x)
{
for(int dim = 0 ;dim<3;dim++){
if(x[dim]<mn[dim] || x[dim] > mx[dim]){
return false;
}
}
return true;
}
void makeCube(Mesh & m, const CompFab::Vec3 & mn,
const CompFab::Vec3 mx)
{
CompFab::Vec3 ss = mx -mn;
m=UNIT_CUBE;
for(unsigned int ii = 0;ii<m.v.size();ii++){
m.v[ii][0] = mn[0] + ss[0]*m.v[ii][0];
m.v[ii][1] = mn[1] + ss[1]*m.v[ii][1];
m.v[ii][2] = mn[2] + ss[2]*m.v[ii][2];
}
}
void Mesh::append(const Mesh & m)
{
unsigned int offset = v.size();
unsigned int ot = t.size();
v.insert(v.end(),m.v.begin(),m.v.end());
t.insert(t.end(),m.t.begin(), m.t.end());
for(unsigned int ii = ot;ii<t.size();ii++){
for(int jj = 0 ;jj<3;jj++){
t[ii][jj] += offset;
}
}
}
Mesh & Mesh::operator= (const Mesh& m)
{
v = m.v;
t = m.t;
n = m.n;
return *this;
}
///@brief cube [0,1]^3
CompFab::Vec3 CUBE_VERT[8]={
CompFab::Vec3 (0, 0, 0),
CompFab::Vec3 (1, 0, 0),
CompFab::Vec3 (1, 1, 0),
CompFab::Vec3 (0, 1, 0),
CompFab::Vec3 (0, 0, 1),
CompFab::Vec3 (1, 0, 1),
CompFab::Vec3 (1, 1, 1),
CompFab::Vec3 (0, 1, 1)
};
CompFab::Vec3i CUBE_TRIG[12]={CompFab::Vec3i(0,3,1),
CompFab::Vec3i(1, 3, 2),
CompFab::Vec3i(5, 4, 0),
CompFab::Vec3i(5, 0, 1),
CompFab::Vec3i(6, 5, 1),
CompFab:: Vec3i(1, 2, 6),
CompFab:: Vec3i(3, 6, 2),
CompFab:: Vec3i(3, 7, 6),
CompFab:: Vec3i(4, 3, 0),
CompFab:: Vec3i(4, 7, 3),
CompFab:: Vec3i(7, 4, 5),
CompFab:: Vec3i(7, 5, 6)};
Mesh UNIT_CUBE(CUBE_VERT,CUBE_TRIG);
Mesh::Mesh():v(0),t(0){}
Mesh::Mesh(const std::vector<CompFab::Vec3>&_v,
const std::vector<CompFab::Vec3i>&_t):v(_v),t(_t)
{
compute_norm();
}
Mesh::Mesh(const CompFab::Vec3 * _v,
const CompFab::Vec3i * _t)
{
v.assign(_v,_v+8);
t.assign(_t,_t+12);
compute_norm();
}
void Mesh::save(std::ostream & out, std::vector<CompFab::Vec3> * vert)
{
std::string vTok("v");
std::string fTok("f");
std::string texTok("vt");
char bslash='/';
std::string tok;
if(vert==0){
vert = &v;
}
for(size_t ii=0;ii<vert->size();ii++){
out<<vTok<<" "<<(*vert)[ii][0]<<" "<<(*vert)[ii][1]<<" "<<(*vert)[ii][2]<<"\n";
}
if(tex.size()>0){
for(size_t ii=0;ii<tex.size();ii++){
out<<texTok<<" "<<tex[ii][0]<<" "<<tex[ii][1]<<"\n";
}
for(size_t ii=0;ii<t.size();ii++){
out<<fTok<<" "<<t[ii][0]+1<<bslash<<texId[ii][0]+1<<" "
<<t[ii][1]+1<<bslash<<texId[ii][1]+1<<" "
<<t[ii][2]+1<<bslash<<texId[ii][2]+1<<"\n";
}
}else{
for(size_t ii=0;ii<t.size();ii++){
out<<fTok<<" "<<t[ii][0]+1<<" "<<
t[ii][1]+1<<" "<<t[ii][2]+1<<"\n";
}
}
out<<"#end\n";
}
void Mesh::save(const char * filename)
{
std::ofstream out;
out.open(filename);
save(out);
out.close();
}
void Mesh::load(std::istream &in)
{
read_obj(in);
}
void Mesh::read_obj(std::istream & f)
{
std::string line;
std::string vTok("v");
std::string fTok("f");
std::string texTok("vt");
char bslash='/',space=' ';
std::string tok;
while(1) {
std::getline(f,line);
if(f.eof()) {
break;
}
if(line == "#end"){
break;
}
if(line.size()<3) {
continue;
}
if(line.at(0)=='#') {
continue;
}
std::stringstream ss(line);
ss>>tok;
if(tok==vTok) {
CompFab::Vec3 vec;
ss>>vec[0]>>vec[1]>>vec[2];
v.push_back(vec);
} else if(tok==fTok) {
bool hasTexture = false;
if (line.find(bslash) != std::string::npos) {
std::replace(line.begin(), line.end(), bslash, space);
hasTexture = true;
}
std::stringstream facess(line);
facess>>tok;
std::vector<int> vidx;
std::vector<int> texIdx;
int x;
while(facess>>x){
vidx.push_back(x);
if(hasTexture){
facess>>x;
texIdx.push_back(x);
}
}
texIdx.resize(vidx.size());
for(int ii = 0;ii<vidx.size()-2;ii++){
CompFab::Vec3i trig, textureId;
trig[0] = vidx[0]-1;
textureId[0] = texIdx[0]-1;
for (int jj = 1; jj < 3; jj++) {
trig[jj] = vidx[ii+jj]-1;
textureId[jj] = texIdx[ii+jj]-1;
}
t.push_back(trig);
texId.push_back(textureId);
}
} else if(tok==texTok) {
CompFab::Vec2f texcoord;
ss>>texcoord[0];
ss>>texcoord[1];
tex.push_back(texcoord);
}
}
std::cout<<"Num Triangles: "<< t.size()<<"\n";
}
void Mesh::read_ply(std::istream & f)
{
std::string line;
std::string vertLine("element vertex");
std::string faceLine("element face");
std::string texLine("property float s");
std::string endHeaderLine("end_header");
while(true) {
std::getline(f,line);
if(std::string::npos!=line.find(vertLine)) {
break;
}
}
std::string token;
std::stringstream ss(line);
ss>>token>>token;
int nvert;
ss>>nvert;
bool hasTex=false;
while(true) {
std::getline(f,line);
if(std::string::npos!=line.find(faceLine)) {
break;
}
if(std::string::npos!=line.find(texLine)) {
hasTex=true;
}
}
std::stringstream ss1(line);
ss1>>token>>token;
int nface;
ss1>>nface;
while(true) {
std::getline(f,line);
if(std::string::npos!=line.find(endHeaderLine)) {
break;
}
}
v.resize(nvert);
t.resize(nface);
if(hasTex) {
tex.resize(nvert);
}
for (int ii =0; ii<nvert; ii++) {
for (int jj=0; jj<3; jj++) {
f>>v[ii][jj];
}
if(hasTex) {
for (int jj=0; jj<2; jj++) {
f>>tex[ii][jj];
}
tex[ii][1]=1-tex[ii][1];;
}
}
for (int ii =0; ii<nface; ii++) {
int nidx;
f>>nidx;
for (int jj=0; jj<3; jj++) {
f>>t[ii][jj];
}
}
}
void Mesh::save_obj(const char * filename)
{
std::ofstream out(filename);
if(!out.good()){
std::cout<<"cannot open output file"<<filename<<"\n";
return;
}
save(out);
out.close();
}
void Mesh::update()
{}
Mesh::Mesh(const char * filename,bool normalize)
{
load_mesh(filename,normalize);
}
void Mesh::load_mesh(const char * filename, bool normalize)
{
std::ifstream f ;
f.open(filename);
if(!f.good()) {
std::cout<<"Error: cannot open mesh "<<filename<<"\n";
return;
}
switch(filename[strlen(filename)-1]) {
case 'y':
read_ply(f);
break;
case 'j':
read_obj(f);
break;
default:
break;
}
if(normalize){
rescale();
}
compute_norm();
f.close();
}
void Mesh::rescale()
{
if(v.size()==0){
std::cout<<"empty mesh\n";
return;
}
CompFab::Vec3 mn=v[0],mx=v[0];
//scale and translate to [0 , 1]
for (unsigned int dim = 0; dim<3; dim++) {
for( size_t ii=0; ii<v.size(); ii++) {
mn[dim]= std::min(v[ii][dim],mn[dim]);
mx[dim] = std::max(v[ii][dim],mx[dim]);
}
CompFab::precision_type translate = -mn[dim];
for(size_t ii=0; ii<v.size(); ii++) {
v[ii][dim]=(v[ii][dim]+translate);
}
}
CompFab::precision_type scale = 1/(mx[0]-mn[0]);
for(unsigned int dim=1; dim<3; dim++) {
scale=std::min(1/(mx[dim]-mn[dim]),scale);
}
for(size_t ii=0; ii<v.size(); ii++) {
for (unsigned int dim = 0; dim<3; dim++) {
v[ii][dim]=v[ii][dim]*scale;
}
}
}
void Mesh::compute_norm()
{
CompFab::Vec3 ZERO;
n.resize(v.size(), ZERO);
for(unsigned int ii=0; ii<t.size(); ii++) {
CompFab::Vec3 a = v[t[ii][1]] - v[t[ii][0]];
CompFab::Vec3 b = v[t[ii][2]] - v[t[ii][0]];
b=a%b;
b.normalize();
for(int jj=0; jj<3; jj++) {
n[t[ii][jj]]+=b;
if(t[ii][jj]>=(int)n.size() || t[ii][jj]<0){
std::cout<<ii<<" "<<jj<<" "<<t[ii][jj]<<" normal computation error\n";
}
}
}
for(unsigned int ii=0; ii<v.size(); ii++) {
n[ii].normalize();
}
}
void BBox(const Mesh & m,
CompFab::Vec3 & mn, CompFab::Vec3 & mx)
{
BBox(m.v, mn, mx);
}
bool is_nbr(const CompFab::Vec3i & a, const CompFab::Vec3i&b, int vert)
{
for (int ii=0; ii<3; ii++) {
int va=a[ii];
if(va<=vert) {
continue;
}
for (unsigned int jj=0; jj<3; jj++) {
int vb=b[jj];
if(vb<=vert) {
continue;
}
if(va==vb) {
return true;
}
}
}
return false;
}
void adjlist(const Mesh & m, std::vector<std::vector<int> > & adjMat)
{
if(adjMat.size()==m.t.size()) {
return;
}
std::vector<std::vector<int> >trigList;
trigList.resize(m.v.size());
for (unsigned int ii=0; ii<m.t.size(); ii++) {
for (unsigned int jj=0; jj<3; jj++) {
int vidx=m.t[ii][jj];
trigList[vidx].push_back(ii);
}
}
adjMat.resize(m.t.size());
for (unsigned int ii=0; ii<m.v.size(); ii++) {
int n_nbr=trigList[ii].size();
for (int jj=0; jj<n_nbr; jj++) {
int tj=trigList[ii][jj];
for (int kk=(jj+1); kk<n_nbr; kk++) {
int tk=trigList[ii][kk];
if(is_nbr(m.t[tj],m.t[tk],ii)) {
adjMat[tj].push_back(tk);
adjMat[tk].push_back(tj);
}
}
}
}
}
void BBox(const std::vector<CompFab::Vec3 >& v,
CompFab::Vec3 & mn, CompFab::Vec3 & mx)
{
mn = v[0];
mx = v[0];
for(unsigned int ii = 1 ;ii<v.size();ii++){
for(int dim = 0 ; dim<3;dim++){
if(v[ii][dim]<mn[dim]){
mn[dim] = v[ii][dim];
}
if(v[ii][dim]>mx[dim]){
mx[dim] = v[ii][dim];
}
}
}
}