FixedPoint4.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System.Globalization;
  2. using System.Linq;
  3. using Robust.Shared.Serialization;
  4. using Robust.Shared.Utility;
  5. namespace Content.Shared.FixedPoint
  6. {
  7. /// <summary>
  8. /// Represents a quantity of something, to a precision of 0.01.
  9. /// To enforce this level of precision, floats are shifted by 2 decimal points, rounded, and converted to an int.
  10. /// </summary>
  11. [Serializable, CopyByRef]
  12. public struct FixedPoint4 : ISelfSerialize, IComparable<FixedPoint4>, IEquatable<FixedPoint4>, IFormattable
  13. {
  14. public long Value { get; private set; }
  15. private const long Shift = 4;
  16. private const long ShiftConstant = 10000; // Must be equal to pow(10, Shift)
  17. public static FixedPoint4 MaxValue { get; } = new(long.MaxValue);
  18. public static FixedPoint4 Epsilon { get; } = new(1);
  19. public static FixedPoint4 Zero { get; } = new(0);
  20. // This value isn't picked by any proper testing, don't @ me.
  21. private const float FloatEpsilon = 0.00001f;
  22. #if DEBUG
  23. static FixedPoint4()
  24. {
  25. // ReSharper disable once CompareOfFloatsByEqualityOperator
  26. DebugTools.Assert(Math.Pow(10, Shift) == ShiftConstant, "ShiftConstant must be equal to pow(10, Shift)");
  27. }
  28. #endif
  29. private readonly double ShiftDown()
  30. {
  31. return Value / (double) ShiftConstant;
  32. }
  33. private FixedPoint4(long value)
  34. {
  35. Value = value;
  36. }
  37. public static FixedPoint4 New(long value)
  38. {
  39. return new(value * ShiftConstant);
  40. }
  41. public static FixedPoint4 FromTenThousandths(long value) => new(value);
  42. public static FixedPoint4 New(float value)
  43. {
  44. return new((long) ApplyFloatEpsilon(value * ShiftConstant));
  45. }
  46. private static float ApplyFloatEpsilon(float value)
  47. {
  48. return value + FloatEpsilon * Math.Sign(value);
  49. }
  50. private static double ApplyFloatEpsilon(double value)
  51. {
  52. return value + FloatEpsilon * Math.Sign(value);
  53. }
  54. /// <summary>
  55. /// Create the closest <see cref="FixedPoint4"/> for a float value, always rounding up.
  56. /// </summary>
  57. public static FixedPoint4 NewCeiling(float value)
  58. {
  59. return new((long) MathF.Ceiling(value * ShiftConstant));
  60. }
  61. public static FixedPoint4 New(double value)
  62. {
  63. return new((long) ApplyFloatEpsilon(value * ShiftConstant));
  64. }
  65. public static FixedPoint4 New(string value)
  66. {
  67. return New(Parse.Float(value));
  68. }
  69. public static FixedPoint4 operator +(FixedPoint4 a) => a;
  70. public static FixedPoint4 operator -(FixedPoint4 a) => new(-a.Value);
  71. public static FixedPoint4 operator +(FixedPoint4 a, FixedPoint4 b)
  72. => new(a.Value + b.Value);
  73. public static FixedPoint4 operator -(FixedPoint4 a, FixedPoint4 b)
  74. => new(a.Value - b.Value);
  75. public static FixedPoint4 operator *(FixedPoint4 a, FixedPoint4 b)
  76. {
  77. return new(b.Value * a.Value / ShiftConstant);
  78. }
  79. public static FixedPoint4 operator *(FixedPoint4 a, float b)
  80. {
  81. return new((long) ApplyFloatEpsilon(a.Value * b));
  82. }
  83. public static FixedPoint4 operator *(FixedPoint4 a, double b)
  84. {
  85. return new((long) ApplyFloatEpsilon(a.Value * b));
  86. }
  87. public static FixedPoint4 operator *(FixedPoint4 a, long b)
  88. {
  89. return new(a.Value * b);
  90. }
  91. public static FixedPoint4 operator /(FixedPoint4 a, FixedPoint4 b)
  92. {
  93. return new((long) (ShiftConstant * (long) a.Value / b.Value));
  94. }
  95. public static FixedPoint4 operator /(FixedPoint4 a, float b)
  96. {
  97. return new((long) ApplyFloatEpsilon(a.Value / b));
  98. }
  99. public static bool operator <=(FixedPoint4 a, long b)
  100. {
  101. return a <= New(b);
  102. }
  103. public static bool operator >=(FixedPoint4 a, long b)
  104. {
  105. return a >= New(b);
  106. }
  107. public static bool operator <(FixedPoint4 a, long b)
  108. {
  109. return a < New(b);
  110. }
  111. public static bool operator >(FixedPoint4 a, long b)
  112. {
  113. return a > New(b);
  114. }
  115. public static bool operator ==(FixedPoint4 a, long b)
  116. {
  117. return a == New(b);
  118. }
  119. public static bool operator !=(FixedPoint4 a, long b)
  120. {
  121. return a != New(b);
  122. }
  123. public static bool operator ==(FixedPoint4 a, FixedPoint4 b)
  124. {
  125. return a.Equals(b);
  126. }
  127. public static bool operator !=(FixedPoint4 a, FixedPoint4 b)
  128. {
  129. return !a.Equals(b);
  130. }
  131. public static bool operator <=(FixedPoint4 a, FixedPoint4 b)
  132. {
  133. return a.Value <= b.Value;
  134. }
  135. public static bool operator >=(FixedPoint4 a, FixedPoint4 b)
  136. {
  137. return a.Value >= b.Value;
  138. }
  139. public static bool operator <(FixedPoint4 a, FixedPoint4 b)
  140. {
  141. return a.Value < b.Value;
  142. }
  143. public static bool operator >(FixedPoint4 a, FixedPoint4 b)
  144. {
  145. return a.Value > b.Value;
  146. }
  147. public readonly float Float()
  148. {
  149. return (float) ShiftDown();
  150. }
  151. public readonly double Double()
  152. {
  153. return ShiftDown();
  154. }
  155. public readonly long Long()
  156. {
  157. return Value / ShiftConstant;
  158. }
  159. public readonly int Int()
  160. {
  161. return (int)Long();
  162. }
  163. // Implicit operators ftw
  164. public static implicit operator FixedPoint4(FixedPoint2 n) => New(n.Int());
  165. public static implicit operator FixedPoint4(float n) => New(n);
  166. public static implicit operator FixedPoint4(double n) => New(n);
  167. public static implicit operator FixedPoint4(int n) => New(n);
  168. public static implicit operator FixedPoint4(long n) => New(n);
  169. public static explicit operator FixedPoint2(FixedPoint4 n) => n.Int();
  170. public static explicit operator float(FixedPoint4 n) => n.Float();
  171. public static explicit operator double(FixedPoint4 n) => n.Double();
  172. public static explicit operator int(FixedPoint4 n) => n.Int();
  173. public static explicit operator long(FixedPoint4 n) => n.Long();
  174. public static FixedPoint4 Min(params FixedPoint4[] fixedPoints)
  175. {
  176. return fixedPoints.Min();
  177. }
  178. public static FixedPoint4 Min(FixedPoint4 a, FixedPoint4 b)
  179. {
  180. return a < b ? a : b;
  181. }
  182. public static FixedPoint4 Max(FixedPoint4 a, FixedPoint4 b)
  183. {
  184. return a > b ? a : b;
  185. }
  186. public static long Sign(FixedPoint4 value)
  187. {
  188. if (value < Zero)
  189. {
  190. return -1;
  191. }
  192. if (value > Zero)
  193. {
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. public static FixedPoint4 Abs(FixedPoint4 a)
  199. {
  200. return FromTenThousandths(Math.Abs(a.Value));
  201. }
  202. public static FixedPoint4 Dist(FixedPoint4 a, FixedPoint4 b)
  203. {
  204. return FixedPoint4.Abs(a - b);
  205. }
  206. public static FixedPoint4 Clamp(FixedPoint4 number, FixedPoint4 min, FixedPoint4 max)
  207. {
  208. if (min > max)
  209. {
  210. throw new ArgumentException($"{nameof(min)} {min} cannot be larger than {nameof(max)} {max}");
  211. }
  212. return number < min ? min : number > max ? max : number;
  213. }
  214. public override readonly bool Equals(object? obj)
  215. {
  216. return obj is FixedPoint4 unit &&
  217. Value == unit.Value;
  218. }
  219. public override readonly int GetHashCode()
  220. {
  221. // ReSharper disable once NonReadonlyMemberInGetHashCode
  222. return HashCode.Combine(Value);
  223. }
  224. public void Deserialize(string value)
  225. {
  226. // TODO implement "lossless" serializer.
  227. // I.e., dont use floats.
  228. if (value == "MaxValue")
  229. Value = int.MaxValue;
  230. else
  231. this = New(Parse.Double(value));
  232. }
  233. public override readonly string ToString() => $"{ShiftDown().ToString(CultureInfo.InvariantCulture)}";
  234. public string ToString(string? format, IFormatProvider? formatProvider)
  235. {
  236. return ToString();
  237. }
  238. public readonly string Serialize()
  239. {
  240. // TODO implement "lossless" serializer.
  241. // I.e., dont use floats.
  242. if (Value == int.MaxValue)
  243. return "MaxValue";
  244. return ToString();
  245. }
  246. public readonly bool Equals(FixedPoint4 other)
  247. {
  248. return Value == other.Value;
  249. }
  250. public readonly int CompareTo(FixedPoint4 other)
  251. {
  252. if (other.Value > Value)
  253. {
  254. return -1;
  255. }
  256. if (other.Value < Value)
  257. {
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. }
  263. public static class FixedPoint4EnumerableExt
  264. {
  265. public static FixedPoint4 Sum(this IEnumerable<FixedPoint4> source)
  266. {
  267. var acc = FixedPoint4.Zero;
  268. foreach (var n in source)
  269. {
  270. acc += n;
  271. }
  272. return acc;
  273. }
  274. }
  275. }