Browse Source

In progress origin work

Kevin Lee 11 tháng trước cách đây
mục cha
commit
eeabd4ce06

BIN
samples/Circle.mlp


BIN
samples/Circle2.mlp


BIN
samples/Circle3.mlp


BIN
samples/Circle4.mlp


BIN
samples/Circle5.mlp


BIN
samples/Rectangle2.mlp


+ 19 - 12
samples/config.yml

@@ -32,19 +32,26 @@ Ops:
   #   Input: !Existing { Layer: 0, Object: 0 }
   #   Export: 'export.bin'
 
+  # - !ObjectOperation
+  #   Input: !Existing { Layer: 0, Object: 0 }
+  #   Z: 1.0
+  #   Origin: { X: 10.0, Y: 10.0 }
+  #   Pen: 1
+  #   Array:
+  #     Columns: 3
+  #     Rows: 2
+  #     Spacing: 6.0
+  #     RandomizeOrder: False
+  #     StartingPen: 0
+  #   # Hatch:
+  #   #   LineSpacing: 0.01
+  #   ReplaceObject: 0
+
   - !ObjectOperation
-    Input: !Existing { Layer: 0, Object: 0 }
-    Z: 1.0
-    Origin: { X: 10.0, Y: 10.0 }
-    Pen: 1
-    Array:
-      Columns: 3
-      Rows: 2
-      Spacing: 6.0
-      RandomizeOrder: False
-      StartingPen: 0
-    # Hatch:
-    #   LineSpacing: 0.01
+    Input: !Circle { Radius: 10 }
+    Z: 0.0
+    Origin: { X: 0.0, Y: 0.0 }
+    Pen: 0
     ReplaceObject: 0
 
   # - !Object

+ 2 - 2
src/config/object.rs

