-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmesh.h
265 lines (208 loc) · 6.47 KB
/
mesh.h
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
/*
* Copyright (c) 2012 Samuel Rødal
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef MESH_H
#define MESH_H
#include "point.h"
#include <QHash>
#include <QSet>
#include <QVarLengthArray>
#include <QVector>
#include <QVector3D>
#include <QtDebug>
class Mesh
{
public:
Mesh();
~Mesh();
void verify();
void borderize(qreal factor);
void catmullClarkSubdivide();
void addFace(const QVector<QVector3D> &points);
void addFace(const QVector<Point> &points);
void addConvexOutline(const QVector<Point> &outline);
QVector<QVector3D> normalBuffer() const;
QVector<QVector3D> vertexBuffer() const;
QVector<uint> indexBuffer() const;
private:
void makeBuffers() const;
void swap(Mesh &other);
class Allocator;
class Edge;
class Face;
class Vertex
{
public:
Point point() const { return m_point; }
Point normal() const
{
Point sum;
foreach (Face *face, m_faceList)
sum += face->normal();
return sum;
}
bool isPlanar() const
{
for (int i = 1; i < m_faceList.size(); ++i) {
if (m_faceList.at(i-1)->normal() != m_faceList.at(i)->normal())
return false;
}
return true;
}
int index() const { return m_index; }
Point catmullClarkMidpoint() const
{
int n = m_faceList.size();
Point result = point() / (n / (n - 3.));
Point faceSum;
for (int i = 0; i < n; ++i)
faceSum += m_faceList.at(i)->center();
result += faceSum / (n * n);
Point edgeSum;
for (int i = 0; i < n; ++i)
edgeSum += m_edges.at(i)->center();
result += edgeSum / (n * n * 0.5);
return result;
}
QSet<Face *> faceSet() const
{
return m_faces;
}
private:
Vertex(Point p, uint index)
: m_point(p)
, m_index(index)
{}
Point m_point;
QVarLengthArray<Edge *, 4> m_edges;
QSet<Face *> m_faces;
QList<Face *> m_faceList;
uint m_index;
friend class Mesh::Allocator;
};
class Edge
{
public:
Vertex *pa() const { return m_pa; }
Vertex *pb() const { return m_pb; }
Face *fa() const { return m_fa; }
Face *fb() const { return m_fb; }
Point center() const
{
return (m_pa->point() + m_pb->point()) / 2;
}
Point catmullClarkMidpoint() const
{
return (center() + (m_fa->center() + m_fb->center()) / 2) / 2;
}
bool planarNeighborhood() const
{
return m_fa->planarNeighborhood() && m_fb->planarNeighborhood();
}
bool contains(Vertex *v) const
{
return pa() == v || pb() == v;
}
static Vertex *sharedVertex(Edge *ea, Edge *eb)
{
if (ea->pa() == eb->pa() || ea->pa() == eb->pb())
return ea->pa();
else
return ea->pb();
}
private:
Edge(Vertex *a, Vertex *b, Face *f)
: m_pa(a), m_pb(b), m_fa(f), m_fb(0)
{
}
Vertex *m_pa;
Vertex *m_pb;
Face *m_fa;
Face *m_fb;
bool m_subdivides;
friend class Mesh::Allocator;
};
class Face
{
public:
void addEdge(Edge *edge);
int edgeCount() const { return m_edges.size(); }
Edge *edgeAt(int i) const { return m_edges.at(i); }
int edgeDirection(int i) const
{
Vertex *shared = Edge::sharedVertex(edgeAt(i), edgeAt((i+1)%edgeCount()));
return shared == edgeAt(i)->pb() ? 1 : -1;
}
bool planarNeighborhood() const
{
for (int i = 0; i < vertexCount(); ++i)
if (!vertexAt(i)->isPlanar())
return false;
return true;
}
QVector<Point> points() const
{
QVector<Point> result;
for (int i = 0; i < vertexCount(); ++i)
result << vertexAt(i)->point();
return result;
}
int vertexCount() const { return m_edges.size(); }
Vertex *vertexAt(int i) const;
Point normal() const
{
Point pa = vertexAt(0)->point();
Point pb = vertexAt(1)->point();
Point pc = vertexAt(2)->point();
return Point::crossProduct(pb - pa, pc - pa).normalized();
}
Point center() const
{
Point p;
for (int i = 0; i < vertexCount(); ++i)
p += vertexAt(i)->point();
return p / vertexCount();
}
private:
QVarLengthArray<Edge *, 4> m_edges;
bool m_subdivides;
};
class Allocator
{
public:
Allocator(Mesh *mesh);
~Allocator();
Edge *operator()(const Point &a, const Point &b, Face *face);
private:
typedef QHash<Point, Vertex *> VertexHash;
VertexHash m_vertexHash;
Mesh *m_mesh;
Mesh::Vertex *findVertex(const Point &p);
} allocate;
QList<Face *> m_faces;
QList<Vertex *> m_vertices;
QList<Edge *> m_edges;
mutable QVector<uint> m_indexBuffer;
mutable QVector<QVector3D> m_normalBuffer;
mutable QVector<QVector3D> m_vertexBuffer;
friend class Allocator;
};
#endif