summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvmesh/MeshBuilder.cpp
blob: 24d8ddff89f508fc14ad5a977ab1e4619b4cccde (plain)
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This code is in the public domain -- castanyo@yahoo.es

#include "nvmesh.h" // pch

#include "MeshBuilder.h"
#include "TriMesh.h"
#include "QuadTriMesh.h"
#include "halfedge/Mesh.h"
#include "halfedge/Vertex.h"
#include "halfedge/Face.h"

#include "weld/Weld.h"

#include "nvmath/Box.h"
#include "nvmath/Vector.inl"

#include "nvcore/StrLib.h"
#include "nvcore/RadixSort.h"
#include "nvcore/Ptr.h"
#include "nvcore/Array.inl"
#include "nvcore/HashMap.inl"


using namespace nv;

/*
By default the mesh builder creates 3 streams (position, normal, texcoord), I'm planning to add support for extra streams as follows:

enum StreamType { StreamType_Float, StreamType_Vector2, StreamType_Vector3, StreamType_Vector4 };

uint addStream(const char *, uint idx, StreamType);

uint addAttribute(float)
uint addAttribute(Vector2)
uint addAttribute(Vector3)
uint addAttribute(Vector4)

struct Vertex
{
    uint pos;
    uint nor;
    uint tex;
    uint * attribs;	// NULL or NIL terminated array?
};

All streams must be added before hand, so that you know the size of the attribs array.

The vertex hash function could be kept as is, but the == operator should be extended to test 
the extra atributes when available.

That might require a custom hash implementation, or an extension of the current one. How to
handle the variable number of attributes in the attribs array?

bool operator()(const Vertex & a, const Vertex & b) const
{ 
    if (a.pos != b.pos || a.nor != b.nor || a.tex != b.tex) return false;
    if (a.attribs == NULL && b.attribs == NULL) return true;
    return 0 == memcmp(a.attribs, b.attribs, ???);
}

We could use a NIL terminated array, or provide custom user data to the equals functor.

vertexMap.setUserData((void *)vertexAttribCount);

bool operator()(const Vertex & a, const Vertex & b, void * userData) const { ... }

*/



namespace 
{
    struct Material
    {
        Material() : faceCount(0) {}
        Material(const String & str) : name(str), faceCount(0) {}

        String name;
        uint faceCount;
    };

    struct Vertex
    {
        //Vertex() {}
        //Vertex(uint p, uint n, uint t0, uint t1, uint c) : pos(p), nor(n), tex0(t0), tex1(t1), col(c) {}

        friend bool operator==(const Vertex & a, const Vertex & b)
        {
            return a.pos == b.pos && a.nor == b.nor && a.tex[0] == b.tex[0] && a.tex[1] == b.tex[1] && a.col[0] == b.col[0] && a.col[1] == b.col[1] && a.col[2] == b.col[2];
        }

        uint pos;
        uint nor;
        uint tex[2];
        uint col[3];
    };

    struct Face
    {
        uint id;
        uint firstIndex;
        uint indexCount;
        uint material;
        uint group;
    };

} // namespace


namespace nv
{
    // This is a much better hash than the default and greatly improves performance!
    template <> struct Hash<Vertex>
    {
        uint operator()(const Vertex & v) const { return v.pos + v.nor + v.tex[0]/* + v.col*/; }
    };
}

struct MeshBuilder::PrivateData
{
    PrivateData() : currentGroup(NIL), currentMaterial(NIL), maxFaceIndexCount(0) {}

    uint pushVertex(uint p, uint n, uint t0, uint t1, uint c0, uint c1, uint c2);
    uint pushVertex(const Vertex & v);

    Array<Vector3> posArray;
    Array<Vector3> norArray;
    Array<Vector2> texArray[2];
    Array<Vector4> colArray[3];

    Array<Vertex> vertexArray;
    HashMap<Vertex, uint> vertexMap;

    HashMap<String, uint> materialMap;
    Array<Material> materialArray;

    uint currentGroup;
    uint currentMaterial;

    Array<uint> indexArray;
    Array<Face> faceArray;

