| | 1 | | using NUnit.Framework; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | |
|
| | 8 | | public class AgglomerativeClustererTest { |
| 4 | 9 | | public static GameObject GenerateObject(in Vector3 position) { |
| 4 | 10 | | GameObject obj = new GameObject(); |
| 4 | 11 | | obj.transform.position = position; |
| 4 | 12 | | return obj; |
| 4 | 13 | | } |
| | 14 | |
|
| 1 | 15 | | public static readonly List<GameObject> Objects = new List<GameObject> { |
| | 16 | | GenerateObject(new Vector3(0, 0, 0)), |
| | 17 | | GenerateObject(new Vector3(0, 1, 0)), |
| | 18 | | GenerateObject(new Vector3(0, 1.5f, 0)), |
| | 19 | | GenerateObject(new Vector3(0, 2.5f, 0)), |
| | 20 | | }; |
| | 21 | |
|
| | 22 | | [Test] |
| 1 | 23 | | public void TestSingleCluster() { |
| 1 | 24 | | AgglomerativeClusterer clusterer = |
| | 25 | | new AgglomerativeClusterer(Objects, maxSize: Objects.Count, maxRadius: Mathf.Infinity); |
| 1 | 26 | | clusterer.Cluster(); |
| 1 | 27 | | Assert.AreEqual(1, clusterer.Clusters.Count); |
| 1 | 28 | | Cluster cluster = clusterer.Clusters[0]; |
| 1 | 29 | | Assert.AreEqual(Objects.Count, cluster.Size()); |
| 1 | 30 | | Assert.AreEqual(new Vector3(0, 1.25f, 0), cluster.Centroid()); |
| 1 | 31 | | } |
| | 32 | |
|
| | 33 | | [Test] |
| 1 | 34 | | public void TestMaxSizeOne() { |
| 1 | 35 | | AgglomerativeClusterer clusterer = |
| | 36 | | new AgglomerativeClusterer(Objects, maxSize: 1, maxRadius: Mathf.Infinity); |
| 1 | 37 | | clusterer.Cluster(); |
| 1 | 38 | | Assert.AreEqual(Objects.Count, clusterer.Clusters.Count); |
| 15 | 39 | | foreach (var cluster in clusterer.Clusters) { |
| 4 | 40 | | Assert.AreEqual(1, cluster.Size()); |
| 4 | 41 | | } |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | [Test] |
| 1 | 45 | | public void TestZeroRadius() { |
| 1 | 46 | | AgglomerativeClusterer clusterer = |
| | 47 | | new AgglomerativeClusterer(Objects, maxSize: Objects.Count, maxRadius: 0); |
| 1 | 48 | | clusterer.Cluster(); |
| 1 | 49 | | Assert.AreEqual(Objects.Count, clusterer.Clusters.Count); |
| 15 | 50 | | foreach (var cluster in clusterer.Clusters) { |
| 4 | 51 | | Assert.AreEqual(1, cluster.Size()); |
| 4 | 52 | | } |
| 1 | 53 | | } |
| | 54 | |
|
| | 55 | | [Test] |
| 1 | 56 | | public void TestSmallRadius() { |
| 1 | 57 | | AgglomerativeClusterer clusterer = |
| | 58 | | new AgglomerativeClusterer(Objects, maxSize: Objects.Count, maxRadius: 1); |
| 1 | 59 | | clusterer.Cluster(); |
| 1 | 60 | | Assert.AreEqual(3, clusterer.Clusters.Count); |
| 4 | 61 | | List<Cluster> clusters = clusterer.Clusters.OrderBy(cluster => cluster.Coordinates[1]).ToList(); |
| 1 | 62 | | Assert.AreEqual(1, clusters[0].Size()); |
| 1 | 63 | | Assert.AreEqual(new Vector3(0, 0, 0), clusters[0].Coordinates); |
| 1 | 64 | | Assert.AreEqual(2, clusters[1].Size()); |
| 1 | 65 | | Assert.AreEqual(new Vector3(0, 1.25f, 0), clusters[1].Coordinates); |
| 1 | 66 | | Assert.AreEqual(1, clusters[2].Size()); |
| 1 | 67 | | Assert.AreEqual(new Vector3(0, 2.5f, 0), clusters[2].Coordinates); |
| 1 | 68 | | } |
| | 69 | | } |