public ISpatialCollection <T> getNeighborsInSphere(T item, double r) { double rSquared = r * r; // ISpatialCollection<T> neighbors = new SpatialCollectionAsBinLattice<T>(); IPosition position = (IPosition)item; LinkedList <T> possibleNeighbors = getBin(item); ISpatialCollection <T> neighbors = new SpatialCollectionAsList <T>(); foreach (T other in possibleNeighbors) { // DK: changed this: // IPosition otherPosition = (IPosition)other; // double d = position.getPoint3d().DistanceTo(otherPosition.getPoint3d()); // if (d < r && !Object.ReferenceEquals(item, other)) // { // neighbors.Add(other); // } // to this: if (!Object.ReferenceEquals(item, other)) { Point3d p1 = position.getPoint3d(); Point3d p2 = ((IPosition)other).getPoint3d(); if (Point.DistanceSquared(p1, p2) < rSquared) { neighbors.Add(other); } } } return(neighbors); }
public ISpatialCollection <T> getNeighborsInSphere(T item, double r) { ISpatialCollection <T> neighbors = new SpatialCollectionAsList <T>(); IPosition position = (IPosition)item; foreach (T other in this.spatialObjects) { // DK: changed this: // IPosition otherPosition = (IPosition)other; // double d = position.getPoint3d().DistanceTo(otherPosition.getPoint3d()); // if (d < r && !Object.ReferenceEquals(item, other)) // { // neighbors.Add(other); // } // to this: if (!Object.ReferenceEquals(item, other)) { Point3d p1 = position.getPoint3d(); Point3d p2 = ((IPosition)other).getPoint3d(); if (p1.DistanceSquared(p2) < r * r) { neighbors.Add(other); } } } return(neighbors); }
public ISpatialCollection <T> getNeighborsInSphere(T item, double r) { double rSquared = r * r; // ISpatialCollection<T> neighbors = new SpatialCollectionAsBinLattice3<T>(); IPosition position = (IPosition)item; //LinkedList<T> possibleNeighbors = getBin(item); List <T> possibleNeighbors = getBins(item, r); ISpatialCollection <T> neighbors = new SpatialCollectionAsList <T>(); foreach (T other in possibleNeighbors) { if (!Object.ReferenceEquals(item, other)) { Point3d p1 = position.getPoint3d(); Point3d p2 = ((IPosition)other).getPoint3d(); if (Point.DistanceSquared(p1, p2) < rSquared) { neighbors.Add(other); } } } return(neighbors); }
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); }
public SpatialCollectionAsList(SpatialCollectionAsList <T> collection) { this.spatialObjects = collection.spatialObjects; }