Field-Robotics-Japan / UnitySensors

ROS/ROS2 enabled Sensor models (Assets) on Unity
Apache License 2.0
191 stars 28 forks source link

NativeArray extensions for accelerating LiDAR processing #154

Open Autumn60 opened 4 months ago

Autumn60 commented 4 months ago

This NativeArray extension could optimize LiDAR calculation.

using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

namespace HOGE_NAMESPACE
{
    public static partial class NativeArrayRefExtensions
    {
        public static ref T GetRef<T>(this NativeArray<T> array, int index)
            where T : struct
        {
            if (index < 0 || index >= array.Length)
                throw new ArgumentOutOfRangeException(nameof(index));
            unsafe
            {
                return ref UnsafeUtility.ArrayElementAsRef<T>(array.GetUnsafePtr(), index);
            }
        }
    }
}