Utils.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using InABox.Core;
  2. using netDxf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Numerics;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Vector3 = System.Numerics.Vector3;
  13. namespace InABox.Dxf;
  14. internal static class Utils
  15. {
  16. public static PointF Transform(this Matrix matrix, PointF point)
  17. {
  18. var arr = new[] { point };
  19. matrix.TransformPoints(arr);
  20. return arr[0];
  21. }
  22. public static PointF TransformVector(this Matrix matrix, PointF point)
  23. {
  24. var arr = new[] { point };
  25. matrix.TransformVectors(arr);
  26. return arr[0];
  27. }
  28. public static float GetRotation(this Matrix matrix)
  29. {
  30. var vec = matrix.TransformVector(new PointF(1, 0));
  31. return (float)(Math.Atan2(vec.Y, vec.X) * 180 / Math.PI);
  32. }
  33. public static float GetScale(this Matrix matrix)
  34. {
  35. var vec = matrix.TransformVector(new PointF(1, 0));
  36. return (float)Math.Sqrt(vec.X * vec.X + vec.Y * vec.Y);
  37. }
  38. public static float Mod(float x, float y)
  39. {
  40. return (float)(x - y * Math.Floor(x / y));
  41. }
  42. public static Vector3 ToVec3(this netDxf.Vector3 vec)
  43. {
  44. return new((float)vec.X, (float)vec.Y, (float)vec.Z);
  45. }
  46. public static netDxf.Vector3 ToNetVec3(this Vector3 vec)
  47. {
  48. return new(vec.X, vec.Y, vec.Z);
  49. }
  50. public static Matrix4x4 Translate(this Matrix4x4 matrix, float x, float y, float z)
  51. {
  52. return Matrix4x4.CreateTranslation(x, y, z) * matrix;
  53. }
  54. public static Matrix4x4 Scale(this Matrix4x4 matrix, float x, float y, float z)
  55. {
  56. return Matrix4x4.CreateScale(x, y, z) * matrix;
  57. }
  58. public static Matrix4x4 Rotate(this Matrix4x4 matrix, float x, float y, float z, float angle)
  59. {
  60. var cos = (float)Math.Cos(angle * Math.PI / 180);
  61. var sin = (float)Math.Sin(angle * Math.PI / 180);
  62. var rotMatrix = new Matrix4x4();
  63. rotMatrix.M11 = x * x * (1 - cos) + cos;
  64. rotMatrix.M21 = x * y * (1 - cos) - z * sin;
  65. rotMatrix.M31 = x * z * (1 - cos) + y * sin;
  66. rotMatrix.M12 = y * x * (1 - cos) + z * sin;
  67. rotMatrix.M22 = y * y * (1 - cos) + cos;
  68. rotMatrix.M32 = y * z * (1 - cos) - x * sin;
  69. rotMatrix.M13 = z * x * (1 - cos) - y * sin;
  70. rotMatrix.M23 = z * y * (1 - cos) + x * sin;
  71. rotMatrix.M33 = z * z * (1 - cos) + cos;
  72. rotMatrix.M14 = 0;
  73. rotMatrix.M24 = 0;
  74. rotMatrix.M34 = 0;
  75. rotMatrix.M44 = 1;
  76. rotMatrix.M43 = 0;
  77. rotMatrix.M42 = 0;
  78. rotMatrix.M41 = 0;
  79. return rotMatrix * matrix;
  80. }
  81. public static RectangleF? CombineBounds(IEnumerable<RectangleF?> bounds)
  82. {
  83. var e = bounds.NotNull().GetEnumerator();
  84. if (!e.MoveNext())
  85. {
  86. return null;
  87. }
  88. var bound = e.Current;
  89. while(e.MoveNext())
  90. {
  91. var thisBound = e.Current;
  92. var x = Math.Min(bound.X, thisBound.X);
  93. var y = Math.Min(bound.Y, thisBound.Y);
  94. var right = Math.Max(bound.Right, thisBound.Right);
  95. var bottom = Math.Max(bound.Bottom, thisBound.Bottom);
  96. bound.X = x;
  97. bound.Y = y;
  98. bound.Height = bottom - bound.Y;
  99. bound.Width = right - bound.X;
  100. }
  101. return bound;
  102. }
  103. public static RectangleF? CombineBounds(params RectangleF?[] bounds)
  104. {
  105. return CombineBounds((IEnumerable<RectangleF?>)bounds);
  106. }
  107. public static RectangleF AddPoint(RectangleF rectangle, PointF point)
  108. {
  109. var x = Math.Min(rectangle.X, point.X);
  110. var y = Math.Min(rectangle.Y, point.Y);
  111. var right = Math.Max(rectangle.Right, point.X);
  112. var bottom = Math.Max(rectangle.Bottom, point.Y);
  113. rectangle.X = x;
  114. rectangle.Y = y;
  115. rectangle.Height = bottom - rectangle.Y;
  116. rectangle.Width = right - rectangle.X;
  117. return rectangle;
  118. }
  119. public static RectangleF? RectangleFromPoints(params PointF[] points)
  120. {
  121. if (points.Length == 0) return null;
  122. var rectangle = new RectangleF(points[0].X, points[0].Y, 0, 0);
  123. for(int i = 1; i < points.Length; ++i)
  124. {
  125. var point = points[i];
  126. var x = Math.Min(rectangle.X, point.X);
  127. var y = Math.Min(rectangle.Y, point.Y);
  128. var right = Math.Max(rectangle.Right, point.X);
  129. var bottom = Math.Max(rectangle.Bottom, point.Y);
  130. rectangle.X = x;
  131. rectangle.Y = y;
  132. rectangle.Height = bottom - rectangle.Y;
  133. rectangle.Width = right - rectangle.X;
  134. }
  135. return rectangle;
  136. }
  137. }