From 9dc809d3e4800324c9cfca934ba3bf3c50a03b35 Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Thu, 30 May 2024 14:00:19 -0400 Subject: [PATCH 1/3] Validate array length when converting to `Matrix3`. --- src/lime/math/Matrix3.hx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lime/math/Matrix3.hx b/src/lime/math/Matrix3.hx index b9e42356a..4145b799d 100644 --- a/src/lime/math/Matrix3.hx +++ b/src/lime/math/Matrix3.hx @@ -22,7 +22,7 @@ import lime.utils.Float32Array; @:fileXml('tags="haxe,release"') @:noDebug #end -abstract Matrix3(Float32Array) from Float32Array to Float32Array +abstract Matrix3(Float32Array) to Float32Array { /** The matrix a component, used in scaling and skewing (default is 1) @@ -335,6 +335,16 @@ abstract Matrix3(Float32Array) from Float32Array to Float32Array return new Matrix3(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); } + @:from private static inline function fromFloat32Array(array:Float32Array):Matrix3 + { + if (array.length != 9) + { + throw "Expected array of length 9, got " + array.length; + } + + return cast array; + } + /** Resets the matrix to default identity values **/ From 93ad84faeb4192c49143bb3e04fc445547372fc0 Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Thu, 30 May 2024 18:41:16 -0400 Subject: [PATCH 2/3] Remove unused code. These appear to have been copied from elsewhere and never used. --- src/lime/math/Matrix3.hx | 42 ---------------------------------------- 1 file changed, 42 deletions(-) diff --git a/src/lime/math/Matrix3.hx b/src/lime/math/Matrix3.hx index 4145b799d..ba3dc2efd 100644 --- a/src/lime/math/Matrix3.hx +++ b/src/lime/math/Matrix3.hx @@ -54,8 +54,6 @@ abstract Matrix3(Float32Array) to Float32Array **/ public var ty(get, set):Float; - private static var __identity = new Matrix3(); - /** Creates a new `Matrix` instance @param a (Optional) An initial a component value (default is 1) @@ -389,12 +387,6 @@ abstract Matrix3(Float32Array) to Float32Array return this; } - // public inline function mult (m:Matrix3) { - // var result = clone (); - // result.concat (m); - // return result; - // } - /** Applies rotation to the current matrix @param theta A rotation value in degrees @@ -482,40 +474,6 @@ abstract Matrix3(Float32Array) to Float32Array set_ty(ty); } - @:dox(hide) @:noCompletion public inline function to3DString(roundPixels:Bool = false):String - { - // identity matrix - // [a,b,tx,0], - // [c,d,ty,0], - // [0,0,1, 0], - // [0,0,0, 1] - // - // matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) - - if (roundPixels) - { - return "matrix3d(" - + a - + ", " - + b - + ", " - + "0, 0, " - + c - + ", " - + d - + ", " - + "0, 0, 0, 0, 1, 0, " - + Std.int(tx) - + ", " - + Std.int(ty) - + ", 0, 1)"; - } - else - { - return "matrix3d(" + a + ", " + b + ", " + "0, 0, " + c + ", " + d + ", " + "0, 0, 0, 0, 1, 0, " + tx + ", " + ty + ", 0, 1)"; - } - } - @:dox(hide) @:noCompletion @:to public inline function toCairoMatrix3():CairoMatrix3 { return new CairoMatrix3(a, b, c, d, tx, ty); From 084324a7ed2cb801d20d9ab394e28c5471113aaa Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Thu, 30 May 2024 18:42:08 -0400 Subject: [PATCH 3/3] Consistently refer to `Matrix3` values in column-major order. --- src/lime/math/Matrix3.hx | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/lime/math/Matrix3.hx b/src/lime/math/Matrix3.hx index ba3dc2efd..330cad97c 100644 --- a/src/lime/math/Matrix3.hx +++ b/src/lime/math/Matrix3.hx @@ -1,5 +1,7 @@ package lime.math; +import lime.utils.Float32Array; + /** `Matrix3` is a 3x3 transformation matrix particularly useful for two-dimensional transformation. It can be used for rotation, scale @@ -13,8 +15,9 @@ package lime.math; [ b, d, ty ] [ 0, 0, 1 ] ``` + + Values are stored in column-major order for GLSL compatibility. **/ -import lime.utils.Float32Array; #if hl @:keep #end @@ -65,10 +68,11 @@ abstract Matrix3(Float32Array) to Float32Array **/ public function new(a:Float = 1, b:Float = 0, c:Float = 0, d:Float = 1, tx:Float = 0, ty:Float = 0) { + // Column-major order means adjacent values form a column, not a row. this = new Float32Array([ - a, c, 0, - b, d, 0, - tx, ty, 1 + a, b, 0, // column 0 + c, d, 0, // column 1 + tx, ty, 1 // column 2 ]); } @@ -396,9 +400,9 @@ abstract Matrix3(Float32Array) to Float32Array /* Rotate object "after" other transforms - [ a b 0 ][ ma mb 0 ] - [ c d 0 ][ mc md 0 ] - [ tx ty 1 ][ mtx mty 1 ] + [ a c tx ][ ma mc mtx ] + [ b d ty ][ mb md mty ] + [ 0 0 1 ][ 0 0 1 ] ma = md = cos mb = sin @@ -434,9 +438,9 @@ abstract Matrix3(Float32Array) to Float32Array Scale object "after" other transforms - [ a b 0 ][ sx 0 0 ] - [ c d 0 ][ 0 sy 0 ] - [ tx ty 1 ][ 0 0 1 ] + [ a c tx ][ sx 0 0 ] + [ b d ty ][ 0 sy 0 ] + [ 0 0 1 ][ 0 0 1 ] */ a *= sx; @@ -481,7 +485,7 @@ abstract Matrix3(Float32Array) to Float32Array @:dox(hide) public inline function toString():String { - return "matrix(" + a + ", " + b + ", " + c + ", " + d + ", " + tx + ", " + ty + ")"; + return 'matrix($a, $b, $c, $d, $tx, $ty)'; } /** @@ -564,20 +568,20 @@ abstract Matrix3(Float32Array) to Float32Array inline function get_b():Float { - return this[3]; + return this[1]; } inline function set_b(value: Float):Float { - return this[3] = value; + return this[1] = value; } inline function get_c():Float { - return this[1]; + return this[3]; } inline function set_c(value: Float):Float { - return this[1] = value; + return this[3] = value; } inline function get_d():Float