-
Notifications
You must be signed in to change notification settings - Fork 80
Added DBScan clustering to ColorPaletteSampler #753
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||
| // See the LICENSE file in the project root for more information. | ||||||
|
|
||||||
| using System.Numerics; | ||||||
|
|
||||||
| namespace CommunityToolkit.WinUI.Helpers; | ||||||
|
|
||||||
| public partial class ColorPaletteSampler | ||||||
| { | ||||||
| private ref struct DBScan | ||||||
| { | ||||||
| private const int Unclassified = -1; | ||||||
|
|
||||||
| public static Vector3[] Cluster(Span<Vector3> points, float epsilon, int minPoints, ref float[] weights) | ||||||
| { | ||||||
| var centroids = new List<Vector3>(); | ||||||
| var newWeights = new List<float>(); | ||||||
|
|
||||||
| // Create context | ||||||
| var context = new DBScan(points, weights, epsilon, minPoints); | ||||||
|
|
||||||
| // Attempt to create a cluster around each point, | ||||||
| // skipping that point if already classified | ||||||
| for (int i = 0; i < points.Length; i++) | ||||||
| { | ||||||
| // Already classified, skip | ||||||
| if (context.PointClusterIds[i] is not Unclassified) | ||||||
| continue; | ||||||
|
|
||||||
| // Attempt to create cluster | ||||||
| if(context.CreateCluster(i, out var centroid, out var weight)) | ||||||
| { | ||||||
| centroids.Add(centroid); | ||||||
| newWeights.Add(weight); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| weights = newWeights.ToArray(); | ||||||
| return centroids.ToArray(); | ||||||
| } | ||||||
|
|
||||||
| private bool CreateCluster(int originIndex, out Vector3 centroid, out float weight) | ||||||
| { | ||||||
| weight = 0; | ||||||
| centroid = Vector3.Zero; | ||||||
| var seeds = GetSeeds(originIndex, out bool isCore); | ||||||
|
|
||||||
| // Not enough seeds to be a core point. | ||||||
| // Cannot create a cluster around it | ||||||
| if (!isCore) | ||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| ExpandCluster(seeds, out centroid, out weight); | ||||||
| ClusterId++; | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| private void ExpandCluster(Queue<int> seeds, out Vector3 centroid, out float weight) | ||||||
| { | ||||||
| weight = 0; | ||||||
| centroid = Vector3.Zero; | ||||||
| while(seeds.Count > 0) | ||||||
| { | ||||||
| var seedIndex = seeds.Dequeue(); | ||||||
|
|
||||||
| // Skip duplicate seed entries | ||||||
| if (PointClusterIds[seedIndex] is not Unclassified) | ||||||
| continue; | ||||||
|
|
||||||
| // Assign this seed's id to the cluster | ||||||
| PointClusterIds[seedIndex] = ClusterId; | ||||||
| var w = Weights[seedIndex]; | ||||||
| centroid += Points[seedIndex] * w; | ||||||
| weight += w; | ||||||
|
|
||||||
| // Check if this seed is a core point | ||||||
| var grandSeeds = GetSeeds(seedIndex, out var seedIsCore); | ||||||
| if (!seedIsCore) | ||||||
| continue; | ||||||
|
|
||||||
| // This seed is a core point. Enqueue all its seeds | ||||||
| foreach(var grandSeedIndex in grandSeeds) | ||||||
| if (PointClusterIds[grandSeedIndex] is Unclassified) | ||||||
| seeds.Enqueue(grandSeedIndex); | ||||||
| } | ||||||
|
|
||||||
| centroid /= weight; | ||||||
| } | ||||||
|
|
||||||
| private Queue<int> GetSeeds(int originIndex, out bool isCore) | ||||||
| { | ||||||
| var origin = Points[originIndex]; | ||||||
|
|
||||||
| var seeds = new Queue<int>(); | ||||||
| for (int i = 0; i < Points.Length; i++) | ||||||
| { | ||||||
| if (Vector3.DistanceSquared(origin, Points[i]) <= Epsilon2) | ||||||
| seeds.Enqueue(i); | ||||||
| } | ||||||
|
|
||||||
| // Count includes self, so compare without checking equals | ||||||
| isCore = seeds.Count > MinPoints; | ||||||
| return seeds; | ||||||
| } | ||||||
|
|
||||||
| private DBScan(Span<Vector3> points, Span<float> weights, double epsilon, int minPoints) | ||||||
|
||||||
| { | ||||||
| Points = points; | ||||||
| Weights = weights; | ||||||
| Epsilon2 = epsilon * epsilon; | ||||||
| MinPoints = minPoints; | ||||||
|
|
||||||
| ClusterId = 0; | ||||||
| PointClusterIds = new int[points.Length]; | ||||||
| for(int i = 0; i < points.Length; i++) | ||||||
| PointClusterIds[i] = Unclassified; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets the points being clustered. | ||||||
| /// </summary> | ||||||
| public Span<Vector3> Points { get; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets the weights of the points. | ||||||
| /// </summary> | ||||||
| public Span<float> Weights { get; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets or sets the id of the currently evaluating cluster. | ||||||
| /// </summary> | ||||||
| public int ClusterId { get; set; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets an array containing the id of the cluster each point belongs to. | ||||||
| /// </summary> | ||||||
| public int[] PointClusterIds { get; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets epsilon squared. Where epslion is the max distance to consider two points connected. | ||||||
|
||||||
| /// Gets epsilon squared. Where epslion is the max distance to consider two points connected. | |
| /// Gets epsilon squared. Where epsilon is the max distance to consider two points connected. |
Outdated
Copilot
AI
Oct 13, 2025
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.
Corrected spelling of 'miniumum' to 'minimum'.
| /// Gets the miniumum number of points required to make a core point. | |
| /// Gets the minimum number of points required to make a core point. |
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.
This creates an O(n²) algorithm for seed finding. For large datasets, consider using spatial data structures like KD-trees or grid-based approaches to improve performance.
Uh oh!
There was an error while loading. Please reload this page.
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.
This is run after KMeans, there are only 8 points. Building a KD-Tree is far more effort than it's worth, and may introduce overhead with a net loss