    uint maxFaceIndexCount;
};


uint MeshBuilder::PrivateData::pushVertex(uint p, uint n, uint t0, uint t1, uint c0, uint c1, uint c2)
{
    Vertex v;
    v.pos = p;
    v.nor = n;
    v.tex[0] = t0;
    v.tex[1] = t1;
    v.col[0] = c0;
    v.col[1] = c1;
    v.col[2] = c2;
    return pushVertex(v);
}

uint MeshBuilder::PrivateData::pushVertex(const Vertex & v)
{
    // Lookup vertex v in map.
    uint idx;
    if (vertexMap.get(v, &idx))
    {
        return idx;
    }

    idx = vertexArray.count();
    vertexArray.pushBack(v);
    vertexMap.add(v, idx);

    return idx;
}


MeshBuilder::MeshBuilder() : d(new PrivateData())
{
}

MeshBuilder::~MeshBuilder()
{
    nvDebugCheck(d != NULL);
    delete d;
}


// Builder methods.
uint MeshBuilder::addPosition(const Vector3 & v)
{
    d->posArray.pushBack(validate(v));
    return d->posArray.count() - 1;
}

uint MeshBuilder::addNormal(const Vector3 & v)
{
    d->norArray.pushBack(validate(v));
    return d->norArray.count() - 1;
}

uint MeshBuilder::addTexCoord(const Vector2 & v, uint set/*=0*/)
{
    d->texArray[set].pushBack(validate(v));
    return d->texArray[set].count() - 1;
}

uint MeshBuilder::addColor(const Vector4 & v, uint set/*=0*/)
{
    d->colArray[set].pushBack(validate(v));
    return d->colArray[set].count() - 1;
}

void MeshBuilder::beginGroup(uint id)
{
    d->currentGroup = id;
}

void MeshBuilder::endGroup()
{
    d->currentGroup = NIL;
}

// Add named material, check for uniquenes.
uint MeshBuilder::addMaterial(const char * name)
{
    uint index;
    if (d->materialMap.get(name, &index)) {
        nvDebugCheck(d->materialArray[index].name == name);
    }
    else {
        index = d->materialArray.count();
        d->materialMap.add(name, index);
        
        Material material(name);
        d->materialArray.append(material);
    }
    return index;
}

void MeshBuilder::beginMaterial(uint id)
{
    d->currentMaterial = id;
}

void MeshBuilder::endMaterial()
{
    d->currentMaterial = NIL;
}

void MeshBuilder::beginPolygon(uint id/*=0*/)
{
    Face face;
    face.id = id;
    face.firstIndex = d->indexArray.count();
    face.indexCount = 0;
    face.material = d->currentMaterial;
    face.group = d->currentGroup;

    d->faceArray.pushBack(face);
}

uint MeshBuilder::addVertex(uint p, uint n/*= NIL*/, uint t0/*= NIL*/, uint t1/*= NIL*/, uint c0/*= NIL*/, uint c1/*= NIL*/, uint c2/*= NIL*/)
{
    // @@ In theory there's no need to add vertices before faces, but I'm adding this to debug problems in our maya exporter:
    nvDebugCheck(p < d->posArray.count());
    nvDebugCheck(n == NIL || n < d->norArray.count());
    nvDebugCheck(t0 == NIL || t0 < d->texArray[0].count());
    nvDebugCheck(t1 == NIL || t1 < d->texArray[1].count());
    //nvDebugCheck(c0 == NIL || c0 < d->colArray[0].count());
    if (c0 > d->colArray[0].count()) c0 = NIL;    // @@ This seems to be happening in loc_swamp_catwalk.mb! No idea why.
    nvDebugCheck(c1 == NIL || c1 < d->colArray[1].count());
    nvDebugCheck(c2 == NIL || c2 < d->colArray[2].count());

    uint idx = d->pushVertex(p, n, t0, t1, c0, c1, c2);
    d->indexArray.pushBack(idx);
    d->faceArray.back().indexCount++;
    return idx;
}

