Skip to content

Conversation

Oko-Tester
Copy link

@Oko-Tester Oko-Tester commented Sep 25, 2025

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 declusteredEvent and supporting API methods. The original clusterEvent only 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

  • New declusteredEvent: Fires with complete clustering information including both clustered and declustered entities
  • New API methods:
    • getLastClusteredEntities() - Returns currently clustered entities
    • getLastDeclusteredEntities() - Returns currently declustered entities
    • getAllProcessedEntities() - Returns all entities processed during clustering
  • Enhanced event coverage: Events now fire when clustering is disabled or clusters become empty
  • Backward compatibility: Original clusterEvent remains unchanged and functional

Issue 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

  1. Create CustomDataSource with clustering enabled
  2. Add entities at various distances to create both clustered and declustered entities
  3. Verify declusteredEvent fires with correct clustered and declustered arrays
  4. Disable clustering and verify event fires with all entities as declustered
  5. Test API methods return correct entity lists
  6. Verify original clusterEvent still works as before

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • I have updated CHANGES.md with a short summary of my change
  • I have added or updated unit tests to ensure consistent code coverage
  • I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

Copy link

Thank you for the pull request, @Oko-Tester!

✅ We can confirm we have a CLA on file for you.

@Oko-Tester Oko-Tester force-pushed the fix-issue-5760-declustered-entities branch from 746701c to 2d42fdc Compare September 26, 2025 12:07
@Oko-Tester Oko-Tester marked this pull request as ready for review September 26, 2025 12:07
@Oko-Tester
Copy link
Author

this is ready

javagl and others added 6 commits September 29, 2025 10:42
Fixes CesiumGS#5760 by adding declusteredEvent that provides both clustered
and declustered entities, plus new API methods for accessing
clustering state. Maintains backward compatibility.
@Oko-Tester Oko-Tester force-pushed the fix-issue-5760-declustered-entities branch from 9bd82e9 to 587e235 Compare September 29, 2025 08:43
@Oko-Tester
Copy link
Author

Oko-Tester commented Oct 4, 2025

@mzschwartz5 Is there any news regarding the approval?

@mzschwartz5
Copy link
Contributor

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.

Copy link
Contributor

@mzschwartz5 mzschwartz5 left a 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.

* Returns all entities that were processed in the most recent clustering operation.
* @returns {Entity[]} Array of all processed entities
*/
EntityCluster.prototype.getAllProcessedEntities = function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an opinion, but I feel like the name of this API could use some work shopping - before I even suggest an alternative, though, let me ask: does "all" entities mean all entities that are in clusters after the most recent operation, or also entities that are declustered?

Copy link
Contributor

Choose a reason for hiding this comment

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

(And similarly rename the internal array)

* @returns {Entity[]} Array of entities that were clustered
*/
EntityCluster.prototype.getLastClusteredEntities = function () {
return this._lastClusteredEntities.slice();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to return a shallow copy of the internal array (via slice())? Why not just make a property / standard getter for these arrays?

Copy link
Contributor

@javagl javagl Oct 17, 2025

Choose a reason for hiding this comment

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

With zero context, I'll throw in the baseline comment: Minimize mutablilty and make ownership clear. It at least serves the purpose of avoiding the nearly-impossible-to-debug glitches that may result from a client doing this:

const entities = entityCluster.getLastClusteredEntities();

// (French accent:) A few moments later...
entities[42] = undefined;

// Even later
entityCluster.updateSomething(); // Crashes due to `undefined` in array...

It's a thin protection layer, but better than nothing. In other environments, this would/could/should return a read-only view on the internal state (Collections.unmodifiableList et al).

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... I see. I would still rather we protect against undefined in updateSomething.

In a typed language, you would just have a compile-time contract here, marking this function as returning something const or read-only. But even there, a user can generally find a way to shoot themselves in the foot at run-time, if they really want to.

So, here too, a user could do something crazy with the array, but we can't guard against everything. (But we should still guard against the basic things)

return;
}
const allVisibleEntities = [
...getVisibleEntitiesFromCollection(entityCluster._labelCollection),
Copy link
Contributor

Choose a reason for hiding this comment

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

I know there was previous discussion about the use of the spread (...) operator on large collections. Were these instances overlooked or intentionally left as-is because we know they're not large?

];

if (allVisibleEntities.length > 0) {
const uniqueEntities = Array.from(new Set(allVisibleEntities));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it possible for allVisibleEntities to contain duplicates?

Comment on lines +928 to +954
if (allVisibleEntities.length > 0) {
const uniqueEntities = Array.from(new Set(allVisibleEntities));

entityCluster._declusteredEvent.raiseEvent({
clustered: [],
declustered: uniqueEntities,
cluster: null,
allProcessed: uniqueEntities,
});

entityCluster._lastClusteredEntities = [];
entityCluster._lastDeclusteredEntities = uniqueEntities.slice();
entityCluster._allProcessedEntities = uniqueEntities.slice();
} else {
entityCluster._declusteredEvent.raiseEvent({
clustered: [],
declustered: [],
cluster: null,
allProcessed: [],
});

entityCluster._lastClusteredEntities = [];
entityCluster._lastDeclusteredEntities = [];
entityCluster._allProcessedEntities = [];
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe this need any conditional logic. Just set
const uniqueEntities = Array.from(new Set(allVisibleEntities));

and the rest handles itself whether allVisibleEntities is empty or not.


function getVisibleEntitiesFromCollection(collection) {
if (!defined(collection)) {
return [];
Copy link
Contributor

Choose a reason for hiding this comment

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

small style nit - define
const visibleEntities = [];

before this, and return it instead of a literal []

},
},
/**
* Gets the event that will be raised when clustering is processed, including both clustered and declustered entities.
Copy link
Contributor

Choose a reason for hiding this comment

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

So does this event do everything the pre-existing clusteredEvent did, and more? Because the comment says in includes clustered entities. So it's not just an event for declustering but also for clustering?

Comment on lines +524 to +527
clustered: entityCluster._lastClusteredEntities.slice(),
declustered: entityCluster._lastDeclusteredEntities.slice(),
cluster: null,
allProcessed: entityCluster._allProcessedEntities.slice(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to my other comment - do we want / need to be creating a shallow copy of these arrays here, via .slice()?

Copy link
Contributor

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.

@Oko-Tester
Copy link
Author

Hey @mzschwartz5,
Thanks for your feedback. I'll rethink my current implementation and maybe find a better way. But I won't be able to deal with this MR for a few days.

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.

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.

Include declustered entities in cluster callback

3 participants