-
Notifications
You must be signed in to change notification settings - Fork 378
/
RevolveGenerator.cs
330 lines (251 loc) · 12.3 KB
/
RevolveGenerator.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace g3
{
public class Curve3Axis3RevolveGenerator : MeshGenerator
{
public Vector3d[] Curve;
public Frame3f Axis = Frame3f.Identity;
public int RevolveAxis = 1;
public bool Capped = true;
public int Slices = 16;
public bool NoSharedVertices = true;
public int startCapCenterIndex = -1;
public int endCapCenterIndex = -1;
public override void Generate()
{
int nRings = Curve.Length;
int nRingSize = (NoSharedVertices) ? Slices + 1 : Slices;
int nCapVertices = (NoSharedVertices) ? Slices + 1 : 1;
if (Capped == false)
nCapVertices = 0;
vertices = new VectorArray3d(nRingSize*nRings + 2*nCapVertices);
uv = new VectorArray2f(vertices.Count);
normals = new VectorArray3f(vertices.Count);
int nSpanTris = (nRings - 1) * (2 * Slices);
int nCapTris = (Capped) ? 2 * Slices : 0;
triangles = new IndexArray3i(nSpanTris + nCapTris);
float fDelta = (float)((Math.PI * 2.0) / Slices);
Frame3f f = Axis;
// generate tube
for (int ri = 0; ri < nRings; ++ri) {
Vector3d v_along = Curve[ri];
Vector3f v_frame = f.ToFrameP((Vector3f)v_along);
float uv_along = (float)ri / (float)(nRings - 1);
// generate vertices
int nStartR = ri * nRingSize;
for (int j = 0; j < nRingSize; ++j) {
float angle = (float)j * fDelta;
// [TODO] this is not efficient...use Matrix3f?
Vector3f v_rot = Quaternionf.AxisAngleR(Vector3f.AxisY, angle) * v_frame;
Vector3d v_new = f.FromFrameP(v_rot);
int k = nStartR + j;
vertices[k] = v_new;
float uv_around = (float)j / (float)(nRingSize);
uv[k] = new Vector2f(uv_along, uv_around);
// [TODO] proper normal
Vector3f n = (Vector3f)(v_new - f.Origin).Normalized;
normals[k] = n;
}
}
// generate triangles
int ti = 0;
for (int ri = 0; ri < nRings - 1; ++ri) {
int r0 = ri * nRingSize;
int r1 = r0 + nRingSize;
for (int k = 0; k < nRingSize - 1; ++k) {
triangles.Set(ti++, r0 + k, r0 + k + 1, r1 + k + 1, Clockwise);
triangles.Set(ti++, r0 + k, r1 + k + 1, r1 + k, Clockwise);
}
if (NoSharedVertices == false) { // close disc if we went all the way
triangles.Set(ti++, r1 - 1, r0, r1, Clockwise);
triangles.Set(ti++, r1 - 1, r1, r1 + nRingSize - 1, Clockwise);
}
}
if (Capped) {
// find avg start loop size
Vector3d vAvgStart = Vector3d.Zero, vAvgEnd = Vector3d.Zero;
for (int k = 0; k < Slices; ++k) {
vAvgStart += vertices[k];
vAvgEnd += vertices[(nRings - 1) * nRingSize + k];
}
vAvgStart /= (double)Slices;
vAvgEnd /= (double)Slices;
Frame3f fStart = f;
fStart.Origin = (Vector3f)vAvgStart;
Frame3f fEnd = f;
fEnd.Origin = (Vector3f)vAvgEnd;
// add endcap verts
int nBottomC = nRings * nRingSize;
vertices[nBottomC] = fStart.Origin;
uv[nBottomC] = new Vector2f(0.5f, 0.5f);
normals[nBottomC] = -fStart.Z;
startCapCenterIndex = nBottomC;
int nTopC = nBottomC + 1;
vertices[nTopC] = fEnd.Origin;
uv[nTopC] = new Vector2f(0.5f, 0.5f);
normals[nTopC] = fEnd.Z;
endCapCenterIndex = nTopC;
if (NoSharedVertices) {
// duplicate first loop and make a fan w/ bottom-center
int nExistingB = 0;
int nStartB = nTopC + 1;
for (int k = 0; k < Slices; ++k) {
vertices[nStartB + k] = vertices[nExistingB + k];
//uv[nStartB + k] = (Vector2f)Polygon.Vertices[k].Normalized;
float angle = (float)k * fDelta;
double cosa = Math.Cos(angle), sina = Math.Sin(angle);
uv[nStartB + k] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1 + sina));
normals[nStartB + k] = normals[nBottomC];
}
append_disc(Slices, nBottomC, nStartB, true, Clockwise, ref ti);
// duplicate second loop and make fan
int nExistingT = nRingSize * (nRings - 1);
int nStartT = nStartB + Slices;
for (int k = 0; k < Slices; ++k) {
vertices[nStartT + k] = vertices[nExistingT + k];
//uv[nStartT + k] = (Vector2f)Polygon.Vertices[k].Normalized;
float angle = (float)k * fDelta;
double cosa = Math.Cos(angle), sina = Math.Sin(angle);
uv[nStartT + k] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1 + sina));
normals[nStartT + k] = normals[nTopC];
}
append_disc(Slices, nTopC, nStartT, true, !Clockwise, ref ti);
} else {
append_disc(Slices, nBottomC, 0, true, Clockwise, ref ti);
append_disc(Slices, nTopC, nRingSize * (nRings - 1), true, !Clockwise, ref ti);
}
}
}
}
public class Curve3Curve3RevolveGenerator : MeshGenerator
{
public Vector3d[] Curve;
public Vector3d[] Axis;
public bool Capped = true;
public int Slices = 16;
public bool NoSharedVertices = true;
public int startCapCenterIndex = -1;
public int endCapCenterIndex = -1;
public override void Generate()
{
double tCurveLen = CurveUtils.ArcLength(Curve);
SampledArcLengthParam pAxis = new SampledArcLengthParam(Axis, Axis.Length);
double tAxisLen = pAxis.ArcLength;
double tScale = tAxisLen / tCurveLen;
int nRings = Curve.Length;
int nRingSize = (NoSharedVertices) ? Slices + 1 : Slices;
int nCapVertices = (NoSharedVertices) ? Slices + 1 : 1;
if (Capped == false)
nCapVertices = 0;
vertices = new VectorArray3d(nRingSize * nRings + 2 * nCapVertices);
uv = new VectorArray2f(vertices.Count);
normals = new VectorArray3f(vertices.Count);
int nSpanTris = (nRings - 1) * (2 * Slices);
int nCapTris = (Capped) ? 2 * Slices : 0;
triangles = new IndexArray3i(nSpanTris + nCapTris);
float fDelta = (float)((Math.PI * 2.0) / Slices);
double tCur = 0;
CurveSample s = pAxis.Sample(tCur);
Frame3f f0 = new Frame3f((Vector3f)s.position, (Vector3f)s.tangent, 1);
Frame3f fCur = f0;
// generate tube
for (int ri = 0; ri < nRings; ++ri) {
if ( ri > 0 ) {
tCur += (Curve[ri] - Curve[ri - 1]).Length;
s = pAxis.Sample(tCur * tScale);
fCur.Origin = (Vector3f)s.position;
fCur.AlignAxis(1, (Vector3f)s.tangent);
}
Vector3d v_along = Curve[ri];
Vector3f v_frame = fCur.ToFrameP((Vector3f)v_along);
float uv_along = (float)ri / (float)(nRings - 1);
// generate vertices
int nStartR = ri * nRingSize;
for (int j = 0; j < nRingSize; ++j) {
float angle = (float)j * fDelta;
// [TODO] this is not efficient...use Matrix3f?
Vector3f v_rot = Quaternionf.AxisAngleR(Vector3f.AxisY, angle) * v_frame;
Vector3d v_new = fCur.FromFrameP(v_rot);
int k = nStartR + j;
vertices[k] = v_new;
float uv_around = (float)j / (float)(nRingSize);
uv[k] = new Vector2f(uv_along, uv_around);
// [TODO] proper normal
Vector3f n = (Vector3f)(v_new - fCur.Origin).Normalized;
normals[k] = n;
}
}
// generate triangles
int ti = 0;
for (int ri = 0; ri < nRings - 1; ++ri) {
int r0 = ri * nRingSize;
int r1 = r0 + nRingSize;
for (int k = 0; k < nRingSize - 1; ++k) {
triangles.Set(ti++, r0 + k, r0 + k + 1, r1 + k + 1, Clockwise);
triangles.Set(ti++, r0 + k, r1 + k + 1, r1 + k, Clockwise);
}
if (NoSharedVertices == false) { // close disc if we went all the way
triangles.Set(ti++, r1 - 1, r0, r1, Clockwise);
triangles.Set(ti++, r1 - 1, r1, r1 + nRingSize - 1, Clockwise);
}
}
if (Capped) {
// find avg start loop size
Vector3d vAvgStart = Vector3d.Zero, vAvgEnd = Vector3d.Zero;
for (int k = 0; k < Slices; ++k) {
vAvgStart += vertices[k];
vAvgEnd += vertices[(nRings - 1) * nRingSize + k];
}
vAvgStart /= (double)Slices;
vAvgEnd /= (double)Slices;
Frame3f fStart = f0;
fStart.Origin = (Vector3f)vAvgStart;
Frame3f fEnd = fCur;
fEnd.Origin = (Vector3f)vAvgEnd;
// add endcap verts
int nBottomC = nRings * nRingSize;
vertices[nBottomC] = fStart.Origin;
uv[nBottomC] = new Vector2f(0.5f, 0.5f);
normals[nBottomC] = -fStart.Z;
startCapCenterIndex = nBottomC;
int nTopC = nBottomC + 1;
vertices[nTopC] = fEnd.Origin;
uv[nTopC] = new Vector2f(0.5f, 0.5f);
normals[nTopC] = fEnd.Z;
endCapCenterIndex = nTopC;
if (NoSharedVertices) {
// duplicate first loop and make a fan w/ bottom-center
int nExistingB = 0;
int nStartB = nTopC + 1;
for (int k = 0; k < Slices; ++k) {
vertices[nStartB + k] = vertices[nExistingB + k];
//uv[nStartB + k] = (Vector2f)Polygon.Vertices[k].Normalized;
float angle = (float)k * fDelta;
double cosa = Math.Cos(angle), sina = Math.Sin(angle);
uv[nStartB + k] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1 + sina));
normals[nStartB + k] = normals[nBottomC];
}
append_disc(Slices, nBottomC, nStartB, true, Clockwise, ref ti);
// duplicate second loop and make fan
int nExistingT = nRingSize * (nRings - 1);
int nStartT = nStartB + Slices;
for (int k = 0; k < Slices; ++k) {
vertices[nStartT + k] = vertices[nExistingT + k];
//uv[nStartT + k] = (Vector2f)Polygon.Vertices[k].Normalized;
float angle = (float)k * fDelta;
double cosa = Math.Cos(angle), sina = Math.Sin(angle);
uv[nStartT + k] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1 + sina));
normals[nStartT + k] = normals[nTopC];
}
append_disc(Slices, nTopC, nStartT, true, !Clockwise, ref ti);
} else {
append_disc(Slices, nBottomC, 0, true, Clockwise, ref ti);
append_disc(Slices, nTopC, nRingSize * (nRings - 1), true, !Clockwise, ref ti);
}
}
}
}
}