uint MeshBuilder::addVertex(const Vector3 & pos)
{
    uint p = addPosition(pos);
    return addVertex(p);
}

#if 0
uint MeshBuilder::addVertex(const Vector3 & pos, const Vector3 & nor, const Vector2 & tex0, const Vector2 & tex1, const Vector4 & col0, const Vector4 & col1)
{
    uint p = addPosition(pos);
    uint n = addNormal(nor);
    uint t0 = addTexCoord(tex0, 0);
    uint t1 = addTexCoord(tex1, 1);
    uint c0 = addColor(col0);
    uint c1 = addColor(col1);
    return addVertex(p, n, t0, t1, c0, c1);
}
#endif

// Return true if the face is valid and was added to the mesh.
bool MeshBuilder::endPolygon()
{
    const Face & face = d->faceArray.back();
    const uint count = face.indexCount;

    // Validate polygon here.
    bool invalid = count <= 2;

    if (!invalid) {
        // Skip zero area polygons. Or polygons with degenerate edges (which will result in zero-area triangles).
        const uint first = face.firstIndex;
        for (uint j = count - 1, i = 0; i < count; j = i, i++) {
            uint v0 = d->indexArray[first + i];
            uint v1 = d->indexArray[first + j];

            uint p0 = d->vertexArray[v0].pos;
            uint p1 = d->vertexArray[v1].pos;

            if (p0 == p1) {
                invalid = true;
                break;
            }

            if (equal(d->posArray[p0], d->posArray[p1], FLT_EPSILON)) {
                invalid = true;
                break;
            }
        }

        uint v0 = d->indexArray[first];
        uint p0 = d->vertexArray[v0].pos;
        Vector3 x0 = d->posArray[p0];

        float area = 0.0f;
        for (uint j = 1, i = 2; i < count; j = i, i++) {
            uint v1 = d->indexArray[first + i];
            uint v2 = d->indexArray[first + j];

            uint p1 = d->vertexArray[v1].pos;
            uint p2 = d->vertexArray[v2].pos;

            Vector3 x1 = d->posArray[p1];
            Vector3 x2 = d->posArray[p2];

            area += length(cross(x1-x0, x2-x0));
        }

        if (0.5 * area < 1e-6) {    // Reduce this threshold if artists have legitimate complains.
            invalid = true;
        }

        // @@ This is not complete. We may still get zero area triangles after triangulation.
        // However, our plugin triangulates before building the mesh, so hopefully that's not a problem.

    }

    if (invalid)
    {
        d->indexArray.resize(d->indexArray.size() - count);
        d->faceArray.popBack();
        return false;
    }
    else
    {
        if (d->currentMaterial != NIL) {
            d->materialArray[d->currentMaterial].faceCount++;
        }

        d->maxFaceIndexCount = max(d->maxFaceIndexCount, count);
        return true;
    }
}


uint MeshBuilder::weldPositions()
{
    Array<uint> xrefs;
    Weld<Vector3> weldVector3;

    if (d->posArray.count()) {
        // Weld vertex attributes.
        weldVector3(d->posArray, xrefs);

        // Remap vertex indices.
        const uint vertexCount = d->vertexArray.count();
        for (uint v = 0; v < vertexCount; v++)
        {
            Vertex & vertex = d->vertexArray[v];
            if (vertex.pos != NIL) vertex.pos = xrefs[vertex.pos];
        }
    }

    return d->posArray.count();
}

uint MeshBuilder::weldNormals()
{
    Array<uint> xrefs;
    Weld<Vector3> weldVector3;

    if (d->norArray.count()) {
        // Weld vertex attributes.
        weldVector3(d->norArray, xrefs);

        // Remap vertex indices.
        const uint vertexCount = d->vertexArray.count();
        for (uint v = 0; v < vertexCount; v++)
        {
            Vertex & vertex = d->vertexArray[v];
            if (vertex.nor != NIL) vertex.nor = xrefs[vertex.nor];
        }
    }

    return d->norArray.count();
}

