Permalink
Browse files

Add GeomRegions, using AffineRealRandomAccessible for rotation

The ability to create rotated HyperRectangles and HyperEllipsoids is
done via RealViews here. The rotation functionality for both of those
will be removed soon. GeomRegions has convenience methods for creating
rotated HyperRectangles and HyperEllipsoids, which actually create
AffineRealRandomAccessibles.
  • Loading branch information...
awalter17 committed Oct 19, 2016
1 parent 692c43a commit e2ae88a6710d70303a5bb2bc0d8f6853fdca99a9
Showing with 136 additions and 0 deletions.
  1. +136 −0 src/main/java/net/imglib2/roi/geometric/GeomRegions.java
@@ -0,0 +1,136 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2016 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2.roi.geometric;
import java.util.List;
import net.imglib2.RealLocalizable;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineRealRandomAccessible;
import net.imglib2.realtransform.AffineTransform;
import net.imglib2.realtransform.RealViews;
import net.imglib2.type.logic.BoolType;
public class GeomRegions
{
// -- Create 2D polygon --
public static Polygon2D polygon2D( final List< ? extends RealLocalizable > vertices )
{
return new Polygon2D( vertices );
}
// -- Create HyperRectangle --
public static HyperRectangle hyperRectangle( final int dim )
{
return new HyperRectangle( dim );
}
public static HyperRectangle hyperRectangle( final RealLocalizable center, final double[] semiAxisLengths )
{
return new HyperRectangle( center, semiAxisLengths );
}
public static AffineRealRandomAccessible< BoolType, AffineGet > hyperRectangle( final RealLocalizable center, final double[] semiAxisLengths, final double[][] rotationMatrix )
{
return RealViews.affineReal( hyperRectangle( center, semiAxisLengths ), createAffineMatrix( center, rotationMatrix, semiAxisLengths.length ) );
}
// -- Create HyperEllipsoid --
public static HyperEllipsoid hyperEllipsoid( final int dim )
{
return new HyperEllipsoid( dim );
}
public static HyperEllipsoid hyperEllipsoid( final RealLocalizable center, final double[] semiAxisLengths )
{
return new HyperEllipsoid( center, semiAxisLengths );
}
public static HyperEllipsoid hyperEllipsoid( final RealLocalizable center, final double[] semiAxisLengths, final double exponent )
{
return new HyperEllipsoid( center, semiAxisLengths, exponent );
}
public static AffineRealRandomAccessible< BoolType, AffineGet > hyperEllipsoid( final RealLocalizable center, final double[] semiAxisLengths, final double[][] rotationMatrix )
{
return RealViews.affineReal( hyperEllipsoid( center, semiAxisLengths ), createAffineMatrix( center, rotationMatrix, semiAxisLengths.length ) );
}
public static AffineRealRandomAccessible< BoolType, AffineGet > hyperEllipsoid( final RealLocalizable center, final double[] semiAxisLengths, final double[][] rotationMatrix, final double exponent )
{
return RealViews.affineReal( hyperEllipsoid( center, semiAxisLengths, exponent ), createAffineMatrix( center, rotationMatrix, semiAxisLengths.length ) );
}
// -- Helper methods --
private static AffineGet createAffineMatrix( final RealLocalizable center, final double[][] rotationMatrix, final int dim )
{
assert rotationMatrix.length == dim;
assert rotationMatrix[ 0 ].length == dim;
final AffineTransform affine = new AffineTransform( dim );
final double[][] transform = new double[ dim ][ dim + 1 ];
assert rotationMatrix[0].length == dim;
for ( int i = 0; i < dim; i++ )
{
double translate = 0;
for ( int j = 0; j < dim + 1; j++ )
{
if ( i < rotationMatrix.length && j < rotationMatrix[ i ].length )
{
transform[ i ][ j ] = rotationMatrix[ i ][ j ];
translate += transform[ i ][ j ] * -center.getDoublePosition( j );
}
if ( j == dim )
{
transform[ i ][ j ] = translate;
}
}
}
for ( int n = 0; n < dim; n++ )
{
transform[ n ][ dim ] += center.getDoublePosition( n );
}
affine.set( transform );
return affine;
}
}

0 comments on commit e2ae88a

Please sign in to comment.