From 06c01debef40c5804783437a6df2b2072ef1b0f7 Mon Sep 17 00:00:00 2001 From: Peter Abeles Date: Thu, 21 Dec 2023 15:42:28 -0800 Subject: [PATCH] PLY - Fixed endian issue when reading/writing polyline --- .../src/main/java/boofcv/io/points/impl/PlyCodec.java | 7 ++++++- .../test/java/boofcv/io/points/impl/TestPlyCodec.java | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/main/boofcv-io/src/main/java/boofcv/io/points/impl/PlyCodec.java b/main/boofcv-io/src/main/java/boofcv/io/points/impl/PlyCodec.java index 1b483470cf..bddbcf4d7d 100644 --- a/main/boofcv-io/src/main/java/boofcv/io/points/impl/PlyCodec.java +++ b/main/boofcv-io/src/main/java/boofcv/io/points/impl/PlyCodec.java @@ -168,6 +168,7 @@ public static void saveBinary( PlyWriter data, ByteOrder order, boolean saveAsFl var indexes = new int[100]; bytes = ByteBuffer.allocate(1 + indexes.length*4); + bytes.order(order); for (int i = 0; i < data.getPolygonCount(); i++) { int size = data.getIndexes(i, indexes); bytes.position(0); @@ -467,6 +468,7 @@ private static void readCloudBinary( PlyReader output, InputStream reader, List< final var polygonLine = new byte[4*10]; final ByteBuffer polygonBB = ByteBuffer.wrap(polygonLine); + polygonBB.order(order); int[] indexes = new int[100]; for (int i = 0; i < triangleCount; i++) { if (1 != reader.read(line, 0, 1)) @@ -482,7 +484,10 @@ private static void readCloudBinary( PlyReader output, InputStream reader, List< throw new IOException("Read unexpected number of bytes. " + found + " vs " + lineLength); for (int wordIndex = 0; wordIndex < count; wordIndex++) { - indexes[wordIndex] = polygonBB.getInt(wordIndex*4); + int foundIndex = polygonBB.getInt(wordIndex*4); + if (foundIndex < 0 || foundIndex > vertexCount) + throw new IOException("Negative index. word: " + wordIndex + " value: " + foundIndex + " count: " + vertexCount); + indexes[wordIndex] = foundIndex; } output.addPolygon(indexes, 0, count); diff --git a/main/boofcv-io/src/test/java/boofcv/io/points/impl/TestPlyCodec.java b/main/boofcv-io/src/test/java/boofcv/io/points/impl/TestPlyCodec.java index 0d86394aaf..7de91d0665 100644 --- a/main/boofcv-io/src/test/java/boofcv/io/points/impl/TestPlyCodec.java +++ b/main/boofcv-io/src/test/java/boofcv/io/points/impl/TestPlyCodec.java @@ -140,11 +140,14 @@ class TestPlyCodec extends BoofStandardJUnit { for (var endian : new ByteOrder[]{ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN}) { var mesh = new VertexMesh(); mesh.offsets.add(0); - for (int i = 0; i < 10; i++) { + int numVertexes = 10; + for (int i = 0; i < numVertexes; i++) { mesh.vertexes.append(i, 2, 3); - mesh.indexes.add(i*3); - mesh.indexes.add(i*3 + 1); - mesh.indexes.add(i*3 + 2); + + // bound indexes to ensure they are in the valid range + mesh.indexes.add((i*3)%numVertexes); + mesh.indexes.add((i*3 + 1)%numVertexes); + mesh.indexes.add((i*3 + 2)%numVertexes); mesh.offsets.add(mesh.indexes.size); } var colors = new DogArray_I32();