@@ -139,8 +139,8 @@ impl InputObject {
                 height,
                 round_corner,
             } => Object::Rectangle(Rectangle {
-                corner_a: Coordinate::from((-width / 2.0, -height / 2.0)).into(),
-                corner_b: Coordinate::from((width / 2.0, height / 2.0)).into(),
+                drawn_corner_a: Coordinate::from((-width / 2.0, -height / 2.0)).into(),
+                drawn_corner_b: Coordinate::from((width / 2.0, height / 2.0)).into(),
                 round_bottom_left: round_corner.unwrap_or(0.0).into(),
                 round_bottom_right: round_corner.unwrap_or(0.0).into(),
                 round_top_left: round_corner.unwrap_or(0.0).into(),

+ 6 - 5
src/ezcad/objects/circle.rs

@@ -18,10 +18,11 @@ use super::{ObjectCore, Translate, ObjectModified};
 pub struct Circle {
     pub core: ObjectCore,
     #[brw(magic(6u32))] // Number of following fields in struct
-    pub origin: FieldOf<Coordinate>,
+    pub drawn_origin: FieldOf<Coordinate>,
     pub radius: F64,
     pub start_angle: F64, // Radians
     pub clockwise: U32,
+    // origin_x - drawn_x * width_scale = origin_x_delta
     pub modified: ObjectModified,
 }
 
@@ -31,7 +32,7 @@ impl Debug for Circle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Circle")
             .field("core", &self.core)
-            .field("origin", &self.origin)
+            .field("origin", &self.drawn_origin)
             .field("radius", &self.radius)
             .field("start_angle", &self.start_angle)
             .field("clockwise", &self.clockwise)
@@ -43,7 +44,7 @@ impl Default for Circle {
     fn default() -> Self {
         Self {
             core: ObjectCore::default(ObjectType::Circle),
-            origin: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_origin: Coordinate { x: 0.0, y: 0.0 }.into(),
             radius: 0.0.into(),
             start_angle: 0.0.into(),
             clockwise: 0.into(),
@@ -54,12 +55,12 @@ impl Default for Circle {
 
 impl Translate for Circle {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
-        origin.map(|origin| *self.origin = origin);
+        origin.map(|origin| *self.drawn_origin = origin);
         self.core.move_absolute(origin, z);
     }
 
     fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
-        delta.map(|delta| *self.origin += delta);
+        delta.map(|delta| *self.drawn_origin += delta);
         self.core.move_relative(delta, z);
     }
 }

+ 10 - 10
src/ezcad/objects/ellipse.rs

@@ -19,8 +19,8 @@ pub struct Ellipse {
     pub core: ObjectCore,
     #[brw(magic(8u32))] // Number of following fields in struct
     pub clockwise: U32,
-    pub corner_a: FieldOf<Coordinate>,
-    pub corner_b: FieldOf<Coordinate>,
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
     pub start_angle: F64, // Radians
     pub end_angle: F64,   // Radians
     pub modified: ObjectModified,
@@ -34,8 +34,8 @@ impl Debug for Ellipse {
         f.debug_struct("Ellipse")
             .field("core", &self.core)
             .field("clockwise", &self.clockwise)
-            .field("corner_a", &self.corner_a)
-            .field("corner_b", &self.corner_b)
+            .field("corner_a", &self.drawn_corner_a)
+            .field("corner_b", &self.drawn_corner_b)
             .field("start_angle", &self.start_angle)
             .field("end_angle", &self.end_angle)
             .field("open_curve", &self.open_curve)
@@ -48,8 +48,8 @@ impl Default for Ellipse {
         Self {
             core: ObjectCore::default(ObjectType::Ellipse),
             clockwise: 0.into(),
-            corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
             start_angle: 0.0.into(),
             end_angle: 0.0.into(),
             modified: ObjectModified::default(),
@@ -62,16 +62,16 @@ impl Translate for Ellipse {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         origin.map(|origin| {
             let delta: Coordinate = *self.core.origin - origin;
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_absolute(origin, z);
     }
 
     fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
         delta.map(|delta| {
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_relative(delta, z);
     }

+ 14 - 13
src/ezcad/objects/mod.rs

@@ -30,8 +30,9 @@ pub mod polygon;
 pub mod rectangle;
 
 pub trait Translate {
-    /// Move origin of object to point
+    /// Move origin by absolute coordinates
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>);
+    /// Move origin by relative coordinates
     fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>);
 }
 
@@ -257,18 +258,18 @@ impl Default for ObjectModified {
     #[derive(Debug, PartialEq)]
 ))]
 pub struct ScaleValues {
-    pub width_scale: f64,
+    pub x_scale: f64, // X scaled ratio on drawn object
     #[br(assert(_unknown_1 == 0.0))]
     _unknown_1: f64,
     #[br(assert(_unknown_2 == 0.0))]
     _unknown_2: f64,
     #[br(assert(_unknown_3 == 0.0))]
     _unknown_3: f64,
-    pub height_scale: f64,
+    pub y_scale: f64, // Y scaled ratio on drawn object
     #[br(assert(_unknown_4 == 0.0))]
     _unknown_4: f64,
-    pub origin_x_delta: f64,
-    pub origin_y_delta: f64,
+    pub x_correction: f64, // X correction factor due to scaling
+    pub y_correction: f64, // Y correction factor due to scaling
     #[br(assert(_unknown_5 == 1.0))]
     _unknown_5: f64,
 }
@@ -276,14 +277,14 @@ pub struct ScaleValues {
 impl Default for ScaleValues {
     fn default() -> Self {
         Self {
-            width_scale: 1.0.into(),
+            x_scale: 1.0.into(),
             _unknown_1: 0.0.into(),
             _unknown_2: 0.0.into(),
             _unknown_3: 0.0.into(),
-            height_scale: 1.0.into(),
+            y_scale: 1.0.into(),
             _unknown_4: 0.0.into(),
-            origin_x_delta: 0.0.into(),
-            origin_y_delta: 0.0.into(),
+            x_correction: 0.0.into(),
+            y_correction: 0.0.into(),
             _unknown_5: 1.0.into(),
         }
     }
@@ -293,10 +294,10 @@ impl Default for ScaleValues {
 impl Debug for ScaleValues {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("ScaleValues")
-            .field("width_scale", &self.width_scale)
-            .field("height_scale", &self.height_scale)
-            .field("origin_x_delta", &self.origin_x_delta)
-            .field("origin_y_delta", &self.origin_y_delta)
+            .field("width_scale", &self.x_scale)
+            .field("height_scale", &self.y_scale)
+            .field("origin_x_delta", &self.x_correction)
+            .field("origin_y_delta", &self.y_correction)
             .finish()
     }
 }

+ 10 - 10
src/ezcad/objects/polygon.rs

@@ -19,8 +19,8 @@ pub struct Polygon {
     pub core: ObjectCore,
     #[brw(magic(10u32))] // Number of following fields in struct
     pub invert_shape: U32,
-    pub corner_a: FieldOf<Coordinate>,
-    pub corner_b: FieldOf<Coordinate>,
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
     pub offset_cx: F64,
     pub offset_cy: F64,
     pub offset_dx: F64,
@@ -36,8 +36,8 @@ impl Debug for Polygon {
         f.debug_struct("Polygon")
             .field("core", &self.core)
             .field("invert_shape", &self.invert_shape)
-            .field("corner_a", &self.corner_a)
-            .field("corner_b", &self.corner_b)
+            .field("corner_a", &self.drawn_corner_a)
+            .field("corner_b", &self.drawn_corner_b)
             .field("offset_cx", &self.offset_cx)
             .field("offset_cy", &self.offset_cy)
             .field("offset_dx", &self.offset_dx)
@@ -52,8 +52,8 @@ impl Default for Polygon {
         Self {
             core: ObjectCore::default(ObjectType::Polygon),
             invert_shape: 1.into(),
-            corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
             offset_cx: 0.0.into(),
             offset_cy: 0.0.into(),
             offset_dx: 0.0.into(),
@@ -68,16 +68,16 @@ impl Translate for Polygon {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         origin.map(|origin| {
             let delta: Coordinate = *self.core.origin - origin;
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_absolute(origin, z);
     }
 
     fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
         delta.map(|delta| {
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_relative(delta, z);
     }

+ 12 - 10
src/ezcad/objects/rectangle.rs

@@ -18,13 +18,15 @@ use super::{ObjectCore, Translate, ObjectModified};
 pub struct Rectangle {
     pub core: ObjectCore,
     #[brw(magic(8u32))] // Number of following fields in struct
-    pub corner_a: FieldOf<Coordinate>,
-    pub corner_b: FieldOf<Coordinate>,
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
     pub round_bottom_left: F64,
     pub round_bottom_right: F64,
     pub round_top_right: F64,
     pub round_top_left: F64,
+    // origin_x = (corner_a.x + corner_b.x) / 2 * width_scale + origin_x_delta
     pub modified: ObjectModified,
+
 }
 
 // Custom Debug implementation to only print known fields
@@ -33,8 +35,8 @@ impl Debug for Rectangle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Rectangle")
             .field("core", &self.core)
-            .field("corner_a", &self.corner_a)
-            .field("corner_b", &self.corner_b)
+            .field("corner_a", &self.drawn_corner_a)
+            .field("corner_b", &self.drawn_corner_b)
             .field("round_bottom_left", &self.round_bottom_left)
             .field("round_bottom_right", &self.round_bottom_right)
             .field("round_top_right", &self.round_top_right)
@@ -47,8 +49,8 @@ impl Default for Rectangle {
     fn default() -> Self {
         Self {
             core: ObjectCore::default(ObjectType::Rectangle),
-            corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
             round_bottom_left: 0.0.into(),
             round_bottom_right: 0.0.into(),
             round_top_right: 0.0.into(),
@@ -62,16 +64,16 @@ impl Translate for Rectangle {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         origin.map(|origin| {
             let delta: Coordinate = *self.core.origin - origin;
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_absolute(origin, z);
     }
 
     fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
         delta.map(|delta| {
-            *self.corner_a += delta;
-            *self.corner_b += delta;
+            *self.drawn_corner_a += delta;
+            *self.drawn_corner_b += delta;
         });
         self.core.move_relative(delta, z);
     }