forked from nissmar/VSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalfedgeDS.cpp
233 lines (202 loc) · 5.19 KB
/
HalfedgeDS.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
/* ========================================================================= *
* *
* Luca Castelli Aleardi *
* Copyright (c) 2019, Ecole Polytechnique *
* Department of Computer Science *
* All rights reserved. *
* *
*---------------------------------------------------------------------------*
* This file is part of the course material developed for *
* INF574 Digital Representation and Analysis of Shapes (2019/20) *
* ========================================================================= */
#include <igl/opengl/glfw/Viewer.h>
#include <iostream>
#include <ostream>
using namespace Eigen;
using namespace std;
/**
* @author Luca Castelli Aleardi (2019)
* Minimal array-based implementation of a the Half-edge data structure for representing polygonal meshes<br>
* Features:<br>
* -) References are encoded with integer values (32 bits), stores in an array of int <br>
* -) vertex coordinates are not stored <br>
* -) no implementation of dynamic updates <br>
*/
class HalfedgeDS
{
public:
/**
* Allocate the memory space for storing the data structure.
* This version of the constructor does NOT allocate face indices (for reducing memory requirements)
**/
HalfedgeDS(int n, int h)
{
nVertices = n;
nHalfedges = h;
T = new int[nHalfedges * sizeT];
for (int i = 0; i < nHalfedges * sizeT; i++)
T[i] = -1; // "-1" means that reference is NOT DEFINED (null)
incidentEdge = new int[nVertices];
}
/**
* Allocate the memory space for storing the data structure.
* This version of the constructor stores face/halfedge incidence relations
**/
HalfedgeDS(int n, int h, int f)
{
nVertices = n;
nHalfedges = h;
nFaces = f;
T = new int[nHalfedges * sizeT];
for (int i = 0; i < nHalfedges * sizeT; i++)
T[i] = -1; // "-1" means that reference is NOT DEFINED (null)
incidentEdge = new int[nVertices];
faces=new int[nFaces];
}
/**
* Set the opposite half-edge
**/
void setOpposite(int e, int eOpposite)
{
T[e * sizeT] = eOpposite;
}
/**
* Set the next half-edge: the half-edge 'eNext' following 'e' in the same face
**/
void setNext(int e, int eNext)
{
T[sizeT * e + 1] = eNext;
}
/**
* Set the (target) incident vertex of 'e'
**/
void setVertex(int e, int v)
{
T[sizeT * e + 2] = v;
}
/**
* Set the face containing the given half-edge
**/
void setFace(int e, int f)
{
T[sizeT * e + 3] = f;
}
/**
* Set the face containing the given half-edge
**/
void setPrev(int e, int ePrev)
{
T[sizeT * e + 4] = ePrev;
}
void setEdge(int v, int e)
{
incidentEdge[v] = e;
}
/**
* Set the half-edge 'e' incident to the given face 'f'
**/
void setEdgeInFace(int f, int e)
{
faces[f]=e;
}
//--- methods for accessing data and navigating between mesh elements ---
/**
* Return the following edge in ccw orientation around the same face
**/
int getNext(int e)
{
return T[sizeT * e + 1];
}
/**
* Return the opposite edge in the neighboring face (having opposite direction)
**/
int getOpposite(int e)
{
return T[e * sizeT];
}
/**
* Return the previous edge in ccw orientation around the same face
**/
int getPrev(int e)
{
return T[sizeT * e + 4];
}
/**
* Return the target vertex incident to the half-edge (its target vertex)
**/
int getTarget(int e)
{
return T[sizeT * e + 2];
}
/**
* Return the face containing the half-edge
**/
int getFace(int e)
{
return T[sizeT * e + 3];
}
/**
* Return a half edge incident to the vertex
**/
int getEdge(int v)
{
return incidentEdge[v];
}
/**
* Return a half edge incident to the face
**/
int getEdgeInFace(int f)
{
return faces[f];
}
/**
* Return the number of vertices
**/
int sizeOfVertices()
{
return nVertices;
}
/**
* Return the number of half-edges
**/
int sizeOfHalfedges()
{
return nHalfedges;
}
/**
* Return the number of faces
**/
int sizeOfFaces()
{
return nFaces;
}
/**
* Print the array T[] storing all references
**/
void print()
{
for (int i = 0; i < nHalfedges; i++)
{
cout << "he" << i << ": \t" << T[sizeT * i] << "\t" << T[sizeT * i + 1] << "\t" << T[sizeT * i + 2] << "\t" << T[sizeT * i + 3] << endl;
}
cout << "face list: " << nFaces << endl;
if(faces!=NULL) {
for (int i = 0; i < nFaces; i++)
{
cout << "f" << i << ": \t";
int e1=getEdgeInFace(i);
cout << "incident edge: e" << e1;
int e2=getNext(e1);
int e3=getNext(e2);
cout << "\t v" << getTarget(e3) << ", v" << getTarget(e1) << ", v" << getTarget(e2);
cout << endl;
}
}
}
private:
int nVertices, nHalfedges, nFaces; // number of vertices, halfedges and faces in the mesh
int *T; // a table for storing references between half-edges: each halfedge is represented with three integer references
int *incidentEdge; // for each vertex we store an incident (ingoing) halfedge
int *faces; // for each face we store an incident halfedge
const int sizeT = 5;
};