Java Code Examples for net.imglib2.Interval#max()

The following examples show how to use net.imglib2.Interval#max() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenderBoxHelperFX.java    From paintera with GNU General Public License v2.0 6 votes vote down vote up
public void renderCanvas(final Interval targetInterval, final Path canvas)
{
	final double tX0 = targetInterval.min(0);
	final double tX1 = targetInterval.max(0);
	final double tY0 = targetInterval.min(1);
	final double tY1 = targetInterval.max(1);

	final double[] c000 = new double[] {tX0, tY0, 0};
	final double[] c100 = new double[] {tX1, tY0, 0};
	final double[] c010 = new double[] {tX0, tY1, 0};
	final double[] c110 = new double[] {tX1, tY1, 0};

	canvas.getElements().add(new MoveTo(perspectiveX(c000), perspectiveY(c000)));
	canvas.getElements().add(new LineTo(perspectiveX(c100), perspectiveY(c100)));
	canvas.getElements().add(new LineTo(perspectiveX(c110), perspectiveY(c110)));
	canvas.getElements().add(new LineTo(perspectiveX(c010), perspectiveY(c010)));
	canvas.getElements().add(new ClosePath());
}
 
Example 2
Source File: BlendingRealRandomAccess.java    From BigStitcher with GNU General Public License v2.0 6 votes vote down vote up
/**
 * RealRandomAccess that computes a blending function for a certain {@link Interval}
 * 
 * @param interval - the interval it is defined on (return zero outside of it)
 * @param border - how many pixels to skip before starting blending (on each side of each dimension)
 * @param blending - how many pixels to compute the blending function on (on each side of each dimension)
 */
public BlendingRealRandomAccess(
		final Interval interval,
		final float[] border,
		final float[] blending )
{
	this.interval = interval;
	this.n = interval.numDimensions();
	this.l = new float[ n ];
	this.border = border;
	this.blending = blending;
	this.v = new FloatType();
	
	this.min = new int[ n ];
	this.dimMinus1 = new int[ n ];
	
	for ( int d = 0; d < n; ++d )
	{
		this.min[ d ] = (int)interval.min( d );
		this.dimMinus1[ d ] = (int)interval.max( d ) - min[ d ];
	}
}
 
Example 3
Source File: BlendingRealRandomAccess.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
/**
 * RealRandomAccess that computes a blending function for a certain {@link Interval}
 * 
 * @param interval - the interval it is defined on (return zero outside of it)
 * @param border - how many pixels to skip before starting blending (on each side of each dimension)
 * @param blending - how many pixels to compute the blending function on (on each side of each dimension)
 */
public BlendingRealRandomAccess(
		final Interval interval,
		final float[] border,
		final float[] blending )
{
	this.interval = interval;
	this.n = interval.numDimensions();
	this.l = new float[ n ];
	this.border = border;
	this.blending = blending;
	this.v = new FloatType();
	
	this.min = new int[ n ];
	this.dimMinus1 = new int[ n ];
	
	for ( int d = 0; d < n; ++d )
	{
		this.min[ d ] = (int)interval.min( d );
		this.dimMinus1[ d ] = (int)interval.max( d ) - min[ d ];
	}
}
 
Example 4
Source File: FormatTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Checks that the given tile size is valid for the given reader. */
public static void checkTileSize(final Metadata m, final Interval bounds,
	final int imageIndex) throws FormatException
{
	final List<CalibratedAxis> axes = m.get(imageIndex).getAxesPlanar();

	for (int i = 0; i < axes.size(); i++) {
		final long start = bounds.min(i);
		final long end = bounds.max(i);
		final long length = m.get(imageIndex).getAxisLength(axes.get(i));

		if (start < 0 || end < 0 || end >= length) {
			throw new FormatException("Invalid planar size: start=" + start +
				", end=" + end + ", length in metadata=" + length);
		}
	}
}
 
