plotine_core/
transform.rs1use crate::geom::{Point, Rect};
4use crate::scale::ScaleKind;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct Affine {
9 pub sx: f64,
10 pub sy: f64,
11 pub tx: f64,
12 pub ty: f64,
13}
14
15impl Affine {
16 pub const IDENTITY: Self = Self {
17 sx: 1.0,
18 sy: 1.0,
19 tx: 0.0,
20 ty: 0.0,
21 };
22
23 pub const fn new(sx: f64, sy: f64, tx: f64, ty: f64) -> Self {
24 Self { sx, sy, tx, ty }
25 }
26
27 pub fn apply(self, p: Point) -> Point {
28 Point::new(self.sx * p.x + self.tx, self.sy * p.y + self.ty)
29 }
30
31 pub fn then(self, other: Self) -> Self {
32 Self {
33 sx: other.sx * self.sx,
34 sy: other.sy * self.sy,
35 tx: other.sx * self.tx + other.tx,
36 ty: other.sy * self.ty + other.ty,
37 }
38 }
39}
40
41#[derive(Debug, Clone, Copy)]
43pub struct DataToPixel {
44 x_scale: ScaleKind,
45 y_scale: ScaleKind,
46 pixel_rect: Rect,
47 invert_y: bool,
50}
51
52impl DataToPixel {
53 pub fn new(x_scale: ScaleKind, y_scale: ScaleKind, pixel_rect: Rect) -> Self {
54 Self {
55 x_scale,
56 y_scale,
57 pixel_rect,
58 invert_y: false,
59 }
60 }
61
62 pub fn with_invert_y(mut self, invert: bool) -> Self {
64 self.invert_y = invert;
65 self
66 }
67
68 pub fn invert_y(self) -> bool {
69 self.invert_y
70 }
71
72 pub fn pixel_rect(self) -> Rect {
73 self.pixel_rect
74 }
75
76 pub fn x_scale(self) -> ScaleKind {
77 self.x_scale
78 }
79
80 pub fn y_scale(self) -> ScaleKind {
81 self.y_scale
82 }
83
84 pub fn map(&self, data: Point) -> Point {
86 let ux = self.x_scale.normalize(data.x);
87 let uy = self.y_scale.normalize(data.y);
88 let py = if self.invert_y {
89 self.pixel_rect.y0 + uy * self.pixel_rect.height()
90 } else {
91 self.pixel_rect.y1 - uy * self.pixel_rect.height()
92 };
93 Point::new(self.pixel_rect.x0 + ux * self.pixel_rect.width(), py)
94 }
95
96 pub fn map_x(&self, x: f64) -> f64 {
97 let ux = self.x_scale.normalize(x);
98 self.pixel_rect.x0 + ux * self.pixel_rect.width()
99 }
100
101 pub fn map_y(&self, y: f64) -> f64 {
102 let uy = self.y_scale.normalize(y);
103 if self.invert_y {
104 self.pixel_rect.y0 + uy * self.pixel_rect.height()
105 } else {
106 self.pixel_rect.y1 - uy * self.pixel_rect.height()
107 }
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114 use crate::scale::{LinearScale, ScaleKind};
115
116 #[test]
117 fn maps_corners() {
118 let x = ScaleKind::Linear(LinearScale::new(0.0, 10.0).unwrap());
119 let y = ScaleKind::Linear(LinearScale::new(0.0, 5.0).unwrap());
120 let rect = Rect::new(100.0, 50.0, 500.0, 350.0);
121 let t = DataToPixel::new(x, y, rect);
122
123 let bl = t.map(Point::new(0.0, 0.0));
124 assert!((bl.x - 100.0).abs() < 1e-9);
125 assert!((bl.y - 350.0).abs() < 1e-9);
126
127 let tr = t.map(Point::new(10.0, 5.0));
128 assert!((tr.x - 500.0).abs() < 1e-9);
129 assert!((tr.y - 50.0).abs() < 1e-9);
130 }
131
132 #[test]
133 fn inverted_y_puts_low_data_at_top() {
134 let x = ScaleKind::Linear(LinearScale::new(0.0, 1.0).unwrap());
135 let y = ScaleKind::Linear(LinearScale::new(-0.5, 1.5).unwrap());
136 let rect = Rect::new(0.0, 0.0, 100.0, 200.0);
137 let t = DataToPixel::new(x, y, rect).with_invert_y(true);
138 let top = t.map(Point::new(0.0, -0.5));
139 let bot = t.map(Point::new(0.0, 1.5));
140 assert!((top.y - 0.0).abs() < 1e-9);
141 assert!((bot.y - 200.0).abs() < 1e-9);
142 }
143}