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 polyline color issue involving duplicate positions #9676

Merged
merged 6 commits into from
Jul 19, 2021

Conversation

j9liu
Copy link
Contributor

@j9liu j9liu commented Jul 13, 2021

Fixes #9379. This PR changes how colors are handled in PolylineGeometry.createGeometry:

  • arrayRemoveDuplicates now accepts an optional removedIndices parameter. If any entries are removed from the original array during this function, the indices of those entries will be stored in removedIndices in sorted order.
  • createGeometry will use this array to remove the colors that correspond to those extraneous positions. This should work for both segment coloring and per-vertex coloring.

Additionally, I noticed that if wrapAround were set to true in the parameters, and if an array's first and last entries were equal, arrayRemoveDuplicates would remove the first entry. However, the examples in the documentation suggested that the last entry was supposed to be removed instead. I changed the function to match the example.


Segment Coloring

This sandcastle was linked in the issue to demonstrate the bug. It sets up the following polyline:

image

In the current version of Cesium, if the orange segment is collapsed, the remaining segments will be colored incorrectly as shown.

image

The changes in PR result in this polyline:

image


Vertex Coloring

We can modify the sandcastle to have per-vertex coloring like so.

image

If we try to remove the black vertex by making it a duplicate, in the current version of Cesium, we'll find that it just shifts over to the other vertices:

image

However, we can remove it with these PR's changes:

image

cc @lilleyse for review

EDIT: now that I'm thinking about it, in the per-vertex example it probably makes more sense for the orange point to be removed rather than the middle black vertex, because that preserves the original coloring. I'll work a fix tomorrow

@cesium-concierge
Copy link

Thanks for the pull request @j9liu!

  • ✔️ Signed CLA found.
  • CHANGES.md was not updated.
    • If this change updates the public API in any way, please add a bullet point to CHANGES.md.

Reviewers, don't forget to make sure that:

  • Cesium Viewer works.
  • Works in 2D/CV.
  • Works (or fails gracefully) in IE11.

Copy link
Contributor

@lilleyse lilleyse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed summary at the top of PR. It was good to see that you tested a bunch of different cases. And nice catch with the wrapAround bug.

EDIT: now that I'm thinking about it, in the per-vertex example it probably makes more sense for the orange point to be removed rather than the middle black vertex, because that preserves the original coloring. I'll work a fix tomorrow

Thank for pointing this out. I'll look out for your fix tomorrow.

Source/Core/arrayRemoveDuplicates.js Outdated Show resolved Hide resolved
Source/Core/arrayRemoveDuplicates.js Outdated Show resolved Hide resolved
}

return cleanedvalues;
return cleanedValues;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two separate sections of code that handle wrapAround can be combined. It should be fine to check the first and last element of values rather than cleanedValues, the answer ought to be the same.

Even nicer would be to combine all the code into a single for loop, but only if it can be done cleanly.

Specs/Core/PolylineGeometrySpec.js Show resolved Hide resolved
Source/Core/PolylineGeometry.js Outdated Show resolved Hide resolved
Source/Core/PolylineGeometry.js Outdated Show resolved Hide resolved
@j9liu
Copy link
Contributor Author

j9liu commented Jul 14, 2021

Context for just-committed changes: I ran into a case today where if I have an array [A, B, C, ...., A, A, A], the function deletes the last two A entries, and then it will also remove the final duplicate A when it sees wrapAround is true. But it doesn't properly account for the index of that A with respect to the original array; its index is length - 3, but the code will only push values.length - 1. Even if I used cleanValues.length - 1, the indices will be out of order because it takes care of this case at the very end.

This commit should take care of this case, but the wrapAround step is a check that happens at the end of the loop. I'm not sure how to cleanly integrate it into the existing for loop. Will work on PolylineGeometry.createGeometry next.

@j9liu
Copy link
Contributor Author

j9liu commented Jul 14, 2021

@lilleyse - Fix just pushed. Now the sandcastle should look like this.

image

I handled endpoint cases differently than cases where the point is in the middle of the polyline. If a polyline looks like [A, B, B, C] with the colors [red, green, blue, white], it arbitrarily the color of the point's first appearance, so the line's colors will become [red, green, white]. But when the point is on the end, then it makes more sense to have it drop the first-appearing color.

Source/Core/arrayRemoveDuplicates.js Outdated Show resolved Hide resolved
Source/Core/arrayRemoveDuplicates.js Outdated Show resolved Hide resolved
Source/Core/arrayRemoveDuplicates.js Outdated Show resolved Hide resolved
Source/Core/PolylineGeometry.js Outdated Show resolved Hide resolved
@j9liu
Copy link
Contributor Author

j9liu commented Jul 19, 2021

@lilleyse - made the changes you suggested; let me know if there's anything else that needs adjustment!

@lilleyse
Copy link
Contributor

Thanks, the updates look great!

@lilleyse lilleyse merged commit 9081c2c into main Jul 19, 2021
@lilleyse lilleyse deleted the polyline-color-bug branch July 19, 2021 15:18
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.

Polyline issue with per-vertex colors and duplicate position values
3 participants