Example 5
Source File: PlaneSeparator.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns true if we have a cached plane matching the given image and plane
 * indices, with extents to cover the desired offsets and lengths
 */
private boolean haveCached(final long source, final int imageIndex,
	final Interval bounds)
{
	if (source != lastPlaneIndex || imageIndex != lastImageIndex ||
		lastPlane == null || lastPlaneMin == null || lastPlaneMax == null)
	{
		return false;
	}
	// TODO It would be nice to fix up this logic so that we can use
	// cached planes when requesting a sub-region of the cached plane.
	// See https://github.com/scifio/scifio/issues/155
	for (int d = 0; d < bounds.numDimensions(); d++) {
		if (bounds.min(d) != lastPlaneMin[d]) return false;
		if (bounds.max(d) != lastPlaneMax[d]) return false;
	}
	return true;
}
 
Example 6
Source File: FourNeighborhoodExtrema.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
/**
 * split the given Interval into nSplits intervals along the largest dimension
 * @param interval input interval
 * @param nSplits how may splits
 * @return list of intervals input was split into
 */
public static List<Interval> splitAlongLargestDimension(Interval interval, long nSplits){
	
	List<Interval> res = new ArrayList<Interval>();
	
	long[] min = new long[interval.numDimensions()];
	long[] max = new long[interval.numDimensions()];
	interval.min(min);
	interval.max(max);
	
	int splitDim = 0;
	for (int i = 0; i< interval.numDimensions(); i++){
		if (interval.dimension(i) > interval.dimension(splitDim)) splitDim = i;
	}

	// there could be more splits than actual dimension entries
	nSplits = Math.min( nSplits, interval.dimension(splitDim) );

	long chunkSize = interval.dimension(splitDim) / nSplits;
	long maxSplitDim = max[splitDim];
	
	for (int i = 0; i<nSplits; i++){
		if (i != 0){
			min[splitDim] += chunkSize;	
		}
		max[splitDim] = min[splitDim] + chunkSize - 1;
		if (i == nSplits -1){
			max[splitDim] = maxSplitDim;
		}
		res.add(new FinalInterval(min, max));
	}
		
	return res;
}
 
Example 7
Source File: AbstractArrayLoader.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void validateBounds(final long[] lengths, final Interval bounds) {
	if (lengths.length != bounds.numDimensions()) {
		throw new IllegalArgumentException("Expected bounds of dimensionality " +
			lengths.length + " but was " + bounds.numDimensions());
	}
	for (int d = 0; d < bounds.numDimensions(); d++) {
		if (bounds.min(d) < 0 || bounds.max(d) >= lengths[d]) {
			throw new IllegalArgumentException("Bound #" + d + " of " + //
				"[" + bounds.min(d) + ", " + bounds.max(d) + "] " + //
				"is not contained in [0, " + lengths[d] + "]");
		}
	}
}
 
Example 8
Source File: PlaneSeparator.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Converts the given plane information using the current metadata to a format
 * usable by the wrapped reader, stored in the "lastPlane"... variables.
 */
private void updateLastPlaneInfo(final long source, final int imageIndex,
	final int splitOffset, final Interval bounds)
{
	final Metadata meta = getMetadata();
	final Metadata parentMeta = getParentMeta();
	lastPlaneIndex = source;
	lastImageIndex = imageIndex;
	// create the plane offsets and lengths to match the underlying image
	lastPlaneMin = new long[bounds.numDimensions() + splitOffset];
	lastPlaneMax = new long[bounds.numDimensions() + splitOffset];

	// Create the offset and length arrays to match the underlying,
	// unsplit dimensions. This is required to pass to the wrapped reader.
	// The unsplit plane will then have the appropriate region extracted.
	for (final CalibratedAxis axis : parentMeta.get(imageIndex)
		.getAxesPlanar())
	{
		final int parentIndex = parentMeta.get(imageIndex).getAxisIndex(axis
			.type());
		final int currentIndex = meta.get(imageIndex).getAxisIndex(axis.type());
		// This axis is still a planar axis, so we can read it from the
		// current plane offsets/lengths
		if (currentIndex >= 0 && currentIndex < meta.get(imageIndex)
			.getPlanarAxisCount())
		{
			lastPlaneMin[parentIndex] = bounds.min(currentIndex);
			lastPlaneMax[parentIndex] = bounds.max(currentIndex);
		}
		// This axis is a planar axis in the underlying metadata that was
		// split out, so we will insert a [0,length] range
		else if (parentMeta.get(imageIndex).getAxisIndex(axis.type()) < parentMeta
			.get(imageIndex).getPlanarAxisCount())
		{
			lastPlaneMin[parentIndex] = 0;
			lastPlaneMax[parentIndex] = parentMeta.get(imageIndex).getAxisLength(
				axis.type()) - 1;
		}
	}
}
 
