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 2a377db9990e9e2f77f2522e8e5a632a01fcc8b4 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:48:21 2026 +0100 feat(Geometry): add MeshPrimitive.computeTangents --- .../apache/sis/geometries/mesh/MeshPrimitive.java | 104 +++++++++++++++++++++ .../apache/sis/geometries/mesh/PrimitiveTest.java | 43 +++++++++ 2 files changed, 147 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java index 58da7a4a65..983eab1035 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java @@ -299,6 +299,24 @@ public sealed interface MeshPrimitive extends Geometry */ void computeSmoothNormals(); + /** + * Sets the tangent of each vertex from the normals and the first texture coordinates, as needed by normal mapping. + * The tangent has 4 components: the unit tangent vector in the direction of increasing <var>u</var> texture + * coordinate, then the handedness, 1 or -1, giving the direction of the bitangent as + * <var>w</var>⋅(<var>normal</var> × <var>tangent</var>). + * + * <ul> + * <li>The tangents of the triangles sharing a vertex are averaged, then made perpendicular to the normal.</li> + * <li>Triangles whose texture coordinates are aligned contribute nothing.</li> + * <li>A vertex without contribution gets any unit vector perpendicular to its normal.</li> + * </ul> + * + * @throws IllegalStateException if this primitive has no normals or no texture coordinates. + * + * @see "glTF 2.0 specification, TANGENT vertex attribute" + */ + void computeTangents(); + /** * Removes any duplicated vertice. @@ -908,6 +926,92 @@ public sealed interface MeshPrimitive extends Geometry setNormals(normals); } + @Override + public void computeTangents() { + final Array positions = getAttribute(ATT_POSITION); + final Array normals = getNormals(); + final Array texCoords = getTexCoords(0); + if (normals == null || texCoords == null) { + throw new IllegalStateException("The tangents are computed from the normals and the first texture coordinates."); + } + final long size = positions.getLength(); + final Array tangents = NDArrays.of(SampleSystem.ofSize(3), DataType.DOUBLE, size); + final Array bitangents = NDArrays.of(SampleSystem.ofSize(3), DataType.DOUBLE, size); + new MeshPrimitiveVisitor(this) { + @Override + protected void visit(Vertex vertex) { + } + + @Override + protected void visit(Triangle candidate) { + final DataPoints points = candidate.getExteriorRing().getDataPoints(); + final long[] indices = new long[3]; + for (int k = 0; k < 3; k++) { + indices[k] = ((Vertex) points.getPoint(k)).getIndex(); + } + // Edges of the triangle and their differences of texture coordinates. + final Vector<?> origin = positions.get(indices[0]).copy(DataType.DOUBLE); + final Vector<?> e1 = positions.get(indices[1]).copy(DataType.DOUBLE).subtract(origin); + final Vector<?> e2 = positions.get(indices[2]).copy(DataType.DOUBLE).subtract(origin); + final Vector<?> uv0 = texCoords.get(indices[0]); + final Vector<?> uv1 = texCoords.get(indices[1]); + final Vector<?> uv2 = texCoords.get(indices[2]); + final double du1 = uv1.get(0) - uv0.get(0), dv1 = uv1.get(1) - uv0.get(1); + final double du2 = uv2.get(0) - uv0.get(0), dv2 = uv2.get(1) - uv0.get(1); + final double det = du1 * dv2 - du2 * dv1; + if (det == 0 || !Double.isFinite(det)) { + return; + } + final Vector<?> tangent = e1.combine(dv2 / det, e2, -dv1 / det); + final Vector<?> bitangent = e2.combine(du1 / det, e1, -du2 / det); + for (final long index : indices) { + tangents.set(index, tangents.get(index).add(tangent)); + bitangents.set(index, bitangents.get(index).add(bitangent)); + } + } + + @Override + protected void visit(LineString candidate) { + } + + @Override + protected void visit(Point candidate) { + } + }.visit(); + + final Array result = NDArrays.of(SampleSystem.ofSize(4), DataType.FLOAT, size); + for (long i = 0; i < size; i++) { + final Vector<?> normal = normals.get(i).copy(DataType.DOUBLE).normalize(); + if (!normal.isFinite()) { + result.set(i, Vector.of(1f, 0f, 0f, 1f)); // No normal, any tangent is as good. + continue; + } + // Gram-Schmidt: remove the part of the tangent along the normal. + Vector<?> tangent = tangents.get(i).perpendicular(normal); + if (!(tangent.length() > 0)) { + tangent = anyPerpendicular(normal); + } + tangent = tangent.normalize(); + final double handedness = (normal.cross(tangent).dot(bitangents.get(i)) < 0) ? -1 : 1; + result.set(i, tangent.extend(handedness)); + } + setTangents(result); + } + + /** + * Returns a vector perpendicular to the given unit normal: the coordinate axis the most perpendicular + * to the normal, without its component along the normal. + */ + private static Vector<?> anyPerpendicular(final Vector<?> normal) { + int axis = 0; + for (int i = 1; i < 3; i++) { + if (Math.abs(normal.get(i)) < Math.abs(normal.get(axis))) axis = i; + } + final double[] unit = new double[3]; + unit[axis] = 1; + return Vector.of(unit).perpendicular(normal); + } + private static boolean fastEquals(Vector t1, Vector t2) { return t1.get(0) == t2.get(0) && t1.get(1) == t2.get(1) && t1.get(2) == t2.get(2); } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/mesh/PrimitiveTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/mesh/PrimitiveTest.java index 81fcb3d960..a695cf72ec 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/mesh/PrimitiveTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/mesh/PrimitiveTest.java @@ -234,4 +234,47 @@ public class PrimitiveTest extends GeometryTest { assertArrayEquals(new int[]{0,1,0, 2,3,0, 4,5,0}, p0.getPositions().toArrayInt()); assertArrayEquals(new int[]{4,5,0, 0,1,0, 2,3,0}, p1.getPositions().toArrayInt()); } + + /** + * Creates a unit square in the plane <var>z</var> = 0 made of two triangles, + * with the normal (0 0 1) and the given texture coordinates. + */ + private static MeshPrimitive square(final double... texCoords) { + final MeshPrimitive primitive = new MeshPrimitive.Triangles(); + primitive.setPositions(NDArrays.of(Geometries.getUndefinedCRS(3), 0,0,0, 1,0,0, 1,1,0, 0,1,0)); + primitive.setNormals(NDArrays.of(3, 0,0,1, 0,0,1, 0,0,1, 0,0,1)); + primitive.setTexCoords(0, NDArrays.of(2, texCoords)); + primitive.setIndex(NDArrays.ofUnsigned(1, 0, 1, 2, 0, 2, 3)); + return primitive; + } + + /** + * Tests {@link MeshPrimitive#computeTangents()}: the tangent follows the increasing <var>u</var> texture coordinate, + * and the handedness is -1 when the texture is mirrored. + */ + @Test + public void testComputeTangents() { + MeshPrimitive primitive = square(0,0, 1,0, 1,1, 0,1); + primitive.computeTangents(); + assertArrayEquals(new float[] {1,0,0,1, 1,0,0,1, 1,0,0,1, 1,0,0,1}, primitive.getTangents().toArrayFloat(), 1E-6f); + + primitive = square(0,0, -1,0, -1,1, 0,1); + primitive.computeTangents(); + assertArrayEquals(new float[] {-1,0,0,-1, -1,0,0,-1, -1,0,0,-1, -1,0,0,-1}, primitive.getTangents().toArrayFloat(), 1E-6f); + + primitive = square(0,0, 0,1, 1,1, 1,0); + primitive.computeTangents(); + assertArrayEquals(new float[] {0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1}, primitive.getTangents().toArrayFloat(), 1E-6f); + } + + /** + * The tangents need texture coordinates. + */ + @Test + public void testComputeTangentsWithoutTexCoords() { + final MeshPrimitive primitive = new MeshPrimitive.Triangles(); + primitive.setPositions(NDArrays.of(Geometries.getUndefinedCRS(3), 0,0,0, 1,0,0, 1,1,0)); + primitive.setNormals(NDArrays.of(3, 0,0,1, 0,0,1, 0,0,1)); + assertThrows(IllegalStateException.class, primitive::computeTangents); + } }
