pub const PI: f32 = core::f32::consts::PI;
pub const TAU: f32 = core::f32::consts::TAU;
#[inline(always)]
pub fn abs_f32(x: f32) -> f32 {
f32::from_bits(x.to_bits() & 0x7FFF_FFFF)
}
#[inline]
pub fn floor_f32(x: f32) -> f32 {
{
x.floor()
}
}
#[inline]
pub fn ceil_f32(x: f32) -> f32 {
{
x.ceil()
}
}
#[inline]
pub fn round_f32(x: f32) -> f32 {
{
x.round()
}
}
#[inline]
pub fn sqrt_f32(x: f32) -> f32 {
{
if x <= 0.0 {
0.0
} else {
x.sqrt()
}
}
}
#[inline]
pub fn cbrt_f32(x: f32) -> f32 {
{
x.cbrt()
}
}
#[inline]
pub fn srgb_to_linear_f32(c: f32) -> f32 {
let x = c.clamp(0.0, 1.0);
if x <= 0.04045 {
return x / 12.92;
}
{
((x + 0.055) / 1.055).powf(2.4)
}
}
#[inline]
pub fn linear_to_srgb_f32(c: f32) -> f32 {
let x = c.clamp(0.0, 1.0);
if x <= 0.003_130_8 {
return x * 12.92;
}
{
1.055 * x.powf(1.0 / 2.4) - 0.055
}
}
#[inline]
pub fn sin_cos_f32(rad: f32) -> (f32, f32) {
{
rad.sin_cos()
}
}
#[inline]
pub fn tan_f32(rad: f32) -> f32 {
let (s, c) = sin_cos_f32(rad);
if abs_f32(c) < 1e-6 {
if s >= 0.0 {
1e6
} else {
-1e6
}
} else {
s / c
}
}
#[inline]
pub fn atan2_f32(y: f32, x: f32) -> f32 {
{
y.atan2(x)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Transform2D {
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub e: f32,
pub f: f32,
}
impl Default for Transform2D {
#[inline(always)]
fn default() -> Self {
Self::IDENTITY
}
}
impl Transform2D {
pub const IDENTITY: Self = Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
};
#[inline(always)]
pub const fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
Self { a, b, c, d, e, f }
}
#[inline(always)]
pub const fn translate(tx: f32, ty: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: tx,
f: ty,
}
}
#[inline(always)]
pub const fn scale(sx: f32, sy: f32) -> Self {
Self {
a: sx,
b: 0.0,
c: 0.0,
d: sy,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn rotate(deg: f32) -> Self {
let (s, c) = sin_cos_f32(deg * (PI / 180.0));
Self {
a: c,
b: s,
c: -s,
d: c,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn rotate_around(deg: f32, cx: f32, cy: f32) -> Self {
Self::translate(cx, cy)
.concat(Self::rotate(deg))
.concat(Self::translate(-cx, -cy))
}
#[inline]
pub fn skew_x(deg: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: tan_f32(deg * (PI / 180.0)),
d: 1.0,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn skew_y(deg: f32) -> Self {
Self {
a: 1.0,
b: tan_f32(deg * (PI / 180.0)),
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn concat(self, rhs: Self) -> Self {
Self {
a: self.a * rhs.a + self.c * rhs.b,
b: self.b * rhs.a + self.d * rhs.b,
c: self.a * rhs.c + self.c * rhs.d,
d: self.b * rhs.c + self.d * rhs.d,
e: self.a * rhs.e + self.c * rhs.f + self.e,
f: self.b * rhs.e + self.d * rhs.f + self.f,
}
}
#[inline(always)]
pub fn apply(self, x: f32, y: f32) -> (f32, f32) {
(
self.a * x + self.c * y + self.e,
self.b * x + self.d * y + self.f,
)
}
#[inline(always)]
pub fn is_identity(self) -> bool {
abs_f32(self.a - 1.0) < 1e-6
&& abs_f32(self.b) < 1e-6
&& abs_f32(self.c) < 1e-6
&& abs_f32(self.d - 1.0) < 1e-6
&& abs_f32(self.e) < 1e-6
&& abs_f32(self.f) < 1e-6
}
#[inline]
pub fn scale_around(sx: f32, sy: f32, cx: f32, cy: f32) -> Self {
Self::translate(cx, cy)
.concat(Self::scale(sx, sy))
.concat(Self::translate(-cx, -cy))
}
#[inline]
pub fn invert(self) -> Option<Self> {
let det = self.a * self.d - self.b * self.c;
if abs_f32(det) < 1e-8 || !det.is_finite() {
return None;
}
let inv = 1.0 / det;
let a = self.d * inv;
let b = -self.b * inv;
let c = -self.c * inv;
let d = self.a * inv;
let e = -(a * self.e + c * self.f);
let f = -(b * self.e + d * self.f);
Some(Self { a, b, c, d, e, f })
}
#[inline]
pub fn transform_region(self, r: crate::solver::Region) -> crate::solver::Region {
if r.is_empty() {
return crate::solver::Region::EMPTY;
}
let x0 = r.x as f32;
let y0 = r.y as f32;
let x1 = r.right() as f32;
let y1 = r.bottom() as f32;
let p0 = self.apply(x0, y0);
let p1 = self.apply(x1, y0);
let p2 = self.apply(x0, y1);
let p3 = self.apply(x1, y1);
let min_x = p0.0.min(p1.0).min(p2.0).min(p3.0);
let max_x = p0.0.max(p1.0).max(p2.0).max(p3.0);
let min_y = p0.1.min(p1.1).min(p2.1).min(p3.1);
let max_y = p0.1.max(p1.1).max(p2.1).max(p3.1);
let rx0 = (floor_f32(min_x) as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
let ry0 = (floor_f32(min_y) as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
let rx1 = (ceil_f32(max_x) as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
let ry1 = (ceil_f32(max_y) as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
crate::solver::Region::new(rx0, ry0, rx1.saturating_sub(rx0), ry1.saturating_sub(ry0))
}
#[inline]
pub fn centered_at(self, cx: f32, cy: f32) -> Self {
Self::translate(cx, cy)
.concat(self)
.concat(Self::translate(-cx, -cy))
}
#[inline]
pub fn approx_scale(self) -> f32 {
let sx = sqrt_f32(self.a * self.a + self.b * self.b);
let sy = sqrt_f32(self.c * self.c + self.d * self.d);
let avg = 0.5 * (sx + sy);
if avg > 1e-5 {
avg
} else {
1.0
}
}
pub fn parse_svg_attr(input: &str) -> Self {
let bytes = input.as_bytes();
let mut pos = 0usize;
let mut acc = Self::IDENTITY;
while pos < bytes.len() {
while pos < bytes.len() && is_sep(bytes[pos]) {
pos += 1;
}
if pos >= bytes.len() {
break;
}
let name_start = pos;
while pos < bytes.len() && bytes[pos].is_ascii_alphabetic() {
pos += 1;
}
let name = &input[name_start..pos];
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b'(' {
pos += 1;
continue;
}
pos += 1; let args_start = pos;
while pos < bytes.len() && bytes[pos] != b')' {
pos += 1;
}
let args_str = &input[args_start..pos];
if pos < bytes.len() && bytes[pos] == b')' {
pos += 1;
}
let mut nums = [0.0f32; 6];
let count = parse_number_list(args_str, &mut nums);
let step = if name.eq_ignore_ascii_case("translate") {
if count >= 2 {
Self::translate(nums[0], nums[1])
} else if count == 1 {
Self::translate(nums[0], 0.0)
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("scale") {
if count >= 2 {
Self::scale(nums[0], nums[1])
} else if count == 1 {
Self::scale(nums[0], nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("rotate") {
if count >= 3 {
Self::rotate_around(nums[0], nums[1], nums[2])
} else if count >= 1 {
Self::rotate(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("skewX") {
if count >= 1 {
Self::skew_x(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("skewY") {
if count >= 1 {
Self::skew_y(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("matrix") && count >= 6 {
Self::new(nums[0], nums[1], nums[2], nums[3], nums[4], nums[5])
} else {
Self::IDENTITY
};
acc = acc.concat(step);
}
acc
}
}
#[inline(always)]
fn is_sep(b: u8) -> bool {
b == b' ' || b == b'\t' || b == b'\r' || b == b'\n' || b == b','
}
pub(crate) fn parse_number_list(s: &str, out: &mut [f32]) -> usize {
let mut count = 0usize;
let mut cur = s;
while count < out.len() {
let Some((val, rest)) = parse_next_f32(cur) else {
break;
};
out[count] = val;
count += 1;
cur = rest;
}
count
}
pub(crate) fn parse_next_f32(s: &str) -> Option<(f32, &str)> {
let bytes = s.as_bytes();
let mut i = 0usize;
while i < bytes.len() && is_sep(bytes[i]) {
i += 1;
}
if i >= bytes.len() {
return None;
}
let start = i;
if bytes[i] == b'+' || bytes[i] == b'-' {
i += 1;
}
let mut has_digits = false;
while i < bytes.len() && bytes[i].is_ascii_digit() {
has_digits = true;
i += 1;
}
if i < bytes.len() && bytes[i] == b'.' {
i += 1;
while i < bytes.len() && bytes[i].is_ascii_digit() {
has_digits = true;
i += 1;
}
}
if !has_digits {
return None;
}
if i < bytes.len() && (bytes[i] == b'e' || bytes[i] == b'E') {
let exp_pos = i;
i += 1;
if i < bytes.len() && (bytes[i] == b'+' || bytes[i] == b'-') {
i += 1;
}
let mut exp_digits = false;
while i < bytes.len() && bytes[i].is_ascii_digit() {
exp_digits = true;
i += 1;
}
if !exp_digits {
i = exp_pos;
}
}
let token = &s[start..i];
let val = token.parse::<f32>().ok()?;
Some((val, &s[i..]))
}
pub(crate) fn parse_svg_length(s: &str) -> Option<f32> {
let trimmed = s.trim();
if trimmed.is_empty() {
return None;
}
parse_next_f32(trimmed).map(|(v, _)| v)
}