Skip to content

Commit

Permalink
Improve #1152
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Jun 19, 2024
1 parent fcc8db2 commit 98f0a6a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 116 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>org.embl.mobie</groupId>
<artifactId>mobie-viewer-fiji</artifactId>
<version>5.0.7</version>
<version>5.0.9</version>

<!-- force javadoc generation to fetch errors: -->
<!-- mvn javadoc:javadoc | grep error -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*-
* #%L
* Fiji viewer for MoBIE projects
* %%
* Copyright (C) 2018 - 2024 EMBL
* %%
* 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 org.embl.mobie.command.context;

import bdv.util.BdvHandle;
import com.google.gson.Gson;
import de.embl.cba.bdv.utils.BdvUtils;
import de.embl.cba.bdv.utils.Logger;
import net.imglib2.util.LinAlgHelpers;
import org.embl.mobie.command.CommandConstants;
import org.embl.mobie.lib.playground.BdvPlaygroundHelper;
import org.embl.mobie.lib.serialize.JsonHelper;
import org.embl.mobie.lib.transform.viewer.AffineViewerTransform;
import org.embl.mobie.lib.transform.viewer.NormalVectorViewerTransform;
import org.embl.mobie.lib.serialize.transformation.NormalizedAffineViewerTransform;
import org.embl.mobie.lib.transform.viewer.PositionViewerTransform;
import net.imglib2.realtransform.AffineTransform3D;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand;

