-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Command to set a transformation
- Loading branch information
Showing
32 changed files
with
367 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/main/java/org/embl/mobie/command/context/SetTransformationCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/*- | ||
* #%L | ||
* Fiji viewer for MoBIE projects | ||
* %% | ||
* Copyright (C) 2018 - 2023 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.tools.transformation.TransformedSource; | ||
import bdv.util.BdvHandle; | ||
import bdv.viewer.SourceAndConverter; | ||
import ij.IJ; | ||
import net.imglib2.realtransform.AffineTransform3D; | ||
import org.embl.mobie.command.CommandConstants; | ||
import org.embl.mobie.lib.MoBIEHelper; | ||
import org.embl.mobie.lib.serialize.transformation.AffineTransformation; | ||
import org.scijava.Initializable; | ||
import org.scijava.command.DynamicCommand; | ||
import org.scijava.plugin.Parameter; | ||
import org.scijava.plugin.Plugin; | ||
import org.scijava.widget.Button; | ||
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Transform>Registration - Set Transformation") | ||
public class SetTransformationCommand extends DynamicCommand implements BdvPlaygroundActionCommand, Initializable | ||
{ | ||
static { net.imagej.patcher.LegacyInjector.preinit(); } | ||
|
||
@Parameter | ||
public BdvHandle bdvHandle; | ||
|
||
@Parameter ( label = "Image", choices = {""} ) | ||
private String sourceName; | ||
|
||
@Parameter ( label = "3D Affine" ) | ||
private String transformation = Arrays.toString( new AffineTransform3D().getRowPackedCopy() ); | ||
private AffineTransform3D previousTransform; | ||
private TransformedSource< ? > transformedSource; | ||
|
||
public enum AdditionMode | ||
{ | ||
Concatenate, | ||
Replace; | ||
} | ||
|
||
@Parameter ( label = "Addition Mode" ) | ||
private AdditionMode mode = AdditionMode.Replace; | ||
|
||
@Parameter ( label = "Preview", callback = "apply" ) | ||
private Button preview; | ||
|
||
private List< SourceAndConverter< ? > > sourceAndConverters; | ||
|
||
@Override | ||
public void initialize() | ||
{ | ||
sourceAndConverters = MoBIEHelper.getVisibleSacs( bdvHandle ); | ||
|
||
final List< String > imageNames = sourceAndConverters.stream() | ||
.map( sac -> sac.getSpimSource().getName() ) | ||
.collect( Collectors.toList() ); | ||
|
||
getInfo().getMutableInput( "sourceName", String.class ) | ||
.setChoices( imageNames ); | ||
|
||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
apply(); | ||
} | ||
|
||
@Override | ||
public void cancel() | ||
{ | ||
if ( transformedSource != null ) | ||
{ | ||
transformedSource.setFixedTransform( previousTransform ); | ||
bdvHandle.getViewerPanel().requestRepaint(); | ||
} | ||
} | ||
|
||
private void apply() | ||
{ | ||
double[] doubles = parseStringToDoubleArray( transformation ); | ||
AffineTransform3D additionalTransform = new AffineTransform3D(); | ||
additionalTransform.set( doubles ); | ||
|
||
if ( additionalTransform.isIdentity() ) | ||
return; | ||
|
||
// TODO: the below logic will break when the | ||
// user decides to change the source name | ||
SourceAndConverter< ? > sourceAndConverter = sourceAndConverters.stream() | ||
.filter( sac -> sac.getSpimSource().getName().equals( sourceName ) ) | ||
.findFirst().get(); | ||
|
||
if ( sourceAndConverter.getSpimSource() instanceof TransformedSource ) | ||
{ | ||
transformedSource = ( TransformedSource< ? > ) sourceAndConverter.getSpimSource(); | ||
|
||
if ( previousTransform != null ) | ||
{ | ||
// reset transform to initial state | ||
transformedSource.setFixedTransform( previousTransform ); | ||
} | ||
else | ||
{ | ||
// remember the previous transform | ||
// such that we can reset it | ||
previousTransform = new AffineTransform3D(); | ||
transformedSource.getFixedTransform( previousTransform ); | ||
} | ||
|
||
if ( mode.equals( AdditionMode.Replace ) ) | ||
{ | ||
transformedSource.setFixedTransform( additionalTransform ); | ||
} | ||
else if ( mode.equals( AdditionMode.Concatenate ) ) | ||
{ | ||
AffineTransform3D newTransform = previousTransform.copy().preConcatenate( additionalTransform ); | ||
transformedSource.setFixedTransform( newTransform ); | ||
} | ||
|
||
bdvHandle.getViewerPanel().requestRepaint(); | ||
} | ||
else | ||
{ | ||
IJ.error( "Cannot set the transformation of a " + sourceAndConverter.getSpimSource().getClass() ); | ||
} | ||
} | ||
|
||
public static double[] parseStringToDoubleArray(String arrayStr) { | ||
arrayStr = arrayStr.replaceAll("[^0-9.,]", ""); | ||
String[] items = arrayStr.split(",\\s*"); | ||
double[] doubles = Arrays.stream(items).mapToDouble(Double::parseDouble).toArray(); | ||
return doubles; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/embl/mobie/command/context/SourcesInfoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*- | ||
* #%L | ||
* Fiji viewer for MoBIE projects | ||
* %% | ||
* Copyright (C) 2018 - 2023 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.tools.transformation.TransformedSource; | ||
import bdv.util.BdvHandle; | ||
import bdv.viewer.Source; | ||
import bdv.viewer.SourceAndConverter; | ||
import ij.IJ; | ||
import net.imglib2.realtransform.AffineTransform3D; | ||
import org.embl.mobie.MoBIE; | ||
import org.embl.mobie.command.CommandConstants; | ||
import org.embl.mobie.lib.MoBIEHelper; | ||
import org.embl.mobie.lib.bdv.ScreenShotMaker; | ||
import org.scijava.Initializable; | ||
import org.scijava.command.DynamicCommand; | ||
import org.scijava.module.MutableModuleItem; | ||
import org.scijava.plugin.Parameter; | ||
import org.scijava.plugin.Plugin; | ||
import sc.fiji.bdvpg.bdv.BdvHandleHelper; | ||
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Log Images Info") | ||
public class SourcesInfoCommand implements BdvPlaygroundActionCommand | ||
{ | ||
static { net.imagej.patcher.LegacyInjector.preinit(); } | ||
|
||
public static final String CAPTURE_SIZE_PIXELS = "Capture size [pixels]: "; | ||
|
||
@Parameter | ||
public BdvHandle bdvHandle; | ||
|
||
@Override | ||
public void run() | ||
{ | ||
List< SourceAndConverter< ? > > visibleSacs = MoBIEHelper.getVisibleSacs( bdvHandle ); | ||
visibleSacs.forEach( sac -> | ||
{ | ||
Source< ? > source = sac.getSpimSource(); | ||
IJ.log( "# " + source.getName() ); | ||
IJ.log( "Number of resolution levels: " + source.getNumMipmapLevels() ); | ||
IJ.log( "Voxel size: " + Arrays.toString( source.getVoxelDimensions().dimensionsAsDoubleArray() ) ); | ||
|
||
if ( source instanceof TransformedSource ) | ||
{ | ||
AffineTransform3D transform3D = new AffineTransform3D(); | ||
( ( TransformedSource<?> ) source ).getFixedTransform( transform3D ); | ||
IJ.log( "Additional transform: " + transform3D ); | ||
source.getSourceTransform( 0, 0, transform3D ); | ||
IJ.log( "Full transform: " + transform3D ); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.