You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2020/06/13 08:21:06 UTC
svn commit: r1878791 - in /pdfbox/trunk/pdfbox/src:
main/java/org/apache/pdfbox/util/Matrix.java
test/java/org/apache/pdfbox/util/MatrixTest.java
Author: lehmi
Date: Sat Jun 13 08:21:05 2020
New Revision: 1878791
URL: http://svn.apache.org/viewvc?rev=1878791&view=rev
Log:
PDFBOX-4877: improve Matrix implementation based on a proposal by Alfred Faltiska
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/MatrixTest.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java?rev=1878791&r1=1878790&r2=1878791&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java Sat Jun 13 08:21:05 2020
@@ -226,7 +226,7 @@ public final class Matrix implements Clo
*/
public void concatenate(Matrix matrix)
{
- matrix.multiply(this, this);
+ single = checkFloatValues(multiplyArrays(matrix.single, single));
}
/**
@@ -280,44 +280,22 @@ public final class Matrix implements Clo
*/
public Matrix multiply(Matrix other)
{
- return multiply(other, new Matrix());
+ return new Matrix(checkFloatValues(multiplyArrays(single, other.single)));
}
- /**
- * This method multiplies this Matrix with the specified other Matrix, storing the product in the specified result
- * Matrix. It is allowed to have (other == this) or (result == this) or indeed (other == result).</br>
- * See {@link #multiply(Matrix)} if you need a version with a single operator.
- *
- * @param other the second operand Matrix in the multiplication; required
- * @param result the Matrix instance into which the result should be stored. If result is null, a new Matrix
- * instance is created.
- * @return the result.
- */
- public Matrix multiply( Matrix other, Matrix result )
+ private float[] checkFloatValues(float[] values)
{
- float[] c = result != null && result != other && result != this ? result.single
- : new float[SIZE];
-
- multiplyArrays(single, other.single, c);
-
- if (!Float.isFinite(c[0]) || !Float.isFinite(c[1]) || !Float.isFinite(c[2])
- || !Float.isFinite(c[3]) || !Float.isFinite(c[4]) || !Float.isFinite(c[5])
- || !Float.isFinite(c[6]) || !Float.isFinite(c[7]) || !Float.isFinite(c[8]))
+ if (!Float.isFinite(values[0]) || !Float.isFinite(values[1]) || !Float.isFinite(values[2])
+ || !Float.isFinite(values[3]) || !Float.isFinite(values[4]) || !Float.isFinite(values[5])
+ || !Float.isFinite(values[6]) || !Float.isFinite(values[7]) || !Float.isFinite(values[8]))
throw new IllegalArgumentException("Multiplying two matrices produces illegal values");
-
- if (result == null)
- {
- return new Matrix(c);
- }
- else
- {
- result.single = c;
- return result;
- }
+ return values;
}
- private void multiplyArrays(float[] a, float[] b, float[] c)
+
+ private float[] multiplyArrays(float[] a, float[] b)
{
+ float[] c = new float[SIZE];
c[0] = a[0] * b[0] + a[1] * b[3] + a[2] * b[6];
c[1] = a[0] * b[1] + a[1] * b[4] + a[2] * b[7];
c[2] = a[0] * b[2] + a[1] * b[5] + a[2] * b[8];
@@ -327,6 +305,7 @@ public final class Matrix implements Clo
c[6] = a[6] * b[0] + a[7] * b[3] + a[8] * b[6];
c[7] = a[6] * b[1] + a[7] * b[4] + a[8] * b[7];
c[8] = a[6] * b[2] + a[7] * b[5] + a[8] * b[8];
+ return c;
}
/**
* Transforms the given point by this matrix.
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/MatrixTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/MatrixTest.java?rev=1878791&r1=1878790&r2=1878791&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/MatrixTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/MatrixTest.java Sat Jun 13 08:21:05 2020
@@ -133,31 +133,9 @@ public class MatrixTest
assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m2);
assertMatrixValuesEqualTo(new float[] { 5, 8, 11, 8, 14, 20, 11, 20, 29 }, product);
- // Multiply two matrices together with the result being written to a third matrix
- // (Any existing values there will be overwritten).
- Matrix resultMatrix = new Matrix();
-
- Matrix retVal = m1.multiply(m2, resultMatrix);
- assertSame(retVal, resultMatrix);
- // Operand 1 should not have changed
- assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m1);
- // Operand 2 should not have changed
- assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m2);
- assertMatrixValuesEqualTo(new float[] { 5, 8, 11, 8, 14, 20, 11, 20, 29 }, resultMatrix);
-
- // Multiply two matrices together with the result being written into the other matrix
- retVal = m1.multiply(m2, m2);
- assertSame(retVal, m2);
+ Matrix retVal = m1.multiply(m2);
// Operand 1 should not have changed
assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m1);
- assertMatrixValuesEqualTo(new float[] { 5, 8, 11, 8, 14, 20, 11, 20, 29 }, retVal);
-
- // Multiply two matrices together with the result being written into 'this' matrix
- m1 = testMatrix.clone();
- m2 = testMatrix.clone();
-
- retVal = m1.multiply(m2, m1);
- assertSame(retVal, m1);
// Operand 2 should not have changed
assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m2);
assertMatrixValuesEqualTo(new float[] { 5, 8, 11, 8, 14, 20, 11, 20, 29 }, retVal);
@@ -165,8 +143,9 @@ public class MatrixTest
// Multiply the same matrix with itself with the result being written into 'this' matrix
m1 = testMatrix.clone();
- retVal = m1.multiply(m1, m1);
- assertSame(retVal, m1);
+ retVal = m1.multiply(m1);
+ // Operand 1 should not have changed
+ assertMatrixValuesEqualTo(new float[] { 0, 1, 2, 1, 2, 3, 2, 3, 4 }, m1);
assertMatrixValuesEqualTo(new float[] { 5, 8, 11, 8, 14, 20, 11, 20, 29 }, retVal);
}
@@ -175,7 +154,7 @@ public class MatrixTest
{
Matrix m = new Matrix();
m.setValue(0, 0, Float.MAX_VALUE);
- m.multiply(m, m);
+ m.multiply(m);
}
@Test(expected = IllegalArgumentException.class)
@@ -183,7 +162,7 @@ public class MatrixTest
{
Matrix m = new Matrix();
m.setValue(0, 0, Float.NaN);
- m.multiply(m, m);
+ m.multiply(m);
}
@Test(expected = IllegalArgumentException.class)
@@ -191,7 +170,7 @@ public class MatrixTest
{
Matrix m = new Matrix();
m.setValue(0, 0, Float.POSITIVE_INFINITY);
- m.multiply(m, m);
+ m.multiply(m);
}
@Test(expected = IllegalArgumentException.class)
@@ -199,7 +178,7 @@ public class MatrixTest
{
Matrix m = new Matrix();
m.setValue(0, 0, Float.NEGATIVE_INFINITY);
- m.multiply(m, m);
+ m.multiply(m);
}
/**