| | | 1 | | using System.Linq; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | // The cluster represents a collection of hierarchical objects. |
| | | 5 | | public class Cluster : HierarchicalBase { |
| | | 6 | | [SerializeField] |
| | | 7 | | private Vector3 _centroid; |
| | | 8 | | |
| | | 9 | | public Vector3 Centroid { |
| | 0 | 10 | | get => _centroid; |
| | 0 | 11 | | set => _centroid = value; |
| | | 12 | | } |
| | | 13 | | |
| | 0 | 14 | | public int Size => ActiveSubHierarchicals.Count(); |
| | 0 | 15 | | public bool IsEmpty => Size == 0; |
| | | 16 | | |
| | 0 | 17 | | public float Radius() { |
| | 0 | 18 | | if (IsEmpty) { |
| | 0 | 19 | | return 0; |
| | | 20 | | } |
| | 0 | 21 | | return ActiveSubHierarchicals.DefaultIfEmpty().Max( |
| | | 22 | | subHierarchical => |
| | 0 | 23 | | subHierarchical == null ? 0 : Vector3.Distance(Centroid, subHierarchical.Position)); |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | public void Recenter() { |
| | 0 | 27 | | Centroid = Position; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | // Merging with another cluster does not update the centroid of the current cluster. |
| | 0 | 31 | | public void Merge(Cluster cluster) { |
| | 0 | 32 | | foreach (var subHierarchical in cluster.SubHierarchicals) { |
| | 0 | 33 | | AddSubHierarchical(subHierarchical); |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | } |