Skip to content

Commit

Permalink
TransformationModel: add method setTransformationForInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
maarzt committed Feb 12, 2018
1 parent b10779d commit 0a18618
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 67 deletions.
20 changes: 1 addition & 19 deletions src/main/java/net/imglib2/labkit/actions/OrthogonalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,7 @@ public class OrthogonalView {
public OrthogonalView( Extensible extensible, ImageLabelingModel model ) {
extensible.addAction("Orthogonal View", "resetView", () -> {
TransformationModel transformationModel = model.transformationModel();
AffineTransform3D transformation = initialTransformation( transformationModel, model.spatialDimensions() );
transformationModel.setTransformation( transformation );
transformationModel.transformToShowInterval( model.image() );
}, "");
}

private AffineTransform3D initialTransformation( TransformationModel transformationModel, Dimensions dimensions )
{
double imageWidth = dimensions.dimension( 0 );
double imageHeight = dimensions.dimension( 1 );
double imageDepth = dimensions.numDimensions() > 2 ? dimensions.dimension( 2 ) : 0;
double screenWidth = transformationModel.width();
double screenHeight = transformationModel.height();
double zoom = Math.min( screenWidth / imageWidth, screenHeight / imageHeight);
AffineTransform3D transformation = new AffineTransform3D();
transformation.set(
zoom, 0, 0, (screenWidth - imageWidth * zoom) * 0.5,
0, zoom, 0, ( screenHeight - imageHeight * zoom) * 0.5,
0, 0, zoom, - imageDepth * zoom * 0.5
);
return transformation;
}
}
63 changes: 15 additions & 48 deletions src/main/java/net/imglib2/labkit/models/ColoredLabelsModel.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.imglib2.labkit.models;

import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.labkit.color.ColorMap;
import net.imglib2.labkit.labeling.Labeling;
import net.imglib2.labkit.utils.Notifier;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.roi.IterableRegion;
import net.imglib2.transform.integer.BoundingBox;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.ARGBType;

Expand Down Expand Up @@ -96,64 +97,30 @@ private String suggestName(List<String> labels) {
}

public void localizeLabel( final String label ) {
final BoundingBox labelBox = getBoundingBox( model.labeling().get().iterableRegions().get( label ) );
if ( labelBox != null ) {
final AffineTransform3D transform = getTransformation( labelBox );
model.transformationModel().setTransformation( transform );
}
final Interval labelBox = getBoundingBox( model.labeling().get().iterableRegions().get( label ) );
if ( labelBox != null )
model.transformationModel().transformToShowInterval( labelBox );
}

private AffineTransform3D getTransformation( BoundingBox labelBox )
{
final double[] screenSize = { model.transformationModel().width(), model.transformationModel().height() };
final double scale = 0.5 * getBiggestScaleFactor( screenSize, labelBox );
final double[] translate = getTranslation( screenSize, labelBox, scale );
final AffineTransform3D transform = new AffineTransform3D();
transform.scale( scale );
transform.translate( translate );
return transform;
}

private static double[] getTranslation( final double[] screenSize, final BoundingBox labelBox, final double labelScale ) {
final double[] translate = new double[ 3 ];
for ( int i = 0; i < Math.min( translate.length, labelBox.numDimensions() ); i++ ) {
translate[ i ] = - ( labelBox.corner2[ i ] + labelBox.corner1[ i ] ) * labelScale / 2;
if ( i < 2 ) {
translate[ i ] += screenSize[ i ] / 2;
}
}
return translate;
}

private static double getBiggestScaleFactor( final double[] screenSize, final BoundingBox labelBox ) {
final Double[] scales = new Double[ 2 ];
final double minLength = 20.0;
for ( int i = 0; i < 2; i++ ) {
scales[ i ] = screenSize[ i ] / Math.max( labelBox.corner2[ i ] - labelBox.corner1[ i ], minLength );
}
return Collections.min( Arrays.asList( scales ) );
}

private static BoundingBox getBoundingBox( IterableRegion< BitType > region )
private static Interval getBoundingBox( IterableRegion< BitType > region )
{
int numDimensions = region.numDimensions();
BoundingBox box = new BoundingBox( numDimensions );
Cursor<?> cursor = region.cursor();
if ( cursor.hasNext() ) {
cursor.fwd();
cursor.localize( box.corner1 );
cursor.localize( box.corner2 );
}
else
if ( ! cursor.hasNext() )
return null;
long[] min = new long[ numDimensions ];
long[] max = new long[ numDimensions ];
cursor.fwd();
cursor.localize( min );
cursor.localize( max );
while( cursor.hasNext() ) {
cursor.fwd();
for( int i = 0; i < numDimensions; i++){
int pos = cursor.getIntPosition( i );
box.corner1[ i ] = Math.min( box.corner1[ i ], pos);
box.corner2[ i ] = Math.max( box.corner2[ i ], pos);
min[ i ] = Math.min( min[ i ], pos);
max[ i ] = Math.max( max[ i ], pos);
}
}
return box;
return new FinalInterval( min, max );
}
}
38 changes: 38 additions & 0 deletions src/main/java/net/imglib2/labkit/models/TransformationModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.imglib2.labkit.models;

import bdv.viewer.ViewerPanel;
import net.imglib2.Interval;
import net.imglib2.realtransform.AffineTransform3D;

import java.util.Arrays;
import java.util.Collections;

public class TransformationModel
{
private ViewerPanel viewerPanel;
Expand All @@ -26,4 +30,38 @@ public void setTransformation( AffineTransform3D transformation )
if(viewerPanel != null)
viewerPanel.setCurrentViewerTransform( transformation );
}

public void transformToShowInterval( Interval interval ) {
final double[] screenSize = { width(), height() };
setTransformation( getTransformation( interval, screenSize ) );
}

private static AffineTransform3D getTransformation( Interval interval, double[] screenSize )
{
final double scale = 0.5 * getBiggestScaleFactor( screenSize, interval );
final double[] translate = getTranslation( screenSize, interval, scale );
final AffineTransform3D transform = new AffineTransform3D();
transform.scale( scale );
transform.translate( translate );
return transform;
}

private static double[] getTranslation( final double[] screenSize, final Interval labelBox, final double labelScale ) {
final double[] translate = new double[ 3 ];
for ( int i = 0; i < Math.min( translate.length, labelBox.numDimensions() ); i++ ) {
translate[ i ] = - ( labelBox.min( i ) + labelBox.max( i ) ) * labelScale / 2;
if ( i < 2 ) {
translate[ i ] += screenSize[ i ] / 2;
}
}
return translate;
}

private static double getBiggestScaleFactor( final double[] screenSize, final Interval labelBox ) {
final Double[] scales = new Double[ 2 ];
final double minLength = 20.0;
for ( int i = 0; i < 2; i++ )
scales[ i ] = screenSize[ i ] / Math.max( labelBox.max( i ) - labelBox.min( i ), minLength );
return Collections.min( Arrays.asList( scales ) );
}
}

0 comments on commit 0a18618

Please sign in to comment.