-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMesh.cpp
More file actions
1808 lines (1546 loc) · 52.1 KB
/
Mesh.cpp
File metadata and controls
1808 lines (1546 loc) · 52.1 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mesh.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "Mesh.h"
#include <map>
#include <string>
#include <cassert>
#include <iomanip>
using namespace std;
#define ITERATE_TIMES 1
#define ALPHA_STRETCH 1.0
//////////////////////////////////////////////////////
// CVertex //
//////////////////////////////////////////////////////
CVertex::~CVertex()
{
if (m_piEdge != NULL)
delete[] m_piEdge;
m_lEdgeList.clear();
}
CVertex& CVertex::operator = (CVertex& v)
{
m_vPosition = v.m_vPosition;//
m_nValence = v.m_nValence;//Ķ
m_piEdge = new UINT[m_nValence];//Ӹõ㷢halfedge,ҪݵĶ̬
for (short i = 0; i<m_nValence; i++)
m_piEdge[i] = v.m_piEdge[i];
// m_lEdgeList = v.m_lEdgeList;//m_piEdgeʱ
m_bIsBoundary = v.m_bIsBoundary;
m_vNormal = v.m_vNormal;
m_nCutValence = v.m_nCutValence;
m_color = v.m_color;
m_bValid = v.m_bValid;
return *this;
}
//////////////////////////////////////////////////////
// CEdge //
//////////////////////////////////////////////////////
CEdge::~CEdge()
{
}
CEdge& CEdge::operator = (const CEdge& e)
{
m_iVertex[0] = e.m_iVertex[0];//ߵ˵㣬Vertex0>Vertex1
m_iVertex[1] = e.m_iVertex[1];
m_iTwinEdge = e.m_iTwinEdge;//ñ߷෴һߣΪ-1ñΪ߽
m_iNextEdge = e.m_iNextEdge;//ʱ뷽һ
m_iFace = e.m_iFace;//ñ棬Ӧ
m_bCut = e.m_bCut;
m_nCutTag = e.m_nCutTag;
m_color = e.m_color;
m_length = e.m_length;
m_bValid = e.m_bValid;
return *this;
}
//////////////////////////////////////////////////////
// CFace //
//////////////////////////////////////////////////////
CFace::CFace(short s)
{
m_nType = s;
m_vNormal = Vector3D(0.0, 0.0, 1.0);
m_dArea = 0.0;
m_piEdge = new UINT[s];
m_piVertex = new UINT[s];
m_bValid = true;
}
CFace::~CFace()
{
if (m_piEdge != NULL)
delete[] m_piEdge;
if (m_piVertex != NULL)
delete[] m_piVertex;
if (m_pdAngle != NULL)
delete[] m_pdAngle;
}
void CFace::Create(short s)
{
m_nType = s;
m_vNormal = Vector3D(0.0, 0.0, 1.0);
m_dArea = 0.0;
m_piEdge = new UINT[s];
m_piVertex = new UINT[s];
m_pdAngle = new double[s];
}
CFace& CFace::operator =(const CFace& f)
{
m_nType = f.m_nType;//
m_piVertex = new UINT[m_nType];//е
m_piEdge = new UINT[m_nType];//б
for (short i = 0; i<m_nType; i++)
{
m_piVertex[i] = f.m_piVertex[i];
m_piEdge[i] = f.m_piEdge[i];
}
m_vNormal = f.m_vNormal;//
m_vMassPoint = f.m_vMassPoint;//
m_dArea = f.m_dArea;//
m_color = f.m_color;
m_bValid = f.m_bValid;
return (*this);
}
//////////////////////////////////////////////////////
// CMesh //
//////////////////////////////////////////////////////
void CMesh::clear()
{
if (m_pVertex != NULL) { delete[] m_pVertex; m_pVertex = NULL; }
if (m_pEdge != NULL) { delete[] m_pEdge; m_pEdge = NULL; }
if (m_pFace != NULL) { delete[] m_pFace; m_pFace = NULL; }
if (m_pAngles != NULL) { delete[] m_pAngles; m_pAngles = NULL; }
m_nVertex = m_nEdge = m_nFace = 0;
m_lFocusEdge.clear();
m_lFocusVertex.clear();
}
CMesh::CMesh(CMesh* pMesh)
{
m_nVertexCapacity = pMesh->m_nVertexCapacity;
m_nEdgeCapacity = pMesh->m_nEdgeCapacity;
m_nFaceCapacity = pMesh->m_nFaceCapacity;
m_nVertex = pMesh->m_nVertex; //
m_pVertex = new CVertex[m_nVertexCapacity]; //
UINT i;
for (i = 0; i<m_nVertex; i++)
m_pVertex[i] = pMesh->m_pVertex[i];
m_pAngles = new double[m_nVertexCapacity];
for (i = 0; i < m_nVertex; ++i)
m_pAngles[i] = pMesh->m_pAngles[i];
m_nEdge = pMesh->m_nEdge; //
m_pEdge = new CEdge[m_nEdgeCapacity]; //߱
for (i = 0; i<m_nEdge; i++)
m_pEdge[i] = pMesh->m_pEdge[i];
m_nFace = pMesh->m_nFace; //
m_pFace = new CFace[m_nFaceCapacity]; //
for (i = 0; i<m_nFace; i++)
m_pFace[i] = pMesh->m_pFace[i];
// m_pAngles = new double[m_nVertex];
// for (i = 0; i < m_nVertex; ++i)
// m_pAngles[i] = pMesh->m_pAngles[i];
m_lFocusEdge = pMesh->m_lFocusEdge;
m_lFocusVertex = pMesh->m_lFocusVertex;
m_lFocusFace = pMesh->m_lFocusFace;
// _UINTLIST m_lPickedVertex;
m_iPickedVertex = pMesh->m_iPickedVertex;
m_iPickedEdge = pMesh->m_iPickedEdge;
m_iPickedFace = pMesh->m_iPickedFace;
filename = pMesh->filename;
isVisible = pMesh->isVisible;
m_pTexture = pMesh->m_pTexture;
maxTex = pMesh->maxTex;
color = pMesh->color;
isVisible = pMesh->isVisible;
m_nValidVertNum = pMesh->m_nValidVertNum;
m_nValidFaceNum = pMesh->m_nValidFaceNum;
m_nValidEdgeNum = pMesh->m_nValidEdgeNum;
isolatedPoints = pMesh->isolatedPoints;
scaleD = pMesh->scaleD;
origin = pMesh->origin;
bboxSize = pMesh->bboxSize;
m_bClosed = pMesh->m_bClosed;
}
CMesh* CMesh::clone()
{
return NULL;
}
CMesh::~CMesh()
{
clear();
}
bool CMesh::Load(const char* sFileName)
{
clear();
filename = sFileName;
std::string suffix = filename.substr(filename.rfind('.'), filename.length() - filename.rfind('.'));
if (suffix == ".obj" || suffix == ".OBJ")
return loadOBJ(sFileName);
if (suffix == ".off" || suffix == ".OFF")
return loadOFF(sFileName);
if (suffix == ".m" || suffix == ".M")
return loadM(sFileName);
}
bool CMesh::loadOFF(const char* sFileName)
{
_VECTORLIST VertexList;
_UINTLIST FaceList;
std::vector<Vector3D> normals;
//open the file
filename = sFileName;
ifstream fin;
fin.open(sFileName);
if (!fin) return false;
Vector3D vector;
Vector2D vector2d;
Vector3D vMin(1e30, 1e30, 1e30);
Vector3D vMax(-1e30, -1e30, -1e30);
bool bFirst = true;
maxTex = CTexture(0, 0);
UINT l[3];
short j;
std::string curLine;
getline(fin, curLine);
getline(fin, curLine);
int nVert = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
int nFace = atoi(curLine.substr(0, curLine.find(" ")).c_str());
for (int i = 0; i < nVert; ++i)
{
getline(fin, curLine);
vector.x = atof(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
vector.y = atof(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
vector.z = atof(curLine.c_str());
if (vector.x < vMin.x) vMin.x = vector.x;
if (vector.x > vMax.x) vMax.x = vector.x;
if (vector.y < vMin.y) vMin.y = vector.y;
if (vector.y > vMax.y) vMax.y = vector.y;
if (vector.z < vMin.z) vMin.z = vector.z;
if (vector.z > vMax.z) vMax.z = vector.z;
VertexList.push_back(vector);
}
for (int i = 0; i < nFace; ++i)
{
getline(fin, curLine);
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[0] = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[1] = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[2] = atoi(curLine.c_str());
FaceList.push_back(l[0]); FaceList.push_back(l[1]); FaceList.push_back(l[2]);
}
vector = vMax - vMin;
scaleD = max(vector.x, vector.y);
scaleD = 0.5*max(scaleD, vector.z);
bboxSize = vector;
origin = 0.5*(vMax + vMin);//ƫ
return loadFromSMF(VertexList, FaceList, normals);
}
bool CMesh::loadOBJ(const char* sFileName)
{
_VECTORLIST VertexList;
_UINTLIST FaceList;
std::vector<Vector3D> normals;
//open the file
filename = sFileName;
ifstream fin;
fin.open(sFileName);
if (!fin) return false;
Vector3D vector;
Vector2D vector2d;
Vector3D vMin(1e30, 1e30, 1e30);
Vector3D vMax(-1e30, -1e30, -1e30);
bool bFirst = true;
maxTex = CTexture(0, 0);
UINT l[3];
char tmpC;
short j;
std::map<Vector3D, UINT> redunVertex;
std::vector<UINT> realIndex;
UINT vertexIndex = 0;
string buf;
while (getline(fin, buf))
{
if (buf.size() > 0) //non-empty line
{
if (buf[0] == 'v' && buf[1] == ' ')//A line starting with a 'v' designating a vertex position information
{
//%S: S same as ls. A wide character string is expected; the corresponding argument should be a wide character pointer pointing to an array of wide characters large enough to accept the wide character string and a terminating \0
istringstream sin(buf);
sin >> tmpC >> vector.x >> vector.y >> vector.z;
// if(bFirst)
// {
// bFirst=false;
// vMin=vMax=vector;
// continue;
// }
// map<Vector3D, UINT>::iterator iter = redunVertex.find(vector);
// if (iter == redunVertex.end())
// {
redunVertex[vector] = vertexIndex;
realIndex.push_back(vertexIndex++);
VertexList.push_back(Vector3D(vector));
// }
// else
// realIndex.push_back(iter->second);
if (vector.x<vMin.x) vMin.x = vector.x;
if (vector.x>vMax.x) vMax.x = vector.x;
if (vector.y<vMin.y) vMin.y = vector.y;
if (vector.y>vMax.y) vMax.y = vector.y;
if (vector.z<vMin.z) vMin.z = vector.z;
if (vector.z>vMax.z) vMax.z = vector.z;
}
else if (buf[0] == 'v' && buf[1] == 't')
{
string buftmp = buf.substr(3, buf.length() - 3);
istringstream sin(buftmp);
sin >> vector2d.x >> vector2d.y;
CTexture curTexture(vector2d);
m_pTexture.push_back(curTexture);
if (vector2d.x > maxTex.m_vPosition.x) maxTex.m_vPosition.x = vector2d.x;
if (vector2d.y > maxTex.m_vPosition.y) maxTex.m_vPosition.y = vector2d.y;
}
else if (buf[0] == 'v' && buf[1] == 'n')
{
istringstream sin(buf);
sin >> tmpC >> tmpC >> vector.x >> vector.y >> vector.z;
normals.push_back(vector);
}
else if (buf[0] == 'f')
{
istringstream sin(buf);
int tx, ty, tz;
size_t slashPos = buf.find("/");
if (slashPos == string::npos) sscanf(buf.c_str(), "f %d %d %d", &l[0], &l[1], &l[2]);
else if (buf[slashPos + 1] != '/') sscanf(buf.c_str(), "f %d/%d %d/%d %d/%d", &l[0], &tx, &l[1], &ty, &l[2], &tz);
else if (buf[slashPos + 2] != '/') sscanf(buf.c_str(), "f %d//%d %d//%d %d//%d", &l[0], &tx, &l[1], &ty, &l[2], &tz);
if (realIndex[l[0] - 1] == realIndex[l[1] - 1] || realIndex[l[1] - 1] == realIndex[l[2] - 1] || realIndex[l[2] - 1] == realIndex[l[0] - 1]) continue;
//sin>>tmpC>>l[0]>>l[1]>>l[2];
for (j = 0; j < 3; j++)
FaceList.push_back(realIndex[l[j] - 1]);
}
else if (buf[0] == 'p')
{
istringstream sin(buf);
sin >> tmpC >> vector.x >> vector.y >> vector.z;
isolatedPoints.push_back(vector);
}
}
}
fin.close();
assert(normals.empty() || VertexList.size() == normals.size());
vector = vMax - vMin;
scaleD = max(vector.x, vector.y);
scaleD = 0.5*max(scaleD, vector.z);
bboxSize = vector;
origin = 0.5*(vMax + vMin);//ƫ
return loadFromSMF(VertexList, FaceList, normals);
}
bool CMesh::loadM(const char* sFileName)
// ----- format: smf,obj,dat -----
//vertex:
// v x y z,
//face(triangle):
// f v1 v2 v3 (the vertex index is 1-based)
{
//open the file
ifstream fin;
fin.open(sFileName);
if (!fin) return false;
_VECTORLIST VertexList;//临时存储所有点的链表
_UINTLIST FaceList;//临时存储所有面包含的点的信息
std::vector<Vector3D> normals;
Vector3D vector;
Vector2D vector2d;
Vector3D vMin(1e30, 1e30, 1e30);
Vector3D vMax(-1e30, -1e30, -1e30);
bool bFirst = true;
maxTex = CTexture(0, 0);
UINT l[3];
char tmpC;
short j;
std::vector<UINT> realIndex;
UINT vertexIndex = 0;
std::map<pair<unsigned, unsigned>, double> realEdgeLen;
string buf;
while (getline(fin, buf)) //将点和面的信息临时存在VertexList和FaceList中
{
std::vector<string> parts = splitString(buf, " ");
if (parts.size() > 0) //non-empty line
{
if (parts[0] == "Vertex")
{
vector.x = atof(parts[2].c_str()); vector.y = atof(parts[3].c_str()); vector.z = atof(parts[4].c_str());
VertexList.push_back(vector);
vertexIndex = atoi(parts[1].c_str());
realIndex.resize(vertexIndex, -1);
realIndex[vertexIndex - 1] = VertexList.size() - 1;
if (vector.x<vMin.x)
vMin.x = vector.x;
if (vector.x>vMax.x)
vMax.x = vector.x;
if (vector.y<vMin.y)
vMin.y = vector.y;
if (vector.y>vMax.y)
vMax.y = vector.y;
if (vector.z<vMin.z)
vMin.z = vector.z;
if (vector.z>vMax.z)
vMax.z = vector.z;
}
else if (parts[0] == "Face")
{
for (j = 0; j < 3; j++)
l[j] = atoi(parts[j + 2].c_str());
if (realIndex[l[0] - 1] == realIndex[l[1] - 1] || realIndex[l[1] - 1] == realIndex[l[2] - 1] || realIndex[l[2] - 1] == realIndex[l[0] - 1]) continue;
//sin>>tmpC>>l[0]>>l[1]>>l[2];
for (j = 0; j < 3; j++) FaceList.push_back(realIndex[l[j] - 1]);
if (parts.size() <= 5) continue;
for (int j = 5; j < parts.size(); ++j)
{
if (parts[j].find("length") != string::npos)
{
double len0, len1, len2;
len0 = atof(parts[j].substr(parts[j].find("(") + 1, parts[j].length() - parts[j].find("(") - 1).c_str());
len1 = atof(parts[j + 1].c_str());
len2 = atof(parts[j + 2].substr(0, parts[j + 2].find(")")).c_str());
realEdgeLen[make_pair(realIndex[l[0] - 1], realIndex[l[1] - 1])] = len0;
realEdgeLen[make_pair(realIndex[l[1] - 1], realIndex[l[2] - 1])] = len1;
realEdgeLen[make_pair(realIndex[l[2] - 1], realIndex[l[0] - 1])] = len2;
}
}
}
}
}
fin.close();
//根据包围盒将物体中心移至原点,同时进行归一化
vector = vMax - vMin;
double d = __max(vector.x, vector.y);
d = 0.5*__max(d, vector.z);
vector = 0.5*(vMax + vMin);//偏移量
scaleD = d;
origin = vector;
loadFromSMF(VertexList, FaceList, normals);
if (realEdgeLen.empty()) return true;
for (unsigned i = 0; i < m_nEdge; ++i)
{
unsigned v0 = m_pEdge[i].m_iVertex[0];
unsigned v1 = m_pEdge[i].m_iVertex[1];
m_pEdge[i].m_length = realEdgeLen[make_pair(v0, v1)];
}
// check edge length
for (unsigned i = 0; i < m_nEdge; ++i)
{
if (i > m_pEdge[i].m_iTwinEdge) continue;
if (m_pEdge[i].m_iTwinEdge == -1) continue;
assert(m_pEdge[i].m_length == m_pEdge[m_pEdge[i].m_iTwinEdge].m_length);
}
// re-calculate areas
for (unsigned i = 0; i < m_nFace; ++i)
{
double a = m_pEdge[m_pFace[i].m_piEdge[0]].m_length;
double b = m_pEdge[m_pFace[i].m_piEdge[1]].m_length;
double c = m_pEdge[m_pFace[i].m_piEdge[2]].m_length;
double p = (a + b + c) / 2.0;
m_pFace[i].m_dArea = sqrt(fabs(p * (p - a) * (p - b) * (p - c)));
}
return true;
}
bool CMesh::loadFromSMF(std::list<Vector3D> &VertexList, std::list<UINT> &FaceList, std::vector<Vector3D> &normals)
// ----- format: smf,obj,dat -----
//vertex:
// v x y z,
//face(triangle):
// f v1 v2 v3 (the vertex index is 1-based)
{
m_nVertex = (UINT)VertexList.size();
m_nFace = (UINT)FaceList.size() / 3;
m_nEdge = (UINT)3 * m_nFace;
m_nVertexCapacity = m_nVertex * 2;
m_nEdgeCapacity = m_nEdge * 2;
m_nFaceCapacity = m_nFace * 2;
//read vertices and faces
m_pVertex = new CVertex[m_nVertexCapacity];
if (m_pVertex == NULL) { clear(); return false; }//out of memory
m_pFace = new CFace[m_nFaceCapacity];
if (m_pFace == NULL) { clear(); return false; }//out of memory
m_pAngles = new double[m_nVertexCapacity];
if (!m_pAngles) { clear(); return false; }
memset(m_pAngles, 0, sizeof(double) * m_nVertexCapacity);
UINT i;
_VECTORLIST::iterator iVertex = VertexList.begin();
_UINTLIST::iterator iFace = FaceList.begin();
for (i = 0; i<m_nVertex; i++)
/*m_pVertex[i].m_vPosition=(*(iVertex++)-vector)/d;*/
m_pVertex[i].m_vPosition = *(iVertex++);
for (i = 0; i < isolatedPoints.size(); ++i)
isolatedPoints[i] = (isolatedPoints[i] - origin) / scaleD;
for (i = 0; i<m_nFace; i++)
{
m_pFace[i].Create(3);
for (int j = 0; j<3; j++)
m_pFace[i].m_piVertex[j] = *iFace++;
}
VertexList.clear();
FaceList.clear();
if (!construct()) return false;
if (!normals.empty())
{
for (unsigned i = 0; i < m_nVertex; ++i)
{
m_pVertex[i].m_vRawNormal = normals[i];
/*m_pVertex[i].m_vRawNormal.normalize();*/
}
}
return true;
}
bool CMesh::Save(const char* sFileName)
{
//this->filename = sFileName;
ofstream fout(sFileName);
fout << "# vertices: " << m_nValidVertNum << endl;
fout << "# faces: " << m_nValidFaceNum << endl;
std::vector<unsigned> realIndex;
unsigned idx = 0;
for (UINT i = 0; i < m_nVertex; ++i) {
realIndex.push_back(idx);
if (!m_pVertex[i].m_bValid) continue;
Vector3D v = m_pVertex[i].m_vPosition;
fout << "v " << setprecision(20) << v.x << " " << v.y << " " << v.z << endl;
++idx;
}
for (UINT i = 0; i < m_nFace; ++i) {
if (!m_pFace[i].m_bValid) continue;
UINT* v = m_pFace[i].m_piVertex;
fout << "f " << realIndex[v[0]] + 1 << " " << realIndex[v[1]] + 1 << " " << realIndex[v[2]] + 1 << endl;
}
fout.close();
return true;
}
std::istream& operator >> (std::istream &input, CMesh &mesh)
{
_VECTORLIST VertexList;
_UINTLIST FaceList;
std::vector<Vector3D> normals;
Vector3D vector;
Vector2D vector2d;
Vector3D vMin(1e30, 1e30, 1e30);
Vector3D vMax(-1e30, -1e30, -1e30);
bool bFirst = true;
mesh.maxTex = CTexture(0, 0);
UINT l[3];
short j;
std::string curLine;
getline(input, curLine);
getline(input, curLine);
int nVert = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
int nFace = atoi(curLine.substr(0, curLine.find(" ")).c_str());
for (int i = 0; i < nVert; ++i)
{
getline(input, curLine);
vector.x = atof(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
vector.y = atof(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
vector.z = atof(curLine.c_str());
if (vector.x < vMin.x) vMin.x = vector.x;
if (vector.x > vMax.x) vMax.x = vector.x;
if (vector.y < vMin.y) vMin.y = vector.y;
if (vector.y > vMax.y) vMax.y = vector.y;
if (vector.z < vMin.z) vMin.z = vector.z;
if (vector.z > vMax.z) vMax.z = vector.z;
VertexList.push_back(vector);
}
for (int i = 0; i < nFace; ++i)
{
getline(input, curLine);
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[0] = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[1] = atoi(curLine.substr(0, curLine.find(" ")).c_str());
curLine = curLine.substr(curLine.find(" ") + 1, curLine.length() - curLine.find(" ") - 1);
l[2] = atoi(curLine.c_str());
FaceList.push_back(l[0]); FaceList.push_back(l[1]); FaceList.push_back(l[2]);
}
vector = vMax - vMin;
mesh.scaleD = max(vector.x, vector.y);
mesh.scaleD = 0.5*max(mesh.scaleD, vector.z);
mesh.bboxSize = vector;
mesh.origin = 0.5*(vMax + vMin);//ƫ
mesh.loadFromSMF(VertexList, FaceList, normals);
return input;
}
void CMesh::calcAllEdgeLength(){
for (UINT i = 0; i < m_nEdge; ++i) {
UINT nodeA = m_pEdge[i].m_iVertex[0];
UINT nodeB = m_pEdge[i].m_iVertex[1];
double len = Vector3D(m_pVertex[nodeA].m_vPosition
- m_pVertex[nodeB].m_vPosition).length();
m_pEdge[i].m_length = len;
}
}
bool CMesh::saveToSMF(const char* sFileName)
{
return true;
}
bool CMesh::reConstruct()
{
_VECTORLIST VertexList;//ʱ洢е
_UINTLIST FaceList;//ʱ洢ĵϢ
Vector3D vector;
Vector3D vMin;
Vector3D vMax;
bool bFirst = true;
UINT i;
UINT* old2new = new UINT[m_nVertex];
UINT iNewIndex = 0;
for (i = 0; i<m_nVertex; i++)
{
VertexList.push_back(m_pVertex[i].m_vPosition);
old2new[i] = iNewIndex++;
}
for (i = 0; i<m_nFace; i++)
for (short j = 0; j<3; j++)
FaceList.push_back(old2new[m_pFace[i].m_piVertex[j]]);
clear();
m_nVertex = (UINT)VertexList.size();
m_nFace = (UINT)FaceList.size() / 3;
m_nEdge = (UINT)3 * m_nFace;
//read vertices and faces
m_nVertexCapacity = m_nVertex * 2;
m_nFaceCapacity = m_nFace * 2;
m_pVertex = new CVertex[m_nVertexCapacity];
if (m_pVertex == NULL) { clear(); return false; }//out of memory
m_pFace = new CFace[m_nFaceCapacity];
if (m_pFace == NULL) { clear(); return false; }//out of memory
m_pAngles = new double[m_nVertexCapacity];
if (!m_pAngles) { clear(); return false; }
memset(m_pAngles, 0, sizeof(double) * m_nVertexCapacity);
_VECTORLIST::iterator iVertex = VertexList.begin();
_UINTLIST::iterator iFace = FaceList.begin();
for (i = 0; i<m_nVertex; i++)
m_pVertex[i].m_vPosition = *iVertex++;
for (i = 0; i<m_nFace; i++)
{
m_pFace[i].Create(3);
for (short j = 0; j<3; j++)
m_pFace[i].m_piVertex[j] = *iFace++;
}
delete[] old2new;
VertexList.clear();
FaceList.clear();
return construct();
}
bool CMesh::construct()
{
if ((m_pVertex == NULL) || (m_pFace == NULL)) return false;//empty
if (m_pEdge != NULL){ delete[] m_pEdge; m_pEdge = NULL; }//delete old edgelist
m_bClosed = true;
UINT i;
if (m_nEdge == 0)//loadӦѾδɨ棬õܱ
for (i = 0; i<m_nFace; i++)
m_nEdge += m_pFace[i].m_nType;
m_nEdgeCapacity = m_nEdge * 2;
m_pEdge = new CEdge[m_nEdgeCapacity];
short j, nType;
UINT iEdge = 0;
UINT iVertex;
for (i = 0; i<m_nFace; i++)//ɨ
{
calFaceNormal(i);//˳ķ
nType = m_pFace[i].m_nType;
for (j = 0; j<nType; j++)//ÿ潨صĵ㡢ߡϢ
{
//Ϣ
m_pFace[i].m_piEdge[j] = iEdge;//˳ÿ߷
//Ϣ
iVertex = m_pFace[i].m_piVertex[j];
m_pVertex[iVertex].m_nValence++;//1
m_pVertex[iVertex].m_lEdgeList.push_back(iEdge);//Ƚñʱ߱
//ߵϢ
m_pEdge[iEdge].m_iFace = i;
m_pEdge[iEdge].m_iVertex[0] = iVertex;//Դ
m_pEdge[iEdge].m_iVertex[1] = m_pFace[i].m_piVertex[(j + 1) % nType];//Ŀ
m_pEdge[iEdge].m_iNextEdge = iEdge + 1;
iEdge++;
}
m_pEdge[iEdge - 1].m_iNextEdge -= nType;
}
_UINTLIST::iterator iv;
UINT iSrcVertex, iDesVertex;
bool bFlag;
for (i = 0; i<m_nEdge; i++)//ɨбߣбߵm_iTwinEdgeӹϵ
{
if (m_pEdge[i].m_iTwinEdge != -1)//Ѿ
continue;
iSrcVertex = m_pEdge[i].m_iVertex[0];//Դ
iDesVertex = m_pEdge[i].m_iVertex[1];//Ŀ
//Ŀ㷢бߣûлصԴı
bFlag = true;
for (iv = m_pVertex[iDesVertex].m_lEdgeList.begin(); iv != m_pVertex[iDesVertex].m_lEdgeList.end(); iv++)
{
iEdge = *iv;
if (m_pEdge[iEdge].m_iVertex[1] == iSrcVertex)
{
m_pEdge[i].m_iTwinEdge = iEdge;
m_pEdge[iEdge].m_iTwinEdge = i;
bFlag = false;
break;
}
}
//ûУ˵ԴDZ߽
if (bFlag)
{
m_pVertex[iSrcVertex].m_bIsBoundary = true;
m_bClosed = false;
}
}
short nValence;
UINT iTwinEdge;
//ʼWedgeĴСΪ2m_nVertexԱԺչ
for (i = 0; i<m_nVertex; i++)//ɨе㣬ÿm_lEdgelist,˳ʱ뷽m_piEdge,
{
nValence = m_pVertex[i].m_nValence;
if (nValence<2)//ijĶС2
m_pVertex[i].m_bIsBoundary = true;
if (nValence == 0)//аĵ㣬Ӧýֹ
continue;
if (m_pVertex[i].m_piEdge != NULL)
delete[] m_pVertex[i].m_piEdge;
m_pVertex[i].m_piEdge = new UINT[nValence];
// if(m_pVertex[i].m_piEdge[0]==-1)//temp
// return false;
if (m_pVertex[i].m_bIsBoundary)//߽
{
for (iv = m_pVertex[i].m_lEdgeList.begin(); iv != m_pVertex[i].m_lEdgeList.end(); iv++)
{
bFlag = true;
m_pVertex[i].m_piEdge[0] = *iv;
for (j = 1; j<nValence; j++)
{
iTwinEdge = m_pEdge[m_pVertex[i].m_piEdge[j - 1]].m_iTwinEdge;
if (iTwinEdge == -1)//߽Edge
{
bFlag = false;
break;
}
m_pVertex[i].m_piEdge[j] = m_pEdge[iTwinEdge].m_iNextEdge;
}
if (bFlag)//success
break;
}
}
else//ڲ
{
iv = m_pVertex[i].m_lEdgeList.begin();
m_pVertex[i].m_piEdge[0] = *iv;
for (j = 1; j<nValence; j++)//õ㷢half edge˳ʱm_piEdge
{
iTwinEdge = m_pEdge[m_pVertex[i].m_piEdge[j - 1]].m_iTwinEdge;
// if(iTwinEdge==-1)//temp
// return false;
m_pVertex[i].m_piEdge[j] = m_pEdge[iTwinEdge].m_iNextEdge;
}
}
m_pVertex[i].m_lEdgeList.clear();//ʱ
for (j = 0; j<nValence; j++)
m_pVertex[i].m_lEdgeList.push_back(m_pVertex[i].m_piEdge[j]);
calVertexNormal(i);//ݵϢõƽ
// calVertexMeanCurvature(i);
}
calcAllEdgeLength();
calcVertexAngle();
m_nValidVertNum = m_nVertex;
m_nValidFaceNum = m_nFace;
m_nValidEdgeNum = m_nEdge;
return true;
}
void CMesh::expandCapacity(){
if ((m_nVertex + 8 >= m_nVertexCapacity) ||
(m_nEdge + 8 >= m_nEdgeCapacity) ||
(m_nFace + 8 >= m_nFaceCapacity)){
m_nVertexCapacity *= 2;
m_nEdgeCapacity *= 2;
m_nFaceCapacity *= 2;
CVertex* m_pVertexNew = new CVertex[m_nVertexCapacity];
for (UINT i = 0; i < m_nVertex; ++i) {
m_pVertexNew[i] = m_pVertex[i];
}
CEdge* m_pEdgeNew = new CEdge[m_nEdgeCapacity];
for (UINT i = 0; i < m_nEdge; ++i) {
m_pEdgeNew[i] = m_pEdge[i];
}
CFace* m_pFaceNew = new CFace[m_nFaceCapacity];
for (UINT i = 0; i < m_nFace; ++i) {
m_pFaceNew[i] = m_pFace[i];
}
double *m_pAnglesNew = new double[m_nVertexCapacity];
for (unsigned i = 0; i < m_nVertex; ++i)
m_pAnglesNew[i] = m_pAngles[i];
if (m_pVertex != NULL) delete[] m_pVertex;
if (m_pEdge != NULL) delete[] m_pEdge;
if (m_pFace != NULL) delete[] m_pFace;
if (m_pAngles) delete[] m_pAngles;
m_pVertex = m_pVertexNew;
m_pEdge = m_pEdgeNew;
m_pFace = m_pFaceNew;
m_pAngles = m_pAnglesNew;
}
}
void CMesh::calcVertexAngle()
{
for (unsigned i = 0; i < m_nFace; ++i)
{
for (unsigned j = 0; j < m_pFace[i].m_nType; ++j)
{
unsigned curVert = m_pFace[i].m_piVertex[j];
double edge0 = m_pEdge[m_pFace[i].m_piEdge[j]].m_length;
double edge1 = m_pEdge[m_pFace[i].m_piEdge[(j + m_pFace[i].m_nType - 1) % m_pFace[i].m_nType]].m_length;
Vector3D vec0 = m_pVertex[m_pFace[i].m_piVertex[j]].m_vPosition;
Vector3D vec1 = m_pVertex[m_pFace[i].m_piVertex[(j + 1) % m_pFace[i].m_nType]].m_vPosition;
Vector3D vec2 = m_pVertex[m_pFace[i].m_piVertex[(j - 1 + m_pFace[i].m_nType) % m_pFace[i].m_nType]].m_vPosition;
double edge2 = (vec1 - vec2).length();
double curAngle = acos((edge0*edge0 + edge1*edge1 - edge2*edge2) / (2 * edge0*edge1));
if (((vec0 - vec2) ^ (vec1 - vec0)) * m_pFace[i].m_vNormal < 0) curAngle = 2 * PI - curAngle;
m_pFace[i].m_pdAngle[j] = curAngle;
m_pAngles[curVert] += curAngle;
}
}
}
/******************************************************
O O
/ \ /|\
/ \ / | \
O-----O =========> O--O--O
\ / \ | /
\ / \|/
O O
*******************************************************/
UINT CMesh::split(UINT iEdge, double pos){ //split at the middle of the edge
if (iEdge < 0 || iEdge >= m_nEdge) return -1;
double originEdgeLen = m_pEdge[iEdge].m_length;
UINT e2 = iEdge;
UINT e6 = m_pEdge[e2].m_iTwinEdge;
UINT e3 = m_pEdge[e2].m_iNextEdge;
UINT e1 = m_pEdge[e3].m_iNextEdge;
UINT f1 = m_pEdge[e2].m_iFace;
UINT v2 = m_pEdge[e2].m_iVertex[0];
UINT v4 = m_pEdge[e2].m_iVertex[1];
UINT v1 = m_pEdge[e1].m_iVertex[0];
Vector3D newVertPos = m_pVertex[v4].m_vPosition - m_pVertex[v2].m_vPosition;
newVertPos.normalize();
if (pos > 0.0) newVertPos = m_pVertex[v2].m_vPosition + newVertPos * pos;
else newVertPos = (m_pVertex[v2].m_vPosition + m_pVertex[v4].m_vPosition) / 2.0;
double edgeLen1 = (newVertPos - m_pVertex[v2].m_vPosition).length();
double edgeLen2 = (newVertPos - m_pVertex[v4].m_vPosition).length();
double newEdgeLen = (m_pVertex[v1].m_vPosition - newVertPos).length();
if (e6 == -1) {
UINT v5 = (m_nVertex++);
UINT e8 = (m_nEdge++);
UINT e9 = (m_nEdge++);