Skip to content

Commit a09f1d9

Browse files
committed
Work in progress
1 parent 368f1d6 commit a09f1d9

File tree

3 files changed

+31
-90
lines changed

3 files changed

+31
-90
lines changed

Assets/Scripts/model/util/BurstSpatial.cs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,70 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Linq;
1718
using com.google.apps.peltzer.client.model.core;
1819
using com.google.apps.peltzer.client.model.util;
1920
using NativeTrees;
21+
using Unity.Collections;
2022
using UnityEngine;
2123

2224
/// <summary>
2325
/// Class wrapping NativeTrees functions which implement our collision system.
2426
/// </summary>
2527
public static class BurstSpatialFunction
2628
{
27-
private static List<NativeOctree<int>> _indices = new();
29+
private static List<NativeOctree<int>> _octrees = new();
2830

2931
public static int AllocSpatialPartitioner()
3032
{
3133
var octree = new NativeOctree<int>();
32-
_indices.Add(octree);
33-
return _indices.Count - 1;
34+
_octrees.Add(octree);
35+
return _octrees.Count - 1;
3436
}
3537

3638
public static void SpatialPartitionerAddItem(int idx, int itemId, Vector3 center, Vector3 extents)
3739
{
38-
_indices[idx].InsertPoint(itemId, center);
40+
_octrees[idx].InsertPoint(itemId, center);
3941
}
4042

4143
public static void SpatialPartitionerUpdateAll(int idx, object itemIds)
4244
{
4345
var octree = new NativeOctree<int>();
44-
_indices[idx] = _indices[idx];
46+
octree = _octrees[idx];
47+
jgtuu
4548
}
4649

47-
public static void SpatialPartitionerRemoveItem(int idx, int itemId)
50+
public static int SpatialPartitionerIntersectedBy(int idx, Vector3 testCenter, Vector3 testExtents, int[] returnArray, int returnArrayMaxSize)
4851
{
49-
throw new NotImplementedException();
52+
AABB range = new AABB(testCenter, testExtents);
53+
var results = new NativeParallelHashSet<int>(returnArrayMaxSize, Allocator.TempJob);
54+
RangeAABBUnique(_octrees[idx], range, results);
55+
returnArray = results.ToArray();
5056
}
5157

52-
public static int SpatialPartitionerContainedBy(int idx, Vector3 testCenter, Vector3 testExtents, int[] returnArray, int returnArrayMaxSize)
58+
public static void RangeAABBUnique<T>(this NativeOctree<T> octree, AABB range, NativeParallelHashSet<T> results)
59+
where T : unmanaged, IEquatable<T>
5360
{
54-
throw new NotImplementedException();
55-
}
61+
var vistor = new RangeAABBUniqueVisitor<T>()
62+
{
63+
results = results
64+
};
5665

57-
public static int SpatialPartitionerIntersectedBy(int idx, Vector3 testCenter, Vector3 testExtents, int[] returnArray, int returnArrayMaxSize)
58-
{
59-
throw new NotImplementedException();
66+
octree.Range(range, ref vistor);
6067
}
6168

62-
public static int HasItem(int idx, int itemHandle)
69+
struct RangeAABBUniqueVisitor<T> : IOctreeRangeVisitor<T> where T : unmanaged, IEquatable<T>
6370
{
64-
throw new NotImplementedException();
71+
public NativeParallelHashSet<T> results;
72+
73+
public bool OnVisit(T obj, AABB objBounds, AABB queryRange)
74+
{
75+
// check if our object's AABB overlaps with the query AABB
76+
if (objBounds.Overlaps(queryRange))
77+
results.Add(obj);
78+
79+
return true; // always keep iterating, we want to catch all objects
80+
}
6581
}
6682
}
6783

@@ -97,19 +113,6 @@ public void Add(T item, Bounds bounds)
97113
itemBounds[item] = bounds;
98114
BurstSpatialFunction.SpatialPartitionerAddItem(spatialPartitionId, id, bounds.center, bounds.extents);
99115
}
100-
public void UpdateItemBounds(T item, Bounds bounds)
101-
{
102-
throw new NotImplementedException();
103-
}
104-
105-
/// <summary>
106-
/// Updates the bounding box of an item already in the collision system.
107-
/// </summary>
108-
// public void UpdateItemBounds(T item, Bounds bounds)
109-
// {
110-
// itemIds[item].b
111-
// BurstSpatialFunction.SpatialPartitionerUpdateAll(spatialPartitionId, itemIds);
112-
// }
113116

114117
/// <summary>
115118
/// Remove an item from the system.
@@ -126,29 +129,6 @@ public void Remove(T item)
126129
BurstSpatialFunction.SpatialPartitionerUpdateAll(spatialPartitionId, itemIds);
127130
}
128131

129-
/// <summary>
130-
/// Find items contained entirely within the given bounds.
131-
/// This method will create a set when the number of items
132-
/// is greater than zero.
133-
/// </summary>
134-
/// <param name="bounds">Containing bounds.</param>
135-
/// <param name="items">Set of items found. Null when this
136-
/// method returns false.</param>
137-
/// <param name="limit">Maximum number of items to find.</param>
138-
/// <returns>true if any items are found.</returns>
139-
public bool ContainedBy(Bounds bounds, out HashSet<T> items,
140-
int limit = SpatialIndex.MAX_INTERSECT_RESULTS)
141-
{
142-
int numResults = BurstSpatialFunction.SpatialPartitionerContainedBy(spatialPartitionId, bounds.center,
143-
bounds.extents, results, limit);
144-
items = new HashSet<T>();
145-
for (int i = 0; i < numResults; i++)
146-
{
147-
items.Add(idsToItems[results[i]]);
148-
}
149-
return items.Count > 0;
150-
}
151-
152132
/// <summary>
153133
/// Returns whether the item is tracked in this system.
154134
/// </summary>

Assets/Scripts/model/util/CollisionSystem.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ public interface CollisionSystem<T>
3636
/// Thrown when bounds is not contained by the CollisionSystem's bounds.</exception>
3737
void Add(T item, Bounds bounds);
3838

39-
/// <summary>
40-
/// Update the bounds of an item. The item must already exist
41-
/// in the index.
42-
/// </summary>
43-
/// <param name="item">The item to update.</param>
44-
/// <param name="bounds">The item's updated bounds.</param>
45-
/// <exception cref="System.Exception">
46-
/// Thrown when the item isn't in the tree.</exception>
47-
void UpdateItemBounds(T item, Bounds bounds);
48-
4939
/// <summary>
5040
/// Remove an item from the index.
5141
/// </summary>
@@ -54,19 +44,6 @@ public interface CollisionSystem<T>
5444
/// Thrown when the item isn't in the tree.</exception>
5545
void Remove(T item);
5646

57-
/// <summary>
58-
/// Find items contained entirely within the given bounds.
59-
/// This method will create a set when the number of items
60-
/// is greater than zero.
61-
/// </summary>
62-
/// <param name="bounds">Containing bounds.</param>
63-
/// <param name="items">Set of items found. Null when this
64-
/// method returns false.</param>
65-
/// <param name="limit">Maximum number of items to find.</param>
66-
/// <returns>true if any items are found.</returns>
67-
bool ContainedBy(Bounds bounds, out HashSet<T> items,
68-
int limit = SpatialIndex.MAX_INTERSECT_RESULTS);
69-
7047
/// <summary>
7148
/// Find items that intersect the given bounds.
7249
/// This method will create a Set when the number of items

Assets/Scripts/model/util/OctreeImpl.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,6 @@ public void Add(T item, Bounds bounds)
6666
itemNode[item] = node;
6767
}
6868

69-
/// <summary>
70-
/// Update the bounds of an item. The item must already exist
71-
/// in the index.
72-
/// </summary>
73-
/// <param name="item">The item to update.</param>
74-
/// <param name="bounds">The item's updated bounds.</param>
75-
/// <exception cref="System.Exception">
76-
/// Thrown when the item isn't in the tree.</exception>
77-
public void UpdateItemBounds(T item, Bounds bounds)
78-
{
79-
OTNode oldNode = itemNode[item];
80-
oldNode.Remove(item);
81-
itemBounds[item] = bounds;
82-
itemNode[item] = root.Add(item, bounds);
83-
}
84-
8569
/// <summary>
8670
/// Remove an item from the index.
8771
/// </summary>

0 commit comments

Comments
 (0)