Example 9
Source File: RenderBoxHelperFX.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
public void renderBox(final Interval sourceInterval, final AffineTransform3D transform, final Path front, final
Path back)
{
	final double sX0 = sourceInterval.min(0);
	final double sX1 = sourceInterval.max(0);
	final double sY0 = sourceInterval.min(1);
	final double sY1 = sourceInterval.max(1);
	final double sZ0 = sourceInterval.min(2);
	final double sZ1 = sourceInterval.max(2);

	final double[] p000 = new double[] {sX0, sY0, sZ0};
	final double[] p100 = new double[] {sX1, sY0, sZ0};
	final double[] p010 = new double[] {sX0, sY1, sZ0};
	final double[] p110 = new double[] {sX1, sY1, sZ0};
	final double[] p001 = new double[] {sX0, sY0, sZ1};
	final double[] p101 = new double[] {sX1, sY0, sZ1};
	final double[] p011 = new double[] {sX0, sY1, sZ1};
	final double[] p111 = new double[] {sX1, sY1, sZ1};

	final double[] q000 = new double[3];
	final double[] q100 = new double[3];
	final double[] q010 = new double[3];
	final double[] q110 = new double[3];
	final double[] q001 = new double[3];
	final double[] q101 = new double[3];
	final double[] q011 = new double[3];
	final double[] q111 = new double[3];

	transform.apply(p000, q000);
	transform.apply(p100, q100);
	transform.apply(p010, q010);
	transform.apply(p110, q110);
	transform.apply(p001, q001);
	transform.apply(p101, q101);
	transform.apply(p011, q011);
	transform.apply(p111, q111);

	splitEdge(q000, q100, front, back);
	splitEdge(q100, q110, front, back);
	splitEdge(q110, q010, front, back);
	splitEdge(q010, q000, front, back);

	splitEdge(q001, q101, front, back);
	splitEdge(q101, q111, front, back);
	splitEdge(q111, q011, front, back);
	splitEdge(q011, q001, front, back);

	splitEdge(q000, q001, front, back);
	splitEdge(q100, q101, front, back);
	splitEdge(q110, q111, front, back);
	splitEdge(q010, q011, front, back);
}
 
Example 10
Source File: BlockSpec.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
public void fromInterval(final Interval interval)
{
	interval.min(min);
	interval.max(max);
	grid.getCellPosition(min, pos);
}
 
