-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.cpp
141 lines (111 loc) · 2.42 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
#include "Mesh.h"
void Mesh::loadOff(char* name)
{
FILE* fPtr = fopen(name, "r");
char str[334];
fscanf(fPtr, "%s", str);
int nVerts, nTris, n, i = 0;
float x, y, z;
fscanf(fPtr, "%d %d %d\n", &nVerts, &nTris, &n);
while (i++ < nVerts)
{
fscanf(fPtr, "%f %f %f", &x, &y, &z);
addVertex(x, y, z);
}
while (fscanf(fPtr, "%d", &i) != EOF)
{
fscanf(fPtr, "%f %f %f", &x, &y, &z);
addTriangle((int) x, (int) y, (int) z);
makeVertsNeighbor(x, y);
makeVertsNeighbor(y, z);
makeVertsNeighbor(x, z);
}
fclose(fPtr);
}
void Mesh::createCube(float sideLen)
{
//coordinates
float flbc[3] = {0, 0, 0}, deltaX = 0, deltaY = 0, deltaZ = 0;
for (int v = 0; v < 8; v++)
{
switch (v)
{
case 1:
deltaX = sideLen;
break;
case 2:
deltaZ = -sideLen;
break;
case 3:
deltaX = 0;
break;
case 4:
deltaZ = 0;
deltaY = sideLen;
break;
case 5:
deltaX = sideLen;
break;
case 6:
deltaZ = -sideLen;
break;
default:
deltaX = 0;;
break;
}
addVertex(flbc[0] + deltaX, flbc[1] + deltaY, flbc[2] + deltaZ);
}
addTriangle(0, 2, 1);
addTriangle(0, 3, 2);
addTriangle(1, 2, 5);
addTriangle(2, 6, 5);
addTriangle(2, 3, 6);
addTriangle(3, 7, 6);
addTriangle(3, 4, 7);
addTriangle(3, 0, 4);
addTriangle(4, 5, 6);
addTriangle(4, 6, 7);
addTriangle(0, 1, 5);
addTriangle(0, 5, 4);
}
void Mesh::addTriangle(int v1, int v2, int v3)
{
int idx = tris.size();
tris.push_back( new Triangle(idx, v1, v2, v3) );
//set up structure
verts[v1]->triList.push_back(idx);
verts[v2]->triList.push_back(idx);
verts[v3]->triList.push_back(idx);
if (! makeVertsNeighbor(v1, v2) )
addEdge(v1, v2);
if (! makeVertsNeighbor(v1, v3) )
addEdge(v1, v3);
if (! makeVertsNeighbor(v2, v3) )
addEdge(v2, v3);
}
bool Mesh::makeVertsNeighbor(int v1i, int v2i)
{
//returns true if v1i already neighbor w/ v2i; false o/w
for (int i = 0; i < verts[v1i]->vertList.size(); i++)
if (verts[v1i]->vertList[i] == v2i)
return true;
verts[v1i]->vertList.push_back(v2i);
verts[v2i]->vertList.push_back(v1i);
return false;
}
void Mesh::addVertex(float x, float y, float z)
{
int idx = verts.size();
float* c = new float[3];
c[0] = x;
c[1] = y;
c[2] = z;
verts.push_back( new Vertex(idx, c) );
}
void Mesh::addEdge(int v1, int v2)
{
int idx = edges.size();
edges.push_back( new Edge(idx, v1, v2) );
verts[v1]->edgeList.push_back(idx);
verts[v2]->edgeList.push_back(idx);
}