8000 Improve WritablePolygon2D and WritablePolyline by ctrueden · Pull Request #35 · imglib/imglib2-roi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve WritablePolygon2D and WritablePolyline #35

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

Merged
merged 12 commits into from
Sep 21, 2018
Merged

Conversation

ctrueden
Copy link
Member

This branch makes the following improvements:

  • Polygon2D and Polyline common API is now inherited from Polyshape interface.
  • WritablePolygon2D and WritablePolyline common API is now inherited from WritablePolyshape interface. The WritablePolygon2D#addVertex(int, double[]) method is deprecated in favor of WritablePolyshape#addVertex(int, RealLocalizable).
  • DefaultWritablePolygon2D#addVertex, DefaultWritablePolyline#addVertex and DefaultWritableRealPointCollection#addPoint now update the bounds based on the new point only, avoiding an O(n) bounds rebuild. (RealPointSampleListWritableRealPointCollection did not suffer from this issue.)
  • WritablePolyshape gained an addVertices method for adding many vertices at once. This bulk operation enables the default implementations to perform better, expanding their internal storage once as appropriate, rather than doing repeated incremental expansions.
  • Add methods to GeomMaths to compare Localizable and RealLocalizable positions. (Maybe these should go into net.imglib2.util.Util? What do others think?)
  • The WritablePolygon2DTest code has been improved a bit.

Note that WritableRealPointCollection still does not have addPoints or removePoints methods; I considered doing them as part of this PR, but I have no immediate use case, so I've held off for now.

With these changes, it will be more feasible to pass WritablePolyshape objects as outputs into which calculated points can be injected. In particular, it will become feasible to recast ImageJ Ops's geometry operations such as geom.convexHull(Polygon2D) as computer ops rather than function ops.

@awalter17
Copy link
Contributor

@ctrueden Thank you for your work on this (and cleaning up some of my tests)!

Add methods to GeomMaths to compare Localizable and RealLocalizable positions. (Maybe these should go into net.imglib2.util.Util? What do others think?)

I think these methods should go in net.imglib2.util.Util, because they seem more general than GeomMaths.

Aside from that though, I think these changes are wonderful and (pending a decision on the above) are good to merge. Thoughts @tpietzsch?

@ctrueden
Copy link
Member Author
ctrueden commented Jun 6, 2018

Thanks for reviewing, @awalter17. I filed imglib/imglib2#211 to add the locationsEqual methods to Util in ImgLib2 core. @tpietzsch If you like it, we can leave this PR here until it is merged and released and then update the PR accordingly. Or if not, close it and I'll put those methods wherever you deem best.

@ctrueden
Copy link
Member Author

Rebased over the latest master. I also added a commit cleaning up the GeomMasks javadoc. And I removed the GeomMaths.locationsEqual methods in favor of Util.locationsEqual from imglib/imglib2#211. This PR depends on imglib/imglib2#211 being merged and released first.

Implementations of this interface are always 2-dimensional.
It now lives in a shared Polyshape interface.
It now lives in a shared WritablePolyshape interface.

And the addVertex(int, double[]) method is deprecated
in favor of addVertex(int, RealLocalizable).
Now it repeatedly delegates to an expandMinMax method
allowing to grow the bounds vertex by vertex.

This will come in handy shortly for performance reasons.
When repeatedly calling addVertex, the bounds were being recomputed
from the entire (growing) collection of vertices every time.
Now, we only expand the bounds as needed based on the new vertex.

See: https://gitter.im/imglib/imglib2?at=5ae1d3f32d0e228d7bb52ea7
This method allows to add a collection of vertices all at once.
One big advantage of supporting this is performance: it avoids repeated
reallocation of internal data structures, since it is known in advance
how many vertices are intended to be added in the batch.

For DefaultWritablePolygon2D, the x and y Trove-based fields needed
extension to support one-shot right-shifting of the arrays to enable
performant mass injection of new values.

For DefaultWritablePolyline, the vertices field needed more specific
typing on ArrayList to make use of its ensureCapacity method.
The order is (expected, actual) not vice versa.

While at it, also call assertEquals(long, long) rather than
assertEquals(double, double, double) for integer values.
The javadoc was promising specific subtypes would be returned, but the
methods were not actually typed on those specific subtypes. Let's back
off on those promises, and instead explain things in an interface-driven
way: in general, the closed* methods give a ROI with CLOSED boundary
type, while the open* methods give a ROI with OPEN boundary type.

This commit does narrow the return type of kDTreeRealPointCollection and
realPointSampleListRealPointCollection, since it is helpful (especially
for the RealPointSampleListRealPointCollection) to have the properly
typed object for subsequent API calls like addPoint.
@ctrueden ctrueden merged commit dc8b706 into master Sep 21, 2018
@ctrueden ctrueden deleted the polyshape-api branch September 21, 2018 19:05
@imagejan
Copy link
Member

The build on master failed because of a typo in the javadoc here: @Link -> @link

@ctrueden
Copy link
Member Author

Fixed with 07f5716

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.

3 participants
0