|
| 1 | +package bdv.tools.links; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.IntStream; |
| 7 | + |
| 8 | +import bdv.tools.brightness.ConverterSetup; |
| 9 | +import bdv.tools.JsonUtils.TypedList; |
| 10 | +import bdv.util.Bounds; |
| 11 | +import bdv.viewer.ConverterSetups; |
| 12 | +import bdv.viewer.DisplayMode; |
| 13 | +import bdv.viewer.Interpolation; |
| 14 | +import bdv.viewer.SourceAndConverter; |
| 15 | +import bdv.viewer.ViewerState; |
| 16 | +import net.imglib2.Dimensions; |
| 17 | +import net.imglib2.FinalDimensions; |
| 18 | +import net.imglib2.Point; |
| 19 | +import net.imglib2.realtransform.AffineTransform3D; |
| 20 | +import net.imglib2.type.numeric.ARGBType; |
| 21 | + |
| 22 | +class BdvPropertiesV0 |
| 23 | +{ |
| 24 | + private final AffineTransform3D transform; |
| 25 | + private final int timepoint; |
| 26 | + private final DisplayMode displaymode; |
| 27 | + private final Interpolation interpolation; |
| 28 | + private final TypedList< ResourceSpec< ? > > sourceSpecs; |
| 29 | + private final TypedList< ResourceConfig > sourceConfigs; |
| 30 | + private final List< SourceConverterConfig > converterConfigs; |
| 31 | + private final int currentSourceIndex; |
| 32 | + private final int[] activeSourceIndices; |
| 33 | + private final long[] panelsize; |
| 34 | + private final long[] mousepos; |
| 35 | + |
| 36 | + private final Anchor anchor; |
| 37 | + |
| 38 | + public enum Anchor |
| 39 | + { |
| 40 | + CENTER( "center" ), |
| 41 | + MOUSE( "mouse" ); |
| 42 | + |
| 43 | + private final String label; |
| 44 | + |
| 45 | + Anchor( final String label ) |
| 46 | + { |
| 47 | + this.label = label; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String toString() |
| 52 | + { |
| 53 | + return label; |
| 54 | + } |
| 55 | + |
| 56 | + public static Anchor fromString( final String string ) |
| 57 | + { |
| 58 | + for ( final Anchor value : values() ) |
| 59 | + if ( value.toString().equals( string ) ) |
| 60 | + return value; |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public BdvPropertiesV0() |
| 66 | + { |
| 67 | + transform = new AffineTransform3D(); |
| 68 | + timepoint = 0; |
| 69 | + displaymode = DisplayMode.SINGLE; |
| 70 | + interpolation = Interpolation.NLINEAR; |
| 71 | + sourceSpecs = new TypedList<>(); |
| 72 | + sourceConfigs = new TypedList<>(); |
| 73 | + converterConfigs = new ArrayList<>(); |
| 74 | + currentSourceIndex = -1; |
| 75 | + activeSourceIndices = new int[ 0 ]; |
| 76 | + panelsize = new long[ 2 ]; |
| 77 | + mousepos = new long[ 2 ]; |
| 78 | + anchor = Anchor.CENTER; |
| 79 | + } |
| 80 | + |
| 81 | + public BdvPropertiesV0( |
| 82 | + final ViewerState state, |
| 83 | + final ConverterSetups converterSetups, |
| 84 | + final Dimensions panelsize, |
| 85 | + final Point mousepos, |
| 86 | + final ResourceManager resources ) |
| 87 | + { |
| 88 | + this.transform = state.getViewerTransform(); |
| 89 | + this.timepoint = state.getCurrentTimepoint(); |
| 90 | + this.displaymode = state.getDisplayMode(); |
| 91 | + this.interpolation = state.getInterpolation(); |
| 92 | + this.sourceSpecs = getSourceSpecs( state.getSources(), resources ); |
| 93 | + this.sourceConfigs = getSourceConfigs( sourceSpecs.list(), resources ); |
| 94 | + this.converterConfigs = getSourceConverterConfigs( state.getSources(), converterSetups ); |
| 95 | + this.currentSourceIndex = getCurrentSourceIndex( state ); |
| 96 | + this.activeSourceIndices = getActiveSourceIndices( state ); |
| 97 | + this.panelsize = panelsize.dimensionsAsLongArray(); |
| 98 | + this.mousepos = mousepos.positionAsLongArray(); |
| 99 | + this.anchor = Anchor.CENTER; |
| 100 | + } |
| 101 | + |
| 102 | + private static int getCurrentSourceIndex( final ViewerState state ) |
| 103 | + { |
| 104 | + return state.getSources().indexOf( state.getCurrentSource() ); |
| 105 | + } |
| 106 | + |
| 107 | + private static int[] getActiveSourceIndices( final ViewerState state ) |
| 108 | + { |
| 109 | + final List< SourceAndConverter< ? > > sources = state.getSources(); |
| 110 | + return IntStream.range( 0, sources.size() ).filter( i -> state.isSourceActive( sources.get( i ) ) ).toArray(); |
| 111 | + } |
| 112 | + |
| 113 | + private static List< SourceConverterConfig > getSourceConverterConfigs( final List< SourceAndConverter< ? > > sources, final ConverterSetups converterSetups ) |
| 114 | + { |
| 115 | + List< SourceConverterConfig > configs = new ArrayList<>(); |
| 116 | + for ( SourceAndConverter< ? > soc : sources ) |
| 117 | + { |
| 118 | + final ConverterSetup setup = converterSetups.getConverterSetup( soc ); |
| 119 | + final boolean hasColor = setup.supportsColor(); |
| 120 | + final int color = setup.getColor().get(); |
| 121 | + final double min = setup.getDisplayRangeMin(); |
| 122 | + final double max = setup.getDisplayRangeMax(); |
| 123 | + final Bounds bounds = converterSetups.getBounds().getBounds( setup ); |
| 124 | + final double minBound = bounds.getMinBound(); |
| 125 | + final double maxBound = bounds.getMaxBound(); |
| 126 | + configs.add( new SourceConverterConfig( hasColor, color, min, max, minBound, maxBound ) ); |
| 127 | + } |
| 128 | + return configs; |
| 129 | + } |
| 130 | + |
| 131 | + private static TypedList< ResourceSpec< ? > > getSourceSpecs( final List< SourceAndConverter< ? > > sources, final ResourceManager resources ) |
| 132 | + { |
| 133 | + List< ResourceSpec< ? > > sourceSpecs = new ArrayList<>(); |
| 134 | + for ( final SourceAndConverter< ? > soc : sources ) |
| 135 | + { |
| 136 | + sourceSpecs.add( resources.getResourceSpec( soc ) ); |
| 137 | + } |
| 138 | + return new TypedList<>( sourceSpecs ); |
| 139 | + } |
| 140 | + |
| 141 | + private static TypedList< ResourceConfig > getSourceConfigs( final List< ResourceSpec< ? > > sourceSpecs, final ResourceManager resources ) |
| 142 | + { |
| 143 | + final List< ResourceConfig > sourceConfigs = new ArrayList<>(); |
| 144 | + for ( ResourceSpec< ? > spec : sourceSpecs ) { |
| 145 | + sourceConfigs.add( spec.getConfig( resources ) ); |
| 146 | + } |
| 147 | + return new TypedList<>( sourceConfigs ); |
| 148 | + } |
| 149 | + |
| 150 | + public AffineTransform3D transform() |
| 151 | + { |
| 152 | + return transform; |
| 153 | + } |
| 154 | + |
| 155 | + public int timepoint() |
| 156 | + { |
| 157 | + return timepoint; |
| 158 | + } |
| 159 | + |
| 160 | + public Dimensions panelsize() |
| 161 | + { |
| 162 | + return FinalDimensions.wrap( panelsize ); |
| 163 | + } |
| 164 | + |
| 165 | + public Point mousepos() |
| 166 | + { |
| 167 | + return Point.wrap( mousepos ); |
| 168 | + } |
| 169 | + |
| 170 | + public Anchor getAnchor() |
| 171 | + { |
| 172 | + return anchor; |
| 173 | + } |
| 174 | + |
| 175 | + public DisplayMode displaymode() |
| 176 | + { |
| 177 | + return displaymode; |
| 178 | + } |
| 179 | + |
| 180 | + public Interpolation interpolation() |
| 181 | + { |
| 182 | + return interpolation; |
| 183 | + } |
| 184 | + |
| 185 | + public List< ResourceSpec< ? > > sourceSpecs() |
| 186 | + { |
| 187 | + return sourceSpecs.list(); |
| 188 | + } |
| 189 | + |
| 190 | + public List< ResourceConfig > sourceConfigs() |
| 191 | + { |
| 192 | + return sourceConfigs.list(); |
| 193 | + } |
| 194 | + |
| 195 | + public List< SourceConverterConfig > converterConfigs() |
| 196 | + { |
| 197 | + return converterConfigs; |
| 198 | + } |
| 199 | + |
| 200 | + public int currentSourceIndex() |
| 201 | + { |
| 202 | + return currentSourceIndex; |
| 203 | + } |
| 204 | + |
| 205 | + public int[] activeSourceIndices() |
| 206 | + { |
| 207 | + return activeSourceIndices; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public String toString() |
| 212 | + { |
| 213 | + return "BdvPropertiesV0{" + |
| 214 | + "transform=" + transform + |
| 215 | + ", timepoint=" + timepoint + |
| 216 | + ", displaymode=" + displaymode + |
| 217 | + ", interpolation=" + interpolation + |
| 218 | + ", sourceSpecs =" + sourceSpecs + |
| 219 | + ", sourceConfigs =" + sourceConfigs + |
| 220 | + ", converterConfigs=" + converterConfigs + |
| 221 | + ", currentSourceIndex=" + currentSourceIndex + |
| 222 | + ", activeSourceIndices=" + Arrays.toString( activeSourceIndices ) + |
| 223 | + ", panelsize=" + Arrays.toString( panelsize ) + |
| 224 | + ", mousepos=" + Arrays.toString( mousepos ) + |
| 225 | + ", anchor=" + anchor + |
| 226 | + '}'; |
| 227 | + } |
| 228 | + |
| 229 | + public static class SourceConverterConfig |
| 230 | + { |
| 231 | + final boolean hasColor; |
| 232 | + final int color; |
| 233 | + final double min; |
| 234 | + final double max; |
| 235 | + final double minBound; |
| 236 | + final double maxBound; |
| 237 | + |
| 238 | + public SourceConverterConfig( final boolean hasColor, final int color, final double min, final double max, final double minBound, final double maxBound ) |
| 239 | + { |
| 240 | + this.hasColor = hasColor; |
| 241 | + this.color = color; |
| 242 | + this.min = min; |
| 243 | + this.max = max; |
| 244 | + this.minBound = minBound; |
| 245 | + this.maxBound = maxBound; |
| 246 | + } |
| 247 | + |
| 248 | + public double rangeMin() { |
| 249 | + return min; |
| 250 | + } |
| 251 | + |
| 252 | + public double rangeMax() { |
| 253 | + return max; |
| 254 | + } |
| 255 | + |
| 256 | + public ARGBType color() { |
| 257 | + return new ARGBType( color ); |
| 258 | + } |
| 259 | + |
| 260 | + public Bounds bounds() { |
| 261 | + return new Bounds( minBound, maxBound ); |
| 262 | + } |
| 263 | + |
| 264 | + @Override |
| 265 | + public String toString() |
| 266 | + { |
| 267 | + return "SourceConverterConfig{" + |
| 268 | + "hasColor=" + hasColor + |
| 269 | + ", color=" + String.format("#%08x", color) + |
| 270 | + ", min=" + min + |
| 271 | + ", max=" + max + |
| 272 | + ", minBound=" + minBound + |
| 273 | + ", maxBound=" + maxBound + |
| 274 | + '}'; |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments