This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 2184ccb8b00615c446d4b29a158023ee95b00d37 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 16:52:15 2026 +0100 fix(Math): fix Matrix3D gimbal lock --- .../main/org/apache/sis/maths/Matrix3D.java | 6 +-- .../test/org/apache/sis/maths/Matrix3DTest.java | 49 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java index 05dcbc4f55..e13f18b294 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java @@ -301,7 +301,7 @@ public class Matrix3D extends AbstractMatrix<Matrix3D> { double angle_y; double angle_z; - if (m20 != -1 && m20 != +1){ + if (m20 > -1 && m20 < 1){ //first possible solution angle_y = -Math.asin(m20); double cosy1 = Math.cos(angle_y); @@ -315,9 +315,9 @@ public class Matrix3D extends AbstractMatrix<Matrix3D> { //angle_z = Math.atan2(mat[1][0]/cosy2, mat[0][0]/cosy2); } else { - // Gimbal lock + // Gimbal lock, including a value which drifted past ±1 by rounding errors angle_z = 0; - if (m20 == -1){ + if (m20 < 0){ angle_y = Math.PI / 2.0; angle_x = angle_z + Math.atan2(m01, m02); } else { diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Matrix3DTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Matrix3DTest.java new file mode 100644 index 0000000000..5d106bb936 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Matrix3DTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.maths; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link Matrix3D}. + * + * @author Johann Sorel (Geomatys) + */ +public class Matrix3DTest { + + /** + * Rotations of ±90° around Y whose value at row 2, column 0 drifted one ulp past ±1, + * as after repeated multiplications, are in gimbal lock and must not give NaN angles. + */ + @Test + public void testToEulerGimbalLockDrift() { + final Matrix3D up = new Matrix3D( + 0, 0, 1, + 0, 1, 0, + -1.0000000000000002, 0, 0); + assertArrayEquals(new double[] {0, 1.5707963267948966, 0}, up.toEuler().toArrayDouble(), 0.0); + + final Matrix3D down = new Matrix3D( + 0, 0, -1, + 0, 1, 0, + 1.0000000000000002, 0, 0); + assertArrayEquals(new double[] {0, -1.5707963267948966, 0}, down.toEuler().toArrayDouble(), 0.0); + } +}
