Skip to content

Commit 5654283

Browse files
added dummy-implementation of prefetch for underlying images that are not CellImgs assuming a 128x128x64 blocksize
1 parent 3752739 commit 5654283

File tree

1 file changed

+106
-57
lines changed

1 file changed

+106
-57
lines changed

src/main/java/net/preibisch/bigstitcher/spark/util/ViewUtil.java

Lines changed: 106 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import net.imglib2.util.Util;
5151
import net.imglib2.view.IntervalView;
5252
import net.imglib2.view.MixedTransformView;
53+
import util.Grid;
5354

5455
public class ViewUtil
5556
{
@@ -200,72 +201,120 @@ public static String viewIdToString(final ViewId viewId) {
200201
transformToSource = null;
201202
}
202203

203-
// TODO: this fails for the AllenOMEZarrLoader because the RandomAccessibleInterval< ? > img is .view().slice( 4, 0 ).slice( 3, 0 );
204+
// this failed for the AllenOMEZarrLoader because the RandomAccessibleInterval< ? > img is .view().slice( 4, 0 ).slice( 3, 0 );
205+
// TODO: I added some copy of the code below that assumes a 128x128x64 blocksize, this should be fixed. But better this than a non-working code ...
204206
if ( ! ( rai instanceof AbstractCellImg ) )
205207
{
206-
throw new IllegalArgumentException( "TODO. Handling source types other than CellImg is not implemented yet, rai is=" + rai.getClass().getName() );
207-
}
208+
// use some random blocksize and fetch for all
209+
final List<long[][]> grid = Grid.create( new FinalInterval( img ).dimensionsAsLongArray(), new int[] { 128, 128, 64 } );
208210

209-
// Brute force search for overlapping cells:
210-
//
211-
// For each grid cell, estimate its bounding box in world space and test
212-
// for intersection with fusedBlock
213-
//
214-
// TODO: BigVolumeViewer has a more sophisticated method for
215-
// intersecting the View Frustum with the source grid and
216-
// determining overlapping cells. This is similar and could be
217-
// re-used here to make the search more efficient. It should
218-
// provide more accurate results because it uses non-axis aligned
219-
// planes for intersection. See class FindRequiredBlocks.
220-
//
221-
// TODO: The following works for the hyperslice views currently produced by ZarrImageLoader
222-
// (from https://github.com/bigdataviewer/bigdataviewer-omezarr)
223-
// However, for the general case, the logic should be inverted:
224-
// Project the "fused" bounding box into source coordinates (see
225-
// above), because that is well-defined.
226-
// In contrast, the code below performs a projection onto the
227-
// "fused" hyper-slice, which can lead to non-required blocks
228-
// being loaded.
229-
230-
// iterate all cells (intervals) in grid
231-
final CellGrid grid = ( ( AbstractCellImg< ?, ?, ?, ? > ) rai ).getCellGrid();
232-
233-
final int n = grid.numDimensions();
234-
final long[] gridPos = new long[ n ];
235-
final BoundingBox cellBBox = new BoundingBox( n );
236-
final long[] cellMin = cellBBox.corner1;
237-
final long[] cellMax = cellBBox.corner2;
238-
239-
final BoundingBox projectedCellBBox;
240-
final Interval projectedCellInterval;
241-
final int m = img.numDimensions(); // should be always ==3
242-
projectedCellBBox = new BoundingBox( m );
243-
projectedCellInterval = FinalInterval.wrap( projectedCellBBox.corner1, projectedCellBBox.corner2 );
244-
245-
final IntervalIterator gridIter = new LocalizingIntervalIterator( grid.getGridDimensions() );
246-
while( gridIter.hasNext() )
247-
{
248-
gridIter.fwd();
249-
gridIter.localize( gridPos );
250-
grid.getCellInterval( gridPos, cellMin, cellMax );
211+
final int n = img.numDimensions();
212+
final BoundingBox cellBBox = new BoundingBox( n );
213+
final long[] cellMin = cellBBox.corner1;
214+
final long[] cellMax = cellBBox.corner2;
251215

252-
if ( transformToSource == null )
216+
final BoundingBox projectedCellBBox;
217+
final Interval projectedCellInterval;
218+
final int m = img.numDimensions(); // should be always ==3
219+
projectedCellBBox = new BoundingBox( m );
220+
projectedCellInterval = FinalInterval.wrap( projectedCellBBox.corner1, projectedCellBBox.corner2 );
221+
222+
for ( final long[][] block : grid )
253223
{
254-
expand( cellBBox, expand, projectedCellBBox );
224+
final long[] offset = block[ 0 ];
225+
final long[] size = block[ 1 ];
226+
227+
for ( int d = 0; d < n; ++d )
228+
{
229+
cellMin[ d ] = offset[ d ];
230+
cellMax[ d ] = cellMin[ d ] + size[ d ] - 1;
231+
}
232+
233+
if ( transformToSource == null )
234+
{
235+
expand( cellBBox, expand, projectedCellBBox );
236+
}
237+
else
238+
{
239+
transform( transformToSource, projectedCellBBox, cellBBox );
240+
expand( projectedCellBBox, expand );
241+
}
242+
243+
final Interval bounds = Intervals.smallestContainingInterval(
244+
imgToWorld.estimateBounds( projectedCellInterval ) );
245+
246+
if ( overlaps( bounds, fusedBlock ) )
247+
{
248+
System.out.println( Arrays.toString( offset ) + ", " + Arrays.toString( size ) );
249+
250+
prefetch.add( new PrefetchPixel<>( rai, cellMin.clone() ) );
251+
}
255252
}
256-
else
253+
254+
//throw new IllegalArgumentException( "TODO. Handling source types other than CellImg is not implemented yet, rai is=" + rai.getClass().getName() );
255+
}
256+
else
257+
{
258+
// Brute force search for overlapping cells:
259+
//
260+
// For each grid cell, estimate its bounding box in world space and test
261+
// for intersection with fusedBlock
262+
//
263+
// TODO: BigVolumeViewer has a more sophisticated method for
264+
// intersecting the View Frustum with the source grid and
265+
// determining overlapping cells. This is similar and could be
266+
// re-used here to make the search more efficient. It should
267+
// provide more accurate results because it uses non-axis aligned
268+
// planes for intersection. See class FindRequiredBlocks.
269+
//
270+
// TODO: The following works for the hyperslice views currently produced by ZarrImageLoader
271+
// (from https://github.com/bigdataviewer/bigdataviewer-omezarr)
272+
// However, for the general case, the logic should be inverted:
273+
// Project the "fused" bounding box into source coordinates (see
274+
// above), because that is well-defined.
275+
// In contrast, the code below performs a projection onto the
276+
// "fused" hyper-slice, which can lead to non-required blocks
277+
// being loaded.
278+
279+
// iterate all cells (intervals) in grid
280+
final CellGrid grid = ( ( AbstractCellImg< ?, ?, ?, ? > ) rai ).getCellGrid();
281+
282+
final int n = grid.numDimensions();
283+
final long[] gridPos = new long[ n ];
284+
final BoundingBox cellBBox = new BoundingBox( n );
285+
final long[] cellMin = cellBBox.corner1;
286+
final long[] cellMax = cellBBox.corner2;
287+
288+
final BoundingBox projectedCellBBox;
289+
final Interval projectedCellInterval;
290+
final int m = img.numDimensions(); // should be always ==3
291+
projectedCellBBox = new BoundingBox( m );
292+
projectedCellInterval = FinalInterval.wrap( projectedCellBBox.corner1, projectedCellBBox.corner2 );
293+
294+
final IntervalIterator gridIter = new LocalizingIntervalIterator( grid.getGridDimensions() );
295+
while( gridIter.hasNext() )
257296
{
258-
transform( transformToSource, projectedCellBBox, cellBBox );
259-
expand( projectedCellBBox, expand );
297+
gridIter.fwd();
298+
gridIter.localize( gridPos );
299+
grid.getCellInterval( gridPos, cellMin, cellMax );
300+
301+
if ( transformToSource == null )
302+
{
303+
expand( cellBBox, expand, projectedCellBBox );
304+
}
305+
else
306+
{
307+
transform( transformToSource, projectedCellBBox, cellBBox );
308+
expand( projectedCellBBox, expand );
309+
}
310+
311+
final Interval bounds = Intervals.smallestContainingInterval(
312+
imgToWorld.estimateBounds( projectedCellInterval ) );
313+
314+
if ( overlaps( bounds, fusedBlock ) )
315+
prefetch.add( new PrefetchPixel<>( rai, cellMin.clone() ) );
260316
}
261-
262-
final Interval bounds = Intervals.smallestContainingInterval(
263-
imgToWorld.estimateBounds( projectedCellInterval ) );
264-
265-
if ( overlaps( bounds, fusedBlock ) )
266-
prefetch.add( new PrefetchPixel<>( rai, cellMin.clone() ) );
267317
}
268-
269318
// prefetch.forEach( System.out::println );
270319
return prefetch;
271320
}

0 commit comments

Comments
 (0)