Skip to content

Commit

Permalink
feat: move bounding box calculation into RealTransform
Browse files Browse the repository at this point in the history
with extensible interface for sampling method,
override trivial cases in Translations, scales, and
affine transformations
  • Loading branch information
axtimwalde committed Jun 13, 2024
1 parent 265ddcb commit 20c8765
Show file tree
Hide file tree
Showing 15 changed files with 807 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* 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
Expand All @@ -34,17 +34,19 @@

package net.imglib2.realtransform;

import Jama.Matrix;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealPositionable;
import Jama.Matrix;
import net.imglib2.realtransform.interval.IntervalSamplingMethod;

/**
* An abstract implementation of an affine transformation that returns default
* values referring to the identity transformation for all fields. This
* implementation is not thread safe. Create a {@link #copy()} for each
* consumer.
*
*
* @author Stephan Saalfeld
*/
public abstract class AbstractAffineTransform implements AffineGet, AffineSet
Expand Down Expand Up @@ -136,7 +138,7 @@ public int numTargetDimensions()
{
return n;
}

@Override
public void apply( final double[] source, final double[] target )
{
Expand All @@ -148,11 +150,11 @@ public void apply( final double[] source, final double[] target )
for ( int c = 0; c < n; ++c )
tmp[ r ] += source[ c ] * a.get( r, c );
}

for ( int r = 0; r < n; ++r )
target[ r ] = tmp[ r ] + t[ r ];
}

@Override
public void apply( final float[] source, final float[] target )
{
Expand All @@ -164,7 +166,7 @@ public void apply( final float[] source, final float[] target )
for ( int c = 0; c < n; ++c )
tmp[ r ] += source[ c ] * a.get( r, c );
}

for ( int r = 0; r < n; ++r )
target[ r ] = ( float )( tmp[ r ] + t[ r ] );
}
Expand All @@ -180,7 +182,7 @@ public void apply( final RealLocalizable source, final RealPositionable target )
for ( int c = 0; c < n; ++c )
tmp[ r ] += source.getDoublePosition( c ) * a.get( r, c );
}

for ( int r = 0; r < n; ++r )
target.setPosition( tmp[ r ] + t[ r ], r );
}
Expand Down Expand Up @@ -215,4 +217,10 @@ public RealLocalizable d( final int d )

return ds[ d ];
}

@Override
public RealInterval boundingInterval( final RealInterval interval, final IntervalSamplingMethod samplingMethdod )
{
return AffineGet.super.boundingInterval( interval, IntervalSamplingMethod.CORNERS );
}
}
52 changes: 37 additions & 15 deletions src/main/java/net/imglib2/realtransform/AbstractScale.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* 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
Expand All @@ -34,9 +34,12 @@

package net.imglib2.realtransform;

import net.imglib2.FinalRealInterval;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealPositionable;
import net.imglib2.realtransform.interval.IntervalSamplingMethod;

