-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from chekhovana/clustering
Add clustering to provide low tile detalization
- Loading branch information
Showing
7 changed files
with
2,659 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Accord.MachineLearning; | ||
using Wkx; | ||
|
||
namespace i3dm.export; | ||
|
||
public static class TileClustering | ||
{ | ||
public static List<Instance> Cluster(List<Instance> instances, int size) | ||
{ | ||
var data = instances.Select(instance => instance.Position) | ||
.OfType<Point>() | ||
.Select(pt => new double[] { (double)pt.X, (double)pt.Y, (double)pt.Z }) | ||
.ToList(); | ||
double[][] matrix = data.ToArray(); | ||
KMeans kmeans = new MiniBatchKMeans(k: size, batchSize: 10) // this batchSize is optimal in terms of performance | ||
{ | ||
MaxIterations = 100, | ||
Tolerance = 1e-3, | ||
// based on https://scikit-learn.org/dev/modules/generated/sklearn.cluster.MiniBatchKMeans.html | ||
// without this parameter Learn method sometimes hangs | ||
InitializationBatchSize = size * 3 | ||
}; | ||
KMeansClusterCollection clusters = kmeans.Learn(matrix); | ||
int[] labels = clusters.Decide(matrix); | ||
Instance[] result = new Instance[size]; | ||
int count = 0; | ||
foreach (var (instance, label) in instances.Zip(labels)) | ||
{ | ||
if (result[label] == null) | ||
{ | ||
result[label] = instance; | ||
count++; | ||
if (count == size) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
return result.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using i3dm.export; | ||
|
||
namespace i3dm.export.tests.Clustering; | ||
|
||
public class ClusteringTests | ||
{ | ||
|
||
[Test] | ||
public void TestNumberOfClusters() | ||
{ | ||
Assert.That(true); | ||
|
||
// create 1000 random instances | ||
var random = new Random(); | ||
var instances = new List<Instance>(); | ||
for (int i = 0; i < 1000; i++) | ||
{ | ||
var x = random.NextDouble() * 1000; | ||
var y = random.NextDouble() * 1000; | ||
instances.Add(new Instance | ||
{ | ||
Position = new Wkx.Point(x, y, 0), | ||
Scale = 1, | ||
Rotation = 0 | ||
}); | ||
} | ||
|
||
Assert.That(instances.Count, Is.EqualTo(1000)); | ||
|
||
// cluster them into 10 groups | ||
var clusters = TileClustering.Cluster(instances, 10); | ||
Assert.That(clusters.Count, Is.EqualTo(10)); | ||
} | ||
} |
Oops, something went wrong.