From 5467c19c80c992f03890264ed58156305a26b19a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 23 Oct 2023 03:13:28 +0200 Subject: Replace `Primitive::Translate` with `Transform` --- graphics/src/damage.rs | 18 +++++----- graphics/src/primitive.rs | 32 +++++++++++------ graphics/src/transformation.rs | 80 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 106 insertions(+), 24 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs index ba9192ef..8edf69d7 100644 --- a/graphics/src/damage.rs +++ b/graphics/src/damage.rs @@ -102,10 +102,10 @@ impl Damage for Primitive { .fold(Rectangle::with_size(Size::ZERO), |a, b| { Rectangle::union(&a, &b) }), - Self::Translate { - translation, + Self::Transform { + transformation, content, - } => content.bounds() + *translation, + } => content.bounds() * *transformation, Self::Cache { content } => content.bounds(), Self::Custom(custom) => custom.bounds(), } @@ -144,19 +144,19 @@ fn regions(a: &Primitive, b: &Primitive) -> Vec { } } ( - Primitive::Translate { - translation: translation_a, + Primitive::Transform { + transformation: transformation_a, content: content_a, }, - Primitive::Translate { - translation: translation_b, + Primitive::Transform { + transformation: transformation_b, content: content_b, }, ) => { - if translation_a == translation_b { + if transformation_a == transformation_b { return regions(content_a, content_b) .into_iter() - .map(|r| r + *translation_a) + .map(|r| r * *transformation_a) .collect(); } } diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index aed59e1a..32008698 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -8,6 +8,7 @@ use crate::core::{ }; use crate::text::editor; use crate::text::paragraph; +use crate::Transformation; use std::sync::Arc; @@ -104,12 +105,12 @@ pub enum Primitive { /// The content of the clip content: Box>, }, - /// A primitive that applies a translation - Translate { - /// The translation vector - translation: Vector, + /// A primitive that applies a [`Transformation`] + Transform { + /// The [`Transformation`] + transformation: Transformation, - /// The primitive to translate + /// The primitive to transform content: Box>, }, /// A cached primitive. @@ -125,12 +126,12 @@ pub enum Primitive { } impl Primitive { - /// Creates a [`Primitive::Group`]. + /// Groups the current [`Primitive`]. pub fn group(primitives: Vec) -> Self { Self::Group { primitives } } - /// Creates a [`Primitive::Clip`]. + /// Clips the current [`Primitive`]. pub fn clip(self, bounds: Rectangle) -> Self { Self::Clip { bounds, @@ -138,10 +139,21 @@ impl Primitive { } } - /// Creates a [`Primitive::Translate`]. + /// Translates the current [`Primitive`]. pub fn translate(self, translation: Vector) -> Self { - Self::Translate { - translation, + Self::Transform { + transformation: Transformation::translate( + translation.x, + translation.y, + ), + content: Box::new(self), + } + } + + /// Transforms the current [`Primitive`]. + pub fn transform(self, transformation: Transformation) -> Self { + Self::Transform { + transformation, content: Box::new(self), } } diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs index cf0457a4..1aeb691e 100644 --- a/graphics/src/transformation.rs +++ b/graphics/src/transformation.rs @@ -1,4 +1,6 @@ -use glam::{Mat4, Vec3}; +use crate::core::{Point, Rectangle, Size, Vector}; + +use glam::{Mat4, Vec3, Vec4}; use std::ops::Mul; /// A 2D transformation matrix. @@ -6,10 +8,8 @@ use std::ops::Mul; pub struct Transformation(Mat4); impl Transformation { - /// Get the identity transformation. - pub fn identity() -> Transformation { - Transformation(Mat4::IDENTITY) - } + /// A [`Transformation`] that preserves whatever is transformed. + pub const IDENTITY: Self = Self(Mat4::IDENTITY); /// Creates an orthographic projection. #[rustfmt::skip] @@ -30,6 +30,26 @@ impl Transformation { pub fn scale(x: f32, y: f32) -> Transformation { Transformation(Mat4::from_scale(Vec3::new(x, y, 1.0))) } + + /// The scale factor on the X axis. + pub fn scale_x(&self) -> f32 { + self.0.x_axis.x + } + + /// The scale factor on the Y axis. + pub fn scale_y(&self) -> f32 { + self.0.y_axis.y + } + + /// The translation on the X axis. + pub fn translation_x(&self) -> f32 { + self.0.w_axis.x + } + + /// The translation on the Y axis. + pub fn translation_y(&self) -> f32 { + self.0.w_axis.y + } } impl Mul for Transformation { @@ -40,6 +60,56 @@ impl Mul for Transformation { } } +impl Mul for Point { + type Output = Self; + + fn mul(self, transformation: Transformation) -> Self { + let point = transformation + .0 + .mul_vec4(Vec4::new(self.x, self.y, 1.0, 1.0)); + + Point::new(point.x, point.y) + } +} + +impl Mul for Vector { + type Output = Self; + + fn mul(self, transformation: Transformation) -> Self { + let new_vector = transformation + .0 + .mul_vec4(Vec4::new(self.x, self.y, 1.0, 0.0)); + + Vector::new(new_vector.x, new_vector.y) + } +} + +impl Mul for Size { + type Output = Self; + + fn mul(self, transformation: Transformation) -> Self { + let new_size = transformation.0.mul_vec4(Vec4::new( + self.width, + self.height, + 1.0, + 0.0, + )); + + Size::new(new_size.x, new_size.y) + } +} + +impl Mul for Rectangle { + type Output = Self; + + fn mul(self, transformation: Transformation) -> Self { + let position = self.position(); + let size = self.size(); + + Self::new(position * transformation, size * transformation) + } +} + impl AsRef<[f32; 16]> for Transformation { fn as_ref(&self) -> &[f32; 16] { self.0.as_ref() -- cgit From a6e91d13d5d43796d0e6bb570fb4f010cf27921a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Oct 2023 03:18:03 +0200 Subject: Allow only uniform scaling in `Transformation` --- graphics/src/transformation.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs index 1aeb691e..e2642980 100644 --- a/graphics/src/transformation.rs +++ b/graphics/src/transformation.rs @@ -26,21 +26,16 @@ impl Transformation { Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0))) } - /// Creates a scale transformation. - pub fn scale(x: f32, y: f32) -> Transformation { - Transformation(Mat4::from_scale(Vec3::new(x, y, 1.0))) + /// Creates a uniform scaling transformation. + pub fn scale(scaling: f32) -> Transformation { + Transformation(Mat4::from_scale(Vec3::new(scaling, scaling, 1.0))) } - /// The scale factor on the X axis. - pub fn scale_x(&self) -> f32 { + /// The scale factor of the [`Transformation`]. + pub fn scale_factor(&self) -> f32 { self.0.x_axis.x } - /// The scale factor on the Y axis. - pub fn scale_y(&self) -> f32 { - self.0.y_axis.y - } - /// The translation on the X axis. pub fn translation_x(&self) -> f32 { self.0.w_axis.x -- cgit From f4d66486016076bb339a338bc589645119962d1e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Oct 2023 05:34:03 +0200 Subject: Introduce `with_transformation` to `Renderer` trait --- graphics/src/lib.rs | 2 - graphics/src/primitive.rs | 4 +- graphics/src/renderer.rs | 18 +++--- graphics/src/transformation.rs | 124 ----------------------------------------- graphics/src/viewport.rs | 4 +- 5 files changed, 12 insertions(+), 140 deletions(-) delete mode 100644 graphics/src/transformation.rs (limited to 'graphics/src') diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 76de56bf..aa9d00e8 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -19,7 +19,6 @@ mod antialiasing; mod error; mod primitive; -mod transformation; mod viewport; pub mod backend; @@ -46,7 +45,6 @@ pub use gradient::Gradient; pub use mesh::Mesh; pub use primitive::Primitive; pub use renderer::Renderer; -pub use transformation::Transformation; pub use viewport::Viewport; pub use iced_core as core; diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 32008698..6929b0a1 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -4,11 +4,11 @@ use crate::core::image; use crate::core::svg; use crate::core::text; use crate::core::{ - Background, Border, Color, Font, Pixels, Point, Rectangle, Shadow, Vector, + Background, Border, Color, Font, Pixels, Point, Rectangle, Shadow, + Transformation, Vector, }; use crate::text::editor; use crate::text::paragraph; -use crate::Transformation; use std::sync::Arc; diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index cb07c23b..143f348b 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -6,7 +6,7 @@ use crate::core::renderer; use crate::core::svg; use crate::core::text::Text; use crate::core::{ - Background, Color, Font, Pixels, Point, Rectangle, Size, Vector, + Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation, }; use crate::text; use crate::Primitive; @@ -73,20 +73,20 @@ impl Renderer { } /// Starts recording a translation. - pub fn start_translation(&mut self) -> Vec> { + pub fn start_transformation(&mut self) -> Vec> { std::mem::take(&mut self.primitives) } /// Ends the recording of a translation. - pub fn end_translation( + pub fn end_transformation( &mut self, primitives: Vec>, - translation: Vector, + transformation: Transformation, ) { let layer = std::mem::replace(&mut self.primitives, primitives); self.primitives - .push(Primitive::group(layer).translate(translation)); + .push(Primitive::group(layer).transform(transformation)); } } @@ -99,16 +99,16 @@ impl iced_core::Renderer for Renderer { self.end_layer(current, bounds); } - fn with_translation( + fn with_transformation( &mut self, - translation: Vector, + transformation: Transformation, f: impl FnOnce(&mut Self), ) { - let current = self.start_translation(); + let current = self.start_transformation(); f(self); - self.end_translation(current, translation); + self.end_transformation(current, transformation); } fn fill_quad( diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs deleted file mode 100644 index e2642980..00000000 --- a/graphics/src/transformation.rs +++ /dev/null @@ -1,124 +0,0 @@ -use crate::core::{Point, Rectangle, Size, Vector}; - -use glam::{Mat4, Vec3, Vec4}; -use std::ops::Mul; - -/// A 2D transformation matrix. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Transformation(Mat4); - -impl Transformation { - /// A [`Transformation`] that preserves whatever is transformed. - pub const IDENTITY: Self = Self(Mat4::IDENTITY); - - /// Creates an orthographic projection. - #[rustfmt::skip] - pub fn orthographic(width: u32, height: u32) -> Transformation { - Transformation(Mat4::orthographic_rh_gl( - 0.0, width as f32, - height as f32, 0.0, - -1.0, 1.0 - )) - } - - /// Creates a translate transformation. - pub fn translate(x: f32, y: f32) -> Transformation { - Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0))) - } - - /// Creates a uniform scaling transformation. - pub fn scale(scaling: f32) -> Transformation { - Transformation(Mat4::from_scale(Vec3::new(scaling, scaling, 1.0))) - } - - /// The scale factor of the [`Transformation`]. - pub fn scale_factor(&self) -> f32 { - self.0.x_axis.x - } - - /// The translation on the X axis. - pub fn translation_x(&self) -> f32 { - self.0.w_axis.x - } - - /// The translation on the Y axis. - pub fn translation_y(&self) -> f32 { - self.0.w_axis.y - } -} - -impl Mul for Transformation { - type Output = Self; - - fn mul(self, rhs: Self) -> Self { - Transformation(self.0 * rhs.0) - } -} - -impl Mul for Point { - type Output = Self; - - fn mul(self, transformation: Transformation) -> Self { - let point = transformation - .0 - .mul_vec4(Vec4::new(self.x, self.y, 1.0, 1.0)); - - Point::new(point.x, point.y) - } -} - -impl Mul for Vector { - type Output = Self; - - fn mul(self, transformation: Transformation) -> Self { - let new_vector = transformation - .0 - .mul_vec4(Vec4::new(self.x, self.y, 1.0, 0.0)); - - Vector::new(new_vector.x, new_vector.y) - } -} - -impl Mul for Size { - type Output = Self; - - fn mul(self, transformation: Transformation) -> Self { - let new_size = transformation.0.mul_vec4(Vec4::new( - self.width, - self.height, - 1.0, - 0.0, - )); - - Size::new(new_size.x, new_size.y) - } -} - -impl Mul for Rectangle { - type Output = Self; - - fn mul(self, transformation: Transformation) -> Self { - let position = self.position(); - let size = self.size(); - - Self::new(position * transformation, size * transformation) - } -} - -impl AsRef<[f32; 16]> for Transformation { - fn as_ref(&self) -> &[f32; 16] { - self.0.as_ref() - } -} - -impl From for [f32; 16] { - fn from(t: Transformation) -> [f32; 16] { - *t.as_ref() - } -} - -impl From for Mat4 { - fn from(transformation: Transformation) -> Self { - transformation.0 - } -} diff --git a/graphics/src/viewport.rs b/graphics/src/viewport.rs index 5792555d..dc8e21d3 100644 --- a/graphics/src/viewport.rs +++ b/graphics/src/viewport.rs @@ -1,6 +1,4 @@ -use crate::Transformation; - -use iced_core::Size; +use crate::core::{Size, Transformation}; /// A viewing region for displaying computer graphics. #[derive(Debug, Clone)] -- cgit