-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add declusteredEvent to EntityCluster #12920
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
base: main
Are you sure you want to change the base?
Add declusteredEvent to EntityCluster #12920
Conversation
|
Thank you for the pull request, @Oko-Tester! ✅ We can confirm we have a CLA on file for you. |
746701c to
2d42fdc
Compare
|
this is ready |
Fixes CesiumGS#5760 by adding declusteredEvent that provides both clustered and declustered entities, plus new API methods for accessing clustering state. Maintains backward compatibility.
… skipping unused declustering computations
9bd82e9 to
587e235
Compare
|
@mzschwartz5 Is there any news regarding the approval? |
|
Hi @Oko-Tester, I haven't had a chance to review this yet. It might not happen until Thursday as I'm out tomorrow and Wednesday. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Oko-Tester,
Thanks for the PR. I have a number of mostly minor comments about code style and performance, but those might be irrelevant because I'm a little unsure of the overall approach. It's a lot of extra state that we're tracking now, which means more memory consumed, more bookkeeping to sync arrays, more room for potential edge cases.
In the linked issue you said "clusterEvent does not fire when a cluster becomes empty, which makes it impossible to detect de-clustered entities." My intuition is that there's a better way to deal with this than to track more state and manually fire the decluster event accordingly. I haven't actually looked at the code closely to tell, but that's my instinct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not an expert on unit testing philosphy, so maybe this is a bad suggestion, but could we maybe have unit test(s) that check that clustering generally works for all three of: points, billboards, and labels (collections)?
I'm just a little wary given the discussion about using the item.show property as if these classes all have a shared abstract interface (which they don't). I don't expect that to be an issue, but if someone were to ever change that property name on one of the classes, at least having a failing unit test here would prevent the issue.
|
Hey @mzschwartz5, As for your comment on unit tests, I am not an expert in this field either, but it seems sensible to me to use them to avoid potential future problems. |
|
Okay, I rethought my current implementation and realized that I had adapted the component too much for my specific needs. I’ve reset the EntityCluster component locally and will commit the new changes shortly after testing them. I also need to update the tests to match the new implementation. I’d really appreciate your feedback on this new approach once the changes are pushed. |
|
@Oko-Tester When you're ready, just re-request review from me and/or leave a comment saying you're ready for another pass. Depending on when it's ready, I may need a few days before I can take another look. |
| if (newClusters.length > 0 && defined(clusteredLabelCollection)) { | ||
| for (let c = 0; c < clusteredLabelCollection.length; ++c) { | ||
| const clusterLabel = clusteredLabelCollection.get(c); | ||
| if (defined(clusterLabel) && defined(clusterLabel.id)) { | ||
| currentlyClusteredIds.push(...clusterLabel.id); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why do we need to check
newClusters.length > 0? - Will it ever be the case that
clusterLabelorclusterLabel.idis not defined? When? - Why do we need to use the spread operator (
...) when pushing an ID? To make a copy of it (if so, why isn't a reference sufficient?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why check newClusters.length > 0?
This check is redundant. If there are no clusters, clusteredLabelCollection.length would be 0 anyway, so the loop wouldn't execute. I'll remove this check. - Will clusterLabel or clusterLabel.id ever be undefined?
In normal operation, no. clusterLabel.id is always set in addCluster(). I think keeping these defensive checks for safety would be good. - Why use the spread operator (...)?
Because clusterLabel.id is an array of entity IDs (set in addCluster()), not a single ID. The spread operator flattens it. Without it, you'd have an array of arrays instead of a flat array of IDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Random drive-by comment:
In normal operation, no. clusterLabel.id is always set in addCluster(). I think keeping these defensive checks for safety would be good.
When something should never be undefined, then this should be a contract that people can rely on. When one of these things is ever undefined, then this is a bug, and it will show up as a crash, with a clear stack trace. With these checks, it will not show up with a stack trace. But it will still be a bug. And this bug will only show up as an issue like "Sometimes some labels are not properly shown in some clusters at some zoom level" or so. And one can easily spend 5 or 10 hours of painful debugging to find the reason for an issue like that.
I know, there are different philosophies. But there's an important difference between being "defensive"/"resilient", and what I refer to as "fail silent".
"Defensive" would mean that there is a defined check, and when the thing is undefined, then it does not crash, but it still logs an error to the console, saying "This is undefined, but it never should be". Ideally, this should not be necessary within a library. It should only be necessary for things that are provided from outside, via the public API. But... that ship has sailed.
"Fail silent" means that something is wrong, and there's not way to figure out what is wrong. That's not good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I didn’t consider that. I think it was mentioned earlier in this PR, but you’re right, better to fail visibly than silently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so sounds like we can address points 1 and 2 easily.
For point 3 (the spread operator) - are we copying the all currently clustered IDs (i.e. every ID of every entity that is clustered) on every declutter callback? That seems like it could be bad for performance. It also doubles the memory we consume in tracking which entities are clustered (since we always have a copy of the previouslyClustered...)
Side note: does this only fire a declustered event for clusteredLabelCollection? But not for clusteredBillboardCollection or clusteredPointCollection?
| this._removeEventListener = undefined; | ||
|
|
||
| this._clusterEvent = new Event(); | ||
| this._declusterEvent = new Event(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is a bit misleading. clusterEvent fires whenever a new cluster is added, right? But declusterEvent only fires when all clusters are removed. (If I've understood this PR correctly)
It seems like declusterEvent should also fire whenever a cluster is removed. (I don't know if that's feasible to implement, though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct about the asymmetry. The behavior is as follows:
clusterEvent: Triggered every time addCluster is called, which happens in the following cases:
When new clusters are created
When existing clusters are reused (when the camera zooms in and they still meet the minimumClusterSize threshold)
declusterEvent: Only fires when transitioning from "has clusters" to "no clusters at all". It does NOT fire when individual clusters are removed.
However, users can still detect which clusters have been removed by tracking the entity IDs themselves. Since clusterEvent is triggered every frame with the current cluster composition, you can compare frame by frame to determine which entities were declustered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A wild guess is that this question may have to do with the name.
When you have methods like
addSomething/removeSomething or
createSomething/destroySomething
then you'd also expect a certain "symmetry".
(And when you have addSomething, removeSomething, and deleteSomething, you'll wonder: Wait what...?)
In this case, one could expect this event to be raises when ... the opposite of that happened what causes a clusterEvent. Yes, there is the documentation that says that this event "... will be raised when all entities have been declustered.", but still.
@mzschwartz5 It's up to you, but I think that options could be to
- rename this event (to something like
declusteredAllEventorallDeclusteredEventor ... whatever is appropriate here) - and (!!!optional!!!) consider adding a
declusterEventthat indeed is the opposite of whatclusterEventis doing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you’re right about the naming confusion. I think we should rename the event to lastDeclusterEvent (or allDeclusteredEvent, whatever you prefer) so it’s clear that it only fires when the last cluster is removed. But there’s no real need for a new event that tracks every declustering — users can already detect removed clusters by comparing the cluster composition from the clusterEvent. So I don’t think an additional event for every declustering is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not deeply involved here - it was just a comment while looking at my GitHub notifications. So I don't know in how far it makes sense to define an event for individual removed clusters. But... (again: a bit shallow)...:
users can already detect removed clusters by comparing the cluster composition from the clusterEvent. So I don’t think an additional event for every declustering is necessary.
Users can do a lot of stuff. Users can track this and that and store this and that and compute some difference here and there. The question is whether there's a convincing use case for being informed about individual clusters. If there is, then uers should not be forced to write 200 lines of (possibly buggy) code to derive the information they need, when they could just listen for a single, predefined event.
Maybe there is no such use case. And in doubt, this new event could be added later, when the demand arises. And following the mantra: "When in doubt, leave it out!", it's probably OK to omit it for now (and certainly in the context of this PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally agree with @javagl, I and I think it would be sufficient for this PR to simply rename this event. I liked the proposed "allDeclusteredEvent"
| * }); | ||
| */ | ||
| /** | ||
| * A event listener function used when all entities have been declustered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small typo- "A event" --> "An event"
Fixes #5760 by adding declusteredEvent that provides both clustered and declustered entities, plus new API methods for accessing clustering state. Maintains backward compatibility.
Description
This PR adds comprehensive clustering state information to EntityCluster by introducing a new
declusteredEventand supporting API methods. The originalclusterEventonly provided information about entities that were successfully clustered, but developers also needed access to entities that were processed but not clustered (declustered entities).Key Changes
declusteredEvent: Fires with complete clustering information including both clustered and declustered entitiesgetLastClusteredEntities()- Returns currently clustered entitiesgetLastDeclusteredEntities()- Returns currently declustered entitiesgetAllProcessedEntities()- Returns all entities processed during clusteringclusterEventremains unchanged and functionalIssue number and link
Fixes #5760 - Include declustered entities in cluster callback
This addresses multiple user requests from the forum and GitHub comments spanning several years.
Testing plan
Manual Testing
declusteredEventfires with correctclusteredanddeclusteredarraysclusterEventstill works as beforeAuthor checklist
CONTRIBUTORS.mdCHANGES.mdwith a short summary of my change