public static void testSpatialCollection(ISpatialCollection<AgentType> testingAgents) { Console.WriteLine("Running test for " + testingAgents.GetType().Name); ISpatialCollection<AgentType> baseAgents = new SpatialCollectionAsList<AgentType>(); List<AgentType> agents = new List<AgentType>(); Console.WriteLine("Creating Agents."); for (int i = 0; i < NUM_AGENTS; i++) agents.Add(new AgentType(Random.RandomPoint(min, max))); // DK: For testing/debugging, was using just these 2 points: // agents.Add(new AgentType(new Point3d(1, 1, 1))); // agents.Add(new AgentType(new Point3d(1, 1, 1.01))); Stopwatch stopwatchBase = new Stopwatch(); Stopwatch stopwatchTesting = new Stopwatch(); Console.WriteLine("Getting add time data."); stopwatchBase.Start(); foreach (AgentType agent in agents) { baseAgents.Add(agent); } //baseAgents.Add(new AgentType(new Point3d(min.X - 100, 0, 0))); stopwatchBase.Stop(); stopwatchTesting.Start(); foreach (AgentType agent in agents) { testingAgents.Add(agent); } //testingAgents.Add(new AgentType(new Point3d(min.X - 100, 0, 0))); stopwatchTesting.Stop(); TimeSpan baseAddTime = stopwatchBase.Elapsed; TimeSpan testAddTime = stopwatchTesting.Elapsed; Console.WriteLine("Base time elapsed: {0}", baseAddTime); Console.WriteLine("Testing time elapsed: {0}", testAddTime); Console.WriteLine("Elapsed time ratio: {0}", 1.0 * stopwatchTesting.ElapsedTicks / stopwatchBase.ElapsedTicks); if (CHECKMATCH) // DK: added so we can easily turn on and off this expensive check { Console.WriteLine("Checking neighbors match."); foreach (AgentType agent in agents) { ISpatialCollection<AgentType> testingNeighbors = testingAgents.getNeighborsInSphere(agent, visionRadius); ISpatialCollection<AgentType> baseNeighbors = baseAgents.getNeighborsInSphere(agent, visionRadius); foreach (AgentType neighbor in testingNeighbors) { if (!listContainsByReferenceEquals(neighbor, baseNeighbors)) { Console.WriteLine("Mismatch1! testingNeighbors size: " + testingNeighbors.Count + " baseNeighbors size: " + baseNeighbors.Count); Console.ReadLine(); //throw new Exception(); } } foreach (AgentType neighbor in baseNeighbors) { if (!listContainsByReferenceEquals(neighbor, testingNeighbors)) { Console.WriteLine("Mismatch2! testingNeighbors size: " + testingNeighbors.Count + " baseNeighbors size: " + baseNeighbors.Count); Console.ReadLine(); //throw new Exception(); } } } } Console.WriteLine("Getting getNeighbors timing data."); stopwatchBase.Restart(); foreach (AgentType agent in agents) { ISpatialCollection<AgentType> neighbors = baseAgents.getNeighborsInSphere(agent, visionRadius); } stopwatchBase.Stop(); TimeSpan baseNeighborsTime = stopwatchBase.Elapsed; Console.WriteLine("Base time elapsed: {0}", baseNeighborsTime); stopwatchTesting.Restart(); foreach (AgentType agent in agents) { ISpatialCollection<AgentType> neighbors = testingAgents.getNeighborsInSphere(agent, visionRadius); } stopwatchTesting.Stop(); TimeSpan testNeighborsTime = stopwatchTesting.Elapsed; Console.WriteLine("Testing time elapsed: {0}", testNeighborsTime); Console.WriteLine("Elapsed time ratio: {0}", 1.0 * stopwatchTesting.ElapsedMilliseconds / stopwatchBase.ElapsedMilliseconds); TimeSpan totalBaseTime = baseAddTime.Add(baseNeighborsTime); Console.WriteLine("Total base time: {0}", totalBaseTime); TimeSpan totalTestTime = testAddTime.Add(testNeighborsTime); Console.WriteLine("Total test time: {0}", totalTestTime); Console.WriteLine("Total elapsed time ratio: {0}", 1.0 * totalTestTime.TotalMilliseconds / totalBaseTime.TotalMilliseconds); }
private ISpatialCollection<IQuelea> GetNeighborsInVisionAngle(ISpatialCollection<IQuelea> neighborsInSphere) { ISpatialCollection<IQuelea> neighborsInVisionAngle = new SpatialCollectionAsList<IQuelea>(); Point3d position = agent.Position; Vector3d velocity = agent.Velocity; Plane pl1 = new Plane(position, velocity); pl1.Rotate(-RS.HALF_PI, pl1.YAxis); Plane pl2 = pl1; pl2.Rotate(-RS.HALF_PI, pl1.XAxis); double halfVisionAngle = agent.VisionAngle * visionAngleMultiplier / 2; foreach (IQuelea neighbor in neighborsInSphere) { Vector3d diff = Util.Vector.Vector2Point(position, neighbor.Position); double angle1 = Util.Vector.CalcAngle(velocity, diff, pl1); double angle2 = Util.Vector.CalcAngle(velocity, diff, pl2); if (Util.Number.DefinitelyLessThan(angle1, halfVisionAngle, Constants.AbsoluteTolerance) && Util.Number.DefinitelyLessThan(angle2, halfVisionAngle, Constants.AbsoluteTolerance)) { neighborsInVisionAngle.Add(neighbor); } } return neighborsInVisionAngle; }