|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -namespace CommunityToolkit.WinUI.Controls.Extensions; |
| 5 | +using System.Numerics; |
| 6 | + |
| 7 | +namespace CommunityToolkit.WinUI.Extensions; |
6 | 8 |
|
7 | 9 | public static partial class AccentExtractor |
8 | 10 | { |
| 11 | + private static Vector3[] KMeansCluster(Span<Vector3> points, int k) |
| 12 | + { |
| 13 | + // Track the assigned cluster of each point |
| 14 | + int[] clusterIds = new int[points.Length]; |
| 15 | + |
| 16 | + // Track the centroids of each cluster and its member count |
| 17 | + // TODO: stackalloc is great here, but pooling should be thresholded |
| 18 | + // just in case |
| 19 | + Span<Vector3> centroids = stackalloc Vector3[k]; |
| 20 | + Span<int> counts = stackalloc int[k]; |
| 21 | + |
| 22 | + // Split the points into arbitrary clusters |
| 23 | + // NOTE: Can this be rearranged to converge faster? |
| 24 | + var offset = Random.Shared.Next(k); // Mathematically true random sampling |
| 25 | + for (int i = 0; i < clusterIds.Length; i++) |
| 26 | + clusterIds[i] = (i + offset) % k; |
| 27 | + |
| 28 | + bool converged = false; |
| 29 | + while (!converged) |
| 30 | + { |
| 31 | + // Assume we've converged. If we haven't, we'll assign converged |
| 32 | + // to false when adjust the clusters |
| 33 | + converged = true; |
| 34 | + |
| 35 | + // KMeans Loop Step 1: |
| 36 | + // Calculate/Recalculate the centroids of each cluster |
| 37 | + |
| 38 | + // Clear centroids and counts before recalculation |
| 39 | + for(int i = 0; i < centroids.Length; i++) |
| 40 | + { |
| 41 | + centroids[i] = Vector3.Zero; |
| 42 | + counts[i] = 0; |
| 43 | + } |
| 44 | + |
| 45 | + // Accumlate step in centroid calculation |
| 46 | + for(int i = 0; i < clusterIds.Length; i++) |
| 47 | + { |
| 48 | + int id = clusterIds[i]; |
| 49 | + centroids[id] += points[i]; |
| 50 | + counts[id]++; |
| 51 | + } |
| 52 | + |
| 53 | + // Prune empty clusters |
| 54 | + // All empty clusters are swapped to the end of the span |
| 55 | + // then a slice is taken with only the remaining populated clusters |
| 56 | + int pivot = counts.Length; |
| 57 | + for (int i = 0; i < pivot;) |
| 58 | + { |
| 59 | + // Increment and continue if populated |
| 60 | + if (counts[i] != 0) |
| 61 | + { |
| 62 | + i++; |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + // The item is not populated. Swap to end and move pivot |
| 67 | + // NOTE: This is a oneway swap. We're discarding the 0 anyways. |
| 68 | + pivot--; |
| 69 | + counts[i] = counts[pivot]; |
| 70 | + } |
| 71 | + counts = counts[..pivot]; |
| 72 | + centroids = centroids[..pivot]; |
| 73 | + |
| 74 | + // Division step in centroid calculation |
| 75 | + for (int i = 0; i < centroids.Length; i++) |
| 76 | + centroids[i] /= counts[i]; |
| 77 | + |
| 78 | + // KMeans Loop Step 2: |
| 79 | + // Move each point's clusterId to the nearest cluster centroid |
| 80 | + for (int i = 0; i < points.Length; i++) |
| 81 | + { |
| 82 | + Vector3 point = points[i]; |
| 83 | + var oldId = clusterIds[i]; |
| 84 | + |
| 85 | + // Track the nearest centroid's distance and the index of that centroid |
| 86 | + float nearestDistance = float.PositiveInfinity; |
| 87 | + int nearestIndex = -1; |
| 88 | + |
| 89 | + for (int j = 0; j < centroids.Length; j++) |
| 90 | + { |
| 91 | + // Compare the point to the jth centroid |
| 92 | + float distance = Vector3.DistanceSquared(point, centroids[j]); |
| 93 | + |
| 94 | + // Skip the cluster if further than the nearest seen cluster |
| 95 | + if (nearestDistance < distance) |
| 96 | + continue; |
| 97 | + |
| 98 | + // This is the nearest cluster |
| 99 | + // Update the distance and index |
| 100 | + nearestDistance = distance; |
| 101 | + nearestIndex = j; |
| 102 | + } |
| 103 | + |
| 104 | + // The nearest cluster hasn't changed. Do nothing |
| 105 | + if (oldId == nearestIndex) |
| 106 | + continue; |
| 107 | + |
| 108 | + // Update the cluster id and note that we have not converged |
| 109 | + clusterIds[i] = nearestIndex; |
| 110 | + converged = false; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return centroids.ToArray(); |
| 115 | + } |
9 | 116 |
|
| 117 | + private static float FindColorfulness(Vector3 color) |
| 118 | + { |
| 119 | + var rg = color.X - color.Y; |
| 120 | + var yb = ((color.X + color.Y) / 2) - color.Z; |
| 121 | + return 0.3f * new Vector2(rg, yb).Length(); |
| 122 | + } |
10 | 123 | } |
0 commit comments