/**
* <em>n</em>-d arbitrary scaling. Abstract base implementation.
Expand All @@ -63,7 +66,7 @@ public AbstractScale( final double... s )

/**
* Set the scale vector.
*
*
* @param s
* s.length &lt;= the number of dimensions of this
* {@link AbstractScale}
Expand All @@ -74,7 +77,7 @@ public AbstractScale( final double... s )
public void applyInverse( final double[] source, final double[] target )
{
assert source.length >= s.length && target.length >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
source[ d ] = target[ d ] / s[ d ];
}
Expand All @@ -83,17 +86,17 @@ public void applyInverse( final double[] source, final double[] target )
public void applyInverse( final float[] source, final float[] target )
{
assert source.length >= s.length && target.length >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
source[ d ] = ( float )( target[ d ] / s[ d ] );

}

@Override
public void applyInverse( final RealPositionable source, final RealLocalizable target )
{
assert source.numDimensions() >= s.length && target.numDimensions() >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
source.setPosition( target.getDoublePosition( d ) / s[ d ], d );
}
Expand Down Expand Up @@ -123,7 +126,7 @@ public int numTargetDimensions()
public void apply( final double[] source, final double[] target )
{
assert source.length >= s.length && target.length >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
target[ d ] = source[ d ] * s[ d ];
}
Expand All @@ -132,7 +135,7 @@ public void apply( final double[] source, final double[] target )
public void apply( final float[] source, final float[] target )
{
assert source.length >= s.length && target.length >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
target[ d ] = ( float )( source[ d ] * s[ d ] );
}
Expand All @@ -141,7 +144,7 @@ public void apply( final float[] source, final float[] target )
public void apply( final RealLocalizable source, final RealPositionable target )
{
assert source.numDimensions() >= s.length && target.numDimensions() >= s.length : "Input dimensions too small.";

for ( int d = 0; d < s.length; ++d )
target.setPosition( source.getDoublePosition( d ) * s[ d ], d );
}
Expand All @@ -150,7 +153,7 @@ public void apply( final RealLocalizable source, final RealPositionable target )
public double get( final int row, final int column )
{
assert row >= 0 && row < numDimensions() : "Dimension index out of bounds.";

return row == column ? s[ row ] : 0;
}

Expand All @@ -168,7 +171,7 @@ public double[] getRowPackedCopy()
public RealLocalizable d( final int d )
{
assert d >= 0 && d < numDimensions() : "Dimension index out of bounds.";

return ds[ d ];
}

Expand All @@ -186,14 +189,33 @@ public double[] getScaleCopy()
{
return s.clone();
}

@Override
public double getTranslation( final int d ) {
public double getTranslation( final int d )
{
return 0.0;
}

@Override
public double[] getTranslationCopy() {
public double[] getTranslationCopy()
{
return new double[ s.length ];
}

@Override
public RealInterval boundingInterval( final RealInterval interval, final IntervalSamplingMethod samplingMethdod )
{
assert interval.numDimensions() >= s.length : "Interval does not have enough dimensions.";

final double[] min = interval.minAsDoubleArray();
final double[] max = interval.maxAsDoubleArray();

for ( int d = 0; d < s.length; ++d )
{
min[ d ] *= s[ d ];
max[ d ] *= s[ d ];
}

return new FinalRealInterval( min, max, false );
}
}
32 changes: 26 additions & 6 deletions src/main/java/net/imglib2/realtransform/AbstractTranslation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* 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
Expand All @@ -34,13 +34,16 @@

package net.imglib2.realtransform;

import net.imglib2.FinalRealInterval;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealPositionable;
import net.imglib2.realtransform.interval.IntervalSamplingMethod;

/**
* <em>n</em>-d translation. Abstract base implementation.
*
*
* @author Stephan Saalfeld
*/
abstract public class AbstractTranslation implements TranslationGet
Expand Down Expand Up @@ -77,7 +80,7 @@ public AbstractTranslation( final double... t )

/**
* Set the translation vector.
*
*
* @param t
* t.length &lt;= the number of dimensions of this
* {@link AbstractTranslation}
Expand All @@ -86,7 +89,7 @@ public AbstractTranslation( final double... t )

/**
* Set one value of the translation vector.
*
*
* @param t
* t.length &lt;= the number of dimensions of this
* {@link AbstractTranslation}
Expand Down Expand Up @@ -197,7 +200,7 @@ public RealLocalizable d( final int d )

return ds[ d ];
}

@Override
public double getScale( final int d ) {
return 0.0;
Expand All @@ -224,4 +227,21 @@ public double[] getTranslationCopy()

@Override
abstract public AbstractTranslation inverse();

@Override
public RealInterval boundingInterval( final RealInterval interval, final IntervalSamplingMethod samplingMethdod )
{
assert interval.numDimensions() >= t.length : "Interval does not have enough dimensions.";

final double[] min = interval.minAsDoubleArray();
final double[] max = interval.maxAsDoubleArray();

for ( int d = 0; d < t.length; ++d )
{
min[ d ] += t[ d ];
max[ d ] += t[ d ];
}

return new FinalRealInterval( min, max, false );
}
}
24 changes: 24 additions & 0 deletions src/main/java/net/imglib2/realtransform/RealTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@

package net.imglib2.realtransform;

import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealPositionable;
import net.imglib2.realtransform.interval.IntervalSamplingMethod;

/**
* Transformation from R<sup><em>n</em></sup> to R<sup><em>m</em></sup>.
Expand Down Expand Up @@ -257,4 +259,26 @@ default boolean isIdentity()
{
return false;
}

/**
* Estimate the {@link RealInterval} that bounds the given RealInterval
* after being transformed by a {@link RealTransform}.
* <p>
* For arbitrary transformations, it is not necessarily possible to
* directly calculate the resulting bounding box trivially, therefore,
* a sampling methods for coordinates in the source interval must be
* provided. {@link RealTransform}s that can calculate the bounding
* interval more efficiently than by sampling coordinates are encouraged
* to override this method and to ignore the provided sampling method.
*
* @param interval
* the real interval
* @param samplingMethod
* the method used to sample coordinates of the source interval
* @return the bounding interval
*/
default RealInterval boundingInterval( final RealInterval interval, final IntervalSamplingMethod samplingMethod )
{
return samplingMethod.bounds( interval, this );
}
}
Loading

0 comments on commit 20c8765

Please sign in to comment.