initial
This commit is contained in:
+184
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#if !(ENABLE_MONO || ENABLE_IL2CPP)
|
||||
using System.Numerics;
|
||||
#else
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public struct QuantizedVector2 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
|
||||
public QuantizedVector2(uint x, uint y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public struct QuantizedVector3 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
public uint z;
|
||||
|
||||
public QuantizedVector3(uint x, uint y, uint z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
public struct QuantizedVector4 {
|
||||
public uint x;
|
||||
public uint y;
|
||||
public uint z;
|
||||
public uint w;
|
||||
|
||||
public QuantizedVector4(uint x, uint y, uint z, uint w) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DeBruijn {
|
||||
public static readonly int[] Lookup = new int[32] {
|
||||
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||||
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
||||
};
|
||||
}
|
||||
|
||||
public class BoundedRange {
|
||||
private readonly float minValue;
|
||||
private readonly float maxValue;
|
||||
private readonly float precision;
|
||||
private readonly int requiredBits;
|
||||
private readonly uint mask;
|
||||
|
||||
public BoundedRange(float minValue, float maxValue, float precision) {
|
||||
this.minValue = minValue;
|
||||
this.maxValue = maxValue;
|
||||
this.precision = precision;
|
||||
|
||||
requiredBits = Log2((uint)((maxValue - minValue) * (1.0f / precision) + 0.5f)) + 1;
|
||||
mask = (uint)((1L << requiredBits) - 1);
|
||||
}
|
||||
|
||||
private int Log2(uint value) {
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
|
||||
return DeBruijn.Lookup[(value * 0x07C4ACDDU) >> 27];
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public uint Quantize(float value) {
|
||||
if (value < minValue)
|
||||
value = minValue;
|
||||
else if (value > maxValue)
|
||||
value = maxValue;
|
||||
|
||||
return (uint)((float)((value - minValue) * (1f / precision)) + 0.5f) & mask;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public float Dequantize(uint data) {
|
||||
float adjusted = ((float)data * precision) + minValue;
|
||||
|
||||
if (adjusted < minValue)
|
||||
adjusted = minValue;
|
||||
else if (adjusted > maxValue)
|
||||
adjusted = maxValue;
|
||||
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
public static QuantizedVector2 Quantize(Vector2 vector2, BoundedRange[] boundedRange) {
|
||||
QuantizedVector2 data = default(QuantizedVector2);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector2.x);
|
||||
data.y = boundedRange[1].Quantize(vector2.y);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector2.X);
|
||||
data.y = boundedRange[1].Quantize(vector2.Y);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static QuantizedVector3 Quantize(Vector3 vector3, BoundedRange[] boundedRange) {
|
||||
QuantizedVector3 data = default(QuantizedVector3);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector3.x);
|
||||
data.y = boundedRange[1].Quantize(vector3.y);
|
||||
data.z = boundedRange[2].Quantize(vector3.z);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector3.X);
|
||||
data.y = boundedRange[1].Quantize(vector3.Y);
|
||||
data.z = boundedRange[2].Quantize(vector3.Z);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static QuantizedVector4 Quantize(Vector4 vector4, BoundedRange[] boundedRange) {
|
||||
QuantizedVector4 data = default(QuantizedVector4);
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
data.x = boundedRange[0].Quantize(vector4.x);
|
||||
data.y = boundedRange[1].Quantize(vector4.y);
|
||||
data.z = boundedRange[2].Quantize(vector4.z);
|
||||
data.w = boundedRange[3].Quantize(vector4.w);
|
||||
#else
|
||||
data.x = boundedRange[0].Quantize(vector4.X);
|
||||
data.y = boundedRange[1].Quantize(vector4.Y);
|
||||
data.z = boundedRange[2].Quantize(vector4.Z);
|
||||
data.w = boundedRange[3].Quantize(vector4.W);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static Vector2 Dequantize(QuantizedVector2 data, BoundedRange[] boundedRange) {
|
||||
return new Vector2(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y));
|
||||
}
|
||||
|
||||
public static Vector3 Dequantize(QuantizedVector3 data, BoundedRange[] boundedRange) {
|
||||
return new Vector3(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y), boundedRange[2].Dequantize(data.z));
|
||||
}
|
||||
|
||||
public static Vector4 Dequantize(QuantizedVector4 data, BoundedRange[] boundedRange) {
|
||||
return new Vector4(boundedRange[0].Dequantize(data.x), boundedRange[1].Dequantize(data.y), boundedRange[2].Dequantize(data.z), boundedRange[3].Dequantize(data.w));
|
||||
}
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Stanislav Denisov
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public static class HalfPrecision {
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct Values {
|
||||
[FieldOffset(0)]
|
||||
public float f;
|
||||
[FieldOffset(0)]
|
||||
public int i;
|
||||
[FieldOffset(0)]
|
||||
public uint u;
|
||||
}
|
||||
|
||||
[MethodImpl(256)]
|
||||
public static ushort Quantize(float value) {
|
||||
var values = new Values {
|
||||
f = value
|
||||
};
|
||||
|
||||
return Quantize(values.i);
|
||||
}
|
||||
|
||||
public static ushort Quantize(int value) {
|
||||
int s = (value >> 16) & 0x00008000;
|
||||
int e = ((value >> 23) & 0X000000FF) - (127 - 15);
|
||||
int m = value & 0X007FFFFF;
|
||||
|
||||
if (e <= 0) {
|
||||
if (e < -10)
|
||||
return (ushort)s;
|
||||
|
||||
m = m | 0x00800000;
|
||||
|
||||
int t = 14 - e;
|
||||
int a = (1 << (t - 1)) - 1;
|
||||
int b = (m >> t) & 1;
|
||||
|
||||
m = (m + a + b) >> t;
|
||||
|
||||
return (ushort)(s | m);
|
||||
}
|
||||
|
||||
if (e == 0XFF - (127 - 15)) {
|
||||
if (m == 0)
|
||||
return (ushort)(s | 0X7C00);
|
||||
|
||||
m >>= 13;
|
||||
|
||||
return (ushort)(s | 0X7C00 | m | ((m == 0) ? 1 : 0));
|
||||
}
|
||||
|
||||
m = m + 0X00000FFF + ((m >> 13) & 1);
|
||||
|
||||
if ((m & 0x00800000) != 0) {
|
||||
m = 0;
|
||||
e++;
|
||||
}
|
||||
|
||||
if (e > 30)
|
||||
return (ushort)(s | 0X7C00);
|
||||
|
||||
return (ushort)(s | (e << 10) | (m >> 13));
|
||||
}
|
||||
|
||||
public static float Dequantize(ushort value) {
|
||||
uint result;
|
||||
uint mantissa = (uint)(value & 1023);
|
||||
uint exponent = 0XFFFFFFF2;
|
||||
|
||||
if ((value & -33792) == 0) {
|
||||
if (mantissa != 0) {
|
||||
while ((mantissa & 1024) == 0) {
|
||||
exponent--;
|
||||
mantissa = mantissa << 1;
|
||||
}
|
||||
|
||||
mantissa &= 0XFFFFFBFF;
|
||||
result = ((uint)((((uint)value & 0x8000) << 16) | ((exponent + 127) << 23))) | (mantissa << 13);
|
||||
} else {
|
||||
result = (uint)((value & 0x8000) << 16);
|
||||
}
|
||||
} else {
|
||||
result = ((((uint)value & 0x8000) << 16) | ((((((uint)value >> 10) & 0X1F) - 15) + 127) << 23)) | (mantissa << 13);
|
||||
}
|
||||
|
||||
var values = new Values {
|
||||
u = result
|
||||
};
|
||||
|
||||
return values.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Stanislav Denisov, Maxim Munning, Davin Carten
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
#if !(ENABLE_MONO || ENABLE_IL2CPP)
|
||||
using System.Numerics;
|
||||
#else
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace NetStack.Quantization {
|
||||
public struct QuantizedQuaternion {
|
||||
public uint m;
|
||||
public uint a;
|
||||
public uint b;
|
||||
public uint c;
|
||||
|
||||
public QuantizedQuaternion(uint m, uint a, uint b, uint c) {
|
||||
this.m = m;
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SmallestThree {
|
||||
private const float smallestThreeUnpack = 0.70710678118654752440084436210485f + 0.0000001f;
|
||||
private const float smallestThreePack = 1.0f / smallestThreeUnpack;
|
||||
|
||||
public static QuantizedQuaternion Quantize(Quaternion quaternion, int bitsPerElement = 12) {
|
||||
float halfRange = (1 << bitsPerElement - 1);
|
||||
float packer = smallestThreePack * halfRange;
|
||||
float maxValue = float.MinValue;
|
||||
bool signMinus = false;
|
||||
uint m = 0;
|
||||
uint a = 0;
|
||||
uint b = 0;
|
||||
uint c = 0;
|
||||
|
||||
for (uint i = 0; i <= 3; i++) {
|
||||
float element = 0.0f;
|
||||
float abs = 0.0f;
|
||||
|
||||
switch (i) {
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
case 0:
|
||||
element = quaternion.x;
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
element = quaternion.y;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
element = quaternion.z;
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
element = quaternion.w;
|
||||
|
||||
break;
|
||||
#else
|
||||
case 0:
|
||||
element = quaternion.X;
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
element = quaternion.Y;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
element = quaternion.Z;
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
element = quaternion.W;
|
||||
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
abs = Math.Abs(element);
|
||||
|
||||
if (abs > maxValue) {
|
||||
signMinus = (element < 0.0f);
|
||||
m = i;
|
||||
maxValue = abs;
|
||||
}
|
||||
}
|
||||
|
||||
float af = 0.0f;
|
||||
float bf = 0.0f;
|
||||
float cf = 0.0f;
|
||||
|
||||
#if ENABLE_MONO || ENABLE_IL2CPP
|
||||
switch (m) {
|
||||
case 0:
|
||||
af = quaternion.y;
|
||||
bf = quaternion.z;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.z;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.y;
|
||||
cf = quaternion.w;
|
||||
|
||||
break;
|
||||
default:
|
||||
af = quaternion.x;
|
||||
bf = quaternion.y;
|
||||
cf = quaternion.z;
|
||||
|
||||
break;
|
||||
}
|
||||
#else
|
||||
switch (m) {
|
||||
case 0:
|
||||
af = quaternion.Y;
|
||||
bf = quaternion.Z;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Z;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Y;
|
||||
cf = quaternion.W;
|
||||
|
||||
break;
|
||||
default:
|
||||
af = quaternion.X;
|
||||
bf = quaternion.Y;
|
||||
cf = quaternion.Z;
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (signMinus) {
|
||||
a = (uint)((-af * packer) + halfRange);
|
||||
b = (uint)((-bf * packer) + halfRange);
|
||||
c = (uint)((-cf * packer) + halfRange);
|
||||
} else {
|
||||
a = (uint)((af * packer) + halfRange);
|
||||
b = (uint)((bf * packer) + halfRange);
|
||||
c = (uint)((cf * packer) + halfRange);
|
||||
}
|
||||
|
||||
return new QuantizedQuaternion(m, a, b, c);
|
||||
}
|
||||
|
||||
public static Quaternion Dequantize(QuantizedQuaternion data, int bitsPerElement = 12) {
|
||||
int halfRange = (1 << bitsPerElement - 1);
|
||||
float unpacker = smallestThreeUnpack * (1.0f / halfRange);
|
||||
uint m = data.m;
|
||||
int ai = (int)data.a;
|
||||
int bi = (int)data.b;
|
||||
int ci = (int)data.c;
|
||||
|
||||
ai -= halfRange;
|
||||
bi -= halfRange;
|
||||
ci -= halfRange;
|
||||
|
||||
float a = ai * unpacker;
|
||||
float b = bi * unpacker;
|
||||
float c = ci * unpacker;
|
||||
|
||||
float d = (float)Math.Sqrt(1.0f - ((a * a) + (b * b) + (c * c)));
|
||||
|
||||
switch (m) {
|
||||
case 0:
|
||||
return new Quaternion(d, a, b, c);
|
||||
|
||||
case 1:
|
||||
return new Quaternion(a, d, b, c);
|
||||
|
||||
case 2:
|
||||
return new Quaternion(a, b, d, c);
|
||||
|
||||
default:
|
||||
return new Quaternion(a, b, c, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user