< Summary

Class:Cluster
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Algorithms/Clustering/Cluster.cs
Covered lines:43
Uncovered lines:20
Coverable lines:63
Total lines:122
Line coverage:68.2% (43 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%110100%
Cluster(...)0%110100%
Cluster(...)0%110100%
Size()0%110100%
IsEmpty()0%110100%
Radius()0%2.092071.43%
Centroid()0%3.023086.67%
Recenter()0%110100%
Velocity()0%20400%
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.
 849  private Vector3 _coordinates = Vector3.zero;
 10
 11  // List of game objects in the cluster.
 8412  private List<GameObject> _objects = new List<GameObject>();
 13
 2414  public Cluster() {}
 7815  public Cluster(in Vector3 coordinates) {
 3916    _coordinates = coordinates;
 3917  }
 7418  public Cluster(in GameObject obj) {
 3719    _coordinates = obj.transform.position;
 3720  }
 21
 22  // Get the cluster coordinates.
 23  public Vector3 Coordinates {
 127824    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 {
 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.
 20453  public int Size() {
 20454    return _objects.Count;
 20455  }
 56
 57  // Check whether the cluster is empty.
 13858  public bool IsEmpty() {
 13859    return Size() == 0;
 13860  }
 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.
 7873  public Vector3 Centroid() {
 7874    if (IsEmpty()) {
 075      return Vector3.zero;
 76    }
 77
 7878    Vector3 centroid = Vector3.zero;
 103279    foreach (var obj in _objects) {
 26680      centroid += obj.transform.position;
 26681    }
 7882    centroid /= _objects.Count;
 7883    return centroid;
 7884  }
 85
 86  // Recenter the cluster's centroid to be the mean of all game objects' positions in the cluster.
 1287  public void Recenter() {
 1288    _coordinates = Centroid();
 1289  }
 90
 91  // Calculate the velocity of the cluster.
 92  // The velocity is the mean velocity of all game objects.
 093  public Vector3 Velocity() {
 094    Vector3 velocity = Vector3.zero;
 095    int numObjects = 0;
 096    foreach (var obj in _objects) {
 097      if (obj.GetComponent<Rigidbody>() != null) {
 098        velocity += obj.GetComponent<Rigidbody>().linearVelocity;
 099        ++numObjects;
 0100      }
 0101    }
 0102    return numObjects > 0 ? velocity / numObjects : Vector3.zero;
 0103  }
 104
 105  // Add a game object to the cluster.
 106  // This function does not update the centroid of the cluster.
 154107  public void AddObject(in GameObject obj) {
 154108    _objects.Add(obj);
 154109  }
 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}