summaryrefslogtreecommitdiff
path: root/Runtime/NavMesh/DynamicMeshTests.cpp
blob: 8f4d2305543065571d340c9a8e9d2ea472e5257b (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
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"

#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"
#include "DynamicMesh.h"

SUITE (DynamicMeshTests)
{
	struct DynamicMeshTestFixture
	{
		DynamicMeshTestFixture ()
		{
			data = 0;
			data2 = 2;

			goodPolygon.push_back (Vector3f (0,0,0));
			goodPolygon.push_back (Vector3f (0,0,1));
			goodPolygon.push_back (Vector3f (1,0,1));

			goodPolygonNeighbor.push_back (Vector3f (0,0,0));
			goodPolygonNeighbor.push_back (Vector3f (1,0,1));
			goodPolygonNeighbor.push_back (Vector3f (1,0,0));

			upsideDownPolygon.push_back (Vector3f (0,0,0));
			upsideDownPolygon.push_back (Vector3f (1,0,1));
			upsideDownPolygon.push_back (Vector3f (0,0,1));

			degeneratePolygon.push_back (Vector3f (0,0,0));
			degeneratePolygon.push_back (Vector3f (1,0,0));
			degeneratePolygon.push_back (Vector3f (2,0,0));

			degeneratePolygon2.push_back (Vector3f (0,0,0));
			degeneratePolygon2.push_back (Vector3f (1,0,0));
		}

		DynamicMesh mesh;
		unsigned char data;
		unsigned char data2;

		DynamicMesh::Polygon goodPolygon;
		DynamicMesh::Polygon goodPolygonNeighbor;
		DynamicMesh::Polygon degeneratePolygon;
		DynamicMesh::Polygon degeneratePolygon2;
		DynamicMesh::Polygon upsideDownPolygon;
	};

	// Create a container for DynamicMesh to use for carving.
	// Hull is a simple half-space defined by world-space position and normal.
	DynamicMesh::HullContainer HullsFromNormalAndPosition (const Vector3f& normal, const Vector3f& position)
	{
		Plane plane;
		plane.SetNormalAndPosition (normal, position);

		DynamicMesh::Hull hull;
		hull.push_back (plane);

		DynamicMesh::HullContainer hulls;
		hulls.push_back (hull);

		return hulls;
	}

	TEST_FIXTURE (DynamicMeshTestFixture, Construction)
	{
		CHECK (mesh.PolyCount () == 0);
		CHECK (mesh.VertCount () == 0);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, AddPolygon)
	{
		mesh.AddPolygon (goodPolygon, data);

		CHECK (mesh.PolyCount () == 1);
		CHECK (mesh.VertCount () == 3);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, AddPolygon_IgnoreDegeneratePolygon)
	{
		mesh.AddPolygon (degeneratePolygon, data);
		mesh.AddPolygon (degeneratePolygon2, data);

		CHECK (mesh.PolyCount () == 0);
		CHECK (mesh.VertCount () == 0);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, AddPolygon_IgnoreUpsideDown)
	{
		mesh.AddPolygon (upsideDownPolygon, data);

		CHECK (mesh.PolyCount () == 0);
		CHECK (mesh.VertCount () == 0);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, AddPolygon_SameTwice)
	{
		mesh.AddPolygon (goodPolygon, data);
		mesh.AddPolygon (goodPolygon, data);

		CHECK (mesh.PolyCount () == 2);
		CHECK (mesh.VertCount () == 3);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, MergePolygonsWithSameData)
	{
		mesh.AddPolygon (goodPolygon, data);
		mesh.AddPolygon (goodPolygonNeighbor, data);
		mesh.MergePolygons ();

		CHECK (mesh.PolyCount () == 1);
		CHECK (mesh.VertCount () == 4);
	}

	TEST_FIXTURE (DynamicMeshTestFixture, DontMergePolygonsWithDifferentData)
	{
		mesh.AddPolygon (goodPolygon, data);
		mesh.AddPolygon (goodPolygonNeighbor, data2);
		mesh.MergePolygons ();

		CHECK (mesh.PolyCount () == 2);
		CHECK (mesh.VertCount () == 4);
	}

	// Verify mesh contains exactly one triangle. Return the area weighted normal.
	// ie. vector in direction of the triangle normal - with a magnitude equal to the triangle area.
	Vector3f CheckSingleTriangleGetAreaNormal (DynamicMesh& mesh)
	{
		// Verify a single polygon is left
		CHECK (mesh.PolyCount () == 1);

		// .. and it's a triangle
		const DynamicMesh::Poly* poly = mesh.GetPoly (0);
		CHECK (poly->m_VertexCount == 3);

		const Vector3f v0 = Vector3f (mesh.GetVertex (poly->m_VertexIDs[0]));
		const Vector3f v1 = Vector3f (mesh.GetVertex (poly->m_VertexIDs[1]));
		const Vector3f v2 = Vector3f (mesh.GetVertex (poly->m_VertexIDs[2]));
		const Vector3f triangleAreaNormal = 0.5f*Cross (v1 - v0, v2 - v0);
		return triangleAreaNormal;
	}

	TEST_FIXTURE (DynamicMeshTestFixture, ClipTriangleWithPlane_Result_ClippedTriangle)
	{
		// Cut everything z > 0.5f
		DynamicMesh::HullContainer carveHulls = HullsFromNormalAndPosition (-Vector3f::zAxis, Vector3f (0.0f, 0.0f, 0.5f));

		mesh.AddPolygon (goodPolygon, data);
		mesh.ClipPolys (carveHulls);

		// Expect a triangle in the horizontal plane with area 1/8
		const Vector3f expectedAreaNormal = Vector3f (0.0f, 0.125f, 0.0f);
		const Vector3f triangleAreaNormal = CheckSingleTriangleGetAreaNormal (mesh);

		CHECK (CompareApproximately (expectedAreaNormal, triangleAreaNormal));
	}

	TEST_FIXTURE (DynamicMeshTestFixture, ClipTriangleWithPlane_Result_OriginalTriangle)
	{
		// Cut everything z > 1.0f
		DynamicMesh::HullContainer carveHulls = HullsFromNormalAndPosition (-Vector3f::zAxis, Vector3f (0.0f, 0.0f, 1.0f));

		mesh.AddPolygon (goodPolygon, data);

		mesh.ClipPolys (carveHulls);

		// Expect a triangle in the horizontal plane with area 1/2
		const Vector3f expectedAreaNormal = Vector3f (0.0f, 0.5f, 0.0f);
		const Vector3f triangleAreaNormal = CheckSingleTriangleGetAreaNormal (mesh);
		CHECK (CompareApproximately (expectedAreaNormal, triangleAreaNormal));
	}

	TEST_FIXTURE (DynamicMeshTestFixture, ClipTriangleWithPlane_Result_NoTriangle)
	{
		// Cut everything z > 0
		DynamicMesh::HullContainer carveHulls = HullsFromNormalAndPosition (-Vector3f::zAxis, Vector3f (0.0f, 0.0f, 0.0f));

		mesh.AddPolygon (goodPolygon, data);
		mesh.ClipPolys (carveHulls);

		// Verify that the polygon is removed
		CHECK (mesh.PolyCount () == 0);
	}
	
	TEST_FIXTURE (DynamicMeshTestFixture, SplitTriangleIntoTwoPolygons)
	{
		// Split polygon into two
		Vector3f planePos(0.0f, 0.0f, 0.5f);
		Plane planeLeft, planeRight;
		planeLeft.SetNormalAndPosition (-Vector3f::zAxis, planePos);
		planeRight.SetNormalAndPosition (Vector3f::zAxis, planePos);
		
		DynamicMesh::Hull hull;
		hull.push_back (planeLeft);
		hull.push_back (planeRight);
		
		DynamicMesh::HullContainer carveHulls;
		carveHulls.push_back (hull);
		
		mesh.AddPolygon (goodPolygon, data);
		mesh.ClipPolys (carveHulls);
		
		// Verify that the polygon is cut in half
		CHECK (mesh.PolyCount () == 2);
	}

	static bool HasNeighbor (const DynamicMesh::Poly* poly, int neighborId)
	{
		for (int i = 0; i < poly->m_VertexCount; i++)
			if (poly->m_Neighbours[i] == neighborId)
				return true;
		return false;
	}

	TEST_FIXTURE (DynamicMeshTestFixture, CheckMeshConnectivity)
	{
		mesh.AddPolygon (goodPolygon, data);
		mesh.AddPolygon (goodPolygonNeighbor, data2);
		mesh.MergePolygons ();
		mesh.FindNeighbors ();

		CHECK (mesh.PolyCount () == 2);
		CHECK (mesh.VertCount () == 4);

		// Check that polygon A is connected to polygon B.
		const DynamicMesh::Poly* pa = mesh.GetPoly (0);
		CHECK (HasNeighbor (pa, 2)); // One based neighbor indices.

		// Check that polygon B is connected to polygon A.
		const DynamicMesh::Poly* pb = mesh.GetPoly (1);
		CHECK (HasNeighbor (pb, 1));
	}


	static DynamicMesh::Hull HullFromPolygon (const dynamic_array<Vector3f>& points)
	{
		DynamicMesh::Hull hull;
		for (int i = 0, j = points.size()-1; i < points.size(); j = i++)
		{
			Vector3f edgeDir = points[i] - points[j];
			Vector3f edgeNormal = Normalize (Vector3f (-edgeDir.z, 0.0f, edgeDir.x));
			Plane plane;
			plane.SetNormalAndPosition (edgeNormal, points[j]);
			hull.push_back (plane);
		}
		return hull;
	}

	TEST_FIXTURE (DynamicMeshTestFixture, CutTriangleWithRectangle)
	{
		// Cut triangle with rectangle		
		//
		//  o------o
		//  |  x../..x
		//  |  :/    :
		//  | /:     :
		//  o  x.....x
		//
		dynamic_array<Vector3f> points;
		points.push_back (Vector3f (0.25f, 0, 0));
		points.push_back (Vector3f (0.25f, 0, 0.75f));
		points.push_back (Vector3f (1.0f, 0, 0.75f));
		points.push_back (Vector3f (1.0f, 0, 0));
		DynamicMesh::Hull hull = HullFromPolygon (points);

		DynamicMesh::HullContainer carveHulls;
		carveHulls.push_back (hull);

		mesh.AddPolygon (goodPolygon, data);
		mesh.ClipPolys (carveHulls);
		
		// Verify that there are more polygons after clipping, because result is non-convex.
		CHECK (mesh.PolyCount () > 1);
		// Verify that there are 6 vertices.
		CHECK (mesh.VertCount() == 6);
	}
	
	
}

#endif