Skip to content

Commit

Permalink
Read bytes more efficiently (jMonkeyEngine#2137)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tonihele authored Nov 17, 2023
1 parent b6735af commit 20ecf68
Showing 1 changed file with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,37 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou
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);
}
index += stride;
}
}

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;
Expand Down Expand Up @@ -737,30 +755,24 @@ public static void assertNotNull(Object o, String errorMessage) {
}
}

// 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);
}


Expand Down

0 comments on commit 20ecf68

Please sign in to comment.