-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.h
610 lines (582 loc) · 21.7 KB
/
generate.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
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
/*
* Voxels is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Voxels is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Voxels; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#ifndef GENERATE_H_INCLUDED
#define GENERATE_H_INCLUDED
#include "mesh.h"
#include "image.h"
#include <utility>
#include <functional>
#include <tuple>
#include <vector>
#include <cwchar>
#include <string>
#include <cmath>
#include <deque>
#include <unordered_map>
#include <array>
#include <list>
using namespace std;
inline Mesh reverse(const Mesh & meshIn)
{
vector<Triangle> triangles = meshIn.triangles;
for(Triangle & tri : triangles)
{
tri = reverse(tri);
}
return Mesh(std::move(triangles), meshIn.image);
}
inline Mesh reverse(Mesh && meshIn)
{
vector<Triangle> triangles = std::move(meshIn.triangles);
for(Triangle & tri : triangles)
{
tri = reverse(tri);
}
return Mesh(std::move(triangles), std::move(meshIn.image));
}
template <typename Fn>
inline Mesh shadeMesh(const Mesh &meshIn, Fn shadeFn)
{
vector<Triangle> triangles;
triangles.reserve(meshIn.triangles.size());
for(Triangle tri : meshIn.triangles)
{
if(tri.n1 == VectorF(0) || tri.n2 == VectorF(0) || tri.n3 == VectorF(0))
{
triangles.push_back(tri);
continue;
}
tri.c1 = shadeFn(tri.c1, tri.n1, tri.p1);
tri.c2 = shadeFn(tri.c2, tri.n2, tri.p2);
tri.c3 = shadeFn(tri.c3, tri.n3, tri.p3);
triangles.push_back(tri);
}
return Mesh(std::move(triangles), meshIn.image);
}
template <typename Fn>
inline Mesh shadeMesh(Mesh &&meshIn, Fn shadeFn)
{
vector<Triangle> triangles = std::move(meshIn.triangles);
for(Triangle &tri : triangles)
{
if(tri.n1 == VectorF(0) || tri.n2 == VectorF(0) || tri.n3 == VectorF(0))
{
continue;
}
tri.c1 = shadeFn(tri.c1, tri.n1, tri.p1);
tri.c2 = shadeFn(tri.c2, tri.n2, tri.p2);
tri.c3 = shadeFn(tri.c3, tri.n3, tri.p3);
}
return Mesh(std::move(triangles), std::move(meshIn.image));
}
struct CutMesh
{
Mesh front, coplanar, back;
CutMesh(Mesh front, Mesh coplanar, Mesh back)
: front(std::move(front)), coplanar(std::move(coplanar)), back(std::move(back))
{
}
CutMesh()
{
}
};
inline CutMesh cut(const Mesh &mesh, VectorF planeNormal, float planeD)
{
Mesh front, coplanar, back;
front.reserve(mesh.triangleCount() * 2);
coplanar.reserve(mesh.triangleCount() * 2);
back.reserve(mesh.triangleCount() * 2);
front.image = mesh.image;
coplanar.image = mesh.image;
back.image = mesh.image;
for(Triangle tri : mesh.triangles)
{
CutTriangle ct = cut(tri, planeNormal, planeD);
for(size_t i = 0; i < ct.frontTriangleCount; i++)
front.append(ct.frontTriangles[i]);
for(size_t i = 0; i < ct.coplanarTriangleCount; i++)
coplanar.append(ct.coplanarTriangles[i]);
for(size_t i = 0; i < ct.backTriangleCount; i++)
back.append(ct.backTriangles[i]);
}
return CutMesh(std::move(front), std::move(coplanar), std::move(back));
}
inline Mesh cutAndGetFront(Mesh mesh, VectorF planeNormal, float planeD)
{
static thread_local vector<Triangle> triangles;
triangles = std::move(mesh.triangles);
mesh.triangles.clear();
mesh.reserve(triangles.size());
for(Triangle tri : triangles)
{
CutTriangle ct = cut(tri, planeNormal, planeD);
for(size_t i = 0; i < ct.frontTriangleCount; i++)
mesh.append(ct.frontTriangles[i]);
for(size_t i = 0; i < ct.coplanarTriangleCount; i++)
mesh.append(ct.coplanarTriangles[i]);
}
return std::move(mesh);
}
inline Mesh cutAndGetBack(Mesh mesh, VectorF planeNormal, float planeD)
{
static thread_local vector<Triangle> triangles;
triangles = std::move(mesh.triangles);
mesh.triangles.clear();
mesh.reserve(triangles.size());
for(Triangle tri : triangles)
{
CutTriangle ct = cut(tri, planeNormal, planeD);
for(size_t i = 0; i < ct.backTriangleCount; i++)
mesh.append(ct.backTriangles[i]);
for(size_t i = 0; i < ct.coplanarTriangleCount; i++)
mesh.append(ct.coplanarTriangles[i]);
}
return std::move(mesh);
}
constexpr const float simplifyDefaultEps = 1e-6;
inline vector<Triangle> simplify(vector<Triangle> meshTriangles, float faceNormalEps = simplifyDefaultEps, float distanceEps = simplifyDefaultEps, float vertexNormalEps = simplifyDefaultEps, float vertexTextureEps = simplifyDefaultEps, float vertexColorEps = simplifyDefaultEps)
{
vector<Vertex> vertexList;
unordered_map<Vertex, size_t> vertexMap;
constexpr size_t verticesPerTriangle = 3;
typedef tuple<array<size_t, verticesPerTriangle>, VectorF> SimpTriangle;
typedef list<SimpTriangle> STList;
vector<vector<STList::iterator>> trianglesMap;
STList triangles;
deque<STList::iterator> changedQueue;
for(Triangle tri : meshTriangles)
{
STList::iterator triangleIter = triangles.insert(triangles.end(), SimpTriangle());
changedQueue.push_back(triangleIter);
SimpTriangle &triangle = triangles.back();
array<Vertex, verticesPerTriangle> vertices = {tri.v1(), tri.v2(), tri.v3()};
for(size_t i = 0; i < verticesPerTriangle; i++)
{
Vertex vertex = vertices[i];
size_t vertexIndex;
{
auto iter = vertexMap.find(vertex);
if(iter == vertexMap.end())
{
vertexIndex = vertexList.size();
vertexMap[vertex] = vertexList.size();
vertexList.push_back(vertex);
trianglesMap.push_back(vector<STList::iterator>{triangleIter});
}
else
{
vertexIndex = std::get<1>(*iter);
trianglesMap[vertexIndex].push_back(triangleIter);
}
}
std::get<0>(triangle)[i] = vertexIndex;
}
std::get<1>(triangle) = tri.normal();
}
while(!changedQueue.empty())
{
STList::iterator triangleIter = changedQueue.front();
changedQueue.pop_front();
if(std::get<1>(*triangleIter) == VectorF(0))
continue;
bool done = false;
for(size_t i = 0; i < verticesPerTriangle; i++)
{
for(STList::iterator secondTriangle : trianglesMap[std::get<0>(*triangleIter)[i]])
{
if(secondTriangle == triangleIter)
continue;
if(std::get<1>(*secondTriangle) == VectorF(0))
continue;
if(absSquared(std::get<1>(*triangleIter) - std::get<1>(*secondTriangle)) > faceNormalEps * faceNormalEps)
continue;
array<int, verticesPerTriangle> matchIndices;
array<bool, verticesPerTriangle> used;
for(bool &v : used)
v = false;
size_t matchCount = 0;
for(size_t j = 0; j < verticesPerTriangle; j++)
{
matchIndices[j] = -1;
for(size_t k = 0; k < verticesPerTriangle; k++)
{
if(std::get<0>(*triangleIter)[j] == std::get<0>(*secondTriangle)[k])
{
matchIndices[j] = k;
used[k] = true;
matchCount++;
break;
}
}
}
if(matchCount >= 3)
{
std::get<1>(*triangleIter) = VectorF(0); // delete duplicates
continue;
}
if(matchCount < 2)
continue;
size_t firstTriangleNonTouchingIndex = 0, secondTriangleNonTouchingIndex = 0;
for(size_t j = 0; j < verticesPerTriangle; j++)
{
if(matchIndices[j] == -1)
{
firstTriangleNonTouchingIndex = j;
break;
}
}
for(size_t j = 0; j < verticesPerTriangle; j++)
{
if(!used[j])
{
secondTriangleNonTouchingIndex = j;
break;
}
}
array<size_t, verticesPerTriangle> tri1, tri2;
for(size_t j = 0, k = firstTriangleNonTouchingIndex; j < verticesPerTriangle; j++, k = (k >= verticesPerTriangle - 1 ? k + 1 - verticesPerTriangle : k + 1))
{
tri1[j] = std::get<0>(*triangleIter)[k];
}
for(size_t j = 0, k = secondTriangleNonTouchingIndex; j < verticesPerTriangle; j++, k = (k >= verticesPerTriangle - 1 ? k + 1 - verticesPerTriangle : k + 1))
{
tri2[j] = std::get<0>(*secondTriangle)[k];
}
Vertex lineStartVertex = vertexList[tri1[0]], lineEndVertex = vertexList[tri2[0]];
VectorF deltaPosition = lineEndVertex.p - lineStartVertex.p;
float planeD = -dot(deltaPosition, lineStartVertex.p);
if(absSquared(deltaPosition) < distanceEps * distanceEps)
continue;
size_t removeVertex = 0;
for(size_t j = 1; j < verticesPerTriangle; j++)
{
Vertex middleVertex = vertexList[tri1[j]];
float t = (dot(middleVertex.p, deltaPosition) + planeD) / absSquared(deltaPosition);
Vertex calculatedMiddleVertex = interpolate(t, lineStartVertex, lineEndVertex);
if(absSquared(calculatedMiddleVertex.p - middleVertex.p) > distanceEps * distanceEps)
continue;
if(absSquared(calculatedMiddleVertex.n - middleVertex.n) > vertexNormalEps * vertexNormalEps)
continue;
if(abs(calculatedMiddleVertex.c.r - middleVertex.c.r) > vertexColorEps)
continue;
if(abs(calculatedMiddleVertex.c.g - middleVertex.c.g) > vertexColorEps)
continue;
if(abs(calculatedMiddleVertex.c.b - middleVertex.c.b) > vertexColorEps)
continue;
if(abs(calculatedMiddleVertex.c.a - middleVertex.c.a) > vertexColorEps)
continue;
if(abs(calculatedMiddleVertex.t.u - middleVertex.t.u) > vertexTextureEps)
continue;
if(abs(calculatedMiddleVertex.t.v - middleVertex.t.v) > vertexTextureEps)
continue;
removeVertex = j;
break;
}
if(removeVertex == 0)
continue;
std::get<1>(*secondTriangle) = VectorF(0); // remove second triangle
if(removeVertex == 1)
{
std::get<0>(*triangleIter)[0] = tri1[0];
std::get<0>(*triangleIter)[1] = tri2[0];
std::get<0>(*triangleIter)[2] = tri1[2];
}
else
{
std::get<0>(*triangleIter)[0] = tri1[0];
std::get<0>(*triangleIter)[1] = tri1[1];
std::get<0>(*triangleIter)[2] = tri2[0];
}
changedQueue.push_back(triangleIter);
done = true;
break;
}
if(done)
break;
}
}
meshTriangles.clear();
for(const SimpTriangle &triangle : triangles)
{
if(std::get<1>(triangle) == VectorF(0))
continue;
meshTriangles.push_back(Triangle(vertexList[std::get<0>(triangle)[0]], vertexList[std::get<0>(triangle)[1]], vertexList[std::get<0>(triangle)[2]]));
}
return std::move(meshTriangles);
}
inline Mesh simplify(Mesh mesh, float faceNormalEps = simplifyDefaultEps, float distanceEps = simplifyDefaultEps, float vertexNormalEps = simplifyDefaultEps, float vertexTextureEps = simplifyDefaultEps, float vertexColorEps = simplifyDefaultEps)
{
mesh.triangles = simplify(std::move(mesh.triangles), faceNormalEps, distanceEps, vertexNormalEps, vertexTextureEps, vertexColorEps);
return std::move(mesh);
}
namespace Generate
{
inline Mesh quadrilateral(TextureDescriptor texture, VectorF p1, ColorF c1, VectorF p2, ColorF c2, VectorF p3, ColorF c3, VectorF p4, ColorF c4)
{
const TextureCoord t1 = TextureCoord(texture.minU, texture.minV);
const TextureCoord t2 = TextureCoord(texture.maxU, texture.minV);
const TextureCoord t3 = TextureCoord(texture.maxU, texture.maxV);
const TextureCoord t4 = TextureCoord(texture.minU, texture.maxV);
return Mesh(vector<Triangle>{Triangle(p1, c1, t1, p2, c2, t2, p3, c3, t3), Triangle(p3, c3, t3, p4, c4, t4, p1, c1, t1)}, texture.image);
}
inline Mesh convexPolygon(shared_ptr<Texture> texture, const vector<tuple<VectorF, ColorF, TextureCoord, VectorF>> &vertices)
{
if(vertices.size() < 3)
return Mesh();
vector<Triangle> triangles;
triangles.reserve(vertices.size() - 2);
tuple<VectorF, ColorF, TextureCoord, VectorF> v1 = vertices[0];
for(size_t i = 1, j = 2; j < vertices.size(); i++, j++)
{
tuple<VectorF, ColorF, TextureCoord, VectorF> v2 = vertices[i], v3 = vertices[j];
triangles.push_back(Triangle(get<0>(v1), get<1>(v1), get<2>(v1), get<3>(v1),
get<0>(v2), get<1>(v2), get<2>(v2), get<3>(v2),
get<0>(v3), get<1>(v3), get<2>(v3), get<3>(v3)));
}
return Mesh(std::move(triangles), texture);
}
inline Mesh convexPolygon(shared_ptr<Texture> texture, const vector<tuple<VectorF, ColorF, TextureCoord>> &vertices)
{
if(vertices.size() < 3)
return Mesh();
vector<Triangle> triangles;
triangles.reserve(vertices.size() - 2);
tuple<VectorF, ColorF, TextureCoord> v1 = vertices[0];
for(size_t i = 1, j = 2; j < vertices.size(); i++, j++)
{
tuple<VectorF, ColorF, TextureCoord> v2 = vertices[i], v3 = vertices[j];
triangles.push_back(Triangle(get<0>(v1), get<1>(v1), get<2>(v1),
get<0>(v2), get<1>(v2), get<2>(v2),
get<0>(v3), get<1>(v3), get<2>(v3)));
}
return Mesh(std::move(triangles), texture);
}
inline Mesh convexPolygon(shared_ptr<Texture> texture, const vector<tuple<VectorF, TextureCoord>> &vertices)
{
if(vertices.size() < 3)
return Mesh();
vector<Triangle> triangles;
triangles.reserve(vertices.size() - 2);
tuple<VectorF, TextureCoord> v1 = vertices[0];
for(size_t i = 1, j = 2; j < vertices.size(); i++, j++)
{
tuple<VectorF, TextureCoord> v2 = vertices[i], v3 = vertices[j];
triangles.push_back(Triangle(get<0>(v1), get<1>(v1),
get<0>(v2), get<1>(v2),
get<0>(v3), get<1>(v3)));
}
return Mesh(std::move(triangles), texture);
}
inline Mesh convexPolygon(shared_ptr<Texture> texture, const vector<Vertex> &vertices)
{
if(vertices.size() < 3)
return Mesh();
vector<Triangle> triangles;
triangles.reserve(vertices.size() - 2);
Vertex v1 = vertices[0];
for(size_t i = 1, j = 2; j < vertices.size(); i++, j++)
{
Vertex v2 = vertices[i], v3 = vertices[j];
triangles.push_back(Triangle(v1, v2, v3));
}
return Mesh(std::move(triangles), texture);
}
/// make a box from <0, 0, 0> to <1, 1, 1>
inline Mesh unitBox(TextureDescriptor nx, TextureDescriptor px, TextureDescriptor ny, TextureDescriptor py, TextureDescriptor nz, TextureDescriptor pz)
{
const VectorF p0 = VectorF(0, 0, 0);
const VectorF p1 = VectorF(1, 0, 0);
const VectorF p2 = VectorF(0, 1, 0);
const VectorF p3 = VectorF(1, 1, 0);
const VectorF p4 = VectorF(0, 0, 1);
const VectorF p5 = VectorF(1, 0, 1);
const VectorF p6 = VectorF(0, 1, 1);
const VectorF p7 = VectorF(1, 1, 1);
Mesh retval;
const ColorF c = RGBAF(1, 1, 1, 1);
if(nx)
{
retval.append(quadrilateral(nx,
p0, c,
p4, c,
p6, c,
p2, c
));
}
if(px)
{
retval.append(quadrilateral(px,
p5, c,
p1, c,
p3, c,
p7, c
));
}
if(ny)
{
retval.append(quadrilateral(ny,
p0, c,
p1, c,
p5, c,
p4, c
));
}
if(py)
{
retval.append(quadrilateral(py,
p6, c,
p7, c,
p3, c,
p2, c
));
}
if(nz)
{
retval.append(quadrilateral(nz,
p1, c,
p0, c,
p2, c,
p3, c
));
}
if(pz)
{
retval.append(quadrilateral(pz,
p4, c,
p5, c,
p7, c,
p6, c
));
}
return std::move(retval);
}
struct TextProperties final
{
float tabWidth = 8;
};
class Text final
{
Text() = delete;
Text(const Text &) = delete;
void operator =(const Text &) = delete;
static void renderChar(Mesh &dest, float x, float y, unsigned ch, ColorF c, const std::shared_ptr<Texture> &fontTexture)
{
TextureDescriptor td = getTextFontCharacterTextureDescriptor(ch, fontTexture);
dest.append(quadrilateral(td,
VectorF(0 + x, 0 + y, 0), c,
VectorF(1 + x, 0 + y, 0), c,
VectorF(1 + x, 1 + y, 0), c,
VectorF(0 + x, 1 + y, 0), c
));
}
static void renderEngine(Mesh *dest, float &x, float &y, float &w, float &h, std::wstring str, ColorF c, const TextProperties &tp, const std::shared_ptr<Texture> &fontTexture = nullptr)
{
float wholeHeight = 0;
if(dest)
renderEngine(nullptr, x, y, w, wholeHeight, str, c, tp);
x = 0;
y = 0;
w = 0;
h = 0;
for(auto ch : str)
{
switch(ch)
{
case '\r':
x = 0;
break;
case '\n':
x = 0;
y += 1;
break;
case '\t':
{
float tabWidth = tp.tabWidth;
if(!std::isfinite(tabWidth) || tabWidth < 1e-4)
tabWidth = 1;
if(tabWidth > 1e6)
tabWidth = 1e6;
x /= tabWidth;
x += 1e-4;
x = std::floor(x);
x += 1;
x *= tabWidth;
break;
}
case '\b':
x -= 1;
if(x < 0)
x = 0;
break;
case '\0':
case ' ':
x += 1;
break;
default:
if(dest)
renderChar(*dest, x, wholeHeight - y - 1, ch, c, fontTexture);
x += 1;
break;
}
if(y >= h)
h = y + 1;
if(x > w)
w = x;
}
y = h - y - 1;
}
public:
static float width(std::wstring str, const TextProperties &tp = TextProperties())
{
float x, y, w, h;
renderEngine(nullptr, x, y, w, h, str, ColorF(), tp);
return w;
}
static float height(std::wstring str, const TextProperties &tp = TextProperties())
{
float x, y, w, h;
renderEngine(nullptr, x, y, w, h, str, ColorF(), tp);
return h;
}
static float x(std::wstring str, const TextProperties &tp = TextProperties())
{
float x, y, w, h;
renderEngine(nullptr, x, y, w, h, str, ColorF(), tp);
return x;
}
static float y(std::wstring str, const TextProperties &tp = TextProperties())
{
float x, y, w, h;
renderEngine(nullptr, x, y, w, h, str, ColorF(), tp);
return y;
}
static Mesh &mesh(Mesh &dest, std::wstring str, std::shared_ptr<Texture> fontTexture = nullptr, ColorF c = GrayscaleF(1), const TextProperties &tp = TextProperties())
{
float x, y, w, h;
renderEngine(&dest, x, y, w, h, str, c, tp, fontTexture);
return dest;
}
static Mesh mesh(std::wstring str, std::shared_ptr<Texture> fontTexture = nullptr, ColorF c = GrayscaleF(1), const TextProperties &tp = TextProperties())
{
Mesh dest;
mesh(dest, str, fontTexture, c, tp);
return std::move(dest);
}
};
}
#endif // GENERATE_H_INCLUDED