< Summary

Class:Cluster
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Algorithms/Clustering/Cluster.cs
Covered lines:48
Uncovered lines:19
Coverable lines:67
Total lines:127
Line coverage:71.6% (48 of 67)
Covered branches:0
Total branches:0
Covered methods:14
Total methods:18
Method coverage:77.7% (14 of 18)

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%2100%
Radius()0%6200%
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.
 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 {
 155599824    get { return _coordinates; }
 25  }
 26
 27  // Get the list of game objects.
 28  public IReadOnlyList<GameObject> Objects {
 173429    get { return _objects; }
 30  }
 31
 32  // Get the list of agents.
 33  public IReadOnlyList<Agent> Agents {
 2025834    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.
 178253  public int Size() {
 178254    return _objects.Count;
 178255  }
 56
 57  // Check whether the cluster is empty.
 058  public bool IsEmpty() {
 059    return Size() == 0;
 060  }
 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.
 73  // The centroid is the mean position of all active game objects.
 169574  public Vector3 Centroid() {
 169575    Vector3 positionSum = Vector3.zero;
 169576    int activeAgentCount = 0;
 2571677    foreach (var agent in Agents) {
 1375478      if (agent != null && !agent.IsTerminated()) {
 687779        positionSum += agent.GetPosition();
 687780        ++activeAgentCount;
 687781      }
 687782    }
 169583    return activeAgentCount > 0 ? positionSum / activeAgentCount : Vector3.zero;
 169584  }
 85
 86  // Recenter the cluster's centroid to be the mean of all game objects' positions in the cluster.
 104887  public void Recenter() {
 104888    _coordinates = Centroid();
 104889  }
 90
 91  // Calculate the velocity of the cluster.
 92  // The velocity is the mean velocity of all active game objects.
 64793  public Vector3 Velocity() {
 64794    Vector3 velocitySum = Vector3.zero;
 64795    int activeAgentCount = 0;
 1049196    foreach (var agent in Agents) {
 570097      if (agent != null && !agent.IsTerminated()) {
 285098        velocitySum += agent.GetVelocity();
 285099        ++activeAgentCount;
 2850100      }
 2850101    }
 647102    return activeAgentCount > 0 ? velocitySum / activeAgentCount : Vector3.zero;
 647103  }
 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.
 578113  public void AddObjects(in IReadOnlyList<GameObject> objects) {
 578114    _objects.AddRange(objects);
 578115  }
 116
 117  // Merge another cluster into this one.
 118  // This function does not update the centroid of the cluster.
 578119  public void Merge(in Cluster cluster) {
 578120    AddObjects(cluster.Objects);
 578121  }
 122
 123  // Returns true if all agents in the cluster are terminated.
 470124  public bool IsFullyTerminated() {
 940125    return Agents.All(agent => agent?.IsTerminated() ?? true);
 470126  }
 127}