uint MeshBuilder::weldTexCoords(uint set/*=0*/)
{
    Array<uint> xrefs;
    Weld<Vector2> weldVector2;

    if (d->texArray[set].count()) {
        // Weld vertex attributes.
        weldVector2(d->texArray[set], xrefs);

        // Remap vertex indices.
        const uint vertexCount = d->vertexArray.count();
        for (uint v = 0; v < vertexCount; v++)
        {
            Vertex & vertex = d->vertexArray[v];
            if (vertex.tex[set] != NIL) vertex.tex[set] = xrefs[vertex.tex[set]];
        }
    }

    return d->texArray[set].count();
}

uint  MeshBuilder::weldColors(uint set/*=0*/)
{
    Array<uint> xrefs;
    Weld<Vector4> weldVector4;

    if (d->colArray[set].count()) {
        // Weld vertex attributes.
        weldVector4(d->colArray[set], xrefs);

        // Remap vertex indices.
        const uint vertexCount = d->vertexArray.count();
        for (uint v = 0; v < vertexCount; v++)
        {
            Vertex & vertex = d->vertexArray[v];
            if (vertex.col[set] != NIL) vertex.col[set] = xrefs[vertex.col[set]];
        }
    }

    return d->colArray[set].count();
}

void MeshBuilder::weldVertices() {

    if (d->vertexArray.count() == 0) {
        // Nothing to do.
        return;
    }

    Array<uint> xrefs;
    Weld<Vertex> weldVertex;

    // Weld vertices.
    weldVertex(d->vertexArray, xrefs);

    // Remap face indices.
    const uint indexCount = d->indexArray.count();
    for (uint i = 0; i < indexCount; i++)
    {
        d->indexArray[i] = xrefs[d->indexArray[i]];
    }

    // Remap vertex map.
    foreach(i, d->vertexMap)
    {
        d->vertexMap[i].value = xrefs[d->vertexMap[i].value];
    }
}


void MeshBuilder::optimize()
{
    if (d->vertexArray.count() == 0)
    {
        return;
    }

    weldPositions();
    weldNormals();
    weldTexCoords(0);
    weldTexCoords(1);
    weldColors();

    weldVertices();
}






void MeshBuilder::removeUnusedMaterials(Array<uint> & newMaterialId)
{
    uint materialCount = d->materialArray.count();

    // Reset face counts.
    for (uint i = 0; i < materialCount; i++) {
        d->materialArray[i].faceCount = 0;
    }

    // Count faces.
    foreach(i, d->faceArray) {
        Face & face = d->faceArray[i];

        if (face.material != NIL) {
            nvDebugCheck(face.material < materialCount);

            d->materialArray[face.material].faceCount++;
        }
    }

    // Remove unused materials.
    newMaterialId.resize(materialCount);

    for (uint i = 0, m = 0; i < materialCount; i++)
    {
        if (d->materialArray[m].faceCount > 0)
        {
            newMaterialId[i] = m++;
        }
        else
        {
            newMaterialId[i] = NIL;
            d->materialArray.removeAt(m);
        }
    }

    materialCount = d->materialArray.count();

    // Update face material ids.
    foreach(i, d->faceArray) {
        Face & face = d->faceArray[i];

        if (face.material != NIL) {
            uint id = newMaterialId[face.material];
            nvDebugCheck(id != NIL && id < materialCount);

            face.material = id;
        }
    }
}

void MeshBuilder::sortFacesByGroup()
{
    const uint faceCount = d->faceArray.count();

    Array<uint> faceGroupArray;
    faceGroupArray.resize(faceCount);
    
    for (uint i = 0; i < faceCount; i++) {
        faceGroupArray[i] = d->faceArray[i].group;
    }

    RadixSort radix;
    radix.sort(faceGroupArray);

    Array<Face> newFaceArray;
    newFaceArray.resize(faceCount);

    for (uint i = 0; i < faceCount; i++) {
        newFaceArray[i] = d->faceArray[radix.rank(i)];
    }

    swap(newFaceArray, d->faceArray);
}

