Commit 08a0db18 authored by hs1217.lee's avatar hs1217.lee Committed by Commit bot

Implement inverse & invertSelf function in DOMMatrix & DOMMatrixReadOnly.

this function return inverse matrix.
if the current matrix is not invertible set all attributes to NaN and set is2D to false.
refer to detail spec([1],[2])

[1] = https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-inverse
[2] = https://drafts.fxtf.org/geometry/#dom-dommatrix-invertself

 BUG=388780

Review-Url: https://codereview.chromium.org/2283173003
Cr-Commit-Position: refs/heads/master@{#416348}
parent 8ded23e7
<!DOCTYPE HTML>
<html>
<head>
<title>Geometry Interfaces: DOMMatrix invert</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
// TODO(hs1217.lee): create the DOMMatrix directly when the sequence constructor is supported.
function initMatrix(values) {
return new DOMMatrixReadOnly(values);
}
test(function() {
var matrix2d = new DOMMatrix(initMatrix([1, 2, 3, 4, 5, 6]));
matrix2d.invertSelf();
assert_true(matrix2d.is2D);
assert_equals(matrix2d.a, -2);
assert_equals(matrix2d.b, 1);
assert_equals(matrix2d.c, 1.5);
assert_equals(matrix2d.d, -0.5);
assert_equals(matrix2d.e, 1);
assert_equals(matrix2d.f, -2);
}, "DOMMatrix invertSelf() - invertible - 2D matrix");
test(function() {
var matrix2d = new DOMMatrix(initMatrix([1, 1, 1, 1, 1, 1]));
matrix2d.invertSelf();
assert_false(matrix2d.is2D);
assert_equals(matrix2d.a, NaN);
assert_equals(matrix2d.b, NaN);
assert_equals(matrix2d.c, NaN);
assert_equals(matrix2d.d, NaN);
assert_equals(matrix2d.e, NaN);
assert_equals(matrix2d.f, NaN);
}, "DOMMatrix invertSelf() - uninvertible - 2D matrix");
test(function() {
var matrix3d = new DOMMatrix(initMatrix([10, 20, 30, 40, 40, 40, 30, 20, 10, 20, 40, 30, 20, 40, 50, 100]));
matrix3d.invertSelf();
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, -1.6);
assert_equals(matrix3d.m12, 0.05);
assert_equals(matrix3d.m13, 0.6);
assert_equals(matrix3d.m14, 0.45);
assert_equals(matrix3d.m21, 2.05);
assert_equals(matrix3d.m22, -0.025);
assert_equals(matrix3d.m23, -0.8);
assert_equals(matrix3d.m24, -0.575);
assert_equals(matrix3d.m31, -0.4);
assert_equals(matrix3d.m32, 0);
assert_equals(matrix3d.m33, 0.2);
assert_equals(matrix3d.m34, 0.1);
assert_equals(matrix3d.m41, -0.3);
assert_equals(matrix3d.m42, -0);
assert_equals(matrix3d.m43, 0.1);
assert_equals(matrix3d.m44, 0.1);
}, "DOMMatrix invertSelf() - 3D matrix");
test(function() {
var matrix3d = new DOMMatrix(initMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]));
matrix3d.invertSelf();
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, NaN);
assert_equals(matrix3d.m12, NaN);
assert_equals(matrix3d.m13, NaN);
assert_equals(matrix3d.m14, NaN);
assert_equals(matrix3d.m21, NaN);
assert_equals(matrix3d.m22, NaN);
assert_equals(matrix3d.m23, NaN);
assert_equals(matrix3d.m24, NaN);
assert_equals(matrix3d.m31, NaN);
assert_equals(matrix3d.m32, NaN);
assert_equals(matrix3d.m33, NaN);
assert_equals(matrix3d.m34, NaN);
assert_equals(matrix3d.m41, NaN);
assert_equals(matrix3d.m42, NaN);
assert_equals(matrix3d.m43, NaN);
assert_equals(matrix3d.m44, NaN);
}, "DOMMatrix invertSelf() - uninvertible - 3D matrix");
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Geometry Interfaces: DOMMatrixReadOnly inverse</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
test(function() {
var matrix2d = new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6]);
var inverse = matrix2d.inverse();
assert_true(inverse.is2D);
assert_equals(inverse.a, -2);
assert_equals(inverse.b, 1);
assert_equals(inverse.c, 1.5);
assert_equals(inverse.d, -0.5);
assert_equals(inverse.e, 1);
assert_equals(inverse.f, -2);
}, "DOMMatrix inverse() - invertible - 2D matrix");
test(function() {
var matrix2d = new DOMMatrixReadOnly([1, 1, 1, 1, 1, 1]);
var inverse =matrix2d.inverse();
assert_false(inverse.is2D);
assert_equals(inverse.a, NaN);
assert_equals(inverse.b, NaN);
assert_equals(inverse.c, NaN);
assert_equals(inverse.d, NaN);
assert_equals(inverse.e, NaN);
assert_equals(inverse.f, NaN);
}, "DOMMatrix inverse() - uninvertible - 2D matrix");
test(function() {
var matrix3d = new DOMMatrixReadOnly([10, 20, 30, 40, 40, 40, 30, 20, 10, 20, 40, 30, 20, 40, 50, 100]);
var inverse = matrix3d.inverse();
assert_false(inverse.is2D);
assert_equals(inverse.m11, -1.6);
assert_equals(inverse.m12, 0.05);
assert_equals(inverse.m13, 0.6);
assert_equals(inverse.m14, 0.45);
assert_equals(inverse.m21, 2.05);
assert_equals(inverse.m22, -0.025);
assert_equals(inverse.m23, -0.8);
assert_equals(inverse.m24, -0.575);
assert_equals(inverse.m31, -0.4);
assert_equals(inverse.m32, 0);
assert_equals(inverse.m33, 0.2);
assert_equals(inverse.m34, 0.1);
assert_equals(inverse.m41, -0.3);
assert_equals(inverse.m42, -0);
assert_equals(inverse.m43, 0.1);
assert_equals(inverse.m44, 0.1);
}, "DOMMatrix inverse() - invertible - 3D matrix");
test(function() {
var matrix3d = new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
var inverse = matrix3d.inverse();
assert_false(inverse.is2D);
assert_equals(inverse.m11, NaN);
assert_equals(inverse.m12, NaN);
assert_equals(inverse.m13, NaN);
assert_equals(inverse.m14, NaN);
assert_equals(inverse.m21, NaN);
assert_equals(inverse.m22, NaN);
assert_equals(inverse.m23, NaN);
assert_equals(inverse.m24, NaN);
assert_equals(inverse.m31, NaN);
assert_equals(inverse.m32, NaN);
assert_equals(inverse.m33, NaN);
assert_equals(inverse.m34, NaN);
assert_equals(inverse.m41, NaN);
assert_equals(inverse.m42, NaN);
assert_equals(inverse.m43, NaN);
assert_equals(inverse.m44, NaN);
}, "DOMMatrix inverse() - uninvertible - 3D matrix");
</script>
</body>
</html>
......@@ -7,8 +7,14 @@
</head>
<body>
<script>
// TODO(hs1217.lee): create the DOMMatrix directly when the sequence constructor is supported.
function initMatrix(values) {
return new DOMMatrixReadOnly(values);
}
test(function() {
var matrix2d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 3.1, 2, 1]));
var matrix2d = new DOMMatrix(initMatrix([1, 2, 3, 3.1, 2, 1]));
matrix2d.skewXSelf();
assert_true(matrix2d.is2D);
assert_equals(matrix2d.a, 1);
......@@ -20,7 +26,7 @@ test(function() {
}, "DOMMatrix skewX() - 2D matrix");
test(function() {
var matrix2d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 3.1, 2, 1]));
var matrix2d = new DOMMatrix(initMatrix([1, 2, 3, 3.1, 2, 1]));
matrix2d.skewXSelf(45);
assert_true(matrix2d.is2D);
assert_equals(matrix2d.a, 1);
......@@ -32,7 +38,7 @@ test(function() {
}, "DOMMatrix skewX(45) - 2D matrix");
test(function() {
var matrix3d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
var matrix3d = new DOMMatrix(initMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
matrix3d.skewXSelf();
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, 1);
......@@ -54,7 +60,7 @@ test(function() {
}, "DOMMatrix skewX() - 3D matrix");
test(function() {
var matrix3d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
var matrix3d = new DOMMatrix(initMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
matrix3d.skewXSelf(45);
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, 1);
......@@ -76,7 +82,7 @@ test(function() {
}, "DOMMatrix skewX(45) - 3D matrix");
test(function() {
var matrix2d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 3.1, 2, 1]));
var matrix2d = new DOMMatrix(initMatrix([1, 2, 3, 3.1, 2, 1]));
matrix2d.skewYSelf();
assert_true(matrix2d.is2D);
assert_equals(matrix2d.a, 1);
......@@ -88,7 +94,7 @@ test(function() {
}, "DOMMatrix skewY() - 2D matrix");
test(function() {
var matrix2d = new DOMMatrix(new DOMMatrixReadOnly([2, 2, 2, 2, 2, 2]));
var matrix2d = new DOMMatrix(initMatrix([2, 2, 2, 2, 2, 2]));
matrix2d.skewYSelf(45);
assert_true(matrix2d.is2D);
assert_equals(matrix2d.a, 4);
......@@ -100,7 +106,7 @@ test(function() {
}, "DOMMatrix skewY(45) - 2D matrix");
test(function() {
var matrix3d = new DOMMatrix(new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
var matrix3d = new DOMMatrix(initMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.1, 11, 12, 13, 14, 15, 16.6]));
matrix3d.skewYSelf();
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, 1);
......@@ -122,7 +128,7 @@ test(function() {
}, "DOMMatrix skewY() - 3D matrix");
test(function() {
var matrix3d = new DOMMatrix(new DOMMatrixReadOnly([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]));
var matrix3d = new DOMMatrix(initMatrix([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]));
matrix3d.skewYSelf(45);
assert_false(matrix3d.is2D);
assert_equals(matrix3d.m11, 2);
......
......@@ -993,6 +993,7 @@ interface DOMMatrix : DOMMatrixReadOnly
getter m43
getter m44
method constructor
method invertSelf
method multiplySelf
method preMultiplySelf
method scale3dSelf
......@@ -1052,6 +1053,7 @@ interface DOMMatrixReadOnly
method constructor
method flipX
method flipY
method inverse
method multiply
method scale
method scale3d
......
......@@ -118,4 +118,30 @@ DOMMatrix* DOMMatrix::skewYSelf(double sy)
return this;
}
DOMMatrix* DOMMatrix::invertSelf()
{
if (m_matrix->isInvertible()) {
m_matrix = TransformationMatrix::create(m_matrix->inverse());
} else {
setM11(NAN);
setM12(NAN);
setM13(NAN);
setM14(NAN);
setM21(NAN);
setM22(NAN);
setM23(NAN);
setM24(NAN);
setM31(NAN);
setM32(NAN);
setM33(NAN);
setM34(NAN);
setM41(NAN);
setM42(NAN);
setM43(NAN);
setM44(NAN);
setIs2D(false);
}
return this;
}
} // namespace blink
......@@ -49,6 +49,7 @@ public:
double ox = 0, double oy = 0, double oz = 0);
DOMMatrix* skewXSelf(double sx = 0);
DOMMatrix* skewYSelf(double sy = 0);
DOMMatrix* invertSelf();
private:
DOMMatrix(const TransformationMatrix&, bool is2D = true);
......
......@@ -58,4 +58,5 @@
optional unrestricted double originZ = 0);
DOMMatrix skewXSelf(optional unrestricted double sx = 0);
DOMMatrix skewYSelf(optional unrestricted double sy = 0);
DOMMatrix invertSelf();
};
......@@ -104,6 +104,11 @@ DOMMatrix* DOMMatrixReadOnly::flipY()
return flipY;
}
DOMMatrix* DOMMatrixReadOnly::inverse()
{
return DOMMatrix::create(this)->invertSelf();
}
DOMFloat32Array* DOMMatrixReadOnly::toFloat32Array() const
{
float array[] = {
......
......@@ -59,6 +59,7 @@ public:
DOMMatrix* skewY(double sy);
DOMMatrix* flipX();
DOMMatrix* flipY();
DOMMatrix* inverse();
DOMFloat32Array* toFloat32Array() const;
DOMFloat64Array* toFloat64Array() const;
......
......@@ -61,6 +61,7 @@
DOMMatrix skewY(optional unrestricted double sy = 0);
DOMMatrix flipX();
DOMMatrix flipY();
DOMMatrix inverse();
Float32Array toFloat32Array();
Float64Array toFloat64Array();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment