Permalink
Browse files

Add tests for interval method

  • Loading branch information...
awalter17 committed Jan 6, 2017
1 parent 23d9ec6 commit c366632b0a00757bd9450d2749fe91a8b189b243
Showing with 153 additions and 0 deletions.
  1. +153 −0 src/test/java/net/imglib2/roi/geometric/GeometricShapeTest.java
@@ -5,14 +5,19 @@
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.imglib2.Cursor;
import net.imglib2.RandomAccess;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealRandomAccess;
import net.imglib2.RealRandomAccessibleRealInterval;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineRealRandomAccessible;
import net.imglib2.realtransform.AffineTransform;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.roi.IterableRegion;
@@ -129,6 +134,27 @@ public void testTwoDimensionalRotatedRectangle()
assertTrue( hcra.get().get() );
}
@Test
public void testTwoDimensionalRotatedRectangleInterval()
{
double angle = 270.0 / 180.0 * Math.PI;
double[][] rotationMatrix = {
{ Math.cos( angle ), -Math.sin( angle ) },
{ Math.sin( angle ), Math.cos( angle ) }
};
AffineRealRandomAccessible< BoolType, AffineGet > hc = GeomRegions.hyperRectangle( new RealPoint( new double[] { 4.5, 4.5 } ), new double[] { 2, 3 }, rotationMatrix );
HyperRectangle hr = ( HyperRectangle ) hc.getSource();
RealRandomAccessibleRealInterval< BoolType > arra = Regions.interval( hc, getCorners( hr.semiAxisLengths, hr.position, ( AffineTransform ) hc.getTransformToSource().inverse() ) );
// check interval boundaries
assertEquals( arra.realMax( 0 ), hr.semiAxisLengths[ 1 ] + hr.position[ 0 ], 0 );
assertEquals( arra.realMax( 1 ), hr.semiAxisLengths[ 0 ] + hr.position[ 1 ], 0 );
assertEquals( arra.realMin( 0 ), -hr.semiAxisLengths[ 1 ] + hr.position[ 0 ], 0.000000000000001 );
assertEquals( arra.realMin( 1 ), -hr.semiAxisLengths[ 0 ] + hr.position[ 1 ], 0.00000000000001 );
}
@Test
public void testThreeDimensionalRotatedRectangle()
{
@@ -193,6 +219,31 @@ public void testThreeDimensionalRotatedRectangle()
assertTrue( ora.get().get() );
}
@Test
public void testThreeDimensionalRotatedRectangleInterval()
{
double angle = 30.0 / 180.0 * Math.PI;
double[][] rotationMatrix = {
{ Math.cos( angle ), 0, Math.sin( angle ) },
{ 0, 1, 0 },
{ -Math.sin( angle ), 0, Math.cos( angle ) }
};
// rotated using GeomRegions with AffineTransform
AffineRealRandomAccessible< BoolType, AffineGet > hc = GeomRegions.hyperRectangle( new RealPoint( new double[] { 3, 7, 1 } ), new double[] { 2, 1.25, 5 }, rotationMatrix );
HyperRectangle hr = ( HyperRectangle) hc.getSource();
RealRandomAccessibleRealInterval< BoolType > arra = Regions.interval( hc, getCorners( hr.semiAxisLengths, hr.position, ( AffineTransform ) hc.getTransformToSource().inverse() ) );
// check interval bounds
assertEquals( arra.realMax( 0 ), hr.semiAxisLengths[ 0 ] * Math.cos( angle ) + hr.semiAxisLengths[ 2 ] * Math.sin( angle ) + hr.position[ 0 ], 0 );
assertEquals( arra.realMax( 1 ), hr.semiAxisLengths[ 1 ] + hr.position[ 1 ], 0 );
assertEquals( arra.realMax( 2 ), -hr.semiAxisLengths[ 0 ] * -Math.sin( angle ) + hr.semiAxisLengths[ 2 ] * Math.cos( angle ) + hr.position[ 2 ], 0.000000000000001 );
assertEquals( arra.realMin( 0 ), -hr.semiAxisLengths[ 0 ] * Math.cos( angle ) + -hr.semiAxisLengths[ 2 ] * Math.sin( angle ) + hr.position[ 0 ], 0.000000000000001 );
assertEquals( arra.realMin( 1 ), -hr.semiAxisLengths[ 1 ] + hr.position[ 1 ], 0 );
assertEquals( arra.realMin( 2 ), hr.semiAxisLengths[ 0 ] * -Math.sin( angle ) + -hr.semiAxisLengths[ 2 ] * Math.cos( angle ) + hr.position[ 2 ], 0.000000000000001 );
}
@Test
public void testHyperCubeWithoutVolume()
{
@@ -303,6 +354,19 @@ public void testRotated2DEllipse()
assertFalse( ra.get().get() );
}
@Test
public void testRotated0EllipseInterval()
{
AffineRealRandomAccessible< BoolType, AffineGet > he = GeomRegions.hyperEllipsoid( new RealPoint( new double[]{ 10, 10 } ), new double[]{ 8, 8 }, new double[][] { { 1,0 }, { 0,1 } }, 1.0 );
RealRandomAccessibleRealInterval< BoolType > arra = Regions.interval( he, getFurthestPoints( ( ( HyperEllipsoid ) he.getSource() ).semiAxisLengths, ( ( HyperEllipsoid ) he.getSource() ).position, ( AffineTransform ) he.getTransformToSource().inverse() ) );
// check interval boundaries
assertEquals( arra.realMax( 0 ), 18, 0 );
assertEquals( arra.realMax( 1 ), 18, 0 );
assertEquals( arra.realMin( 0 ), 2, 0 );
assertEquals( arra.realMin( 1 ), 2, 0 );
}
@Test
public void testRotated45Ellipse()
{
@@ -328,6 +392,20 @@ public void testRotated45Ellipse()
assertFalse( ra.get().get() );
}
@Test
public void testRotated45EllipseInterval()
{
final double angle = Math.PI * 45.0 / 180.0;
final AffineRealRandomAccessible< BoolType, AffineGet > he = GeomRegions.hyperEllipsoid( new RealPoint( new double[] { 10, 10 } ), new double[] { 2, 8 }, new double[][] { { Math.cos( angle ), Math.sin( angle ) },{ -Math.sin( angle ),Math.cos( angle ) } }, 0.5 );
final RealRandomAccessibleRealInterval< BoolType > arra = Regions.interval( he, getFurthestPoints( ( ( HyperEllipsoid ) he.getSource() ).semiAxisLengths, ( ( HyperEllipsoid ) he.getSource() ).position, ( AffineTransform ) he.getTransformToSource().inverse() ) );
// check interval boundaries
assertEquals( arra.realMax( 0 ), 8 * Math.sin( angle ) + 10, 0 );
assertEquals( arra.realMax( 1 ), 8 * Math.cos( angle ) + 10, 0.00000000000001 );
assertEquals( arra.realMin( 0 ), 8 * -Math.sin( angle ) + 10, 0 );
assertEquals( arra.realMin( 1 ), -8 * Math.cos( angle ) + 10, 0.000000000000001 );
}
@Test
public void testIfUnionWorksProperly()
{
@@ -618,4 +696,79 @@ private String getGeometricShapeAsAsciiArt( final RealRandomAccessibleRealInterv
}
return res;
}
/* Get an interval containing the transformed corners for a hyper-rectangle */
private static RealInterval getCorners( final double[] semiAxes, final double[] center, final AffineTransform transform )
{
final int dim = center.length;
final List< RealLocalizable > corners = new ArrayList< RealLocalizable >();
final double numCorners = Math.pow( 2, dim );
for ( int p = 0; p < numCorners; p++)
{
corners.add( new RealPoint( dim ) );
}
double change = numCorners / 2;
for ( int n = 0; n < dim; n++ )
{
double axis = semiAxes[ n ];
for ( int p = 0; p < numCorners; p++ )
{
if( p % change == 0 ) axis = - axis;
RealPoint vert = ( RealPoint ) corners.get( p );
vert.setPosition( center[ n ] + axis, n );
}
change = change / 2;
}
for ( int p = 0; p < numCorners; p++ )
{
RealPoint vert = ( RealPoint ) corners.get( p );
double[] source = new double[ dim ];
double[] target = new double[ dim ];
vert.localize( source );
transform.apply( source, target );
vert.setPosition( target );
}
return ROIUtils.getBoundsReal( corners );
}
/* Calculates the points on the hyper-ellipse edge which are farthest from the center in each dimension, transforms these
* points, and then returns an interval based on these points. */
private static RealInterval getFurthestPoints( final double[] semiAxes, final double[] center, final AffineTransform transform )
{
final int dim = center.length;
final List< RealLocalizable > points = new ArrayList< RealLocalizable >();
final double numPoints = 2 * dim;
int pos = 0;
double axis = -semiAxes[ pos ];
for ( int n = 0; n < numPoints; n++ )
{
if( n != 0 && n % 2 == 0 )
{
pos++;
axis = semiAxes[ pos ];
}
else axis = - axis;
double[] source = new double[ dim ];
for( int i = 0; i < dim; i++ )
{
if ( i == pos ) source[ i ] = axis + center[ i ];
else source[ i ] = center[ i ];
}
double[] target = new double[ dim ];
transform.apply( source, target );
points.add( new RealPoint( target ) );
}
return ROIUtils.getBoundsReal( points );
}
}

0 comments on commit c366632

Please sign in to comment.