Browse Source

Read bytes more efficiently (#2137)

* Read bytes more efficiently

* Read straight to the buffer

* Epsilon as final

* Simple assert for reading malformed stream

* Stream might not have reached the end even if it doesn't return requested amount of bytes

* Stream might not have reached the end even if it doesn't return requested amount of bytes

* Refactor to method
Toni Helenius 1 year ago
parent
commit
20ecf6890e
1 changed files with 34 additions and 22 deletions
  1. 34 22
      jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java

+ 34 - 22
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java

@@ -425,12 +425,19 @@ public class GltfUtils {
         int stride = Math.max(dataLength, byteStride);
         int end = count * stride + byteOffset;
         stream.skipBytes(byteOffset);
+
+        if (dataLength == stride) {
+            read(stream, array, end - index);
+
+            return;
+        }
+
         int arrayIndex = 0;
+        byte[] buffer = new byte[numComponents];
         while (index < end) {
-            for (int i = 0; i < numComponents; i++) {
-                array[arrayIndex] = stream.readByte();
-                arrayIndex++;
-            }
+            read(stream, buffer, numComponents);
+            System.arraycopy(buffer, 0, array, arrayIndex, numComponents);
+            arrayIndex += numComponents;
             if (dataLength < stride) {
                 stream.skipBytes(stride - dataLength);
             }
@@ -438,6 +445,17 @@ public class GltfUtils {
         }
     }
 
+    private static void read(LittleEndien stream, byte[] buffer, int length) throws IOException {
+        int n = 0;
+        while (n < length) {
+            int cnt = stream.read(buffer, n, length - n);
+            if (cnt < 0) {
+                throw new AssetLoadException("Data ended prematurely");
+            }
+            n += cnt;
+        }
+    }
+
     private static void populateShortArray(short[] array, LittleEndien stream, int count, int byteOffset, int byteStride, int numComponents, VertexBuffer.Format format) throws IOException {
         int componentSize = format.getComponentSize();
         int index = byteOffset;
@@ -737,30 +755,24 @@ public class GltfUtils {
         }
     }
 
-//    public static boolean equalBindAndLocalTransforms(Joint b) {
-//        return equalsEpsilon(b.getBindPosition(), b.getLocalPosition())
-//                && equalsEpsilon(b.getBindRotation(), b.getLocalRotation())
-//                && equalsEpsilon(b.getBindScale(), b.getLocalScale());
-//    }
-
-    private static float epsilon = 0.0001f;
+    private static final float EPSILON = 0.0001f;
 
     public static boolean equalsEpsilon(Vector3f v1, Vector3f v2) {
-        return FastMath.abs(v1.x - v2.x) < epsilon
-                && FastMath.abs(v1.y - v2.y) < epsilon
-                && FastMath.abs(v1.z - v2.z) < epsilon;
+        return FastMath.abs(v1.x - v2.x) < EPSILON
+                && FastMath.abs(v1.y - v2.y) < EPSILON
+                && FastMath.abs(v1.z - v2.z) < EPSILON;
     }
 
     public static boolean equalsEpsilon(Quaternion q1, Quaternion q2) {
-        return (FastMath.abs(q1.getX() - q2.getX()) < epsilon
-                && FastMath.abs(q1.getY() - q2.getY()) < epsilon
-                && FastMath.abs(q1.getZ() - q2.getZ()) < epsilon
-                && FastMath.abs(q1.getW() - q2.getW()) < epsilon)
+        return (FastMath.abs(q1.getX() - q2.getX()) < EPSILON
+                && FastMath.abs(q1.getY() - q2.getY()) < EPSILON
+                && FastMath.abs(q1.getZ() - q2.getZ()) < EPSILON
+                && FastMath.abs(q1.getW() - q2.getW()) < EPSILON)
                 ||
-                (FastMath.abs(q1.getX() + q2.getX()) < epsilon
-                        && FastMath.abs(q1.getY() + q2.getY()) < epsilon
-                        && FastMath.abs(q1.getZ() + q2.getZ()) < epsilon
-                        && FastMath.abs(q1.getW() + q2.getW()) < epsilon);
+ (FastMath.abs(q1.getX() + q2.getX()) < EPSILON
+                && FastMath.abs(q1.getY() + q2.getY()) < EPSILON
+                && FastMath.abs(q1.getZ() + q2.getZ()) < EPSILON
+                && FastMath.abs(q1.getW() + q2.getW()) < EPSILON);
     }