use crate::model::shape::ShapeComponentAttr;
pub(super) type Matrix = [f64; 6];
pub(super) type RenderMatrices = (Matrix, Vec<(Matrix, Matrix)>);
const IDENTITY: Matrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0];
pub(super) fn generated(attr: &ShapeComponentAttr) -> RenderMatrices {
let (trans, scale, rotation) = if attr.rotation_angle.rem_euclid(360) != 0 {
(
[
1.0,
0.0,
attr.offset_x as f64,
0.0,
1.0,
attr.offset_y as f64,
],
shape_scale_matrix(attr),
shape_rotation_matrix(attr),
)
} else if has_explicit_rendering_matrix(attr) {
(
[1.0, 0.0, attr.render_tx, 0.0, 1.0, attr.render_ty],
[
attr.render_sx,
attr.render_b,
0.0,
attr.render_c,
attr.render_sy,
0.0,
],
IDENTITY,
)
} else {
(IDENTITY, attr.local_reflection_matrix(), IDENTITY)
};
let mut pairs = vec![(scale, rotation)];
pairs.extend((1..attr.group_level.saturating_add(1)).map(|_| (IDENTITY, IDENTITY)));
(trans, pairs)
}
fn has_explicit_rendering_matrix(attr: &ShapeComponentAttr) -> bool {
const EPSILON: f64 = 0.000_001;
(attr.render_sx - 1.0).abs() > EPSILON
|| attr.render_b.abs() > EPSILON
|| attr.render_tx.abs() > EPSILON
|| attr.render_c.abs() > EPSILON
|| (attr.render_sy - 1.0).abs() > EPSILON
|| attr.render_ty.abs() > EPSILON
}
fn shape_scale_matrix(attr: &ShapeComponentAttr) -> [f64; 6] {
let sx = if attr.original_width > 0 && attr.current_width > 0 {
attr.current_width as f64 / attr.original_width as f64
} else {
1.0
};
let sy = if attr.original_height > 0 && attr.current_height > 0 {
attr.current_height as f64 / attr.original_height as f64
} else {
1.0
};
let reflection = attr.local_reflection_matrix();
[
sx * reflection[0],
0.0,
sx * reflection[2],
0.0,
sy * reflection[4],
sy * reflection[5],
]
}
fn shape_rotation_positive_correction(attr: &ShapeComponentAttr) -> (f64, f64) {
let width = attr.current_width as f64;
let height = attr.current_height as f64;
if width <= 0.0 || height <= 0.0 {
return (0.0, 0.0);
}
let theta = (attr.rotation_angle as f64).to_radians();
let cos = theta.cos();
let sin = theta.sin();
let corners = [
(0.0, 0.0),
(width * cos, width * sin),
(-height * sin, height * cos),
(width * cos - height * sin, width * sin + height * cos),
];
let min_x = corners
.iter()
.map(|(x, _)| *x)
.fold(f64::INFINITY, f64::min);
let min_y = corners
.iter()
.map(|(_, y)| *y)
.fold(f64::INFINITY, f64::min);
(-min_x, -min_y)
}
fn shape_rotation_matrix(attr: &ShapeComponentAttr) -> [f64; 6] {
if attr.rotation_angle.rem_euclid(360) == 0 {
return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0];
}
let theta = (attr.rotation_angle as f64).to_radians();
let cos = theta.cos();
let sin = theta.sin();
let (tx, ty) = shape_rotation_positive_correction(attr);
[
cos,
-sin,
tx - attr.offset_x as f64,
sin,
cos,
ty - attr.offset_y as f64,
]
}