@Plugin( type = BdvPlaygroundActionCommand.class, name = CurrentLocationLoggerCommand.NAME, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + CurrentLocationLoggerCommand.NAME )
public class CurrentLocationLoggerCommand implements BdvPlaygroundActionCommand
{

private static PositionViewerTransform mousePointerPositionTransform;

static { net.imagej.patcher.LegacyInjector.preinit(); }

public static final String NAME = "Log Current Location [ C ]";

public static final String SHORTCUT = "C";

@Parameter
BdvHandle bdvHandle;

@Override
public void run()
{
new Thread( () ->
{
logCurrentPosition( bdvHandle,
BdvPlaygroundHelper.getWindowCentreInCalibratedUnits( bdvHandle ),
null );
} ).start();
}

public static void logCurrentPosition( BdvHandle bdvHandle,
double[] windowCentre,
double[] mousePointer )
{

// position
final int timePoint = bdvHandle.getViewerPanel().state().getCurrentTimepoint();
final PositionViewerTransform positionViewerTransform = new PositionViewerTransform( windowCentre, timePoint );

// affine
final AffineTransform3D affineTransform3D = new AffineTransform3D();
bdvHandle.getViewerPanel().state().getViewerTransform( affineTransform3D );
final AffineViewerTransform affineViewerTransform = new AffineViewerTransform( affineTransform3D.getRowPackedCopy(), timePoint );

// normalized affine
final NormalizedAffineViewerTransform normalizedAffineViewerTransform = new NormalizedAffineViewerTransform( bdvHandle );

// normal vector
double[] currentNormalVector = BdvUtils.getCurrentViewNormalVector( bdvHandle );
final NormalVectorViewerTransform normalVectorViewerTransform = new NormalVectorViewerTransform( currentNormalVector, timePoint );

// print
final Gson gson = JsonHelper.buildGson( false );
Logger.log( "" );
Logger.log( "# Current location" );
Logger.log( "To restore the current location, any of the below {...} JSON strings can be pasted into MoBIE's \"location\" field." );
Logger.log( "To share views with other people we recommend using the normalised viewer transform." );

Logger.log("## Mouse pointer position" );
Double distanceToRecentPosition = null;
if ( mousePointer == null )
{
Logger.log( "To get this please use the \"" + SHORTCUT + "\" keyboard shortcut to trigger this action." );
}
else
{
if ( mousePointerPositionTransform != null )
{
distanceToRecentPosition = LinAlgHelpers.distance( mousePointerPositionTransform.getParameters(), mousePointer );
}
mousePointerPositionTransform = new PositionViewerTransform( mousePointer, timePoint );
Logger.log( gson.toJson( mousePointerPositionTransform ) );
}
Logger.log("## Window center position" );
Logger.log( gson.toJson( positionViewerTransform ) );
Logger.log("## Viewer transform" );
Logger.log( gson.toJson( affineViewerTransform ) );
Logger.log("## Normalised viewer transform" );
Logger.log( gson.toJson( normalizedAffineViewerTransform ) );
Logger.log("## Normal vector" );
Logger.log( gson.toJson( normalVectorViewerTransform ) );

if ( distanceToRecentPosition != null )
{
Logger.log("" );
Logger.log( "Distance between current and most recent mouse position: " + distanceToRecentPosition );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import net.imglib2.RealPoint;
import org.embl.mobie.DataStore;
import org.embl.mobie.command.CommandConstants;
import org.embl.mobie.lib.bdv.GlobalMousePositionProvider;
import org.embl.mobie.lib.bdv.CalibratedMousePositionProvider;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.image.RegionAnnotationImage;
import org.embl.mobie.lib.image.StitchedImage;
Expand All @@ -52,7 +52,6 @@
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.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -90,7 +89,7 @@ public void run()

if ( image instanceof StitchedImage )
{
final RealPoint position = new GlobalMousePositionProvider( bdvHandle ).getPositionAsRealPoint();
final RealPoint position = new CalibratedMousePositionProvider( bdvHandle ).getPositionAsRealPoint();

// traverse through the potentially several
// layers of stitching
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import bdv.viewer.SourceAndConverter;
import net.imglib2.RealPoint;
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.lib.bdv.GlobalMousePositionProvider;
import org.embl.mobie.lib.bdv.CalibratedMousePositionProvider;
import org.embl.mobie.lib.serialize.display.AbstractAnnotationDisplay;
import org.embl.mobie.lib.source.AnnotationType;
import sc.fiji.bdvpg.sourceandconverter.SourceAndConverterHelper;
Expand Down Expand Up @@ -66,7 +66,7 @@ public synchronized void clearSelection()

private synchronized void toggleSelectionAtMousePosition()
{
final GlobalMousePositionProvider positionProvider = new GlobalMousePositionProvider( bdvHandle );
final CalibratedMousePositionProvider positionProvider = new CalibratedMousePositionProvider( bdvHandle );
final int timePoint = positionProvider.getTimePoint();
final RealPoint realPosition = positionProvider.getPositionAsRealPoint();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
import bdv.util.BdvHandle;
import net.imglib2.RealPoint;

public class GlobalMousePositionProvider
public class CalibratedMousePositionProvider
{
private final RealPoint realPoint;
private final int timePoint;

public GlobalMousePositionProvider( BdvHandle bdvHandle )
public CalibratedMousePositionProvider( BdvHandle bdvHandle )
{
realPoint = new RealPoint( 3 );
bdvHandle.getBdvHandle().getViewerPanel().getGlobalMouseCoordinates( realPoint );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SourcesAtMousePositionSupplier( BdvHandle bdvHandle, boolean is2D )
@Override
public Collection< SourceAndConverter< ? > > get()
{
final GlobalMousePositionProvider positionProvider = new GlobalMousePositionProvider( bdvHandle );
final CalibratedMousePositionProvider positionProvider = new CalibratedMousePositionProvider( bdvHandle );

final List< SourceAndConverter< ? > > sourceAndConverters = SourceAndConverterServices.getBdvDisplayService().getSourceAndConverterOf( bdvHandle )
.stream()
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/org/embl/mobie/lib/bdv/view/SliceViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.DataStore;
import org.embl.mobie.command.context.*;
import org.embl.mobie.command.view.CurrentLocationLoggerCommand;
import org.embl.mobie.command.context.CurrentLocationLoggerCommand;
import org.embl.mobie.MoBIE;
import org.embl.mobie.lib.annotation.SliceViewAnnotationSelector;
import org.embl.mobie.lib.bdv.MobieBdvSupplier;
import org.embl.mobie.lib.bdv.MobieSerializableBdvOptions;
import org.embl.mobie.lib.bdv.ImageNameOverlay;
import org.embl.mobie.lib.bdv.SourcesAtMousePositionSupplier;
import org.embl.mobie.lib.bdv.*;
import org.embl.mobie.lib.bdv.blend.AccumulateAlphaBlendingProjectorARGB;
import org.embl.mobie.lib.bdv.blend.BlendingMode;
import org.embl.mobie.lib.color.OpacityHelper;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.image.RegionAnnotationImage;
import org.embl.mobie.lib.playground.BdvPlaygroundHelper;
import org.embl.mobie.lib.serialize.View;
import org.embl.mobie.lib.serialize.display.AbstractDisplay;
import org.embl.mobie.lib.source.SourceHelper;
Expand Down Expand Up @@ -147,10 +145,10 @@ private void installContextMenuAndKeyboardShortCuts( )

final ArrayList< String > actions = new ArrayList< String >();
actions.add( SourceAndConverterService.getCommandName( SourcesInfoCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ShowRasterImagesCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( CurrentLocationLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ScreenShotMakerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ShowRasterImagesCommand.class ) );
// TODO https://github.com/mobie/mobie-viewer-fiji/issues/1152
actions.add( SourceAndConverterService.getCommandName( CurrentLocationLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( BigWarpRegistrationCommand.class ) );
//actions.add( SourceAndConverterService.getCommandName( AutomaticRegistrationCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ManualTransformationCommand.class ) );
Expand Down Expand Up @@ -199,6 +197,18 @@ private void installContextMenuAndKeyboardShortCuts( )
ConfigureLabelRenderingCommand.incrementRandomColorSeed( sourceAndConverters, bdvHandle );
}).start(),
"Change random color seed", "ctrl L" ) ;

behaviours.behaviour(
( ClickBehaviour ) ( x, y ) ->
new Thread( () ->
{
CurrentLocationLoggerCommand.logCurrentPosition(
bdvHandle,
BdvPlaygroundHelper.getWindowCentreInCalibratedUnits( bdvHandle ),
new CalibratedMousePositionProvider( bdvHandle ).getPositionAsDoubles()
);
}).start(),
"Log current position", "C" ) ;
}

public static BdvHandle createBdv( boolean is2D, String frameTitle )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public List< Image< T > > images()
@Override
public List< SourceAndConverter< T > > sourceAndConverters()
{
// returns an unmodifiableList
return DataStore.getSourceAndConverters( ( Collection ) images );
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/embl/mobie/lib/source/SourceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import net.imglib2.roi.geom.GeomMasks;
import net.imglib2.roi.geom.real.WritableBox;
import net.imglib2.util.Intervals;
import org.embl.mobie.lib.bdv.GlobalMousePositionProvider;
import org.embl.mobie.lib.bdv.CalibratedMousePositionProvider;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -277,7 +277,7 @@ public static FinalRealInterval bounds( Source< ? > source, int t )

public static List< Source< ? > > filterAtCurrentMousePosition( Collection< Source< ? > > sources, BdvHandle bdvHandle )
{
final GlobalMousePositionProvider positionProvider = new GlobalMousePositionProvider( bdvHandle );
final CalibratedMousePositionProvider positionProvider = new CalibratedMousePositionProvider( bdvHandle );
final RealPoint position = positionProvider.getPositionAsRealPoint();
final int timePoint = positionProvider.getTimePoint();
final List< Source< ? > > sourcesAtCurrentPosition = sources.stream()
Expand Down
Loading

0 comments on commit 98f0a6a

Please sign in to comment.