浏览代码

More work around skew/rotation

Kevin Lee 6 月之前
父节点
当前提交
217bac7f20
共有 3 个文件被更改,包括 41 次插入24 次删除
  1. 16 3
      src/ezcad/objects/circle.rs
  2. 14 12
      src/ezcad/objects/mod.rs
  3. 11 9
      src/ezcad/objects/polygon.rs

+ 16 - 3
src/ezcad/objects/circle.rs

@@ -2,6 +2,7 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
+use log::warn;
 
 use crate::{
     field_of::FieldOf,
@@ -26,13 +27,25 @@ pub struct Circle {
 
 impl Display for Circle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let (origin, width, height) = self.modifier.corrected(
+            *self.drawn_origin - Point::from(*self.radius),
+            *self.drawn_origin + Point::from(*self.radius),
+        );
+
+        if format!("{}", origin) != format!("{}", *self.core.origin) {
+            warn!(
+                "Origin mismatch! Core: {}, Calculated: {}",
+                *self.core.origin, origin
+            );
+        }
+
         write!(
             f,
             "{}, Origin: {}, Width : {:.2}, Height: {:.2}, Start Radian: {:.2}, Clockwise: {}",
             self.core,
-            self.modifier.translation() + *self.drawn_origin * self.modifier.scale(),
-            *self.radius * self.modifier.modifiers.x_scale * 2.0,
-            *self.radius * self.modifier.modifiers.y_scale * 2.0,
+            origin,
+            width,
+            height,
             *self.start_angle,
             *self.clockwise != 0,
         )

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

@@ -318,15 +318,6 @@ impl ObjectModifier {
 
     /// Returns origin, width, and height of bounding box after applying scale, skew, and  offset corrections
     pub fn corrected(&self, pt1: Point, pt2: Point) -> (Point, f64, f64) {
-        debug!(
-            "Skew degree from X axis: {:.2}",
-            self.modifiers.x_skew.asin().to_degrees()
-        );
-        debug!(
-            "Skew degree from Y axis: {:.2}",
-            self.modifiers.y_skew.asin().to_degrees()
-        );
-
         // Calculate 4 points of drawn rectangle
         let corners: Vec<Point> = vec![
             Point {
@@ -359,9 +350,20 @@ impl ObjectModifier {
         // Apply skew, then scale and translation
         let corners: Vec<Point> = corners
             .iter()
-            .map(|pt| Point {
-                x: pt.x + pt.y * self.modifiers.x_skew.asin().tan(),
-                y: pt.y + pt.x * self.modifiers.y_skew.asin().tan(),
+            .map(|pt| {
+                // TODO: WTF is this and why is it necessary?
+                // Seems to be correct as far as calculation of correct origin..
+                if self.modifiers.x_skew == 0.0 || self.modifiers.y_skew == 0.0 {
+                    Point {
+                        x: pt.x + pt.y * self.modifiers.x_skew,
+                        y: pt.y + pt.x * self.modifiers.y_skew,
+                    }
+                } else {
+                    Point {
+                        x: pt.x + pt.y * self.modifiers.x_skew.asin().tan(),
+                        y: pt.y + pt.x * self.modifiers.y_skew.asin().tan(),
+                    }
+                }
             })
             .inspect(|pt| debug!("pt: {pt}"))
             .map(|pt| pt * self.scale() + self.translation())

+ 11 - 9
src/ezcad/objects/polygon.rs

@@ -2,6 +2,7 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
+use log::warn;
 
 use crate::{
     field_of::FieldOf,
@@ -30,21 +31,22 @@ pub struct Polygon {
 
 impl Display for Polygon {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        let width: f64 = (self.drawn_corner_a.x.max(self.drawn_corner_b.x)
-            - self.drawn_corner_a.x.min(self.drawn_corner_b.x))
-            * self.modifier.modifiers.x_scale;
-        let height: f64 = (self.drawn_corner_a.y.max(self.drawn_corner_b.y)
-            - self.drawn_corner_a.y.min(self.drawn_corner_b.y))
-            * self.modifier.modifiers.y_scale;
+        let (origin, width, height) = self
+            .modifier
+            .corrected(*self.drawn_corner_a, *self.drawn_corner_b);
 
-        let drawn_origin: Point =
-            (*self.drawn_corner_a + *self.drawn_corner_b) / Point { x: 2.0, y: 2.0 };
+        if format!("{}", origin) != format!("{}", *self.core.origin) {
+            warn!(
+                "Origin mismatch! Core: {}, Calculated: {}",
+                *self.core.origin, origin
+            );
+        }
 
         write!(
             f,
             "{}, Origin: {}, Width: {:.2}, Height: {:.2}, Inverted: {}, Offset CX: {:.2}, Offset CY: {:.2}, Offset DX: {:.2}, Offset: DY: {:.2}, Edges: {}",
             self.core,
-            self.modifier.translation() + drawn_origin * self.modifier.scale(),
+            origin,
             width,
             height,
             *self.invert_shape != 0,