File: weldmesh.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (202 lines) | stat: -rw-r--r-- 6,154 bytes parent folder | download | duplicates (4)
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
// Slightly modified version of weldmesh, by Wildfire Games, for 0 A.D.
// 
// Motivation for changes:
//  * Fix build on *BSD (including malloc.h produces an error)

#include "precompiled.h"

/**
 *  Copyright (C) 2011 by Morten S. Mikkelsen
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty.  In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *  3. This notice may not be removed or altered from any source distribution.
 */


#include "weldmesh.h"
#include <string.h>
#include <assert.h>

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <stdlib.h>  /* BSD-based OSes get their malloc stuff through here */
#else
#include <malloc.h> 
#endif

static void MergeVertsFast(int * piCurNrUniqueVertices, int * piRemapTable, float * pfVertexDataOut, int * piVertexIDs,
			  const float pfVertexDataIn[], const int iNrVerticesIn, const int iFloatsPerVert,
			  const int iL_in, const int iR_in, const int iChannelNum);

int WeldMesh(int * piRemapTable, float * pfVertexDataOut,
			  const float pfVertexDataIn[], const int iNrVerticesIn, const int iFloatsPerVert)
{
	int iUniqueVertices = 0, i=0;
	int * piVertexIDs = NULL;
	if(iNrVerticesIn<=0) return 0;


	iUniqueVertices = 0;
	piVertexIDs = (int *) malloc(sizeof(int)*iNrVerticesIn);
	if(piVertexIDs!=NULL)
	{
		for(i=0; i<iNrVerticesIn; i++)
		{
			piRemapTable[i] = -1;
			piVertexIDs[i] = i;
		}

		MergeVertsFast(&iUniqueVertices, piRemapTable, pfVertexDataOut, piVertexIDs,
										 pfVertexDataIn, iNrVerticesIn, iFloatsPerVert, 0, iNrVerticesIn-1, 0);

		free(piVertexIDs);

		// debug check
		for(i=0; i<iUniqueVertices; i++)
			assert(piRemapTable[i]>=0);
	}

	return iUniqueVertices;
}





static void MergeVertsFast(int * piCurNrUniqueVertices, int * piRemapTable, float * pfVertexDataOut, int * piVertexIDs,
			  const float pfVertexDataIn[], const int iNrVerticesIn, const int iFloatsPerVert,
			  const int iL_in, const int iR_in, const int iChannelNum)
{
	const int iCount = iR_in-iL_in+1;
	int l=0;
	float fMin, fMax, fAvg;
	assert(iCount>0);
	// make bbox
	fMin = pfVertexDataIn[ piVertexIDs[iL_in]*iFloatsPerVert + iChannelNum]; fMax = fMin;
	for(l=(iL_in+1); l<=iR_in; l++)
	{
		const int index = piVertexIDs[l]*iFloatsPerVert + iChannelNum;
		const float fVal = pfVertexDataIn[index];
		if(fMin>fVal) fMin=fVal;
		else if(fMax<fVal) fMax=fVal;
	}

	// terminate recursion when the separation/average value
	// is no longer strictly between fMin and fMax values.
	fAvg = 0.5f*(fMax + fMin);
	if(fAvg<=fMin || fAvg>=fMax || iCount==1)
	{
		if((iChannelNum+1) == iFloatsPerVert || iCount==1)	// we are done, weld by hand
		{
			int iUniqueNewVertices = 0;
			float * pfNewUniVertsOut = &pfVertexDataOut[ piCurNrUniqueVertices[0]*iFloatsPerVert ];

			for(l=iL_in; l<=iR_in; l++)
			{
				const int index = piVertexIDs[l]*iFloatsPerVert;

				int iFound = 0;	// didn't find copy yet.
				int l2=0;
				while(l2<iUniqueNewVertices && iFound==0)
				{
					const int index2 = l2*iFloatsPerVert;

					int iAllSame = 1;
					int c=0;
					while(iAllSame!=0 && c<iFloatsPerVert)
					{
						iAllSame &= (pfVertexDataIn[index+c] == pfNewUniVertsOut[index2+c] ? 1 : 0);
						++c;
					}

					iFound = iAllSame;
					if(iFound==0) ++l2;
				}
				
				// generate new entry
				if(iFound==0)
				{
					memcpy(pfNewUniVertsOut+iUniqueNewVertices*iFloatsPerVert, pfVertexDataIn+index, sizeof(float)*iFloatsPerVert);
					++iUniqueNewVertices;
				}

				assert(piRemapTable[piVertexIDs[l]] == -1);	// has not yet been assigned
				piRemapTable[piVertexIDs[l]] = piCurNrUniqueVertices[0] + l2;
			}

			piCurNrUniqueVertices[0] += iUniqueNewVertices;
		}
		else
		{
			MergeVertsFast(piCurNrUniqueVertices, piRemapTable, pfVertexDataOut, piVertexIDs,
						   pfVertexDataIn, iNrVerticesIn, iFloatsPerVert,
							iL_in, iR_in, iChannelNum+1);
		}
	}
	else
	{
		int iL=iL_in, iR=iR_in, index;

		// seperate (by fSep) all points between iL_in and iR_in in pTmpVert[]
		while(iL < iR)
		{
			int iReadyLeftSwap = 0;
			int iReadyRightSwap = 0;
			while(iReadyLeftSwap==0 && iL<iR)
			{
				assert(iL>=iL_in && iL<=iR_in);
				index = piVertexIDs[iL]*iFloatsPerVert+iChannelNum;
				iReadyLeftSwap = !(pfVertexDataIn[index]<fAvg) ? 1 : 0;
				if(iReadyLeftSwap==0) ++iL;
			}
			while(iReadyRightSwap==0 && iL<iR)
			{
				assert(iR>=iL_in && iR<=iR_in);
				index = piVertexIDs[iR]*iFloatsPerVert+iChannelNum;
				iReadyRightSwap = pfVertexDataIn[index]<fAvg ? 1 : 0;
				if(iReadyRightSwap==0) --iR;
			}
			assert( (iL<iR) || (iReadyLeftSwap==0 || iReadyRightSwap==0));

			if(iReadyLeftSwap!=0 && iReadyRightSwap!=0)
			{
				int iID=0;
				assert(iL<iR);
				iID = piVertexIDs[iL];
				piVertexIDs[iL] = piVertexIDs[iR];
				piVertexIDs[iR] = iID;
				++iL; --iR;
			}
		}

		assert(iL==(iR+1) || (iL==iR));
		if(iL==iR)
		{
			const int index = piVertexIDs[iR]*iFloatsPerVert+iChannelNum;
			const int iReadyRightSwap = pfVertexDataIn[index]<fAvg ? 1 : 0;
			if(iReadyRightSwap!=0) ++iL;
			else --iR;
		}

		// recurse
		if(iL_in <= iR)
			MergeVertsFast(piCurNrUniqueVertices, piRemapTable, pfVertexDataOut, piVertexIDs,
						   pfVertexDataIn, iNrVerticesIn, iFloatsPerVert, iL_in, iR, iChannelNum);	// weld all left of fSep
		if(iL <= iR_in)
			MergeVertsFast(piCurNrUniqueVertices, piRemapTable, pfVertexDataOut, piVertexIDs,
						   pfVertexDataIn, iNrVerticesIn, iFloatsPerVert, iL, iR_in, iChannelNum);	// weld all right of (or equal to) fSep
	}
}