diff --git a/src/main/java/bdv/util/ResampledSource.java b/src/main/java/bdv/util/ResampledSource.java index 090c4e30..ea8794d1 100644 --- a/src/main/java/bdv/util/ResampledSource.java +++ b/src/main/java/bdv/util/ResampledSource.java @@ -308,7 +308,7 @@ public RandomAccessibleInterval buildSource(int t, int level) { AffineTransform3D atOrigin = new AffineTransform3D(); origin.getSourceTransform(t, getModelToOriginMipMapLevel(level), atOrigin); at.concatenate(atOrigin); - RandomAccessible ra = RealViews.affine(ipimg, at); // Gets the view + RandomAccessible ra = RealViews.simplify(RealViews.affine(ipimg, at)); // Gets the view // ... interval RandomAccessibleInterval view = Views.interval(ra, new long[] { 0, 0, diff --git a/src/test/resources/bdvplaygroundstate.json b/src/test/resources/bdvplaygroundstate.json index f62c2d00..5ffc8015 100644 --- a/src/test/resources/bdvplaygroundstate.json +++ b/src/test/resources/bdvplaygroundstate.json @@ -14,7 +14,7 @@ "name": "upsampled", "mipmaps_reused": false, "defaultMipmapLevel": 0, - "origin_source_id": 42, + "origin_source_id": 43, "model_source_id": 15 }, "string_metadata": {} @@ -35,7 +35,7 @@ "mipmaps_reused": false, "defaultMipmapLevel": 0, "origin_source_id": 19, - "model_source_id": 42 + "model_source_id": 43 }, "string_metadata": {} }, @@ -516,7 +516,7 @@ ] } }, - "wrapped_source_id": 35 + "wrapped_source_id": 26 }, "string_metadata": {} }, @@ -532,12 +532,12 @@ "affinetransform_fixed": { "type": "AffineTransform3D", "affinetransform3d": [ - 0.6513850362660715, - -0.348062595687508, + 0.6572454814574047, + -0.08891326296113902, 0.0, 0.0, - 0.3210141181429151, - 0.6007649071725533, + 0.09087326321914962, + 0.671733773421179, 0.0, 0.0, 0.0, @@ -546,7 +546,7 @@ 0.0 ] }, - "wrapped_source_id": 26 + "wrapped_source_id": 38 }, "string_metadata": {} }, @@ -562,12 +562,12 @@ "affinetransform_fixed": { "type": "AffineTransform3D", "affinetransform3d": [ - 0.7322009971821706, - -0.0746706963911756, + 0.4628521345566832, + -0.2966675521328264, 0.0, 0.0, - 0.07190530019561053, - 0.7050842572847801, + 0.2923677958182328, + 0.45614377911310516, 0.0, 200.0, 0.0, @@ -576,7 +576,7 @@ 0.0 ] }, - "wrapped_source_id": 26 + "wrapped_source_id": 38 }, "string_metadata": {} }, @@ -592,12 +592,12 @@ "affinetransform_fixed": { "type": "AffineTransform3D", "affinetransform3d": [ - 0.5827716098463275, - -0.28792723236518375, + 0.5524777405081435, + -0.322463046706307, 0.0, 200.0, - 0.2726004154275721, - 0.5517497655171907, + 0.3540082949864662, + 0.6065243907262191, 0.0, 0.0, 0.0, @@ -606,7 +606,7 @@ 0.0 ] }, - "wrapped_source_id": 26 + "wrapped_source_id": 38 }, "string_metadata": {} }, @@ -622,12 +622,12 @@ "affinetransform_fixed": { "type": "AffineTransform3D", "affinetransform3d": [ - 0.3137355646376311, - -0.40035280761079706, + 0.49868524403796227, + -0.1105528659431934, 0.0, 200.0, - 0.5654233237060595, - 0.44309269811505764, + 0.14369057046244033, + 0.6481638136259267, 0.0, 200.0, 0.0, @@ -636,7 +636,7 @@ 0.0 ] }, - "wrapped_source_id": 26 + "wrapped_source_id": 38 }, "string_metadata": {} }, @@ -655,7 +655,7 @@ "name": "downsampled-with-mipmap", "mipmaps_reused": true, "defaultMipmapLevel": 0, - "origin_source_id": 42, + "origin_source_id": 43, "model_source_id": 20 }, "string_metadata": {} @@ -675,7 +675,7 @@ "name": "downsampled", "mipmaps_reused": false, "defaultMipmapLevel": 0, - "origin_source_id": 42, + "origin_source_id": 43, "model_source_id": 21 }, "string_metadata": {} @@ -730,7 +730,7 @@ } } }, - "wrapped_source_id": 37 + "wrapped_source_id": 24 }, "string_metadata": {} }, @@ -914,7 +914,7 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 26, + "source_id": 22, "color": -1, "converter_setup_min": 0.033052792654934965, "converter_setup_max": 147.05187452180567, @@ -927,32 +927,32 @@ "string_metadata": {} }, { - "source_name": "mri-stack", + "source_name": "demoSlice.tif channel 1", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 26, - "color": -1, - "converter_setup_min": 0.033052792654934965, - "converter_setup_max": 147.05187452180567, + "source_id": 24, + "color": 65535, + "converter_setup_min": 0.03902065799540933, + "converter_setup_max": 131.07039020657996, "sac": { "spimdata": { - "datalocation": "src/test/resources/mri-stack.xml" + "datalocation": "src/test/resources/demoSlice.xml" }, "viewsetup": 0 }, "string_metadata": {} }, { - "source_name": "mri-stack", + "source_name": "demoSlice.tif channel 1", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 26, - "color": -1, - "converter_setup_min": 0.033052792654934965, - "converter_setup_max": 147.05187452180567, + "source_id": 24, + "color": 65535, + "converter_setup_min": 0.03902065799540933, + "converter_setup_max": 131.07039020657996, "sac": { "spimdata": { - "datalocation": "src/test/resources/mri-stack.xml" + "datalocation": "src/test/resources/demoSlice.xml" }, "viewsetup": 0 }, @@ -963,7 +963,7 @@ "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", "source_id": 26, - "color": -1, + "color": 65535, "converter_setup_min": 0.033052792654934965, "converter_setup_max": 147.05187452180567, "sac": { @@ -979,7 +979,7 @@ "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", "source_id": 26, - "color": -1, + "color": 65535, "converter_setup_min": 0.033052792654934965, "converter_setup_max": 147.05187452180567, "sac": { @@ -1087,16 +1087,16 @@ "string_metadata": {} }, { - "source_name": "mri-stack", + "source_name": "demoSlice.tif channel 1", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", "source_id": 33, "color": -1, - "converter_setup_min": 0.033052792654934965, - "converter_setup_max": 147.05187452180567, + "converter_setup_min": 0.03902065799540933, + "converter_setup_max": 131.07039020657996, "sac": { "spimdata": { - "datalocation": "src/test/resources/mri-stack.xml" + "datalocation": "src/test/resources/demoSlice.xml" }, "viewsetup": 0 }, @@ -1106,8 +1106,8 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 35, - "color": 65535, + "source_id": 38, + "color": -1, "converter_setup_min": 0.033052792654934965, "converter_setup_max": 147.05187452180567, "sac": { @@ -1122,8 +1122,8 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 35, - "color": 65535, + "source_id": 38, + "color": -1, "converter_setup_min": 0.033052792654934965, "converter_setup_max": 147.05187452180567, "sac": { @@ -1135,32 +1135,32 @@ "string_metadata": {} }, { - "source_name": "demoSlice.tif channel 1", + "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 37, - "color": 65535, - "converter_setup_min": 0.03902065799540933, - "converter_setup_max": 131.07039020657996, + "source_id": 38, + "color": -1, + "converter_setup_min": 0.033052792654934965, + "converter_setup_max": 147.05187452180567, "sac": { "spimdata": { - "datalocation": "src/test/resources/demoSlice.xml" + "datalocation": "src/test/resources/mri-stack.xml" }, "viewsetup": 0 }, "string_metadata": {} }, { - "source_name": "demoSlice.tif channel 1", + "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 37, - "color": 65535, - "converter_setup_min": 0.03902065799540933, - "converter_setup_max": 131.07039020657996, + "source_id": 38, + "color": -1, + "converter_setup_min": 0.033052792654934965, + "converter_setup_max": 147.05187452180567, "sac": { "spimdata": { - "datalocation": "src/test/resources/demoSlice.xml" + "datalocation": "src/test/resources/mri-stack.xml" }, "viewsetup": 0 }, @@ -1170,13 +1170,13 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 42, + "source_id": 38, "color": -1, - "converter_setup_min": 0.014078041315990818, - "converter_setup_max": 80.01958684009182, + "converter_setup_min": 0.033052792654934965, + "converter_setup_max": 147.05187452180567, "sac": { "spimdata": { - "datalocation": "src/test/resources/mri-stack-multilevel.xml" + "datalocation": "src/test/resources/mri-stack.xml" }, "viewsetup": 0 }, @@ -1186,7 +1186,7 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 42, + "source_id": 43, "color": -1, "converter_setup_min": 0.014078041315990818, "converter_setup_max": 80.01958684009182, @@ -1202,7 +1202,7 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 42, + "source_id": 43, "color": -1, "converter_setup_min": 0.014078041315990818, "converter_setup_max": 80.01958684009182, @@ -1218,7 +1218,7 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 42, + "source_id": 43, "color": -1, "converter_setup_min": 0.014078041315990818, "converter_setup_max": 80.01958684009182, @@ -1234,7 +1234,7 @@ "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", - "source_id": 42, + "source_id": 43, "color": -1, "converter_setup_min": 0.014078041315990818, "converter_setup_max": 80.01958684009182, @@ -1247,16 +1247,16 @@ "string_metadata": {} }, { - "source_name": "demoSlice.tif channel 1", + "source_name": "mri-stack", "source_class": "bdv.SpimSource", "converter_class": "class net.imglib2.display.Instances$Imp", "source_id": 43, "color": -1, - "converter_setup_min": 0.03902065799540933, - "converter_setup_max": 131.07039020657996, + "converter_setup_min": 0.014078041315990818, + "converter_setup_max": 80.01958684009182, "sac": { "spimdata": { - "datalocation": "src/test/resources/demoSlice.xml" + "datalocation": "src/test/resources/mri-stack-multilevel.xml" }, "viewsetup": 0 }, diff --git a/src/test/src/sc/fiji/bdvpg/transform/BigWarpDemo2D.java b/src/test/src/sc/fiji/bdvpg/transform/BigWarpDemo2D.java new file mode 100644 index 00000000..8fbc8016 --- /dev/null +++ b/src/test/src/sc/fiji/bdvpg/transform/BigWarpDemo2D.java @@ -0,0 +1,194 @@ +/*- + * #%L + * BigDataViewer-Playground + * %% + * Copyright (C) 2019 - 2023 Nicolas Chiaruttini, EPFL - Robert Haase, MPI CBG - Christian Tischer, 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 sc.fiji.bdvpg.transform; + +import bdv.tools.brightness.ConverterSetup; +import bdv.viewer.SourceAndConverter; +import bigwarp.BigWarp; +import mpicbg.spim.data.generic.AbstractSpimData; +import net.imagej.ImageJ; +import net.imglib2.realtransform.AffineTransform3D; +import net.imglib2.type.numeric.ARGBType; +import org.scijava.util.VersionUtils; +import sc.fiji.bdvpg.TestHelper; +import sc.fiji.bdvpg.scijava.services.SourceAndConverterService; +import sc.fiji.bdvpg.sourceandconverter.SourceAndConverterAndTimeRange; +import sc.fiji.bdvpg.sourceandconverter.display.BrightnessAutoAdjuster; +import sc.fiji.bdvpg.sourceandconverter.register.BigWarpLauncher; +import sc.fiji.bdvpg.sourceandconverter.transform.SourceAffineTransformer; +import sc.fiji.bdvpg.sourceandconverter.transform.SourceTransformHelper; +import sc.fiji.bdvpg.spimdata.importer.SpimDataFromXmlImporter; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class BigWarpDemo2D { + + static ImageJ ij; + + static SourceAndConverterService sourceService; + + static final String filePath = "src/test/resources/demoSlice.xml"; + + public static void main(String... args) { + // Initializes static SourceService and Display Service + ij = new ImageJ(); + TestHelper.startFiji(ij);//ij.ui().showUI(); + System.out.println("BigWarp version:"+VersionUtils.getVersion(BigWarp.class)); + sourceService = ij.get(SourceAndConverterService.class); + + // Makes BDV Source + // --------------------------- START BIGWARP + bigwarp(); + bigwarpRot(); + bigwarpRotTranslate(); + bigwarpRotPostTranslate(); + + } + + public static void bigwarp() { + + // --------------------------- MAKE SOURCES + SourceAndConverter fixedSource = takeFirstSource(filePath); + SourceAndConverter movingSource = takeFirstSource(filePath); + + // Chqnge moving source color + sourceService.getConverterSetup(movingSource) + .setColor(new ARGBType(ARGBType.rgba(0, 255, 255,0))); + + + startBigWarp("Rotate PI/2", fixedSource, movingSource, "src/test/resources/landmarks2d-demoSlice.csv"); + } + + public static void bigwarpRot() { + + // --------------------------- MAKE SOURCES + SourceAndConverter fixedSource = takeFirstSource(filePath); + + AffineTransform3D rot90 = new AffineTransform3D(); + + rot90.rotate(2, Math.PI/2.0); + + SourceAndConverter rotatedMovingSource = new SourceAffineTransformer<>(takeFirstSource(filePath), rot90).get(); + + // Chqnge moving source color + sourceService.getConverterSetup(rotatedMovingSource) + .setColor(new ARGBType(ARGBType.rgba(0, 255, 255,0))); + + startBigWarp("Rotate PI/2", fixedSource, rotatedMovingSource, "src/test/resources/landmarks2d-demoSlice.csv"); + } + + public static void bigwarpRotTranslate() { + + // --------------------------- MAKE SOURCES + SourceAndConverter fixedSource = takeFirstSource(filePath); + + AffineTransform3D rot90 = new AffineTransform3D(); + + rot90.rotate(2, Math.PI/2.0); + + SourceAndConverter rotatedMovingSource = new SourceAffineTransformer<>(takeFirstSource(filePath), rot90).get(); + + + AffineTransform3D translateRight = new AffineTransform3D(); + translateRight.translate(3,0,0); + + SourceAndConverter rotatedTranslatedMovingSource = SourceTransformHelper.mutate(translateRight, new SourceAndConverterAndTimeRange<>(rotatedMovingSource,0,1)); + + // Chqnge moving source color + sourceService.getConverterSetup(rotatedMovingSource) + .setColor(new ARGBType(ARGBType.rgba(0, 255, 255,0))); + + startBigWarp("Rotate PI/2", fixedSource, rotatedTranslatedMovingSource, "src/test/resources/landmarks2d-demoSlice.csv"); + } + + public static void bigwarpRotPostTranslate() { + + // --------------------------- MAKE SOURCES + SourceAndConverter fixedSource = takeFirstSource(filePath); + + AffineTransform3D rot90 = new AffineTransform3D(); + + rot90.rotate(2, Math.PI/2.0); + + SourceAndConverter rotatedMovingSource = new SourceAffineTransformer<>(takeFirstSource(filePath), rot90).get(); + + // Change moving source color + sourceService.getConverterSetup(rotatedMovingSource) + .setColor(new ARGBType(ARGBType.rgba(0, 255, 255,0))); + + startBigWarp("Rotate PI/2", fixedSource, rotatedMovingSource, "src/test/resources/landmarks2d-demoSlice.csv"); + + + AffineTransform3D translateRight = new AffineTransform3D(); + translateRight.translate(5,0,0); + + SourceTransformHelper.mutate(translateRight, new SourceAndConverterAndTimeRange<>(rotatedMovingSource,0,1)); + + } + + + + public static SourceAndConverter takeFirstSource(String xmlPath) { + SourceAndConverterService sourceService = ij.get(SourceAndConverterService.class); + // Fixed SourceAndConverter + AbstractSpimData spimDataFixed = new SpimDataFromXmlImporter(xmlPath).get(); + + SourceAndConverter source = sourceService + .getSourceAndConverterFromSpimdata(spimDataFixed) + .get(0); + + new BrightnessAutoAdjuster<>(source, 0).run(); + + return source; + } + + + public static void startBigWarp(String bwName, SourceAndConverter fixedSource, SourceAndConverter movingSource, String landmarkFilePath) { + SourceAndConverterService sourceService = ij.get(SourceAndConverterService.class); + + List> movingSources = new ArrayList<>(); + movingSources.add(movingSource); + + List> fixedSources = new ArrayList<>(); + fixedSources.add(fixedSource); + + List converterSetups = movingSources.stream().map(sourceService::getConverterSetup).collect(Collectors.toList()); + converterSetups.addAll(fixedSources.stream().map(sourceService::getConverterSetup).collect(Collectors.toList())); + + BigWarpLauncher bwl = new BigWarpLauncher(movingSources, fixedSources, bwName, converterSetups); + bwl.run(); + + bwl.getBigWarp().loadLandmarks( landmarkFilePath ); + bwl.getBigWarp().toggleMovingImageDisplay(); + bwl.getBigWarp().matchActiveViewerPanelToOther(); + } + +}