aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/OptimizedBvh.cs
blob: c22c9902e0cc8ef2f99725255cff9a294372c64f (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
/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  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.
*/

using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;

namespace XnaDevRu.BulletX
{
	/// <summary>
	/// OptimizedBvh store an AABB tree that can be quickly traversed on CPU (and SPU,GPU in future)
	/// </summary>
	public class OptimizedBvh
	{
		private static int _maxIterations = 0;
		private OptimizedBvhNode _rootNode;

		private OptimizedBvhNode[] _contiguousNodes;
		private int _curNodeIndex;

		private List<OptimizedBvhNode> _leafNodes = new List<OptimizedBvhNode>();

        public OptimizedBvh() { }

		public void Build(StridingMeshInterface triangles)
		{
			NodeTriangleCallback callback = new NodeTriangleCallback(_leafNodes);

			Vector3 aabbMin = new Vector3(-1e30f, -1e30f, -1e30f);
			Vector3 aabbMax = new Vector3(1e30f, 1e30f, 1e30f);

			triangles.InternalProcessAllTriangles(callback, aabbMin, aabbMax);

			//now we have an array of leafnodes in m_leafNodes
			_contiguousNodes = new OptimizedBvhNode[2 * _leafNodes.Count];
            for (int i = 0; i < _contiguousNodes.Length; i++)
                _contiguousNodes[i] = new OptimizedBvhNode();
			_curNodeIndex = 0;

			_rootNode = BuildTree(_leafNodes, 0, _leafNodes.Count);
		}

		public OptimizedBvhNode BuildTree(List<OptimizedBvhNode> leafNodes, int startIndex, int endIndex)
		{
			OptimizedBvhNode internalNode;

			int splitAxis, splitIndex, i;
			int numIndices = endIndex - startIndex;
			int curIndex = _curNodeIndex;

			if (numIndices <= 0)
				throw new BulletException();

			if (numIndices == 1)
			{
				_contiguousNodes[_curNodeIndex++] = leafNodes[startIndex];
				//return new (&m_contiguousNodes[m_curNodeIndex++]) btOptimizedBvhNode(leafNodes[startIndex]);
				return leafNodes[startIndex];
			}

			//calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
			splitAxis = CalculateSplittingAxis(leafNodes, startIndex, endIndex);

			splitIndex = SortAndCalculateSplittingIndex(leafNodes, startIndex, endIndex, splitAxis);

			internalNode = _contiguousNodes[_curNodeIndex++];

			internalNode.AabbMax = new Vector3(-1e30f, -1e30f, -1e30f);
			internalNode.AabbMin = new Vector3(1e30f, 1e30f, 1e30f);

			for (i = startIndex; i < endIndex; i++)
			{
				internalNode.AabbMax = MathHelper.SetMax(internalNode.AabbMax, leafNodes[i].AabbMax);
				internalNode.AabbMin = MathHelper.SetMin(internalNode.AabbMin, leafNodes[i].AabbMin);
			}

			//internalNode->m_escapeIndex;
			internalNode.LeftChild = BuildTree(leafNodes, startIndex, splitIndex);
			internalNode.RightChild = BuildTree(leafNodes, splitIndex, endIndex);

			internalNode.EscapeIndex = _curNodeIndex - curIndex;
			return internalNode;
		}

		public int CalculateSplittingAxis(List<OptimizedBvhNode> leafNodes, int startIndex, int endIndex)
		{
			Vector3 means = new Vector3();
			Vector3 variance = new Vector3();
			int numIndices = endIndex - startIndex;

			for (int i = startIndex; i < endIndex; i++)
			{
				Vector3 center = 0.5f * (leafNodes[i].AabbMax + leafNodes[i].AabbMin);
				means += center;
			}
			means *= (1f / (float)numIndices);

			for (int i = startIndex; i < endIndex; i++)
			{
				Vector3 center = 0.5f * (leafNodes[i].AabbMax + leafNodes[i].AabbMin);
				Vector3 diff2 = center - means;
				diff2 = diff2 * diff2;
				variance += diff2;
			}
			variance *= (1f / ((float)numIndices - 1));

			return MathHelper.MaxAxis(variance);
		}

		public int SortAndCalculateSplittingIndex(List<OptimizedBvhNode> leafNodes, int startIndex, int endIndex, int splitAxis)
		{
			int splitIndex = startIndex;
			int numIndices = endIndex - startIndex;
			float splitValue;

			Vector3 means = new Vector3();
			for (int i = startIndex; i < endIndex; i++)
			{
				Vector3 center = 0.5f * (leafNodes[i].AabbMax + leafNodes[i].AabbMin);
				means += center;
			}
			means *= (1f / (float)numIndices);

			if (splitAxis == 0)
				splitValue = means.X;
			else if (splitAxis == 1)
				splitValue = means.Y;
			else if (splitAxis == 2)
				splitValue = means.Z;
			else
				throw new ArgumentException();

			//sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
			for (int i = startIndex; i < endIndex; i++)
			{
				Vector3 center = 0.5f * (leafNodes[i].AabbMax + leafNodes[i].AabbMin);
				float centerSplit;

				if (splitAxis == 0)
					centerSplit = means.X;
				else if (splitAxis == 1)
					centerSplit = means.Y;
				else if (splitAxis == 2)
					centerSplit = means.Z;
				else
					throw new ArgumentException();

				if (centerSplit > splitValue)
				{
					//swap
					OptimizedBvhNode tmp = leafNodes[i];
					leafNodes[i] = leafNodes[splitIndex];
					leafNodes[splitIndex] = tmp;
					splitIndex++;
				}
			}
			if ((splitIndex == startIndex) || (splitIndex == (endIndex - 1)))
			{
				splitIndex = startIndex + (numIndices >> 1);
			}
			return splitIndex;
		}

		public void WalkTree(OptimizedBvhNode rootNode, INodeOverlapCallback nodeCallback, Vector3 aabbMin, Vector3 aabbMax)
		{
			bool isLeafNode, aabbOverlap = MathHelper.TestAabbAgainstAabb2(aabbMin, aabbMax, rootNode.AabbMin, rootNode.AabbMax);
			if (aabbOverlap)
			{
				isLeafNode = (rootNode.LeftChild == null && rootNode.RightChild == null);
				if (isLeafNode)
				{
					nodeCallback.ProcessNode(rootNode);
				}
				else
				{
					WalkTree(rootNode.LeftChild, nodeCallback, aabbMin, aabbMax);
					WalkTree(rootNode.RightChild, nodeCallback, aabbMin, aabbMax);
				}
			}
		}

		public void WalkStacklessTree(OptimizedBvhNode[] rootNodeArray, INodeOverlapCallback nodeCallback, Vector3 aabbMin, Vector3 aabbMax)
		{
			int escapeIndex, curIndex = 0;
			int walkIterations = 0;
			bool aabbOverlap, isLeafNode;
            int rootNodeIndex = 0;
            OptimizedBvhNode rootNode = rootNodeArray[rootNodeIndex];

			while (curIndex < _curNodeIndex)
			{
				//catch bugs in tree data
				if (walkIterations >= _curNodeIndex)
					throw new BulletException();

				walkIterations++;
				aabbOverlap = MathHelper.TestAabbAgainstAabb2(aabbMin, aabbMax, rootNode.AabbMin, rootNode.AabbMax);
				isLeafNode = (rootNode.LeftChild == null && rootNode.RightChild == null);

				if (isLeafNode && aabbOverlap)
				{
					nodeCallback.ProcessNode(rootNode);
				}

				if (aabbOverlap || isLeafNode)
				{
					rootNodeIndex++; // this
					curIndex++;
                    if (rootNodeIndex < rootNodeArray.Length)
                        rootNode = rootNodeArray[rootNodeIndex];
                }
				else
				{
					escapeIndex = rootNode.EscapeIndex;
					rootNodeIndex += escapeIndex; // and this
					curIndex += escapeIndex;
                    if (rootNodeIndex < rootNodeArray.Length)
                        rootNode = rootNodeArray[rootNodeIndex];
				}

			}

			if (_maxIterations < walkIterations)
				_maxIterations = walkIterations;
		}

		public void ReportAabbOverlappingNodex(INodeOverlapCallback nodeCallback, Vector3 aabbMin, Vector3 aabbMax)
		{
			//either choose recursive traversal (walkTree) or stackless (walkStacklessTree)
			//walkTree(m_rootNode1,nodeCallback,aabbMin,aabbMax);
			//WalkStacklessTree(_rootNode, nodeCallback, aabbMin, aabbMax);
            WalkStacklessTree(_contiguousNodes, nodeCallback, aabbMin, aabbMax);
		}

		public void ReportSphereOverlappingNodex(INodeOverlapCallback nodeCallback, Vector3 aabbMin, Vector3 aabbMax) { }
	}

	public class NodeTriangleCallback : ITriangleIndexCallback
	{
		private List<OptimizedBvhNode> _triangleNodes;

		public NodeTriangleCallback(List<OptimizedBvhNode> triangleNodes)
		{
			_triangleNodes = triangleNodes;
		}

		public List<OptimizedBvhNode> TriangleNodes { get { return _triangleNodes; } set { _triangleNodes = value; } }

		public void ProcessTriangleIndex(Vector3[] triangle, int partId, int triangleIndex)
		{

			OptimizedBvhNode node = new OptimizedBvhNode();
			node.AabbMin = new Vector3(1e30f, 1e30f, 1e30f);
			node.AabbMax = new Vector3(-1e30f, -1e30f, -1e30f);

			node.AabbMin = MathHelper.SetMin(node.AabbMin, triangle[0]);
			node.AabbMax = MathHelper.SetMax(node.AabbMax, triangle[0]);
			node.AabbMin = MathHelper.SetMin(node.AabbMin, triangle[1]);
			node.AabbMax = MathHelper.SetMax(node.AabbMax, triangle[1]);
			node.AabbMin = MathHelper.SetMin(node.AabbMin, triangle[2]);
			node.AabbMax = MathHelper.SetMax(node.AabbMax, triangle[2]);

			node.EscapeIndex = -1;
			node.LeftChild = null;
			node.RightChild = null;

			//for child nodes
			node.SubPart = partId;
			node.TriangleIndex = triangleIndex;

			_triangleNodes.Add(node);
		}
	}
}