summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/TriStripper.cpp
blob: 89d6c1b54a2a55f50bd48f9a5a6eaf7e05e45a63 (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
#include "UnityPrefix.h"
#include "External/Tristripper/Striper.h"
#include "TriStripper.h"
#include "Runtime/Filters/Mesh/Mesh.h"
#include "Runtime/Utilities/vector_utility.h"
#include "Runtime/Utilities/LogAssert.h"

using namespace std;


bool Stripify (const UInt32* faces, int count, UNITY_TEMP_VECTOR(UInt32)& strip)
{
	Assert (faces != NULL || !count);
	strip.clear ();

	// Fail on empty input.
	if (!count)
		return false;

	STRIPERCREATE stripinput;
	stripinput.DFaces = const_cast<UInt32*> (faces);
	stripinput.NbFaces = count / 3;

	STRIPERRESULT stripresult;
	Striper striper;
	if (striper.Init (stripinput) && striper.Compute (stripresult) && stripresult.NbStrips == 1)
	{
		int stripSize = stripresult.StripLengths[0];
		UInt32* stripData = reinterpret_cast<UInt32*> (stripresult.StripRuns);
		reserve_trimmed (strip, stripSize);
		strip.assign (stripData, stripData + stripSize);
		return true;
	}
	return false;
}

template<typename T>
int CountTrianglesInStrip (const T* strip, int length)
{
	int count = 0;
	// de-stripify :
	int n = length;
	for(int i=0;i<(n-2);i++)
	{
		T a = strip[i];
		T b = strip[i+1];
		T c = strip[i+2];

		// skip degenerates
		if ( a == b || a == c || b == c )
			continue;
		
		count++;
	}
	return count;
}

template int CountTrianglesInStrip<UInt16> (const UInt16* strip, int length);
template int CountTrianglesInStrip<UInt32> (const UInt32* strip, int length);

// WARNING: You have to make sure you have space for all indices in trilist (you can use CountTrianglesInStrip for that)
template<typename Tin, typename Tout>
void Destripify(const Tin* strip, int length, Tout* trilist, int capacity)
{
	// de-stripify :
	int n = length;
	int triIndex = 0;
	for(int i=0;i<(n-2);i++)
	{
		Tin a = strip[i];
		Tin b = strip[i+1];
		Tin c = strip[i+2];

		// skip degenerates
		if ( a == b || a == c || b == c )
			continue;

		// do the winding flip-flop of strips :
		if ( (i&1) == 1 )
			std::swap (a,b);

		trilist[triIndex++] = a;
		trilist[triIndex++] = b;
		trilist[triIndex++] = c;
	}
}


template void Destripify<UInt32, UInt32> (const UInt32* strip, int length, UInt32* trilist, int capacity);
template void Destripify<UInt16, UInt32> (const UInt16* strip, int length, UInt32* trilist, int capacity);
template void Destripify<UInt16, UInt16> (const UInt16* strip, int length, UInt16* trilist, int capacity);

void Destripify (const UInt16* strip, int length, UNITY_TEMP_VECTOR(UInt16)& trilist)
{
	int oldSize = trilist.size();
	trilist.resize (oldSize + CountTrianglesInStrip(strip, length) * 3);
	Destripify(strip, length, &trilist[oldSize], trilist.size());
}

void Destripify (const UInt16* strip, int length, UNITY_TEMP_VECTOR(UInt32)& trilist)
{
	int oldSize = trilist.size();
	trilist.resize (oldSize + CountTrianglesInStrip(strip, length) * 3);
	Destripify(strip, length, &trilist[oldSize], trilist.size());
}

void Destripify (const UInt32* strip, int length, UNITY_TEMP_VECTOR(UInt32)& trilist)
{
	int oldSize = trilist.size();
	trilist.resize (oldSize + CountTrianglesInStrip(strip, length) * 3);
	Destripify(strip, length, &trilist[oldSize], trilist.size());
}