Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indexToPosition when index == -1 #51

Closed
wants to merge 1 commit into from

Conversation

hinerm
Copy link
Member

@hinerm hinerm commented Feb 20, 2014

The -1 index is used throughout ImgLib2 to signify a position one step before the starting position. However, the indexToPosition calculations in IntervalIndexer were not taking the -1 index into account, producing incorrect position arrays for this index.

This patch fixes the indexToPosition calculations. To avoid copying and pasting the fix throughout all signatures, the array-based methods were refactored to funnel through a single method using SciJava-Common PrimitiveArrays.

@tpietzsch
Copy link
Member

Where exactly is the problem with the current version?
I just tried IntervalIndexer.indexToPosition( -1, new int[] { 5, 3, 2 }, pos );
and the outcome is pos = {-1,0,0} as expected.

@ctrueden
Copy link
Member

@hinerm: Sounds like a job for a unit test!

@tpietzsch
Copy link
Member

Please note, that the current implementation also has the property, that indexToPosition( index, dim, pos) satisfies positionToIndex( pos, dim ) == index for all values, also negative ones (not only for index = -1). This is a property that should hold with any new implementation.

I also have reservations about possible performance implications of the proposed implementation as it incurs the cost of at least two object creations and at least one polymorphic virtual function call. So before considering to merge this I'd like to see a benchmark.
But this is only my second concern really, because I find the current behaviour is correct. Can you explain, what the use case is, where it is a problem?

The array-based indexToPosition methods were essentially all repeating
one another. It was discovered that these methods did not handle the
special "-1" index appropriately, which is supposed to signify that the
position "will be at the origin after one fwd() call", essentially.

Instead, these methods would use the standard index to position
algorithm, which would create incorrect arrays (e.g. if the array was
[1, 1, 5] the -1 index would give position [0, 0, 1] instead of [-1, 0,
0] as desired.

All array-based indexToPosition methods now funnel through a single new
SciJava PrimitiveArray signature. This method can operate on all arrays
agnostically, thus the logic for creating a starting position when given
index=-1 was added here as well.

Note that the base primitive array accessor methods cause autoboxing,
thus specialized casting methods were created to access the underlying
primitive arrays directly.
@hinerm hinerm closed this Feb 25, 2014
@hinerm hinerm deleted the consolidate-index-to-position branch February 25, 2014 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants