diff --git a/rugged-core/src/main/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTile.java b/rugged-core/src/main/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTile.java index 89e9f72726eb490748649e44675cb99346cd9e8c..34ce31eda153f59c657540161390e5e0bc017723 100644 --- a/rugged-core/src/main/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTile.java +++ b/rugged-core/src/main/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTile.java @@ -220,9 +220,11 @@ public class MinMaxTreeTile extends SimpleTile { final int rows = 1 << ((start.length - level) / 2); if (row1 <= row2) { - return buildCrossings(rows * (row1 / rows + 1), row2, rows); + final int nextMultiple = row1 + rows - (row1 % rows); + return buildCrossings(nextMultiple, row2, rows); } else { - return buildCrossings(rows * ((row1 - 1) / rows), row2, -rows); + final int previousMultiple = row1 - 1 - ((row1 - 1) % rows); + return buildCrossings(previousMultiple, row2, -rows); } } @@ -245,9 +247,11 @@ public class MinMaxTreeTile extends SimpleTile { final int columns = 1 << ((start.length + 1 - level) / 2);; if (column1 <= column2) { - return buildCrossings(columns * (column1 / columns + 1), column2, columns); + final int nextMultiple = column1 + columns - (column1 % columns); + return buildCrossings(nextMultiple, column2, columns); } else { - return buildCrossings(columns * ((column1 - 1) / columns), column2, -columns); + final int previousMultiple = column1 - 1 - ((column1 - 1) % columns); + return buildCrossings(previousMultiple, column2, -columns); } } @@ -261,7 +265,8 @@ public class MinMaxTreeTile extends SimpleTile { private int[] buildCrossings(final int begin, final int end, final int step) { // allocate array - final int[] crossings = new int[FastMath.max(0, (end - begin) / step)]; + final int n = FastMath.max(0, (end - begin + step + ((step > 0) ? -1 : +1)) / step); + final int[] crossings = new int[n]; // fill it up int crossing = begin; diff --git a/rugged-core/src/test/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTileTest.java b/rugged-core/src/test/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTileTest.java index d9afa57031434fee8bc481c98d799eafed77f50f..fbc66df073eb6480ab49c16a5815050de80851f5 100644 --- a/rugged-core/src/test/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTileTest.java +++ b/rugged-core/src/test/java/org/orekit/rugged/core/duvenhage/MinMaxTreeTileTest.java @@ -192,34 +192,24 @@ public class MinMaxTreeTileTest { MinMaxTreeTile tile = createTile(nbRows, nbColumns); for (int level = 0; level < tile.getLevels(); ++level) { + int[] neighbors = neighbors(0, 0, nbRows, nbColumns, tile.getLevels() - level); + int subTileRows = neighbors[1] - neighbors[0]; + int subTileCols = neighbors[3] - neighbors[2]; for (int i1 = 0; i1 < nbRows; ++i1) { for (int i2 = 0; i2 < nbRows; ++i2) { int[] crossings = tile.getCrossedBoundaryRows(i1, i2, level); - if (FastMath.abs(i2 - i1) < 2) { - Assert.assertEquals(0, crossings.length); - } - for (int crossed : crossings) { - Assert.assertNotEquals(i1, crossed); - Assert.assertNotEquals(i2, crossed); - int[] neighbors1 = neighbors(crossed - 1, 0, nbRows, nbColumns, tile.getLevels() - level); - int[] neighbors2 = neighbors(crossed, 0, nbRows, nbColumns, tile.getLevels() - level); - Assert.assertEquals(neighbors1[1], neighbors2[0]); - } + int[] ref = multiples(i1, i2, subTileRows); + Assert.assertArrayEquals(ref, crossings); } - for (int j1 = 0; j1 < nbColumns; ++j1) { - for (int j2 = 0; j2 < nbColumns; ++j2) { - int[] crossings = tile.getCrossedBoundaryColumns(j1, j2, level); - if (FastMath.abs(j2 - j1) < 2) { - Assert.assertEquals(0, crossings.length); - } - for (int crossed : crossings) { - Assert.assertNotEquals(j1, crossed); - Assert.assertNotEquals(j2, crossed); - int[] neighbors1 = neighbors(0, crossed - 1, nbRows, nbColumns, tile.getLevels() - level); - int[] neighbors2 = neighbors(0, crossed, nbRows, nbColumns, tile.getLevels() - level); - Assert.assertEquals(neighbors1[3], neighbors2[2]); - } + } + for (int j1 = 0; j1 < nbColumns; ++j1) { + for (int j2 = 0; j2 < nbColumns; ++j2) { + int[] crossings = tile.getCrossedBoundaryColumns(j1, j2, level); + int[] ref = multiples(j1, j2, subTileCols); + if (ref.length != crossings.length) { + crossings = tile.getCrossedBoundaryColumns(j1, j2, level); } + Assert.assertArrayEquals(ref, crossings); } } } @@ -260,6 +250,41 @@ public class MinMaxTreeTileTest { } + private int[] multiples(int k1, int k2, int n) { + + // poor man identification of rows/columns crossings + // this identification is intentionally independent of the MinMaxTreeTile class, + // for testing purposes + + // intentionally dumb way of counting multiples of n + int count = 0; + for (int k = FastMath.min(k1, k2) + 1; k < FastMath.max(k1, k2); ++k) { + if (k % n == 0) { + ++count; + } + } + + int[] multiples = new int[count]; + int index = 0; + for (int k = FastMath.min(k1, k2) + 1; k < FastMath.max(k1, k2); ++k) { + if (k % n == 0) { + multiples[index++] = k; + } + } + + if (k1 > k2) { + // revert the array + for (int i = 0; i < count / 2; ++i) { + int tmp = multiples[i]; + multiples[i] = multiples[count - 1 - i]; + multiples[count - 1 - i] = tmp; + } + } + + return multiples; + + } + private MinMaxTreeTile createTile(int nbRows, int nbColumns) throws RuggedException { MinMaxTreeTile tile = new MinMaxTreeTileFactory().createTile(); tile.setGeometry(1.0, 2.0, 0.1, 0.2, nbRows, nbColumns);