Skip to content

Commit

Permalink
Added to standalone repository
Browse files Browse the repository at this point in the history
  • Loading branch information
RossRH committed Dec 31, 2019
1 parent cd3e9ca commit 67a4af7
Show file tree
Hide file tree
Showing 20 changed files with 1,855 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Adapters.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions Adapters/BVHGameObjectAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace DataStructures
{
public class BVHGameObjectAdapter : IBVHNodeAdapter<GameObject>
{
private BVH<GameObject> _bvh;
Dictionary<GameObject, BVHNode<GameObject>> gameObjectToLeafMap = new Dictionary<GameObject, BVHNode<GameObject>>();
private event Action<GameObject> _onPositionOrSizeChanged;

BVH<GameObject> IBVHNodeAdapter<GameObject>.BVH
{
get
{
return _bvh;
}
set
{
_bvh = value;
}
}

//TODO: this is not used?
public void CheckMap(GameObject obj)
{
if (!gameObjectToLeafMap.ContainsKey(obj))
{
throw new Exception("missing map for shuffled child");
}
}

public BVHNode<GameObject> GetLeaf(GameObject obj)
{
return gameObjectToLeafMap[obj];
}

public Vector3 GetObjectPos(GameObject obj)
{
return obj.transform.position;
}

public float GetRadius(GameObject obj)
{
Bounds encapsulatingBounds = GetBounds(obj);
return Mathf.Max(Mathf.Max(encapsulatingBounds.extents.x, encapsulatingBounds.extents.y), encapsulatingBounds.extents.z);
}

private Bounds GetBounds(GameObject obj)
{
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
Bounds encapsulatingBounds = new Bounds(GetObjectPos(obj), Vector3.zero);
foreach (Renderer renderer in renderers)
{
encapsulatingBounds.Encapsulate(renderer.bounds.max);
encapsulatingBounds.Encapsulate(renderer.bounds.min);
}
return encapsulatingBounds;
}

public void MapObjectToBVHLeaf(GameObject obj, BVHNode<GameObject> leaf)
{
gameObjectToLeafMap[obj] = leaf;
}

// this allows us to be notified when an object moves, so we can adjust the BVH
public void OnPositionOrSizeChanged(GameObject changed)
{
// the SSObject has changed, so notify the BVH leaf to refit for the object
gameObjectToLeafMap[changed].RefitObjectChanged(this, changed);
}

public void UnmapObject(GameObject obj)
{
gameObjectToLeafMap.Remove(obj);
}
}
}
11 changes: 11 additions & 0 deletions Adapters/BVHGameObjectAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions Adapters/BVHSphereAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace DataStructures
{
public interface ISphere
{
Vector3 Position
{
get;
}

float Radius
{
get;
}
}

public class BVHParticleDataAdapter : IBVHNodeAdapter<ISphere>
{
private BVH<ISphere> _bvh;
Dictionary<ISphere, BVHNode<ISphere>> gameObjectToLeafMap = new Dictionary<ISphere, BVHNode<ISphere>>();
private event Action<ISphere> _onPositionOrSizeChanged;

BVH<ISphere> IBVHNodeAdapter<ISphere>.BVH
{
get
{
return _bvh;
}
set
{
_bvh = value;
}
}

//TODO: this is not used?
public void CheckMap(ISphere particle)
{
if (!gameObjectToLeafMap.ContainsKey(particle))
{
throw new Exception("missing map for shuffled child");
}
}

public BVHNode<ISphere> GetLeaf(ISphere particle)
{
return gameObjectToLeafMap[particle];
}

public Vector3 GetObjectPos(ISphere particle)
{
return particle.Position;
}

public float GetRadius(ISphere particle)
{
return particle.Radius;
}

private Bounds GetBounds(ISphere particle)
{
Bounds bounds = new Bounds
{
min = new Vector3(particle.Position.x - particle.Radius, particle.Position.y - particle.Radius, particle.Position.z - particle.Radius),
max = new Vector3(particle.Position.x + particle.Radius, particle.Position.y + particle.Radius, particle.Position.z + particle.Radius)
};
return bounds;
}

public void MapObjectToBVHLeaf(ISphere particle, BVHNode<ISphere> leaf)
{
gameObjectToLeafMap[particle] = leaf;
}

// this allows us to be notified when an object moves, so we can adjust the BVH
public void OnPositionOrSizeChanged(ISphere changed)
{
// the SSObject has changed, so notify the BVH leaf to refit for the object
gameObjectToLeafMap[changed].RefitObjectChanged(this, changed);
}

public void UnmapObject(ISphere particle)
{
gameObjectToLeafMap.Remove(particle);
}
}
}
11 changes: 11 additions & 0 deletions Adapters/BVHSphereAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Adapters/IBVHNodeAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnityEngine;

namespace DataStructures
{
public interface IBVHNodeAdapter<T>
{
BVH<T> BVH { get; set; }
Vector3 GetObjectPos(T obj);
float GetRadius(T obj);
void MapObjectToBVHLeaf(T obj, BVHNode<T> leaf);
void OnPositionOrSizeChanged(T changed);
void UnmapObject(T obj);
void CheckMap(T obj);
BVHNode<T> GetLeaf(T obj);
}
}
11 changes: 11 additions & 0 deletions Adapters/IBVHNodeAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 67a4af7

Please sign in to comment.