From ced765cc00a49dde2c60bcd6a00a14be516c0275 Mon Sep 17 00:00:00 2001 From: John Bogovic Date: Fri, 8 Mar 2024 19:37:41 -0500 Subject: [PATCH 1/2] feat: add RadialKDTreeInterpolatorFactory * and RadialKDTreeInterpolator --- .../RadialKDTreeInterpolator.java | 80 +++++++++++++++++++ .../RadialKDTreeInterpolatorFactory.java | 45 +++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java create mode 100644 src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java diff --git a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java new file mode 100644 index 000000000..d17631528 --- /dev/null +++ b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java @@ -0,0 +1,80 @@ +package net.imglib2.interpolation.neighborsearch; + +import java.util.function.DoubleUnaryOperator; + +import net.imglib2.KDTree; +import net.imglib2.RealPoint; +import net.imglib2.RealRandomAccess; +import net.imglib2.interpolation.InterpolatorFactory; +import net.imglib2.neighborsearch.RadiusNeighborSearch; +import net.imglib2.neighborsearch.RadiusNeighborSearchOnKDTree; +import net.imglib2.type.numeric.NumericType; + +/** + * A {@link RealRandomAccess} for {@link KDTree}s using a radial function used by + * {@link RadialKDTreeInterpolatorFactory}. + */ +public class RadialKDTreeInterpolator> extends RealPoint implements RealRandomAccess { + + protected static final double minThreshold = Double.MIN_VALUE * 1000; + + protected final RadiusNeighborSearch search; + protected final double maxRadius; + protected final double maxSquaredRadius; + protected final KDTree tree; + protected final T value; + protected final T tmp; + protected final DoubleUnaryOperator squaredRadiusFunction; + + public RadialKDTreeInterpolator( + final KDTree tree, + final DoubleUnaryOperator squaredRadiusFunction, + final double maxRadius, + T t) { + + super(tree.numDimensions()); + + this.squaredRadiusFunction = squaredRadiusFunction; + this.tree = tree; + this.search = new RadiusNeighborSearchOnKDTree(tree); + this.maxRadius = maxRadius; + this.maxSquaredRadius = maxRadius * maxRadius; + this.value = t.copy(); + this.tmp = t.copy(); + } + + public double getMaxRadius() { + + return maxRadius; + } + + @Override + public T get() { + + value.setZero(); + search.search(this, maxRadius, false); + if (search.numNeighbors() == 0) + return value; + + for (int i = 0; i < search.numNeighbors(); ++i) { + // can't multiply the value returned + tmp.set(search.getSampler(i).get()); + tmp.mul(squaredRadiusFunction.applyAsDouble(search.getSquareDistance(i))); + value.add(tmp); + } + return value; + } + + @Override + public RadialKDTreeInterpolator copy() { + + return new RadialKDTreeInterpolator(tree, squaredRadiusFunction, maxRadius, value); + } + + @Override + public RadialKDTreeInterpolator copyRealRandomAccess() { + + return copy(); + } + +} diff --git a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java new file mode 100644 index 000000000..41e9242cb --- /dev/null +++ b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java @@ -0,0 +1,45 @@ +package net.imglib2.interpolation.neighborsearch; + +import java.util.function.DoubleUnaryOperator; + +import net.imglib2.KDTree; +import net.imglib2.RealInterval; +import net.imglib2.RealRandomAccess; +import net.imglib2.interpolation.InterpolatorFactory; +import net.imglib2.type.numeric.NumericType; + +/** + * An {@link InterpolatorFactory} for {@link KDTree}s using a radial function. + *

+ * The resulting {@link RealRandomAccess} produced by this InterpolatorFactory + * returns values as a linear combination of all points within a certain radius. + * The contribution of each point is weighted according to a function of its + * squared radius. + */ +public class RadialKDTreeInterpolatorFactory> implements InterpolatorFactory> { + + protected final double maxRadius; + protected final DoubleUnaryOperator squaredRadiusFunction; + protected final T val; + + public RadialKDTreeInterpolatorFactory( + final DoubleUnaryOperator squaredRadiusFunction, final double maxRadius, T t) { + + this.maxRadius = maxRadius; + this.squaredRadiusFunction = squaredRadiusFunction; + this.val = t; + } + + @Override + public RadialKDTreeInterpolator create(final KDTree tree) { + + return new RadialKDTreeInterpolator(tree, squaredRadiusFunction, maxRadius, val); + } + + @Override + public RealRandomAccess create(final KDTree tree, final RealInterval interval) { + + return create(tree); + } + +} From 89da3dff53c44522c69baee020d44fba1ad4ef45 Mon Sep 17 00:00:00 2001 From: Stephan Saalfeld Date: Wed, 12 Jun 2024 08:56:04 -0400 Subject: [PATCH 2/2] style: apply imglib2 style --- .../RadialKDTreeInterpolator.java | 61 ++++++++++--------- .../RadialKDTreeInterpolatorFactory.java | 24 ++++---- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java index d17631528..e4615dc69 100644 --- a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java +++ b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolator.java @@ -5,76 +5,81 @@ import net.imglib2.KDTree; import net.imglib2.RealPoint; import net.imglib2.RealRandomAccess; -import net.imglib2.interpolation.InterpolatorFactory; import net.imglib2.neighborsearch.RadiusNeighborSearch; import net.imglib2.neighborsearch.RadiusNeighborSearchOnKDTree; import net.imglib2.type.numeric.NumericType; /** - * A {@link RealRandomAccess} for {@link KDTree}s using a radial function used by - * {@link RadialKDTreeInterpolatorFactory}. + * A {@link RealRandomAccess} for {@link KDTree}s using a radial function used + * by {@link RadialKDTreeInterpolatorFactory}. */ -public class RadialKDTreeInterpolator> extends RealPoint implements RealRandomAccess { - +public class RadialKDTreeInterpolator< T extends NumericType< T > > extends RealPoint implements RealRandomAccess< T > +{ protected static final double minThreshold = Double.MIN_VALUE * 1000; - protected final RadiusNeighborSearch search; + protected final RadiusNeighborSearch< T > search; + protected final double maxRadius; + protected final double maxSquaredRadius; - protected final KDTree tree; + + protected final KDTree< T > tree; + protected final T value; + protected final T tmp; + protected final DoubleUnaryOperator squaredRadiusFunction; public RadialKDTreeInterpolator( - final KDTree tree, + final KDTree< T > tree, final DoubleUnaryOperator squaredRadiusFunction, final double maxRadius, - T t) { - - super(tree.numDimensions()); + final T t ) + { + super( tree.numDimensions() ); this.squaredRadiusFunction = squaredRadiusFunction; this.tree = tree; - this.search = new RadiusNeighborSearchOnKDTree(tree); + this.search = new RadiusNeighborSearchOnKDTree< T >( tree ); this.maxRadius = maxRadius; this.maxSquaredRadius = maxRadius * maxRadius; this.value = t.copy(); this.tmp = t.copy(); } - public double getMaxRadius() { - + public double getMaxRadius() + { return maxRadius; } @Override - public T get() { - + public T get() + { value.setZero(); - search.search(this, maxRadius, false); - if (search.numNeighbors() == 0) + search.search( this, maxRadius, false ); + if ( search.numNeighbors() == 0 ) return value; - for (int i = 0; i < search.numNeighbors(); ++i) { + for ( int i = 0; i < search.numNeighbors(); ++i ) + { // can't multiply the value returned - tmp.set(search.getSampler(i).get()); - tmp.mul(squaredRadiusFunction.applyAsDouble(search.getSquareDistance(i))); - value.add(tmp); + tmp.set( search.getSampler( i ).get() ); + tmp.mul( squaredRadiusFunction.applyAsDouble( search.getSquareDistance( i ) ) ); + value.add( tmp ); } return value; } @Override - public RadialKDTreeInterpolator copy() { - - return new RadialKDTreeInterpolator(tree, squaredRadiusFunction, maxRadius, value); + public RadialKDTreeInterpolator< T > copy() + { + return new RadialKDTreeInterpolator< T >( tree, squaredRadiusFunction, maxRadius, value ); } @Override - public RadialKDTreeInterpolator copyRealRandomAccess() { - + public RadialKDTreeInterpolator< T > copyRealRandomAccess() + { return copy(); } - } diff --git a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java index 41e9242cb..a4463202a 100644 --- a/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java +++ b/src/main/java/net/imglib2/interpolation/neighborsearch/RadialKDTreeInterpolatorFactory.java @@ -16,30 +16,32 @@ * The contribution of each point is weighted according to a function of its * squared radius. */ -public class RadialKDTreeInterpolatorFactory> implements InterpolatorFactory> { - +public class RadialKDTreeInterpolatorFactory< T extends NumericType< T > > implements InterpolatorFactory< T, KDTree< T > > +{ protected final double maxRadius; + protected final DoubleUnaryOperator squaredRadiusFunction; + protected final T val; public RadialKDTreeInterpolatorFactory( - final DoubleUnaryOperator squaredRadiusFunction, final double maxRadius, T t) { - + final DoubleUnaryOperator squaredRadiusFunction, + final double maxRadius, final T t ) + { this.maxRadius = maxRadius; this.squaredRadiusFunction = squaredRadiusFunction; this.val = t; } @Override - public RadialKDTreeInterpolator create(final KDTree tree) { - - return new RadialKDTreeInterpolator(tree, squaredRadiusFunction, maxRadius, val); + public RadialKDTreeInterpolator< T > create( final KDTree< T > tree ) + { + return new RadialKDTreeInterpolator< T >( tree, squaredRadiusFunction, maxRadius, val ); } @Override - public RealRandomAccess create(final KDTree tree, final RealInterval interval) { - - return create(tree); + public RealRandomAccess< T > create( final KDTree< T > tree, final RealInterval interval ) + { + return create( tree ); } - }