void MeshBuilder::sortFacesByMaterial()
{
    const uint faceCount = d->faceArray.count();

    Array<uint> faceMaterialArray;
    faceMaterialArray.resize(faceCount);
    
    for (uint i = 0; i < faceCount; i++) {
        faceMaterialArray[i] = d->faceArray[i].material;
    }

    RadixSort radix;
    radix.sort(faceMaterialArray);

    Array<Face> newFaceArray;
    newFaceArray.resize(faceCount);

    for (uint i = 0; i < faceCount; i++) {
        newFaceArray[i] = d->faceArray[radix.rank(i)];
    }

    swap(newFaceArray, d->faceArray);
}


void MeshBuilder::reset()
{
    nvDebugCheck(d != NULL);
    delete d;
    d = new PrivateData();
}

void MeshBuilder::done()
{
    if (d->currentGroup != NIL) {
        endGroup();
    }

    if (d->currentMaterial != NIL) {
        endMaterial();
    }
}

// Hints.
void MeshBuilder::hintTriangleCount(uint count)
{
    d->indexArray.reserve(d->indexArray.count() + count * 4);
}

void MeshBuilder::hintVertexCount(uint count)
{
    d->vertexArray.reserve(d->vertexArray.count() + count);
    d->vertexMap.resize(d->vertexMap.count() + count);
}

void MeshBuilder::hintPositionCount(uint count)
{
    d->posArray.reserve(d->posArray.count() + count);
}

void MeshBuilder::hintNormalCount(uint count)
{
    d->norArray.reserve(d->norArray.count() + count);
}

void MeshBuilder::hintTexCoordCount(uint count, uint set/*=0*/)
{
    d->texArray[set].reserve(d->texArray[set].count() + count);
}

void MeshBuilder::hintColorCount(uint count, uint set/*=0*/)
{
    d->colArray[set].reserve(d->colArray[set].count() + count);
}


// Helpers.
void MeshBuilder::addTriangle(uint v0, uint v1, uint v2)
{
    beginPolygon();
    addVertex(v0);
    addVertex(v1);
    addVertex(v2);
    endPolygon();
}

void MeshBuilder::addQuad(uint v0, uint v1, uint v2, uint v3)
{
    beginPolygon();
    addVertex(v0);
    addVertex(v1);
    addVertex(v2);
    addVertex(v3);
    endPolygon();
}


// Get tri mesh.
TriMesh * MeshBuilder::buildTriMesh() const
{
    const uint faceCount = d->faceArray.count();
    uint triangleCount = 0;
    for (uint f = 0; f < faceCount; f++) {
        triangleCount += d->faceArray[f].indexCount - 2;
    }
    
    const uint vertexCount = d->vertexArray.count();
    TriMesh * mesh = new TriMesh(triangleCount, vertexCount);

    // Build faces.
    Array<TriMesh::Face> & faces = mesh->faces();

    for(uint f = 0; f < faceCount; f++)
    {
        int firstIndex = d->faceArray[f].firstIndex;
        int indexCount = d->faceArray[f].indexCount;

        int v0 = d->indexArray[firstIndex + 0];
        int v1 = d->indexArray[firstIndex + 1];

        for(int t = 0; t < indexCount - 2; t++) {
            int v2 = d->indexArray[firstIndex + t + 2];

            TriMesh::Face face;
            face.id = faces.count();
            face.v[0] = v0;
            face.v[1] = v1;
            face.v[2] = v2;
            faces.append(face);

            v1 = v2;
        }
    }

    // Build vertices.
    Array<BaseMesh::Vertex> & vertices = mesh->vertices();

    for(uint i = 0; i < vertexCount; i++)
    {
        BaseMesh::Vertex vertex;
        vertex.id = i;
        if (d->vertexArray[i].pos != NIL) vertex.pos = d->posArray[d->vertexArray[i].pos];
        if (d->vertexArray[i].nor != NIL) vertex.nor = d->norArray[d->vertexArray[i].nor];
        if (d->vertexArray[i].tex[0] != NIL) vertex.tex = d->texArray[0][d->vertexArray[i].tex[0]];

        vertices.append(vertex);
    }

    return mesh;
}

