Selaa lähdekoodia

Fix to inserts by implementing my own maths.

Kenric Nugteren 6 kuukautta sitten
vanhempi
commit
54ffa49028
4 muutettua tiedostoa jossa 145 lisäystä ja 28 poistoa
  1. 76 11
      inabox.dxf/DrawData.cs
  2. 3 3
      inabox.dxf/DxfUtils.cs
  3. 28 14
      inabox.dxf/Objects.cs
  4. 38 0
      inabox.dxf/Utils.cs

+ 76 - 11
inabox.dxf/DrawData.cs

@@ -5,8 +5,12 @@ using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Drawing2D;
 using System.Linq;
+using System.Numerics;
 using System.Text;
 using System.Threading.Tasks;
+using Vector2 = System.Numerics.Vector2;
+using Vector3 = System.Numerics.Vector3;
+using Vector4 = System.Numerics.Vector4;
 
 namespace InABox.Dxf;
 
@@ -16,35 +20,90 @@ internal class DrawData
 
     public Graphics Graphics { get; set; }
 
-    private Stack<Matrix> MatrixStack = new();
+    private Stack<Matrix4x4> MatrixStack = new();
+
+    private Matrix4x4 Transform = Matrix4x4.Identity;
+
+    public DrawData()
+    {
+    }
 
     public void PushTransform()
     {
-        MatrixStack.Push(Graphics.Transform);
+        MatrixStack.Push(Transform);
+    }
+
+    private Matrix ProjectMatrix(Matrix4x4 matrix)
+    {
+        var elMatrix = new Matrix3x2();
+        elMatrix.M11 = (float)matrix.M11;
+        elMatrix.M12 = (float)matrix.M12;
+        elMatrix.M21 = (float)matrix.M21;
+        elMatrix.M22 = (float)matrix.M22;
+        elMatrix.M31 = (float)matrix.M41;
+        elMatrix.M32 = (float)matrix.M42;
+        var newMatrix = new Matrix();
+        newMatrix.MatrixElements = elMatrix;
+        return newMatrix;
     }
 
     public void PopTransform()
     {
-        Graphics.Transform = MatrixStack.Pop();
+        Transform = MatrixStack.Pop();
+        Graphics.Transform = ProjectMatrix(Transform);
     }
 
+    public Matrix4x4 ArbitraryAxisMatrix(Vector3 zAxis)
+    {
+        if (zAxis.Equals(Vector3.UnitZ))
+        {
+            return Matrix4x4.Identity;
+        }
+
+        var unitY = Vector3.UnitY;
+        var unitZ = Vector3.UnitZ;
+        var v = ((!(Math.Abs(zAxis.X) < 1.0 / 64.0) || !(Math.Abs(zAxis.Y) < 1.0 / 64.0)) ? Vector3.Cross(unitZ, zAxis) : Vector3.Cross(unitY, zAxis));
+        v = Vector3.Normalize(v);
+        var vector = Vector3.Cross(zAxis, v);
+        vector = Vector3.Normalize(vector);
+        return new Matrix4x4(v.X, vector.X, zAxis.X, 0, v.Y, vector.Y, zAxis.Y, 0, v.Z, vector.Z, zAxis.Z, 0, 0, 0, 0, 1);
+    }
+    public void ArbitraryAxis(netDxf.Vector3 zAxis)
+    {
+        Transform = ArbitraryAxisMatrix(new((float)zAxis.X, (float)zAxis.Y, (float)zAxis.Z)) * Transform;
+        Graphics.Transform = ProjectMatrix(Transform);
+    }
+    public void ArbitraryAxis(Vector3 zAxis)
+    {
+        Transform = ArbitraryAxisMatrix(zAxis) * Transform;
+        Graphics.Transform = ProjectMatrix(Transform);
+    }
+
+    public void Translate(float x, float y)
+    {
+        Transform = Transform.Translate(x, y, 0);
+        Graphics.Transform = ProjectMatrix(Transform);
+    }
     public void Translate(PointF point)
     {
-        Graphics.TranslateTransform(point.X, point.Y);
+        Transform = Transform.Translate(point.X, point.Y, 0);
+        Graphics.Transform = ProjectMatrix(Transform);
     }
 
     public void Rotate(float angle)
     {
-        Graphics.RotateTransform(angle);
+        Transform = Transform.Rotate(0, 0, 1, angle);
+        Graphics.Transform = ProjectMatrix(Transform);
     }
 
     public void Scale(float scale)
     {
-        Graphics.ScaleTransform(scale, scale);
+        Scale(scale, scale);
     }
     public void Scale(float scaleX, float scaleY)
     {
-        Graphics.ScaleTransform(scaleX, scaleY);
+        Transform = Transform.Scale(scaleX, scaleY, 1);
+        Graphics.Transform = ProjectMatrix(Transform);
     }
 
     public float ConvertThickness(float thickness)
@@ -54,20 +113,26 @@ internal class DrawData
 
     public PointF TransformVec(PointF vec)
     {
-        return Graphics.Transform.TransformVector(vec);
+        var nVec = Vector4.Transform(new Vector4(vec.X, vec.Y, 0, 1), Transform);
+        return new(nVec.X, nVec.Y);
+    }
+    public Vector2 TransformVec(Vector2 vec)
+    {
+        var nVec = Vector4.Transform(new Vector4(vec.X, vec.Y, 0, 1), Transform);
+        return new(nVec.X, nVec.Y);
     }
 
     public float ScaleFactor()
     {
-        return ((System.Numerics.Vector2)TransformVec(new(1, 0))).Length();
+        return (TransformVec(new Vector2(1, 0))).Length();
     }
 
