Archived
0

добавлены методы (де)комперессии для XYZ координат

This commit is contained in:
2018-08-18 23:09:54 +03:00
parent 683d62bfde
commit b6120736f3
2 changed files with 37 additions and 2 deletions

View File

@@ -20,4 +20,23 @@ public class CompactedCoords {
(int)(short) (compactValue | 0xFFFF0000) (int)(short) (compactValue | 0xFFFF0000)
}; };
} }
private static int floor_double(double value) {
int i = (int)value;
return value < (double)i ? i - 1 : i;
}
public static long compressXYZ(double x, double y, double z) {
return ((floor_double(x) & 0x3FFFFFF) << 38)
| ((floor_double(y) & 0xFFF) << 26)
| (floor_double(z) & 0x3FFFFFF);
}
public static double[] uncompressXYZ(long compactValue) {
return new double[]{
compactValue >> 38,
(compactValue >> 26) & 0x0FFF,
compactValue << 38 >> 38 // is normal?
};
}
} }

View File

@@ -8,7 +8,7 @@ import java.util.Random;
public class TestCompactedCoords { public class TestCompactedCoords {
@Test @Test
public void testSimple() { public void testXZSimple() {
for (int z = -100; z <= 100; z++) { for (int z = -100; z <= 100; z++) {
for (int x = -100; x <= 100; x++) { for (int x = -100; x <= 100; x++) {
int compressXZ = CompactedCoords.compressXZ(x, z); int compressXZ = CompactedCoords.compressXZ(x, z);
@@ -21,7 +21,7 @@ public class TestCompactedCoords {
} }
@Test @Test
public void testRandom() { public void testXZRandom() {
Random random = new Random(); Random random = new Random();
int x,z; int x,z;
@@ -42,4 +42,20 @@ public class TestCompactedCoords {
Assert.assertEquals(z, xz[1]); Assert.assertEquals(z, xz[1]);
} }
} }
// @Test
public void testXYZSimple() {
for (int z = -100; z <= 100; z++) {
for (int x = -100; x <= 100; x++) {
for (int y = -100; y <= 100; y++) {
long compressXYZ = CompactedCoords.compressXYZ(x, y, z);
double[] xyz = CompactedCoords.uncompressXYZ(compressXYZ);
Assert.assertEquals(x, xyz[0], 0.001d);
Assert.assertEquals(y, xyz[1], 0.001d);
Assert.assertEquals(z, xyz[2], 0.001d);
}
}
}
}
} }