// Get quad/tri mesh.
QuadTriMesh * MeshBuilder::buildQuadTriMesh() const
{
    const uint faceCount = d->faceArray.count();
    const uint vertexCount = d->vertexArray.count();
    QuadTriMesh * mesh = new QuadTriMesh(faceCount, vertexCount);

    // Build faces.
    Array<QuadTriMesh::Face> & faces = mesh->faces();

    for (uint f = 0; f < faceCount; f++) 
    {
        int firstIndex = d->faceArray[f].firstIndex;
        int indexCount = d->faceArray[f].indexCount;

        QuadTriMesh::Face face;
        face.id = f;

        face.v[0] = d->indexArray[firstIndex + 0];
        face.v[1] = d->indexArray[firstIndex + 1];
        face.v[2] = d->indexArray[firstIndex + 2];

        // Only adds triangles and quads. Ignores polygons.
        if (indexCount == 3) {
            face.v[3] = NIL;
            faces.append(face);
        }
        else if (indexCount == 4) {
            face.v[3] = d->indexArray[firstIndex + 3];
            faces.append(face);
        }
    }

    // Build vertices.
    Array<BaseMesh::Vertex> & vertices = mesh->vertices();

    for(uint i = 0; i < vertexCount; i++)
    {
        BaseMesh::Vertex vertex;
        vertex.id = i;
        if (d->vertexArray[i].pos != NIL) vertex.pos = d->posArray[d->vertexArray[i].pos];
        if (d->vertexArray[i].nor != NIL) vertex.nor = d->norArray[d->vertexArray[i].nor];
        if (d->vertexArray[i].tex[0] != NIL) vertex.tex = d->texArray[0][d->vertexArray[i].tex[0]];

        vertices.append(vertex);
    }

    return mesh;
}

// Get half edge mesh.
HalfEdge::Mesh * MeshBuilder::buildHalfEdgeMesh(bool weldPositions, Error * error/*=NULL*/, Array<uint> * badFaces/*=NULL*/) const
{
    if (error != NULL) *error = Error_None;

    const uint vertexCount = d->vertexArray.count();
    AutoPtr<HalfEdge::Mesh> mesh(new HalfEdge::Mesh());

    for(uint v = 0; v < vertexCount; v++)
    {
        HalfEdge::Vertex * vertex = mesh->addVertex(d->posArray[d->vertexArray[v].pos]);
        if (d->vertexArray[v].nor != NIL) vertex->nor = d->norArray[d->vertexArray[v].nor];
        if (d->vertexArray[v].tex[0] != NIL) vertex->tex = Vector2(d->texArray[0][d->vertexArray[v].tex[0]]);
        if (d->vertexArray[v].col[0] != NIL) vertex->col = d->colArray[0][d->vertexArray[v].col[0]];
    }

    if (weldPositions) {
        mesh->linkColocals();
    }
    else {
        // Build canonical map from position indices.
        Array<uint> canonicalMap(vertexCount);
        
        foreach (i, d->vertexArray) {
            canonicalMap.append(d->vertexArray[i].pos);
        }

        mesh->linkColocalsWithCanonicalMap(canonicalMap);
    }

    const uint faceCount = d->faceArray.count();
    for (uint f = 0; f < faceCount; f++)
    {
        const uint firstIndex = d->faceArray[f].firstIndex;
        const uint indexCount = d->faceArray[f].indexCount;

        HalfEdge::Face * face = mesh->addFace(d->indexArray, firstIndex, indexCount);
        
        // @@ This is too late, removing the face here will leave the mesh improperly connected.
        /*if (face->area() <= FLT_EPSILON) {
            mesh->remove(face);
            face = NULL;
        }*/

        if (face == NULL) {
            // Non manifold mesh.
            if (error != NULL) *error = Error_NonManifoldEdge;
            if (badFaces != NULL) {
                badFaces->append(d->faceArray[f].id);
            }
            //return NULL; // IC: Ignore error and continue building the mesh.
        }

        if (face != NULL) {
            face->group = d->faceArray[f].group;
            face->material = d->faceArray[f].material;
        }
    }

    mesh->linkBoundary();

    // We cannot fix functions here, because this would introduce new vertices and these vertices won't have the corresponding builder data.

    // Maybe the builder should perform the search for T-junctions and update the vertex data directly.

    // For now, we don't fix T-junctions at export time, but only during parameterization.

    //mesh->fixBoundaryJunctions();

    //mesh->sewBoundary();

    return mesh.release();
}


