-
Notifications
You must be signed in to change notification settings - Fork 100
/
tristrip.cpp
682 lines (614 loc) · 15.3 KB
/
tristrip.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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "rwbase.h"
#include "rwerror.h"
#include "rwplg.h"
#include "rwpipeline.h"
#include "rwobjects.h"
#include "rwengine.h"
#define PLUGIN_ID 2
namespace rw {
struct GraphEdge
{
int32 node; /* index of the connected node */
uint32 isConnected : 1; /* is connected to other node */
uint32 otherEdge : 2; /* edge number on connected node */
uint32 isStrip : 1; /* is strip edge */
};
struct StripNode
{
uint16 v[3]; /* vertex indices */
uint8 parent : 2; /* tunnel parent node (edge index) */
uint8 visited : 1; /* visited in breadth first search */
uint8 stripVisited : 1; /* strip starting at this node was visited during search */
uint8 isEnd : 1; /* is in end list */
GraphEdge e[3];
int32 stripId; /* index of start node */
LLLink inlist;
};
struct StripMesh
{
int32 numNodes;
StripNode *nodes;
LinkList loneNodes; /* nodes not connected to any others */
LinkList endNodes; /* strip start/end nodes */
};
//#define trace(...) printf(__VA_ARGS__)
#define trace(...)
static void
printNode(StripMesh *sm, StripNode *n)
{
trace("%3ld: %3d %3d.%d %3d.%d %3d.%d || %3d %3d %3d\n",
n - sm->nodes,
n->stripId,
n->e[0].node,
n->e[0].isStrip,
n->e[1].node,
n->e[1].isStrip,
n->e[2].node,
n->e[2].isStrip,
n->v[0],
n->v[1],
n->v[2]);
}
static void
printLone(StripMesh *sm)
{
FORLIST(lnk, sm->loneNodes)
printNode(sm, LLLinkGetData(lnk, StripNode, inlist));
}
static void
printEnds(StripMesh *sm)
{
FORLIST(lnk, sm->endNodes)
printNode(sm, LLLinkGetData(lnk, StripNode, inlist));
}
static void
printSmesh(StripMesh *sm)
{
for(int32 i = 0; i < sm->numNodes; i++)
printNode(sm, &sm->nodes[i]);
}
static void
collectFaces(Geometry *geo, StripMesh *sm, uint16 m)
{
StripNode *n;
Triangle *t;
sm->numNodes = 0;
for(int32 i = 0; i < geo->numTriangles; i++){
t = &geo->triangles[i];
if(t->matId == m){
n = &sm->nodes[sm->numNodes++];
n->v[0] = t->v[0];
n->v[1] = t->v[1];
n->v[2] = t->v[2];
assert(t->v[0] < geo->numVertices);
assert(t->v[1] < geo->numVertices);
assert(t->v[2] < geo->numVertices);
n->e[0].node = 0;
n->e[1].node = 0;
n->e[2].node = 0;
n->e[0].isConnected = 0;
n->e[1].isConnected = 0;
n->e[2].isConnected = 0;
n->e[0].isStrip = 0;
n->e[1].isStrip = 0;
n->e[2].isStrip = 0;
n->parent = 0;
n->visited = 0;
n->stripVisited = 0;
n->isEnd = 0;
n->stripId = -1;
n->inlist.init();
}
}
}
/* Find Triangle that has edge e that is not connected yet. */
static GraphEdge
findEdge(StripMesh *sm, int32 e[2])
{
StripNode *n;
GraphEdge ge = { 0, 0, 0, 0 };
for(int32 i = 0; i < sm->numNodes; i++){
n = &sm->nodes[i];
for(int32 j = 0; j < 3; j++){
if(n->e[j].isConnected)
continue;
if(e[0] == n->v[j] &&
e[1] == n->v[(j+1) % 3]){
ge.node = i;
// signal success
ge.isConnected = 1;
ge.otherEdge = j;
return ge;
}
}
}
return ge;
}
/* Connect nodes sharing an edge, preserving winding */
static void
connectNodesPreserve(StripMesh *sm)
{
StripNode *n, *nn;
int32 e[2];
GraphEdge ge;
for(int32 i = 0; i < sm->numNodes; i++){
n = &sm->nodes[i];
for(int32 j = 0; j < 3; j++){
if(n->e[j].isConnected)
continue;
/* flip edge and search for node */
e[1] = n->v[j];
e[0] = n->v[(j+1) % 3];
ge = findEdge(sm, e);
if(ge.isConnected){
/* found node, now connect */
n->e[j].node = ge.node;
n->e[j].isConnected = 1;
n->e[j].otherEdge = ge.otherEdge;
n->e[j].isStrip = 0;
nn = &sm->nodes[ge.node];
nn->e[ge.otherEdge].node = i;
nn->e[ge.otherEdge].isConnected = 1;
nn->e[ge.otherEdge].otherEdge = j;
nn->e[ge.otherEdge].isStrip = 0;
}
}
}
}
static int32
numConnections(StripNode *n)
{
return n->e[0].isConnected +
n->e[1].isConnected +
n->e[2].isConnected;
}
static int32
numStripEdges(StripNode *n)
{
return n->e[0].isStrip +
n->e[1].isStrip +
n->e[2].isStrip;
}
#define IsEnd(n) (numConnections(n) > 0 && numStripEdges(n) < 2)
/* Complement the strip-ness of an edge */
static void
complementEdge(StripMesh *sm, GraphEdge *e)
{
e->isStrip = !e->isStrip;
e = &sm->nodes[e->node].e[e->otherEdge];
e->isStrip = !e->isStrip;
}
/* While possible extend a strip from a starting node until
* we find a node already in a strip. N.B. this function
* makes no attempts to connect to an already existing strip.
* It also doesn't try to alternate between left and right. */
static void
extendStrip(StripMesh *sm, StripNode *start)
{
StripNode *n, *nn;
n = start;
if(numConnections(n) == 0){
sm->loneNodes.append(&n->inlist);
return;
}
sm->endNodes.append(&n->inlist);
n->isEnd = 1;
loop:
/* Find the next node to connect to on any of the three edges */
for(int32 i = 0; i < 3; i++){
if(!n->e[i].isConnected)
continue;
nn = &sm->nodes[n->e[i].node];
if(nn->stripId >= 0)
continue;
/* found one */
nn->stripId = n->stripId;
/* We know it's not a strip edge yet,
* so complementing it will make it one. */
complementEdge(sm, &n->e[i]);
n = nn;
goto loop;
}
if(n != start){
sm->endNodes.append(&n->inlist);
n->isEnd = 1;
}
}
static void
buildStrips(StripMesh *sm)
{
StripNode *n;
for(int32 i = 0; i < sm->numNodes; i++){
n = &sm->nodes[i];
if(n->stripId >= 0)
continue;
n->stripId = i;
extendStrip(sm, n);
}
}
static StripNode*
findTunnel(StripMesh *sm, StripNode *n)
{
LinkList searchNodes;
StripNode *nn;
int edgetype;
int isEnd;
searchNodes.init();
edgetype = 0;
for(;;){
for(int32 i = 0; i < 3; i++){
/* Find a node connected by the right edgetype */
if(!n->e[i].isConnected ||
n->e[i].isStrip != edgetype)
continue;
nn = &sm->nodes[n->e[i].node];
/* If the node has been visited already,
* there's a shorter path. */
if(nn->visited)
continue;
/* Don't allow non-strip edge between nodes of the same
* strip to prevent loops.
* Actually these edges are allowed under certain
* circumstances, but they require complex checks. */
if(edgetype == 0 &&
n->stripId == nn->stripId)
continue;
isEnd = IsEnd(nn);
/* Can't add end nodes to two lists, so skip. */
if(isEnd && edgetype == 1)
continue;
nn->parent = n->e[i].otherEdge;
nn->visited = 1;
sm->nodes[nn->stripId].stripVisited = 1;
/* Search complete. */
if(isEnd && edgetype == 0)
return nn;
/* Found a valid node. */
searchNodes.append(&nn->inlist);
}
if(searchNodes.isEmpty())
return nil;
n = LLLinkGetData(searchNodes.link.next, StripNode, inlist);
n->inlist.remove();
edgetype = !edgetype;
}
}
static void
resetGraph(StripMesh *sm)
{
StripNode *n;
for(int32 i = 0; i < sm->numNodes; i++){
n = &sm->nodes[i];
n->visited = 0;
n->stripVisited = 0;
}
}
static StripNode*
walkStrip(StripMesh *sm, StripNode *start)
{
StripNode *n, *nn;
int32 last;
//trace("stripend: ");
//printNode(sm, start);
n = start;
last = -1;
for(;;n = nn){
n->visited = 0;
n->stripVisited = 0;
if(n->isEnd)
n->inlist.remove();
n->isEnd = 0;
if(IsEnd(n) && n != start)
return n;
/* find next node */
nn = nil;
for(int32 i = 0; i < 3; i++){
if(!n->e[i].isStrip || i == last)
continue;
nn = &sm->nodes[n->e[i].node];
last = n->e[i].otherEdge;
nn->stripId = n->stripId;
break;
}
//trace(" next: ");
//printNode(sm, nn);
if(nn == nil)
return nil;
}
}
static void
applyTunnel(StripMesh *sm, StripNode *end, StripNode *start)
{
LinkList tmplist;
StripNode *n, *nn;
for(n = end; n != start; n = &sm->nodes[n->e[n->parent].node]){
//trace(" ");
//printNode(sm, n);
complementEdge(sm, &n->e[n->parent]);
}
//trace(" ");
//printNode(sm, start);
//printSmesh(sm);
//trace("-------\n");
tmplist.init();
while(!sm->endNodes.isEmpty()){
n = LLLinkGetData(sm->endNodes.link.next, StripNode, inlist);
/* take out of end list */
n->inlist.remove();
n->isEnd = 0;
/* no longer an end node */
if(!IsEnd(n))
continue;
// TODO: only walk strip if it was touched
/* set new id, walk strip and find other end */
n->stripId = n - sm->nodes;
nn = walkStrip(sm, n);
tmplist.append(&n->inlist);
n->isEnd = 1;
if(nn && n != nn){
tmplist.append(&nn->inlist);
nn->isEnd = 1;
}
}
/* Move new end nodes to the real list. */
sm->endNodes = tmplist;
sm->endNodes.link.next->prev = &sm->endNodes.link;
sm->endNodes.link.prev->next = &sm->endNodes.link;
}
static void
tunnel(StripMesh *sm)
{
StripNode *n, *nn;
again:
FORLIST(lnk, sm->endNodes){
n = LLLinkGetData(lnk, StripNode, inlist);
// trace("searching %p %d\n", n, numStripEdges(n));
nn = findTunnel(sm, n);
// trace(" %p %p\n", n, nn);
if(nn){
applyTunnel(sm, nn, n);
resetGraph(sm);
/* applyTunnel changes sm->endNodes, so we have to
* jump out of the loop. */
goto again;
}
resetGraph(sm);
}
trace("tunneling done!\n");
}
/* Get next edge in strip.
* Last is the edge index whence we came lest we go back. */
static int
getNextEdge(StripNode *n, int32 last)
{
int32 i;
for(i = 0; i < 3; i++)
if(n->e[i].isStrip && i != last)
return i;
return -1;
}
#define NEXT(x) (((x)+1) % 3)
#define PREV(x) (((x)+2) % 3)
#define RIGHT(x) NEXT(x)
#define LEFT(x) PREV(x)
/* Generate mesh indices for all strips in a StripMesh */
static void
makeMesh(StripMesh *sm, Mesh *m)
{
int32 i, j;
int32 rightturn, lastrightturn;
int32 seam;
int32 even;
StripNode *n;
/* three indices + two for stitch per triangle must be enough */
m->indices = rwNewT(uint16, sm->numNodes*5, MEMDUR_FUNCTION | ID_GEOMETRY);
memset(m->indices, 0xFF, sm->numNodes*5*sizeof(uint16));
even = 1;
FORLIST(lnk, sm->endNodes){
n = LLLinkGetData(lnk, StripNode, inlist);
/* only interested in start nodes, not the ends */
if(n->stripId != (n - sm->nodes))
continue;
/* i is the edge we enter this triangle from.
* j is the edge we exit. */
j = getNextEdge(n, -1);
/* starting triangle must have connection */
if(j < 0)
continue;
/* Space to stitch together strips */
seam = m->numIndices;
if(seam)
m->numIndices += 2;
/* Start ccw for even tris */
if(even){
/* Start with a right turn */
i = LEFT(j);
m->indices[m->numIndices++] = n->v[i];
m->indices[m->numIndices++] = n->v[NEXT(i)];
}else{
/* Start with a left turn */
i = RIGHT(j);
m->indices[m->numIndices++] = n->v[NEXT(i)];
m->indices[m->numIndices++] = n->v[i];
}
trace("\nstart %d %d\n", numStripEdges(n), m->numIndices-2);
lastrightturn = -1;
while(j >= 0){
rightturn = RIGHT(i) == j;
if(rightturn == lastrightturn){
// insert a swap if we're not alternating
m->indices[m->numIndices] = m->indices[m->numIndices-2];
trace("SWAP\n");
m->numIndices++;
even = !even;
}
trace("%d:%d%c %d %d %d\n", n-sm->nodes, m->numIndices, even ? ' ' : '.', n->v[0], n->v[1], n->v[2]);
lastrightturn = rightturn;
if(rightturn)
m->indices[m->numIndices++] = n->v[NEXT(j)];
else
m->indices[m->numIndices++] = n->v[j];
even = !even;
/* go to next triangle */
i = n->e[j].otherEdge;
n = &sm->nodes[n->e[j].node];
j = getNextEdge(n, i);
}
/* finish strip */
trace("%d:%d%c %d %d %d\nend\n", n-sm->nodes, m->numIndices, even ? ' ' : '.', n->v[0], n->v[1], n->v[2]);
m->indices[m->numIndices++] = n->v[LEFT(i)];
even = !even;
if(seam){
m->indices[seam] = m->indices[seam-1];
m->indices[seam+1] = m->indices[seam+2];
trace("STITCH %d: %d %d\n", seam, m->indices[seam], m->indices[seam+1]);
}
}
/* Add all unconnected and lonely triangles */
FORLIST(lnk, sm->endNodes){
n = LLLinkGetData(lnk, StripNode, inlist);
if(numStripEdges(n) != 0)
continue;
if(m->numIndices != 0){
m->indices[m->numIndices] = m->indices[m->numIndices-1];
m->numIndices++;
m->indices[m->numIndices++] = n->v[!even];
}
m->indices[m->numIndices++] = n->v[!even];
m->indices[m->numIndices++] = n->v[even];
m->indices[m->numIndices++] = n->v[2];
even = !even;
}
FORLIST(lnk, sm->loneNodes){
n = LLLinkGetData(lnk, StripNode, inlist);
if(m->numIndices != 0){
m->indices[m->numIndices] = m->indices[m->numIndices-1];
m->numIndices++;
m->indices[m->numIndices++] = n->v[!even];
}
m->indices[m->numIndices++] = n->v[!even];
m->indices[m->numIndices++] = n->v[even];
m->indices[m->numIndices++] = n->v[2];
even = !even;
}
}
static void verifyMesh(Geometry *geo);
/*
* For each material:
* 1. build dual graph (collectFaces, connectNodes)
* 2. make some simple strip (buildStrips)
* 3. apply tunnel operator (tunnel)
*/
void
Geometry::buildTristrips(void)
{
int32 i;
uint16 *indices;
MeshHeader *header;
Mesh *ms, *md;
StripMesh smesh;
// trace("%ld\n", sizeof(StripNode));
this->allocateMeshes(matList.numMaterials, 0, 1);
smesh.nodes = rwNewT(StripNode, this->numTriangles, MEMDUR_FUNCTION | ID_GEOMETRY);
ms = this->meshHeader->getMeshes();
for(int32 i = 0; i < this->matList.numMaterials; i++){
smesh.loneNodes.init();
smesh.endNodes.init();
collectFaces(this, &smesh, i);
connectNodesPreserve(&smesh);
buildStrips(&smesh);
printSmesh(&smesh);
//trace("-------\n");
//printLone(&smesh);
//trace("-------\n");
//printEnds(&smesh);
//trace("-------\n");
// TODO: make this work
// tunnel(&smesh);
//trace("-------\n");
//printEnds(&smesh);
ms[i].material = this->matList.materials[i];
makeMesh(&smesh, &ms[i]);
this->meshHeader->totalIndices += ms[i].numIndices;
}
rwFree(smesh.nodes);
/* Now re-allocate and copy data */
header = this->meshHeader;
this->meshHeader = nil;
this->allocateMeshes(header->numMeshes, header->totalIndices, 0);
this->meshHeader->flags = MeshHeader::TRISTRIP;
md = this->meshHeader->getMeshes();
indices = md->indices;
for(i = 0; i < header->numMeshes; i++){
md[i].material = ms[i].material;
md[i].numIndices = ms[i].numIndices;
md[i].indices = indices;
indices += md[i].numIndices;
memcpy(md[i].indices, ms[i].indices, md[i].numIndices*sizeof(uint16));
rwFree(ms[i].indices);
}
rwFree(header);
verifyMesh(this);
}
/* Check that tristripped mesh and geometry triangles are actually the same. */
static void
verifyMesh(Geometry *geo)
{
int32 i, k;
uint32 j;
int32 x;
int32 a, b, c, m;
Mesh *mesh;
Triangle *t;
uint8 *seen;
seen = rwNewT(uint8, geo->numTriangles, MEMDUR_FUNCTION | ID_GEOMETRY);
memset(seen, 0, geo->numTriangles);
mesh = geo->meshHeader->getMeshes();
for(i = 0; i < geo->meshHeader->numMeshes; i++){
m = geo->matList.findIndex(mesh->material);
x = 0;
for(j = 0; j < mesh->numIndices-2; j++){
a = mesh->indices[j+x];
x = !x;
b = mesh->indices[j+x];
c = mesh->indices[j+2];
if(a >= geo->numVertices ||
b >= geo->numVertices ||
c >= geo->numVertices){
fprintf(stderr, "triangle %d %d %d out of range (%d)\n", a, b, c, geo->numVertices);
goto loss;
}
if(a == b || a == c || b == c)
continue;
trace("%d %d %d\n", a, b, c);
/* now that we have a triangle, try to find it */
for(k = 0; k < geo->numTriangles; k++){
t = &geo->triangles[k];
if(seen[k] || t->matId != m) continue;
if((t->v[0] == a && t->v[1] == b && t->v[2] == c) ||
(t->v[1] == a && t->v[2] == b && t->v[0] == c) ||
(t->v[2] == a && t->v[0] == b && t->v[1] == c)){
seen[k] = 1;
goto found;
}
}
goto loss;
found: ;
}
mesh++;
}
/* Also check that all triangles are in the mesh */
for(i = 0; i < geo->numTriangles; i++)
if(!seen[i]){
loss:
fprintf(stderr, "TRISTRIP verify failed\n");
exit(1);
}
rwFree(seen);
}
}