public virtual void AddParticlesToNeighborsList(Aggregate aggregate) { foreach (var pp in aggregate.Cluster.SelectMany(c => c.PrimaryParticles)) { _kdTree.Add(pp.Position.ToArray(), pp); } }
public void KDTreeTestsAddAndHas() { var root = new KDTree(new Vector2(0, 0)); root.Add(new Vector2(1, 2)); root.Add(new Vector2(3, -4)); root.Add(new Vector2(0, -2)); Debug.Assert(root.Has(new Vector2(1, 2))); Debug.Assert(root.Has(new Vector2(3, -4))); Debug.Assert(root.Has(new Vector2(0, -2))); Debug.Assert(!root.Has(new Vector2(1, -4))); Debug.Assert(!root.Has(new Vector2(1, -2))); Debug.Assert(!root.Has(new Vector2(-1, -2))); }
public void TestTreeWithOneValue() { KDTree tree = new KDTree(); tree.Add(GetGameObject()); DumpTree(tree); }
public void KDTreeTestsFind() { var root = new KDTree(new Vector2(0, 0)); List <Vector2> vs = new List <Vector2> { new Vector2(1, 2), new Vector2(3, -4), new Vector2(0, -2), new Vector2(1, -4), new Vector2(1, -2), new Vector2(-1, -2), }; foreach (Vector2 v in vs) { root.Add(v); } for (int i = 0; i < vs.Count; i++) { var found = root.Find(vs[i]); Debug.Assert(found != null); Debug.Assert(found.Pt == vs[i]); } Debug.Assert(root.Find(new Vector2(-123, 123)) == null); }
/// <summary>The growth phase of growing nodes, killing auxins, and /// creating braches. </summary> /// <param name="activeNodes">A list of nodes / endpts / buds that are /// growing towards auxins.</param> /// <param name="veinNodes">activeNodes, but as a KDTree instead of a list.</param> /// <param name="auxins">All auxins as a KDTree.</param> /// <param name="completedVeins">All vein edges that have been grown.</param> public void GrowNodes(List <Vector2> activeNodes, KDTree veinNodes, KDTree auxins, List <Edge> completedVeins) { var attractorDict = Attractors(veinNodes, auxins); var newActiveNodes = new List <Vector2>(); while (activeNodes.Count > 0) { var node = activeNodes[0]; activeNodes.RemoveAt(0); if (attractorDict.ContainsKey(node)) { if (Random.value < growChance) { var nextEndpt = GrowEndpt(node, attractorDict[node]); completedVeins.Add(new Edge(node, nextEndpt)); newActiveNodes.Add(nextEndpt); veinNodes.Add(nextEndpt); } else { newActiveNodes.Add(node); } } } activeNodes.AddRange(newActiveNodes); }
public void AddToTreeTest() { const int COUNT = 1000; var rng = new Random(120); // Some arbitrary seed ensuring that all sequences for all test runs are identical (and therefore NOT random) var tree = new KDTree <City>(2, City.GetAxisValue, City.GetDistance); tree.RebuildTree(COUNT); // pre-allocate memory to try to avoid use of the sparse leaves in the tree var list = new List <City>(); for (var i = 0; i < COUNT; i++) { var city = new City() { Id = i, Lat = rng.NextDouble() * 10, Lng = rng.NextDouble() * 10 }; tree.Add(city); list.Add(city); } VerifyTreeToList(tree, list); var tree2 = new KDTree <City>(list, 2, City.GetAxisValue, City.GetDistance); VerifyTreeToList(tree2, list); }
public void RegisterInformer(Informer informer) { var position = informer.transform.position; NodesTree.Add(position.ToArray(), informer); NodesArray [(int)position.x / 3, (int)position.z / 3] = new Node(informer, NodeState.Undiscovered); }
// Adds a new record of the Player's movement to the State if it doesn't exist. If it does, then it updates the Action's weight public void addAction(double[] stateVals, GameObjectState moveState) { List <NodeDistance <KDTreeNode <PlayerMoveAction> > > foundActions = actionsAvailable.Nearest(stateVals, GameState.voxel_radius); if (foundActions.Count < 1) { PlayerMoveAction newMove = new PlayerMoveAction("Move", 0.05); newMove.stateChange = moveState; actionsAvailable.Add(stateVals, newMove); addAction(newMove); if (numActionsMade < maxActionsMade) { numActionsMade++; newMove.updateProbabilityFromActionStateCount(numActionsMade); } } else { foreach (var action in foundActions) { if (numActionsMade < maxActionsMade) { numActionsMade++; action.Node.Value.updateProbabilityFromActionStateCount(numActionsMade); } } } }
private void GenerateMarble() { var newMarble = Instantiate(MarblePrefab, new Vector3(Random.value, Random.value, Random.value), Quaternion.identity); newMarble.transform.parent = this.transform; newMarble.transform.position = Random.insideUnitSphere * 100f; _marbles.Add(newMarble); }
public void KDTreeMin() { var origin = new Vector2(0, 0); var root = new KDTree(origin); var v = new Vector2(1, 2); root.Add(v); Debug.Assert(root.Min(0) == origin); Debug.Assert(root.Min(1) == origin); v = new Vector2(-1, 2); root.Add(v); Debug.Assert(root.Min(0) == v); Debug.Assert(root.Min(1) == origin); v = new Vector2(-1, -2); root.Add(v); Debug.Assert(root.Min(0)[0] == -1); Debug.Assert(root.Min(1) == v); }
public void TestGetAttractors() { var a = new Vector2(0, 0); var b = new Vector2(0, 1); var c = new Vector2(0, 2); var d = new Vector2(0, 3); var veinNodes = new KDTree(a); veinNodes.Add(b); veinNodes.Add(c); veinNodes.Add(d); var v = new Vector2(-1, 0.75f); var w = new Vector2(1.75f, 2.25f); var x = new Vector2(-1.5f, 3.25f); var y = new Vector2(-1.5f, 5f); var z = new Vector2(2, 4.5f); var auxins = new KDTree(v); auxins.Add(w); auxins.Add(x); auxins.Add(y); auxins.Add(z); var colonizer = new SpaceColonization(100f, 1f, 1f, 0f, 90f, 1000); var attractors = colonizer.Attractors(veinNodes, auxins); Debug.Assert(!attractors.ContainsKey(a)); Debug.Assert(attractors.ContainsKey(b)); Debug.Assert(attractors[b].Count == 1); Debug.Assert(attractors[b][0] == v); Debug.Assert(attractors.ContainsKey(c)); Debug.Assert(attractors[c].Count == 1); Debug.Assert(attractors[c][0] == w); Debug.Assert(attractors.ContainsKey(d)); Debug.Assert(attractors[d].Count == 3); Debug.Assert(attractors[d][0] == x); Debug.Assert(attractors[d][1] == y); Debug.Assert(attractors[d][2] == z); }
/// <summary> /// Copy construct a map from another. /// </summary> /// <param name="that">Map to copy.</param> public Map(IMap that) { landmarks = new KDTree <Gaussian>(that.Dimensions); Dimensions = that.Dimensions; foreach (var landmark in that) { landmarks.Add(landmark.Mean, landmark); } }
public void KDTreeNearestNeighbor() { var root = new KDTree(new Vector2(0, 0)); root.Add(new Vector2(0, 1)); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i != 0 && j != 0) { root.Add(new Vector2(i, j)); } } } List <Vector2> pts = root.NearestNeighbors(new Rect(0.95f, 0.95f, 1.1f, 1.1f)); Debug.Assert(pts.Count == 4); pts = root.NearestNeighbors(new Rect(0.95f, 0.95f, 2.1f, 2.1f)); Debug.Assert(pts.Count == 9); }
// Attempts to retrieve the ActionState pairing for a particular GameState. If the ActionState is not in the Map, then it is added. private PlayerActionState getActionState(double[] stateVals) { List <NodeDistance <KDTreeNode <PlayerActionState> > > actionStates = actionStateMap.Nearest(stateVals, GameState.voxel_radius * 0.1); if (actionStates.Count < 1) { PlayerActionState newState = new PlayerActionState(new GameState(stateVals)); actionStateMap.Add(stateVals, newState); return(newState); } return(actionStates[0].Node.Value); }
void Start() { tree = new KDTree <PointWithState>(); //Random.InitState(1385); foreach (var i in Enumerable.Range(1, count)) { var v2 = Random.insideUnitCircle * rangeRadius; var pws = new PointWithState(); pws.pos = v2.ToAVec(); managed.Add(pws); tree.Add(pws); } }
//Avoid at all costs! public void RemoveEmpty() { var newNodesTree = new KDTree <Informer>(3); foreach (var kdTreeNode in NodesTree) { if (kdTreeNode.Value != null && kdTreeNode.Value) { newNodesTree.Add(kdTreeNode.Position, kdTreeNode.Value); } } NodesTree = newNodesTree; }
public HashSet <T[]> CreateClusters(bool removeoutliers) { var allPointsDBSCAN = datapoints; var kdtree = new KDTree <Double, T>(2, new DoubleMath()); for (var i = 0; i < allPointsDBSCAN.Length; ++i) { kdtree.Add(allPointsDBSCAN[i].GetPointDataAsArray(), allPointsDBSCAN[i]); } var expandcounter = 0; for (int i = 0; i < allPointsDBSCAN.Length; i++) { var p = allPointsDBSCAN[i]; if (p.isVisited) { continue; } p.isVisited = true; T[] neighborPts = RegionQuery(kdtree, p, epsilon); if (neighborPts.Length < minPts) { p.clusterID = (int)ClusterIDs.Noise; } else { expandcounter++; ExpandCluster(kdtree, p, neighborPts, expandcounter, epsilon, minPts); } } if (removeoutliers) { return(new HashSet <T[]>( allPointsDBSCAN .Where(x => x.clusterID > 0) .GroupBy(x => x.clusterID) .Select(x => x.Select(y => y).ToArray()) )); } else { return(new HashSet <T[]>( allPointsDBSCAN .GroupBy(x => x.clusterID) //All ClusterIDs .Select(x => x.Select(y => y).ToArray()) )); } }
/// <summary> /// Convert a list of Edges into a KDTree. /// </summary> public KDTree EdgesToKDTree(List <Edge> begVein) { if (begVein.Count == 0) { Debug.LogError("Empty List passed to EdgesToKDTree."); } var veinNodes = new KDTree(begVein[0].L); foreach (var veinNode in begVein) { veinNodes.Add(veinNode.R); } return(veinNodes); }
/// <summary> /// Convert a list of Vector2s into a KDTree. /// </summary> public KDTree ListToKDTree(List <Vector2> vs) { if (vs.Count == 0) { Debug.LogError("Empty List passed to ListToKDTree."); } var tree = new KDTree(vs[0]); for (int i = 1; i < vs.Count; i++) { tree.Add(vs[i]); } return(tree); }
public void TestTreeWithThreeValues() { List <Vector3> positions = new List <Vector3>() { new Vector3(5, 1, 3), new Vector3(4, 4, 4), new Vector3(6, 6, 6) }; KDTree tree = new KDTree(); tree.Add(GetGameObjectsAtPos(positions)); DumpTree(tree); }
// Start is called before the first frame update void Start() { int numpoints = 20; kdt = new KDTree(numpoints); for (int i = 0; i < numpoints; i++) { tmp = MakeRandomPoint(); kdt.Add(tmp); } p = MakePoint(1, 1); l = MakeLine(p, tmp); }
public KDTree <BasePiece> GetAllPlayerAliveUnits() { KDTree <BasePiece> allPlayerPieces = new KDTree <BasePiece>(); for (int i = 0; i < _rows; i++) { for (int j = 0; j < _cols; j++) { if (BoardManager.instance.BasePieces[i, j] != null && BoardManager.instance.BasePieces[i, j].isPlayer) { allPlayerPieces.Add(BoardManager.instance.BasePieces[i, j]); } } } return(allPlayerPieces); }
public void Test2DTreeWithSampleData1() { List <Vector2> positions = new List <Vector2>() { new Vector2(30, 40), new Vector2(5, 25), new Vector2(10, 12), new Vector2(70, 70), new Vector2(50, 30), new Vector2(35, 45) }; KDTree tree = new KDTree(2); tree.Add(GetGameObjectsAt2DPos(positions)); DumpTree(tree); }
public void TestKillAuxins() { var colonizer = new SpaceColonization(1f, 1f, 2f, 0f, 90f, 1000); var activeNodes = new List <Vector2>(); activeNodes.Add(Vector2.zero); activeNodes.Add(new Vector2(-1, -1)); var auxins = new KDTree(new Vector2(1, 1)); auxins.Add(new Vector2(7, -2)); Debug.Assert(auxins.Count == 2); colonizer.KillAuxins(activeNodes, auxins); Debug.Assert(auxins.Count == 1); activeNodes.AddRange(auxins.ToList()); colonizer.KillAuxins(activeNodes, auxins); Debug.Assert(auxins.Count == 0); }
protected PaddleActionState addActionState(GameObjectState objState) { double[] stateVals = objState.getStateVals(); GameState state = new GameState(stateVals); state.voxelize(); PaddleActionState actionState = new PaddleActionState(state); actionState.addAction(new PaddleAttackAction("Attack", 0.5)); actionState.addAction(new PaddleDefendAction("Defend", 0.5)); actionState.addAction(new PaddleStopAction("Stop", 0.5)); actionStateMap.Add(state.getVoxel(), actionState); return(actionState); }
public void Test2DTreeWithSampleData2() { List <Vector2> positions = new List <Vector2>() { new Vector2(51, 75), new Vector2(70, 70), new Vector2(25, 40), new Vector2(35, 90), new Vector2(10, 30), new Vector2(55, 1), new Vector2(60, 80), new Vector2(50, 50), new Vector2(1, 10) }; KDTree tree = new KDTree(2); tree.Add(GetGameObjectsAt2DPos(positions)); DumpTree(tree); }
/// <summary> /// Branch a list of active veins in the main grow veins, kill auxins, /// create branches cycle. /// </summary> public void Branch(KDTree veinNodes, KDTree auxins, List <Vector2> activeNodes, List <Edge> completedVeins) { var attractors = Attractors(veinNodes, auxins); var copy = new List <Vector2>(); copy.AddRange(activeNodes); foreach (var node in copy) { if (Random.value < splitChance && attractors.ContainsKey(node)) { var branchedNode = (Avg(attractors[node]) - node).normalized * growDist; branchedNode = rotate(branchedNode, branchAngle); branchedNode += node; completedVeins.Add(new Edge(node, branchedNode)); activeNodes.Add(branchedNode); veinNodes.Add(branchedNode); } } }
private void InitAppearanceManifold() { int w = this.labImage.Width, h = this.labImage.Height; this.manifoldPoints = new ManifoldPoint[w, h]; this.leastWeatheredPoints = new List <ManifoldPoint>(); this.mostWeatheredPoints = new List <ManifoldPoint>(); this.nonWeatheredPoints = new List <ManifoldPoint>(); this.kdTree = new KDTree <ManifoldPoint>(3); // create manifold points for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { Lab lab = this.labImage[j, i]; ManifoldPoint mp = new ManifoldPoint(i * h + j, new Vector3d(lab.X, lab.Y, lab.Z) ); this.manifoldPoints[i, j] = mp; switch (this.userMask[i, j]) { case 1: // least weathered -- left mouse button this.leastWeatheredPoints.Add(mp); break; case 2: // least weathered -- left mouse button this.mostWeatheredPoints.Add(mp); break; case 3: // unused pointes (e.g., shadows, etc) this.nonWeatheredPoints.Add(mp); break; } // fill tree kdTree.Add(mp, mp.Pos.x, mp.Pos.y, mp.Pos.z); } } }
public void KDTreeRm() { var root = new KDTree(new Vector2(0, 0)); List <Vector2> vs = new List <Vector2> { new Vector2(1, 2), new Vector2(3, -4), new Vector2(0, -2), new Vector2(1, -4), new Vector2(1, -2), new Vector2(-1, -2), }; foreach (Vector2 v in vs) { root.Add(v); } foreach (Vector2 v in vs) { Debug.Assert(root.Has(v)); root.Rm(v); Debug.Assert(!root.Has(v)); } }
//reads LAZ, builds KD tree, reads LAZ again and sets color & normal and writes private void EnrichLazWithRGBSAndNormals(string chunk) { var lazReader = new laszip_dll(); var compressed = true; var filePath = LidarFilesSavePath + tempfile2name(chunk); lazReader.laszip_open_reader(filePath, ref compressed); var numberOfPoints = lazReader.header.number_of_point_records; var coordArray = new double[3]; var kdTree = new KDTree(3); if (IncludeNormals) { Console.Write("[{0:hh:mm:ss}] Reading LAZ and building KD tree...", DateTime.Now); for (var pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) { lazReader.laszip_read_point(); lazReader.laszip_get_coordinates(coordArray); kdTree.Add(coordArray); } Console.WriteLine("[DONE] "); } var img = GetOrthophotoImg(); Console.Write("[{0:hh:mm:ss}] Reading and writing LAZ...", DateTime.Now); lazReader.laszip_seek_point(0L);//read from the beginning again lazReader.laszip_open_reader(filePath, ref compressed); var lazWriter = new laszip_dll(); lazWriter.header = lazReader.header; lazWriter.laszip_open_writer(LidarFilesSavePath + current + ".laz", true); for (var pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) { lazReader.laszip_read_point(); lazReader.laszip_get_coordinates(coordArray); lazWriter.point = lazReader.point; int[] pxCoordinates = FindClosestPxCoordinates(coordArray[0], coordArray[1]); int i = (pxCoordinates[0] - _bottomLeftX) * 2; int j = img.Height - 1 - ((pxCoordinates[1] - _bottomLeftY) * 2); //j index of image goes from top to bottom Color color = img.GetPixel(i, j); //binary int value lazReader.point.rgb = new[] { (ushort)(color.R << 8), (ushort)(color.G << 8), (ushort)(color.B << 8), (ushort)0 }; if (IncludeNormals) { var kNeighbours = kdTree.ApproximateNearest(coordArray, 20, 1000); var normal = GetNormal(coordArray, kNeighbours); var xt = (float)normal[0];//xt in LAS is float var yt = (float)normal[1]; var zt = (float)normal[2]; var xtBytes = BitConverter.GetBytes(xt); var ytBytes = BitConverter.GetBytes(yt); var ztBytes = BitConverter.GetBytes(zt); var waveformPacket = lazWriter.point.wave_packet; waveformPacket[17] = xtBytes[0]; waveformPacket[18] = xtBytes[1]; waveformPacket[19] = xtBytes[2]; waveformPacket[20] = xtBytes[3]; waveformPacket[21] = ytBytes[0]; waveformPacket[22] = ytBytes[1]; waveformPacket[23] = ytBytes[2]; waveformPacket[24] = ytBytes[3]; waveformPacket[25] = ztBytes[0]; waveformPacket[26] = ztBytes[1]; waveformPacket[27] = ztBytes[2]; waveformPacket[28] = ztBytes[3]; } lazWriter.laszip_write_point(); } lazReader.laszip_close_reader(); lazWriter.laszip_close_writer(); Console.WriteLine("[DONE]"); }//end readwrite function