File size: 12,430 Bytes
05c9ac2 |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 |
#if MLA_UNITY_PHYSICS_MODULE
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using UnityEngine;
using Unity.MLAgents.Sensors;
namespace Unity.MLAgents.Tests
{
internal class TestBoxOverlapChecker : BoxOverlapChecker
{
public TestBoxOverlapChecker(
Vector3 cellScale,
Vector3Int gridSize,
bool rotateWithAgent,
LayerMask colliderMask,
GameObject centerObject,
GameObject agentGameObject,
string[] detectableTags,
int initialColliderBufferSize,
int maxColliderBufferSize
) : base(
cellScale,
gridSize,
rotateWithAgent,
colliderMask,
centerObject,
agentGameObject,
detectableTags,
initialColliderBufferSize,
maxColliderBufferSize)
{}
public Vector3[] CellLocalPositions
{
get
{
return (Vector3[])typeof(BoxOverlapChecker).GetField("m_CellLocalPositions",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
}
}
public Collider[] ColliderBuffer
{
get
{
return (Collider[])typeof(BoxOverlapChecker).GetField("m_ColliderBuffer",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
}
}
public static TestBoxOverlapChecker CreateChecker(
float cellScaleX = 1f,
float cellScaleZ = 1f,
int gridSizeX = 10,
int gridSizeZ = 10,
bool rotateWithAgent = true,
GameObject centerObject = null,
GameObject agentGameObject = null,
string[] detectableTags = null,
int initialColliderBufferSize = 4,
int maxColliderBufferSize = 500)
{
return new TestBoxOverlapChecker(
new Vector3(cellScaleX, 0.01f, cellScaleZ),
new Vector3Int(gridSizeX, 1, gridSizeZ),
rotateWithAgent,
LayerMask.GetMask("Default"),
centerObject,
agentGameObject,
detectableTags,
initialColliderBufferSize,
maxColliderBufferSize);
}
}
public class BoxOverlapCheckerTests
{
[Test]
public void TestCellLocalPosition()
{
var testGo = new GameObject("test");
testGo.transform.position = Vector3.zero;
var boxOverlapSquare = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo);
var localPos = boxOverlapSquare.CellLocalPositions;
Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f), localPos[0]);
Assert.AreEqual(new Vector3(-4.5f, 0, 4.5f), localPos[9]);
Assert.AreEqual(new Vector3(4.5f, 0, -4.5f), localPos[90]);
Assert.AreEqual(new Vector3(4.5f, 0, 4.5f), localPos[99]);
Object.DestroyImmediate(testGo);
var testGo2 = new GameObject("test");
testGo2.transform.position = new Vector3(3.5f, 8f, 17f); // random, should have no effect on local positions
var boxOverlapRect = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo);
localPos = boxOverlapRect.CellLocalPositions;
Assert.AreEqual(new Vector3(-2f, 0, -7f), localPos[0]);
Assert.AreEqual(new Vector3(-2f, 0, 7f), localPos[14]);
Assert.AreEqual(new Vector3(2f, 0, -7f), localPos[60]);
Assert.AreEqual(new Vector3(2f, 0, 7f), localPos[74]);
Object.DestroyImmediate(testGo2);
}
[Test]
public void TestCellGlobalPositionNoRotate()
{
var testGo = new GameObject("test");
var position = new Vector3(3.5f, 8f, 17f);
testGo.transform.position = position;
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo, centerObject: testGo);
Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(0));
Assert.AreEqual(new Vector3(-4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(9));
Assert.AreEqual(new Vector3(4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(90));
Assert.AreEqual(new Vector3(4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(99));
testGo.transform.Rotate(0, 90, 0); // should have no effect on positions
Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(0));
Assert.AreEqual(new Vector3(-4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(9));
Assert.AreEqual(new Vector3(4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(90));
Assert.AreEqual(new Vector3(4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(99));
Object.DestroyImmediate(testGo);
}
[Test]
public void TestCellGlobalPositionRotate()
{
var testGo = new GameObject("test");
var position = new Vector3(15f, 6f, 13f);
testGo.transform.position = position;
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo, centerObject: testGo);
Assert.AreEqual(new Vector3(-2f, 0, -7f) + position, boxOverlap.GetCellGlobalPosition(0));
Assert.AreEqual(new Vector3(-2f, 0, 7f) + position, boxOverlap.GetCellGlobalPosition(14));
Assert.AreEqual(new Vector3(2f, 0, -7f) + position, boxOverlap.GetCellGlobalPosition(60));
Assert.AreEqual(new Vector3(2f, 0, 7f) + position, boxOverlap.GetCellGlobalPosition(74));
testGo.transform.Rotate(0, 90, 0);
// round to int to ignore numeric errors
Assert.AreEqual(Vector3Int.RoundToInt(new Vector3(-7f, 0, 2f) + position), Vector3Int.RoundToInt(boxOverlap.GetCellGlobalPosition(0)));
Assert.AreEqual(Vector3Int.RoundToInt(new Vector3(7f, 0, 2f) + position), Vector3Int.RoundToInt(boxOverlap.GetCellGlobalPosition(14)));
Assert.AreEqual(Vector3Int.RoundToInt(new Vector3(-7f, 0, -2f) + position), Vector3Int.RoundToInt(boxOverlap.GetCellGlobalPosition(60)));
Assert.AreEqual(Vector3Int.RoundToInt(new Vector3(7f, 0, -2f) + position), Vector3Int.RoundToInt(boxOverlap.GetCellGlobalPosition(74)));
Object.DestroyImmediate(testGo);
}
[Test]
public void TestBufferResize()
{
List<GameObject> testObjects = new List<GameObject>();
var testGo = new GameObject("test");
testGo.transform.position = Vector3.zero;
testObjects.Add(testGo);
var boxOverlap = TestBoxOverlapChecker.CreateChecker(agentGameObject: testGo, centerObject: testGo, initialColliderBufferSize: 2, maxColliderBufferSize: 5);
boxOverlap.Perceive();
Assert.AreEqual(2, boxOverlap.ColliderBuffer.Length);
for (var i = 0; i < 3; i++)
{
var boxGo = new GameObject("test");
boxGo.transform.position = Vector3.zero;
boxGo.AddComponent<BoxCollider>();
testObjects.Add(boxGo);
}
boxOverlap.Perceive();
Assert.AreEqual(4, boxOverlap.ColliderBuffer.Length);
for (var i = 0; i < 2; i++)
{
var boxGo = new GameObject("test");
boxGo.transform.position = Vector3.zero;
boxGo.AddComponent<BoxCollider>();
testObjects.Add(boxGo);
}
boxOverlap.Perceive();
Assert.AreEqual(5, boxOverlap.ColliderBuffer.Length);
Object.DestroyImmediate(testGo);
foreach (var go in testObjects)
{
Object.DestroyImmediate(go);
}
}
[Test]
public void TestParseCollidersClosest()
{
var tag1 = "Player";
List<GameObject> testObjects = new List<GameObject>();
var testGo = new GameObject("test");
testGo.transform.position = Vector3.zero;
var boxOverlap = TestBoxOverlapChecker.CreateChecker(
cellScaleX: 10f,
cellScaleZ: 10f,
gridSizeX: 2,
gridSizeZ: 2,
agentGameObject: testGo,
centerObject: testGo,
detectableTags: new[] { tag1 });
var helper = new VerifyParseCollidersHelper();
boxOverlap.GridOverlapDetectedClosest += helper.DetectedAction;
for (var i = 0; i < 3; i++)
{
var boxGo = new GameObject("test");
boxGo.transform.position = new Vector3(i + 1, 0, 1);
boxGo.AddComponent<BoxCollider>();
boxGo.tag = tag1;
testObjects.Add(boxGo);
}
boxOverlap.Perceive();
helper.Verify(1, new List<GameObject> { testObjects[0] });
Object.DestroyImmediate(testGo);
foreach (var go in testObjects)
{
Object.DestroyImmediate(go);
}
}
[Test]
public void TestParseCollidersAll()
{
var tag1 = "Player";
List<GameObject> testObjects = new List<GameObject>();
var testGo = new GameObject("test");
testGo.transform.position = Vector3.zero;
var boxOverlap = TestBoxOverlapChecker.CreateChecker(
cellScaleX: 10f,
cellScaleZ: 10f,
gridSizeX: 2,
gridSizeZ: 2,
agentGameObject: testGo,
centerObject: testGo,
detectableTags: new[] { tag1 });
var helper = new VerifyParseCollidersHelper();
boxOverlap.GridOverlapDetectedAll += helper.DetectedAction;
for (var i = 0; i < 3; i++)
{
var boxGo = new GameObject("test");
boxGo.transform.position = new Vector3(i + 1, 0, 1);
boxGo.AddComponent<BoxCollider>();
boxGo.tag = tag1;
testObjects.Add(boxGo);
}
boxOverlap.Perceive();
helper.Verify(3, testObjects);
Object.DestroyImmediate(testGo);
foreach (var go in testObjects)
{
Object.DestroyImmediate(go);
}
}
public class VerifyParseCollidersHelper
{
int m_NumInvoked;
List<GameObject> m_ParsedObjects = new List<GameObject>();
public void DetectedAction(GameObject go, int cellIndex)
{
m_NumInvoked += 1;
m_ParsedObjects.Add(go);
}
public void Verify(int expectNumInvoke, List<GameObject> expectedObjects)
{
Assert.AreEqual(expectNumInvoke, m_NumInvoked);
Assert.AreEqual(expectedObjects.Count, m_ParsedObjects.Count);
foreach (var obj in expectedObjects)
{
Assert.Contains(obj, m_ParsedObjects);
}
}
}
[Test]
public void TestOnlyOneChecker()
{
var testGo = new GameObject("test");
testGo.transform.position = Vector3.zero;
var gridSensorComponent = testGo.AddComponent<SimpleTestGridSensorComponent>();
gridSensorComponent.SetComponentParameters(useGridSensorBase: true, useTestingGridSensor: true);
var sensors = gridSensorComponent.CreateSensors();
int numChecker = 0;
foreach (var sensor in sensors)
{
var gridsensor = (GridSensorBase)sensor;
if (gridsensor.m_GridPerception != null)
{
numChecker += 1;
}
}
Assert.AreEqual(1, numChecker);
}
}
}
#endif
|