net.imglib2.util.LinAlgHelpers Java Examples

The following examples show how to use net.imglib2.util.LinAlgHelpers. 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: PaintUtils.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param labelToGlobalTransform
 * @param viewerTransform
 * @param axis
 * 		{@code 0=x, 1=y, 2=z}
 * @param tolerance
 * 		{@code 0 <= tolerance << 1}
 *
 * @return {@code -1} if axis no unique corresponding axis according to tolerance, else {@code 0=x, 1=y, 2=z}
 */
public static int labelAxisCorrespondingToViewerAxis(
		final AffineTransform3D labelToGlobalTransform,
		final AffineTransform3D viewerTransform,
		final int axis,
		final double tolerance)
{
	final double[] transformedAxis = viewerAxisInLabelCoordinates(labelToGlobalTransform, viewerTransform, axis);
	LinAlgHelpers.normalize(transformedAxis);
	if (Math.abs(Math.abs(transformedAxis[0]) - 1.0) <= tolerance) { return 0; }
	if (Math.abs(Math.abs(transformedAxis[1]) - 1.0) <= tolerance) { return 1; }
	if (Math.abs(Math.abs(transformedAxis[2]) - 1.0) <= tolerance) { return 2; }
	return -1;
}
 
Example #2
Source File: PaintUtils.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This should be equivalent to {@link #maximumVoxelDiagonalLengthPerDimension(AffineTransform3D,
 * AffineTransform3D)}.
 *
 * @param labelToGlobalTransform
 * @param viewerTransform
 *
 * @return
 */
public static double[] labelUnitLengthAlongViewerAxis(
		final AffineTransform3D labelToGlobalTransform,
		final AffineTransform3D viewerTransform)
{
	final AffineTransform3D labelToGlobalTransformWithoutTranslation = duplicateWithoutTranslation(labelToGlobalTransform);
	final AffineTransform3D viewerTransformWithoutTranslation        = duplicateWithoutTranslation(viewerTransform);
	final AffineTransform3D labelToViewerTransformWithoutTranslation = labelToGlobalTransformWithoutTranslation.preConcatenate(viewerTransformWithoutTranslation);

	final double[] unitX = {1.0, 0.0, 0.0};
	final double[] unitY = {0.0, 1.0, 0.0};
	final double[] unitZ = {0.0, 0.0, 1.0};
	labelToViewerTransformWithoutTranslation.applyInverse(unitX, unitX);
	labelToViewerTransformWithoutTranslation.applyInverse(unitY, unitY);
	labelToViewerTransformWithoutTranslation.applyInverse(unitZ, unitZ);

	LinAlgHelpers.normalize(unitX);
	LinAlgHelpers.normalize(unitY);
	LinAlgHelpers.normalize(unitZ);

	labelToViewerTransformWithoutTranslation.apply(unitX, unitX);
	labelToViewerTransformWithoutTranslation.apply(unitY, unitY);
	labelToViewerTransformWithoutTranslation.apply(unitZ, unitZ);

	return new double[] {
			unitX[0],
			unitY[1],
			unitZ[2]
	};

}
 
