We define the Juliaset as a function in 2D real space using a BiConsumer lambda function. The BiConsumer receives two parameters, the first one (x) is the 2D coordinate, the second one (fx) is the target of the function whose value will be set in place, here we use an UnsignedByteType. We also have to provide a Supplier for instances of the target such that multiple threads can each create their own.
The result is a function over continuous coordinates that is unbounded.
Code
importbdv.util.*;importbdv.util.volatiles.*;importbdv.viewer.*;importnet.imglib2.*;importnet.imglib2.algorithm.gauss3.*;importnet.imglib2.cache.img.*;importnet.imglib2.loops.*;importnet.imglib2.parallel.*;importnet.imglib2.position.*;importnet.imglib2.realtransform.*;importnet.imglib2.type.numeric.*;importnet.imglib2.type.numeric.integer.*;importnet.imglib2.util.*;importstatic net.imglib2.view.fluent.RandomAccessibleIntervalView.Extension.*;var juliaset =new FunctionRealRandomAccessible<UnsignedByteType>(2,(x, fx)->{int i =0;double v =0;double c = x.getDoublePosition(0);double d = x.getDoublePosition(1);for(; i <255&& v <4096;++i){finaldouble e = c * c - d * d; d =2* c * d; c = e +0.3; d +=0.6; v =Math.sqrt(c * c + d * d);++i;} fx.set(i);}, UnsignedByteType::new);BdvSource bdv = BdvFunctions.show( juliaset, Intervals.createMinMax(-1,-1,1,1),"juliaset", Bdv.options().is2D());bdv.setDisplayRange(0,127);
Caching results of expensive operations
Trying to show benefits of caching with very contrived example…
Use the juliaset from above. To have something that can be put in a cache, we rasterize (virtually)
It is relatively expensive (not really, but use your imagination) to compute the value of a pixel in transformed. And the value is re-computed everytime it is accessed.
To avoid that, we can wrap that into a CachedCellImg. Pixel values are computed once and cached for subsequent accesses. (The CachedCellImg pre-computes whole blocks of data when a single pixel from the block is accessed. This is often what you want, but you should be aware of it…)
We define two additional CachedCellImgs: one that generates data by smoothing the procedural image, one that generates data by smoothing the cached image.