Skip to content

Commit

Permalink
Moved rectangle padding to 1:100 to only maxDepth
Browse files Browse the repository at this point in the history
instead of the actual search space.
  • Loading branch information
sherfert committed Apr 9, 2018
1 parent b31c6d2 commit 674e9c0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 66 deletions.
Expand Up @@ -23,6 +23,8 @@

public class Envelope
{
private static final double MAXIMAL_ENVELOPE_SIDE_RATIO = 100;

protected final double[] min;
protected final double[] max;

Expand Down Expand Up @@ -55,6 +57,31 @@ public Envelope( double xmin, double xmax, double ymin, double ymax )
this( new double[] { xmin, ymin }, new double[] { xmax, ymax } );
}

/**
* @return a copy of the envelope where the ratio of smallest to largest side is not more than 1:100
*/
public Envelope withSideRatioNotTooSmall( )
{
double[] from = Arrays.copyOf( this.min, min.length );
double[] to = Arrays.copyOf( this.max, max.length );
double highestDiff = -Double.MAX_VALUE;
double[] diffs = new double[from.length];
for ( int i = 0; i < from.length; i++ )
{
diffs[i] = to[i] - from[i];
highestDiff = Math.max( highestDiff, diffs[i] );
}
final double mindiff = highestDiff / MAXIMAL_ENVELOPE_SIDE_RATIO;
for ( int i = 0; i < from.length; i++ )
{
if ( diffs[i] < mindiff )
{
to[i] = from[i] + mindiff;
}
}
return new Envelope( from, to );
}

public double[] getMin()
{
return min;
Expand Down
Expand Up @@ -30,8 +30,6 @@

public abstract class SpaceFillingCurve
{

private static final double MAXIMAL_ENVELOPE_SIDE_RATIO = 100;
/**
* Description of the space filling curve structure
*/
Expand Down Expand Up @@ -284,30 +282,10 @@ else if ( toOrNull == null )
}
}
}
ensureSideRatioNotTooSmall( from, to );
Envelope referenceEnvelope = new Envelope( from, to );
return getTilesIntersectingEnvelope( referenceEnvelope, config, null );
}

static void ensureSideRatioNotTooSmall( double[] from, double[] to )
{
double highestDiff = -Double.MAX_VALUE;
double[] diffs = new double[from.length];
for ( int i = 0; i < from.length; i++ )
{
diffs[i] = to[i] - from[i];
highestDiff = Math.max( highestDiff, diffs[i] );
}
final double mindiff = highestDiff / MAXIMAL_ENVELOPE_SIDE_RATIO;
for ( int i = 0; i < from.length; i++ )
{
if ( diffs[i] < mindiff )
{
to[i] = from[i] + mindiff;
}
}
}