Example #3
Source File: ScaleBarOverlayRenderer.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
@Override
public synchronized void drawOverlays(GraphicsContext graphicsContext) {
	if (width > 0.0 && height > 0.0 && config.getIsShowing()) {
		double targetLength = Math.min(width, config.getTargetScaleBarLength());
		double[] onePixelWidth = {1.0, 0.0, 0.0};
		transform.applyInverse(onePixelWidth, onePixelWidth);
		final double globalCoordinateWidth = LinAlgHelpers.length(onePixelWidth);
		// length of scale bar in global coordinate system
		final double scaleBarWidth = targetLength * globalCoordinateWidth;
		final double pot = Math.floor( Math.log10( scaleBarWidth ) );
		final double l2 =  scaleBarWidth / Math.pow( 10, pot );
		final int fracs = ( int ) ( 0.1 * l2 * subdivPerPowerOfTen );
		final double scale1 = ( fracs > 0 ) ? Math.pow( 10, pot + 1 ) * fracs / subdivPerPowerOfTen : Math.pow( 10, pot );
		final double scale2 = ( fracs == 3 ) ? Math.pow( 10, pot + 1 ) : Math.pow( 10, pot + 1 ) * ( fracs + 1 ) / subdivPerPowerOfTen;

		final double lB1 = scale1 / globalCoordinateWidth;
		final double lB2 = scale2 / globalCoordinateWidth;

		final double scale;
		final double scaleBarLength;
		if (Math.abs(lB1 - targetLength) < Math.abs(lB2 - targetLength))
		{
			scale = scale1;
			scaleBarLength = lB1;
		}
		else
		{
			scale = scale2;
			scaleBarLength = lB2;
		}

		final double[] ratios = UNITS.stream().mapToDouble(unit -> config.getBaseUnit().getConverterTo(unit).convert(scale)).toArray();
		int firstSmallerThanZeroIndex = 0;
		for (; firstSmallerThanZeroIndex < ratios.length; ++firstSmallerThanZeroIndex) {
			if (ratios[firstSmallerThanZeroIndex] < 1.0)
				break;
		}
		final int unitIndex = Math.max(firstSmallerThanZeroIndex - 1, 0);
		final Unit<Length> targetUnit = UNITS.get(unitIndex);
		final double targetScale = config.getBaseUnit().getConverterTo(targetUnit).convert(scale);

		final double x = 20;
		final double y = height - 30;

		final DecimalFormat format = new DecimalFormat(String.format("0.%s", String.join("", Collections.nCopies(config.getNumDecimals(), "#"))));
		final String scaleBarText = format.format(targetScale) + targetUnit.toString();
		final Text text = new Text(scaleBarText);
		text.setFont(config.getOverlayFont());
		final Bounds bounds = text.getBoundsInLocal();
		final double tx = 20 + (scaleBarLength - bounds.getMaxX()) / 2;
		final double ty = y - 5;
		graphicsContext.setFill(config.getBackgroundColor());

		// draw background
		graphicsContext.fillRect(
				x - 7,
				ty - bounds.getHeight() - 3,
			 	scaleBarLength + 14,
				bounds.getHeight() + 25 );

		// draw scalebar
		graphicsContext.setFill(config.getForegroundColor());
		graphicsContext.fillRect(x, y, ( int ) scaleBarLength, 10);

		// draw label
		graphicsContext.setFont(config.getOverlayFont());
		graphicsContext.fillText(scaleBarText, tx, ty);
	}
}
 
Example #4
Source File: PaintUtils.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static double length(double... vec) {
	return LinAlgHelpers.length(vec);
}
 
Example #5
Source File: Align.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
double alignStep(final RandomAccessibleInterval< T > image, ExecutorService service)
{
	// compute error image = warped image - template
	computeDifference( Views.extendBorder( image ), currentTransform, template, error, service, Runtime.getRuntime().availableProcessors() * 2 );

	// compute transform parameter update
	final double[] gradient = new double[numParameters];

	ArrayList< Callable< Void > > calls =  new ArrayList< Callable<Void> >();
	for ( int p = 0; p < numParameters; ++p )
	{
		final int pInner = p;
		Callable< Void > callable = new Callable< Void >()
		{
			@Override
			public Void call() throws Exception
			{
				double gradT = 0;
				final Cursor< FloatType > err = Views.flatIterable( error ).cursor();
				for ( final FloatType t : Views.flatIterable( Views.hyperSlice( descent, n, pInner ) ) )
					gradT += t.getRealDouble() * err.next().getRealDouble();
				gradient[pInner] = gradT;
				return null;

			}
		};

		calls.add( callable );

	}

	List<Future<Void>> futures = null;

	try
	{
		futures = service.invokeAll( calls );
		for (Future<Void> f : futures)
			f.get();
	}
	catch ( InterruptedException | ExecutionException e)
	{
		e.printStackTrace();
	}

	final double[] dp = new double[numParameters];
	LinAlgHelpers.mult( Hinv, gradient, dp );

	// udpate transform
	currentTransform.preConcatenate( warpFunction.getAffine( dp ) );

	// return norm of parameter update vector
	return LinAlgHelpers.length( dp );
}