[]
Utility class.
public static class DXUtil
The multiplier for conversion from radians to degrees.
public const float ToDegrees = 57.29578
The multiplier for conversion from degrees to radians.
public const float ToRadians = 0.017453292
The value for which all absolute numbers smaller than are considered equal to zero.
public const float ZeroTolerance = 1E-06
Allocate an aligned memory buffer and clear it with a specified value (0 by default).
public static IntPtr AllocateClearedMemory(int sizeInBytes, byte clearValue = 0, int align = 16)
sizeInBytes intSize of the buffer to allocate.
clearValue byteDefault value used to clear the buffer.
align intAlignment, 16 bytes by default.
A pointer to a buffer aligned.
To free this buffer, call FreeMemory(IntPtr).
Allocate an aligned memory buffer.
public static IntPtr AllocateMemory(int sizeInBytes, int align = 16)
sizeInBytes intSize of the buffer to allocate.
align intAlignment, 16 bytes by default.
A pointer to a buffer aligned.
To free this buffer, call FreeMemory(IntPtr).
Test if there is an element in this enumeration.
public static bool Any<T>(IEnumerable<T> source)
source System.Collections.Generic.IEnumerable<T><T>The enumerable source.
true if there is an element in this enumeration, false otherwise
TType of the element
Creates an Array of count elements with the given value.
public static T[] Array<T>(T value, int count)
value Tcount intTBuilds a fast property getter from a type and a property info.
public static GetValueFastDelegate<T> BuildPropertyGetter<T>(Type customEffectType, PropertyInfo propertyInfo)
customEffectType System.TypeType of the custom effect.
propertyInfo System.Reflection.PropertyInfoThe property info to get the value from.
A compiled delegate.
TType of the getter.
Builds a fast property setter from a type and a property info.
public static SetValueFastDelegate<T> BuildPropertySetter<T>(Type customEffectType, PropertyInfo propertyInfo)
customEffectType System.TypeType of the custom effect.
propertyInfo System.Reflection.PropertyInfoThe property info to set the value to.
A compiled delegate.
TType of the setter.
Clamps the specified value.
public static int Clamp(int value, int min, int max)
value intThe value.
min intThe min.
max intThe max.
The result of clamping a value between min and max
Clamps the specified value.
public static float Clamp(float value, float min, float max)
value floatThe value.
min floatThe min.
max floatThe max.
The result of clamping a value between min and max
Clears the memory.
public static void ClearMemory(IntPtr dest, byte value, int sizeInBytesToClear)
dest System.IntPtrThe dest.
value byteThe value.
sizeInBytesToClear intThe size in bytes to clear.
Compares two collection, element by elements.
public static bool Compare(IEnumerator leftIt, IEnumerator rightIt)
leftIt System.Collections.IEnumeratorA "from" enumerator.
rightIt System.Collections.IEnumeratorA "to" enumerator.
true if lists are identical; otherwise, false.
Compares two block of memory.
public static bool CompareMemory(IntPtr from, IntPtr against, int sizeToCompare)
from System.IntPtrThe pointer to compare from.
against System.IntPtrThe pointer to compare against.
sizeToCompare intThe size in bytes to compare.
true if the buffers are equivalent; otherwise, false.
Converts Bool array to bool array.
public static bool[] ConvertToBoolArray(Bool[] array)
array Bool[]The array.
Converted array of bool.
Native memcpy.
public static void CopyMemory(IntPtr dest, IntPtr src, int sizeInBytesToCopy)
dest System.IntPtrThe destination memory location.
src System.IntPtrThe source memory location.
sizeInBytesToCopy intThe byte count.
Safely dispose a referencem if not null, and set it to null after dispose.
public static void Dispose<T>(ref T comObject) where T : class, IDisposable
comObject TObject to dispose.
TThe type of COM interface to dispose.
The reference will be set to null after dispose.
Selects distinct elements from an enumeration.
public static IEnumerable<TSource> Distinct<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer = null)
source System.Collections.Generic.IEnumerable<T><TSource>The source.
comparer System.Collections.Generic.IEqualityComparer<T><TSource>The comparer.
A enumeration of selected values
TSourceThe type of the T source.
Releases native pointers to already disposed shadow objects.
public static void FreeDisposedShadowPointers()
Call this method when all unmanaged objects using shadows are destroyed.
Allocate an aligned memory buffer.
public static void FreeMemory(IntPtr alignedBuffer)
alignedBuffer System.IntPtrThe buffer must have been allocated with AllocateMemory(int, int).
Gets the custom attribute.
public static T GetCustomAttribute<T>(MemberInfo memberInfo, bool inherited = false) where T : Attribute
memberInfo System.Reflection.MemberInfoThe member info.
inherited boolif set to true [inherited].
The custom attribute or null if not found.
TType of the custom attribute.
Gets the custom attributes.
public static IEnumerable<T> GetCustomAttributes<T>(MemberInfo memberInfo, bool inherited = false) where T : Attribute
memberInfo System.Reflection.MemberInfoThe member info.
inherited boolif set to true [inherited].
The custom attribute or null if not found.
TType of the custom attribute.
Gets the System.Guid from a type.
public static Guid GetGuidFromType(Type type)
type System.TypeThe type.
The guid associated with this type.
Determines whether fromType can be assigned to toType.
public static bool IsAssignableFrom(Type toType, Type fromType)
toType System.TypeTo type.
fromType System.TypeFrom type.
true if [is assignable from] [the specified to type]; otherwise, false.
Determines whether the specified type to test is an enum.
public static bool IsEnum(Type typeToTest)
typeToTest System.TypeThe type to test.
true if the specified type to test is an enum; otherwise, false.
Determines whether the specified value is close to one (1.0f).
public static bool IsOne(float a)
a floatThe floating value.
true if the specified value is close to one (1.0f); otherwise, false.
Determines whether the specified type to test is a value type.
public static bool IsValueType(Type typeToTest)
typeToTest System.TypeThe type to test.
true if the specified type to test is a value type; otherwise, false.
Determines whether the specified value is close to zero (0.0f).
public static bool IsZero(float a)
a floatThe floating value.
true if the specified value is close to zero (0.0f); otherwise, false.
String helper join method to display an enumrable of object as a single string.
public static string Join(string separator, IEnumerator elements)
separator stringThe separator.
elements System.Collections.IEnumeratorThe enumerable.
A string with array elements serparated by the seperator.
Interpolates between two values using a linear function by a given amount.
public static double Lerp(double from, double to, double amount)
from doubleValue to interpolate from.
to doubleValue to interpolate to.
amount doubleInterpolation amount.
The result of linear interpolation of values based on the amount.
See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
Interpolates between two values using a linear function by a given amount.
public static float Lerp(float from, float to, float amount)
from floatValue to interpolate from.
to floatValue to interpolate to.
amount floatInterpolation amount.
The result of linear interpolation of values based on the amount.
See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
Checks if a and b are almost equals, taking into account the magnitude of floating point numbers. See Remarks. See remarks.
public static bool NearEqual(float a, float b)
a floatThe left value to compare.
b floatThe right value to compare.
true if a almost equal to b, false otherwise
The code is using the technique described by Bruce Dawson in Comparing Floating point numbers 2012 edition.
Pins the specified source and call an action with the pinned pointer.
public static void Pin<T>(T[] source, Action<IntPtr> pinAction) where T : struct
source T[]The source array.
pinAction System.Action<T><System.IntPtr>The pin action to perform on the pinned pointer.
TThe type of the structure to pin.
Pins the specified source and call an action with the pinned pointer.
public static void Pin<T>(ref T source, Action<IntPtr> pinAction) where T : struct
source TThe source.
pinAction System.Action<T><System.IntPtr>The pin action to perform on the pinned pointer.
TThe type of the structure to pin.
Converts a pointer to a null-terminating string up to maxLength characters to a .Net string.
public static string PtrToStringUni(IntPtr pointer, int maxLength)
pointer System.IntPtrThe pointer to an Unicode null string.
maxLength intMaximum length of the string.
The converted string.
Reads the specified array T[] data from a memory location.
public static IntPtr Read<T>(IntPtr source, T[] data, int offset, int count) where T : struct
source System.IntPtrMemory location to read from.
data T[]The data write to.
offset intThe offset in the array to write to.
count intThe number of T element to read from the memory location.
source pointer + sizeof(T) * count.
TType of a data to read.
Reads the specified T data from a memory location.
public static void Read<T>(IntPtr source, ref T data) where T : struct
source System.IntPtrMemory location to read from.
data TThe data write to.
TType of a data to read.
Reads the specified T data from a memory location.
public static T Read<T>(IntPtr source) where T : struct
source System.IntPtrMemory location to read from.
The data read from the memory location.
TType of a data to read.
Reads the specified T data from a memory location.
public static IntPtr ReadAndPosition<T>(IntPtr source, ref T data) where T : struct
source System.IntPtrMemory location to read from.
data TThe data write to.
source pointer + sizeof(T).
TType of a data to read.
Reads the specified T data from a memory location.
public static void ReadOut<T>(IntPtr source, out T data) where T : struct
source System.IntPtrMemory location to read from.
data TThe data write to.
TType of a data to read.
Read stream to a byte[] buffer.
public static byte[] ReadStream(Stream stream, ref int readLength)
stream System.IO.StreamInput stream.
readLength intLength to read.
A byte[] buffer.
Select elements from an enumeration.
public static IEnumerable<TResult> SelectMany<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
source System.Collections.Generic.IEnumerable<T><TSource>The source.
selector System.Func<T, TResult><TSource, System.Collections.Generic.IEnumerable<T><TResult>>The selector.
A enumeration of selected values
TSourceThe type of the T source.
TResultThe type of the T result.
Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.
public static int SizeOf<T>() where T : struct
Size of this struct.
TA struct to evaluate.
Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.
public static int SizeOf<T>(T[] array) where T : struct
array T[]The array of struct to evaluate.
Size in bytes of this array of struct.
TA struct.
Suspends the current thread.
public static void Sleep(TimeSpan sleepTimeInMillis)
sleepTimeInMillis System.TimeSpanThe duration to sleep in milliseconds.
Copies the contents of a managed String into unmanaged memory, converting into ANSI format as it copies.
public static IntPtr StringToHGlobalAnsi(string s)
s stringA managed string to be copied.
The address, in unmanaged memory, to where s was copied, or IntPtr.Zero if s is null.
Copies the contents of a managed String into unmanaged memory.
public static IntPtr StringToHGlobalUni(string s)
s stringA managed string to be copied.
The address, in unmanaged memory, to where s was copied, or IntPtr.Zero if s is null.
Transforms an System.Collections.Generic.IEnumerable<T> to an array of T.
public static T[] ToArray<T>(IEnumerable<T> source)
source System.Collections.Generic.IEnumerable<T><T>The enumerable source.
an array of T
TType of the element
Converts a structured array to an equivalent byte array.
public static byte[] ToByteArray<T>(T[] source) where T : struct
source T[]The source array.
Converted byte array.
TThe type of source array.
Checks if a - b are almost equals within a float epsilon.
public static bool WithinEpsilon(float a, float b, float epsilon)
a floatThe left value to compare.
b floatThe right value to compare.
epsilon floatEpsilon value
true if a almost equal to b within a float epsilon, false otherwise
Writes the specified array T[] data to a memory location.
public static IntPtr Write<T>(IntPtr destination, T[] data, int offset, int count) where T : struct
destination System.IntPtrMemory location to write to.
data T[]The array of T data to write.
offset intThe offset in the array to read from.
count intThe number of T element to write to the memory location.
destination pointer + sizeof(T) * count.
TType of a data to write.
Writes the specified T data to a memory location.
public static void Write<T>(IntPtr destination, ref T data) where T : struct
destination System.IntPtrMemory location to write to.
data TThe data to write.
TType of a data to write.
Writes the specified T data to a memory location.
public static IntPtr WriteAndPosition<T>(IntPtr destination, ref T data) where T : struct
destination System.IntPtrMemory location to write to.
data TThe data to write.
destination pointer + sizeof(T).
TType of a data to write.