/// <summary> /// Returns closest to position point from array /// </summary> /// <param name="position"></param> /// <param name="points"></param> /// <returns></returns> private Vector3 GetClosestPoint(Vector3 position, Vector3[] points) { var tree = new KDTree(points, 8); var query = new KDQuery(); var results = new List <int>(); query.ClosestPoint(tree, position, results); return(points[results.First()]); }
long QueryClosest() { stopwatch.Reset(); stopwatch.Start(); float3 position = float3One() * 0.5f + RandomInsideUnitSphere(); float radius = 0.25f; results.Clear(); query.ClosestPoint(tree, position, results); stopwatch.Stop(); return(stopwatch.ElapsedMilliseconds); }
void AssociateAttractors() { Profiler.BeginSample("AssociateAttractors"); foreach (Attractor attractor in _attractors) { attractor.isInfluencing.Clear(); attractor.isReached = false; _nodesInAttractionZone.Clear(); // a. Open venation = closest vein node only --------------------------------------------------------------------- _query.ClosestPoint(_nodeTree, attractor.position, _nodesInAttractionZone); // ii. If a vein node is found, associate it by pushing attractor ID to _nodeInfluencedBy if (_nodesInAttractionZone.Count > 0) { Node closestNode = _nodes[_nodesInAttractionZone[0]]; float distance = (attractor.position - closestNode.position).sqrMagnitude; if (distance <= AttractionDistance * AttractionDistance) { closestNode.influencedBy.Add(attractor); if (distance > KillDistance * KillDistance) { attractor.isReached = false; } else { attractor.isReached = true; } } } // b. Closed venation = all vein nodes in relative neighborhood } Profiler.EndSample(); }
private void OnDrawGizmos() { if (query == null) { return; } Vector3 size = 0.2f * Vector3.one; for (int i = 0; i < pointCloud.Length; i++) { Gizmos.DrawCube(pointCloud[i], size); } var resultIndices = new List <int>(); Color markColor = Color.red; markColor.a = 0.5f; Gizmos.color = markColor; switch (QueryType) { case QType.ClosestPoint: { query.ClosestPoint(tree, transform.position, resultIndices); } break; case QType.KNearest: { query.KNearest(tree, transform.position, K, resultIndices); } break; case QType.Radius: { query.Radius(tree, transform.position, Radius, resultIndices); Gizmos.DrawWireSphere(transform.position, Radius); } break; case QType.Interval: { query.Interval(tree, transform.position - IntervalSize / 2f, transform.position + IntervalSize / 2f, resultIndices); Gizmos.DrawWireCube(transform.position, IntervalSize); } break; default: break; } for (int i = 0; i < resultIndices.Count; i++) { Gizmos.DrawCube(pointCloud[resultIndices[i]], 2f * size); } Gizmos.color = Color.green; Gizmos.DrawCube(transform.position, 4f * size); if (DrawQueryNodes) { query.DrawLastQuery(); } }
void Update() { Debug.Log(checkObject.transform.position); for (int i = 0; i < tree.Count; i++) { tree.Points[i].Position += LorenzStep(tree.Points[i].Position) * Time.deltaTime * 0.01f; var gobj = (GameObject)tree.Points[i].UserObject; gobj.GetComponent <Renderer>().material.color = Color.white; } tree.Rebuild(); if (query == null) { return; } Vector3 size = 0.2f * Vector3.one; var resultIndices = new List <int>(); Color markColor = Color.red; markColor.a = 0.5f; Gizmos.color = markColor; switch (QueryType) { case QType.ClosestPoint: { query.ClosestPoint(tree, checkObject.transform.position, resultIndices); } break; case QType.KNearest: { query.KNearest(tree, checkObject.transform.position, K, resultIndices); } break; case QType.Radius: { query.Radius(tree, checkObject.transform.position, Radius, resultIndices); } break; case QType.Interval: { query.Interval(tree, checkObject.transform.position - IntervalSize / 2f, checkObject.transform.position + IntervalSize / 2f, resultIndices); } break; default: break; } for (int i = 0; i < resultIndices.Count; i++) { var gobj = (GameObject)tree.Points[resultIndices[i]].UserObject; gobj.GetComponent <Renderer>().material.color = Color.red; } }