bogovicj / transforms_tutorial

ImageJ examples for the 2019 DAIS Learnathon.
Apache License 2.0
0 stars 1 forks source link

FunctionRealRandomAccessible question #5

Closed tischi closed 3 years ago

tischi commented 3 years ago

@bogovicj @axtimwalde

I have a question about RandomAccessible in general.

It is about the public T get() method: How often is that called for one instance of a RandomAccessible? I though one could call this multiple times (while changing the position of the RealRandomAccess).

But, if that is true, then I do not understand why you are not returning a new type.copy(); each time the get method is called? see here: https://github.com/bogovicj/transforms_tutorial/blob/5d9194e38898f942f00fe5e76dd4ca823aa6281d/src/main/java/net/imglib2/interpolation/neighborsearch/RBFInterpolator.java#L71

Currently, you are just creating this copy once in the constructor of the RealRandomAccess. I don't understand how this is supposed to work, because subsequent calls of the get method would override the content of the type variable.

The same is true (and I have the same question) for the code in FunctionRealRandomAccessible.

tischi commented 3 years ago

I got my answer:

* <p>
     * WARNING: The return value is invalidated by next call to
     * {@link #setPositionAndGet}  or {@link #setPosition}.
     * <p>
     * <pre>
     * {@code
     * // This is wrong!!!
     * a = randomAccess.setPositionAndGet( positionA );
     * b = randomAccess.setPositionAndGet( positionB ); // this invalidates "a" !!!
axtimwalde commented 3 years ago

You have to consider the position (RandomAccess, Cursor, RealRandomAccess) and the corresponding T a unit. This is necessary to NOT have to create new pixel instances for every position but to forward this to pointer-like proxies when this is possible. If you need a copy for something later on, you have to make it, but you will find that you may want to carry around a temporary T instance that you re-use for such purposes. Same reason, you do not want to make new instances for every pixel because slow and expensive.