From 58ff3cc6178c6e68b4ad6bea3f74c0e785a0fb5e Mon Sep 17 00:00:00 2001
From: John Bogovic <bogovicj@users.noreply.github.com>
Date: Thu, 24 Sep 2020 06:47:05 -0400
Subject: [PATCH] add flag to skip renderer's bounding box intersection (#110)

* add flag to skip renderer's bounding box culling
* RealRandomAccessibleSource skips render intersecton check by default
* flag is specified in a new constructor
* RealRandomAccessibleSource add bounding box check method
---
 .../tools/transformation/TransformedSource.java  |  6 ++++++
 .../bdv/util/RealRandomAccessibleSource.java     | 16 +++++++++++++++-
 src/main/java/bdv/viewer/Source.java             | 16 ++++++++++++++++
 .../java/bdv/viewer/render/VisibilityUtils.java  |  6 ++++++
 4 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/main/java/bdv/tools/transformation/TransformedSource.java b/src/main/java/bdv/tools/transformation/TransformedSource.java
index 4c91204a..0d84ea5c 100644
--- a/src/main/java/bdv/tools/transformation/TransformedSource.java
+++ b/src/main/java/bdv/tools/transformation/TransformedSource.java
@@ -121,6 +121,12 @@ public class TransformedSource< T > implements Source< T >, MipmapOrdering
 		this.composed = new AffineTransform3D();
 	}
 
+	@Override
+	public boolean doBoundingBoxCulling()
+	{
+		return source.doBoundingBoxCulling();
+	}
+
 	/*
 	 * EXTRA TRANSFORMATION methods
 	 */
diff --git a/src/main/java/bdv/util/RealRandomAccessibleSource.java b/src/main/java/bdv/util/RealRandomAccessibleSource.java
index 6c011e1a..7c337ee3 100644
--- a/src/main/java/bdv/util/RealRandomAccessibleSource.java
+++ b/src/main/java/bdv/util/RealRandomAccessibleSource.java
@@ -57,17 +57,25 @@ public abstract class RealRandomAccessibleSource< T extends Type< T > > implemen
 
 	protected final VoxelDimensions voxelDimensions;
 
+	protected final boolean doBoundingBoxIntersectionCheck;
+
 	public RealRandomAccessibleSource( final RealRandomAccessible< T > accessible, final T type, final String name )
 	{
-		this( accessible, type, name, null );
+		this( accessible, type, name, null, false );
 	}
 
 	public RealRandomAccessibleSource( final RealRandomAccessible< T > accessible, final T type, final String name, final VoxelDimensions voxelDimensions )
+	{
+		this( accessible, type, name, voxelDimensions, false );
+	}
+
+	public RealRandomAccessibleSource( final RealRandomAccessible< T > accessible, final T type, final String name, final VoxelDimensions voxelDimensions, final boolean doBoundingBoxIntersectionCheck )
 	{
 		this.accessible = accessible;
 		this.type = type.createVariable();
 		this.name = name;
 		this.voxelDimensions = voxelDimensions;
+		this.doBoundingBoxIntersectionCheck = doBoundingBoxIntersectionCheck;
 	}
 
 	@Override
@@ -125,4 +133,10 @@ public abstract class RealRandomAccessibleSource< T extends Type< T > > implemen
 	{
 		return 1;
 	}
+
+	@Override
+	public boolean doBoundingBoxCulling()
+	{
+		return doBoundingBoxIntersectionCheck;
+	}
 }
diff --git a/src/main/java/bdv/viewer/Source.java b/src/main/java/bdv/viewer/Source.java
index 9cd100c4..63885f00 100644
--- a/src/main/java/bdv/viewer/Source.java
+++ b/src/main/java/bdv/viewer/Source.java
@@ -68,6 +68,22 @@ public interface Source< T >
 	 */
 	public RandomAccessibleInterval< T > getSource( int t, int level );
 
+	/**
+	 * Whether this source participates in bounding box culling.
+	 * <p>
+	 * If {@code true}, then this source will only be rendered if its bounding
+	 * box, i.e., the interval of {@link #getSource}, intersects the
+	 * current screen area (when transformed to viewer coordinates).
+	 * <p>
+	 * If {@code false}, then this source will be always rendered (if it is
+	 * set to be visible.)
+	 *
+	 * @return {@code true}, if this source participates in bounding box culling.
+	 */
+	public default boolean doBoundingBoxCulling()
+	{
+		return true;
+	}
 
 	/**
 	 * Get the 3D stack at timepoint index t, extended to infinity and interpolated.
diff --git a/src/main/java/bdv/viewer/render/VisibilityUtils.java b/src/main/java/bdv/viewer/render/VisibilityUtils.java
index ee222a2b..3568be69 100644
--- a/src/main/java/bdv/viewer/render/VisibilityUtils.java
+++ b/src/main/java/bdv/viewer/render/VisibilityUtils.java
@@ -83,6 +83,12 @@ class VisibilityUtils
 
 		for ( final SourceAndConverter< ? > source : sources )
 		{
+			if( !source.getSpimSource().doBoundingBoxCulling() )
+			{
+				result.add( source );
+				continue;
+			}
+
 			final Source< ? > spimSource = source.getSpimSource();
 			final int level = MipmapTransforms.getBestMipMapLevel( screenTransform, spimSource, t );
 			spimSource.getSourceTransform( t, level, sourceToScreen );
-- 
GitLab