< Summary

Class:Cluster
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Algorithms/Clustering/Cluster.cs
Covered lines:45
Uncovered lines:18
Coverable lines:63
Total lines:122
Line coverage:71.4% (45 of 63)
Covered branches:0
Total branches:0
Covered methods:13
Total methods:17
Method coverage:76.4% (13 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Cluster()0%2100%
Cluster(...)0%2100%
Cluster(...)0%110100%
Size()0%110100%
IsEmpty()0%110100%
Radius()0%6200%
Centroid()0%3.023086.67%
Recenter()0%110100%
Velocity()0%440100%
AddObject(...)0%110100%
AddObjects(...)0%110100%
Merge(...)0%110100%

File(s)

/github/workspace/Assets/Scripts/Algorithms/Clustering/Cluster.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5
 6// The cluster class represents a collection of game objects.
 7public class Cluster {
 8  // Coordinates of the cluster.
 7559  private Vector3 _coordinates = Vector3.zero;
 10
 11  // List of game objects in the cluster.
 75512  private List<GameObject> _objects = new List<GameObject>();
 13
 014  public Cluster() {}
 015  public Cluster(in Vector3 coordinates) {
 016    _coordinates = coordinates;
 017  }
 151018  public Cluster(in GameObject obj) {
 75519    _coordinates = obj.transform.position;
 75520  }
 21
 22  // Get the cluster coordinates.
 23  public Vector3 Coordinates {
 155583624    get { return _coordinates; }
 25  }
 26
 27  // Get the list of game objects.
 28  public IReadOnlyList<GameObject> Objects {
 174329    get { return _objects; }
 30  }
 31
 32  // Get the list of agents.
 33  public IReadOnlyList<Agent> Agents {
 034    get { return _objects.Select(gameObject => gameObject.GetComponent<Agent>()).ToList(); }
 35  }
 36
 37  // Get the list of interceptors.
 38  public IReadOnlyList<Interceptor> Interceptors {
 039    get {
 040      return _objects.Select(gameObject => gameObject.GetComponent<Agent>() as Interceptor)
 41          .ToList();
 042    }
 43  }
 44
 45  // Get the list of threats.
 46  public IReadOnlyList<Threat> Threats {
 047    get {
 048      return _objects.Select(gameObject => gameObject.GetComponent<Agent>() as Threat).ToList();
 049    }
 50  }
 51
 52  // Return the size of the cluster.
 346353  public int Size() {
 346354    return _objects.Count;
 346355  }
 56
 57  // Check whether the cluster is empty.
 169358  public bool IsEmpty() {
 169359    return Size() == 0;
 169360  }
 61
 62  // Calculate the radius of the cluster.
 063  public float Radius() {
 064    if (IsEmpty()) {
 065      return 0;
 66    }
 67
 068    Vector3 centroid = Centroid();
 069    return _objects.Max(obj => Vector3.Distance(centroid, obj.transform.position));
 070  }
 71
 72  // Calculate the centroid of the cluster.
 169373  public Vector3 Centroid() {
 169374    if (IsEmpty()) {
 075      return Vector3.zero;
 76    }
 77
 169378    Vector3 centroid = Vector3.zero;
 2565079    foreach (var obj in _objects) {
 685780      centroid += obj.transform.position;
 685781    }
 169382    centroid /= _objects.Count;
 169383    return centroid;
 169384  }
 85
 86  // Recenter the cluster's centroid to be the mean of all game objects' positions in the cluster.
 105087  public void Recenter() {
 105088    _coordinates = Centroid();
 105089  }
 90
 91  // Calculate the velocity of the cluster.
 92  // The velocity is the mean velocity of all game objects.
 64393  public Vector3 Velocity() {
 64394    Vector3 velocity = Vector3.zero;
 64395    int numObjects = 0;
 1047996    foreach (var obj in _objects) {
 570097      if (obj.GetComponent<Rigidbody>() != null) {
 285098        velocity += obj.GetComponent<Rigidbody>().linearVelocity;
 285099        ++numObjects;
 2850100      }
 2850101    }
 643102    return numObjects > 0 ? velocity / numObjects : Vector3.zero;
 643103  }
 104
 105  // Add a game object to the cluster.
 106  // This function does not update the centroid of the cluster.
 755107  public void AddObject(in GameObject obj) {
 755108    _objects.Add(obj);
 755109  }
 110
 111  // Add multiple game objects to the cluster.
 112  // This function does not update the centroid of the cluster.
 581113  public void AddObjects(in IReadOnlyList<GameObject> objects) {
 581114    _objects.AddRange(objects);
 581115  }
 116
 117  // Merge another cluster into this one.
 118  // This function does not update the centroid of the cluster.
 581119  public void Merge(in Cluster cluster) {
 581120    AddObjects(cluster.Objects);
 581121  }
 122}