List<LongRange> getTilesIntersectingEnvelope( Envelope referenceEnvelope, SpaceFillingCurveConfiguration config, SpaceFillingCurveMonitor monitor )
{
SearchEnvelope search = new SearchEnvelope( this, referenceEnvelope );
Expand Down
Expand Up @@ -64,7 +64,8 @@ public boolean stopAtThisDepth( double overlap, int depth, int maxDepth )
@Override
public int maxDepth( Envelope referenceEnvelope, Envelope range, int nbrDim, int maxLevel )
{
double searchRatio = range.getArea() / referenceEnvelope.getArea();
Envelope paddedEnvelope = referenceEnvelope.withSideRatioNotTooSmall();
double searchRatio = range.getArea() / paddedEnvelope.getArea();
if ( Double.isInfinite( searchRatio ) )
{
return maxLevel;
Expand Down
Expand Up @@ -24,6 +24,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

public class EnvelopeTest
Expand Down Expand Up @@ -106,6 +107,49 @@ public void shouldHandleIntersectionsIn2D()
testOverlaps(left, new Envelope( 0.5, 1.5, 0.0, 1.0 ), true, 0.5, 0.5); // overlaps right half
}

@Test
public void testWithSideRatioNotTooSmall2D()
{
// No change expected
double[] from = new double[] {0, 0};
double[] to = new double[] {1, 1};

Envelope envelope = new Envelope( from, to ).withSideRatioNotTooSmall();
double[] expectedFrom = new double[] {0, 0};
double[] expectedTo = new double[] {1, 1};
assertArrayEquals( expectedFrom, envelope.min , 0);
assertArrayEquals( expectedTo, envelope.max, 0 );

// Expected to change
to = new double[] {100, 0.1};
Envelope envelope2 = new Envelope( from, to ).withSideRatioNotTooSmall();
double[] expectedTo2 = new double[] {100, 1};
assertArrayEquals( expectedFrom, envelope2.min , 0);
assertArrayEquals( expectedTo2, envelope2.max, 0.00001 );
}

// Works for any number of dimensions, and 4 is more interesting than 3
@Test
public void testWithSideRatioNotTooSmall4D()
{
// No change expected
double[] from = new double[] {0, 0, 0, 0};
double[] to = new double[] {1, 1, 1, 1};

Envelope envelope = new Envelope( from, to ).withSideRatioNotTooSmall();
double[] expectedFrom = new double[] {0, 0, 0, 0};
double[] expectedTo = new double[] {1, 1, 1, 1};
assertArrayEquals( expectedFrom, envelope.min , 0);
assertArrayEquals( expectedTo, envelope.max, 0 );

// Expected to change
to = new double[] {100, 0.1, 12, 0.01};
Envelope envelope2 = new Envelope( from, to ).withSideRatioNotTooSmall();
double[] expectedTo2 = new double[] {100, 1, 12, 1};
assertArrayEquals( expectedFrom, envelope2.min , 0);
assertArrayEquals( expectedTo2, envelope2.max, 0.00001 );
}

private void makeAndTestEnvelope( double[] min, double[] max, double[] width )
{
Envelope env = new Envelope( min, max );
Expand Down
Expand Up @@ -1006,49 +1006,6 @@ public void shouldNotStepMoreThanDistanceOneMoreThan10Percent()
}
}

@Test
public void testEnsureSideRatioNotTooSmall2D()
{
// No change expected
double[] from = new double[] {0, 0};
double[] to = new double[] {1, 1};

SpaceFillingCurve.ensureSideRatioNotTooSmall( from, to );
double[] expectedFrom = new double[] {0, 0};
double[] expectedTo = new double[] {1, 1};
assertArrayEquals( expectedFrom, from , 0);
assertArrayEquals( expectedTo, to, 0 );

// Expected to change
to = new double[] {100, 0.1};
SpaceFillingCurve.ensureSideRatioNotTooSmall( from, to );
double[] expectedTo2 = new double[] {100, 1};
assertArrayEquals( expectedFrom, from , 0);
assertArrayEquals( expectedTo2, to, 0.00001 );
}

// Works for any number of dimensions, and 4 is more interesting than 3
@Test
public void testEnsureSideRatioNotTooSmall4D()
{
// No change expected
double[] from = new double[] {0, 0, 0, 0};
double[] to = new double[] {1, 1, 1, 1};

SpaceFillingCurve.ensureSideRatioNotTooSmall( from, to );
double[] expectedFrom = new double[] {0, 0, 0, 0};
double[] expectedTo = new double[] {1, 1, 1, 1};
assertArrayEquals( expectedFrom, from , 0);
assertArrayEquals( expectedTo, to, 0 );

// Expected to change
to = new double[] {100, 0.1, 12, 0.01};
SpaceFillingCurve.ensureSideRatioNotTooSmall( from, to );
double[] expectedTo2 = new double[] {100, 1, 12, 1};
assertArrayEquals( expectedFrom, from , 0);
assertArrayEquals( expectedTo2, to, 0.00001 );
}

private void shouldNeverStepMoreThanDistanceOne( SpaceFillingCurve curve, int level, int badnessThresholdPercentage )
{
int badCount = 0;
Expand Down

0 comments on commit 674e9c0

Please sign in to comment.