Skip to content

Commit 642fc1b

Browse files
committed
Simplify transform calcs
1 parent 17069cc commit 642fc1b

3 files changed

Lines changed: 17 additions & 30 deletions

File tree

star/src/lower/visit.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -365,22 +365,7 @@ impl<'a, 'input, T: Turtle> XmlVisitor for ConversionVisitor<'a, 'input, T> {
365365
)
366366
};
367367

368-
let corners = [
369-
viewport_box.min,
370-
Point::new(viewport_box.max.x, viewport_box.min.y),
371-
Point::new(viewport_box.min.x, viewport_box.max.y),
372-
viewport_box.max,
373-
];
374-
let new_bounds = Box2D::from_points(
375-
corners.map(|p| self.terrarium.current_transform.transform_point(p)),
376-
);
377-
378-
let combined_bounds = match self.terrarium.current_bounds {
379-
Some(parent) => parent.intersection(&new_bounds),
380-
None => Some(new_bounds),
381-
};
382-
383-
self.terrarium.push_bounds(combined_bounds);
368+
self.terrarium.push_viewport_bounds(viewport_box);
384369
}
385370

386371
if !self.should_draw_node(node) {

star/src/turtle/elements/image_ops.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,10 @@ pub fn transform_image(
3131
let orig_w = image.width();
3232
let orig_h = image.height();
3333

34-
let pixel_corners = [
35-
point(0., 0.),
36-
point(orig_w as f64, 0.),
37-
point(0., orig_h as f64),
38-
point(orig_w as f64, orig_h as f64),
39-
];
40-
let user_corners = pixel_corners.map(|p| image_to_user.transform_point(p));
41-
let user_bounds = Box2D::from_points(user_corners);
42-
43-
let transformed_corners = user_corners.map(|p| user_to_final.transform_point(p));
44-
let transformed_box = Box2D::from_points(transformed_corners);
34+
let pixel_bounds = Box2D::new(point(0., 0.), point(orig_w as f64, orig_h as f64));
35+
let user_bounds = image_to_user.outer_transformed_box(&pixel_bounds);
36+
let image_to_final = image_to_user.then(user_to_final);
37+
let transformed_box = image_to_final.outer_transformed_box(&pixel_bounds);
4538

4639
let (is_transform_axis_aligned, [transformed_x_axis, transformed_y_axis]) = {
4740
let tx = user_to_final.transform_vector(vector(1.0, 0.0));
@@ -79,7 +72,7 @@ pub fn transform_image(
7972
// During non-aligned rotation, the corners need to be transparent
8073
image = add_alpha_channel(image);
8174

82-
let image_to_final = image_to_user.then(user_to_final);
75+
8376

8477
let scale_x = image_to_final.transform_vector(vector(1.0, 0.0)).length();
8578
let scale_y = image_to_final.transform_vector(vector(0.0, 1.0)).length();

star/src/turtle/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ pub(crate) struct Terrarium<T: Turtle + std::fmt::Debug> {
5757
has_begun: bool,
5858
current_position: Point<f64>,
5959
initial_position: Point<f64>,
60-
pub(crate) current_transform: Transform2D<f64>,
60+
current_transform: Transform2D<f64>,
6161
pub transform_stack: Vec<Transform2D<f64>>,
6262
previous_quadratic_control: Option<Point<f64>>,
6363
previous_cubic_control: Option<Point<f64>>,
6464
comment: Option<String>,
65-
pub(crate) current_bounds: Option<Box2D<f64>>,
65+
current_bounds: Option<Box2D<f64>>,
6666
bounds_stack: Vec<Option<Box2D<f64>>>,
6767
}
6868

@@ -84,6 +84,15 @@ impl<T: Turtle + std::fmt::Debug> Terrarium<T> {
8484
}
8585
}
8686

87+
pub fn push_viewport_bounds(&mut self, viewport_box: Box2D<f64>) {
88+
let new_bounds = self.current_transform.outer_transformed_box(&viewport_box);
89+
let combined_bounds = match self.current_bounds {
90+
Some(parent) => parent.intersection(&new_bounds),
91+
None => Some(new_bounds),
92+
};
93+
self.push_bounds(combined_bounds);
94+
}
95+
8796
pub fn push_bounds(&mut self, bounds: Option<Box2D<f64>>) {
8897
self.bounds_stack.push(self.current_bounds);
8998
self.current_bounds = bounds;

0 commit comments

Comments
 (0)