FixedPoint2.cs 9.1 KB

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