-    public static PointF ConvertPoint(Vector2 vec)
+    public static PointF ConvertPoint(netDxf.Vector2 vec)
     {
         return new PointF((float)vec.X, (float)vec.Y);
     }
 
-    public static PointF ConvertPoint(Vector3 vec)
+    public static PointF ConvertPoint(netDxf.Vector3 vec)
     {
         return new PointF((float)vec.X, (float)vec.Y);
     }

+ 3 - 3
inabox.dxf/DxfUtils.cs

@@ -135,10 +135,10 @@ public static class DxfUtils
         Brush _brush = new SolidBrush(Color.White);
         graphics.FillRectangle(_brush, 0, 0, graphics.VisibleClipBounds.Width, graphics.VisibleClipBounds.Height);
 
-        drawData.Graphics.TranslateTransform(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
+        drawData.Translate(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
 
-        drawData.Graphics.ScaleTransform(scale, scale);
-        drawData.Graphics.TranslateTransform(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
+        drawData.Scale(scale, scale);
+        drawData.Translate(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
 
         foreach(var el in data.Document.Entities.All)
         {

+ 28 - 14
inabox.dxf/Objects.cs

@@ -43,15 +43,29 @@ internal class DxfInsert : IDxfObject
     {
         if (!Insert.IsVisible || !data.Data.HasLayer(Insert.Layer)) return;
 
-        var transformation = Insert.GetTransformation();
-        var translation = Insert.Position - transformation * Insert.Block.Origin;
+        // var transformation = Insert.GetTransformation();
+        // var translation = Insert.Position - transformation * Insert.Block.Origin;
 
-        foreach(var entity in Insert.Block.Entities)
+        data.PushTransform();
+        data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
+        data.ArbitraryAxis(Insert.Normal);
+        data.Rotate((float)Insert.Rotation);
+        data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
+
+        foreach(var entity in Objects)
         {
-            entity.TransformBy(transformation, translation);
-            DxfUtils.ConvertEl(entity)?.Draw(data);
+            entity.Draw(data);
         }
 
+        data.PopTransform();
+
+        // foreach(var entity in Insert.Explode())
+        // {
+        //     // var ent = entity.Clone() as EntityObject;
+        //     // ent.TransformBy(transformation, translation);
+        //     DxfUtils.ConvertEl(entity)?.Draw(data);
+        // }
+
         // foreach(var obj in Insert.Explode())
         // {
         //     DxfUtils.ConvertEl(obj)?.Draw(data);
@@ -165,21 +179,21 @@ internal class DxfMText : IDxfObject
     {
         if (!MText.IsVisible || !data.Data.HasLayer(MText.Layer)) return;
 
-        Font font;
+        FontFamily fontFamily;
         if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
         {
-            font = SystemFonts.DefaultFont;
+            fontFamily = SystemFonts.DefaultFont.FontFamily;
         }
         else
         {
-            var fontFamily = new FontFamily(MText.Style.FontFamilyName);
-            font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
-            {
-                netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
-                netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
-                netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
-            });
+            fontFamily = new FontFamily(MText.Style.FontFamilyName);
         }
+        var font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
+        {
+            netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
+            netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
+            netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
+        });
 
         var text = MText.PlainText().Replace("^M", "");
 

+ 38 - 0
inabox.dxf/Utils.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Drawing2D;
 using System.Linq;
+using System.Numerics;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -26,4 +27,41 @@ internal static class Utils
     {
         return (float)(x - y * Math.Floor(x / y));
     }
+
+    public static Matrix4x4 Translate(this Matrix4x4 matrix, float x, float y, float z)
+    {
+        return Matrix4x4.CreateTranslation(x, y, z) * matrix;
+    }
+    public static Matrix4x4 Scale(this Matrix4x4 matrix, float x, float y, float z)
+    {
+        return Matrix4x4.CreateScale(x, y, z) * matrix;
+    }
+    public static Matrix4x4 Rotate(this Matrix4x4 matrix, float x, float y, float z, float angle)
+    {
+        var cos = (float)Math.Cos(angle * Math.PI / 180);
+        var sin = (float)Math.Sin(angle * Math.PI / 180);
+
+        var rotMatrix = new Matrix4x4();
+        rotMatrix.M11 = x * x * (1 - cos) + cos;
+        rotMatrix.M21 = x * y * (1 - cos) - z * sin;
+        rotMatrix.M31 = x * z * (1 - cos) + y * sin;
+
+        rotMatrix.M12 = y * x * (1 - cos) + z * sin;
+        rotMatrix.M22 = y * y * (1 - cos) + cos;
+        rotMatrix.M32 = y * z * (1 - cos) - x * sin;
+
+        rotMatrix.M13 = z * x * (1 - cos) - y * sin;
+        rotMatrix.M23 = z * y * (1 - cos) + x * sin;
+        rotMatrix.M33 = z * z * (1 - cos) + cos;
+
+        rotMatrix.M14 = 0;
+        rotMatrix.M24 = 0;
+        rotMatrix.M34 = 0;
+        rotMatrix.M44 = 1;
+        rotMatrix.M43 = 0;
+        rotMatrix.M42 = 0;
+        rotMatrix.M41 = 0;
+
+        return rotMatrix * matrix;
+    }
 }