Permalink
Browse files

Update tests to use new rotation scheme

  • Loading branch information...
awalter17 committed Nov 30, 2016
1 parent e2ae88a commit 046654550f02291afb49f0c773ff45fad4b85445
Showing with 169 additions and 91 deletions.
  1. +169 −91 src/test/java/net/imglib2/roi/geometric/GeometricShapeTest.java
@@ -1,30 +1,26 @@
package net.imglib2.roi.geometric;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.Test;
import net.imglib2.Interval;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealPoint;
import net.imglib2.RealRandomAccess;
import net.imglib2.RealRandomAccessible;
import net.imglib2.RealRandomAccessibleRealInterval;
import net.imglib2.roi.operators.RealBinarySubtraction;
import net.imglib2.roi.IterableRegion;
import net.imglib2.roi.Regions;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineRealRandomAccessible;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.roi.operators.RealBinaryExclusiveOr;
import net.imglib2.roi.operators.RealBinaryIntersection;
import net.imglib2.roi.operators.RealUnaryNot;
import net.imglib2.roi.operators.RealBinarySubtraction;
import net.imglib2.roi.operators.RealBinaryUnion;
import net.imglib2.type.logic.BoolType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
import org.junit.Test;
/**
* Tests for elipsoids, rectangles (in n dimensions) and logical operations
@@ -35,7 +31,7 @@
*/
public class GeometricShapeTest {
//@Test
@Test
public void testHighDimensionalHypercube() {
AbstractGeometricShape hc = new HyperRectangle(new RealPoint( new double[]{5, 5, 5, 5} ), new double[]{2,2,2,2});
@@ -63,41 +59,130 @@ public void testHighDimensionalHypercube() {
assertTrue(!hc.contains(new RealPoint(new double[]{2,3,3,3})));
assertTrue(!hc.contains(new RealPoint(new double[]{3,2,3,3})));
}
@Test
public void testTwoDimensionalRectangle()
{
HyperRectangle hc = new HyperRectangle( new RealPoint( new double[] { 3.2, 0.0125 } ), new double[] { 10, 3.25 } );
RealRandomAccess< BoolType > rra = hc.realRandomAccess();
// inside
rra.setPosition( new double[] { 13.1, 3.2625 } );
assertTrue( rra.get().get() );
rra.setPosition( new double[] { 6, 1 } );
assertTrue( rra.get().get() );
// outside
rra.setPosition( new double[] { -7.9, 0.0125 } );
assertFalse( rra.get().get() );
rra.setPosition( new double[] { 0, 3.263 } );
assertFalse( rra.get().get() );
}
@Test
public void testTwoDimensionalRotatedRectangle()
{
double angle = 270.0 / 180.0 * Math.PI;
double[][] rotationMatrix = {
{Math.cos(angle), -Math.sin(angle) },
{Math.sin(angle) , Math.cos(angle)}
{ Math.cos( angle ), -Math.sin( angle ) },
{ Math.sin( angle ), Math.cos( angle ) }
};
AbstractGeometricShape hc = new HyperRectangle(new RealPoint( new double[]{4.5, 4.5} ), new double[]{2,3}, rotationMatrix);
long[] minmax = new long[] { 0,0,10,10 };
Interval interval = Intervals.createMinMax(minmax);
System.out.println("min/max y: " + hc.realMin(0) + "/" + hc.realMax(0));
System.out.println(getGeometricShapeAsAsciiArt(hc));
// shift it by 0.5 pixel to the right
hc.move(0.5, 0);
System.out.println("min/max y: " + hc.realMin(0) + "/" + hc.realMax(0));
System.out.println(getGeometricShapeAsAsciiArt(hc));
// Just test if the following compiles...
RealRandomAccess<BoolType> rra = hc.realRandomAccess();
RandomAccessible<BoolType> ra = Views.raster(hc);
RandomAccessibleInterval<BoolType> rai = Views.interval(ra, interval);
rra.numDimensions();
rai.numDimensions();
AffineRealRandomAccessible< BoolType, AffineGet > hc = GeomRegions.hyperRectangle( new RealPoint( new double[]{ 4.5, 4.5 } ), new double[]{ 2, 3 }, rotationMatrix );
assertTrue( hc.numDimensions() == 2 );
assertTrue( HyperRectangle.class.isInstance( hc.getSource() ) );
HyperRectangle hr = ( HyperRectangle ) hc.getSource();
RealRandomAccess< BoolType > hcra = hc.realRandomAccess();
RealRandomAccess< BoolType > hrra = hr.realRandomAccess();
// Check region contains points post rotation
// center
hrra.setPosition( new double[] { 4.5, 4.5 } );
hcra.setPosition( new double[] { 4.5, 4.5 } );
assertTrue( hrra.get().get() );
assertTrue( hcra.get().get() );
// Inside original rectangle but not rotated
hrra.setPosition( new double[] { 6.45, 7.3 } );
hcra.setPosition( new double[] { 6.45, 7.3 } );
assertTrue( hrra.get().get() );
assertFalse( hcra.get().get() );
// Inside rotated rectangle but not original
hrra.setPosition( new double[] { 7.3, 6.45 } );
hcra.setPosition( new double[] { 7.3, 6.45 } );
assertFalse( hrra.get().get() );
assertTrue( hcra.get().get() );
}
@Test
public void testThreeDimensionalRotatedRectangle()
{
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 );
assertTrue( hc.numDimensions() == 3 );
assertTrue( HyperRectangle.class.isInstance( hc.getSource() ) );
// Non-rotated source HyperRectangle
HyperRectangle hr = ( HyperRectangle) hc.getSource();
// Create same rotated rectangle using AffineTransform3D
AffineTransform3D affine = new AffineTransform3D();
affine.translate( new double[] { -3, -7, -1 } );
affine.rotate( 1, angle );
affine.translate( new double[] { 3, 7, 1 } );
AffineRealRandomAccessible< BoolType, AffineGet > o = RealViews.affineReal( hr, affine );
// RealRandomAccess for each HyperRectangle
RealRandomAccess< BoolType > rra = hc.realRandomAccess();
RealRandomAccess< BoolType > hrra = hr.realRandomAccess();
RealRandomAccess< BoolType > ora = o.realRandomAccess();
for ( int i = 0; i < affine.numDimensions(); i++ )
{
for ( int j = 0; j <= affine.numDimensions(); j++ )
{
assertEquals( o.getTransformToSource().get( i, j ), hc.getTransformToSource().get( i, j ), 0.0000000000001 );
}
}
// inside both
hrra.setPosition( new double[]{ 3.5, 6.1, 2 } );
rra.setPosition( new double[]{ 3.5, 6.1, 2 } );
ora.setPosition( new double[]{ 3.5, 6.1, 2 } );
assertTrue( hrra.get().get() );
assertTrue( rra.get().get() );
assertTrue( ora.get().get() );
// inside original only
hrra.setPosition( new double[] { 4.99, 8, 5.93 } );
rra.setPosition( new double[] { 4.99, 8, 5.93 } );
ora.setPosition( new double[] { 4.99, 8, 5.93 } );
assertTrue( hrra.get().get() );
assertFalse( rra.get().get() );
assertFalse( ora.get().get() );
// inside rotated only
hrra.setPosition( new double[] { 7.15374953738, 8, 4.29450524066 } );
rra.setPosition( new double[] { 7.15374953738, 8, 4.29450524066 } );
ora.setPosition( new double[] { 7.15374953738, 8, 4.29450524066 } );
assertFalse( hrra.get().get() );
assertTrue( rra.get().get() );
assertTrue( ora.get().get() );
}
@Test
@@ -126,7 +211,6 @@ public void testHyperEllipsoid()
" _ _ _ _ _\n" +
" _ _ _ _ _\n" +
" _ _ _ _ _\n";
System.out.println(result);
assertTrue(reference.compareTo(result) == 0);
System.out.println("\n");
@@ -157,6 +241,7 @@ public void testHyperEllipsoid()
System.out.println(result);
assertTrue(reference.compareTo(result) == 0);
System.out.println("\n");
hc = new HyperEllipsoid(new RealPoint( new double[]{10, 10} ), new double[]{0,3});
@@ -175,58 +260,51 @@ public void testHyperEllipsoid()
System.out.println(result);
assertTrue(reference.compareTo(result) == 0);
System.out.println("\n");
hc = new HyperEllipsoid(new RealPoint( new double[]{10, 10} ), new double[]{8,8}, new double[][]{{1,0},{0,1}}, 1.0);
result = getGeometricShapeAsAsciiArt(hc);
reference = " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ # _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ # # # _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ # # # # # _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ # # # # # # # _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ # # # # # # # # # _ _ _ _ _ _\n" +
" _ _ _ _ _ # # # # # # # # # # # _ _ _ _ _\n" +
" _ _ _ _ # # # # # # # # # # # # # _ _ _ _\n" +
" _ _ _ # # # # # # # # # # # # # # # _ _ _\n" +
" _ _ # # # # # # # # # # # # # # # # # _ _\n" +
" _ _ _ # # # # # # # # # # # # # # # _ _ _\n" +
" _ _ _ _ # # # # # # # # # # # # # _ _ _ _\n" +
" _ _ _ _ _ # # # # # # # # # # # _ _ _ _ _\n" +
" _ _ _ _ _ _ # # # # # # # # # _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ # # # # # # # _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ # # # # # _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ # # # _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ # _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n";
System.out.println(result);
assertTrue(reference.compareTo(result) == 0);
System.out.println("\n");
AffineRealRandomAccessible< BoolType, AffineGet > arra = GeomRegions.hyperEllipsoid( new RealPoint( new double[]{ 10, 10 } ), new double[]{ 8, 8 }, new double[][] { { 1,0 }, { 0,1 } }, 1.0 );
RealRandomAccess< BoolType > ra = arra.realRandomAccess();
// Inside ellipse
ra.setPosition( new double[] { 10, 17 } );
assertTrue( ra.get().get() );
ra.setPosition( new double[] { 17, 10 } );
assertTrue( ra.get().get() );
ra.setPosition( new double[] { 10, 10 } );
assertTrue( ra.get().get() );
// Outside ellipse
ra.setPosition( new double[] { 10, 1 } );
assertFalse( ra.get().get() );
ra.setPosition( new double[] { 0, 0 } );
assertFalse( ra.get().get() );
ra.setPosition( new double[] { 18.1, 10 } );
assertFalse( ra.get().get() );
double angle = Math.PI * 45.0 / 180.0;
hc = new HyperEllipsoid(new RealPoint( new double[]{10, 10} ), new double[]{4,4}, new double[][]{{Math.cos(angle),Math.sin(angle)},{-Math.sin(angle),Math.cos(angle)}}, 0.5);
result = getGeometricShapeAsAsciiArt(hc);
reference = " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ # _ _ _ # _ _ _ _ _\n" +
" _ _ _ _ _ _ _ # # # _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ # # # _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ # # # _ _ _ _ _ _\n" +
" _ _ _ _ _ _ # _ _ _ # _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n" +
" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n";
System.out.println(result);
assertTrue(reference.compareTo(result) == 0);
System.out.println("\n");
arra = 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 );
ra = arra.realRandomAccess();
// Inside ellipse
ra.setPosition( new double[] { 10, 10 } );
assertTrue( ra.get().get() );
ra.setPosition( new double[] { 11.0606601718, 8.93933982822 } );
assertTrue( ra.get().get() );
ra.setPosition( new double[] { 15.5861435714, 15.5861435714 } );
assertTrue( ra.get().get() );
// Outside ellipse
ra.setPosition( new double[] { 10, 17.75 } );
assertFalse( ra.get().get() );
ra.setPosition( new double[] { 12.5, 10 } );
assertFalse( ra.get().get() );
}
private String getGeometricShapeAsAsciiArt(RealRandomAccessibleRealInterval< BoolType > ags)

0 comments on commit 0466545

Please sign in to comment.