From 2b86c2ba0074afe2eff0cfeb5cb1b18ca9984834 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 22 Mar 2015 11:17:44 +0100 Subject: Use camera transform matrix Rotation and camera transformation are a single matrix now. Speedup is negligible (~1%, depending on #xforms). --- math.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'math.h') diff --git a/math.h b/math.h index fa49d09..d6e3211 100644 --- a/math.h +++ b/math.h @@ -27,6 +27,7 @@ #define REPLACE_WITH_AMDLIBM #include #undef nearbyint +#undef floor #endif #define clamp(a,min,max) (a > max ? max : (a < min ? min : a)) @@ -55,6 +56,14 @@ inline void translate (const double2 xy, double2 matrix[3]) { matrix[2] = xy; } +/* Create affine scaling matrix + */ +inline void scale (const double2 xy, double2 matrix[3]) { + matrix[0] = (double2) { xy[0], 0.0 }; + matrix[1] = (double2) { 0.0, xy[1] }; + matrix[2] = (double2) { 0.0, 0.0 }; +} + /* Multiply two affine matrices a, b and store the result in c. * * The last row of each matrix is assumed to be 0, 0, 1. @@ -65,6 +74,23 @@ inline void matrixmul (const double2 a[3], const double2 b[3], double2 c[3]) { c[2] = a[0] * b[2][0] + a[1] * b[2][1] + a[2]; } +/* Affine matrix that transforms rect from (x1, y1, x2, y2) into rect to + */ +inline void translate_rect (const double4 from, const double4 to, + double2 matrix[3]) { + const double2 from_edge = (double2) { from[0], from[1] }, + to_edge = (double2) { to[0], to[1] }; + /* first align one of A and B’s edges */ + double2 translate_edge[3]; + translate (to_edge - from_edge, translate_edge); + /* then scale it up or down */ + double2 scale_rect[3]; + scale ((double2) { (to[2] - to[0])/(from[2] - from[0]), + (to[3] - to[1])/(from[3] - from[1])}, scale_rect); + /* the result is scale*translate (i.e. translate first) */ + matrixmul (scale_rect, translate_edge, matrix); +} + /* Create rotation around center. Note that matrix multiplication is * right-associative, thus A*B*C == A*(B*C) */ inline void rotate_center (const double2 center, const double angle, double2 out[3]) { -- cgit v1.2.3