Skip to content

Instantly share code, notes, and snippets.

@tpietzsch
Created May 31, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpietzsch/38b0e32dac6226a43e95c3e5fd41fb14 to your computer and use it in GitHub Desktop.
Save tpietzsch/38b0e32dac6226a43e95c3e5fd41fb14 to your computer and use it in GitHub Desktop.
package bdv.examples.interactivedog;
import java.util.ArrayList;
import java.util.List;
import net.imglib2.RealInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPoint;
import net.imglib2.RealRandomAccess;
import net.imglib2.RealRandomAccessible;
import net.imglib2.KDTree;
import net.imglib2.neighborsearch.NearestNeighborSearchOnKDTree;
/**
* An unbounded {@link RealRandomAccessible} that has value 1000 inside radius
* around specified set of points, and value 0 outside.
*
* @param <P>
*
* @author Tobias Pietzsch <tobias.pietzsch@gmail.com>
*/
public class RealPointsRealRandomAccessible< T, P extends RealLocalizable > implements RealRandomAccessible< T >
{
private final int n;
private final double radius;
private final T insideValue;
private final T outsideValue;
private final ArrayList< P > points;
private final KDTree< P > tree;
public RealPointsRealRandomAccessible( final List< P > pts, final double radius, final T insideValue, final T outsideValue )
{
assert pts.isEmpty() == false;
n = pts.get( 0 ).numDimensions();
this.radius = radius;
this.insideValue = insideValue;
this.outsideValue = outsideValue;
points = new ArrayList< P >( pts );
tree = new KDTree< P >( points, points );
}
@Override
public int numDimensions()
{
return n;
}
public RealInterval getBoundingBox()
{
return tree;
}
public class Access extends RealPoint implements RealRandomAccess< T >
{
private final NearestNeighborSearchOnKDTree< P > search;
public Access()
{
super( RealPointsRealRandomAccessible.this.n );
search = new NearestNeighborSearchOnKDTree< P >( tree );
}
protected Access( final Access a )
{
super( a );
search = a.search.copy();
}
@Override
public T get()
{
search.search( this );
return ( search.getDistance() <= radius ) ? insideValue : outsideValue;
}
@Override
public Access copy()
{
return new Access( this );
}
@Override
public Access copyRealRandomAccess()
{
return copy();
}
}
@Override
public RealRandomAccess< T > realRandomAccess()
{
return new Access();
}
@Override
public RealRandomAccess< T > realRandomAccess( final RealInterval interval )
{
return new Access();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment