Skip to content

Commit

Permalink
Remove book-keeping of current source transformations, because they a…
Browse files Browse the repository at this point in the history
…re now part of the images
  • Loading branch information
tischi committed Oct 27, 2023
1 parent 606dcf1 commit 1135cc4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 103 deletions.
40 changes: 30 additions & 10 deletions src/main/java/org/embl/mobie/command/context/FlipSourceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@
import ij.gui.NonBlockingGenericDialog;
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.command.CommandConstants;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.image.RegionAnnotationImage;
import org.embl.mobie.lib.serialize.transformation.AffineTransformation;
import org.embl.mobie.lib.transform.TransformHelper;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand;
import sc.fiji.bdvpg.services.SourceAndConverterServices;

import java.util.Arrays;

@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Transform>Registration - Flip Current Source")
public class FlipSourceCommand implements BdvPlaygroundActionCommand
Expand All @@ -51,24 +57,38 @@ public class FlipSourceCommand implements BdvPlaygroundActionCommand
@Parameter
protected SourceAndConverter< ? >[] sourceAndConverters;

@Parameter( label = "Axis", choices = {"X", "Y", "Z"} )
public String axis;

@Override
public void run()
{
SourceAndConverter< ? > currentSource = bdvh.getViewerPanel().state().getCurrentSource();
TransformedSource< ? > source = ( TransformedSource ) currentSource.getSpimSource();

AffineTransform3D transform = new AffineTransform3D();
source.getFixedTransform( transform );
Image< ? > image = (Image< ? >) SourceAndConverterServices.getSourceAndConverterService().getMetadata( currentSource, Image.class.getName() );

// Create a flip transformation along the X-axis
AffineTransform3D flipTransform = new AffineTransform3D();
flipTransform.identity();
flipTransform.set(-1, 0, 0);
AffineTransform3D flip = new AffineTransform3D();
switch ( axis )
{
case "X":
flip.set(-1, 0, 0);
break;
case "Y":
flip.set(-1, 1, 1);
break;
case "Z":
flip.set(-1, 2, 2);
break;
}

AffineTransform3D transform = new AffineTransform3D();
double[] center = TransformHelper.getCenter( image, 0 );
transform.translate( Arrays.stream( center ).map( x -> -x ).toArray() );
transform.preConcatenate( flip );
transform.translate( center );

transform.preConcatenate( flipTransform );
source.setFixedTransform( transform );
image.transform( transform );

bdvh.getViewerPanel().requestRepaint();
int a = 1;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/embl/mobie/lib/bdv/view/SliceViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void installContextMenuAndKeyboardShortCuts( )

sacService.registerAction( SAVE_CURRENT_SETTINGS_AS_VIEW, sourceAndConverters -> {
// TODO: Maybe only do this for the sacs at the mouse position
moBIE.getViewManager().getViewsSaver().saveCurrentSettingsAsViewDialog();
moBIE.getViewManager().getViewsSaver().saveCurrentViewDialog();
} );

final Set< String > actionsKeys = sacService.getActionsKeys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ public void transform( AffineTransform3D affineTransform3D )
// TODO
// The issue is that the regions should
// just represent the location of the annotated images!?
throw new RuntimeException();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/embl/mobie/lib/table/TableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ public void mouseClicked( MouseEvent e )

if ( doubleClick )
{
// TODO don't remember what?
int a = 1;
}

Expand Down
114 changes: 30 additions & 84 deletions src/main/java/org/embl/mobie/lib/view/ViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,37 @@
import net.imglib2.roi.RealMaskRealInterval;
import net.imglib2.type.numeric.ARGBType;
import org.apache.commons.lang.ArrayUtils;
import org.embl.mobie.lib.DataStore;
import org.embl.mobie.MoBIE;
import org.embl.mobie.lib.DataStore;
import org.embl.mobie.lib.annotation.AnnotatedRegion;
import org.embl.mobie.lib.annotation.AnnotatedSegment;
import org.embl.mobie.lib.annotation.Annotation;
import org.embl.mobie.lib.bdv.ImageNameOverlay;
import org.embl.mobie.lib.bdv.view.AnnotationSliceView;
import org.embl.mobie.lib.bdv.view.ImageSliceView;
import org.embl.mobie.lib.bdv.view.SliceViewer;
import org.embl.mobie.lib.color.CategoricalAnnotationColoringModel;
import org.embl.mobie.lib.color.ColorHelper;
import org.embl.mobie.lib.color.ColoringModels;
import org.embl.mobie.lib.color.MobieColoringModel;
import org.embl.mobie.lib.color.NumericAnnotationColoringModel;
import org.embl.mobie.lib.color.*;
import org.embl.mobie.lib.color.lut.ColumnARGBLut;
import org.embl.mobie.lib.color.lut.LUTs;
import org.embl.mobie.lib.image.AnnotationImage;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.image.RegionAnnotationImage;
import org.embl.mobie.lib.image.StitchedAnnotationImage;
import org.embl.mobie.lib.image.StitchedImage;
import org.embl.mobie.lib.image.*;
import org.embl.mobie.lib.plot.ScatterPlotSettings;
import org.embl.mobie.lib.plot.ScatterPlotView;
import org.embl.mobie.lib.select.MoBIESelectionModel;
import org.embl.mobie.lib.serialize.DataSource;
import org.embl.mobie.lib.serialize.View;
import org.embl.mobie.lib.serialize.display.AbstractAnnotationDisplay;
import org.embl.mobie.lib.serialize.display.Display;
import org.embl.mobie.lib.serialize.display.ImageDisplay;
import org.embl.mobie.lib.serialize.display.RegionDisplay;
import org.embl.mobie.lib.serialize.display.SegmentationDisplay;
import org.embl.mobie.lib.serialize.display.SpotDisplay;
import org.embl.mobie.lib.serialize.transformation.AffineTransformation;
import org.embl.mobie.lib.serialize.transformation.CropTransformation;
import org.embl.mobie.lib.serialize.transformation.GridTransformation;
import org.embl.mobie.lib.serialize.transformation.MergedGridTransformation;
import org.embl.mobie.lib.serialize.transformation.TimepointsTransformation;
import org.embl.mobie.lib.serialize.transformation.Transformation;
import org.embl.mobie.lib.serialize.display.*;
import org.embl.mobie.lib.serialize.transformation.*;
import org.embl.mobie.lib.source.AnnotationType;
import org.embl.mobie.lib.image.CroppedImage;
import org.embl.mobie.lib.source.SourceHelper;
import org.embl.mobie.lib.table.AnnData;
import org.embl.mobie.lib.table.AnnotationTableModel;
import org.embl.mobie.lib.table.ConcatenatedAnnotationTableModel;
import org.embl.mobie.lib.table.RegionDisplayAnnDataCreator;
import org.embl.mobie.lib.table.TableView;
import org.embl.mobie.lib.table.*;
import org.embl.mobie.lib.transform.NormalizedAffineViewerTransform;
import org.embl.mobie.lib.transform.TransformHelper;
import org.embl.mobie.lib.transform.image.ImageTransformer;
import org.embl.mobie.lib.transform.viewer.ImageZoomViewerTransform;
import org.embl.mobie.lib.transform.viewer.MoBIEViewerTransformAdjuster;
import org.embl.mobie.lib.transform.NormalizedAffineViewerTransform;
import org.embl.mobie.lib.transform.viewer.ViewerTransform;
import org.embl.mobie.lib.transform.viewer.ViewerTransformChanger;
import org.embl.mobie.lib.transform.TransformHelper;
import org.embl.mobie.lib.transform.image.ImageTransformer;
import org.embl.mobie.lib.ui.UserInterface;
import org.embl.mobie.lib.ui.WindowArrangementHelper;
import org.embl.mobie.lib.view.save.ViewSaver;
Expand All @@ -101,12 +79,8 @@

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public class ViewManager
Expand All @@ -118,7 +92,6 @@ public class ViewManager
private final SliceViewer sliceViewer;
private final SourceAndConverterService sacService;
private List< Display > currentDisplays;
private List< Transformation > currentTransformations;
private final UniverseManager universeManager;
private final AdditionalViewsLoader additionalViewsLoader;
private final ViewSaver viewSaver;
Expand All @@ -128,7 +101,6 @@ public ViewManager( MoBIE moBIE, UserInterface userInterface, boolean is2D )
this.moBIE = moBIE;
this.userInterface = userInterface;
currentDisplays = new ArrayList<>();
currentTransformations = new ArrayList<>();
sliceViewer = new SliceViewer( moBIE, is2D );
universeManager = new UniverseManager();
additionalViewsLoader = new AdditionalViewsLoader( moBIE );
Expand Down Expand Up @@ -162,12 +134,13 @@ public SliceViewer getSliceViewer()

public ViewSaver getViewsSaver() { return viewSaver; }

private void addManualTransforms( List< Transformation > viewSourceTransforms, List< ? extends SourceAndConverter< ? >> sourceAndConverters )
private void addImageTransforms( List< Transformation > imageTransforms, List< ? extends SourceAndConverter< ? >> sourceAndConverters )
{
for ( SourceAndConverter< ? > sourceAndConverter : sourceAndConverters )
{
Source< ? > source = sourceAndConverter.getSpimSource();

// TODO: we could also refer to the Image here, not sure whether this would be better?
final TransformedSource< ? > transformedSource = SourceHelper.unwrapSource( source, TransformedSource.class );

if ( transformedSource != null )
Expand All @@ -178,49 +151,44 @@ private void addManualTransforms( List< Transformation > viewSourceTransforms, L
{
List< String > sources = new ArrayList<>();
sources.add( source.getName() );
viewSourceTransforms.add( new AffineTransformation( "manualTransform", fixedTransform.getRowPackedCopy(), sources ) );
imageTransforms.add( new AffineTransformation( "manualTransform", fixedTransform.getRowPackedCopy(), sources ) );
}
}
}
}

public View createViewFromCurrentState( String uiSelectionGroup, boolean isExclusive, boolean includeViewerTransform )
public View createCurrentView( String uiSelectionGroup, boolean isExclusive, boolean includeViewerTransform )
{
List< Transformation > viewTransformations = new ArrayList<>( currentTransformations );

// Create copies of the current displays where the
// serializable fields are properly set
List< Display< ? > > viewDisplays = new ArrayList<>();
// Create serialisable copies of the current displays
List< Display< ? > > displays = new ArrayList<>();
List< Transformation > transformations = new ArrayList<>();
for ( Display< ? > display : currentDisplays )
{
if ( display instanceof ImageDisplay )
viewDisplays.add( new ImageDisplay( ( ImageDisplay ) display ) );
displays.add( new ImageDisplay( ( ImageDisplay ) display ) );
else if ( display instanceof SegmentationDisplay )
viewDisplays.add( new SegmentationDisplay( ( SegmentationDisplay ) display ) );
displays.add( new SegmentationDisplay( ( SegmentationDisplay ) display ) );
else if ( display instanceof RegionDisplay )
viewDisplays.add( new RegionDisplay( ( RegionDisplay ) display ) );
displays.add( new RegionDisplay( ( RegionDisplay ) display ) );
else if ( display instanceof SpotDisplay )
viewDisplays.add( new SpotDisplay( ( SpotDisplay) display ) );
displays.add( new SpotDisplay( ( SpotDisplay) display ) );
else
throw new UnsupportedOperationException( "Serialisation of a " + display.getClass().getName() + " is not yet supported." );

// Add transforms that were done by the user in BDV,
// using, e.g., the manual transform or BigWarp.
// Note that other transforms are already captured
// in the {@code viewTransformations} list.
addManualTransforms( viewTransformations, display.sourceAndConverters() );
// Add image transforms
addImageTransforms( transformations, display.sourceAndConverters() );
}

if ( includeViewerTransform )
{
final BdvHandle bdvHandle = sliceViewer.getBdvHandle();
AffineTransform3D normalisedViewTransform = TransformHelper.createNormalisedViewerTransform( bdvHandle.getViewerPanel() );
final NormalizedAffineViewerTransform transform = new NormalizedAffineViewerTransform( normalisedViewTransform.getRowPackedCopy(), bdvHandle.getViewerPanel().state().getCurrentTimepoint() );
return new View( "", uiSelectionGroup, viewDisplays, viewTransformations, transform, isExclusive);
return new View( "", uiSelectionGroup, displays, transformations, transform, isExclusive);
}
else
{
return new View( "", uiSelectionGroup, viewDisplays, viewTransformations, isExclusive );
return new View( "", uiSelectionGroup, displays, transformations, isExclusive );
}
}

Expand Down Expand Up @@ -357,8 +325,6 @@ public void initData( View view )
{
for ( Transformation transformation : transformations )
{
currentTransformations.add( transformation );

if ( transformation instanceof AffineTransformation )
{
final AffineTransformation< ? > affineTransformation = ( AffineTransformation< ? > ) transformation;
Expand Down Expand Up @@ -671,7 +637,7 @@ private void initImageVolumeViewer( ImageDisplay< ? > imageDisplay )
private void initTableView( AbstractAnnotationDisplay< ? extends Annotation > display )
{
display.tableView = new TableView( display );
// TODO: Note that currently we must show the table here
// TODO: currently we must show the table here
// in order to instantiate the window.
// This window is needed in {@code UserInterfaceHelper}
// in the function {@code createWindowVisibilityCheckbox},
Expand Down Expand Up @@ -733,37 +699,17 @@ else if ( display instanceof ImageDisplay )
imageDisplay.imageVolumeViewer.close();
}


userInterface.removeDisplaySettingsPanel( display );
currentDisplays.remove( display );

updateCurrentSourceTransformers();
}

private void updateCurrentSourceTransformers()
{
// remove any sourceTransformers, where none of its relevant sources are displayed

// create a copy of the currently shown source transformers, so we don't iterate over a list that we modify
final ArrayList< Transformation > imageTransformersCopy = new ArrayList<>( this.currentTransformations ) ;

Set<String> currentlyDisplayedSources = new HashSet<>();
for ( Display display: currentDisplays )
currentlyDisplayedSources.addAll( display.getSources() );

for ( Transformation imageTransformation : imageTransformersCopy )
{
if ( ! currentlyDisplayedSources.stream().anyMatch( s -> imageTransformation.getSources().contains( s ) ) )
currentTransformations.remove( imageTransformation );
}
}

// TODO: typing ( or remove )
public Collection< AbstractAnnotationDisplay< ? > > getAnnotationDisplays()
{
final List< AbstractAnnotationDisplay< ? > > collect = getCurrentSourceDisplays().stream().filter( s -> s instanceof AbstractAnnotationDisplay ).map( s -> ( AbstractAnnotationDisplay< ? > ) s ).collect( Collectors.toList() );

return collect;
return getCurrentSourceDisplays().stream()
.filter( s -> s instanceof AbstractAnnotationDisplay )
.map( s -> ( AbstractAnnotationDisplay< ? > ) s )
.collect( Collectors.toList() );
}

public void close()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/embl/mobie/lib/view/save/ViewSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ViewSaver( MoBIE moBIE) {
this.settings = moBIE.getSettings();
}

public void saveCurrentSettingsAsViewDialog()
public void saveCurrentViewDialog()
{
final GenericDialog gd = new GenericDialog("Save current view");

Expand Down Expand Up @@ -155,7 +155,7 @@ public void viewSettingsDialog( SaveMethod saveMethod, MoBIEHelper.FileLocation
uiSelectionGroup = ProjectCreatorHelper.makeNewUiSelectionGroup(currentUiSelectionGroups);
}

View currentView = moBIE.getViewManager().createViewFromCurrentState(uiSelectionGroup, exclusive, includeViewerTransform);
View currentView = moBIE.getViewManager().createCurrentView(uiSelectionGroup, exclusive, includeViewerTransform);

if ( uiSelectionGroup != null && currentView != null ) {
if ( fileLocation == MoBIEHelper.FileLocation.Project && saveMethod == SaveMethod.saveAsNewView ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@

import net.imagej.ImageJ;
import org.embl.mobie.io.ImageDataFormat;
import org.embl.mobie.io.util.IOHelper;
import org.embl.mobie.MoBIE;
import org.embl.mobie.MoBIESettings;
import org.embl.mobie.lib.create.JSONValidator;
import org.embl.mobie.lib.io.StorageLocation;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -91,7 +87,7 @@ public void savePlatyView() throws IOException
moBIE.getViewManager().show( views.get("Suppl. Fig. 2A: Neuron" ) );

// grab the current view and save it
View view = moBIE.getViewManager().createViewFromCurrentState( uiSelectionGroup, isExclusive, true );
View view = moBIE.getViewManager().createCurrentView( uiSelectionGroup, isExclusive, true );
dataset.views().put( viewName, view );

String datasetJSONPath = new File( tempDir, datasetJsonName ).getAbsolutePath();
Expand Down

0 comments on commit 1135cc4

Please sign in to comment.