Example 11
Source File: SceneBlockTree.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
public static BlockTree<BlockTreeFlatKey, BlockTreeNode<BlockTreeFlatKey>> createSceneBlockTree(
		final DataSource<?, ?> source,
		final ViewFrustum viewFrustum,
		final AffineTransform3D eyeToWorldTransform,
		final int levelOfDetail,
		final int coarsestScaleLevel,
		final int finestScaleLevel,
		final CellGrid[] rendererGrids,
		final BooleanSupplier wasInterrupted)
{
	final int numScaleLevels = source.getNumMipmapLevels();

	final double maxPixelsInProjectedVoxel = levelOfDetailMaxPixels[
			Math.max(0, Math.min(levelOfDetail - MeshSettings.Defaults.Values.getMinLevelOfDetail(), levelOfDetailMaxPixels.length - 1))
		];
	LOG.debug("levelOfDetail={}, maxPixelsInProjectedVoxel={}", levelOfDetail, maxPixelsInProjectedVoxel);

	final ViewFrustumCulling[] viewFrustumCullingInSourceSpace = new ViewFrustumCulling[numScaleLevels];
	final double[] minMipmapPixelSize = new double[numScaleLevels];
	for (int i = 0; i < viewFrustumCullingInSourceSpace.length; ++i)
	{
		final AffineTransform3D sourceToWorldTransform = new AffineTransform3D();
		source.getSourceTransform(0, i, sourceToWorldTransform);

		final AffineTransform3D cameraToSourceTransform = new AffineTransform3D();
		cameraToSourceTransform.preConcatenate(eyeToWorldTransform).preConcatenate(sourceToWorldTransform.inverse());

		viewFrustumCullingInSourceSpace[i] = new ViewFrustumCulling(viewFrustum, cameraToSourceTransform);

		final double[] extractedScale = new double[3];
		Arrays.setAll(extractedScale, d -> Affine3DHelpers.extractScale(cameraToSourceTransform.inverse(), d));
		minMipmapPixelSize[i] = Arrays.stream(extractedScale).min().getAsDouble();
	}

	final double[][] sourceScales = new double[numScaleLevels][];
	Arrays.setAll(sourceScales, i -> DataSource.getScale(source, 0, i));

	final BlockTree<BlockTreeFlatKey, BlockTreeNode<BlockTreeFlatKey>> blockTree = new BlockTree<>();
	final LinkedHashMap<BlockTreeFlatKey, BlockTreeFlatKey> blockAndParentQueue = new LinkedHashMap<>();

	// start with all blocks at the lowest resolution
	final int lowestResolutionScaleLevel = numScaleLevels - 1;
	final CellGrid rendererGridAtLowestResolution = rendererGrids[lowestResolutionScaleLevel];
	final long numBlocksAtLowestResolution = Intervals.numElements(rendererGridAtLowestResolution.getGridDimensions());
	LongStream.range(0, numBlocksAtLowestResolution).forEach(blockIndex -> blockAndParentQueue.put(new BlockTreeFlatKey(lowestResolutionScaleLevel, blockIndex), null));

	while (!blockAndParentQueue.isEmpty() && !wasInterrupted.getAsBoolean())
	{
		final Iterator<Map.Entry<BlockTreeFlatKey, BlockTreeFlatKey>> it = blockAndParentQueue.entrySet().iterator();
		final Map.Entry<BlockTreeFlatKey, BlockTreeFlatKey> entry = it.next();
		it.remove();

		final BlockTreeFlatKey key = entry.getKey();
		final BlockTreeFlatKey parentKey = entry.getValue();

		final int scaleLevel = key.scaleLevel;
		final Interval blockInterval = Grids.getCellInterval(rendererGrids[scaleLevel], key.blockIndex);

		if (viewFrustumCullingInSourceSpace[scaleLevel].intersects(blockInterval))
		{
			final double distanceFromCamera = viewFrustumCullingInSourceSpace[scaleLevel].distanceFromCamera(blockInterval);
			final double screenSizeToViewPlaneRatio = viewFrustum.screenSizeToViewPlaneRatio(distanceFromCamera);
			final double screenPixelSize = screenSizeToViewPlaneRatio * minMipmapPixelSize[scaleLevel];
			LOG.debug("scaleIndex={}, screenSizeToViewPlaneRatio={}, screenPixelSize={}", scaleLevel, screenSizeToViewPlaneRatio, screenPixelSize);

			final BlockTreeNode<BlockTreeFlatKey> treeNode = new BlockTreeNode<>(parentKey, new HashSet<>(), distanceFromCamera);
			blockTree.nodes.put(key, treeNode);
			if (parentKey != null)
				blockTree.nodes.get(parentKey).children.add(key);

			// check if needed to subdivide the block
			if (scaleLevel > coarsestScaleLevel || (scaleLevel > finestScaleLevel && screenPixelSize > maxPixelsInProjectedVoxel))
			{
				final int nextScaleLevel = scaleLevel - 1;
				final CellGrid rendererNextLevelGrid = rendererGrids[nextScaleLevel];

				final double[] relativeScales = new double[3];
				Arrays.setAll(relativeScales, d -> sourceScales[scaleLevel][d] / sourceScales[nextScaleLevel][d]);

				final double[] nextScaleLevelBlockMin = new double[3], nextScaleLevelBlockMax = new double[3];
				for (int d = 0; d < 3; ++d)
				{
					nextScaleLevelBlockMin[d] = blockInterval.min(d) * relativeScales[d];
					nextScaleLevelBlockMax[d] = (blockInterval.max(d) + 1) * relativeScales[d] - 1;
				}
				final Interval nextLevelBlockInterval = Intervals.smallestContainingInterval(new FinalRealInterval(nextScaleLevelBlockMin, nextScaleLevelBlockMax));

				// find out what blocks at higher resolution intersect with this block
				final long[] intersectingNextLevelBlockIndices = Grids.getIntersectingBlocks(nextLevelBlockInterval, rendererNextLevelGrid);
				for (final long intersectingNextLevelBlockIndex : intersectingNextLevelBlockIndices)
				{
					final BlockTreeFlatKey childKey = new BlockTreeFlatKey(nextScaleLevel, intersectingNextLevelBlockIndex);
					blockAndParentQueue.put(childKey, key);
				}
			}
		}
	}

	return !wasInterrupted.getAsBoolean() ? blockTree : null;
}
 