bool MeshBuilder::buildPositions(Array<Vector3> & positionArray)
{
    const uint vertexCount = d->vertexArray.count();
    positionArray.resize(vertexCount);

    for (uint v = 0; v < vertexCount; v++)
    {
        nvDebugCheck(d->vertexArray[v].pos != NIL);
        positionArray[v] = d->posArray[d->vertexArray[v].pos];
    }

    return true;
}

bool MeshBuilder::buildNormals(Array<Vector3> & normalArray)
{
    bool anyNormal = false;

    const uint vertexCount = d->vertexArray.count();
    normalArray.resize(vertexCount);

    for (uint v = 0; v < vertexCount; v++)
    {
        if (d->vertexArray[v].nor == NIL) {
            normalArray[v] = Vector3(0, 0, 1);
        }
        else {
            anyNormal = true;
            normalArray[v] = d->norArray[d->vertexArray[v].nor];
        }
    }

    return anyNormal;
}

bool MeshBuilder::buildTexCoords(Array<Vector2> & texCoordArray, uint set/*=0*/)
{
    bool anyTexCoord = false;

    const uint vertexCount = d->vertexArray.count();
    texCoordArray.resize(vertexCount);

    for (uint v = 0; v < vertexCount; v++)
    {
        if (d->vertexArray[v].tex[set] == NIL) {
            texCoordArray[v] = Vector2(0, 0);
        }
        else {
            anyTexCoord = true;
            texCoordArray[v] = d->texArray[set][d->vertexArray[v].tex[set]];
        }
    }

    return anyTexCoord;
}

bool MeshBuilder::buildColors(Array<Vector4> & colorArray, uint set/*=0*/)
{
    bool anyColor = false;

    const uint vertexCount = d->vertexArray.count();
    colorArray.resize(vertexCount);

    for (uint v = 0; v < vertexCount; v++)
    {
        if (d->vertexArray[v].col[set] == NIL) {
            colorArray[v] = Vector4(0, 0, 0, 1);
        }
        else {
            anyColor = true;
            colorArray[v] = d->colArray[set][d->vertexArray[v].col[set]];
        }
    }

    return anyColor;
}

void MeshBuilder::buildVertexToPositionMap(Array<int> &map)
{
	const uint vertexCount = d->vertexArray.count();
	map.resize(vertexCount);

	foreach (i, d->vertexArray) {
		map[i] = d->vertexArray[i].pos;
	}
}



uint MeshBuilder::vertexCount() const
{
    return d->vertexArray.count();
}


uint MeshBuilder::positionCount() const
{
    return d->posArray.count();
}

uint MeshBuilder::normalCount() const
{
    return d->norArray.count();
}

uint MeshBuilder::texCoordCount(uint set/*=0*/) const
{
    return d->texArray[set].count();
}

uint MeshBuilder::colorCount(uint set/*=0*/) const
{
    return d->colArray[set].count();
}


uint MeshBuilder::materialCount() const
{
    return d->materialArray.count();
}

const char * MeshBuilder::material(uint i) const
{
    return d->materialArray[i].name;
}


uint MeshBuilder::positionIndex(uint vertex) const
{
    return d->vertexArray[vertex].pos;
}
uint MeshBuilder::normalIndex(uint vertex) const
{
    return d->vertexArray[vertex].nor;
}
uint MeshBuilder::texCoordIndex(uint vertex, uint set/*=0*/) const
{
    return d->vertexArray[vertex].tex[set];
}
uint MeshBuilder::colorIndex(uint vertex, uint set/*=0*/) const
{
    return d->vertexArray[vertex].col[set];
}