Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created April 17, 2020 23:28
Show Gist options
  • Save jeffvella/6926229d1466d905e6bb50bd2f7e1ce4 to your computer and use it in GitHub Desktop.
Save jeffvella/6926229d1466d905e6bb50bd2f7e1ce4 to your computer and use it in GitHub Desktop.
using System.Runtime.CompilerServices;
using Unity.Mathematics;
using Unity.Physics;
namespace NativeOctree
{
public struct Aabb4
{
public float4 MinXs;
public float4 MaxXs;
public float4 MinYs;
public float4 MaxYs;
public float4 MinZs;
public float4 MaxZs;
public unsafe Aabb this[int i]
{
get => new Aabb
{
Min = new float3(MinXs[i], MinYs[i], MinZs[i]),
Max = new float3(MaxXs[i], MaxYs[i], MaxZs[i])
};
set
{
MinXs[i] = value.Min.x;
MinYs[i] = value.Min.y;
MinZs[i] = value.Min.z;
MaxXs[i] = value.Max.x;
MaxYs[i] = value.Max.y;
MaxZs[i] = value.Max.z;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool4 ContainedBy(Aabb aabb4)
{
return (aabb4.Min.x <= MinXs) & (aabb4.Max.x >= MaxXs)
& (aabb4.Min.y <= MinYs) & (aabb4.Max.y >= MaxYs)
& (aabb4.Min.z <= MinZs) & (aabb4.Max.z >= MaxZs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool4 OverlappedBy(Aabb aabb)
{
return (aabb.Max.x >= MinXs) & (aabb.Min.x <= MaxXs)
& (aabb.Max.y >= MinYs) & (aabb.Min.y <= MaxYs)
& (aabb.Max.z >= MinZs) & (aabb.Min.z <= MaxZs);
}
}
}
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;
using Unity.Physics;
namespace NativeOctree
{
public unsafe struct Aabb4x2
{
public Aabb4 A;
public Aabb4 B;
public ref Aabb4 this[int i]
=> ref UnsafeUtilityEx.ArrayElementAsRef<Aabb4>(UnsafeUtility.AddressOf(ref this), i);
/// <summary>
/// Splits a bounds into eight equal smaller bounds, grouped for SIMD.
/// </summary>
/// <param name="parent">the parent bounds to be split</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Aabb4x2 CreateOctreeBounds(Aabb parent)
{
// Note: Physics Aabb.Extents is the edge length NOT the radius of circumscribed sphere.
return CreateOctreeBounds(parent.Center, parent.Extents * 0.5f);
}
/// <summary>
/// Splits a bounds into eight equal smaller bounds, grouped for SIMD.
/// </summary>
/// <param name="parent">the parent bounds to be split</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Aabb4x2 CreateOctreeBounds(AABB parent)
{
return CreateOctreeBounds(parent.Center, parent.Extents);
}
/// <summary>
/// Splits a bounds into eight equal smaller bounds, grouped for SIMD.
/// </summary>
/// <param name="center">the middle</param>
/// <param name="extents">radius of circumscribed sphere</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Aabb4x2 CreateOctreeBounds(float3 center, float3 extents)
{
Aabb4x2 result = default;
float3 min = center - extents;
result.A.MinXs = default;
result.A.MinXs.xz = min.x;
result.A.MinXs.yw = center.x;
result.A.MinYs = default;
result.A.MinYs.xy = center.y;
result.A.MinYs.zw = min.y;
result.A.MinZs = min.z;
result.A.MaxXs = result.A.MinXs + extents.x;
result.A.MaxYs = result.A.MinYs + extents.y;
result.A.MaxZs = result.A.MinZs + extents.z;
result.B = result.A;
result.B.MinZs += extents.z;
result.B.MaxZs += extents.z;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment