Overview
Engine math: vectors, quaternions and matrices that don't allocate in hot loops, spherical harmonics for diffuse lighting, and low-discrepancy sequences for sampling.
What's in it
| File | Contents |
|---|---|
vec.ray | Vec2, Vec3 (dot, cross by SIMD shuffles, reflect, lerp), Vec4 (perspective divide) |
quat.ray | Quat: axis-angle, Hamilton product, fast rotate (v + 2w(u \times v) + 2u \times (u \times v)), shortest-arc slerp |
mat.ray | Mat4: TRS, look-at, perspective (z in [0,1] for Metal/Vulkan/WebGPU), multiply, transpose, inverse; Mat3; Mat4Buffer |
sh.ray | Order-2 real spherical harmonics (9 coefficients), RGB projection and cosine-convolved irradiance (Ramamoorthi and Hanrahan 2001) |
sequences.ray | Radical inverse, van der Corput, Hammersley, Halton, Roberts R2, uniform sphere mapping, GGX importance sampling (Karis 2013) |
Ratio geometry
The one geometry API for Riptide: quadrance, spread, cross, twist, solid and dihedral spreads, reflections, and rotations from rational Cayley parameters or two reflections (Turn, RatioQuat), against a metric (Metric2, Metric3), with radians only through RatioBoundary. RatioF holds every float formula once (inlined: the machine code is the formula's); RatioBackend is the contract for f64, f32, exact int64 (Int64Ratio) and, in riptide_rational_geometry, exact fields (FieldRatio); RatioBatch / RatioMsl are the batched CPU and Metal kernels. package:riptide_math/ratio_laws.ray checks any backend against the laws.
Exact numbers
Decimal: exact (BigInt unscaled × 10^{-\mathrm{scale}}) for money.fromMinorUnits/toMinorUnitsmatch riptide_finance_core's integer minor units (USD 2, JPY 0, BHD 3), with 8 rounding modes (half-even by default). Compare withequals/compareTo(numeric), not==, which compares the representation.U8,U16,U32,U64(extension types overint) andU128,I128(over a pair of ints): wrapping operators,checked*(throwIntegerOverflow),saturating*, bit ops, shifts andparse. Tested against BigInt on random values.
AOT, 2M iterations (benchmark/int_bench.ray): U128 multiply-add 168.8 ns against 754.8 ns for BigInt mod 2^{128} (same result); a Decimal add is 57 ns.
Functions and constants
Functions
List<Vec3>? gjk(Convex a, Convex b, {int maxIterations = 64})GJK intersection test (Gilbert, Johnson and Keerthi 1988): A and B overlap iff their Minkowski difference A - B contains the origin. Returns the final simplex (4 points when intersecting) for epa, or null when separated.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = SphereShape(Vec3(0, 0, 0), 1), b = BoxShape(Vec3(1.5, 0, 0), Vec3(0.75, 0.5, 0.5));
final c = BoxShape(Vec3(3, 0, 0), Vec3(0.5, 0.5, 0.5));
print('sphere vs near box: ${gjk(a, b) != null ? 'overlap' : 'apart'}');
print('sphere vs far box: ${gjk(a, c) != null ? 'overlap' : 'apart'}');
}sphere vs near box: overlap sphere vs far box: apart
({Vec3 normal, double depth})? epa(Convex a, Convex b, List<Vec3> simplex, {int maxIterations = 128, double tolerance = 1e-4})EPA (Expanding Polytope Algorithm, van den Bergen 2001): from GJK's enclosing simplex, expands toward the boundary of A - B to find the penetration depth and normal: moving B by normal × depth separates the shapes.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = SphereShape(Vec3(0, 0, 0), 1), b = SphereShape(Vec3(1.5, 0, 0), 1);
final s = gjk(a, b)!;
final p = epa(a, b, s)!;
print('penetration depth ${f(p.depth)} along ${v3(p.normal)} (exact 0.5)');
}penetration depth 0.5000 along (1.0000, 0.0000, 0.0093) (exact 0.5)
bool obbOverlap(Obb a, Obb b)Separating axis theorem for two oriented boxes: they're disjoint iff some axis among the 3 + 3 face normals and the 9 edge cross products separates their projections (Gottschalk et al. 1996).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final r = Quat.axisAngle(Vec3.unitZ, math.pi / 4);
Obb box(double x, Quat q) => Obb(Vec3(x, 0, 0), [q.rotate(Vec3.unitX), q.rotate(Vec3.unitY), Vec3.unitZ], Vec3(1, 1, 1));
print('axis-aligned at 0, turned 45° at 2.3: ${obbOverlap(box(0, Quat.identity), box(2.3, r))}');
print('turned at 2.5: ${obbOverlap(box(0, Quat.identity), box(2.5, r))}');
}axis-aligned at 0, turned 45° at 2.3: true turned at 2.5: false
double sphereDepth(SphereShape a, SphereShape b)Distance helper used by tests and physics: penetration depth of two spheres (negative when separated).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = SphereShape(Vec3(0, 0, 0), 1);
for (final x in [1.5, 2.0, 2.5]) {
print('centres $x apart: depth ${sphereDepth(a, SphereShape(Vec3(x, 0, 0), 1))}');
}
}centres 1.5 apart: depth 0.5 centres 2.0 apart: depth 0.0 centres 2.5 apart: depth -0.5
Vec3 bezier(List<Vec3> ctrl, double t)Bézier curve of any degree by de Casteljau's algorithm: repeated linear interpolation of the control polygon (numerically stable, gives the point and the subdivision).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ctrl = [Vec3(0, 0, 0), Vec3(1, 3, 0), Vec3(3, 3, 0), Vec3(4, 0, 0)];
final c = [for (var i = 0; i <= 40; i++) bezier(ctrl, i / 40)];
print('B(0.5) = ${v3(bezier(ctrl, 0.5))}');
}B(0.5) = (2.0000, 2.2500, 0.0000)
Vec3 catmullRom(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double t, {double alpha = 0.5})Uniform cubic Catmull–Rom through p_{1} \to p_{2} with neighbours p0, p3, centripetal parameterisation (\alpha = 0.5) by default, which avoids cusps and self-intersections (Yuksel et al. 2011).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ctrl = [Vec3(0, 0, 0), Vec3(1, 3, 0), Vec3(3, 3, 0), Vec3(4, 0, 0)];
final c = [for (var i = 0; i <= 30; i++) catmullRom(ctrl[0], ctrl[1], ctrl[2], ctrl[3], i / 30)];
print('passes through P1 at t = 0: ${v3(c.first)}, P2 at t = 1: ${v3(c.last)}');
}passes through P1 at t = 0: (1.0000, 3.0000, 0.0000), P2 at t = 1: (3.0000, 3.0000, 0.0000)
Vec3 deBoor(int degree, List<double> knots, List<Vec3> ctrl, double u, {List<double>? weights})B-spline / NURBS evaluation by de Boor's algorithm. knots has ctrl.length + degree + 1 entries; weights (optional) makes it a NURBS curve (rational: evaluate in homogeneous coordinates, then divide by w).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ctrl = [Vec3(0, 0, 0), Vec3(1, 2, 0), Vec3(2, -1, 0), Vec3(3, 2, 0), Vec3(4, 0, 0)];
final knots = clampedKnots(ctrl.length, 3);
final c = [for (var i = 0; i <= 50; i++) deBoor(3, knots, ctrl, i / 50)];
print('knots $knots');
}knots [0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0]
List<double> clampedKnots(int count, int degree)A clamped uniform knot vector for count control points of degree (curve starts/ends at the end points).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('4 points, degree 3: ${clampedKnots(4, 3)}');
print('6 points, degree 2: ${clampedKnots(6, 2)}');
}4 points, degree 3: [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0] 6 points, degree 2: [0.0, 0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0, 1.0]
({double t, double u, double v})? rayTriangle(Ray ray, Vec3 a, Vec3 b, Vec3 c, {bool cullBack = false, double tMin = 1e-7})Ray–triangle intersection (Möller and Trumbore 1997): solves o + t d = (1-u-v) a + u b + v c by Cramer's rule. Returns (t, u, v) for the closest hit with t > tMin, or null. Hits from both sides count unless cullBack.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ray = Ray(Vec3(0, 0, 5), Vec3(0, 0, -1));
final h = rayTriangle(ray, Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(0, 1, 0));
print('t = ${f(h!.t)}, barycentric u = ${f(h.u)}, v = ${f(h.v)}');
print('miss: ${rayTriangle(Ray(Vec3(5, 5, 5), Vec3(0, 0, -1)), Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(0, 1, 0))}');
}t = 5.0000, barycentric u = 0.2500, v = 0.5000 miss: null
({double tNear, double tFar})? rayAabb(Ray ray, Vec3 min, Vec3 max, {double tMax = double.infinity})Ray–AABB slab test (Kay and Kajiya 1986; branchless form after Williams et al. 2005). Returns the entry/exit t-interval clipped to [0, tMax], or null.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ray = Ray(Vec3(-3, 0.5, 0), Vec3(1, 0.2, 0).normalized());
final h = rayAabb(ray, Vec3(-1, -1, -1), Vec3(1, 1, 1))!;
final a = ray.at(h.tNear), b = ray.at(h.tFar);
print('enters at t = ${f(h.tNear)}, leaves at t = ${f(h.tFar)}');
}enters at t = 2.0396, leaves at t = 2.5495
double? raySphere(Ray ray, Vec3 center, double radius)Ray–sphere: the smallest t \ge 0 where |o + t d - c| = r, or null. Uses the numerically stable half-b form.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ray = Ray(Vec3(-3, 0.5, 0), Vec3(1, 0, 0));
final t = raySphere(ray, Vec3.zero, 1)!;
final p = ray.at(t);
print('t = ${f(t)}, hit ${v3(p)}');
}t = 2.1340, hit (-0.8660, 0.5000, 0.0000)
double? rayPlane(Ray ray, Plane plane)Ray–plane: t where the ray meets the plane, or null when parallel or behind.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final ground = Plane.fromPointNormal(Vec3.zero, Vec3.unitY);
final ray = Ray(Vec3(0, 3, 0), Vec3(1, -1, 0).normalized());
final t = rayPlane(ray, ground)!;
print('hits y = 0 at ${v3(ray.at(t))}');
}hits y = 0 at (3.0000, 0.0000, 0.0000)
Vec3 closestPointOnSegment(Vec3 p, Vec3 a, Vec3 b)The point on segment [a, b] closest to p.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(0, 0, 0), b = Vec3(4, 2, 0);
final ps = [Vec3(1, 3, 0), Vec3(-2, 1, 0), Vec3(5, 4, 0)];
for (final p in ps) {
print('${v3(p)} -> ${v3(closestPointOnSegment(p, a, b))}');
}
}(1.0000, 3.0000, 0.0000) -> (2.0000, 1.0000, 0.0000) (-2.0000, 1.0000, 0.0000) -> (0.0000, 0.0000, 0.0000) (5.0000, 4.0000, 0.0000) -> (4.0000, 2.0000, 0.0000)
Vec3 closestPointOnTriangle(Vec3 p, Vec3 a, Vec3 b, Vec3 c)The point on triangle abc closest to p, by Voronoi regions (Ericson, "Real-Time Collision Detection", 5.1.5).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(0, 0, 0), b = Vec3(4, 0, 0), c = Vec3(1, 3, 0);
final ps = [Vec3(2, 1, 2), Vec3(5, 2, 0), Vec3(-1, -1, 0)];
for (final p in ps) {
print('${v3(p)} -> ${v3(closestPointOnTriangle(p, a, b, c))}');
}
}(2.0000, 1.0000, 2.0000) -> (2.0000, 1.0000, 0.0000) (5.0000, 2.0000, 0.0000) -> (3.5000, 0.5000, 0.0000) (-1.0000, -1.0000, 0.0000) -> (0.0000, 0.0000, 0.0000)
({Float64List values, Mat3d vectors}) symmetricEigen3(Mat3d a, {int maxSweeps = 32})Eigen-decomposition of a symmetric 3 \times 3 matrix by cyclic Jacobi rotations: A = V \operatorname{diag}(\lambda) V^{T}. Eigenvalues are sorted descending; V's columns are the matching unit eigenvectors (a right-handed rotation).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.of([4, 1, 0, 1, 3, 1, 0, 1, 2]);
final e = symmetricEigen3(a);
print('eigenvalues ${e.values.map(f).toList()}');
}eigenvalues [4.7321, 3.0000, 1.2679]
({Mat3d u, Float64List sigma, Mat3d v}) svd3(Mat3d a)Singular value decomposition A = U \sum V^{T} of a 3 \times 3 matrix (\sigma descending, ≥ 0), via the eigen-decomposition of A^{T}A. U and V are rotations; if \det(A) < 0 the smallest \sigma is negated so U, V stay proper (the "signed SVD" used for polar decomposition and shape matching).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.of([2, 0, 0, 0, 0, 3, 0, -1, 0]);
final s = svd3(a);
print('singular values ${s.sigma.map(f).toList()}');
}singular values [3.0000, 2.0000, 1.0000]
({Mat3d r, Mat3d s}) polar3(Mat3d a)Polar decomposition A = R S with R the closest rotation to A and S symmetric positive semi-definite.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A rotation times a stretch: the polar decomposition recovers both.
final a = Mat3d.of([0, -2, 0, 1, 0, 0, 0, 0, 1]);
final p = polar3(a);
print('R = ${[for (var i = 0; i < 9; i++) f(p.r.at(i ~/ 3, i % 3))]}');
print('S = ${[for (var i = 0; i < 9; i++) f(p.s.at(i ~/ 3, i % 3))]}');
}R = [0.0000, -1.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000] S = [1.0000, 0.0000, 0.0000, 0.0000, 2.0000, 0.0000, 0.0000, 0.0000, 1.0000]
bool solveLinear(Float64List m, Float64List b, int n)Solves the n \times n system M x = b in place by Gaussian elimination with partial pivoting (row-major m). Returns false when singular.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Float64List.fromList([2, 1, 1, 1, 3, 2, 1, 0, 0]);
final b = Float64List.fromList([4, 5, 6]);
final ok = solveLinear(m, b, 3);
print('solved: $ok, x = $b');
print('singular: ${solveLinear(Float64List.fromList([1, 2, 2, 4]), Float64List.fromList([1, 1]), 2)}');
}solved: true, x = [6.0, 15.000000000000004, -23.000000000000004] singular: false
List<(int, int, int)> convexHull3(Float32x4List pts)Convex hull of a 3D point set by the incremental method with conflict-free face visibility (the same horizon-stitching step QuickHull uses; this version visits points in input order instead of farthest-first, so it is O(n^{2}) worst case). Returns triangles as index triples, wound counter-clockwise seen from outside.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final rng = math.Random(1);
final pts = Float32x4List.fromList([for (var i = 0; i < 30; i++) Float32x4(rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), 0)]);
final faces = convexHull3(pts);
print('${faces.length} triangles on the hull of 30 points');
}95 triangles on the hull of 30 points
List<(int, int, int)> earClip(List<Vec2> poly)Ear-clipping triangulation of a simple polygon (2D, either winding). Returns index triples into poly.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final poly = [Vec2(0, 0), Vec2(4, 0), Vec2(4, 3), Vec2(2, 1), Vec2(0, 3)];
final tris = earClip(poly);
print('${tris.length} triangles: $tris');
}3 triangles: [(1, 2, 3), (0, 1, 3), (0, 3, 4)]
List<(int, int, int)> delaunay2(List<Vec2> pts)Delaunay triangulation of 2D points by Bowyer–Watson: insert points one by one, remove triangles whose circumcircle contains the point, re-triangulate the hole. O(n^{2}) in this simple form.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final rng = math.Random(4);
final pts = [for (var i = 0; i < 25; i++) Vec2(rng.nextDouble() * 10, rng.nextDouble() * 10)];
final tris = delaunay2(pts);
print('${tris.length} triangles');
}40 triangles
Float32x4List computeTangents(Float32x4List positions, Float32x4List normals, Float32List uvs, Uint32List indices)Per-vertex tangent frames for normal mapping (Lengyel's method: solve for the UV-gradient directions per triangle, accumulate, Gram–Schmidt against the normal, store handedness in w). MikkTSpace adds specific accumulation and splitting rules for bit-exact agreement with bakers; this follows the same math but not its exact ordering.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A unit square of two triangles, with positions, normals and uvs.
final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
final nrm = Float32x4List.fromList(List.filled(4, Float32x4(0, 0, 1, 0)));
final uv = Float32List.fromList([0, 0, 1, 0, 1, 1, 0, 1]);
final t = computeTangents(pos, nrm, uv, idx);
print('tangent at vertex 0: (${t[0].x}, ${t[0].y}, ${t[0].z}), handedness ${t[0].w}');
}tangent at vertex 0: (1.0, 0.0, 0.0), handedness 1.0
({Float32x4List positions, Uint32List indices}) simplifyQem(Float32x4List positions, Uint32List indices, int targetTriangles)Quadric error metric simplification (Garland and Heckbert 1997): each vertex accumulates the sum of squared distances to its faces' planes as a 4 \times 4 quadric Q; collapsing edge (i, j) to v costs v^{T}(Q_{i} + Q_{j})v. Edges are collapsed cheapest-first until targetTriangles remain. Returns new positions and indices.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A 6 × 6 flat grid: the quadric error metric can merge it down to few triangles.
const n = 6;
final pos = Float32x4List.fromList([for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) Float32x4(i.toDouble(), j.toDouble(), 0, 0)]);
final idx = Uint32List.fromList([for (var i = 0; i < n - 1; i++) for (var j = 0; j < n - 1; j++) ...[i * n + j, (i + 1) * n + j, (i + 1) * n + j + 1, i * n + j, (i + 1) * n + j + 1, i * n + j + 1]]);
final s = simplifyQem(pos, idx, 8);
print('${idx.length ~/ 3} -> ${s.indices.length ~/ 3} triangles');
}50 -> 8 triangles
double kahanSum(Iterable<double> values)Compensated summation: Neumaier's variant of Kahan summation keeps the low-order bits lost by each addition, so summing many floats of different magnitude stays accurate.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final xs = List<double>.filled(1000000, 0.1);
var naive = 0.0;
for (final x in xs) {
naive += x;
}
print('naive $naive');
print('Kahan ${kahanSum(xs)}');
}naive 100000.00000133288 Kahan 100000.0
double contactImpulse({required Float32x4List vel, required Float32List invMass, required int a, required int b, required Vec3 normal, required double accumulated, double restitution = 0})One sequential-impulse contact solve along normal n (Catto 2005): computes the impulse that removes approaching velocity (with restitution), clamps the accumulated impulse to ≥ 0 so contacts only push, and applies it to the two bodies' linear velocities. Returns the new accumulated impulse.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Two bodies approaching along x at 1 m/s each; one contact normal (1, 0, 0) from a to b.
final vel = Float32x4List.fromList([Float32x4(1, 0, 0, 0), Float32x4(-1, 0, 0, 0)]);
final inv = Float32List.fromList([1, 1]);
final j = contactImpulse(vel: vel, invMass: inv, a: 0, b: 1, normal: Vec3.unitX, accumulated: 0, restitution: 1);
print('impulse $j; velocities now ${vel[0].x}, ${vel[1].x} (elastic bounce)');
}impulse 2.0; velocities now -1.0, 1.0 (elastic bounce)
({double value, double velocity}) springDamp(double value, double velocity, double target, double omega, double dt)Critically damped spring smoothing toward a target (exact solution of x'' = -2\omega x' - \omega^{2} (x − target), no overshoot, frame-rate independent). omega = 2 / smoothTime is a good default.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
var v = 0.0, x = 0.0;
final path = <List<double>>[];
for (var i = 0; i < 60; i++) {
final s = springDamp(x, v, 1, 8, 1 / 30);
x = s.value;
v = s.velocity;
path.add([i / 30, x]);
}
print('after 2 s: ${f(x)} (target 1, no overshoot)');
}after 2 s: 1.0000 (target 1, no overshoot)
List<Vec2> poissonDisk(double width, double height, double r, {int k = 30, int seed = 1})Poisson-disk (blue noise) points in the width × height rectangle with minimum distance r (Bridson 2007).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final pts = poissonDisk(10, 10, 1, seed: 3);
var minD = 1e9;
for (var i = 0; i < pts.length; i++) {
for (var j = i + 1; j < pts.length; j++) {
minD = math.min(minD, (pts[i] - pts[j]).length);
}
}
print('${pts.length} points, closest pair ${f(minD)} (≥ 1)');
}67 points, closest pair 1.0040 (≥ 1)
Float32List rotateSh9(Float32List coeffs, Vec3 Function(Vec3) inverseRotate)Rotation of order-2 SH coefficients (9 per channel) by re-projection: SH bands are closed under rotation, so evaluating the function at 9 fixed directions rotated by R^{-1} and solving the 9 \times 9 basis system gives the rotated coefficients exactly (up to round-off). Slower than the Ivanic–Ruedenberg recurrence but simple and exact.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Light from +x projected to SH, then rotated 90° about z: it should come from +y.
final sh = ShRgb()..addSample(Vec3.unitX, Vec3(1, 1, 1), 1);
final red = Float32List.fromList([for (var i = 0; i < 9; i++) sh.coeffs[3 * i]]);
final q = Quat.axisAngle(Vec3.unitZ, math.pi / 2);
final r = rotateSh9(red, (d) => q.conjugate.rotate(d));
final y = Float32List(9);
Sh9.basis(Vec3.unitY, y);
var atY = 0.0;
for (var i = 0; i < 9; i++) {
atY += r[i] * y[i];
}
print('rotated light seen from +y: ${f(atY)}');
}rotated light seen from +y: -0.0439
Classes
Bvh
final class Bvh
A bounding volume hierarchy over triangles, built top-down with the binned surface area heuristic (SAH; Wald 2007): split where C = C_{\mathrm{trav}} + (\frac{A_{L}}{A}) N_{L} C_{\mathrm{isect}} + (\frac{A_{R}}{A}) N_{R} C_{\mathrm{isect}} is lowest. Nodes are flat arrays (structure of arrays), so traversal allocates nothing per node.
Constructors
factory Bvh.build(Float32x4List positions, Uint32List indices, {int maxLeaf = 4, int bins = 12})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A unit square of two triangles, with positions, normals and uvs.
final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
final bvh = Bvh.build(pos, idx);
final h = bvh.raycast(Ray(Vec3(0.75, 0.25, 1), Vec3(0, 0, -1)));
print('hit triangle ${h!.triangle} at t = ${f(h.t)}');
}hit triangle 0 at t = 1.0000
Properties
final Float32x4List positionsfinal Uint32List indicesfinal Uint32List orderfinal Float32x4List boundsMinfinal Float32x4List boundsMaxfinal Int32List rightfinal Int32List firstfinal Int32List countint get nodeCountMethods
({int triangle, double t, double u, double v})? raycast(Ray ray)The closest triangle hit by ray: (triangle, t, u, v) or null. Children are visited near-first.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A unit square of two triangles, with positions, normals and uvs.
final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
final bvh = Bvh.build(pos, idx);
for (final (x, y) in [(0.2, 0.7), (0.9, 0.1), (2.0, 2.0)]) {
final h = bvh.raycast(Ray(Vec3(x, y, 1), Vec3(0, 0, -1)));
print('($x, $y): ${h == null ? 'miss' : 'triangle ${h.triangle}, u ${f(h.u)}, v ${f(h.v)}'}');
}
}(0.2, 0.7): triangle 1, u 0.2000, v 0.5000 (0.9, 0.1): triangle 0, u 0.8000, v 0.1000 (2.0, 2.0): miss
Convex
abstract interface class Convex
A convex shape described only by its support function: the farthest point in direction d. GJK and EPA work on any such shape (spheres, boxes, capsules, convex hulls, and their Minkowski sums).
Methods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Every convex shape answers one question: the farthest point in a direction.
final List<Convex> shapes = [SphereShape(Vec3.zero, 1), BoxShape(Vec3.zero, Vec3(2, 1, 1)), CapsuleShape(Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5)];
for (final s in shapes) {
print('${s.runtimeType}: support(1, 1, 0) = ${v3(s.support(Vec3(1, 1, 0)))}');
}
}SphereShape: support(1, 1, 0) = (0.7071, 0.7071, 0.0000) BoxShape: support(1, 1, 0) = (2.0000, 1.0000, 1.0000) CapsuleShape: support(1, 1, 0) = (1.3536, 0.3536, 0.0000)
SphereShape
final class SphereShape implements Convex
Constructors
const SphereShape(this.center, this.radius)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = SphereShape(Vec3(1, 2, 0), 1.5);
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (2.5000, 2.0000, 0.0000)
Properties
final Vec3 centerfinal double radiusMethods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = SphereShape(Vec3.zero, 1);
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (1.0000, 0.0000, 0.0000)
BoxShape
final class BoxShape implements Convex
Constructors
const BoxShape(this.center, this.halfExtents)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = BoxShape(Vec3.zero, Vec3(2, 1, 0.5));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (2.0000, 1.0000, 0.5000)
Properties
final Vec3 centerfinal Vec3 halfExtentsMethods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = BoxShape(Vec3(1, 1, 0), Vec3(1, 0.5, 0.5));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (2.0000, 1.5000, 0.5000)
PointCloudShape
final class PointCloudShape implements Convex
Constructors
PointCloudShape(this.points)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = PointCloudShape(Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(2, 0, 0, 0), Float32x4(1, 2, 0, 0), Float32x4(1, 0.5, 0, 0)]));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (2.0000, 0.0000, 0.0000)
Properties
final Float32x4List pointsMethods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = PointCloudShape(Float32x4List.fromList([for (var k = 0; k < 7; k++) Float32x4(math.cos(k * 0.9) * 2, math.sin(k * 0.9), 0, 0)]));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (2.0000, 0.0000, 0.0000)
CapsuleShape
final class CapsuleShape implements Convex
Constructors
const CapsuleShape(this.a, this.b, this.radius)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = CapsuleShape(Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5);
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (1.5000, 0.0000, 0.0000)
Properties
final Vec3 afinal Vec3 bfinal double radiusMethods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = CapsuleShape(Vec3(0, -1, 0), Vec3(0, 1, 0), 0.3);
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (0.3000, 1.0000, 0.0000)
TransformedConvex
final class TransformedConvex implements Convex
A convex shape in local coordinates moved by a rotation and translation: \operatorname{support}(d) = p + R \cdot s(R^{T}d). Lets one local shape (a hull, a box) be tested at any pose without copying its points.
Constructors
const TransformedConvex(this.shape, this.rotation, this.translation)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = TransformedConvex(BoxShape(Vec3.zero, Vec3(1, 0.3, 0.3)), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(2, 1, 0));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (3.0160, 1.2402, 0.3000)
Properties
final Convex shapefinal Quat rotationfinal Vec3 translationMethods
Vec3 support(Vec3 d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = TransformedConvex(SphereShape(Vec3.zero, 1), Quat.identity, Vec3(3, 0, 0));
final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}support(+x) = (4.0000, 0.0000, 0.0000)
Obb
final class Obb
Oriented box for separating-axis tests: center, 3 unit axes, half extents.
Constructors
const Obb(this.center, this.axes, this.halfExtents)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quat.axisAngle(Vec3.unitZ, 0.5);
final o = Obb(Vec3(1, 1, 0), [q.rotate(Vec3.unitX), q.rotate(Vec3.unitY), Vec3.unitZ], Vec3(2, 1, 0.5));
final ax = o.axes, h = o.halfExtents;
Vec3 corner(double sx, double sy) => o.center + ax[0].scale(sx * h.x) + ax[1].scale(sy * h.y);
print('projected radius on x: ${f(o.radiusOn(Vec3.unitX))}');
}projected radius on x: 2.2346
Properties
final Vec3 centerfinal List<Vec3> axesfinal Vec3 halfExtentsMethods
double radiusOn(Vec3 l)Projection radius of this box onto axis l.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final o = Obb(Vec3.zero, [Vec3.unitX, Vec3.unitY, Vec3.unitZ], Vec3(2, 1, 0.5));
for (final l in [Vec3.unitX, Vec3.unitY, Vec3(1, 1, 0).normalized()]) {
print('radius on ${v3(l)}: ${f(o.radiusOn(l))}');
}
}radius on (1.0000, 0.0000, 0.0000): 2.0000 radius on (0.0000, 1.0000, 0.0000): 1.0000 radius on (0.7071, 0.7071, 0.0000): 2.1213
Color
abstract final class Color
Colour spaces and transfer functions. Renderers light in linear space; displays expect sRGB-encoded values; film pipelines grade in ACEScg; OKLab is perceptually uniform for gradients and palettes (Ottosson 2020).
Static
static double srgbToLinear(double c)sRGB electro-optical transfer: encoded → linear (IEC 61966-2-1).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
for (final c in [0.0, 0.04045, 0.5, 1.0]) {
print('sRGB $c -> linear ${f(Color.srgbToLinear(c))}');
}
}sRGB 0.0 -> linear 0.0000 sRGB 0.04045 -> linear 0.0031 sRGB 0.5 -> linear 0.2140 sRGB 1.0 -> linear 1.0000
static double linearToSrgb(double c)Linear → sRGB encoded.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('linear 0.214 -> sRGB ${f(Color.linearToSrgb(0.214))} (about 0.5)');
print('round trip: ${f(Color.linearToSrgb(Color.srgbToLinear(0.3)))}');
}linear 0.214 -> sRGB 0.5000 (about 0.5) round trip: 0.3000
static double luminance(Vec3 rgb)Relative luminance of linear Rec.709/sRGB (BT.709 weights).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('white ${f(Color.luminance(Vec3(1, 1, 1)))}, red ${f(Color.luminance(Vec3(1, 0, 0)))}, green ${f(Color.luminance(Vec3(0, 1, 0)))}, blue ${f(Color.luminance(Vec3(0, 0, 1)))}');
}white 1.0000, red 0.2126, green 0.7152, blue 0.0722
static Vec3 linearSrgbToAcesCg(Vec3 c)Linear sRGB (D65) → ACEScg (AP1, D60), including the Bradford adaptation (matrix from the ACES reference).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final c = Color.linearSrgbToAcesCg(Vec3(1, 0, 0));
print('sRGB red in ACEScg: ${v3(c)}');
print('white stays white: ${v3(Color.linearSrgbToAcesCg(Vec3(1, 1, 1)))}');
}sRGB red in ACEScg: (0.6131, 0.0701, 0.0206) white stays white: (1.0001, 1.0000, 0.9999)
static Vec3 acesCgToLinearSrgb(Vec3 c)ACEScg → linear sRGB (inverse of the above).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final c = Color.acesCgToLinearSrgb(Color.linearSrgbToAcesCg(Vec3(0.2, 0.5, 0.8)));
print('round trip ${v3(c)}');
}round trip (0.2000, 0.5000, 0.8000)
static Vec3 linearSrgbToOklab(Vec3 c)Linear sRGB → OKLab (L, a, b).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
for (final (n, c) in [('white', Vec3(1, 1, 1)), ('red', Vec3(1, 0, 0)), ('green', Vec3(0, 1, 0)), ('blue', Vec3(0, 0, 1))]) {
print('${n.padRight(5)} -> Lab ${v3(Color.linearSrgbToOklab(c))}');
}
final hues = [for (var k = 0; k < 12; k++) Color.linearSrgbToOklab(Vec3(0.5 + 0.5 * math.cos(k * math.pi / 6), 0.5 + 0.5 * math.cos(k * math.pi / 6 - 2.094), 0.5 + 0.5 * math.cos(k * math.pi / 6 + 2.094)))];
}white -> Lab (1.0000, 0.0000, 0.0000) red -> Lab (0.6280, 0.2249, 0.1258) green -> Lab (0.8664, -0.2339, 0.1795) blue -> Lab (0.4520, -0.0325, -0.3115)
static Vec3 oklabToLinearSrgb(Vec3 lab)OKLab → linear sRGB.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Mix two colours in Oklab for a perceptually even gradient.
final a = Color.linearSrgbToOklab(Vec3(1, 0, 0)), b = Color.linearSrgbToOklab(Vec3(0, 0, 1));
final mid = Color.oklabToLinearSrgb(a.lerp(b, 0.5));
print('red–blue midpoint in Oklab: ${v3(mid)}');
}red–blue midpoint in Oklab: (0.2637, 0.0866, 0.3628)
static double acesFilm(double x)ACES filmic tone-mapping curve fit (Narkowicz 2015), applied per channel to linear HDR, result in [0, 1].
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print([for (final x in [0.0, 0.18, 1.0, 4.0, 16.0]) f(Color.acesFilm(x))]);
}[0.0000, 0.2669, 0.8038, 0.9734, 1.0000]
Half
abstract final class Half
IEEE 754 binary16 ("half") packing for GPU buffers and HDR textures: 1 sign, 5 exponent, 10 mantissa bits.
Static
static int pack(double value)Float → half bits, round-to-nearest-even, with overflow to infinity, NaN kept, and subnormals.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
for (final v in [1.0, 0.1, 65504.0, 1e-8]) {
final h = Half.pack(v);
print('$v -> 0x${h.toRadixString(16).padLeft(4, '0')} -> ${Half.unpack(h)}');
}
}1.0 -> 0x3c00 -> 1.0 0.1 -> 0x2e66 -> 0.0999755859375 65504.0 -> 0x7bff -> 65504.0 1e-8 -> 0x0000 -> 0.0
static double unpack(int h)Half bits → float.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print([for (final h in [0x3c00, 0xc000, 0x7bff, 0x0400]) Half.unpack(h)]);
}[1.0, -2.0, 65504.0, 0.00006103515625]
ArcLengthTable
final class ArcLengthTable
Arc-length table for a parametric curve: sample samples segments, accumulate chord lengths, then map an arc length s back to t by binary search + linear interpolation (constant-speed motion along curves).
Constructors
ArcLengthTable(Vec3 Function(double t) curve, {int samples = 256})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Move at constant speed along y = x²: tAt(s) maps distance to the parameter.
Vec3 curve(double t) => Vec3(2 * t, 4 * t * t, 0);
final tab = ArcLengthTable(curve);
final even = [for (var k = 0; k <= 10; k++) curve(tab.tAt(k / 10))];
final naive = [for (var k = 0; k <= 10; k++) curve(k / 10)];
print('parameter at half the length: ${f(tab.tAt(0.5))}');
}parameter at half the length: 0.2232
Properties
final Float64List lengthsdouble get totalMethods
double tAt(double s)The parameter t at arc length s (clamped to [0, total]).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final tab = ArcLengthTable((t) => Vec3(math.cos(math.pi * t), math.sin(math.pi * t), 0));
print([for (final s in [0.0, 0.25, 0.5, 0.75, 1.0]) f(tab.tAt(s))]);
}[0.0000, 0.0796, 0.1592, 0.2387, 0.3183]
RoundingMode
enum RoundingMode
How to round when a result needs fewer digits than it has.
Values
halfEvenToward the nearest; ties to the even neighbour (banker's rounding — no statistical bias over many roundings).
Decimal
extension type const Decimal._((BigInt, int) _r) implements Object
An exact decimal. Immutable; equality is numeric (1.50 = 1.5).
Constructors
factory Decimal.raw(BigInt unscaled, int scale)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final d = Decimal.raw(BigInt.from(123456), 3);
print('${d.toPlainString()} (unscaled 123456, scale 3)');
}123.456 (unscaled 123456, scale 3)
factory Decimal.fromInt(int v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print((Decimal.fromInt(7) * Decimal.parse('0.1')).toPlainString());
}0.7
factory Decimal.fromMinorUnits(int minor, int digits)Integer minor units with digits minor digits: fromMinorUnits(1999, 2) == 19.99.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final d = Decimal.fromMinorUnits(1999, 2); // cents
print('${d.toPlainString()} dollars');
}19.99 dollars
factory Decimal.parse(String s)Parses "-12.3400", "1e-3", "+7". Throws FormatException for anything else.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Decimal.parse('0.1'), b = Decimal.parse('0.2');
print('0.1 + 0.2 = ${(a + b).toPlainString()} (doubles give ${0.1 + 0.2})');
}0.1 + 0.2 = 0.3 (doubles give 0.30000000000000004)
Static
static final zerostatic final oneProperties
BigInt get unscaledint get scaleint get signMethods
Decimal divide(Decimal o, {required int scale, RoundingMode mode = RoundingMode.halfEven})Quotient rounded to scale digits after the point with mode.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final total = Decimal.parse('100.00');
final share = total.divide(Decimal.fromInt(3), scale: 2);
print('100 / 3 to cents: ${share.toPlainString()}');
print('halfUp at 4 places: ${Decimal.fromInt(2).divide(Decimal.fromInt(3), scale: 4, mode: RoundingMode.halfUp).toPlainString()}');
}100 / 3 to cents: 33.33 halfUp at 4 places: 0.6667
Decimal setScale(int newScale, {RoundingMode mode = RoundingMode.halfEven})This rounded (or padded) to newScale digits after the point.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final d = Decimal.parse('2.345');
for (final m in RoundingMode.values.where((m) => m.name != "unnecessary")) {
print('${m.name.padRight(9)} ${d.setScale(2, mode: m).toPlainString()}');
}
}halfEven 2.34 halfUp 2.35 halfDown 2.34 down 2.34 up 2.35 floor 2.34 ceiling 2.35
int toMinorUnits(int digits, {RoundingMode mode = RoundingMode.halfEven})Integer minor units for a currency with digits minor digits, rounding with mode; throws if the result doesn't fit a 64-bit int.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
final t = (price * qty * (Decimal.fromInt(1) + tax));
print('total ${t.toPlainString()} -> ${t.toMinorUnits(2)} cents');
}total 64.917525 -> 6492 cents
int compareTo(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print(price.compareTo(Decimal.parse('19.990')));
print(price.compareTo(Decimal.parse('20')));
}0 -1
bool equals(Decimal o)Numeric equality (scale-insensitive). Use this rather than ==, which on extension types compares the representation (so 1.50 and 1.5 would differ).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('19.99 equals 19.990: ${price.equals(Decimal.parse('19.990'))}');
}19.99 equals 19.990: true
Decimal abs()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Decimal.parse('-3.50').abs().toPlainString());
}3.50
Decimal normalized()Removes trailing zeros (1.500 \to 1.5).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Decimal.parse('12.5000').normalized().toPlainString());
}12.5
String toPlainString()Plain notation with exactly scale digits after the point ("-0.050").
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Decimal.raw(BigInt.from(-5), 4).toPlainString());
}-0.0005
double toDouble()Nearest double (for display or charts only — never for money arithmetic).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print(price.toDouble());
}19.99
Operators
Decimal operator +(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price + tax = ${(price + tax).toPlainString()}');
}price + tax = 20.0725
Decimal operator -(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price - tax = ${(price - tax).toPlainString()}');
}price - tax = 19.9075
Decimal operator -()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print((-price).toPlainString());
}-19.99
Decimal operator *(Decimal o)Exact product (scales add).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price * tax = ${(price * tax).toPlainString()}');
}price * tax = 1.649175
bool operator <(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price < 20: ${price < Decimal.fromInt(20)}');
}price < 20: true
bool operator <=(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price <= 20: ${price <= Decimal.fromInt(20)}');
}price <= 20: true
bool operator >(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price > 20: ${price > Decimal.fromInt(20)}');
}price > 20: false
bool operator >=(Decimal o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
print('price >= 20: ${price >= Decimal.fromInt(20)}');
}price >= 20: false
IntegerOverflow
final class IntegerOverflow implements Exception
Thrown by checked arithmetic when the exact result doesn't fit the type.
Constructors
IntegerOverflow(this.op)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
try {
U8(200) + U8(100);
U8(200).checkedAdd(U8(100));
} on IntegerOverflow catch (e) {
print('caught: ${e.op}');
}
}caught: 200 + 100
Properties
final String opMethods
String toString()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(IntegerOverflow('U16 4000 * 20'));
}IntegerOverflow: U16 4000 * 20
U8
extension type const U8._(int v) implements Object
Constructors
factory U8(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('U8(200) = ${U8(200).value}');
try {
U8(256);
} catch (e) {
print('U8(256) is out of range: ${e.runtimeType}');
}
print('max = ${U8.max.value}');
}U8(200) = 200 U8(256) is out of range: IntegerOverflow max = 255
factory U8.wrap(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('wrap(261) = ${U8.wrap(261).value}');
print('wrap(−1) = ${U8.wrap(-1).value}');
}wrap(261) = 5 wrap(−1) = 255
Static
static const max = U8._(255)static U8 parse(String s, {int radix = 10})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U8.parse('200').value);
print(U8.parse('ff', radix: 16).value);
}200 255
Properties
int get valueMethods
U8 checkedAdd(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U8(200), b = U8(100);
print('small: ${attempt(() => U8(3).checkedAdd(U8(2)).value)}');
print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}small: 5 a + b: IntegerOverflow: 200 + 100
U8 checkedSub(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U8(200), b = U8(100);
print('small: ${attempt(() => U8(3).checkedSub(U8(2)).value)}');
print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}small: 1 b - a: IntegerOverflow: 100 - 200
U8 checkedMul(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U8(200), b = U8(100);
print('small: ${attempt(() => U8(3).checkedMul(U8(2)).value)}');
print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}small: 6 a * b: IntegerOverflow: 200 * 100
U8 saturatingAdd(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
print('small: ${U8(3).saturatingAdd(U8(4)).value}');
}a + b clamps to 255 (max) small: 7
U8 saturatingSub(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('b − a clamps to ${b.saturatingSub(a).value}');
print('a − b = ${a.saturatingSub(b).value}');
}b − a clamps to 0 a − b = 100
Operators
U8 operator +(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('a + b wraps to ${(a + b).value}');
}a + b wraps to 44
U8 operator -(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('b − a wraps to ${(b - a).value}');
}b − a wraps to 156
U8 operator *(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('a × b wraps to ${(a * b).value}');
}a × b wraps to 32
U8 operator &(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('x & y = 0x${(x & y).value.toRadixString(16)}');
}x & y = 0x30
U8 operator |(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('x | y = 0x${(x | y).value.toRadixString(16)}');
}x | y = 0xfc
U8 operator ^(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}x ^ y = 0xcc
U8 operator ~()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('~x = 0x${(~x).value.toRadixString(16)}');
}~x = 0xf
U8 operator <<(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}x << 4 = 0x0 (high bits fall off)
U8 operator >>(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U8(0xf0), y = U8(0x3c);
print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}x >> 4 = 0xf
bool operator <(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('a < b: ${a < b}, b < a: ${b < a}');
}a < b: false, b < a: true
bool operator >(U8 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U8(200), b = U8(100);
print('a > b: ${a > b}');
}a > b: true
U16
extension type const U16._(int v) implements Object
Constructors
factory U16(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('U16(60000) = ${U16(60000).value}');
try {
U16(65536);
} catch (e) {
print('U16(65536) is out of range: ${e.runtimeType}');
}
print('max = ${U16.max.value}');
}U16(60000) = 60000 U16(65536) is out of range: IntegerOverflow max = 65535
factory U16.wrap(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('wrap(65541) = ${U16.wrap(65541).value}');
print('wrap(−1) = ${U16.wrap(-1).value}');
}wrap(65541) = 5 wrap(−1) = 65535
Static
static const max = U16._(0xffff)static U16 parse(String s, {int radix = 10})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U16.parse('60000').value);
print(U16.parse('ff', radix: 16).value);
}60000 255
Properties
int get valueMethods
U16 checkedAdd(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U16(60000), b = U16(7000);
print('small: ${attempt(() => U16(3).checkedAdd(U16(2)).value)}');
print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}small: 5 a + b: IntegerOverflow: 60000 + 7000
U16 checkedSub(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U16(60000), b = U16(7000);
print('small: ${attempt(() => U16(3).checkedSub(U16(2)).value)}');
print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}small: 1 b - a: IntegerOverflow: 7000 - 60000
U16 checkedMul(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U16(60000), b = U16(7000);
print('small: ${attempt(() => U16(3).checkedMul(U16(2)).value)}');
print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}small: 6 a * b: IntegerOverflow: 60000 * 7000
U16 saturatingAdd(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
print('small: ${U16(3).saturatingAdd(U16(4)).value}');
}a + b clamps to 65535 (max) small: 7
U16 saturatingSub(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('b − a clamps to ${b.saturatingSub(a).value}');
print('a − b = ${a.saturatingSub(b).value}');
}b − a clamps to 0 a − b = 53000
Operators
U16 operator +(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('a + b wraps to ${(a + b).value}');
}a + b wraps to 1464
U16 operator -(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('b − a wraps to ${(b - a).value}');
}b − a wraps to 12536
U16 operator *(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('a × b wraps to ${(a * b).value}');
}a × b wraps to 45312
U16 operator &(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('x & y = 0x${(x & y).value.toRadixString(16)}');
}x & y = 0xf00
U16 operator |(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('x | y = 0x${(x | y).value.toRadixString(16)}');
}x | y = 0xfff0
U16 operator ^(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}x ^ y = 0xf0f0
U16 operator ~()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('~x = 0x${(~x).value.toRadixString(16)}');
}~x = 0xff
U16 operator <<(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}x << 4 = 0xf000 (high bits fall off)
U16 operator >>(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U16(0xff00), y = U16(0xff0);
print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}x >> 4 = 0xff0
bool operator <(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('a < b: ${a < b}, b < a: ${b < a}');
}a < b: false, b < a: true
bool operator >(U16 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U16(60000), b = U16(7000);
print('a > b: ${a > b}');
}a > b: true
U32
extension type const U32._(int v) implements Object
Constructors
factory U32(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('U32(4000000000) = ${U32(4000000000).value}');
try {
U32(4294967296);
} catch (e) {
print('U32(4294967296) is out of range: ${e.runtimeType}');
}
print('max = ${U32.max.value}');
}U32(4000000000) = 4000000000 U32(4294967296) is out of range: IntegerOverflow max = 4294967295
factory U32.wrap(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print('wrap(4294967301) = ${U32.wrap(4294967301).value}');
print('wrap(−1) = ${U32.wrap(-1).value}');
}wrap(4294967301) = 5 wrap(−1) = 4294967295
Static
static const max = U32._(0xffffffff)static U32 parse(String s, {int radix = 10})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U32.parse('4000000000').value);
print(U32.parse('ff', radix: 16).value);
}4000000000 255
Properties
int get valueMethods
U32 checkedAdd(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U32(4000000000), b = U32(500000000);
print('small: ${attempt(() => U32(3).checkedAdd(U32(2)).value)}');
print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}small: 5 a + b: IntegerOverflow: 4000000000 + 500000000
U32 checkedSub(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U32(4000000000), b = U32(500000000);
print('small: ${attempt(() => U32(3).checkedSub(U32(2)).value)}');
print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}small: 1 b - a: IntegerOverflow: 500000000 - 4000000000
U32 checkedMul(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U32(4000000000), b = U32(500000000);
print('small: ${attempt(() => U32(3).checkedMul(U32(2)).value)}');
print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}small: 6 a * b: IntegerOverflow: 4000000000 * 500000000
U32 saturatingAdd(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
print('small: ${U32(3).saturatingAdd(U32(4)).value}');
}a + b clamps to 4294967295 (max) small: 7
U32 saturatingSub(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('b − a clamps to ${b.saturatingSub(a).value}');
print('a − b = ${a.saturatingSub(b).value}');
}b − a clamps to 0 a − b = 3500000000
U32 rotateLeft(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0x80000001);
print('rotl 1 = 0x${x.rotateLeft(1).value.toRadixString(16)}');
print('rotl 8 = 0x${x.rotateLeft(8).value.toRadixString(16)}');
}rotl 1 = 0x3 rotl 8 = 0x180
Operators
U32 operator +(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('a + b wraps to ${(a + b).value}');
}a + b wraps to 205032704
U32 operator -(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('b − a wraps to ${(b - a).value}');
}b − a wraps to 794967296
U32 operator *(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('a × b wraps to ${(a * b).value}');
}a × b wraps to 1321730048
U32 operator &(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('x & y = 0x${(x & y).value.toRadixString(16)}');
}x & y = 0xff0000
U32 operator |(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('x | y = 0x${(x | y).value.toRadixString(16)}');
}x | y = 0xffffff00
U32 operator ^(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}x ^ y = 0xff00ff00
U32 operator ~()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('~x = 0x${(~x).value.toRadixString(16)}');
}~x = 0xffff
U32 operator <<(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}x << 4 = 0xfff00000 (high bits fall off)
U32 operator >>(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U32(0xffff0000), y = U32(0xffff00);
print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}x >> 4 = 0xffff000
bool operator <(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('a < b: ${a < b}, b < a: ${b < a}');
}a < b: false, b < a: true
bool operator >(U32 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U32(4000000000), b = U32(500000000);
print('a > b: ${a > b}');
}a > b: true
U64
extension type const U64._(int bits) implements Object
Unsigned 64-bit: the same 64 bits as int, compared and printed as unsigned.
Constructors
const U64.fromBits(this.bits)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final m = U64.fromBits(-1);
print('the bits of −1 as unsigned: ${(m).toRadixString(10)}');
}the bits of −1 as unsigned: 18446744073709551615
factory U64.fromBigInt(BigInt b)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.fromBigInt(BigInt.two.pow(63) + BigInt.from(5));
print('2⁶³ + 5 = ${(x).toRadixString(10)}');
}2⁶³ + 5 = 9223372036854775813
Static
static const max = U64._(-1)static const zero = U64._(0)static U64 parse(String s, {int radix = 10})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U64.parse('18446744073709551615').toRadixString(16));
print(U64.parse('ffff', radix: 16).toBigInt());
}ffffffffffffffff 65535
Methods
int compareTo(U64 o)Unsigned compare: flipping the sign bit maps unsigned order onto signed order.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a vs b: ${a.compareTo(b)}, b vs a: ${b.compareTo(a)}');
}a vs b: 1, b vs a: -1
U64 checkedAdd(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('small: ${attempt(() => U64.fromBits(7).checkedAdd(U64.fromBits(3)).toRadixString(10))}');
print('a Add b: ${attempt(() => a.checkedAdd(b).toRadixString(10))}');
}small: 10 a Add b: IntegerOverflow: U64 add
U64 checkedSub(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('small: ${attempt(() => U64.fromBits(7).checkedSub(U64.fromBits(3)).toRadixString(10))}');
print('b Sub a: ${attempt(() => b.checkedSub(a).toRadixString(10))}');
}small: 4 b Sub a: IntegerOverflow: U64 sub
U64 checkedMul(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('small: ${attempt(() => U64.fromBits(7).checkedMul(U64.fromBits(3)).toRadixString(10))}');
print('a Mul b: ${attempt(() => a.checkedMul(b).toRadixString(10))}');
}small: 21 a Mul b: IntegerOverflow: U64 mul
U64 saturatingAdd(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a + b saturates to ${(a.saturatingAdd(b)).toRadixString(10)}');
}a + b saturates to 18446744073709551615
U64 saturatingSub(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('b − a saturates to ${(b.saturatingSub(a)).toRadixString(10)}');
}b − a saturates to 0
BigInt toBigInt()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a = ${a.toBigInt()}, as a signed int it is ${a == U64.fromBits(-616) ? -616 : '?'}');
}a = 18446744073709551000, as a signed int it is -616
String toRadixString(int radix)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('hex ${a.toRadixString(16)}');
print('binary of b ${b.toRadixString(2)}');
}hex fffffffffffffd98 binary of b 1111101000
Operators
bool operator <(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a < b: ${a < b} (unsigned: a is huge)');
print('as signed ints: ${U64.fromBits(-616) == a ? -616 < 1000 : '?'}');
}a < b: false (unsigned: a is huge) as signed ints: true
bool operator >(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a > b: ${a > b} (unsigned: a is huge)');
print('as signed ints: ${U64.fromBits(-616) == a ? -616 > 1000 : '?'}');
}a > b: true (unsigned: a is huge) as signed ints: false
bool operator <=(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a <= b: ${a <= b} (unsigned: a is huge)');
print('as signed ints: ${U64.fromBits(-616) == a ? -616 <= 1000 : '?'}');
}a <= b: false (unsigned: a is huge) as signed ints: true
bool operator >=(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a >= b: ${a >= b} (unsigned: a is huge)');
print('as signed ints: ${U64.fromBits(-616) == a ? -616 >= 1000 : '?'}');
}a >= b: true (unsigned: a is huge) as signed ints: false
U64 operator +(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a + b wraps to ${(a + b).toRadixString(10)}');
}a + b wraps to 384
U64 operator -(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('b − a wraps to ${(b - a).toRadixString(10)}');
}b − a wraps to 1616
U64 operator *(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('4294967296');
print('2³² × 2³² wraps to ${(x * x).toRadixString(10)}');
print('3 × 5 = ${(U64.fromBits(3) * U64.fromBits(5)).toRadixString(10)}');
}2³² × 2³² wraps to 0 3 × 5 = 15
U64 operator &(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print((x & y).toRadixString(16));
}f000f000f000f00
U64 operator |(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print((x | y).toRadixString(16));
}fff0fff0fff0fff0
U64 operator ^(U64 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print((x ^ y).toRadixString(16));
}f0f0f0f0f0f0f0f0
U64 operator ~()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print((~x).toRadixString(16));
}ff00ff00ff00ff
U64 operator <<(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print((x << 8).toRadixString(16));
}ff00ff00ff0000
U64 operator >>(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
print('logical shift: ${(x >> 60).toRadixString(16)} (no sign extension)');
}logical shift: f (no sign extension)
U64 operator ~/(U64 o)Unsigned division and remainder (via BigInt for the top-bit-set cases).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
print('a ~/ b = ${(a ~/ b).toRadixString(10)}');
print('check: ${a.toBigInt() ~/ b.toBigInt()}');
}a ~/ b = 18446744073709551 check: 18446744073709551
U128
extension type const U128._((int, int) _r) implements Object
Unsigned 128-bit integer as (hi, lo) 64-bit halves, each read as unsigned.
Constructors
const U128(int hi, int lo)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
const x = U128(1, 0); // 2^64
print('U128(1, 0) = ${x.toBigInt()}');
}U128(1, 0) = 18446744073709551616
factory U128.fromU64(U64 v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U128.fromU64(U64.fromBits(-1)).toBigInt());
}18446744073709551615
factory U128.fromBigInt(BigInt b)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U128.fromBigInt(BigInt.parse('123456789012345678901234567890')).toRadixString(16));
}18ee90ff6c373e0ee4e3f0ad2
Static
static const zero = U128(0, 0)static const max = U128(-1, -1)static U128 parse(String s, {int radix = 10})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U128.parse('ffffffffffffffffffffffffffffffff', radix: 16).toBigInt());
}340282366920938463463374607431768211455
Properties
int get hiint get loMethods
int compareTo(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(a.compareTo(b));
}1
bool equals(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(U128.parse('42').equals(const U128(0, 42)));
}true
U128 wrappingMul(U128 o)Product modulo 2^{128}, by 32-bit limbs (schoolbook).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print('a × b wraps to ${a.wrappingMul(b).toBigInt()}');
}a × b wraps to 340282366920938463463374607431767755456
U128 checkedAdd(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(attempt(() => a.checkedAdd(b).toBigInt()));
print(attempt(() => b.checkedAdd(b).toBigInt()));
}IntegerOverflow: U128 add 2000
U128 checkedMul(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(attempt(() => a.checkedMul(b).toBigInt()));
}IntegerOverflow: U128 mul
U128 saturatingAdd(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(a.saturatingAdd(b) == U128.max);
}true
BigInt toBigInt()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(a.toBigInt());
}340282366920938463463374607431768211000
String toRadixString(int radix)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print(a.toRadixString(16));
}fffffffffffffffffffffffffffffe38
Operators
bool operator <(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print('b < a: ${b < a}');
}b < a: true
bool operator >(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print('a > b: ${a > b}');
}a > b: true
U128 operator +(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print('a + b wraps to ${(a + b).toBigInt()}');
}a + b wraps to 544
U128 operator -(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
print('b − a wraps to ${(b - a).toBigInt()}');
}b − a wraps to 1456
U128 operator *(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U128.parse('18446744073709551616'); // 2^64
print('2⁶⁴ × 3 = ${(x * U128.parse('3')).toBigInt()}');
print('2⁶⁴ × 2⁶⁴ wraps to ${(x * x).toBigInt()}');
}2⁶⁴ × 3 = 55340232221128654848 2⁶⁴ × 2⁶⁴ wraps to 0
U128 operator &(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
print((x & y).toRadixString(16));
}f0f0f0f0f0f0f0f0000000000000000
U128 operator |(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
print((x | y).toRadixString(16));
}ffffffffffffffff0f0f0f0f0f0f0f0f
U128 operator ^(U128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
print((x ^ y).toRadixString(16));
}f0f0f0f0f0f0f0f00f0f0f0f0f0f0f0f
U128 operator ~()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print((~U128.zero).toRadixString(16));
}ffffffffffffffffffffffffffffffff
U128 operator <<(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print((const U128(0, 1) << 100).toBigInt() == BigInt.two.pow(100));
}true
U128 operator >>(int s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print((U128.max >> 120).toBigInt());
}255
I128
extension type const I128._(U128 u) implements Object
Signed 128-bit integer (two's complement over U128's bits).
Constructors
factory I128.fromInt(int v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(I128.fromInt(-5).toBigInt());
}-5
factory I128.fromBigInt(BigInt b)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(I128.fromBigInt(-BigInt.two.pow(100)).toBigInt());
}-1267650600228229401496703205376
Static
static final maxstatic final minstatic I128 parse(String s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
print(I128.parse('-12345678901234567890123').toBigInt());
}-12345678901234567890123
Properties
bool get isNegativeMethods
int compareTo(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print('a vs b: ${a.compareTo(b)}');
}a vs b: -1
I128 checkedAdd(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(attempt(() => (a - b).checkedAdd(I128.fromInt(-1000)).toBigInt()));
print(attempt(() => a.checkedAdd(b).toBigInt()));
}170141183460469231731687303715884104456 -170141183460469231731687303715884104000
I128 checkedSub(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(attempt(() => a.checkedSub(b).toBigInt()));
}IntegerOverflow: I128 sub
I128 checkedMul(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
String attempt(Object Function() f) {
try {
return '${f()}';
} on IntegerOverflow catch (e) {
return '$e';
}
}
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(attempt(() => a.checkedMul(b).toBigInt()));
print(attempt(() => b.checkedMul(b).toBigInt()));
}IntegerOverflow: I128 mul 1000000
I128 saturatingAdd(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print('a − 10⁶ saturates to min: ${(a.saturatingAdd(I128.fromInt(-1000000)) == I128.min)}');
}a − 10⁶ saturates to min: true
BigInt toBigInt()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(a.toBigInt());
}-170141183460469231731687303715884105000
Operators
I128 operator +(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print((a + b).toBigInt());
}-170141183460469231731687303715884104000
I128 operator -(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print('a − b wraps: ${(a - b).toBigInt()}');
}a − b wraps: 170141183460469231731687303715884105456
I128 operator *(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print((b * b * b).toBigInt());
}1000000000
I128 operator -()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print((-b).toBigInt());
print('−min wraps to min: ${-I128.min == I128.min}');
}-1000 −min wraps to min: true
bool operator <(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(a < b);
}true
bool operator >(I128 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
void main() {
final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
print(a > b);
}false
Ik
abstract final class Ik
Inverse kinematics on joint position chains (root first). Bone lengths are preserved.
Static
static void fabrik(List<Vec3> joints, Vec3 target, {int iterations = 16, double tolerance = 1e-4})FABRIK (Aristidou and Lasenby 2011): alternate backward (end effector → root) and forward (root → end) passes, re-placing each joint along the line to its neighbour at the bone length. Converges in a few iterations.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final joints = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(2, 0, 0), Vec3(3, 0, 0)];
final start = [for (final j in joints) [j.x, j.y]];
final target = Vec3(1.5, 2, 0);
Ik.fabrik(joints, target);
print('end effector ${v3(joints.last)}, target ${v3(target)}');
print('bone lengths kept: ${[for (var i = 0; i < 3; i++) f((joints[i + 1] - joints[i]).length)]}');
}end effector (1.5000, 1.9999, 0.0000), target (1.5000, 2.0000, 0.0000) bone lengths kept: [1.0000, 1.0000, 1.0000]
static void ccd(List<Vec3> joints, Vec3 target, {int iterations = 32, double tolerance = 1e-4})Cyclic coordinate descent: from the last joint back to the root, rotate each joint so the end effector points at the target. Simple; tends to curl the chain toward the end.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final joints = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(2, 0, 0), Vec3(3, 0, 0)];
final start = [for (final j in joints) [j.x, j.y]];
final target = Vec3(1.5, 2, 0);
Ik.ccd(joints, target);
print('end effector ${v3(joints.last)}');
}end effector (1.4999, 1.9999, 0.0000)
static Vec3 twoBone(Vec3 root, double a, double b, Vec3 target, Vec3 pole)Analytic two-bone IK (arm or leg): with upper length a, lower length b and target distance c, the law of cosines gives the root and middle angles exactly. pole picks the bending plane. Returns the middle joint.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final root = Vec3.zero, target = Vec3(1.2, 0.8, 0), pole = Vec3(0, 2, 0);
final elbow = Ik.twoBone(root, 1, 1, target, pole);
print('elbow ${v3(elbow)}; bones ${f((elbow - root).length)}, ${f((target - elbow).length)}');
}elbow (0.2157, 0.9765, 0.0000); bones 1.0000, 1.0000
Ray
final class Ray
A ray o + t \cdot d (d need not be unit; t is in units of |d|).
Constructors
const Ray(this.origin, this.direction)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final r = Ray(Vec3(1, 1, 0), Vec3(1, 0.5, 0));
print('origin ${v3(r.origin)}, direction ${v3(r.direction)}');
}origin (1.0000, 1.0000, 0.0000), direction (1.0000, 0.5000, 0.0000)
Properties
final Vec3 originfinal Vec3 directionMethods
Vec3 at(double t)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final r = Ray(Vec3(0, 0, 0), Vec3(1, 2, 0));
final pts = [for (final t in [0.0, 0.5, 1.0, 1.5]) r.at(t)];
print([for (final p in pts) v3(p)]);
}[(0.0000, 0.0000, 0.0000), (0.5000, 1.0000, 0.0000), (1.0000, 2.0000, 0.0000), (1.5000, 3.0000, 0.0000)]
Mat3d
extension type Mat3d(Float64List m)
Small dense linear algebra in double precision (Float64List, row-major), for decompositions where float32 isn't enough: physics (inertia, shape matching), animation (polar decomposition), geometry (PCA, OBB fitting). A 3 \times 3 double matrix, row-major: m[r \cdot 3 + c].
Constructors
factory Mat3d.zero()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final z = Mat3d.zero();
print([for (var i = 0; i < 9; i++) z.at(i ~/ 3, i % 3)]);
}[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
factory Mat3d.identity()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final i = Mat3d.identity();
final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
print('I·A == A: ${[for (var k = 0; k < 9; k++) (i * a).at(k ~/ 3, k % 3) == a.at(k ~/ 3, k % 3)].every((x) => x)}');
}I·A == A: true
factory Mat3d.of(List<double> rowMajor)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
print('row 1, column 2: ${a.at(1, 2)}');
}row 1, column 2: 6.0
Properties
Mat3d get transposeddouble get determinantMethods
double at(int r, int c)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
print('trace ${a.at(0, 0) + a.at(1, 1) + a.at(2, 2)}');
}trace 15.0
void set(int r, int c, double v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.identity();
a.set(0, 2, 5);
print([for (var r = 0; r < 3; r++) [for (var c = 0; c < 3; c++) a.at(r, c)]]);
}[[1.0, 0.0, 5.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
Operators
Mat3d operator *(Mat3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat3d.of([0, -1, 0, 1, 0, 0, 0, 0, 1]); // a quarter turn about z
final a4 = a * a * a * a;
print('R⁴ = I: ${[for (var k = 0; k < 9; k++) a4.at(k ~/ 3, k % 3)]}');
}R⁴ = I: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
Mat4
extension type Mat4._(Float32x4List c)
A 4 \times 4 column-major matrix stored in 4 SIMD columns. A view over caller-owned storage: build it once (or slice a Mat4Buffer) and reuse it, writing results into an out matrix, so hot loops never allocate.
Constructors
factory Mat4.identity()A new identity matrix (allocates its storage once).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity();
print([for (var r = 0; r < 4; r++) [for (var c = 0; c < 4; c++) m.entry(r, c)]]);
}[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
factory Mat4.view(Float32x4List buffer, [int column = 0])A matrix over 4 columns of buffer starting at column column.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Two matrices sharing one buffer of 8 columns.
final buf = Float32x4List(8);
final a = Mat4.view(buf), b = Mat4.view(buf, 4);
a.setIdentity();
b.setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
print('b translation column: ${b.col(3)}');
print('buffer column 7 is the same: ${buf[7]}');
}b translation column: [1.000000, 2.000000, 3.000000, 1.000000] buffer column 7 is the same: [1.000000, 2.000000, 3.000000, 1.000000]
Methods
Float32x4 col(int i)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(5, 6, 7), Quat.identity, Vec3(2, 2, 2));
for (var i = 0; i < 4; i++) {
print('column $i: ${m.col(i)}');
}
}column 0: [2.000000, 0.000000, 0.000000, 0.000000] column 1: [0.000000, 2.000000, 0.000000, 0.000000] column 2: [0.000000, 0.000000, 2.000000, 0.000000] column 3: [5.000000, 6.000000, 7.000000, 1.000000]
void setIdentity()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(1, 1, 1), Quat.identity, Vec3(3, 3, 3));
m.setIdentity();
print('reset: ${m.entry(0, 0)}, ${m.entry(0, 3)}');
}reset: 1.0, 0.0
void setFrom(Mat4 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Mat4.identity()..setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
final b = Mat4.identity()..setFrom(a);
print('copied translation: ${b.entry(0, 3)}, ${b.entry(1, 3)}, ${b.entry(2, 3)}');
}copied translation: 1.0, 2.0, 3.0
void setTrs(Vec3 t, Quat r, Vec3 s)Translation · rotation · scale (TRS), the usual model matrix.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
final moved = [for (final p in sq) m.transformPoint(p).xyz];
print('T·R·S applied to (1, 1, 0): ${v3(moved[2])}');
}T·R·S applied to (1, 1, 0): (2.7990, 2.6160, 0.0000)
void setPerspective(double fovY, double aspect, double near, double far)A right-handed perspective projection to clip space with z in [0, 1] (Metal/Vulkan/WebGPU convention).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = Mat4.identity()..setPerspective(math.pi / 3, 16 / 9, 0.1, 100);
for (final z in [-0.1, -1.0, -100.0]) {
print('depth at z = $z -> ${f(p.transformPoint(Vec3(0, 0, z)).xyzOverW.z)}');
}
}depth at z = -0.1 -> 0.0000 depth at z = -1.0 -> 0.9009 depth at z = -100.0 -> 1.0000
void setLookAt(Vec3 eye, Vec3 target, Vec3 up)A right-handed view matrix looking from eye at target.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final v = Mat4.identity()..setLookAt(Vec3(0, 0, 5), Vec3.zero, Vec3.unitY);
print('the target in view space: ${v3(v.transformPoint(Vec3.zero).xyz)}');
print('the eye: ${v3(v.transformPoint(Vec3(0, 0, 5)).xyz)}');
}the target in view space: (0.0000, 0.0000, -5.0000) the eye: (0.0000, 0.0000, 0.0000)
void multiplyInto(Mat4 b, Mat4 out)Out = this · b. out may be this or b.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final t = Mat4.identity()..setTrs(Vec3(3, 0, 0), Quat.identity, Vec3(1, 1, 1));
final r = Mat4.identity()..setTrs(Vec3.zero, Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(1, 1, 1));
final tr = Mat4.identity(), rt = Mat4.identity();
t.multiplyInto(r, tr);
r.multiplyInto(t, rt);
print('T·R moves (1, 0, 0) to ${v3(tr.transformPoint(Vec3.unitX).xyz)}');
print('R·T moves it to ${v3(rt.transformPoint(Vec3.unitX).xyz)}');
}T·R moves (1, 0, 0) to (3.0000, 1.0000, 0.0000) R·T moves it to (0.0000, 4.0000, 0.0000)
Vec4 transformPoint(Vec3 p)This · (p, 1): a point (translation applies).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
final moved = [for (final p in sq) m.transformPoint(p).xyz];
final h = m.transformPoint(Vec3(1, 0, 0));
print('homogeneous result (${f(h.x)}, ${f(h.y)}, ${f(h.z)}, ${f(h.w)})');
}homogeneous result (3.2990, 1.7500, 0.0000, 1.0000)
Vec3 transformDirection(Vec3 d)This · (d, 0): a direction (no translation).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
final moved = [for (final p in sq) m.transformPoint(p).xyz];
print('direction (1, 0, 0) -> ${v3(m.transformDirection(Vec3.unitX))} (no translation)');
}direction (1, 0, 0) -> (1.2990, 0.7500, 0.0000) (no translation)
void transposeInto(Mat4 out)The transpose of this into out (may be this).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
final t = Mat4.identity();
m.transposeInto(t);
print('translation now in the bottom row: ${t.entry(3, 0)}, ${t.entry(3, 1)}, ${t.entry(3, 2)}');
}translation now in the bottom row: 1.0, 2.0, 3.0
bool invertInto(Mat4 out)The general inverse via cofactors into out; returns false (and leaves out unchanged) when singular.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
final moved = [for (final p in sq) m.transformPoint(p).xyz];
final inv = Mat4.identity();
print('invertible: ${m.invertInto(inv)}');
print('inverse undoes it: ${v3(inv.transformPoint(m.transformPoint(Vec3(0.3, -0.7, 0)).xyz).xyz)}');
}invertible: true inverse undoes it: (0.3000, -0.7000, 0.0000)
double entry(int row, int column)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
final moved = [for (final p in sq) m.transformPoint(p).xyz];
print('row 0: ${[for (var c = 0; c < 4; c++) f(m.entry(0, c))]}');
}row 0: [1.2990, -0.5000, 0.0000, 2.0000]
Mat3
extension type Mat3._(Float32x4List c)
A 3 \times 3 matrix (normal matrices, rotations) as 3 SIMD columns (w lanes unused).
Constructors
factory Mat3.identity()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3.identity();
print('transform keeps points: ${v3(m.transform(Vec3(1, 2, 3)))}');
}transform keeps points: (1.0000, 2.0000, 3.0000)
Methods
void setRotation(Quat q)The rotation of q.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3.identity()..setRotation(Quat.axisAngle(Vec3.unitZ, math.pi / 3));
final pts = [for (var k = 0; k < 6; k++) m.transform(Vec3(math.cos(0.0) * (1 + k * 0.2), 0, 0))];
print('x rotated 60°: ${v3(m.transform(Vec3.unitX))}');
}x rotated 60°: (0.5000, 0.8660, 0.0000)
Vec3 transform(Vec3 p)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3.identity()..setRotation(Quat.axisAngle(Vec3.unitZ, math.pi / 4));
final sq = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(1, 1, 0), Vec3(0, 1, 0)];
final r = [for (final p in sq) m.transform(p)];
print('(1, 0, 0) -> ${v3(r[1])}');
}(1, 0, 0) -> (0.7071, 0.7071, 0.0000)
Mat4Buffer
final class Mat4Buffer
Contiguous storage for many 4 \times 4 matrices (e.g. instance transforms uploaded to the GPU): Mat4.view slices it.
Constructors
Mat4Buffer(this.count)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Mat4Buffer(3);
for (var i = 0; i < 3; i++) {
b[i].setTrs(Vec3(i.toDouble(), 0, 0), Quat.identity, Vec3(1, 1, 1));
}
print('${b.count} matrices in ${b.floats.length} floats (ready to upload)');
}3 matrices in 48 floats (ready to upload)
Properties
final int countfinal Float32x4List columnsOperators
Mat4 operator [](int i)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Mat4Buffer(2);
b[1].setTrs(Vec3(0, 5, 0), Quat.identity, Vec3(2, 2, 2));
print('second matrix moves the origin to ${v3(b[1].transformPoint(Vec3.zero).xyz)}');
}second matrix moves the origin to (0.0000, 5.0000, 0.0000)
HalfEdgeMesh
final class HalfEdgeMesh
Half-edge mesh built from an indexed triangle mesh: each directed edge knows its twin (opposite half-edge) and next edge, so adjacency queries (one-ring, boundaries, manifoldness) are O(1) per step.
Constructors
HalfEdgeMesh.fromTriangles(int vertexCount, List<(int, int, int)> tris)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = HalfEdgeMesh.fromTriangles(5, [(0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4)]);
print('one-ring of the centre: ${m.oneRing(4)}');
}one-ring of the centre: [0, 1, 2, 3]
Properties
final Int32List vertexOffinal Int32List nextfinal Int32List twinfinal Int32List outgoingint get halfEdgeCountMethods
List<int> oneRing(int v)The vertices around v (its one-ring), walking twin→next; stops at a boundary.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = HalfEdgeMesh.fromTriangles(4, [(0, 1, 2), (0, 2, 3)]);
for (var v = 0; v < 4; v++) {
print('vertex $v: ${m.oneRing(v)}');
}
}vertex 0: [1, 2] vertex 1: [2] vertex 2: [0] vertex 3: [0]
Noise
final class Noise
Gradient noise. Perlin's improved noise (Perlin 2002) and simplex noise (Perlin 2001; Gustavson's formulation), deterministic for a given seed, values roughly in [-1, 1].
Constructors
Noise([int seed = 0])import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Noise(1), b = Noise(1), c = Noise(2);
print('same seed, same value: ${a.perlin(0.5, 0.3, 0.1) == b.perlin(0.5, 0.3, 0.1)}');
print('another seed: ${f(c.perlin(0.5, 0.3, 0.1))}');
}same seed, same value: true another seed: 0.0815
Methods
double perlin(double x, double y, double z)Improved Perlin noise in 3D.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = Noise(7);
print('at an integer lattice point: ${n.perlin(2, 3, 4)}');
}at an integer lattice point: 0.0
double simplex2(double x, double y)2D simplex noise: sums of radially symmetric kernels on a skewed triangular grid; cheaper and with fewer directional artefacts than 2D Perlin.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = Noise(3);
print('simplex2(1.3, 2.7) = ${f(n.simplex2(1.3, 2.7))}');
}simplex2(1.3, 2.7) = 0.8138
double fbm(Vec3 p, {int octaves = 5, double lacunarity = 2, double gain = 0.5})Fractal Brownian motion: octaves of Perlin noise, each at double frequency and gain amplitude.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = Noise(5);
print('fbm at the origin region: ${f(n.fbm(Vec3(0.1, 0.2, 0.3)))}');
}fbm at the origin region: -0.0429
Sdf
abstract final class Sdf
Signed distance functions (negative inside). Composable primitives for ray marching, collision and procedural modelling (Quilez's catalogue).
Static
static double sphere(Vec3 p, double r)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('inside ${Sdf.sphere(Vec3.zero, 1)}, on ${Sdf.sphere(Vec3(1, 0, 0), 1)}, outside ${Sdf.sphere(Vec3(3, 0, 0), 1)}');
}inside -1.0, on 0.0, outside 2.0
static double box(Vec3 p, Vec3 halfExtents)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('d(2, 0, 0) = ${Sdf.box(Vec3(2, 0, 0), Vec3(1, 1, 1))}');
print('d(2, 2, 0) = ${f(Sdf.box(Vec3(2, 2, 0), Vec3(1, 1, 1)))} (corner distance √2)');
}d(2, 0, 0) = 1.0 d(2, 2, 0) = 1.4142 (corner distance √2)
static double roundBox(Vec3 p, Vec3 halfExtents, double r)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print(f(Sdf.roundBox(Vec3(2, 2, 0), Vec3(1, 1, 1), 0.3)));
}1.5385
static double torus(Vec3 p, double major, double minor)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('on the ring: ${f(Sdf.torus(Vec3(2, 0, 0), 2, 0.5))}');
}on the ring: -0.5000
static double capsule(Vec3 p, Vec3 a, Vec3 b, double r)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print(f(Sdf.capsule(Vec3(0, 1, 0), Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5)));
}0.5000
static double plane(Vec3 p, Vec3 n, double d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('above ${Sdf.plane(Vec3(0, 2, 0), Vec3.unitY, 0)}, below ${Sdf.plane(Vec3(0, -1, 0), Vec3.unitY, 0)}');
}above 2.0, below -1.0
static double smoothUnion(double a, double b, double k)Polynomial smooth minimum (blends two shapes over width k).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
double scene(Vec3 p, double k) => Sdf.smoothUnion(Sdf.sphere(p - Vec3(-0.8, 0, 0), 1), Sdf.sphere(p - Vec3(0.8, 0, 0), 1), k);
print('between the spheres: hard ${f(scene(Vec3(0, 0.9, 0), 0.001))}, smooth ${f(scene(Vec3(0, 0.9, 0), 0.5))}');
}between the spheres: hard 0.2039, smooth 0.0792
static double? march(double Function(Vec3) sdf, Vec3 origin, Vec3 dir, {double maxDist = 100, int maxSteps = 256, double epsilon = 1e-4})Sphere tracing (Hart 1996): step along the ray by the distance bound until within epsilon of the surface.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
double scene(Vec3 p) => math.min(Sdf.sphere(p - Vec3(0, 1, 0), 1), Sdf.plane(p, Vec3.unitY, 0));
final rows = <List<double>>[];
for (var i = 0; i < 9; i++) {
rows.add([for (var j = 0; j < 13; j++) (Sdf.march(scene, Vec3(0, 1, 5), Vec3(-0.6 + j * 0.1, 0.4 - i * 0.1, -1).normalized()) ?? 0).toDouble()]);
}
print('distance along the centre ray: ${f(Sdf.march(scene, Vec3(0, 1, 5), Vec3(0, 0, -1))!)}');
}distance along the centre ray: 4.0000
static Vec3 normal(double Function(Vec3) sdf, Vec3 p, {double h = 1e-4})Surface normal by central differences of the distance field.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = Sdf.normal((p) => Sdf.sphere(p, 1), Vec3(1, 1, 0).normalized());
print('sphere normal at 45°: ${v3(n)}');
}sphere normal at 45°: (0.7071, 0.7071, 0.0000)
Fx16
extension type const Fx16(int raw)
Q16.16 fixed point in a Raya int (64-bit on native): bit-identical results on every device, for deterministic lockstep or rollback netcode where floats can differ between CPUs and compilers.
Constructors
factory Fx16.fromDouble(double v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final x = Fx16.fromDouble(3.14159);
print('raw ${x.raw}, back ${x.toDouble()}');
}raw 205887, back 3.1415863037109375
factory Fx16.fromInt(int v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Fx16.fromInt(5).raw);
print(Fx16.fromInt(5).toDouble());
}327680 5.0
Static
static const one = Fx16(1 << 16)Methods
double toDouble()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Fx16(98304).toDouble());
}1.5
Operators
Fx16 operator +(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print('a + b = ${(a + b).toDouble()}');
}a + b = 3.25
Fx16 operator -(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print('a - b = ${(a - b).toDouble()}');
}a - b = 1.75
Fx16 operator -()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print((-a).toDouble());
}-2.5
Fx16 operator *(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print('a * b = ${(a * b).toDouble()}');
}a * b = 1.875
Fx16 operator /(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print('a / b = ${(a / b).toDouble()}');
}a / b = 3.3333282470703125
bool operator <(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print(b < a);
}true
bool operator >(Fx16 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
print(a > b);
}true
Fx32
extension type const Fx32(int raw)
Q32.32 fixed point. The 64 \times 64 product needs 128 bits, so multiplication splits each operand into 32-bit halves and recombines (exact for results in range).
Constructors
factory Fx32.fromDouble(double v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final x = Fx32.fromDouble(0.1);
print('raw ${x.raw}, back ${x.toDouble()}');
}raw 429496730, back 0.10000000009313226
factory Fx32.fromInt(int v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Fx32.fromInt(-3).toDouble());
}-3.0
Methods
double toDouble()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Fx32(1 << 31).toDouble());
}0.5
Operators
Fx32 operator +(Fx32 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
print('a + b = ${(a + b).toDouble()}');
}a + b = 1.25
Fx32 operator -(Fx32 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
print('a - b = ${(a - b).toDouble()}');
}a - b = 1.75
Fx32 operator *(Fx32 o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
print('a * b = ${(a * b).toDouble()}');
}a * b = -0.375
RobustPredicates
abstract final class RobustPredicates
Exact geometric predicates. A fast floating-point evaluation is used when its error bound (Shewchuk 1997) proves the sign; otherwise the determinant is recomputed exactly with rationals built from the doubles' exact binary values (every finite double is m \cdot 2^{e}). The sign is always correct — the property Shewchuk's adaptive predicates guarantee — at the cost of a slower fallback in rare near-degenerate cases.
Static
static int orient2d(double ax, double ay, double bx, double by, double cx, double cy)Sign of the orientation of (a, b, c): > 0 counter-clockwise, < 0 clockwise, 0 collinear.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('left turn: ${RobustPredicates.orient2d(0, 0, 1, 0, 0, 1)}');
print('right turn: ${RobustPredicates.orient2d(0, 0, 0, 1, 1, 0)}');
// Nearly collinear points where a plain determinant in doubles gets the sign wrong.
print('exact on tiny offsets: ${RobustPredicates.orient2d(0.5, 0.5, 12, 12, 24, 24 + 1e-14)}');
}left turn: 1 right turn: -1 exact on tiny offsets: 1
static int incircle(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)Sign of whether d is inside (> 0), outside (< 0) or on (0) the circle through counter-clockwise a, b, c.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('inside: ${RobustPredicates.incircle(0, 0, 1, 0, 0, 1, 0.5, 0.5)}');
print('outside: ${RobustPredicates.incircle(0, 0, 1, 0, 0, 1, 2, 2)}');
print('cocircular: ${RobustPredicates.incircle(1, 0, 0, 1, -1, 0, 0, -1)}');
}inside: 1 outside: -1 cocircular: 0
static int orient3d(List<double> a, List<double> b, List<double> c, List<double> d)Sign of the orientation of d relative to the plane through a, b, c (> 0 below, matching Shewchuk's orient3d).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print(RobustPredicates.orient3d([0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]));
print(RobustPredicates.orient3d([0, 0, 0], [1, 0, 0], [0, 1, 0], [0.3, 0.3, 0]));
}-1 0
Rat
final class Rat
An exact rational number on BigInt (denominators stay powers of two here).
Constructors
Rat(this.n, this.d)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
print('n ${a.n}, d ${a.d}');
}n 1, d 3
Static
static final zeroProperties
final BigInt nfinal BigInt dint get signOperators
Rat operator +(Rat o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
final r = a + b;
print('${r.n}/${r.d} (not reduced)');
}9/18 (not reduced)
Rat operator -(Rat o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
final r = a - b;
print('${r.n}/${r.d} (not reduced)');
}3/18 (not reduced)
Rat operator *(Rat o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
final r = a * b;
print('${r.n}/${r.d} (not reduced)');
}1/18 (not reduced)
Integrate
abstract final class Integrate
Time integrators for x'' = a(x, v, t), written over flat SIMD arrays so a whole particle system steps without allocation. Semi-implicit (symplectic) Euler and velocity Verlet conserve energy far better than explicit Euler; RK4 is 4th-order accurate for smooth forces.
Static
static void semiImplicitEuler(Float32x4List x, Float32x4List v, Float32x4List a, double dt)v + = a \cdot dt; x + = v \cdot dt (semi-implicit Euler).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A ball thrown at (3, 4) under gravity, one body, Δt = 0.1.
final x = Float32x4List.fromList([Float32x4(0, 0, 0, 0)]), v = Float32x4List.fromList([Float32x4(3, 4, 0, 0)]);
final a = Float32x4List.fromList([Float32x4(0, -9.8, 0, 0)]);
final path = <List<double>>[];
for (var i = 0; i < 9; i++) {
path.add([x[0].x, x[0].y]);
Integrate.semiImplicitEuler(x, v, a, 0.1);
}
print('after 0.9 s: (${f(x[0].x)}, ${f(x[0].y)})');
}after 0.9 s: (2.7000, -0.8100)
static void verlet(Float32x4List x, Float32x4List prev, Float32x4List a, double dt)Position Verlet: x_{n + 1} = 2x_{n} - x_{n - 1} + a dt^{2} (velocity is implicit; prev holds x_{n - 1}).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final x = Float32x4List.fromList([Float32x4(1, 0, 0, 0)]), prev = Float32x4List.fromList([Float32x4(1, 0, 0, 0)]);
final a = Float32x4List(1);
final ys = <List<double>>[];
for (var i = 0; i < 200; i++) {
a[0] = Float32x4(-x[0].x, 0, 0, 0); // a spring: a = −x
Integrate.verlet(x, prev, a, 0.05);
ys.add([i * 0.05, x[0].x]);
}
print('x(10) = ${f(x[0].x)}, cos 10 = ${f(math.cos(10))}');
}x(10) = -0.8249, cos 10 = -0.8391
static Float64List rk4(Float64List Function(double t, Float64List y) f, double t, Float64List y, double h)Classic RK4 for one state y' = f(t, y) (state as a Float64List).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
Float64List f2(double t, Float64List y) => Float64List.fromList([y[1], -y[0]]);
var y = Float64List.fromList([1, 0]);
for (var i = 0; i < 10; i++) {
y = Integrate.rk4(f2, i * 0.1, y, 0.1);
}
print('y(1) = ${y[0].toStringAsFixed(10)}, cos 1 = ${math.cos(1).toStringAsFixed(10)}');
}y(1) = 0.5403029671, cos 1 = 0.5403023059
Xpbd
abstract final class Xpbd
Extended position-based dynamics (Macklin, Müller and Chentanez 2016): constraints are solved on positions with a compliance \alpha (inverse stiffness) so stiffness is independent of the time step and iteration count.
Static
static double distance(Float32x4List x, Float32List w, int i, int j, double restLength, double compliance, double dt, double lambda)One XPBD distance-constraint projection between particles i and j (inverse masses w); lambda is the accumulated Lagrange multiplier for this constraint (reset to 0 each step). Returns the updated lambda.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Two particles 2 apart, constrained to rest length 1 (rigid: compliance 0).
final x = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(2, 0, 0, 0)]);
final w = Float32List.fromList([1, 1]);
final dl = Xpbd.distance(x, w, 0, 1, 1, 0, 1 / 60, 0);
print('after one solve: x0 = ${x[0].x}, x1 = ${x[1].x}, Δλ = ${f(dl)}');
}after one solve: x0 = 0.5, x1 = 1.5, Δλ = -0.5000
Inertia
abstract final class Inertia
Inertia tensors about the centre of mass (body frame).
Static
static Mat3d box(double mass, Vec3 size)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final i = Inertia.box(12, Vec3(1, 2, 3));
print('diagonal ${[f(i.at(0, 0)), f(i.at(1, 1)), f(i.at(2, 2))]}');
}diagonal [13.0000, 10.0000, 5.0000]
static Mat3d solidSphere(double mass, double r)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final i = Inertia.solidSphere(5, 2);
print('I = ${f(i.at(0, 0))} on each axis (2/5 m r² = 8)');
}I = 8.0000 on each axis (2/5 m r² = 8)
static Mat3d cylinder(double mass, double r, double h)Solid cylinder along Y.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final i = Inertia.cylinder(6, 1, 2);
print('about the axis ${f(i.at(1, 1))}, across ${f(i.at(0, 0))}');
}about the axis 3.0000, across 3.5000
static ({double mass, Vec3 center, Mat3d inertia}) mesh(Float32x4List p, Uint32List idx, double density)Mass properties of a closed triangle mesh of uniform density via the divergence theorem (Eberly's "Polyhedral Mass Properties", 2003): volume, centre of mass, inertia about the centre of mass.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A unit cube as 12 triangles, density 2: mass 2, inertia 2/6 = 1/3 about the centre.
final p = Float32x4List.fromList([for (var i = 0; i < 8; i++) Float32x4((i & 1).toDouble(), ((i >> 1) & 1).toDouble(), ((i >> 2) & 1).toDouble(), 0)]);
final idx = Uint32List.fromList([0, 2, 1, 1, 2, 3, 4, 5, 6, 5, 7, 6, 0, 1, 4, 1, 5, 4, 2, 6, 3, 3, 6, 7, 0, 4, 2, 2, 4, 6, 1, 3, 5, 3, 7, 5]);
final r = Inertia.mesh(p, idx, 2);
print('mass ${f(r.mass)}, centre ${v3(r.center)}, I_xx ${f(r.inertia.at(0, 0))}');
}mass 2.0000, centre (0.5000, 0.5000, 0.5000), I_xx 0.3333
Broadphase
abstract final class Broadphase
Broadphase: find candidate overlapping pairs of AABBs cheaply before exact narrowphase tests.
Static
static List<(int, int)> sweepAndPrune(Float32x4List mins, Float32x4List maxs)Sweep and prune on X: sort by min.x, sweep keeping an active list, test Y and Z overlap. O(n \log n + k).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final mins = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1.5, 0.5, 0, 0), Float32x4(4, 0, 0, 0), Float32x4(4.5, 1, 0, 0)]);
final maxs = Float32x4List.fromList([Float32x4(2, 2, 1, 0), Float32x4(3, 2.5, 1, 0), Float32x4(5, 1.5, 1, 0), Float32x4(6, 3, 1, 0)]);
print('pairs: ${Broadphase.sweepAndPrune(mins, maxs)}');
}pairs: [(0, 1), (2, 3)]
static List<(int, int)> uniformGrid(Float32x4List mins, Float32x4List maxs, double cellSize)Uniform grid: hash each box into cells of cellSize; boxes sharing a cell are candidates (deduplicated).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final mins = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1.5, 0.5, 0, 0), Float32x4(4, 0, 0, 0), Float32x4(4.5, 1, 0, 0)]);
final maxs = Float32x4List.fromList([Float32x4(2, 2, 1, 0), Float32x4(3, 2.5, 1, 0), Float32x4(5, 1.5, 1, 0), Float32x4(6, 3, 1, 0)]);
print('pairs: ${Broadphase.uniformGrid(mins, maxs, 2)}');
}pairs: [(0, 1), (2, 3)]
Quat
extension type const Quat._(Float32x4 v)
A unit quaternion for rotations (lanes x, y, z = vector part, w = scalar part), unboxed like Vec3.
Constructors
Quat(double x, double y, double z, double w)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quat(0, 0, math.sin(math.pi / 8), math.cos(math.pi / 8)); // 45° about z
print('(${f(q.x)}, ${f(q.y)}, ${f(q.z)}, ${f(q.w)}), rotate x -> ${v3(q.rotate(Vec3.unitX))}');
}(0.0000, 0.0000, 0.3827, 0.9239), rotate x -> (0.7071, 0.7071, 0.0000)
factory Quat.axisAngle(Vec3 axis, double radians)Rotation of radians about the unit axis: (\mathrm{axis} \cdot \sin(\frac{\theta}{2}), \cos(\frac{\theta}{2})), the half angle's turn made at RatioBoundary (the radians entry point; the rational one is RatioQuat.fromCayley).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final pts = [for (var k = 0; k < 8; k++) Quat.axisAngle(Vec3.unitZ, k * math.pi / 4).rotate(Vec3(1, 0.3, 0))];
print('90° about z: ${v3(Quat.axisAngle(Vec3.unitZ, math.pi / 2).rotate(Vec3.unitX))}');
}90° about z: (0.0000, 1.0000, 0.0000)
Static
static final identityProperties
double get xdouble get ydouble get zdouble get wQuat get conjugateMethods
double dot(Quat o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Quat.axisAngle(Vec3.unitZ, 0), b = Quat.axisAngle(Vec3.unitZ, math.pi / 2);
print('dot = ${f(a.dot(b))} = cos(θ/2) for θ = 90°');
}dot = 0.7071 = cos(θ/2) for θ = 90°
Quat normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quat(1, 2, 3, 4).normalized();
print('unit: ${f(q.dot(q))}');
}unit: 1.0000
Vec3 rotate(Vec3 p)Rotates p: v' = v + 2w(u \times v) + 2u \times (u \times v), with u the vector part (cheaper than q v q*).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quat.axisAngle(Vec3(1, 1, 1).normalized(), 2 * math.pi / 3);
print('x -> ${v3(q.rotate(Vec3.unitX))} (a cyclic permutation)');
}x -> (0.0000, 1.0000, 0.0000) (a cyclic permutation)
Quat slerp(Quat to, double t)Spherical linear interpolation along the shorter arc; falls back to normalised lerp when nearly parallel. The great circle through the two is walked by Turn.fraction of the turn (d, \sqrt{1 - d^{2}}) between them (square roots only): q = a \cdot (c - d \cdot \frac{s}{\sqrt{1 - d^{2}}}) + b \cdot \frac{s}{\sqrt{1 - d^{2}}}, the classical \frac{\sin((1 - t)\theta)}{\sin \theta}, \frac{\sin(t\theta)}{\sin \theta}.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Quat.identity, b = Quat.axisAngle(Vec3.unitZ, 2 * math.pi / 3);
final pts = [for (var i = 0; i <= 8; i++) a.slerp(b, i / 8).rotate(Vec3.unitX)];
print('halfway: ${v3(a.slerp(b, 0.5).rotate(Vec3.unitX))}');
}halfway: (0.5000, 0.8660, 0.0000)
Operators
Quat operator *(Quat o)Hamilton product: this ∘ o (apply o first, then this).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final qz = Quat.axisAngle(Vec3.unitZ, math.pi / 2), qx = Quat.axisAngle(Vec3.unitX, math.pi / 2);
print('qx·qz on x: ${v3((qx * qz).rotate(Vec3.unitX))}');
print('qz·qx on x: ${v3((qz * qx).rotate(Vec3.unitX))}');
}qx·qz on x: (0.0000, 0.0000, 1.0000) qz·qx on x: (0.0000, 1.0000, 0.0000)
QuatInterpolation
extension QuatInterpolation on Quat
Extra quaternion interpolation: normalised lerp (cheap, constant direction, non-constant speed) and SQUAD (smooth cubic spline through key rotations, Shoemake 1987).
Methods
Quat nlerp(Quat to, double t)Normalised linear interpolation along the shorter arc.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Quat.identity, b = Quat.axisAngle(Vec3.unitZ, 2.5);
final s = [for (var i = 0; i <= 8; i++) a.slerp(b, i / 8).rotate(Vec3.unitX)];
final n = [for (var i = 0; i <= 8; i++) a.nlerp(b, i / 8).rotate(Vec3.unitX)];
print('halfway nlerp ${v3(n[4])}, slerp ${v3(s[4])}');
}halfway nlerp (0.3153, 0.9490, 0.0000), slerp (0.3153, 0.9490, 0.0000)
Quat log()Quaternion logarithm of a unit quaternion: (\mathrm{axis} \cdot \frac{\theta}{2}, 0).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quat.axisAngle(Vec3.unitZ, 1.2);
final l = q.log();
print('log q = (${f(l.x)}, ${f(l.y)}, ${f(l.z)}, ${f(l.w)}) = θ/2 · axis');
}log q = (0.0000, 0.0000, 0.6000, 0.0000) = θ/2 · axis
Quat exp()Exponential of a pure quaternion (w ignored).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final l = Quat(0, 0, 0.6, 0);
final q = l.exp();
print('exp gives a 1.2 rad turn: ${v3(q.rotate(Vec3.unitX))}');
}exp gives a 1.2 rad turn: (0.3624, 0.9320, 0.0000)
Quat squad(Quat s1, Quat s2, Quat q2, double t)SQUAD between q1 (this) and q2 with inner control points s1, s2 from squadControl.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final keys = [for (final a in [0.0, 1.0, 2.5, 3.0]) Quat.axisAngle(Vec3.unitZ, a)];
final s1 = keys[1].squadControl(keys[0], keys[2]), s2 = keys[2].squadControl(keys[1], keys[3]);
final pts = [for (var i = 0; i <= 10; i++) keys[1].squad(s1, s2, keys[2], i / 10).rotate(Vec3.unitX)];
print('squad endpoint matches: ${v3(pts.last)} vs ${v3(keys[2].rotate(Vec3.unitX))}');
}squad endpoint matches: (-0.8011, 0.5985, 0.0000) vs (-0.8011, 0.5985, 0.0000)
Quat squadControl(Quat prev, Quat next)The SQUAD inner control point for this key given its neighbours prev and next: s_{i} = q_{i} \cdot \exp(-(\log(q_{i}^{-1} q_{i + 1}) + \log(q_{i}^{-1} q_{i - 1})) / 4).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q0 = Quat.axisAngle(Vec3.unitZ, 0), q1 = Quat.axisAngle(Vec3.unitZ, 1), q2 = Quat.axisAngle(Vec3.unitZ, 3);
final s = q1.squadControl(q0, q2);
print('control (${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}control (0.0000, 0.0000, 0.3663, 0.9305)
DualQuat
final class DualQuat
A unit dual quaternion: rigid transform (rotation + translation) as q_{r} + \varepsilon q_{d} with q_{d} = \tfrac{1}{2} t q_{r}. Blending them (dual-quaternion skinning, Kavan et al. 2007) avoids the volume loss of linear blend skinning.
Constructors
DualQuat(this.real, this.dual)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final d = DualQuat(Quat.identity, Quat(0.5, 0, 0, 0)); // translation (1, 0, 0)
print('moves origin to ${v3(d.transformPoint(Vec3.zero))}, translation ${v3(d.translation)}');
}moves origin to (1.0000, 0.0000, 0.0000), translation (1.0000, 0.0000, 0.0000)
factory DualQuat.fromRotationTranslation(Quat r, Vec3 t)Rotation r then translation t.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final d = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(2, 0, 0));
print('(1, 0, 0) -> ${v3(d.transformPoint(Vec3.unitX))}');
}(1, 0, 0) -> (2.0000, 1.0000, 0.0000)
Static
static DualQuat blend(List<DualQuat> dqs, List<double> weights)Dual-quaternion linear blending (DLB) of transforms with weights, antipodality-corrected, normalised.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = DualQuat.fromRotationTranslation(Quat.identity, Vec3.zero);
final b = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(0, 2, 0));
final pts = [for (var i = 0; i <= 10; i++) DualQuat.blend([a, b], [1 - i / 10, i / 10]).transformPoint(Vec3.unitX)];
final lin = [for (var i = 0; i <= 10; i++) Vec3.unitX.lerp(b.transformPoint(Vec3.unitX), i / 10)];
print('halfway: ${v3(pts[5])}');
}halfway: (1.1213, 1.7071, 0.0000)
Properties
final Quat realfinal Quat dualVec3 get translationMethods
Vec3 transformPoint(Vec3 p)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final d = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 4), Vec3(1, 1, 0));
final sq = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(1, 1, 0), Vec3(0, 1, 0)];
final m = [for (final p in sq) d.transformPoint(p)];
print('(1, 0, 0) -> ${v3(m[1])}');
}(1, 0, 0) -> (1.7071, 1.7071, 0.0000)
Operators
DualQuat operator *(DualQuat o)This ∘ o: apply o, then this.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = DualQuat.fromRotationTranslation(Quat.identity, Vec3(1, 0, 0));
final b = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3.zero);
print('a·b (turn, then move): ${v3((a * b).transformPoint(Vec3.unitX))}');
print('b·a (move, then turn): ${v3((b * a).transformPoint(Vec3.unitX))}');
}a·b (turn, then move): (1.0000, 1.0000, 0.0000) b·a (move, then turn): (0.0000, 2.0000, 0.0000)
LowDiscrepancy
abstract final class LowDiscrepancy
Low-discrepancy sequences: points that fill a domain more evenly than random numbers, so sampling-based estimates (image-based lighting, soft shadows, ambient occlusion, anti-aliasing jitter) converge faster with fewer samples.
Static
static double radicalInverse(int i, int base)The radical inverse of i in base: mirror its digits about the decimal point (base 2: 1 \to 0.5, 2 \to 0.25, 3 \to 0.75).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print([for (var i = 0; i < 8; i++) f(LowDiscrepancy.radicalInverse(i, 3))]);
}[0.0000, 0.3333, 0.6667, 0.1111, 0.4444, 0.7778, 0.2222, 0.5556]
static double vanDerCorput(int i)Base-2 radical inverse by bit reversal (van der Corput), exact for 32-bit indices.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print([for (var i = 0; i < 8; i++) LowDiscrepancy.vanDerCorput(i)]);
}[0.0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875]
static Vec2 hammersley(int i, int n)The ith of n Hammersley points in [0,1)²: (\frac{i}{n}, radical inverse base 2).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('first: ${[for (var i = 0; i < 4; i++) '(${f(LowDiscrepancy.hammersley(i, 128).x)}, ${f(LowDiscrepancy.hammersley(i, 128).y)})']}');
}first: [(0.0000, 0.0000), (0.0078, 0.5000), (0.0156, 0.2500), (0.0234, 0.7500)]
static Vec2 halton(int i)The ith Halton point in [0,1)² (bases 2 and 3); unlike Hammersley, needs no total count.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = LowDiscrepancy.halton(5);
print('halton(5) = (${f(p.x)}, ${f(p.y)})');
}halton(5) = (0.6250, 0.7778)
static Vec2 r2(int i)The ith point of Roberts' R2 sequence, based on the plastic number (the real root of x^{3} = x + 1).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = LowDiscrepancy.r2(1);
print('r2(1) = (${f(p.x)}, ${f(p.y)})');
}r2(1) = (0.2549, 0.0698)
static Vec3 uniformSphere(Vec2 u)Maps a point in [0,1)² to a uniformly distributed unit direction on the sphere.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final d = [for (var i = 0; i < 300; i++) LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 300))];
print('all unit: ${d.every((v) => (v.length - 1).abs() < 1e-5)}');
}all unit: true
static Vec3 importanceSampleGgx(Vec2 u, double roughness)GGX importance sampling of a half vector around +Z for roughness (alpha = roughness²): the distribution used to prefilter environment maps for the split-sum approximation (Karis 2013).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
for (final r in [0.2, 0.8]) {
final h = [for (var i = 0; i < 200; i++) LowDiscrepancy.importanceSampleGgx(LowDiscrepancy.hammersley(i, 200), r)];
print('roughness $r: mean cos θ = ${f(h.map((v) => v.z).reduce((a, b) => a + b) / 200)}');
}
final h = [for (var i = 0; i < 200; i++) LowDiscrepancy.importanceSampleGgx(LowDiscrepancy.hammersley(i, 200), 0.5)];
}roughness 0.2: mean cos θ = 0.9967 roughness 0.8: mean cos θ = 0.7813
Sobol2
abstract final class Sobol2
2D Sobol sequence (first two dimensions, Joe–Kuo direction numbers; dimension 1 is van der Corput), and blue-noise point sets by Bridson's Poisson-disk sampling (no two points closer than r, so samples look even without the visible structure of a grid).
Static
static Vec2 at(int i)The ith point (i \ge 0) in [0,1)².
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print([for (var i = 0; i < 4; i++) '(${Sobol2.at(i).x}, ${Sobol2.at(i).y})']);
}[(0.0, 0.0), (0.5, 0.5), (0.25, 0.75), (0.75, 0.25)]
Sh9
abstract final class Sh9
Real spherical harmonics up to band 2 (9 coefficients), the standard basis for diffuse lighting (Ramamoorthi and Hanrahan 2001, "An Efficient Representation for Irradiance Environment Maps").
A lighting environment L(\omega) is projected to coefficients L_{lm} = \int L(\omega) Y_{lm}(\omega) d\omega. Irradiance (cosine-convolved light) is then E(n) = \sum \hat{A}_{l} L_{lm} Y_{lm}(n) with \hat{A}_{0} = \pi, \hat{A}_{1} = \frac{2\pi}{3}, \hat{A}_{2} = \frac{\pi}{4}, so diffuse shading needs 9 RGB numbers per probe instead of a whole cube map.
Static
static const c1 = 0.488603static const c2 = 1.092548static const c3 = 0.315392static const c4 = 0.546274static const a1 = 2 * math.pi / 3static const a2 = math.pi / 4static void basis(Vec3 d, Float32List out)The 9 basis values for the unit direction d, written into out (length ≥ 9).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final y = Float32List(9);
Sh9.basis(Vec3.unitZ, y);
print('Y at +z: ${y.map(f).toList()}');
}Y at +z: [0.2821, 0.0000, 0.4886, 0.0000, 0.0000, 0.0000, 0.6308, 0.0000, 0.0000]
ShRgb
final class ShRgb
Spherical-harmonics radiance for RGB: 9 coefficients per channel (27 floats), accumulated from samples.
Constructors
ShRgb()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
final sh = ShRgb();
for (var i = 0; i < 200; i++) {
final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
sh.addSample(d, c, 4 * math.pi / 200);
}
print('27 coefficients; DC red/green/blue ${f(sh.coeffs[0])}, ${f(sh.coeffs[1])}, ${f(sh.coeffs[2])}');
}27 coefficients; DC red/green/blue 1.2336, -0.0087, 0.5674
Properties
final Float32List coeffsMethods
void addSample(Vec3 d, Vec3 rgb, double weight)Adds radiance rgb arriving from unit direction d with Monte Carlo weight weight (e.g. \frac{4\pi}{N} for N uniform sphere samples).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
final sh = ShRgb();
for (var i = 0; i < 200; i++) {
final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
sh.addSample(d, c, 4 * math.pi / 200);
}
print('up-facing irradiance ${v3(sh.irradiance(Vec3.unitZ))}');
}up-facing irradiance (1.4829, 1.3750, 2.2177)
Vec3 irradiance(Vec3 n)Irradiance E(n) for unit normal n; divide by \pi for Lambertian outgoing radiance.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
final sh = ShRgb();
for (var i = 0; i < 200; i++) {
final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
sh.addSample(d, c, 4 * math.pi / 200);
}
print('up ${v3(sh.irradiance(Vec3.unitZ))}');
print('sideways ${v3(sh.irradiance(Vec3.unitX))}');
print('down ${v3(sh.irradiance(Vec3(0, 0, -1)))}');
}up (1.4829, 1.3750, 2.2177) sideways (1.1863, 0.8699, 0.9068) down (0.3217, 0.3121, 0.2981)
Brdf
abstract final class Brdf
Physically based shading terms (Cook–Torrance microfacet model as used by UE4, Karis 2013, and Filament). These are CPU reference versions: the same formulas run in riptide_render's shaders, and tests compare the two.
Static
static double ggxD(double nDotH, double roughness)GGX / Trowbridge–Reitz normal distribution D(h) with alpha = roughness²: \alpha^{2} / (\pi ((n \cdot h)^{2}(\alpha^{2} - 1) + 1)^{2}).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('peak at n·h = 1: ${f(Brdf.ggxD(1, 0.5))}');
}peak at n·h = 1: 5.0930
static double schlickF(double vDotH, double f0)Schlick's Fresnel: F_{0} + (1 - F_{0})(1 - v \cdot h)^{5}.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('F at 0°: ${f(Brdf.schlickF(1, 0.04))}, at 90°: ${f(Brdf.schlickF(0, 0.04))}');
}F at 0°: 0.0400, at 90°: 1.0000
static double burleyDiffuse(double nDotV, double nDotL, double lDotH, double roughness)Burley's ("Disney") diffuse, normalised so a white surface under uniform light reflects ≈ 1 (Burley 2012).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print('rough, grazing light: ${f(Brdf.burleyDiffuse(1, 0.1, 0.5, 1))}');
print('smooth: ${f(Brdf.burleyDiffuse(1, 0.1, 0.5, 0))}');
}rough, grazing light: 0.3183 smooth: 0.2243
static double specular(Vec3 n, Vec3 v, Vec3 l, double roughness, double f0)Full specular lobe D \cdot V \cdot F for unit vectors n, v, l.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = Vec3.unitZ, v = Vec3(0, -0.5, 1).normalized();
final lobe = [for (var i = -40; i <= 40; i++) (() { final a = i * math.pi / 90; final l = Vec3(0, math.sin(a), math.cos(a)); return [i * 2, Brdf.specular(n, v, l, 0.4, 0.04)]; })()];
print('peak near the mirror direction');
}peak near the mirror direction
CubeMap
abstract final class CubeMap
Cube-map direction math (D_{3}D/Vulkan/Metal face order +X, -X, +Y, -Y, +Z, -Z; u right, v down).
Static
static Vec3 direction(int face, double u, double v)The direction through texel coordinates (u, v) in [-1, 1] on face.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
for (var face = 0; face < 6; face++) {
print('face $face centre -> ${v3(CubeMap.direction(face, 0.5, 0.5))}');
}
}face 0 centre -> (0.8165, -0.4082, -0.4082) face 1 centre -> (-0.8165, -0.4082, 0.4082) face 2 centre -> (0.4082, 0.8165, 0.4082) face 3 centre -> (0.4082, -0.8165, -0.4082) face 4 centre -> (0.4082, -0.4082, 0.8165) face 5 centre -> (-0.4082, -0.4082, -0.8165)
static ({int face, double u, double v}) faceUv(Vec3 d)The face and (u, v) in [-1, 1] for direction d (inverse of direction).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final r = CubeMap.faceUv(Vec3(0.2, 1, -0.3));
print('face ${r.face}, uv (${f(r.u)}, ${f(r.v)})');
final back = CubeMap.direction(r.face, r.u, r.v);
print('back to a direction: ${v3(back.normalized())} vs ${v3(Vec3(0.2, 1, -0.3).normalized())}');
}face 2, uv (0.2000, -0.3000) back to a direction: (0.1881, 0.9407, -0.2822) vs (0.1881, 0.9407, -0.2822)
static double texelSolidAngle(double u0, double v0, double u1, double v1)Solid angle subtended by the texel spanning (u_{0}, v_{0})–(u_{1}, v_{1}) on a face (exact, via the area element atan form).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A face split into 4 × 4 texels: corner texels subtend less than centre ones; the face is 4π/6.
var total = 0.0;
final rows = <List<double>>[];
for (var i = 0; i < 4; i++) {
final row = <double>[];
for (var j = 0; j < 4; j++) {
final w = CubeMap.texelSolidAngle(j / 4, i / 4, (j + 1) / 4, (i + 1) / 4);
total += w;
row.add((w * 1e4).roundToDouble() / 1e4);
}
rows.add(row);
}
print('face total ${f(total)}, 4π/6 = ${f(4 * math.pi / 6)}');
}face total 0.5236, 4π/6 = 2.0944
Signal
abstract final class Signal
Audio and signal processing kernels.
Static
static void fft(Float64List re, Float64List im, {bool inverse = false})In-place iterative radix-2 Cooley–Tukey FFT on separate real and imaginary arrays (length a power of two). inverse computes the unnormalised inverse; divide by n afterwards.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const n = 16;
final re = Float64List.fromList([for (var i = 0; i < n; i++) math.cos(2 * math.pi * 3 * i / n)]), im = Float64List(n);
Signal.fft(re, im);
print('|X_3| = ${f(math.sqrt(re[3] * re[3] + im[3] * im[3]))}');
}|X_3| = 8.0000
static Float32List resampleLinear(Float32List input, double ratio)Linear-interpolation resampling of input by ratio = outRate / inRate (cheap; use resampleSinc for audio).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final x = Float32List.fromList([for (var i = 0; i < 16; i++) math.sin(2 * math.pi * i / 16)]);
final y = Signal.resampleLinear(x, 2.5);
print('16 -> ${y.length} samples');
}16 -> 40 samples
static Float32List resampleSinc(Float32List input, double ratio, {int taps = 16})Windowed-sinc resampling (Hann window, taps per side): band-limited, so downsampling doesn't alias.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final x = Float32List.fromList([for (var i = 0; i < 16; i++) math.sin(2 * math.pi * i / 16)]);
final y = Signal.resampleSinc(x, 2.5);
print('16 -> ${y.length} samples, windowed sinc');
}16 -> 40 samples, windowed sinc
Biquad
final class Biquad
Second-order IIR filter (biquad) with the RBJ "Audio EQ Cookbook" designs, Direct Form I.
Constructors
factory Biquad.lowPass(double sampleRate, double freq, {double q = 0.7071})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.lowPass(48000, 1000);
print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}gain at 1 kHz 0.7071
factory Biquad.highPass(double sampleRate, double freq, {double q = 0.7071})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.highPass(48000, 1000);
print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}gain at 1 kHz 0.7071
factory Biquad.bandPass(double sampleRate, double freq, {double q = 1})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.bandPass(48000, 1000, q: 4);
print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}gain at 1 kHz 1.0000
factory Biquad.peaking(double sampleRate, double freq, double gainDb, {double q = 1})import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.peaking(48000, 1000, 6);
print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}gain at 1 kHz 1.9953
Properties
final double b0final double b1final double b2final double a1final double a2Methods
double process(double x)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.lowPass(1000, 20);
final y = <List<double>>[];
for (var i = 0; i < 200; i++) {
final x = (i ~/ 50).isEven ? 1.0 : -1.0; // a square wave
y.add([i / 1000, b.process(x)]);
}
print('smoothed square wave at the end: ${f(y.last[1])}');
}smoothed square wave at the end: -1.0307
double magnitudeAt(double sampleRate, double freq)Magnitude response |H(e^{j\omega})| at freq.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final b = Biquad.lowPass(48000, 1000);
print('at the cutoff: ${f(b.magnitudeAt(48000, 1000))} (1/√2)');
print('10× above: ${f(b.magnitudeAt(48000, 10000))}');
}at the cutoff: 0.7071 (1/√2) 10× above: 0.0073
Mat4Projections
extension Mat4Projections on Mat4
Projections and camera helpers beyond Mat4's basics. All use right-handed view space (camera looks down -Z) and clip-space depth in [0, 1] (Metal, Vulkan, WebGPU, D_{3}D).
Methods
void setOrthographic(double l, double r, double b, double t, double near, double far)Orthographic projection of the box [l, r] \times [b, t] × [-near, -far] to clip space.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setOrthographic(-2, 2, -1, 1, 0.1, 10);
print('(2, 1, −0.1) -> ${v3(m.transformPoint(Vec3(2, 1, -0.1)).xyz)} (a corner of the clip cube)');
}(2, 1, −0.1) -> (1.0000, 1.0000, 0.0000) (a corner of the clip cube)
void setPerspectiveReversedZ(double fovY, double aspect, double near, double far)Reversed-Z perspective: near maps to depth 1 and far to 0. With a floating-point depth buffer this spreads precision evenly across distance (Reed 2015, "Depth precision visualized").
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setPerspectiveReversedZ(math.pi / 3, 1, 0.1, 100);
for (final z in [-0.1, -1.0, -100.0]) {
print('z = $z -> depth ${f(m.transformPoint(Vec3(0, 0, z)).xyzOverW.z)} (near 1, far 0)');
}
}z = -0.1 -> depth 1.0000 (near 1, far 0) z = -1.0 -> depth 0.0991 (near 1, far 0) z = -100.0 -> depth 0.0000 (near 1, far 0)
void setPerspectiveInfiniteReversedZ(double fovY, double aspect, double near)Reversed-Z perspective with the far plane at infinity: depth = near / (-z_{\mathrm{view}}). No far clipping at all.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setPerspectiveInfiniteReversedZ(math.pi / 3, 1, 0.1);
print('depth at 1e6: ${m.transformPoint(Vec3(0, 0, -1e6)).xyzOverW.z.toStringAsExponential(2)} (no far plane)');
}depth at 1e6: 1.00e-7 (no far plane)
bool normalMatrixInto(Mat3 out)The normal matrix: inverse-transpose of the upper 3 \times 3, so normals stay perpendicular to surfaces under non-uniform scale. Written into out; returns false when the 3 \times 3 is singular.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat4.identity()..setTrs(Vec3.zero, Quat.identity, Vec3(2, 1, 1)); // non-uniform scale
final n = Mat3.identity();
print('ok: ${m.normalMatrixInto(n)}');
final normal = n.transform(Vec3(1, 1, 0).normalized()).normalized();
print('normal of the x = y plane after stretching x: ${v3(normal)}');
}ok: true normal of the x = y plane after stretching x: (0.4472, 0.8944, 0.0000)
Plane
extension type const Plane._(Float32x4 v)
A plane n \cdot p + d = 0 with unit normal n (lanes x, y, z = n, w = d). Points with n \cdot p + d > 0 are in front.
Constructors
Plane.fromNormalDistance(Vec3 n, double d)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = Plane.fromNormalDistance(Vec3.unitY, -2); // y = 2
print('d(0, 5, 0) = ${f(p.signedDistance(Vec3(0, 5, 0)))}');
}d(0, 5, 0) = 3.0000
factory Plane.fromPointNormal(Vec3 p, Vec3 n)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = Plane.fromPointNormal(Vec3(1, 1, 0), Vec3(1, 1, 0).normalized());
final pts = [Vec3(0, 0, 0), Vec3(2, 2, 0), Vec3(2, 0, 0)];
for (final q in pts) {
print('${v3(q)}: ${f(p.signedDistance(q))}');
}
}(0.0000, 0.0000, 0.0000): -1.4142 (2.0000, 2.0000, 0.0000): 1.4142 (2.0000, 0.0000, 0.0000): 0.0000
Properties
Vec3 get normaldouble get dMethods
double signedDistance(Vec3 p)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = Plane.fromPointNormal(Vec3.zero, Vec3.unitZ);
print([for (final z in [-2.0, 0.0, 3.0]) f(p.signedDistance(Vec3(1, 1, z)))]);
}[-2.0000, 0.0000, 3.0000]
Plane normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final p = Plane.fromNormalDistance(Vec3(0, 3, 4), -10).normalized();
print('normal ${v3(p.normal)}, d ${f(p.d)}');
}normal (0.0000, 0.6000, 0.8000), d -2.0000
Frustum
final class Frustum
The 6 frustum planes of a view-projection matrix, pointing inward (Gribb and Hartmann 2001), for depth in [0, 1].
Constructors
Frustum.fromViewProjection(Mat4 m)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
final fr = Frustum.fromViewProjection(proj);
print('6 planes; near plane normal ${v3(fr.planes[4].normal.normalized())}');
}6 planes; near plane normal (0.0000, 0.0000, -1.0000)
Properties
final List<Plane> planesMethods
bool containsPoint(Vec3 p)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
final fr = Frustum.fromViewProjection(proj);
for (final (n, p) in [('in', Vec3(0, 0, -5)), ('out', Vec3(8, 0, -5)), ('far', Vec3(0, 0, -12))]) {
print('$n: ${fr.containsPoint(p)}');
}
}in: true out: false far: false
bool intersectsSphere(Vec3 center, double radius)Conservative sphere test: false only when the sphere is entirely outside one plane.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
final fr = Frustum.fromViewProjection(proj);
print('sphere at (6, 0, −5) r 1.5: ${fr.intersectsSphere(Vec3(6, 0, -5), 1.5)}');
print('sphere at (8, 0, −5) r 1: ${fr.intersectsSphere(Vec3(8, 0, -5), 1)}');
}sphere at (6, 0, −5) r 1.5: true sphere at (8, 0, −5) r 1: false
bool intersectsAabb(Vec3 min, Vec3 max)Conservative AABB test using each plane's "positive vertex".
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
final fr = Frustum.fromViewProjection(proj);
print('box near the axis: ${fr.intersectsAabb(Vec3(-1, -1, -6), Vec3(1, 1, -4))}');
print('box off to the side: ${fr.intersectsAabb(Vec3(20, -1, -6), Vec3(22, 1, -4))}');
}box near the axis: true box off to the side: false
Vec2
extension type const Vec2._(Float32x4 v)
A 2D vector (lanes x, y).
Constructors
Vec2(double x, double y)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
print('a = (${a.x}, ${a.y}), |a| = ${f(a.length)}');
}a = (3.0, 1.0), |a| = 3.1623
Static
static final zeroProperties
double get xdouble get ydouble get length2double get lengthMethods
Vec2 scale(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final s = a.scale(1.5);
print('1.5a = (${s.x}, ${s.y})');
}1.5a = (4.5, 1.5)
Vec2 mul(Vec2 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final m = a.mul(b);
print('component-wise a·b = (${m.x}, ${m.y})');
}component-wise a·b = (3.0, 2.0)
double dot(Vec2 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
print('a · b = ${a.dot(b)}');
print('spread between them: ${f(1 - a.dot(b) * a.dot(b) / (a.length2 * b.length2))}');
}a · b = 5.0 spread between them: 0.5000
double cross(Vec2 o)The z of the 3D cross product: positive when o is counter-clockwise from this.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
print('a × b = ${a.cross(b)} (twice the triangle area, positive: b is to the left)');
}a × b = 5.0 (twice the triangle area, positive: b is to the left)
Vec2 normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final n = a.normalized();
print('(${f(n.x)}, ${f(n.y)}), length ${f(n.length)}');
}(0.9487, 0.3162), length 1.0000
Vec2 lerp(Vec2 to, double t)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final pts = [for (var i = 0; i <= 4; i++) a.lerp(b, i / 4)];
print([for (final p in pts) '(${f(p.x)}, ${f(p.y)})']);
}[(3.0000, 1.0000), (2.5000, 1.2500), (2.0000, 1.5000), (1.5000, 1.7500), (1.0000, 2.0000)]
Operators
Vec2 operator +(Vec2 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final s = a + b;
print('a + b = (${s.x}, ${s.y})');
}a + b = (4.0, 3.0)
Vec2 operator -(Vec2 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final d = a - b;
print('a − b = (${d.x}, ${d.y})');
}a − b = (2.0, -1.0)
Vec2 operator -()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec2(3, 1), b = Vec2(1, 2);
final n = -a;
print('−a = (${n.x}, ${n.y})');
}−a = (-3.0, -1.0)
Vec3
extension type const Vec3._(Float32x4 v)
A 3D vector (lanes x, y, z; w is 0).
Constructors
Vec3(double x, double y, double z)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('a = ${v3(a)}, |a|² = ${f(a.length2)}');
}a = (1.0000, 2.0000, 3.0000), |a|² = 14.0000
Vec3.fromSimd(Float32x4 v)Wraps a SIMD value read from storage (e.g. a Float32x4List); its w lane is cleared.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final v = Vec3.fromSimd(Float32x4(1, 2, 3, 99));
print('${v3(v)} (the w lane is ignored)');
}(1.0000, 2.0000, 3.0000) (the w lane is ignored)
Static
static final zerostatic final unitXstatic final unitYstatic final unitZProperties
double get xdouble get ydouble get zdouble get length2double get lengthMethods
Vec3 scale(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('2a = ${v3(a.scale(2))}');
}2a = (2.0000, 4.0000, 6.0000)
Vec3 mul(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('a ∘ b = ${v3(a.mul(b))}');
}a ∘ b = (-2.0000, 1.0000, 3.0000)
Vec3 min(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('min = ${v3(a.min(b))}');
}min = (-2.0000, 0.5000, 1.0000)
Vec3 max(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('max = ${v3(a.max(b))}');
}max = (1.0000, 2.0000, 3.0000)
double dot(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('a · b = ${f(a.dot(b))}');
}a · b = 2.0000
Vec3 cross(Vec3 o)Cross product via two shuffles: (a.yzx * b.zxy) - (a.zxy * b.yzx).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
final c = a.cross(b);
print('a × b = ${v3(c)}; perpendicular: ${f(c.dot(a))}, ${f(c.dot(b))}');
}a × b = (0.5000, -7.0000, 4.5000); perpendicular: 0.0000, 0.0000
Vec3 normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
final n = a.normalized();
print('${v3(n)}, length ${f(n.length)}');
}(0.2673, 0.5345, 0.8018), length 1.0000
Vec3 lerp(Vec3 to, double t)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('halfway ${v3(a.lerp(b, 0.5))}');
}halfway (-0.5000, 1.2500, 2.0000)
Vec3 reflect(Vec3 n)Reflects this direction about the unit normal n: d - 2(d \cdot n)n.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final d = Vec3(1, -1, 0), n = Vec3.unitY;
final r = d.reflect(n);
print('${v3(d)} reflected off the floor: ${v3(r)}');
}(1.0000, -1.0000, 0.0000) reflected off the floor: (1.0000, 1.0000, 0.0000)
Vec4 toVec4(double w)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
final p = a.toVec4(1), d = a.toVec4(0);
print('point w = ${p.w}, direction w = ${d.w}');
}point w = 1.0, direction w = 0.0
Operators
Vec3 operator +(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('a + b = ${v3(a + b)}');
}a + b = (-1.0000, 2.5000, 4.0000)
Vec3 operator -(Vec3 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('a − b = ${v3(a - b)}');
}a − b = (3.0000, 1.5000, 2.0000)
Vec3 operator -()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
print('−a = ${v3(-a)}');
}−a = (-1.0000, -2.0000, -3.0000)
Vec4
extension type const Vec4._(Float32x4 v)
A 4D vector or homogeneous point.
Constructors
Vec4(double x, double y, double z, double w)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
print('a = ' + '(${f(a.x)}, ${f(a.y)}, ${f(a.z)}, ${f(a.w)})');
print('perspective divide of (2, 4, 6, 2): ${v3(Vec4(2, 4, 6, 2).xyzOverW)}');
}a = (1.0000, 2.0000, 3.0000, 1.0000) perspective divide of (2, 4, 6, 2): (1.0000, 2.0000, 3.0000)
const Vec4.fromSimd(this.v)Wraps a SIMD value read from storage or produced by a matrix.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final v = Vec4.fromSimd(Float32x4(1, 2, 3, 4));
print('w = ${v.w}');
}w = 4.0
Static
static final zeroProperties
double get xdouble get ydouble get zdouble get wdouble get lengthVec3 get xyzMethods
Vec4 scale(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final s = a.scale(2);
print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}(2.0000, 4.0000, 6.0000, 2.0000)
Vec4 mul(Vec4 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final m = a.mul(b);
print('(${f(m.x)}, ${f(m.y)}, ${f(m.z)}, ${f(m.w)})');
}(0.5000, 0.0000, -3.0000, 0.0000)
double dot(Vec4 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
print('a · b = ${f(a.dot(b))}');
}a · b = -2.5000
Vec4 lerp(Vec4 to, double t)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final m = a.lerp(b, 0.5);
print('(${f(m.x)}, ${f(m.y)}, ${f(m.z)}, ${f(m.w)})');
}(0.7500, 1.0000, 1.0000, 0.5000)
Operators
Vec4 operator +(Vec4 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final s = a + b;
print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}(1.5000, 2.0000, 2.0000, 1.0000)
Vec4 operator -(Vec4 o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final s = a - b;
print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}(0.5000, 2.0000, 4.0000, 1.0000)
Vec4 operator -()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
final s = -a;
print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}(-1.0000, -2.0000, -3.0000, -1.0000)
Vec3d
final class Vec3d
A 3D vector in double precision for simulation state (rigid bodies, orbits, soft bodies), where float32's 7 digits lose momentum and energy over long runs. A value class: on Raya AOT it is stored inline and passed in registers; on other runtimes it is a small immutable object.
Constructors
const Vec3d(this.x, this.y, this.z)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print('a = $a, |a| = ${a.length}');
print('double precision: 0.1 + 0.2 stays ${(b.x + b.y)}');
}a = Vec3d(1.0, 2.0, 2.0), |a| = 3.0 double precision: 0.1 + 0.2 stays 0.30000000000000004
factory Vec3d.read(Float64List a, int i)Reads element i of an interleaved xyz array.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final buf = Float64List.fromList([1, 2, 3, 4, 5, 6]);
print('second vector: ${Vec3d.read(buf, 1)}');
}second vector: Vec3d(4.0, 5.0, 6.0)
factory Vec3d.fromVec3(Vec3 v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final v = Vec3d.fromVec3(Vec3(0.1, 0.2, 0.3));
print('${v.x} (the float32 value of 0.1, widened)');
}0.10000000149011612 (the float32 value of 0.1, widened)
Static
static const zero = Vec3d(0, 0, 0)static const unitX = Vec3d(1, 0, 0)static const unitY = Vec3d(0, 1, 0)static const unitZ = Vec3d(0, 0, 1)Properties
final double xfinal double yfinal double zdouble get length2double get lengthint get hashCodeMethods
Vec3d mul(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a.mul(b));
}Vec3d(0.1, 0.4, 0.6)
double dot(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a.dot(b));
}1.1
Vec3d cross(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
final c = a.cross(b);
print('$c, ⟂ a: ${c.dot(a).abs() < 1e-15}');
}Vec3d(0.19999999999999996, -0.09999999999999998, 0.0), ⟂ a: true
Vec3d normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print('${a.normalized()} (exactly (1, 2, 2)/3)');
}Vec3d(0.3333333333333333, 0.6666666666666666, 0.6666666666666666) (exactly (1, 2, 2)/3)
Vec3d min(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a.min(b));
}Vec3d(0.1, 0.2, 0.3)
Vec3d max(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a.max(b));
}Vec3d(1.0, 2.0, 2.0)
Vec3d abs()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print(const Vec3d(-1, 2, -3).abs());
}Vec3d(1.0, 2.0, 3.0)
void write(Float64List a, int i)Writes this vector as element i of an interleaved xyz array.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
final buf = Float64List(6);
a.write(buf, 0);
b.write(buf, 1);
print(buf);
}[1.0, 2.0, 2.0, 0.1, 0.2, 0.3]
Vec3 toVec3()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(v3(b.toVec3()));
}(0.1000, 0.2000, 0.3000)
(Vec3d, Vec3d) basis()Two unit vectors that complete this unit vector to an orthonormal basis (Duff et al. 2017, branchless).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final n = const Vec3d(1, 1, 1).normalized();
final (t, s) = n.basis();
print('t = ${d3(t)}, s = ${d3(s)}');
print('orthonormal: ${f(t.dot(n))}, ${f(s.dot(n))}, ${f(t.dot(s))}, |t| = ${f(t.length)}');
}t = (0.7887, -0.2113, -0.5774), s = (-0.2113, 0.7887, -0.5774) orthonormal: 0.0000, 0.0000, 0.0000, |t| = 1.0000
String toString()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a);
}Vec3d(1.0, 2.0, 2.0)
Operators
Vec3d operator +(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a + b);
}Vec3d(1.1, 2.2, 2.3)
Vec3d operator -(Vec3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a - b);
}Vec3d(0.9, 1.8, 1.7)
Vec3d operator -()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(-a);
}Vec3d(-1.0, -2.0, -2.0)
Vec3d operator *(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a * 0.5);
}Vec3d(0.5, 1.0, 1.0)
Vec3d operator /(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a / 3);
}Vec3d(0.3333333333333333, 0.6666666666666666, 0.6666666666666666)
double operator [](int i)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print([a[0], a[1], a[2]]);
}[1.0, 2.0, 2.0]
bool operator ==(Object other)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
print(a == const Vec3d(1, 2, 2));
print(b == const Vec3d(0.1, 0.2, 0.30000000000000004));
}true false
Quatd
final class Quatd
A unit quaternion in double precision (x, y, z vector part, w scalar part), for rigid-body orientation.
Constructors
const Quatd(this.x, this.y, this.z, this.w)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
const q = Quatd(0, 0, 0, 1);
print('identity: $q');
}identity: Quatd(0.0, 0.0, 0.0, 1.0)
factory Quatd.axisAngle(Vec3d axis, double radians)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
print('x -> ${d3(q.rotate(Vec3d.unitX))}');
}x -> (0.0000, 1.0000, 0.0000)
factory Quatd.fromQuat(Quat q)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.fromQuat(Quat.axisAngle(Vec3.unitX, 1));
print('widened: $q');
}widened: Quatd(0.4794255495071411, 0.0, 0.0, 0.8775825500488281)
Static
static const identity = Quatd(0, 0, 0, 1)Properties
final double xfinal double yfinal double zfinal double wQuatd get conjugateMethods
double dot(Quatd o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
print('q·q = ${q.dot(q)}');
}q·q = 1.0
Quatd normalized()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
print(const Quatd(1, 1, 1, 1).normalized());
}Quatd(0.5, 0.5, 0.5, 0.5)
Vec3d rotate(Vec3d p)Rotates p: p + 2w(u \times p) + 2u \times (u \times p).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
final pts = [for (var k = 0; k <= 4; k++) Quatd.axisAngle(Vec3d.unitZ, k * math.pi / 8).rotate(const Vec3d(2, 0, 0))];
print('y axis: ${d3(q.rotate(Vec3d.unitY))}');
}y axis: (-1.0000, 0.0000, 0.0000)
Vec3d unrotate(Vec3d p)Rotates p by the inverse rotation.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
final p = q.rotate(const Vec3d(1, 2, 3));
print('back: ${d3(q.unrotate(p))}');
}back: (1.0000, 2.0000, 3.0000)
Quatd integrate(Vec3d omega, double dt)Integrates angular velocity omega (world frame, rad/s) over dt: q + \tfrac{1}{2} dt (\omega, 0) \circ q, renormalised. The first-order update used by Box2D, Bullet and Jolt; exact enough at game time steps, and normalising each step keeps it a rotation.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// Spin at 1 rad/s about z for 1 s in 100 first-order steps.
var q = Quatd.identity;
for (var i = 0; i < 100; i++) {
q = q.integrate(Vec3d.unitZ, 0.01);
}
print('angle reached ${f(2 * math.atan2(q.z, q.w))} rad (1)');
}angle reached 1.0000 rad (1)
Quatd integrateExact(Vec3d omega, double dt)Integrates by the exact rotation exp(\tfrac{1}{2} \omega dt) (no first-order error; preferred for fast spins).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
var q = Quatd.identity;
for (var i = 0; i < 10; i++) {
q = q.integrateExact(const Vec3d(0, 0, 20), 0.01); // fast spin
}
print('angle ${f(2 * math.atan2(q.z, q.w))} rad (2 exactly)');
}angle 2.0000 rad (2 exactly)
Mat3d toMat3d()The rotation matrix R (row-major) with R v = \operatorname{rotate}(v).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
final m = q.toMat3d();
print('m · x = (${f(m.at(0, 0))}, ${f(m.at(1, 0))}, ${f(m.at(2, 0))})');
}m · x = (0.0000, 1.0000, 0.0000)
Quat toQuat()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
final f32 = q.toQuat();
print('as float32: ${v3(f32.rotate(Vec3.unitX))}');
}as float32: (0.0000, 1.0000, 0.0000)
String toString()import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
print(q);
}Quatd(0.0, 0.0, 0.7071067811865475, 0.7071067811865476)
Operators
Quatd operator *(Quatd o)Hamilton product: this ∘ o (apply o first).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final a = Quatd.axisAngle(Vec3d.unitZ, math.pi / 4);
print('two eighth turns: ${d3((a * a).rotate(Vec3d.unitX))}');
}two eighth turns: (0.0000, 1.0000, 0.0000)
Mat3dVec
extension Mat3dVec on Mat3d
Mat3d operations on Vec3d, used for inertia tensors.
Static
static Mat3d skew(Vec3d v)The skew-symmetric cross-product matrix v× (so v× w = v \times w).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final s = Mat3dVec.skew(const Vec3d(1, 2, 3));
print('[v]ₓ · w = ${d3(s.mulVec(const Vec3d(4, 5, 6)))}, v × w = ${d3(const Vec3d(1, 2, 3).cross(const Vec3d(4, 5, 6)))}');
}[v]ₓ · w = (-3.0000, 6.0000, -3.0000), v × w = (-3.0000, 6.0000, -3.0000)
Methods
Vec3d mulVec(Vec3d v)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
print(d3(m.mulVec(const Vec3d(1, 1, 1))));
}(2.0000, 3.0000, 4.0000)
Vec3d mulVecT(Vec3d v)M^{T} v.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3d.of([1, 2, 0, 0, 1, 0, 0, 0, 1]);
print('M x = ${d3(m.mulVec(const Vec3d(1, 1, 0)))}, Mᵀx = ${d3(m.mulVecT(const Vec3d(1, 1, 0)))}');
}M x = (3.0000, 1.0000, 0.0000), Mᵀx = (1.0000, 3.0000, 0.0000)
Mat3d scaled(double s)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
final s = m.scaled(0.5);
print([s.at(0, 0), s.at(1, 1), s.at(2, 2)]);
}[1.0, 1.5, 2.0]
Mat3d inverse()The inverse by the adjugate; a zero matrix when singular (infinite mass in that direction).
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3d.of([4, 7, 0, 2, 6, 0, 0, 0, 1]);
final i = m.inverse();
final p = m * i;
print('M·M⁻¹ diagonal ${[f(p.at(0, 0)), f(p.at(1, 1)), f(p.at(2, 2))]}, off ${f(p.at(0, 1))}');
}M·M⁻¹ diagonal [1.0000, 1.0000, 1.0000], off 0.0000
Mat3d rotatedBy(Mat3d r)R M R^{T}: a body-frame tensor in world frame.
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
// A body-frame inertia tensor seen in the world after a 45° turn about z.
final i = Mat3d.of([1, 0, 0, 0, 4, 0, 0, 0, 5]);
final r = Quatd.axisAngle(Vec3d.unitZ, math.pi / 4).toMat3d();
final w = i.rotatedBy(r);
print('trace kept: ${f(w.at(0, 0) + w.at(1, 1) + w.at(2, 2))}');
}trace kept: 10.0000
Operators
Mat3d operator +(Mat3d o)import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
void main() {
final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
final s = m + Mat3d.identity();
print([s.at(0, 0), s.at(1, 1), s.at(2, 2)]);
}[3.0, 4.0, 5.0]
RatioBackend
abstract interface class RatioBackend<S, V>
One number system for ratio geometry: the same operations (quadrance, spread, cross, twist, solid and dihedral spreads, Gram determinants, reflections, Cayley rotations) in a fixed metric, over scalars S and vectors V.
This is the contract every backend meets and the law suite (package:riptide_math/ratio_laws.ray) checks: f64 and f32 (FloatRatio2, FloatRatio3), exact int64 rationals (Int64Ratio), the batched f32 kernels (CPU and Metal, RatioBatch), and in riptide_rational_geometry every exact field (\mathbb{Q}, \mathbb{Q}(\sqrt{d}), \mathbb{Z}/p) through FieldRatio. Hot paths don't call through this interface; they call the same formulas directly (RatioF, the Vec3 extensions, Turn, RatioQuat), which the float backends delegate to. Inputs are built from integers (vector) so every backend can be fed the same cases and compared.
Properties
String get nameint get dimensionMethods
S fromInt(int n)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('fromInt(7) = ${b.fromInt(7)}');
}fromInt(7) = 7
S add(S a, S b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 add 1/6 = ${b.add(x, y)}');
}1/3 add 1/6 = 1/2
S sub(S a, S b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 sub 1/6 = ${b.sub(x, y)}');
}1/3 sub 1/6 = 1/6
S mul(S a, S b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 mul 1/6 = ${b.mul(x, y)}');
}1/3 mul 1/6 = 1/18
S div(S a, S b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 div 1/6 = ${b.div(x, y)}');
}1/3 div 1/6 = 2
bool isZero(S a, S scale)Whether a is zero: exactly for exact backends, within the backend's rounding relative to scale otherwise.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}1/3 − 2/6 is zero: true
double? toDouble(S a)A double for comparing across backends; null where there is none (finite fields).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}0.6666666666666666
V vector(List<int> coords)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.vector([3, 4]));
}[3, 4]
V vadd(V a, V b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.vadd(b.vector([3, 4]), b.vector([4, -3])));
}[7, 1]
V vsub(V a, V b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.vsub(b.vector([3, 4]), b.vector([4, -3])));
}[-1, 7]
V vscale(S k, V v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([3, 4])));
}[3/2, 2]
bool same(V a, V b, S scale)Whether a and b are equal: exactly for exact backends; for floats within the backend's rounding of an intermediate of coordinate size √scale (pass the largest Euclidean quadrance size involved, since an ill-conditioned step, such as a reflection in a nearly null normal, scales the error with it).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.same(b.vscale(b.fromInt(2), b.vector([1, 2])), b.vadd(b.vector([1, 2]), b.vector([1, 2])), b.size(b.vector([1, 2]))));
}true
S size(V v)The Euclidean quadrance of the coordinates (x^{2} + y^{2} + \cdots), whatever the metric: the size used for error scales.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('size of [3, 4]: ${b.size(b.vector([3, 4]))}');
}size of [3, 4]: 25
S dot(V a, V b)B(a, b) in the backend's metric.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('dot([3, 4], [1, 2]) = ${b.dot(b.vector([3, 4]), b.vector([1, 2]))}');
print('dot([3, 4], [4, -3]) = ${b.dot(b.vector([3, 4]), b.vector([4, -3]))}');
}dot([3, 4], [1, 2]) = 11 dot([3, 4], [4, -3]) = 0
S quadrance(V a)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('Q([3, 4]) = ${b.quadrance(b.vector([3, 4]))}');
}Q([3, 4]) = 25
S spread(V a, V b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('spread([3, 4], [1, 2]) = ${b.spread(b.vector([3, 4]), b.vector([1, 2]))}');
print('spread([3, 4], [4, -3]) = ${b.spread(b.vector([3, 4]), b.vector([4, -3]))}');
}spread([3, 4], [1, 2]) = 4/125 spread([3, 4], [4, -3]) = 1
S cross(V a, V b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('cross([3, 4], [1, 2]) = ${b.cross(b.vector([3, 4]), b.vector([1, 2]))}');
print('cross([3, 4], [4, -3]) = ${b.cross(b.vector([3, 4]), b.vector([4, -3]))}');
}cross([3, 4], [1, 2]) = 121/125 cross([3, 4], [4, -3]) = 0
S twist(V a, V b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('twist([3, 4], [1, 2]) = ${b.twist(b.vector([3, 4]), b.vector([1, 2]))}');
print('twist([3, 4], [1, 3]) = ${b.twist(b.vector([3, 4]), b.vector([1, 3]))}');
}twist([3, 4], [1, 2]) = 4/121 twist([3, 4], [1, 3]) = 1/9
S solidSpread(V a, V b, V c)The solid spread \det \frac{G(a, b, c)}{Q(a)Q(b)Q(c)} (three dimensions).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
// Three-dimensional quantities need a 3-D metric.
final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
print('solid spread of the axes: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([0, 0, 1]))}');
print('of a flat triple: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([1, 1, 0]))}');
}solid spread of the axes: 1 of a flat triple: 0
S dihedralSpread(V common, V a, V b)The dihedral spread between span(common, a) and span(common, b) (three dimensions).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
// Three-dimensional quantities need a 3-D metric.
final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
print('dihedral spread of the xz and yz planes along z: ${b3.dihedralSpread(b3.vector([0, 0, 1]), b3.vector([1, 0, 0]), b3.vector([1, 1, 0]))}');
}dihedral spread of the xz and yz planes along z: 1/2
S gramDeterminant(List<V> vs)Det of the Gram matrix B(v_{i}, v_{j}).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print(b.gramDeterminant([b.vector([3, 4]), b.vector([1, 2])]));
}4
V reflect(V v, V normal)The reflection of v in the hyperplane B-perpendicular to normal.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
print('reflect [3, 4] in [1, 2]: ${b.reflect(b.vector([3, 4]), b.vector([1, 2]))}');
}reflect [3, 4] in [1, 2]: [-7/5, -24/5]
V perpendicular(List<V> vs)A vector B-perpendicular to the dimension - 1 vectors vs (2-D: one vector; 3-D: the B-vector product).
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final p = b.perpendicular([b.vector([3, 4])]);
print('$p, dot ${b.dot(p, b.vector([3, 4]))}');
}[-4, 3], dot 0
V rotate(List<S> cayley, V v)v under the Cayley rotation (B - K)^{-1}(B + K) of the metric: cayley is t in two dimensions, the Gibbs vector g (K = gₓ) in three.
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
// Any backend implements the same exact-geometry interface; here the exact int64 one.
final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
final t = b.div(b.fromInt(1), b.fromInt(2));
print('rotate [3, 4] by t = 1/2: ${b.rotate([t], b.vector([3, 4]))}');
}rotate [3, 4] by t = 1/2: [5, 0]
FloatRatio2
final class FloatRatio2 implements RatioBackend<double, Float64List>
F64 or f32 ratio geometry in a planar Metric2, on the float core. f32 stores every vector and result in single precision (what a GPU buffer holds); arithmetic is done in double and rounded once per result.
Constructors
FloatRatio2(this.metric, {this.single = false})import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final blue = FloatRatio2(const Metric2(1, 0, 1)), red = FloatRatio2(const Metric2(1, 0, -1));
final u = blue.vector([3, 4]);
print('blue Q = ${blue.quadrance(u)}, red Q = ${red.quadrance(red.vector([3, 4]))}');
final single = FloatRatio2(const Metric2(1, 0, 1), single: true);
print('float32 rounding: ${single.div(single.fromInt(1), single.fromInt(3))}');
}blue Q = 25.0, red Q = -7.0 float32 rounding: 0.3333333432674408
Properties
final Metric2 metricfinal bool singleString get nameint get dimensionbool get exactMethods
double fromInt(int n)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('fromInt(7) = ${b.fromInt(7)}');
}fromInt(7) = 7.0
double add(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 add 1/6 = ${b.add(x, y)}');
}1/3 add 1/6 = 0.5
double sub(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 sub 1/6 = ${b.sub(x, y)}');
}1/3 sub 1/6 = 0.16666666666666666
double mul(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 mul 1/6 = ${b.mul(x, y)}');
}1/3 mul 1/6 = 0.05555555555555555
double div(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 div 1/6 = ${b.div(x, y)}');
}1/3 div 1/6 = 2.0
bool isZero(double a, double scale)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}1/3 − 2/6 is zero: true
double? toDouble(double a)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}0.6666666666666666
Float64List vector(List<int> c)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.vector([3, 4]));
}[3.0, 4.0]
Float64List vadd(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.vadd(b.vector([3, 4]), b.vector([4, -3])));
}[7.0, 1.0]
Float64List vsub(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.vsub(b.vector([3, 4]), b.vector([4, -3])));
}[-1.0, 7.0]
Float64List vscale(double k, Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([3, 4])));
}[1.5, 2.0]
bool same(Float64List a, Float64List b, double scale)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.same(b.vscale(b.fromInt(2), b.vector([1, 2])), b.vadd(b.vector([1, 2]), b.vector([1, 2])), b.size(b.vector([1, 2]))));
}true
double size(Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('size of [3, 4]: ${b.size(b.vector([3, 4]))}');
}size of [3, 4]: 25.0
double dot(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('dot([3, 4], [1, 2]) = ${b.dot(b.vector([3, 4]), b.vector([1, 2]))}');
print('dot([3, 4], [4, -3]) = ${b.dot(b.vector([3, 4]), b.vector([4, -3]))}');
}dot([3, 4], [1, 2]) = 11.0 dot([3, 4], [4, -3]) = 0.0
double quadrance(Float64List a)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('Q([3, 4]) = ${b.quadrance(b.vector([3, 4]))}');
}Q([3, 4]) = 25.0
double spread(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('spread([3, 4], [1, 2]) = ${b.spread(b.vector([3, 4]), b.vector([1, 2]))}');
print('spread([3, 4], [4, -3]) = ${b.spread(b.vector([3, 4]), b.vector([4, -3]))}');
}spread([3, 4], [1, 2]) = 0.032 spread([3, 4], [4, -3]) = 1.0
double cross(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('cross([3, 4], [1, 2]) = ${b.cross(b.vector([3, 4]), b.vector([1, 2]))}');
print('cross([3, 4], [4, -3]) = ${b.cross(b.vector([3, 4]), b.vector([4, -3]))}');
}cross([3, 4], [1, 2]) = 0.968 cross([3, 4], [4, -3]) = 0.0
double twist(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('twist([3, 4], [1, 2]) = ${b.twist(b.vector([3, 4]), b.vector([1, 2]))}');
print('twist([3, 4], [1, 3]) = ${b.twist(b.vector([3, 4]), b.vector([1, 3]))}');
}twist([3, 4], [1, 2]) = 0.03305785123966942 twist([3, 4], [1, 3]) = 0.1111111111111111
double solidSpread(Float64List a, Float64List b, Float64List c)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
// Three-dimensional quantities need a 3-D metric.
final b3 = FloatRatio3(const Metric3(1, 1, 1));
print('solid spread of the axes: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([0, 0, 1]))}');
print('of a flat triple: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([1, 1, 0]))}');
}solid spread of the axes: 1.0 of a flat triple: 0.0
double dihedralSpread(Float64List common, Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
// Three-dimensional quantities need a 3-D metric.
final b3 = FloatRatio3(const Metric3(1, 1, 1));
print('dihedral spread of the xz and yz planes along z: ${b3.dihedralSpread(b3.vector([0, 0, 1]), b3.vector([1, 0, 0]), b3.vector([1, 1, 0]))}');
}dihedral spread of the xz and yz planes along z: 0.5
double gramDeterminant(List<Float64List> vs)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print(b.gramDeterminant([b.vector([3, 4]), b.vector([1, 2])]));
}4.0
Float64List reflect(Float64List v, Float64List n)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
print('reflect [3, 4] in [1, 2]: ${b.reflect(b.vector([3, 4]), b.vector([1, 2]))}');
}reflect [3, 4] in [1, 2]: [-1.4000000000000004, -4.800000000000001]
Float64List perpendicular(List<Float64List> vs)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final p = b.perpendicular([b.vector([3, 4])]);
print('$p, dot ${b.dot(p, b.vector([3, 4]))}');
}[-4.0, 3.0], dot 0.0
Float64List rotate(List<double> cayley, Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio2(const Metric2(1, 0, 1));
final t = b.div(b.fromInt(1), b.fromInt(2));
print('rotate [3, 4] by t = 1/2: ${b.rotate([t], b.vector([3, 4]))}');
}rotate [3, 4] by t = 1/2: [5.0, 0.0]
FloatRatio3
final class FloatRatio3 implements RatioBackend<double, Float64List>
F64 or f32 ratio geometry in a three-dimensional Metric3, on the float core.
Constructors
FloatRatio3(this.metric, {this.single = false})import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final e = FloatRatio3(const Metric3(1, 1, 1));
print('Q(1, 2, 2) = ${e.quadrance(e.vector([1, 2, 2]))}');
}Q(1, 2, 2) = 9.0
Properties
final Metric3 metricfinal bool singleString get nameint get dimensionbool get exactMethods
double fromInt(int n)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('fromInt(7) = ${b.fromInt(7)}');
}fromInt(7) = 7.0
double add(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 add 1/6 = ${b.add(x, y)}');
}1/3 add 1/6 = 0.5
double sub(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 sub 1/6 = ${b.sub(x, y)}');
}1/3 sub 1/6 = 0.16666666666666666
double mul(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 mul 1/6 = ${b.mul(x, y)}');
}1/3 mul 1/6 = 0.05555555555555555
double div(double a, double b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
print('1/3 div 1/6 = ${b.div(x, y)}');
}1/3 div 1/6 = 2.0
bool isZero(double a, double scale)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}1/3 − 2/6 is zero: true
double? toDouble(double a)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}0.6666666666666666
Float64List vector(List<int> c)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.vector([1, 2, 2]));
}[1.0, 2.0, 2.0]
Float64List vadd(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.vadd(b.vector([1, 2, 2]), b.vector([2, 1, -2])));
}[3.0, 3.0, 0.0]
Float64List vsub(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.vsub(b.vector([1, 2, 2]), b.vector([2, 1, -2])));
}[-1.0, 1.0, 4.0]
Float64List vscale(double k, Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([1, 2, 2])));
}[0.5, 1.0, 1.0]
bool same(Float64List a, Float64List b, double scale)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.same(b.vscale(b.fromInt(2), b.vector([0, 0, 1])), b.vadd(b.vector([0, 0, 1]), b.vector([0, 0, 1])), b.size(b.vector([0, 0, 1]))));
}true
double size(Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('size of [1, 2, 2]: ${b.size(b.vector([1, 2, 2]))}');
}size of [1, 2, 2]: 9.0
double dot(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('dot([1, 2, 2], [0, 0, 1]) = ${b.dot(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
print('dot([1, 2, 2], [2, 1, -2]) = ${b.dot(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}dot([1, 2, 2], [0, 0, 1]) = 2.0 dot([1, 2, 2], [2, 1, -2]) = 0.0
double quadrance(Float64List a)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('Q([1, 2, 2]) = ${b.quadrance(b.vector([1, 2, 2]))}');
}Q([1, 2, 2]) = 9.0
double spread(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('spread([1, 2, 2], [0, 0, 1]) = ${b.spread(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
print('spread([1, 2, 2], [2, 1, -2]) = ${b.spread(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}spread([1, 2, 2], [0, 0, 1]) = 0.5555555555555556 spread([1, 2, 2], [2, 1, -2]) = 1.0
double cross(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('cross([1, 2, 2], [0, 0, 1]) = ${b.cross(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
print('cross([1, 2, 2], [2, 1, -2]) = ${b.cross(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}cross([1, 2, 2], [0, 0, 1]) = 0.4444444444444444 cross([1, 2, 2], [2, 1, -2]) = 0.0
double twist(Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('twist([1, 2, 2], [0, 0, 1]) = ${b.twist(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
print('twist([1, 2, 2], [2, 1, -2]) = ${b.twist(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}twist([1, 2, 2], [0, 0, 1]) = 1.25 twist([1, 2, 2], [2, 1, -2]) = Infinity
double solidSpread(Float64List a, Float64List b, Float64List c)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.solidSpread(b.vector([1, 0, 0]), b.vector([0, 1, 0]), b.vector([1, 1, 1])));
}0.3333333333333333
double dihedralSpread(Float64List c, Float64List a, Float64List b)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.dihedralSpread(b.vector([0, 0, 1]), b.vector([1, 0, 0]), b.vector([1, 1, 0])));
}0.5
double gramDeterminant(List<Float64List> vs)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print(b.gramDeterminant([b.vector([1, 2, 2]), b.vector([0, 0, 1]), b.vector([2, 1, -2])]));
}9.0
Float64List reflect(Float64List v, Float64List n)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('reflect [1, 2, 2] in [0, 0, 1]: ${b.reflect(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
}reflect [1, 2, 2] in [0, 0, 1]: [1.0, 2.0, -2.0]
Float64List perpendicular(List<Float64List> vs)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
final p = b.perpendicular([b.vector([1, 2, 2]), b.vector([0, 0, 1])]);
print('$p, dots ${b.dot(p, b.vector([1, 2, 2]))}, ${b.dot(p, b.vector([0, 0, 1]))}');
}[2.0, -1.0, 0.0], dots 0.0, 0.0
Float64List rotate(List<double> g, Float64List v)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final b = FloatRatio3(const Metric3(1, 1, 1));
print('rotate x by g = (0, 0, 1): ${b.rotate([b.fromInt(0), b.fromInt(0), b.fromInt(1)], b.vector([1, 0, 0]))}');
}rotate x by g = (0, 0, 1): [0.0, 1.0, 0.0]
Frac
final class Frac
An exact rational held as two int64s, reduced. Arithmetic throws RatioOverflow instead of wrapping.
Constructors
factory Frac(int numerator, [int denominator = 1])import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Frac(6, 8));
print(Frac(3, -9));
print(Frac(5));
}3/4 -1/3 5
Static
static const zero = Frac._(0, 1)static const one = Frac._(1, 1)Properties
final int numeratorfinal int denominatorbool get isZeroint get hashCodeMethods
double toDouble()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print(a.toDouble());
}0.3333333333333333
String toString()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print('$a and $b');
}1/3 and -1/6
Operators
Frac operator +(Frac o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print('a + b = ${a + b}');
}a + b = 1/6
Frac operator -(Frac o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print('a - b = ${a - b}');
}a - b = 1/2
Frac operator *(Frac o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print('a * b = ${a * b}');
}a * b = -1/18
Frac operator /(Frac o)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print('a / b = ${a / b}');
}a / b = -2
Frac operator -()import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
final a = Frac(1, 3), b = Frac(-1, 6);
print(-a);
}-1/3
bool operator ==(Object other)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
print(Frac(2, 4) == Frac(1, 2));
print(Frac(1, 3) == Frac(1, 4));
}true false
RatioOverflow
final class RatioOverflow extends StateError
An exact int64 result left the 62-bit range. Use the \mathbb{Q} backend (FieldRatio over BigInt rationals) instead.
Constructors
RatioOverflow(String what)import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);
void main() {
try {
var x = Frac(1, 3);
for (var i = 0; i < 60; i++) {
x = x * Frac(7, 3) + Frac(1, 5);
}
print(x);
} on RatioOverflow catch (e) {
print('caught: ${e.message}');
} on StateError catch (e) {
print('caught: ${e.message}');
}
print(RatioOverflow('demo').message);
}caught: int64 ratio overflow: 4049457491068014344 × 7 int64 ratio overflow: demo