mirror of
https://github.com/Xevion/Rebirth.git
synced 2025-12-09 10:08:19 -06:00
Bobbing, KDTree implementation
This commit is contained in:
73
Assets/Datastructures/KDTree/KDBounds.cs
Normal file
73
Assets/Datastructures/KDTree/KDBounds.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public struct KDBounds {
|
||||
|
||||
public Vector3 min;
|
||||
public Vector3 max;
|
||||
|
||||
public Vector3 size {
|
||||
|
||||
get {
|
||||
return max - min;
|
||||
}
|
||||
}
|
||||
|
||||
// returns unity bounds
|
||||
public Bounds Bounds {
|
||||
|
||||
get {
|
||||
return new Bounds(
|
||||
(min + max) / 2,
|
||||
(max - min)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Vector3 ClosestPoint(Vector3 point) {
|
||||
|
||||
// X axis
|
||||
if(point.x < min.x) point.x = min.x;
|
||||
else
|
||||
if(point.x > max.x) point.x = max.x;
|
||||
|
||||
// Y axis
|
||||
if(point.y < min.y) point.y = min.y;
|
||||
else
|
||||
if(point.y > max.y) point.y = max.y;
|
||||
|
||||
// Z axis
|
||||
if(point.z < min.z) point.z = min.z;
|
||||
else
|
||||
if(point.z > max.z) point.z = max.z;
|
||||
|
||||
return point;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Datastructures/KDTree/KDBounds.cs.meta
Normal file
13
Assets/Datastructures/KDTree/KDBounds.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b92db3ff3e67ee43a15eda23aea87a3
|
||||
timeCreated: 1521907406
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
49
Assets/Datastructures/KDTree/KDNode.cs
Normal file
49
Assets/Datastructures/KDTree/KDNode.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public class KDNode {
|
||||
|
||||
public float partitionCoordinate;
|
||||
public int partitionAxis = -1;
|
||||
|
||||
public KDNode negativeChild;
|
||||
public KDNode positiveChild;
|
||||
|
||||
public int start;
|
||||
public int end;
|
||||
|
||||
public int Count { get { return end - start; } }
|
||||
|
||||
public bool Leaf { get { return partitionAxis == -1; } }
|
||||
|
||||
public KDBounds bounds;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
13
Assets/Datastructures/KDTree/KDNode.cs.meta
Normal file
13
Assets/Datastructures/KDTree/KDNode.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4440ff733ae6bc488c26393873a5950
|
||||
timeCreated: 1520605573
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Datastructures/KDTree/KDQuery.meta
Normal file
8
Assets/Datastructures/KDTree/KDQuery.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3161368cf6450547b9d21f82757ed69
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
136
Assets/Datastructures/KDTree/KDQuery/Base.cs
Normal file
136
Assets/Datastructures/KDTree/KDQuery/Base.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
The object used for querying. This object should be persistent - re-used for querying.
|
||||
Contains internal array for pooling, so that it doesn't generate (too much) garbage.
|
||||
The array never down-sizes, only up-sizes, so the more you use this object, less garbage will it make over time.
|
||||
|
||||
Should be used only by 1 thread,
|
||||
which means each thread should have it's own KDQuery object in order for querying to be thread safe.
|
||||
|
||||
KDQuery can query different KDTrees.
|
||||
*/
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
protected KDQueryNode[] queueArray; // queue array
|
||||
protected Heap.MinHeap<KDQueryNode> minHeap; //heap for k-nearest
|
||||
protected int count = 0; // size of queue
|
||||
protected int queryIndex = 0; // current index at stack
|
||||
|
||||
/// <summary>
|
||||
/// Returns initialized node from stack that also acts as a pool
|
||||
/// The returned reference to node stays in stack
|
||||
/// </summary>
|
||||
/// <returns>Reference to pooled node</returns>
|
||||
private KDQueryNode PushGetQueue() {
|
||||
|
||||
KDQueryNode node = null;
|
||||
|
||||
if (count < queueArray.Length) {
|
||||
|
||||
if (queueArray[count] == null)
|
||||
queueArray[count] = node = new KDQueryNode();
|
||||
else
|
||||
node = queueArray[count];
|
||||
}
|
||||
else {
|
||||
|
||||
// automatic resize of pool
|
||||
Array.Resize(ref queueArray, queueArray.Length * 2);
|
||||
node = queueArray[count] = new KDQueryNode();
|
||||
}
|
||||
|
||||
count++;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
protected void PushToQueue(KDNode node, Vector3 tempClosestPoint) {
|
||||
|
||||
var queryNode = PushGetQueue();
|
||||
queryNode.node = node;
|
||||
queryNode.tempClosestPoint = tempClosestPoint;
|
||||
}
|
||||
|
||||
protected void PushToHeap(KDNode node, Vector3 tempClosestPoint, Vector3 queryPosition) {
|
||||
|
||||
var queryNode = PushGetQueue();
|
||||
queryNode.node = node;
|
||||
queryNode.tempClosestPoint = tempClosestPoint;
|
||||
|
||||
float sqrDist = Vector3.SqrMagnitude(tempClosestPoint - queryPosition);
|
||||
queryNode.distance = sqrDist;
|
||||
minHeap.PushObj(queryNode, sqrDist);
|
||||
}
|
||||
|
||||
protected int LeftToProcess {
|
||||
|
||||
get {
|
||||
return count - queryIndex;
|
||||
}
|
||||
}
|
||||
|
||||
// just gets unprocessed node from stack
|
||||
// increases queryIndex
|
||||
protected KDQueryNode PopFromQueue() {
|
||||
|
||||
var node = queueArray[queryIndex];
|
||||
queryIndex++;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
protected KDQueryNode PopFromHeap() {
|
||||
|
||||
KDQueryNode heapNode = minHeap.PopObj();
|
||||
|
||||
queueArray[queryIndex]= heapNode;
|
||||
queryIndex++;
|
||||
|
||||
return heapNode;
|
||||
}
|
||||
|
||||
protected void Reset() {
|
||||
|
||||
count = 0;
|
||||
queryIndex = 0;
|
||||
minHeap.Clear();
|
||||
}
|
||||
|
||||
public KDQuery(int queryNodesContainersInitialSize = 2048) {
|
||||
queueArray = new KDQueryNode[queryNodesContainersInitialSize];
|
||||
minHeap = new Heap.MinHeap<KDQueryNode>(queryNodesContainersInitialSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
13
Assets/Datastructures/KDTree/KDQuery/Base.cs.meta
Normal file
13
Assets/Datastructures/KDTree/KDQuery/Base.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57b0c8c3d7b727847b235e82d2fbead0
|
||||
timeCreated: 1521924674
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
56
Assets/Datastructures/KDTree/KDQuery/Debug.cs
Normal file
56
Assets/Datastructures/KDTree/KDQuery/Debug.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#define KDTREE_VISUAL_DEBUG
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
// uses gizmos
|
||||
public void DrawLastQuery() {
|
||||
|
||||
Color start = Color.red;
|
||||
Color end = Color.green;
|
||||
|
||||
start.a = 0.25f;
|
||||
end.a = 0.25f;
|
||||
|
||||
for(int i = 0; i < queryIndex; i++) {
|
||||
|
||||
float val = i / (float)queryIndex;
|
||||
|
||||
Gizmos.color = Color.Lerp(end, start, val);
|
||||
|
||||
Bounds b = queueArray[i].node.bounds.Bounds;
|
||||
|
||||
Gizmos.DrawWireCube(b.center, b.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Datastructures/KDTree/KDQuery/Debug.cs.meta
Normal file
11
Assets/Datastructures/KDTree/KDQuery/Debug.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4aac471bf6e30fd49afa5fff399421c0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
Assets/Datastructures/KDTree/KDQuery/KDQueryNode.cs
Normal file
45
Assets/Datastructures/KDTree/KDQuery/KDQueryNode.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public class KDQueryNode {
|
||||
|
||||
public KDNode node;
|
||||
public Vector3 tempClosestPoint;
|
||||
public float distance;
|
||||
|
||||
public KDQueryNode() {
|
||||
|
||||
}
|
||||
|
||||
public KDQueryNode(KDNode node, Vector3 tempClosestPoint) {
|
||||
this.node = node;
|
||||
this.tempClosestPoint = tempClosestPoint;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
13
Assets/Datastructures/KDTree/KDQuery/KDQueryNode.cs.meta
Normal file
13
Assets/Datastructures/KDTree/KDQuery/KDQueryNode.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbe8b45a46d50254494605e93da9d3ed
|
||||
timeCreated: 1521907406
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
144
Assets/Datastructures/KDTree/KDQuery/QueryClosest.cs
Normal file
144
Assets/Datastructures/KDTree/KDQuery/QueryClosest.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
using Heap;
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
public void ClosestPoint(KDTree tree, Vector3 queryPosition, List<int> resultIndices, List<float> resultDistances = null) {
|
||||
|
||||
Reset();
|
||||
|
||||
Vector3[] points = tree.Points;
|
||||
int[] permutation = tree.Permutation;
|
||||
|
||||
if (points.Length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int smallestIndex = 0;
|
||||
/// Smallest Squared Radius
|
||||
float SSR = Single.PositiveInfinity;
|
||||
|
||||
|
||||
var rootNode = tree.RootNode;
|
||||
|
||||
Vector3 rootClosestPoint = rootNode.bounds.ClosestPoint(queryPosition);
|
||||
|
||||
PushToHeap(rootNode, rootClosestPoint, queryPosition);
|
||||
|
||||
KDQueryNode queryNode = null;
|
||||
KDNode node = null;
|
||||
|
||||
int partitionAxis;
|
||||
float partitionCoord;
|
||||
|
||||
Vector3 tempClosestPoint;
|
||||
|
||||
// searching
|
||||
while(minHeap.Count > 0) {
|
||||
|
||||
queryNode = PopFromHeap();
|
||||
|
||||
if(queryNode.distance > SSR)
|
||||
continue;
|
||||
|
||||
node = queryNode.node;
|
||||
|
||||
if(!node.Leaf) {
|
||||
|
||||
partitionAxis = node.partitionAxis;
|
||||
partitionCoord = node.partitionCoordinate;
|
||||
|
||||
tempClosestPoint = queryNode.tempClosestPoint;
|
||||
|
||||
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
|
||||
|
||||
// we already know we are on the side of negative bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
if(node.positiveChild.Count != 0) {
|
||||
|
||||
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
// we already know we are on the side of positive bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
if(node.positiveChild.Count != 0) {
|
||||
|
||||
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
float sqrDist;
|
||||
// LEAF
|
||||
for(int i = node.start; i < node.end; i++) {
|
||||
|
||||
int index = permutation[i];
|
||||
|
||||
sqrDist = Vector3.SqrMagnitude(points[index] - queryPosition);
|
||||
|
||||
if(sqrDist <= SSR) {
|
||||
|
||||
SSR = sqrDist;
|
||||
smallestIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
resultIndices.Add(smallestIndex);
|
||||
|
||||
if(resultDistances != null) {
|
||||
resultDistances.Add(SSR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Datastructures/KDTree/KDQuery/QueryClosest.cs.meta
Normal file
11
Assets/Datastructures/KDTree/KDQuery/QueryClosest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a226a270de4df034986ad64328da4cc2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
151
Assets/Datastructures/KDTree/KDQuery/QueryInterval.cs
Normal file
151
Assets/Datastructures/KDTree/KDQuery/QueryInterval.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
public void Interval(KDTree tree, Vector3 min, Vector3 max, List<int> resultIndices) {
|
||||
|
||||
Reset();
|
||||
|
||||
Vector3[] points = tree.Points;
|
||||
int[] permutation = tree.Permutation;
|
||||
|
||||
var rootNode = tree.RootNode;
|
||||
|
||||
PushToQueue(
|
||||
|
||||
rootNode,
|
||||
rootNode.bounds.ClosestPoint((min + max) / 2)
|
||||
);
|
||||
|
||||
KDQueryNode queryNode = null;
|
||||
KDNode node = null;
|
||||
|
||||
|
||||
// KD search with pruning (don't visit areas which distance is more away than range)
|
||||
// Recursion done on Stack
|
||||
while(LeftToProcess > 0) {
|
||||
|
||||
queryNode = PopFromQueue();
|
||||
node = queryNode.node;
|
||||
|
||||
if(!node.Leaf) {
|
||||
|
||||
int partitionAxis = node.partitionAxis;
|
||||
float partitionCoord = node.partitionCoordinate;
|
||||
|
||||
Vector3 tempClosestPoint = queryNode.tempClosestPoint;
|
||||
|
||||
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
|
||||
|
||||
// we already know we are inside negative bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
// tempClosestPoint is inside negative side
|
||||
// assign it to negativeChild
|
||||
PushToQueue(node.negativeChild, tempClosestPoint);
|
||||
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
// testing other side
|
||||
if(node.positiveChild.Count != 0
|
||||
&& tempClosestPoint[partitionAxis] <= max[partitionAxis]) {
|
||||
|
||||
PushToQueue(node.positiveChild, tempClosestPoint);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// we already know we are inside positive bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
// tempClosestPoint is inside positive side
|
||||
// assign it to positiveChild
|
||||
PushToQueue(node.positiveChild, tempClosestPoint);
|
||||
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
// testing other side
|
||||
if(node.negativeChild.Count != 0
|
||||
&& tempClosestPoint[partitionAxis] >= min[partitionAxis]) {
|
||||
|
||||
PushToQueue(node.negativeChild, tempClosestPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// LEAF
|
||||
|
||||
// testing if node bounds are inside the query interval
|
||||
if(node.bounds.min[0] >= min[0]
|
||||
&& node.bounds.min[1] >= min[1]
|
||||
&& node.bounds.min[2] >= min[2]
|
||||
|
||||
&& node.bounds.max[0] <= max[0]
|
||||
&& node.bounds.max[1] <= max[1]
|
||||
&& node.bounds.max[2] <= max[2]) {
|
||||
|
||||
for(int i = node.start; i < node.end; i++) {
|
||||
|
||||
resultIndices.Add(permutation[i]);
|
||||
}
|
||||
|
||||
}
|
||||
// node is not inside query interval, need to do test on each point separately
|
||||
else {
|
||||
|
||||
for(int i = node.start; i < node.end; i++) {
|
||||
|
||||
int index = permutation[i];
|
||||
|
||||
Vector3 v = points[index];
|
||||
|
||||
if(v[0] >= min[0]
|
||||
&& v[1] >= min[1]
|
||||
&& v[2] >= min[2]
|
||||
|
||||
&& v[0] <= max[0]
|
||||
&& v[1] <= max[1]
|
||||
&& v[2] <= max[2]) {
|
||||
|
||||
resultIndices.Add(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Datastructures/KDTree/KDQuery/QueryInterval.cs.meta
Normal file
11
Assets/Datastructures/KDTree/KDQuery/QueryInterval.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea62a128d2f9e7848ae1c116012e36e1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
160
Assets/Datastructures/KDTree/KDQuery/QueryKNearest.cs
Normal file
160
Assets/Datastructures/KDTree/KDQuery/QueryKNearest.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#define KDTREE_VISUAL_DEBUG
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
using Heap;
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
SortedList<int, KSmallestHeap<int>> _heaps = new SortedList<int, KSmallestHeap<int>>();
|
||||
/// <summary>
|
||||
/// Returns indices to k closest points, and optionaly can return distances
|
||||
/// </summary>
|
||||
/// <param name="tree">Tree to do search on</param>
|
||||
/// <param name="queryPosition">Position</param>
|
||||
/// <param name="k">Max number of points</param>
|
||||
/// <param name="resultIndices">List where resulting indices will be stored</param>
|
||||
/// <param name="resultDistances">Optional list where resulting distances will be stored</param>
|
||||
public void KNearest(KDTree tree, Vector3 queryPosition, int k, List<int> resultIndices, List<float> resultDistances = null) {
|
||||
|
||||
// pooled heap arrays
|
||||
KSmallestHeap<int> kHeap;
|
||||
|
||||
_heaps.TryGetValue(k, out kHeap);
|
||||
|
||||
if(kHeap == null) {
|
||||
|
||||
kHeap = new KSmallestHeap<int>(k);
|
||||
_heaps.Add(k, kHeap);
|
||||
}
|
||||
|
||||
kHeap.Clear();
|
||||
Reset();
|
||||
|
||||
Vector3[] points = tree.Points;
|
||||
int[] permutation = tree.Permutation;
|
||||
|
||||
///Biggest Smallest Squared Radius
|
||||
float BSSR = Single.PositiveInfinity;
|
||||
|
||||
var rootNode = tree.RootNode;
|
||||
|
||||
Vector3 rootClosestPoint = rootNode.bounds.ClosestPoint(queryPosition);
|
||||
|
||||
PushToHeap(rootNode, rootClosestPoint, queryPosition);
|
||||
|
||||
KDQueryNode queryNode = null;
|
||||
KDNode node = null;
|
||||
|
||||
int partitionAxis;
|
||||
float partitionCoord;
|
||||
|
||||
Vector3 tempClosestPoint;
|
||||
|
||||
// searching
|
||||
while(minHeap.Count > 0) {
|
||||
|
||||
queryNode = PopFromHeap();
|
||||
|
||||
if(queryNode.distance > BSSR)
|
||||
continue;
|
||||
|
||||
node = queryNode.node;
|
||||
|
||||
if(!node.Leaf) {
|
||||
|
||||
partitionAxis = node.partitionAxis;
|
||||
partitionCoord = node.partitionCoordinate;
|
||||
|
||||
tempClosestPoint = queryNode.tempClosestPoint;
|
||||
|
||||
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
|
||||
|
||||
// we already know we are on the side of negative bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
if(node.positiveChild.Count != 0) {
|
||||
|
||||
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
// we already know we are on the side of positive bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
PushToHeap(node.positiveChild, tempClosestPoint, queryPosition);
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
if(node.positiveChild.Count != 0) {
|
||||
|
||||
PushToHeap(node.negativeChild, tempClosestPoint, queryPosition);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
float sqrDist;
|
||||
// LEAF
|
||||
for(int i = node.start; i < node.end; i++) {
|
||||
|
||||
int index = permutation[i];
|
||||
|
||||
sqrDist = Vector3.SqrMagnitude(points[index] - queryPosition);
|
||||
|
||||
if(sqrDist <= BSSR) {
|
||||
|
||||
kHeap.PushObj(index, sqrDist);
|
||||
|
||||
if(kHeap.Full) {
|
||||
BSSR = kHeap.HeadValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
kHeap.FlushResult(resultIndices, resultDistances);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Datastructures/KDTree/KDQuery/QueryKNearest.cs.meta
Normal file
11
Assets/Datastructures/KDTree/KDQuery/QueryKNearest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ded2e8ab341672849898f46a7719a7a0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
131
Assets/Datastructures/KDTree/KDQuery/QueryRadius.cs
Normal file
131
Assets/Datastructures/KDTree/KDQuery/QueryRadius.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public partial class KDQuery {
|
||||
|
||||
/// <summary>
|
||||
/// Search by radius method.
|
||||
/// </summary>
|
||||
/// <param name="tree">Tree to do search on</param>
|
||||
/// <param name="queryPosition">Position</param>
|
||||
/// <param name="queryRadius">Radius</param>
|
||||
/// <param name="resultIndices">Initialized list, cleared.</param>
|
||||
public void Radius(KDTree tree, Vector3 queryPosition, float queryRadius, List<int> resultIndices) {
|
||||
|
||||
Reset();
|
||||
|
||||
Vector3[] points = tree.Points;
|
||||
int[] permutation = tree.Permutation;
|
||||
|
||||
float squaredRadius = queryRadius * queryRadius;
|
||||
|
||||
var rootNode = tree.RootNode;
|
||||
|
||||
PushToQueue(rootNode, rootNode.bounds.ClosestPoint(queryPosition));
|
||||
|
||||
KDQueryNode queryNode = null;
|
||||
KDNode node = null;
|
||||
|
||||
// KD search with pruning (don't visit areas which distance is more away than range)
|
||||
// Recursion done on Stack
|
||||
while(LeftToProcess > 0) {
|
||||
|
||||
queryNode = PopFromQueue();
|
||||
node = queryNode.node;
|
||||
|
||||
if(!node.Leaf) {
|
||||
|
||||
int partitionAxis = node.partitionAxis;
|
||||
float partitionCoord = node.partitionCoordinate;
|
||||
|
||||
Vector3 tempClosestPoint = queryNode.tempClosestPoint;
|
||||
|
||||
if((tempClosestPoint[partitionAxis] - partitionCoord) < 0) {
|
||||
|
||||
// we already know we are inside negative bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
// tempClosestPoint is inside negative side
|
||||
// assign it to negativeChild
|
||||
PushToQueue(node.negativeChild, tempClosestPoint);
|
||||
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
float sqrDist = Vector3.SqrMagnitude(tempClosestPoint - queryPosition);
|
||||
|
||||
// testing other side
|
||||
if(node.positiveChild.Count != 0
|
||||
&& sqrDist <= squaredRadius) {
|
||||
|
||||
PushToQueue(node.positiveChild, tempClosestPoint);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// we already know we are inside positive bound/node,
|
||||
// so we don't need to test for distance
|
||||
// push to stack for later querying
|
||||
|
||||
// tempClosestPoint is inside positive side
|
||||
// assign it to positiveChild
|
||||
PushToQueue(node.positiveChild, tempClosestPoint);
|
||||
|
||||
// project the tempClosestPoint to other bound
|
||||
tempClosestPoint[partitionAxis] = partitionCoord;
|
||||
|
||||
float sqrDist = Vector3.SqrMagnitude(tempClosestPoint - queryPosition);
|
||||
|
||||
// testing other side
|
||||
if(node.negativeChild.Count != 0
|
||||
&& sqrDist <= squaredRadius) {
|
||||
|
||||
PushToQueue(node.negativeChild, tempClosestPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// LEAF
|
||||
for(int i = node.start; i < node.end; i++) {
|
||||
|
||||
int index = permutation[i];
|
||||
|
||||
if(Vector3.SqrMagnitude(points[index] - queryPosition) <= squaredRadius) {
|
||||
|
||||
resultIndices.Add(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Datastructures/KDTree/KDQuery/QueryRadius.cs.meta
Normal file
11
Assets/Datastructures/KDTree/KDQuery/QueryRadius.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17086d61d13921a4ba22e206ef32e74f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
459
Assets/Datastructures/KDTree/KDTree.cs
Normal file
459
Assets/Datastructures/KDTree/KDTree.cs
Normal file
@@ -0,0 +1,459 @@
|
||||
/*MIT License
|
||||
|
||||
Copyright(c) 2018 Vili Volčini / viliwonka
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
// change to !KDTREE_DUPLICATES
|
||||
// if you know for sure you will not use duplicate coordinates (all unique)
|
||||
#define KDTREE_DUPLICATES
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DataStructures.ViliWonka.KDTree {
|
||||
|
||||
public class KDTree {
|
||||
|
||||
public KDNode RootNode { get; private set; }
|
||||
|
||||
public Vector3[] Points { get { return points; } } // points on which kd-tree will build on. This array will stay unchanged when re/building kdtree!
|
||||
private Vector3[] points;
|
||||
|
||||
public int[] Permutation { get { return permutation; } } // index aray, that will be permuted
|
||||
private int[] permutation;
|
||||
|
||||
public int Count { get; private set; }
|
||||
|
||||
private int maxPointsPerLeafNode = 32;
|
||||
|
||||
private KDNode[] kdNodesStack;
|
||||
private int kdNodesCount = 0;
|
||||
|
||||
public KDTree(int maxPointsPerLeafNode = 32) {
|
||||
|
||||
Count = 0;
|
||||
points = new Vector3[0];
|
||||
permutation = new int[0];
|
||||
|
||||
kdNodesStack = new KDNode[64];
|
||||
|
||||
this.maxPointsPerLeafNode = maxPointsPerLeafNode;
|
||||
}
|
||||
|
||||
public KDTree(Vector3[] points, int maxPointsPerLeafNode = 32) {
|
||||
|
||||
this.points = points;
|
||||
this.permutation = new int[points.Length];
|
||||
|
||||
Count = points.Length;
|
||||
kdNodesStack = new KDNode[64];
|
||||
|
||||
this.maxPointsPerLeafNode = maxPointsPerLeafNode;
|
||||
|
||||
Rebuild();
|
||||
}
|
||||
|
||||
public void Build(Vector3[] newPoints, int maxPointsPerLeafNode = -1) {
|
||||
|
||||
SetCount(newPoints.Length);
|
||||
|
||||
for(int i = 0; i < Count; i++) {
|
||||
points[i] = newPoints[i];
|
||||
}
|
||||
|
||||
Rebuild(maxPointsPerLeafNode);
|
||||
}
|
||||
|
||||
public void Build(List<Vector3> newPoints, int maxPointsPerLeafNode = -1) {
|
||||
|
||||
SetCount(newPoints.Count);
|
||||
|
||||
for(int i = 0; i < Count; i++) {
|
||||
points[i] = newPoints[i];
|
||||
}
|
||||
|
||||
Rebuild(maxPointsPerLeafNode);
|
||||
}
|
||||
|
||||
public void Rebuild(int maxPointsPerLeafNode = -1) {
|
||||
|
||||
for(int i = 0; i < Count; i++) {
|
||||
permutation[i] = i;
|
||||
}
|
||||
|
||||
if(maxPointsPerLeafNode > 0) {
|
||||
this.maxPointsPerLeafNode = maxPointsPerLeafNode;
|
||||
}
|
||||
|
||||
BuildTree();
|
||||
}
|
||||
|
||||
public void SetCount(int newSize) {
|
||||
|
||||
Count = newSize;
|
||||
// upsize internal arrays
|
||||
if(Count > points.Length) {
|
||||
|
||||
Array.Resize(ref points, Count);
|
||||
Array.Resize(ref permutation, Count);
|
||||
}
|
||||
}
|
||||
|
||||
void BuildTree() {
|
||||
|
||||
ResetKDNodeStack();
|
||||
|
||||
RootNode = GetKDNode();
|
||||
RootNode.bounds = MakeBounds();
|
||||
RootNode.start = 0;
|
||||
RootNode.end = Count;
|
||||
|
||||
SplitNode(RootNode);
|
||||
}
|
||||
|
||||
KDNode GetKDNode() {
|
||||
|
||||
KDNode node = null;
|
||||
|
||||
if(kdNodesCount < kdNodesStack.Length) {
|
||||
|
||||
if(kdNodesStack[kdNodesCount] == null) {
|
||||
kdNodesStack[kdNodesCount] = node = new KDNode();
|
||||
}
|
||||
else {
|
||||
node = kdNodesStack[kdNodesCount];
|
||||
node.partitionAxis = -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// automatic resize of KDNode pool array
|
||||
Array.Resize(ref kdNodesStack, kdNodesStack.Length * 2);
|
||||
node = kdNodesStack[kdNodesCount] = new KDNode();
|
||||
}
|
||||
|
||||
kdNodesCount++;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void ResetKDNodeStack() {
|
||||
kdNodesCount = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For calculating root node bounds
|
||||
/// </summary>
|
||||
/// <returns>Boundary of all Vector3 points</returns>
|
||||
KDBounds MakeBounds() {
|
||||
|
||||
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
|
||||
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
|
||||
|
||||
int even = Count & ~1; // calculate even Length
|
||||
|
||||
// min, max calculations
|
||||
// 3n/2 calculations instead of 2n
|
||||
for (int i0 = 0; i0 < even; i0 += 2) {
|
||||
|
||||
int i1 = i0 + 1;
|
||||
|
||||
// X Coords
|
||||
if (points[i0].x > points[i1].x) {
|
||||
// i0 is bigger, i1 is smaller
|
||||
if (points[i1].x < min.x)
|
||||
min.x = points[i1].x;
|
||||
|
||||
if (points[i0].x > max.x)
|
||||
max.x = points[i0].x;
|
||||
}
|
||||
else {
|
||||
// i1 is smaller, i0 is bigger
|
||||
if (points[i0].x < min.x)
|
||||
min.x = points[i0].x;
|
||||
|
||||
if (points[i1].x > max.x)
|
||||
max.x = points[i1].x;
|
||||
}
|
||||
|
||||
// Y Coords
|
||||
if (points[i0].y > points[i1].y) {
|
||||
// i0 is bigger, i1 is smaller
|
||||
if (points[i1].y < min.y)
|
||||
min.y = points[i1].y;
|
||||
|
||||
if (points[i0].y > max.y)
|
||||
max.y = points[i0].y;
|
||||
}
|
||||
else {
|
||||
// i1 is smaller, i0 is bigger
|
||||
if (points[i0].y < min.y)
|
||||
min.y = points[i0].y;
|
||||
|
||||
if (points[i1].y > max.y)
|
||||
max.y = points[i1].y;
|
||||
}
|
||||
|
||||
// Z Coords
|
||||
if (points[i0].z > points[i1].z) {
|
||||
// i0 is bigger, i1 is smaller
|
||||
if (points[i1].z < min.z)
|
||||
min.z = points[i1].z;
|
||||
|
||||
if (points[i0].z > max.z)
|
||||
max.z = points[i0].z;
|
||||
}
|
||||
else {
|
||||
// i1 is smaller, i0 is bigger
|
||||
if (points[i0].z < min.z)
|
||||
min.z = points[i0].z;
|
||||
|
||||
if (points[i1].z > max.z)
|
||||
max.z = points[i1].z;
|
||||
}
|
||||
}
|
||||
|
||||
// if array was odd, calculate also min/max for the last element
|
||||
if(even != Count) {
|
||||
// X
|
||||
if (min.x > points[even].x)
|
||||
min.x = points[even].x;
|
||||
|
||||
if (max.x < points[even].x)
|
||||
max.x = points[even].x;
|
||||
// Y
|
||||
if (min.y > points[even].y)
|
||||
min.y = points[even].y;
|
||||
|
||||
if (max.y < points[even].y)
|
||||
max.y = points[even].y;
|
||||
// Z
|
||||
if (min.z > points[even].z)
|
||||
min.z = points[even].z;
|
||||
|
||||
if (max.z < points[even].z)
|
||||
max.z = points[even].z;
|
||||
}
|
||||
|
||||
KDBounds b = new KDBounds();
|
||||
b.min = min;
|
||||
b.max = max;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursive splitting procedure
|
||||
/// </summary>
|
||||
/// <param name="parent">This is where root node goes</param>
|
||||
/// <param name="depth"></param>
|
||||
///
|
||||
void SplitNode(KDNode parent) {
|
||||
|
||||
// center of bounding box
|
||||
KDBounds parentBounds = parent.bounds;
|
||||
Vector3 parentBoundsSize = parentBounds.size;
|
||||
|
||||
// Find axis where bounds are largest
|
||||
int splitAxis = 0;
|
||||
float axisSize = parentBoundsSize.x;
|
||||
|
||||
if (axisSize < parentBoundsSize.y) {
|
||||
splitAxis = 1;
|
||||
axisSize = parentBoundsSize.y;
|
||||
}
|
||||
|
||||
if (axisSize < parentBoundsSize.z) {
|
||||
splitAxis = 2;
|
||||
}
|
||||
|
||||
// Our axis min-max bounds
|
||||
float boundsStart = parentBounds.min[splitAxis];
|
||||
float boundsEnd = parentBounds.max[splitAxis];
|
||||
|
||||
// Calculate the spliting coords
|
||||
float splitPivot = CalculatePivot(parent.start, parent.end, boundsStart, boundsEnd, splitAxis);
|
||||
|
||||
parent.partitionAxis = splitAxis;
|
||||
parent.partitionCoordinate = splitPivot;
|
||||
|
||||
// 'Spliting' array to two subarrays
|
||||
int splittingIndex = Partition(parent.start, parent.end, splitPivot, splitAxis);
|
||||
|
||||
// Negative / Left node
|
||||
Vector3 negMax = parentBounds.max;
|
||||
negMax[splitAxis] = splitPivot;
|
||||
|
||||
KDNode negNode = GetKDNode();
|
||||
negNode.bounds = parentBounds;
|
||||
negNode.bounds.max = negMax;
|
||||
negNode.start = parent.start;
|
||||
negNode.end = splittingIndex;
|
||||
parent.negativeChild = negNode;
|
||||
|
||||
// Positive / Right node
|
||||
Vector3 posMin = parentBounds.min;
|
||||
posMin[splitAxis] = splitPivot;
|
||||
|
||||
KDNode posNode = GetKDNode();
|
||||
posNode.bounds = parentBounds;
|
||||
posNode.bounds.min = posMin;
|
||||
posNode.start = splittingIndex;
|
||||
posNode.end = parent.end;
|
||||
parent.positiveChild = posNode;
|
||||
|
||||
// check if we are actually splitting it anything
|
||||
// this if check enables duplicate coordinates, but makes construction a bit slower
|
||||
#if KDTREE_DUPLICATES
|
||||
if(negNode.Count != 0 && posNode.Count != 0) {
|
||||
#endif
|
||||
// Constraint function deciding if split should be continued
|
||||
if(ContinueSplit(negNode))
|
||||
SplitNode(negNode);
|
||||
|
||||
|
||||
if(ContinueSplit(posNode))
|
||||
SplitNode(posNode);
|
||||
|
||||
#if KDTREE_DUPLICATES
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sliding midpoint splitting pivot calculation
|
||||
/// 1. First splits node to two equal parts (midPoint)
|
||||
/// 2. Checks if elements are in both sides of splitted bounds
|
||||
/// 3a. If they are, just return midPoint
|
||||
/// 3b. If they are not, then points are only on left or right bound.
|
||||
/// 4. Move the splitting pivot so that it shrinks part with points completely (calculate min or max dependent) and return.
|
||||
/// </summary>
|
||||
/// <param name="start"></param>
|
||||
/// <param name="end"></param>
|
||||
/// <param name="boundsStart"></param>
|
||||
/// <param name="boundsEnd"></param>
|
||||
/// <param name="axis"></param>
|
||||
/// <returns></returns>
|
||||
float CalculatePivot(int start, int end, float boundsStart, float boundsEnd, int axis) {
|
||||
|
||||
//! sliding midpoint rule
|
||||
float midPoint = (boundsStart + boundsEnd) / 2f;
|
||||
|
||||
bool negative = false;
|
||||
bool positive = false;
|
||||
|
||||
float negMax = Single.MinValue;
|
||||
float posMin = Single.MaxValue;
|
||||
|
||||
// this for loop section is used both for sorted and unsorted data
|
||||
for (int i = start; i < end; i++) {
|
||||
|
||||
if (points[permutation[i]][axis] < midPoint)
|
||||
negative = true;
|
||||
else
|
||||
positive = true;
|
||||
|
||||
if (negative == true && positive == true)
|
||||
return midPoint;
|
||||
}
|
||||
|
||||
if (negative) {
|
||||
|
||||
for (int i = start; i < end; i++)
|
||||
if (negMax < points[permutation[i]][axis])
|
||||
negMax = points[permutation[i]][axis];
|
||||
|
||||
return negMax;
|
||||
}
|
||||
else {
|
||||
|
||||
for (int i = start; i < end; i++)
|
||||
if (posMin > points[permutation[i]][axis])
|
||||
posMin = points[permutation[i]][axis];
|
||||
|
||||
return posMin;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Similar to Hoare partitioning algorithm (used in Quick Sort)
|
||||
/// Modification: pivot is not left-most element but is instead argument of function
|
||||
/// Calculates splitting index and partially sorts elements (swaps them until they are on correct side - depending on pivot)
|
||||
/// Complexity: O(n)
|
||||
/// </summary>
|
||||
/// <param name="start">Start index</param>
|
||||
/// <param name="end">End index</param>
|
||||
/// <param name="partitionPivot">Pivot that decides boundary between left and right</param>
|
||||
/// <param name="axis">Axis of this pivoting</param>
|
||||
/// <returns>
|
||||
/// Returns splitting index that subdivides array into 2 smaller arrays
|
||||
/// left = [start, pivot),
|
||||
/// right = [pivot, end)
|
||||
/// </returns>
|
||||
int Partition(int start, int end, float partitionPivot, int axis) {
|
||||
|
||||
// note: increasing right pointer is actually decreasing!
|
||||
int LP = start - 1; // left pointer (negative side)
|
||||
int RP = end; // right pointer (positive side)
|
||||
|
||||
int temp; // temporary var for swapping permutation indexes
|
||||
|
||||
while (true) {
|
||||
|
||||
do {
|
||||
// move from left to the right until "out of bounds" value is found
|
||||
LP++;
|
||||
}
|
||||
while (LP < RP && points[permutation[LP]][axis] < partitionPivot);
|
||||
|
||||
do {
|
||||
// move from right to the left until "out of bounds" value found
|
||||
RP--;
|
||||
}
|
||||
while (LP < RP && points[permutation[RP]][axis] >= partitionPivot);
|
||||
|
||||
if (LP < RP) {
|
||||
// swap
|
||||
temp = permutation[LP];
|
||||
permutation[LP] = permutation[RP];
|
||||
permutation[RP] = temp;
|
||||
}
|
||||
else {
|
||||
|
||||
return LP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constraint function. You can add custom constraints here - if you have some other data/classes binded to Vector3 points
|
||||
/// Can hardcode it into
|
||||
/// </summary>
|
||||
/// <param name="node"></param>
|
||||
/// <returns></returns>
|
||||
bool ContinueSplit(KDNode node) {
|
||||
|
||||
return (node.Count > maxPointsPerLeafNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Datastructures/KDTree/KDTree.cs.meta
Normal file
13
Assets/Datastructures/KDTree/KDTree.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 437ce4903d14e2f4182fad0f2ac92678
|
||||
timeCreated: 1520696434
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user