Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManualTransformationEditor with a custom set of sources #169

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 113 additions & 60 deletions src/main/java/bdv/tools/transformation/ManualTransformationEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.Consumer;

import javax.swing.Action;
Expand Down Expand Up @@ -64,8 +66,6 @@ public class ManualTransformationEditor implements TransformListener< AffineTran

private final ArrayList< TransformedSource< ? > > sourcesToFix;

private final ActionMap actionMap;

private final InputMap inputMap;

private final Listeners.List< ManualTransformActiveListener > manualTransformActiveListeners;
Expand Down Expand Up @@ -112,7 +112,7 @@ public ManualTransformationEditor(
final Action abortAction = new RunnableAction( "abort manual transformation", this::abort );
final KeyStroke resetKey = KeyStroke.getKeyStroke( KeyEvent.VK_R, 0 );
final Action resetAction = new RunnableAction( "reset manual transformation", this::reset );
actionMap = new ActionMap();
final ActionMap actionMap = new ActionMap();
inputMap = new InputMap();
actionMap.put( "abort manual transformation", abortAction );
inputMap.put( abortKey, "abort manual transformation" );
Expand All @@ -121,20 +121,75 @@ public ManualTransformationEditor(
bindings.addActionMap( "manual transform", actionMap );
}

public synchronized void abort()
/**
* Get the set of current sources (in the given {@code state}). This
* contains all sources in the current group if grouping is active, and the
* single current source otherwise.
*
* @return the set of current sources
*/
private static Set< SourceAndConverter< ? > > getCurrentSources( final ViewerState state )
{
if ( state.getDisplayMode().hasGrouping() )
return state.getSourcesInGroup( state.getCurrentGroup() );
else
return Collections.singleton( state.getCurrentSource() );
}

/**
* Initiate a manual transformation modifying the given {@code
* sourcesToTransform}. If {@code sourcesToTransform == null}, use the
* current source (or source group).
*/
public synchronized void transform( Collection< SourceAndConverter< ? > > sourcesToTransform )
{
if ( active )
{
final AffineTransform3D identity = new AffineTransform3D();
for ( final TransformedSource< ? > source : sourcesToModify )
source.setIncrementalTransform( identity );
viewerState.setViewerTransform( frozenTransform );
viewerMessageDisplay.accept( "aborted manual transform" );
active = false;
manualTransformActiveListeners.list.forEach( l -> l.manualTransformActiveChanged( active ) );
// if there is an on-going manual transformation, abort it first.
abort();
}

// Enter manual edit mode
final ViewerState state = this.viewerState.snapshot();
if ( sourcesToTransform == null )
{
if ( !state.getDisplayMode().hasFused() )
{
// NB: If a non-null collection of sourcesToTransform was
// passed, we assume that the caller knows what they are doing.
// Otherwise, if not in a FUSED display mode, abort.
viewerMessageDisplay.accept( "Can only do manual transformation when in FUSED mode." );
return;
}
sourcesToTransform = getCurrentSources( state );
}
state.getViewerTransform( frozenTransform );
sourcesToModify.clear();
sourcesToFix.clear();
for ( final SourceAndConverter< ? > source : state.getSources() )
{
if ( source.getSpimSource() instanceof TransformedSource )
{
if ( sourcesToTransform.contains( source ) )
sourcesToModify.add( ( TransformedSource< ? > ) source.getSpimSource() );
else
sourcesToFix.add( ( TransformedSource< ? > ) source.getSpimSource() );
}
}
viewerTransformListeners.add( this );
bindings.addInputMap( "manual transform", inputMap );
viewerMessageDisplay.accept( "starting manual transform" );

active = true;
manualTransformActiveListeners.list.forEach( l -> l.manualTransformActiveChanged( active ) );
}

/**
* During an ongoing manual transformation, reset the transforming sources
* to their original transformations. Note that this will discard previous
* manual transformations that were already applied to the transforming
* sources. The ongoing manual transformation is not terminated by this.
*/
public synchronized void reset()
{
if ( active )
Expand All @@ -154,52 +209,15 @@ public synchronized void reset()
}
}

public synchronized void setActive( final boolean a )
/**
* End the ongoing manual transformation and fix the incremental
* transformation.
*/
public synchronized void apply()
{
if ( this.active == a )
return;

if ( a )
{
// Enter manual edit mode
final ViewerState state = this.viewerState.snapshot();
final List< SourceAndConverter< ? > > currentSources = new ArrayList<>();
switch ( state.getDisplayMode() )
{
case FUSED:
currentSources.add( state.getCurrentSource() );
break;
case FUSEDGROUP:
currentSources.addAll( state.getSourcesInGroup( state.getCurrentGroup() ) );
break;
default:
viewerMessageDisplay.accept( "Can only do manual transformation when in FUSED mode." );
return;
}
state.getViewerTransform( frozenTransform );
sourcesToModify.clear();
sourcesToFix.clear();
for ( final SourceAndConverter< ? > source : state.getSources() )
{
if ( source.getSpimSource() instanceof TransformedSource )
{
if ( currentSources.contains( source ) )
sourcesToModify.add( ( TransformedSource< ? > ) source.getSpimSource() );
else
sourcesToFix.add( ( TransformedSource< ? > ) source.getSpimSource() );
}
}
active = true;
viewerTransformListeners.add( this );
bindings.addInputMap( "manual transform", inputMap );
viewerMessageDisplay.accept( "starting manual transform" );
}
else
if ( active )
{
// Exit manual edit mode.
active = false;
viewerTransformListeners.remove( this );
bindings.removeInputMap( "manual transform" );
final AffineTransform3D tmp = new AffineTransform3D();
for ( final TransformedSource< ? > source : sourcesToModify )
{
Expand All @@ -212,12 +230,49 @@ public synchronized void setActive( final boolean a )
tmp.identity();
for ( final TransformedSource< ? > source : sourcesToFix )
source.setIncrementalTransform( tmp );
viewerState.setViewerTransform( frozenTransform );
viewerMessageDisplay.accept( "fixed manual transform" );

terminate( "fixed manual transform" );
}
}

/**
* End the ongoing manual transformation and discard the incremental
* transformation.
*/
public synchronized void abort()
{
if ( active )
{
final AffineTransform3D identity = new AffineTransform3D();
for ( final TransformedSource< ? > source : sourcesToModify )
source.setIncrementalTransform( identity );

terminate("aborted manual transform");
}
}

private void terminate( final String message )
{
viewerTransformListeners.remove( this );
bindings.removeInputMap( "manual transform" );
viewerState.setViewerTransform( frozenTransform );
active = false;
if ( message != null )
viewerMessageDisplay.accept( message );
manualTransformActiveListeners.list.forEach( l -> l.manualTransformActiveChanged( active ) );
}

public synchronized void setActive( final boolean a )
{
if ( this.active == a )
return;

if ( a )
transform( null );
else
apply();
}

public synchronized void toggle()
{
setActive( !active );
Expand All @@ -233,9 +288,7 @@ public void transformChanged( final AffineTransform3D transform )

liveTransform.set( transform );
liveTransform.preConcatenate( frozenTransform.inverse() );

for ( final TransformedSource< ? > source : sourcesToFix )
source.setIncrementalTransform( liveTransform.inverse() );
sourcesToFix.forEach( s -> s.setIncrementalTransform( liveTransform.inverse() ) );
}

public Listeners< ManualTransformActiveListener > manualTransformActiveListeners()
Expand Down
Loading