Example 12
Source File: EPSFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writePlane(final int imageIndex, final long planeIndex,
	final Plane plane, final Interval bounds) throws FormatException,
	IOException
{

	final byte[] buf = plane.getBytes();
	final boolean interleaved = plane.getImageMetadata()
		.getInterleavedAxisCount() > 0;
	checkParams(imageIndex, planeIndex, buf, bounds);
	final ImageMetadata imageMetadata = getMetadata().get(imageIndex);
	final int xAxis = imageMetadata.getAxisIndex(Axes.X);
	final int yAxis = imageMetadata.getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xAxis);
	final int y = (int) bounds.min(yAxis);
	final int w = (int) bounds.dimension(xAxis);
	final int h = (int) bounds.dimension(yAxis);
	final int sizeX = (int) imageMetadata.getAxisLength(Axes.X);
	final int nChannels = (int) imageMetadata.getAxisLength(Axes.CHANNEL);

	// write pixel data
	// for simplicity, write 80 char lines

	final int planeSize = (int) (bounds.max(xAxis) * bounds.max(yAxis));

	final StringBuilder buffer = new StringBuilder();

	final int offset = y * sizeX * nChannels * 2;
	final DataHandle<Location> handle = getHandle();
	handle.seek(planeOffset + offset);
	for (int row = 0; row < h; row++) {
		handle.skipBytes(nChannels * x * 2);
		for (int col = 0; col < w * nChannels; col++) {
			final int i = row * w * nChannels + col;
			final int index = interleaved || nChannels == 1 ? i : (i %
				nChannels) * planeSize + (i / nChannels);
			final String s = Integer.toHexString(buf[index]);
			// only want last 2 characters of s
			if (s.length() > 1) buffer.append(s.substring(s.length() - 2));
			else {
				buffer.append("0");
				buffer.append(s);
			}
		}
		buffer.append('\n');
		handle.writeBytes(buffer.toString());
		buffer.delete(0, buffer.length());
		handle.skipBytes(nChannels * (sizeX - w - x) * 2);
	}

	// write footer

	handle.seek(handle.length());
	handle.writeBytes("\nshowpage\n");
}