< Summary

Class:Cluster
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Algorithms/Clustering/Cluster.cs
Covered lines:63
Uncovered lines:4
Coverable lines:67
Total lines:127
Line coverage:94% (63 of 67)
Covered branches:0
Total branches:0
Covered methods:17
Total methods:18
Method coverage:94.4% (17 of 18)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Cluster()0%110100%
Cluster(...)0%110100%
Cluster(...)0%110100%
Size()0%110100%
IsEmpty()0%110100%
Radius()0%2.092071.43%
Centroid()0%660100%
Recenter()0%110100%
Velocity()0%660100%
AddObject(...)0%110100%
AddObjects(...)0%110100%
Merge(...)0%110100%
IsFullyTerminated()0%220100%

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.
 809  private Vector3 _coordinates = Vector3.zero;
 10
 11  // List of game objects in the cluster.
 8012  private List<GameObject> _objects = new List<GameObject>();
 13
 2714  public Cluster() {}
 6815  public Cluster(in Vector3 coordinates) {
 3416    _coordinates = coordinates;
 3417  }
 7418  public Cluster(in GameObject obj) {
 3719    _coordinates = obj.transform.position;
 3720  }
 21
 22  // Get the cluster coordinates.
 23  public Vector3 Coordinates {
 116424    get { return _coordinates; }
 25  }
 26
 27  // Get the list of game objects.
 28  public IReadOnlyList<GameObject> Objects {
 1529    get { return _objects; }
 30  }
 31
 32  // Get the list of agents.
 33  public IReadOnlyList<Agent> Agents {
 49734    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 {
 247    get {
 448      return _objects.Select(gameObject => gameObject.GetComponent<Agent>() as Threat).ToList();
 249    }
 50  }
 51
 52  // Return the size of the cluster.
 12153  public int Size() {
 12154    return _objects.Count;
 12155  }
 56
 57  // Check whether the cluster is empty.
 5558  public bool IsEmpty() {
 5559    return Size() == 0;
 5560  }
 61
 62  // Calculate the radius of the cluster.
 1963  public float Radius() {
 1964    if (IsEmpty()) {
 065      return 0;
 66    }
 67
 1968    Vector3 centroid = Centroid();
 5369    return _objects.Max(obj => Vector3.Distance(centroid, obj.transform.position));
 1970  }
 71
 72  // Calculate the centroid of the cluster.
 73  // The centroid is the mean position of all active game objects.
 7674  public Vector3 Centroid() {
 7675    Vector3 positionSum = Vector3.zero;
 7676    int activeAgentCount = 0;
 99977    foreach (var agent in Agents) {
 51278      if (agent != null && !agent.IsTerminated()) {
 25579        positionSum += agent.GetPosition();
 25580        ++activeAgentCount;
 25581      }
 25782    }
 7683    return activeAgentCount > 0 ? positionSum / activeAgentCount : Vector3.zero;
 7684  }
 85
 86  // Recenter the cluster's centroid to be the mean of all game objects' positions in the cluster.
 1387  public void Recenter() {
 1388    _coordinates = Centroid();
 1389  }
 90
 91  // Calculate the velocity of the cluster.
 92  // The velocity is the mean velocity of all active game objects.
 293  public Vector3 Velocity() {
 294    Vector3 velocitySum = Vector3.zero;
 295    int activeAgentCount = 0;
 1296    foreach (var agent in Agents) {
 397      if (agent != null && !agent.IsTerminated()) {
 198        velocitySum += agent.GetVelocity();
 199        ++activeAgentCount;
 1100      }
 2101    }
 2102    return activeAgentCount > 0 ? velocitySum / activeAgentCount : Vector3.zero;
 2103  }
 104
 105  // Add a game object to the cluster.
 106  // This function does not update the centroid of the cluster.
 143107  public void AddObject(in GameObject obj) {
 143108    _objects.Add(obj);
 143109  }
 110
 111  // Add multiple game objects to the cluster.
 112  // This function does not update the centroid of the cluster.
 11113  public void AddObjects(in IReadOnlyList<GameObject> objects) {
 11114    _objects.AddRange(objects);
 11115  }
 116
 117  // Merge another cluster into this one.
 118  // This function does not update the centroid of the cluster.
 5119  public void Merge(in Cluster cluster) {
 5120    AddObjects(cluster.Objects);
 5121  }
 122
 123  // Returns true if all agents in the cluster are terminated.
 1124  public bool IsFullyTerminated() {
 2125    return Agents.All(agent => agent?.IsTerminated() ?? true);
 1126  }
 127}