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 68516ea4b514768dce2594d18c0698d0722f7940 Author: jsorel <[email protected]> AuthorDate: Wed Dec 2 10:06:40 2026 +0100 perf(Math): Vector.copy returns the same vector for the same data type --- .../main/org/apache/sis/maths/Vector.java | 8 +- .../main/org/apache/sis/maths/Vector1D.java | 144 +++++++++++++++++++++ .../main/org/apache/sis/maths/Vector2D.java | 144 +++++++++++++++++++++ .../main/org/apache/sis/maths/Vector3D.java | 144 +++++++++++++++++++++ .../main/org/apache/sis/maths/Vector4D.java | 144 +++++++++++++++++++++ .../main/org/apache/sis/maths/VectorND.java | 45 +++++++ .../org/apache/sis/maths/AbstractVectorTest.java | 7 +- .../test/org/apache/sis/maths/Vector3DTest.java | 26 ++++ 8 files changed, 658 insertions(+), 4 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java index 6aa44fca95..9235e2c86f 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java @@ -514,11 +514,15 @@ public sealed interface Vector<T extends Vector<T>> permits AbstractVector { } /** - * Create a copy of this tuple, with values stored in the given data type. + * Returns this tuple with values stored in the given data type. * The copy has the same sample system as this tuple. * + * <ul> + * <li>Vectors being immutable, this tuple is returned if it already has the given data type.</li> + * </ul> + * * @param dataType data type of the copy values. - * @return tuple copy. + * @return this tuple if it has the given data type, otherwise a copy with values of that type. */ default Vector<?> copy(DataType dataType) { return Vectors.create(toArrayDouble(), dataType); diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java index c868de40bd..f1fb5f6a98 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java @@ -94,6 +94,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.BYTE; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> this; + case UBYTE -> new Vector1D.UByte((byte) x); + case SHORT -> new Vector1D.Short((short) x); + case USHORT -> new Vector1D.UShort((short) x); + case INT -> new Vector1D.Int((int) x); + case UINT -> new Vector1D.UInt((int) (long) x); + case LONG -> new Vector1D.Long((long) x); + case FLOAT -> new Vector1D.Float((float) x); + case DOUBLE -> new Vector1D.Double(x); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -238,6 +254,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.UBYTE; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) java.lang.Byte.toUnsignedInt(x)); + case UBYTE -> this; + case SHORT -> new Vector1D.Short((short) java.lang.Byte.toUnsignedInt(x)); + case USHORT -> new Vector1D.UShort((short) java.lang.Byte.toUnsignedInt(x)); + case INT -> new Vector1D.Int((int) java.lang.Byte.toUnsignedInt(x)); + case UINT -> new Vector1D.UInt((int) (long) java.lang.Byte.toUnsignedInt(x)); + case LONG -> new Vector1D.Long((long) java.lang.Byte.toUnsignedInt(x)); + case FLOAT -> new Vector1D.Float((float) java.lang.Byte.toUnsignedInt(x)); + case DOUBLE -> new Vector1D.Double(java.lang.Byte.toUnsignedInt(x)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -382,6 +414,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.SHORT; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) x); + case UBYTE -> new Vector1D.UByte((byte) x); + case SHORT -> this; + case USHORT -> new Vector1D.UShort((short) x); + case INT -> new Vector1D.Int((int) x); + case UINT -> new Vector1D.UInt((int) (long) x); + case LONG -> new Vector1D.Long((long) x); + case FLOAT -> new Vector1D.Float((float) x); + case DOUBLE -> new Vector1D.Double(x); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -526,6 +574,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.USHORT; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) java.lang.Short.toUnsignedInt(x)); + case UBYTE -> new Vector1D.UByte((byte) java.lang.Short.toUnsignedInt(x)); + case SHORT -> new Vector1D.Short((short) java.lang.Short.toUnsignedInt(x)); + case USHORT -> this; + case INT -> new Vector1D.Int((int) java.lang.Short.toUnsignedInt(x)); + case UINT -> new Vector1D.UInt((int) (long) java.lang.Short.toUnsignedInt(x)); + case LONG -> new Vector1D.Long((long) java.lang.Short.toUnsignedInt(x)); + case FLOAT -> new Vector1D.Float((float) java.lang.Short.toUnsignedInt(x)); + case DOUBLE -> new Vector1D.Double(java.lang.Short.toUnsignedInt(x)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -670,6 +734,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.INT; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) x); + case UBYTE -> new Vector1D.UByte((byte) x); + case SHORT -> new Vector1D.Short((short) x); + case USHORT -> new Vector1D.UShort((short) x); + case INT -> this; + case UINT -> new Vector1D.UInt((int) (long) x); + case LONG -> new Vector1D.Long((long) x); + case FLOAT -> new Vector1D.Float((float) x); + case DOUBLE -> new Vector1D.Double(x); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -814,6 +894,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.UINT; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) (double) Integer.toUnsignedLong(x)); + case UBYTE -> new Vector1D.UByte((byte) (double) Integer.toUnsignedLong(x)); + case SHORT -> new Vector1D.Short((short) (double) Integer.toUnsignedLong(x)); + case USHORT -> new Vector1D.UShort((short) (double) Integer.toUnsignedLong(x)); + case INT -> new Vector1D.Int((int) (double) Integer.toUnsignedLong(x)); + case UINT -> this; + case LONG -> new Vector1D.Long(Integer.toUnsignedLong(x)); + case FLOAT -> new Vector1D.Float((float) (double) Integer.toUnsignedLong(x)); + case DOUBLE -> new Vector1D.Double(Integer.toUnsignedLong(x)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -963,6 +1059,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.LONG; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) (double) x); + case UBYTE -> new Vector1D.UByte((byte) (double) x); + case SHORT -> new Vector1D.Short((short) (double) x); + case USHORT -> new Vector1D.UShort((short) (double) x); + case INT -> new Vector1D.Int((int) (double) x); + case UINT -> new Vector1D.UInt((int) (long) (double) x); + case LONG -> this; + case FLOAT -> new Vector1D.Float((float) (double) x); + case DOUBLE -> new Vector1D.Double(x); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1112,6 +1224,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.FLOAT; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) x); + case UBYTE -> new Vector1D.UByte((byte) x); + case SHORT -> new Vector1D.Short((short) x); + case USHORT -> new Vector1D.UShort((short) x); + case INT -> new Vector1D.Int((int) x); + case UINT -> new Vector1D.UInt((int) (long) x); + case LONG -> new Vector1D.Long((long) x); + case FLOAT -> this; + case DOUBLE -> new Vector1D.Double(x); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1284,6 +1412,22 @@ public abstract sealed class Vector1D<T extends Vector1D<T>> extends AbstractVec return DataType.DOUBLE; } + @Override + public Vector1D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector1D.Byte((byte) x); + case UBYTE -> new Vector1D.UByte((byte) x); + case SHORT -> new Vector1D.Short((short) x); + case USHORT -> new Vector1D.UShort((short) x); + case INT -> new Vector1D.Int((int) x); + case UINT -> new Vector1D.UInt((int) (long) x); + case LONG -> new Vector1D.Long((long) x); + case FLOAT -> new Vector1D.Float((float) x); + case DOUBLE -> this; + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java index 226db707b4..663c837ff6 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java @@ -129,6 +129,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.BYTE; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> this; + case UBYTE -> new Vector2D.UByte((byte) x, (byte) y); + case SHORT -> new Vector2D.Short((short) x, (short) y); + case USHORT -> new Vector2D.UShort((short) x, (short) y); + case INT -> new Vector2D.Int((int) x, (int) y); + case UINT -> new Vector2D.UInt((int) (long) x, (int) (long) y); + case LONG -> new Vector2D.Long((long) x, (long) y); + case FLOAT -> new Vector2D.Float((float) x, (float) y); + case DOUBLE -> new Vector2D.Double(x, y); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -304,6 +320,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.UBYTE; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) java.lang.Byte.toUnsignedInt(x), (byte) java.lang.Byte.toUnsignedInt(y)); + case UBYTE -> this; + case SHORT -> new Vector2D.Short((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y)); + case USHORT -> new Vector2D.UShort((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y)); + case INT -> new Vector2D.Int((int) java.lang.Byte.toUnsignedInt(x), (int) java.lang.Byte.toUnsignedInt(y)); + case UINT -> new Vector2D.UInt((int) (long) java.lang.Byte.toUnsignedInt(x), (int) (long) java.lang.Byte.toUnsignedInt(y)); + case LONG -> new Vector2D.Long((long) java.lang.Byte.toUnsignedInt(x), (long) java.lang.Byte.toUnsignedInt(y)); + case FLOAT -> new Vector2D.Float((float) java.lang.Byte.toUnsignedInt(x), (float) java.lang.Byte.toUnsignedInt(y)); + case DOUBLE -> new Vector2D.Double(java.lang.Byte.toUnsignedInt(x), java.lang.Byte.toUnsignedInt(y)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -479,6 +511,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.SHORT; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) x, (byte) y); + case UBYTE -> new Vector2D.UByte((byte) x, (byte) y); + case SHORT -> this; + case USHORT -> new Vector2D.UShort((short) x, (short) y); + case INT -> new Vector2D.Int((int) x, (int) y); + case UINT -> new Vector2D.UInt((int) (long) x, (int) (long) y); + case LONG -> new Vector2D.Long((long) x, (long) y); + case FLOAT -> new Vector2D.Float((float) x, (float) y); + case DOUBLE -> new Vector2D.Double(x, y); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -654,6 +702,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.USHORT; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y)); + case UBYTE -> new Vector2D.UByte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y)); + case SHORT -> new Vector2D.Short((short) java.lang.Short.toUnsignedInt(x), (short) java.lang.Short.toUnsignedInt(y)); + case USHORT -> this; + case INT -> new Vector2D.Int((int) java.lang.Short.toUnsignedInt(x), (int) java.lang.Short.toUnsignedInt(y)); + case UINT -> new Vector2D.UInt((int) (long) java.lang.Short.toUnsignedInt(x), (int) (long) java.lang.Short.toUnsignedInt(y)); + case LONG -> new Vector2D.Long((long) java.lang.Short.toUnsignedInt(x), (long) java.lang.Short.toUnsignedInt(y)); + case FLOAT -> new Vector2D.Float((float) java.lang.Short.toUnsignedInt(x), (float) java.lang.Short.toUnsignedInt(y)); + case DOUBLE -> new Vector2D.Double(java.lang.Short.toUnsignedInt(x), java.lang.Short.toUnsignedInt(y)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -829,6 +893,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.INT; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) x, (byte) y); + case UBYTE -> new Vector2D.UByte((byte) x, (byte) y); + case SHORT -> new Vector2D.Short((short) x, (short) y); + case USHORT -> new Vector2D.UShort((short) x, (short) y); + case INT -> this; + case UINT -> new Vector2D.UInt((int) (long) x, (int) (long) y); + case LONG -> new Vector2D.Long((long) x, (long) y); + case FLOAT -> new Vector2D.Float((float) x, (float) y); + case DOUBLE -> new Vector2D.Double(x, y); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1004,6 +1084,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.UINT; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y)); + case UBYTE -> new Vector2D.UByte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y)); + case SHORT -> new Vector2D.Short((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y)); + case USHORT -> new Vector2D.UShort((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y)); + case INT -> new Vector2D.Int((int) (double) Integer.toUnsignedLong(x), (int) (double) Integer.toUnsignedLong(y)); + case UINT -> this; + case LONG -> new Vector2D.Long(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y)); + case FLOAT -> new Vector2D.Float((float) (double) Integer.toUnsignedLong(x), (float) (double) Integer.toUnsignedLong(y)); + case DOUBLE -> new Vector2D.Double(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1185,6 +1281,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.LONG; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) (double) x, (byte) (double) y); + case UBYTE -> new Vector2D.UByte((byte) (double) x, (byte) (double) y); + case SHORT -> new Vector2D.Short((short) (double) x, (short) (double) y); + case USHORT -> new Vector2D.UShort((short) (double) x, (short) (double) y); + case INT -> new Vector2D.Int((int) (double) x, (int) (double) y); + case UINT -> new Vector2D.UInt((int) (long) (double) x, (int) (long) (double) y); + case LONG -> this; + case FLOAT -> new Vector2D.Float((float) (double) x, (float) (double) y); + case DOUBLE -> new Vector2D.Double(x, y); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1366,6 +1478,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.FLOAT; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) x, (byte) y); + case UBYTE -> new Vector2D.UByte((byte) x, (byte) y); + case SHORT -> new Vector2D.Short((short) x, (short) y); + case USHORT -> new Vector2D.UShort((short) x, (short) y); + case INT -> new Vector2D.Int((int) x, (int) y); + case UINT -> new Vector2D.UInt((int) (long) x, (int) (long) y); + case LONG -> new Vector2D.Long((long) x, (long) y); + case FLOAT -> this; + case DOUBLE -> new Vector2D.Double(x, y); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1578,6 +1706,22 @@ public abstract sealed class Vector2D<T extends Vector2D<T>> extends AbstractVec return DataType.DOUBLE; } + @Override + public Vector2D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector2D.Byte((byte) x, (byte) y); + case UBYTE -> new Vector2D.UByte((byte) x, (byte) y); + case SHORT -> new Vector2D.Short((short) x, (short) y); + case USHORT -> new Vector2D.UShort((short) x, (short) y); + case INT -> new Vector2D.Int((int) x, (int) y); + case UINT -> new Vector2D.UInt((int) (long) x, (int) (long) y); + case LONG -> new Vector2D.Long((long) x, (long) y); + case FLOAT -> new Vector2D.Float((float) x, (float) y); + case DOUBLE -> this; + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java index 4acb39a63b..e7ee6e5bc7 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java @@ -211,6 +211,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.BYTE; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> this; + case UBYTE -> new Vector3D.UByte((byte) x, (byte) y, (byte) z); + case SHORT -> new Vector3D.Short((short) x, (short) y, (short) z); + case USHORT -> new Vector3D.UShort((short) x, (short) y, (short) z); + case INT -> new Vector3D.Int((int) x, (int) y, (int) z); + case UINT -> new Vector3D.UInt((int) (long) x, (int) (long) y, (int) (long) z); + case LONG -> new Vector3D.Long((long) x, (long) y, (long) z); + case FLOAT -> new Vector3D.Float((float) x, (float) y, (float) z); + case DOUBLE -> new Vector3D.Double(x, y, z); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -405,6 +421,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.UBYTE; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) java.lang.Byte.toUnsignedInt(x), (byte) java.lang.Byte.toUnsignedInt(y), (byte) java.lang.Byte.toUnsignedInt(z)); + case UBYTE -> this; + case SHORT -> new Vector3D.Short((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y), (short) java.lang.Byte.toUnsignedInt(z)); + case USHORT -> new Vector3D.UShort((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y), (short) java.lang.Byte.toUnsignedInt(z)); + case INT -> new Vector3D.Int((int) java.lang.Byte.toUnsignedInt(x), (int) java.lang.Byte.toUnsignedInt(y), (int) java.lang.Byte.toUnsignedInt(z)); + case UINT -> new Vector3D.UInt((int) (long) java.lang.Byte.toUnsignedInt(x), (int) (long) java.lang.Byte.toUnsignedInt(y), (int) (long) java.lang.Byte.toUnsignedInt(z)); + case LONG -> new Vector3D.Long((long) java.lang.Byte.toUnsignedInt(x), (long) java.lang.Byte.toUnsignedInt(y), (long) java.lang.Byte.toUnsignedInt(z)); + case FLOAT -> new Vector3D.Float((float) java.lang.Byte.toUnsignedInt(x), (float) java.lang.Byte.toUnsignedInt(y), (float) java.lang.Byte.toUnsignedInt(z)); + case DOUBLE -> new Vector3D.Double(java.lang.Byte.toUnsignedInt(x), java.lang.Byte.toUnsignedInt(y), java.lang.Byte.toUnsignedInt(z)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -599,6 +631,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.SHORT; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) x, (byte) y, (byte) z); + case UBYTE -> new Vector3D.UByte((byte) x, (byte) y, (byte) z); + case SHORT -> this; + case USHORT -> new Vector3D.UShort((short) x, (short) y, (short) z); + case INT -> new Vector3D.Int((int) x, (int) y, (int) z); + case UINT -> new Vector3D.UInt((int) (long) x, (int) (long) y, (int) (long) z); + case LONG -> new Vector3D.Long((long) x, (long) y, (long) z); + case FLOAT -> new Vector3D.Float((float) x, (float) y, (float) z); + case DOUBLE -> new Vector3D.Double(x, y, z); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -793,6 +841,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.USHORT; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y), (byte) java.lang.Short.toUnsignedInt(z)); + case UBYTE -> new Vector3D.UByte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y), (byte) java.lang.Short.toUnsignedInt(z)); + case SHORT -> new Vector3D.Short((short) java.lang.Short.toUnsignedInt(x), (short) java.lang.Short.toUnsignedInt(y), (short) java.lang.Short.toUnsignedInt(z)); + case USHORT -> this; + case INT -> new Vector3D.Int((int) java.lang.Short.toUnsignedInt(x), (int) java.lang.Short.toUnsignedInt(y), (int) java.lang.Short.toUnsignedInt(z)); + case UINT -> new Vector3D.UInt((int) (long) java.lang.Short.toUnsignedInt(x), (int) (long) java.lang.Short.toUnsignedInt(y), (int) (long) java.lang.Short.toUnsignedInt(z)); + case LONG -> new Vector3D.Long((long) java.lang.Short.toUnsignedInt(x), (long) java.lang.Short.toUnsignedInt(y), (long) java.lang.Short.toUnsignedInt(z)); + case FLOAT -> new Vector3D.Float((float) java.lang.Short.toUnsignedInt(x), (float) java.lang.Short.toUnsignedInt(y), (float) java.lang.Short.toUnsignedInt(z)); + case DOUBLE -> new Vector3D.Double(java.lang.Short.toUnsignedInt(x), java.lang.Short.toUnsignedInt(y), java.lang.Short.toUnsignedInt(z)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -999,6 +1063,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.INT; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) x, (byte) y, (byte) z); + case UBYTE -> new Vector3D.UByte((byte) x, (byte) y, (byte) z); + case SHORT -> new Vector3D.Short((short) x, (short) y, (short) z); + case USHORT -> new Vector3D.UShort((short) x, (short) y, (short) z); + case INT -> this; + case UINT -> new Vector3D.UInt((int) (long) x, (int) (long) y, (int) (long) z); + case LONG -> new Vector3D.Long((long) x, (long) y, (long) z); + case FLOAT -> new Vector3D.Float((float) x, (float) y, (float) z); + case DOUBLE -> new Vector3D.Double(x, y, z); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1193,6 +1273,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.UINT; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y), (byte) (double) Integer.toUnsignedLong(z)); + case UBYTE -> new Vector3D.UByte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y), (byte) (double) Integer.toUnsignedLong(z)); + case SHORT -> new Vector3D.Short((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y), (short) (double) Integer.toUnsignedLong(z)); + case USHORT -> new Vector3D.UShort((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y), (short) (double) Integer.toUnsignedLong(z)); + case INT -> new Vector3D.Int((int) (double) Integer.toUnsignedLong(x), (int) (double) Integer.toUnsignedLong(y), (int) (double) Integer.toUnsignedLong(z)); + case UINT -> this; + case LONG -> new Vector3D.Long(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y), Integer.toUnsignedLong(z)); + case FLOAT -> new Vector3D.Float((float) (double) Integer.toUnsignedLong(x), (float) (double) Integer.toUnsignedLong(y), (float) (double) Integer.toUnsignedLong(z)); + case DOUBLE -> new Vector3D.Double(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y), Integer.toUnsignedLong(z)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1402,6 +1498,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.LONG; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) (double) x, (byte) (double) y, (byte) (double) z); + case UBYTE -> new Vector3D.UByte((byte) (double) x, (byte) (double) y, (byte) (double) z); + case SHORT -> new Vector3D.Short((short) (double) x, (short) (double) y, (short) (double) z); + case USHORT -> new Vector3D.UShort((short) (double) x, (short) (double) y, (short) (double) z); + case INT -> new Vector3D.Int((int) (double) x, (int) (double) y, (int) (double) z); + case UINT -> new Vector3D.UInt((int) (long) (double) x, (int) (long) (double) y, (int) (long) (double) z); + case LONG -> this; + case FLOAT -> new Vector3D.Float((float) (double) x, (float) (double) y, (float) (double) z); + case DOUBLE -> new Vector3D.Double(x, y, z); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1603,6 +1715,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.FLOAT; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) x, (byte) y, (byte) z); + case UBYTE -> new Vector3D.UByte((byte) x, (byte) y, (byte) z); + case SHORT -> new Vector3D.Short((short) x, (short) y, (short) z); + case USHORT -> new Vector3D.UShort((short) x, (short) y, (short) z); + case INT -> new Vector3D.Int((int) x, (int) y, (int) z); + case UINT -> new Vector3D.UInt((int) (long) x, (int) (long) y, (int) (long) z); + case LONG -> new Vector3D.Long((long) x, (long) y, (long) z); + case FLOAT -> this; + case DOUBLE -> new Vector3D.Double(x, y, z); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1837,6 +1965,22 @@ public abstract sealed class Vector3D<T extends Vector3D<T>> extends AbstractVec return DataType.DOUBLE; } + @Override + public Vector3D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector3D.Byte((byte) x, (byte) y, (byte) z); + case UBYTE -> new Vector3D.UByte((byte) x, (byte) y, (byte) z); + case SHORT -> new Vector3D.Short((short) x, (short) y, (short) z); + case USHORT -> new Vector3D.UShort((short) x, (short) y, (short) z); + case INT -> new Vector3D.Int((int) x, (int) y, (int) z); + case UINT -> new Vector3D.UInt((int) (long) x, (int) (long) y, (int) (long) z); + case LONG -> new Vector3D.Long((long) x, (long) y, (long) z); + case FLOAT -> new Vector3D.Float((float) x, (float) y, (float) z); + case DOUBLE -> this; + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java index 796548e1a3..1e1ea889b9 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java @@ -116,6 +116,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.BYTE; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> this; + case UBYTE -> new Vector4D.UByte((byte) x, (byte) y, (byte) z, (byte) w); + case SHORT -> new Vector4D.Short((short) x, (short) y, (short) z, (short) w); + case USHORT -> new Vector4D.UShort((short) x, (short) y, (short) z, (short) w); + case INT -> new Vector4D.Int((int) x, (int) y, (int) z, (int) w); + case UINT -> new Vector4D.UInt((int) (long) x, (int) (long) y, (int) (long) z, (int) (long) w); + case LONG -> new Vector4D.Long((long) x, (long) y, (long) z, (long) w); + case FLOAT -> new Vector4D.Float((float) x, (float) y, (float) z, (float) w); + case DOUBLE -> new Vector4D.Double(x, y, z, w); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -324,6 +340,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.UBYTE; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) java.lang.Byte.toUnsignedInt(x), (byte) java.lang.Byte.toUnsignedInt(y), (byte) java.lang.Byte.toUnsignedInt(z), (byte) java.lang.Byte.toUnsignedInt(w)); + case UBYTE -> this; + case SHORT -> new Vector4D.Short((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y), (short) java.lang.Byte.toUnsignedInt(z), (short) java.lang.Byte.toUnsignedInt(w)); + case USHORT -> new Vector4D.UShort((short) java.lang.Byte.toUnsignedInt(x), (short) java.lang.Byte.toUnsignedInt(y), (short) java.lang.Byte.toUnsignedInt(z), (short) java.lang.Byte.toUnsignedInt(w)); + case INT -> new Vector4D.Int((int) java.lang.Byte.toUnsignedInt(x), (int) java.lang.Byte.toUnsignedInt(y), (int) java.lang.Byte.toUnsignedInt(z), (int) java.lang.Byte.toUnsignedInt(w)); + case UINT -> new Vector4D.UInt((int) (long) java.lang.Byte.toUnsignedInt(x), (int) (long) java.lang.Byte.toUnsignedInt(y), (int) (long) java.lang.Byte.toUnsignedInt(z), (int) (long) java.lang.Byte.toUnsignedInt(w)); + case LONG -> new Vector4D.Long((long) java.lang.Byte.toUnsignedInt(x), (long) java.lang.Byte.toUnsignedInt(y), (long) java.lang.Byte.toUnsignedInt(z), (long) java.lang.Byte.toUnsignedInt(w)); + case FLOAT -> new Vector4D.Float((float) java.lang.Byte.toUnsignedInt(x), (float) java.lang.Byte.toUnsignedInt(y), (float) java.lang.Byte.toUnsignedInt(z), (float) java.lang.Byte.toUnsignedInt(w)); + case DOUBLE -> new Vector4D.Double(java.lang.Byte.toUnsignedInt(x), java.lang.Byte.toUnsignedInt(y), java.lang.Byte.toUnsignedInt(z), java.lang.Byte.toUnsignedInt(w)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -532,6 +564,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.SHORT; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) x, (byte) y, (byte) z, (byte) w); + case UBYTE -> new Vector4D.UByte((byte) x, (byte) y, (byte) z, (byte) w); + case SHORT -> this; + case USHORT -> new Vector4D.UShort((short) x, (short) y, (short) z, (short) w); + case INT -> new Vector4D.Int((int) x, (int) y, (int) z, (int) w); + case UINT -> new Vector4D.UInt((int) (long) x, (int) (long) y, (int) (long) z, (int) (long) w); + case LONG -> new Vector4D.Long((long) x, (long) y, (long) z, (long) w); + case FLOAT -> new Vector4D.Float((float) x, (float) y, (float) z, (float) w); + case DOUBLE -> new Vector4D.Double(x, y, z, w); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -740,6 +788,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.USHORT; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y), (byte) java.lang.Short.toUnsignedInt(z), (byte) java.lang.Short.toUnsignedInt(w)); + case UBYTE -> new Vector4D.UByte((byte) java.lang.Short.toUnsignedInt(x), (byte) java.lang.Short.toUnsignedInt(y), (byte) java.lang.Short.toUnsignedInt(z), (byte) java.lang.Short.toUnsignedInt(w)); + case SHORT -> new Vector4D.Short((short) java.lang.Short.toUnsignedInt(x), (short) java.lang.Short.toUnsignedInt(y), (short) java.lang.Short.toUnsignedInt(z), (short) java.lang.Short.toUnsignedInt(w)); + case USHORT -> this; + case INT -> new Vector4D.Int((int) java.lang.Short.toUnsignedInt(x), (int) java.lang.Short.toUnsignedInt(y), (int) java.lang.Short.toUnsignedInt(z), (int) java.lang.Short.toUnsignedInt(w)); + case UINT -> new Vector4D.UInt((int) (long) java.lang.Short.toUnsignedInt(x), (int) (long) java.lang.Short.toUnsignedInt(y), (int) (long) java.lang.Short.toUnsignedInt(z), (int) (long) java.lang.Short.toUnsignedInt(w)); + case LONG -> new Vector4D.Long((long) java.lang.Short.toUnsignedInt(x), (long) java.lang.Short.toUnsignedInt(y), (long) java.lang.Short.toUnsignedInt(z), (long) java.lang.Short.toUnsignedInt(w)); + case FLOAT -> new Vector4D.Float((float) java.lang.Short.toUnsignedInt(x), (float) java.lang.Short.toUnsignedInt(y), (float) java.lang.Short.toUnsignedInt(z), (float) java.lang.Short.toUnsignedInt(w)); + case DOUBLE -> new Vector4D.Double(java.lang.Short.toUnsignedInt(x), java.lang.Short.toUnsignedInt(y), java.lang.Short.toUnsignedInt(z), java.lang.Short.toUnsignedInt(w)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -963,6 +1027,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.INT; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) x, (byte) y, (byte) z, (byte) w); + case UBYTE -> new Vector4D.UByte((byte) x, (byte) y, (byte) z, (byte) w); + case SHORT -> new Vector4D.Short((short) x, (short) y, (short) z, (short) w); + case USHORT -> new Vector4D.UShort((short) x, (short) y, (short) z, (short) w); + case INT -> this; + case UINT -> new Vector4D.UInt((int) (long) x, (int) (long) y, (int) (long) z, (int) (long) w); + case LONG -> new Vector4D.Long((long) x, (long) y, (long) z, (long) w); + case FLOAT -> new Vector4D.Float((float) x, (float) y, (float) z, (float) w); + case DOUBLE -> new Vector4D.Double(x, y, z, w); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1171,6 +1251,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.UINT; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y), (byte) (double) Integer.toUnsignedLong(z), (byte) (double) Integer.toUnsignedLong(w)); + case UBYTE -> new Vector4D.UByte((byte) (double) Integer.toUnsignedLong(x), (byte) (double) Integer.toUnsignedLong(y), (byte) (double) Integer.toUnsignedLong(z), (byte) (double) Integer.toUnsignedLong(w)); + case SHORT -> new Vector4D.Short((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y), (short) (double) Integer.toUnsignedLong(z), (short) (double) Integer.toUnsignedLong(w)); + case USHORT -> new Vector4D.UShort((short) (double) Integer.toUnsignedLong(x), (short) (double) Integer.toUnsignedLong(y), (short) (double) Integer.toUnsignedLong(z), (short) (double) Integer.toUnsignedLong(w)); + case INT -> new Vector4D.Int((int) (double) Integer.toUnsignedLong(x), (int) (double) Integer.toUnsignedLong(y), (int) (double) Integer.toUnsignedLong(z), (int) (double) Integer.toUnsignedLong(w)); + case UINT -> this; + case LONG -> new Vector4D.Long(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y), Integer.toUnsignedLong(z), Integer.toUnsignedLong(w)); + case FLOAT -> new Vector4D.Float((float) (double) Integer.toUnsignedLong(x), (float) (double) Integer.toUnsignedLong(y), (float) (double) Integer.toUnsignedLong(z), (float) (double) Integer.toUnsignedLong(w)); + case DOUBLE -> new Vector4D.Double(Integer.toUnsignedLong(x), Integer.toUnsignedLong(y), Integer.toUnsignedLong(z), Integer.toUnsignedLong(w)); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1397,6 +1493,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.LONG; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) (double) x, (byte) (double) y, (byte) (double) z, (byte) (double) w); + case UBYTE -> new Vector4D.UByte((byte) (double) x, (byte) (double) y, (byte) (double) z, (byte) (double) w); + case SHORT -> new Vector4D.Short((short) (double) x, (short) (double) y, (short) (double) z, (short) (double) w); + case USHORT -> new Vector4D.UShort((short) (double) x, (short) (double) y, (short) (double) z, (short) (double) w); + case INT -> new Vector4D.Int((int) (double) x, (int) (double) y, (int) (double) z, (int) (double) w); + case UINT -> new Vector4D.UInt((int) (long) (double) x, (int) (long) (double) y, (int) (long) (double) z, (int) (long) (double) w); + case LONG -> this; + case FLOAT -> new Vector4D.Float((float) (double) x, (float) (double) y, (float) (double) z, (float) (double) w); + case DOUBLE -> new Vector4D.Double(x, y, z, w); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1613,6 +1725,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.FLOAT; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) x, (byte) y, (byte) z, (byte) w); + case UBYTE -> new Vector4D.UByte((byte) x, (byte) y, (byte) z, (byte) w); + case SHORT -> new Vector4D.Short((short) x, (short) y, (short) z, (short) w); + case USHORT -> new Vector4D.UShort((short) x, (short) y, (short) z, (short) w); + case INT -> new Vector4D.Int((int) x, (int) y, (int) z, (int) w); + case UINT -> new Vector4D.UInt((int) (long) x, (int) (long) y, (int) (long) z, (int) (long) w); + case LONG -> new Vector4D.Long((long) x, (long) y, (long) z, (long) w); + case FLOAT -> this; + case DOUBLE -> new Vector4D.Double(x, y, z, w); + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { @@ -1864,6 +1992,22 @@ public abstract sealed class Vector4D<T extends Vector4D<T>> extends AbstractVec return DataType.DOUBLE; } + @Override + public Vector4D<?> copy(final DataType dataType) { + return switch (dataType) { + case BYTE -> new Vector4D.Byte((byte) x, (byte) y, (byte) z, (byte) w); + case UBYTE -> new Vector4D.UByte((byte) x, (byte) y, (byte) z, (byte) w); + case SHORT -> new Vector4D.Short((short) x, (short) y, (short) z, (short) w); + case USHORT -> new Vector4D.UShort((short) x, (short) y, (short) z, (short) w); + case INT -> new Vector4D.Int((int) x, (int) y, (int) z, (int) w); + case UINT -> new Vector4D.UInt((int) (long) x, (int) (long) y, (int) (long) z, (int) (long) w); + case LONG -> new Vector4D.Long((long) x, (long) y, (long) z, (long) w); + case FLOAT -> new Vector4D.Float((float) x, (float) y, (float) z, (float) w); + case DOUBLE -> this; + default -> throw new UnsupportedOperationException("Unexpected data type"); + }; + } + @Override public double get(int indice) { switch (indice) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java index e09a93d086..8f5e9be5f3 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java @@ -77,6 +77,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.BYTE; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.BYTE) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -244,6 +249,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.UBYTE; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.UBYTE) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -411,6 +421,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.SHORT; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.SHORT) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -578,6 +593,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.USHORT; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.USHORT) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -745,6 +765,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.INT; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.INT) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -912,6 +937,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.UINT; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.UINT) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -1098,6 +1128,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.LONG; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.LONG) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -1286,6 +1321,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.FLOAT; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.FLOAT) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; @@ -1500,6 +1540,11 @@ public abstract sealed class VectorND<T extends VectorND<T>> extends AbstractVec return DataType.DOUBLE; } + @Override + public Vector<?> copy(final DataType dataType) { + return (dataType == DataType.DOUBLE) ? this : Vectors.create(toArrayDouble(), dataType); + } + @Override public int getDimension() { return values.length; diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java index e5f0ac7579..e8ead2e43d 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java @@ -307,11 +307,14 @@ public abstract class AbstractVectorTest { } /** - * Test copy in another data type. + * Test copy in another data type. A copy in the same data type is the same vector, vectors being immutable. */ private void testCopyDataType(Vector<?> tuple) { tuple = vector(tuple, sequence(tuple.getDimension(), 1)); - for (final DataType dataType : new DataType[] {DataType.DOUBLE, DataType.FLOAT, DataType.SHORT}) { + assertSame(tuple, tuple.copy(tuple.getDataType())); + for (final DataType dataType : new DataType[] {DataType.BYTE, DataType.UBYTE, DataType.SHORT, DataType.USHORT, + DataType.INT, DataType.UINT, DataType.LONG, DataType.FLOAT, DataType.DOUBLE}) + { final Vector<?> copy = tuple.copy(dataType); assertEquals(dataType, copy.getDataType()); assertEquals(tuple.getDimension(), copy.getDimension()); diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Vector3DTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Vector3DTest.java index 0e2a846500..a43a5e34e3 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Vector3DTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Vector3DTest.java @@ -32,6 +32,32 @@ public class Vector3DTest { private static final double TOLERANCE = 1e-12; private static final double HALF_SQRT2 = Math.sqrt(2) / 2; + /** + * Tests {@link Vector3D.Double#copy(DataType)} and the copy of vectors of long and unsigned values. + * The values are converted from their double values: integer types wrap around, + * except a long beyond the integer range which saturates. + */ + @Test + public void testCopy() { + final Vector3D.Double v = new Vector3D.Double(300.7, -1.2, 70000); + assertSame(v, v.copy(DataType.DOUBLE)); + assertInstanceOf(Vector3D.Byte.class, v.copy(DataType.BYTE)); + assertArrayEquals(new double[] {44, -1, 112}, v.copy(DataType.BYTE).toArrayDouble()); + assertArrayEquals(new double[] {44, 255, 112}, v.copy(DataType.UBYTE).toArrayDouble()); + assertArrayEquals(new double[] {300, 4294967295.0, 70000}, v.copy(DataType.UINT).toArrayDouble()); + assertArrayEquals(new double[] {300.7f, -1.2f, 70000}, v.copy(DataType.FLOAT).toArrayDouble()); + final Vector3D.Long large = new Vector3D.Long(1L << 40, -3, 7); + assertSame(large, large.copy(DataType.LONG)); + assertArrayEquals(new double[] {2147483647, -3, 7}, large.copy(DataType.INT).toArrayDouble()); + final Vector3D.UInt unsigned = new Vector3D.UInt((int) 3000000000L, 5, 7); + assertArrayEquals(new double[] {-1, 5, 7}, unsigned.copy(DataType.BYTE).toArrayDouble()); // Saturated to int. + assertArrayEquals(new double[] {3000000000.0, 5, 7}, unsigned.copy(DataType.LONG).toArrayDouble()); + final Vector3D.UByte ubyte = new Vector3D.UByte((byte) 200, (byte) 5, (byte) 7); + assertArrayEquals(new double[] {200, 5, 7}, ubyte.copy(DataType.SHORT).toArrayDouble()); + assertArrayEquals(new double[] {-56, 5, 7}, ubyte.copy(DataType.BYTE).toArrayDouble()); + assertThrows(UnsupportedOperationException.class, () -> v.copy(DataType.NORMALIZED_BYTE)); + } + /** * fromLatLon test on remarkable points : equator/meridian origin, * poles, and a quadrant point.
