23template <
class T>
inline void swap (T& a, T& b)
36template <
class T>
inline void Quick_Sort (T* a,
int N);
40 for (
int i = 1; i <
N; i++)
57template <
class T>
inline void Quick_Sort (T* a,
int l,
int r)
69 do { i++; }
while (i < r && a[i] < v);
70 do { j--; }
while (j > 0 && a[j] > v);
106 for (
int i = 0; i < strip_count; i++)
126static inline int Get_Strip_Similarity (
const int* A,
const int* B)
132 for (
int i = 0; i < lenA; i++)
135 for (
int j = 0; j < lenB; j++)
147static inline int* Copy_Strip (
int* d,
const int* s)
151 for (
int i = 0; i < len; i++)
158 if (strip_count <= 0)
164 for (
int i = 0; i < strip_count; i++)
174 const int* prev = ss[0];
175 o = Copy_Strip (o, ss[0]);
181 int bestSimilarity = -1;
183 for (
int j = 0; j < strip_count; j++)
186 int sim = Get_Strip_Similarity (prev, ss[j]);
187 if (sim > bestSimilarity)
189 bestSimilarity = sim;
197 o = Copy_Strip(o, ss[bestIndex]);
198 prev = ss[bestIndex];
199 ss[bestIndex] =
NULL;
204 for (i = 0; i < outSize; i++)
230static inline int Get_Similarity (
const Tri& A,
const Tri& B)
234 if (A.
a == B.
a || A.
a == B.
b || A.
a == B.
c) sim++;
235 if (A.
b == B.
a || A.
b == B.
b || A.
b == B.
c) sim++;
236 if (A.
c == B.
a || A.
c == B.
b || A.
c == B.
c) sim++;
243 if (triangle_count<=0)
248 for (
int i = 0; i < triangle_count; i++)
250 t[i] = (
Tri*)(tris + i*3);
267 for (
int j = 0; j < triangle_count; j++)
270 int sim = Get_Similarity (*prev, *t[j]);
283 *o++ = *t[bestIndex];
289 WWASSERT(o == (out+triangle_count));
291 for (i = 0; i < triangle_count; i++)
322 bool prevEven =
true;
324 for (
int i = 0; i < strip_count; i++)
336 for (
int j = 0; j < len; j++)
339 if (i != (strip_count-1))
342 prevEven = (!(len&1));
388 Edge (
int v0,
int v1) {
v[0] = v0;
v[1] = v1; }
454 int* m_nodeConnectivity;
472 Stripify (
const Stripify&);
473 Stripify& operator= (
const Stripify&);
477 static int getMod3 (
int i) {
WWASSERT(i>=0 && i<6);
return s_mod[i]; }
481int Stripify::s_mod[6] = {0,1,2,0,1,2};
487 return (s.
v[0]*139) + (s.
v[1]*7);
505 for (
int i = 0; i < 4; i++)
526 return m_nodeConnectivity[i];
539 delete[] m_nodeConnectivity;
553inline void TriangleQueue::reinsert (
Triangle* t)
611 for (i = 0; i < 3; i++)
618 for (k = 0; k < 3; k++)
630 for (i = 0; i < 3; i++)
636 for (i = 0; i < 3; i++)
656 for (i = 0; i < 4; i++)
659 int largestIndex = 0;
661 for (i = 0; i <
N; i++)
662 for (
int j = 0; j < 3; j++)
665 if (tris[i].m_vertices[j] > largestIndex)
669 m_vertexCount = largestIndex+1;
670 m_nodeConnectivity =
W3DNEWARRAY int[m_vertexCount];
671 for (i = 0; i < m_vertexCount; i++)
672 m_nodeConnectivity[i] = 0;
675 for (i = 0; i <
N; i++)
687 for (
int j = 0; j < 3; j++)
709 for (
int i = 0; i < 3; i++)
713 int highestVal = weight[0];
714 if (weight[1] > highestVal) highestVal = weight[1];
715 if (weight[2] > highestVal) highestVal = weight[2];
719 for (i = 0; i < 3; i++) {
720 if (weight[0] == highestVal) v[i] = +1;
752 for (i = 0; i <
N; i++)
759 tris[i].m_vertices[0] = inTris[i][0];
760 tris[i].m_vertices[1] = inTris[i][1];
761 tris[i].m_vertices[2] = inTris[i][2];
768 HashTemplateClass<Edge,Triangle*> hash;
771 for (i = 0; i <
N; i++, t++)
773 for (
int j = 0; j < 3; j++)
774 if (!t->m_neighbors[j])
776 Edge edge = tris[i].getEdge(j);
780 Triangle* n = hash.
Get(e);
784 for (k = 0; k < 3; k++)
785 if (!n->m_neighbors[k])
787 Edge ek = n->getEdge(k);
788 if (ek.v[0] == edge.v[1] && ek.v[1] == edge.v[0])
794 t->m_neighbors[j] = n;
795 n->m_neighbors[k] = t;
839 Triangle* tris = generateTriangleList(inTris,
N);
866 int bestWeight = 0x7fffffff;
868 Vector3i nodeWeights = getTriangleNodeConnectivityWeights(queue, *t);
870 for (
int i = 0; i < 3; i++)
875 weight += nodeWeights[i];
876 weight += nodeWeights[getMod3(i+1)];
879 if (weight <= bestWeight)
896 for (i = 0; i < 3; i++)
900 for (
int j = 0; j < 3; j++)
905 if (e.
v[0] == o[-1] && e.
v[1] == o[-2])
923 int bestWeight = 0x7fffffff;
924 bool bestSwap =
false;
926 Vector3i nodeWeights = getTriangleNodeConnectivityWeights(queue, *next);
928 for (i = 0; i < 3; i++)
933 bool swap = (e.
v[0] == o[-2] || e.
v[1] == o[-2]);
940 w += nodeWeights[getMod3(i+1)];
942 w += (
swap) ? 1 : -1;
952 if (bestEdge != -1 && bestSwap)
998 for (
int i = 0; i < len; i++)
ValueType Get(const KeyType &s) const
void Remove(const KeyType &s)
void Insert(const KeyType &s, const ValueType &d)
static unsigned int Get_Hash_Value(const Key &k)
static int * stripify(const Vector3i *tris, int N)
void removeTriangle(Triangle *t)
TriangleQueue(Triangle *tris, int N)
int getVertexConnectivity(int i) const
Triangle * getTop(void) const
static void Optimize_Triangle_Order(int *tris, int triangle_count)
static int * Combine_Strips(const int *strips, int strip_count)
static int * Stripify(const int *tris, int tri_count)
static void Optimize_Strip_Order(int *strips, int strip_count)
static int Get_Strip_Index_Count(const int *strips, int strips_count)
void Insertion_Sort(T *a, int N)
void Quick_Sort(T *a, int N)
bool operator==(const Edge &s) const
const Edge getEdge(int i) const
Triangle * m_neighbors[3]
int getConnectivity(void) const
Vector3i(int a, int b, int c)
const int & operator[](int i) const
bool operator>(const Tri &s) const
bool operator<(const Tri &s) const