Files
Graphite/libraries/math-parser/src/object.rs
Keavon Chambers aa27f55ab5 Add matrices, with swizzle and range literals, to the math expression parser (#4611)
* Add matrices to the math expression parser as a second sort beside values

* Add Transform input and output to the Math f(x) node

* Add ranges and boxes to the math expression parser with inside, clamp, and remap over them

* Add Transform lists to the Math f(…) node with reducers composing, averaging, and counting them

* Lex a number's trailing e as Euler's number unless an exponent's digits follow it

* Fix matrix inversion scaling, exact matrix powers, infinite shears, matrix means, kept axes, and the Transform default

* Make shared box parts flat, give composed regions their inner map's axes, and fix lone logic items, spaced ranges, and the Math f(…) tooltip

* Transpose across spaces, name fractional matrix powers, check matrix builtin arities when parsing, read range literals by their corners, and let host functions shadow matrix builtins

* Make inside and clamp hold a region's uncovered parts at zero, so a box contains only the points between its corners

* Require hosts to declare the functions they provide, name the region functions' syntax node after its builtin, and remap onto ranges with equal infinite corners

* Apply a matrix to the whole product after it, so M 2 v is M (2 v)

* Pin prefix NOT's precedence above products, comparisons, and AND with tests

* Rename inside to within, since a range's ends count and the geometry predicates with a point first use that name

* Keep a range's corners as written, so clamp orders an integer past the reals' limit exactly

* Give within a tolerance of a billionth of the region's scale, so a point a float error off an edge or a flat side counts

* Write a unit part as its basis alone, so the identity displays as [1;i;j;k]

* Add tests for product folds, mixed chains, remap, and rejected inputs

* Evaluate matrix operands, builder arguments, regions, and comparison chains on the stack instead of the heap

* Build the expression parsers once per thread, since building the combinator tree cost as much as parsing a short expression

* Compare a chain reducer's items by their order directly, and reject an item with no order rather than reading it as false

* Send a host-built list of builder arguments or regions past the stack buffers to the heap instead of rejecting it
2026-09-26 15:11:16 -07:00

95 lines
1.9 KiB
Rust

use crate::matrix::Matrix;
use crate::value::Value;
use std::fmt;
/// What an expression evaluates to, one of the language's two sorts: a value or a matrix. The matrix is boxed so that a
/// value, the common case, stays small.
#[derive(Debug, Clone, PartialEq)]
pub enum Object {
Value(Value),
Matrix(Box<Matrix>),
}
/// Generates readers that see a value's parts and nothing of a matrix.
macro_rules! value_readers {
($($fn_name:ident: $type:ty),* $(,)?) => {
$(
#[doc = concat!("Reads the object as [`Value::", stringify!($fn_name), "`] does, or `None` for a matrix.")]
pub fn $fn_name(&self) -> Option<$type> {
self.as_value()?.$fn_name()
}
)*
};
}
impl Object {
#[inline]
pub fn as_value(&self) -> Option<&Value> {
match self {
Self::Value(value) => Some(value),
Self::Matrix(_) => None,
}
}
#[inline]
pub fn as_matrix(&self) -> Option<&Matrix> {
match self {
Self::Value(_) => None,
Self::Matrix(matrix) => Some(matrix),
}
}
#[inline]
pub fn into_value(self) -> Option<Value> {
match self {
Self::Value(value) => Some(value),
Self::Matrix(_) => None,
}
}
#[inline]
pub fn into_matrix(self) -> Option<Matrix> {
match self {
Self::Value(_) => None,
Self::Matrix(matrix) => Some(*matrix),
}
}
value_readers! {
as_real: f64,
as_f32: f32,
as_bool: bool,
as_u8: u8,
as_u16: u16,
as_u32: u32,
as_u64: u64,
as_u128: u128,
as_i8: i8,
as_i16: i16,
as_i32: i32,
as_i64: i64,
as_i128: i128,
}
}
impl<T: Into<Value>> From<T> for Object {
fn from(value: T) -> Self {
Self::Value(value.into())
}
}
impl From<Matrix> for Object {
fn from(matrix: Matrix) -> Self {
Self::Matrix(Box::new(matrix))
}
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Value(value) => value.fmt(f),
Self::Matrix(matrix) => matrix.fmt(f),
}
}
}