From f8a6f579eac0e0b7fb71ae57ff5e9b1cbef4340d Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 22 May 2026 17:15:18 +0200 Subject: [PATCH] GetNeuron/Cluster instead of GetNucleus --- Editor/ClusterView.cs | 4 +- Editor/Cluster_Drawer.cs | 4 +- Runtime/Scripts/Core/Cluster.cs | 97 +++++++++++++------ .../ScriptableObjects/ClusterPrefab.cs | 6 +- Samples/Braitenberg/Scripts/Motor.cs | 4 +- Samples/Braitenberg/Scripts/Sensor.cs | 4 +- Samples/Braitenberg/Scripts/Vehicle.cs | 2 +- 7 files changed, 78 insertions(+), 43 deletions(-) diff --git a/Editor/ClusterView.cs b/Editor/ClusterView.cs index 7f55f51..9c3f783 100644 --- a/Editor/ClusterView.cs +++ b/Editor/ClusterView.cs @@ -445,7 +445,7 @@ namespace NanoBrain.Unity { float maxValue = 0; foreach (Cluster sibling in nucleus.parent.instances) { - Neuron siblingNeuron = sibling.GetNucleus(nucleus.name) as Neuron; + Neuron siblingNeuron = sibling.GetNeuron(nucleus.name); float value = siblingNeuron.outputMagnitude; // no need to add weight as they are all the same if (value > maxValue) maxValue = value; @@ -458,7 +458,7 @@ namespace NanoBrain.Unity { int row = 0; Vector3 position = Vector3.zero; foreach (Cluster sibling in nucleus.parent.instances) { - Neuron siblingNeuron = sibling.GetNucleus(nucleus.name) as Neuron; + Neuron siblingNeuron = sibling.GetNeuron(nucleus.name); position = new(250, margin + row * spacing, 0.0f); DrawEdge(parentPos, position); Color color = Color.black; diff --git a/Editor/Cluster_Drawer.cs b/Editor/Cluster_Drawer.cs index 3f45913..3ff6e6e 100644 --- a/Editor/Cluster_Drawer.cs +++ b/Editor/Cluster_Drawer.cs @@ -94,7 +94,7 @@ namespace NanoBrain.Unity { object parent = SerializedPropertyUtility.GetParentObjectAndMember(targetObject, property.propertyPath, out var memberInfo, out int outIndex); if (parent != null && memberInfo is FieldInfo fieldInfo) { fieldInfo.SetValue(parent, cluster); - //EditorUtility.SetDirty(targetObject); + EditorUtility.SetDirty(targetObject); } clusterView.initialized = true; } @@ -136,7 +136,7 @@ namespace NanoBrain.Unity { Handles.color = Color.yellow; if (Cluster_Drawer.clusterView.selectedSynapseNeuron != null) { foreach (Cluster sibling in Cluster_Drawer.clusterView.selectedSynapseNeuron.parent.instances) { - Neuron siblingNeuron = sibling.GetNucleus(Cluster_Drawer.clusterView.selectedSynapseNeuron.name) as Neuron; + Neuron siblingNeuron = sibling.GetNeuron(Cluster_Drawer.clusterView.selectedSynapseNeuron.name); Vector3 worldVector = gameObject.transform.TransformVector(siblingNeuron.outputValue); Handles.DrawLine(gameObject.transform.position, gameObject.transform.position + worldVector); } diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index d7cbe27..e588f85 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -522,37 +522,37 @@ namespace NanoBrain { /// /// The name of the nucleus to find /// The found nucleus or null when it is not found - public Nucleus GetNucleus(string nucleusName) { - int dotPosition = nucleusName.IndexOf('.'); - if (dotPosition >= 0) { - string clusterName = nucleusName[..dotPosition]; - string clusterName0 = clusterName + ": 0"; - foreach (Nucleus nucleus in this.nuclei) { - if (nucleus is Cluster cluster) { - if (cluster.name == clusterName || cluster.name == clusterName0) { - // cluster.CheckInstances(); - string subNucleusName = nucleusName[(dotPosition + 1)..]; - return cluster.GetNucleus(subNucleusName); - } - } - } - return null; - } - else { - string nucleusName0 = nucleusName + ": 0"; - foreach (Nucleus nucleus in this.nuclei) { - if (nucleus is Cluster cluster) { - if (nucleus.name == nucleusName || nucleus.name == nucleusName0) { - // cluster.CheckInstances(); - return nucleus; - } - } - else if (nucleus.name == nucleusName) - return nucleus; - } - return null; - } - } + // public Nucleus GetNucleus(string nucleusName) { + // int dotPosition = nucleusName.IndexOf('.'); + // if (dotPosition >= 0) { + // string clusterName = nucleusName[..dotPosition]; + // string clusterName0 = clusterName + ": 0"; + // foreach (Nucleus nucleus in this.nuclei) { + // if (nucleus is Cluster cluster) { + // if (cluster.name == clusterName || cluster.name == clusterName0) { + // // cluster.CheckInstances(); + // string subNucleusName = nucleusName[(dotPosition + 1)..]; + // return cluster.GetNucleus(subNucleusName); + // } + // } + // } + // return null; + // } + // else { + // string nucleusName0 = nucleusName + ": 0"; + // foreach (Nucleus nucleus in this.nuclei) { + // if (nucleus is Cluster cluster) { + // if (nucleus.name == nucleusName || nucleus.name == nucleusName0) { + // // cluster.CheckInstances(); + // return nucleus; + // } + // } + // else if (nucleus.name == nucleusName) + // return nucleus; + // } + // return null; + // } + // } /// /// Get a neuron in this cluster @@ -570,6 +570,41 @@ namespace NanoBrain { return null; } + /// + /// Get a subcluster in this cluster + /// + /// The name of the cluster to find + /// The found cluster or null when it is not found + public Cluster GetCluster(string clusterName) { + int dotPosition = clusterName.IndexOf('.'); + if (dotPosition >= 0) { + string clusterBaseName = clusterName[..dotPosition]; + string clusterName0 = clusterBaseName + ": 0"; + foreach (Nucleus nucleus in this.nuclei) { + if (nucleus is Cluster cluster) { + if (cluster.name == clusterBaseName || cluster.name == clusterName0) { + // cluster.CheckInstances(); + string subNucleusName = clusterName[(dotPosition + 1)..]; + return cluster.GetCluster(subNucleusName); + } + } + } + return null; + } + else { + string nucleusName0 = clusterName + ": 0"; + foreach (Nucleus nucleus in this.nuclei) { + if (nucleus is Cluster cluster) { + if (nucleus.name == clusterName || nucleus.name == nucleusName0) { + // cluster.CheckInstances(); + return cluster; + } + } + } + return null; + } + } + /// /// Get a neuron in an instance of a multi-cluster /// diff --git a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs index 187b8b2..b57e2dc 100644 --- a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs +++ b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs @@ -22,9 +22,9 @@ namespace NanoBrain.Unity { /// /// The name of the nucleus /// The Nucleus with the given name or null if no such Nucleus could be found - public Nucleus GetNucleus(string nucleusName) { - return cluster.GetNucleus(nucleusName); - } + // public Nucleus GetNucleus(string nucleusName) { + // return cluster.GetNucleus(nucleusName); + // } /// /// Call this function to ensure that there is at least one nucleus diff --git a/Samples/Braitenberg/Scripts/Motor.cs b/Samples/Braitenberg/Scripts/Motor.cs index cfa7836..903b952 100644 --- a/Samples/Braitenberg/Scripts/Motor.cs +++ b/Samples/Braitenberg/Scripts/Motor.cs @@ -10,7 +10,7 @@ namespace NanoBrain.Braitenberg { public WheelCollider wheelCollider; - protected ClusterPrefab brain; + protected Cluster brain; public Neuron motorNeuron; protected virtual void Awake() { @@ -18,7 +18,7 @@ namespace NanoBrain.Braitenberg { if (vehicle != null) brain = vehicle.brain; if (brain != null) - motorNeuron = brain.GetNucleus(outputNeuronName) as Neuron; + motorNeuron = brain.GetNeuron(outputNeuronName); wheelCollider = GetComponent(); } diff --git a/Samples/Braitenberg/Scripts/Sensor.cs b/Samples/Braitenberg/Scripts/Sensor.cs index 8de2a40..d7ff6cc 100644 --- a/Samples/Braitenberg/Scripts/Sensor.cs +++ b/Samples/Braitenberg/Scripts/Sensor.cs @@ -25,7 +25,7 @@ namespace NanoBrain.Braitenberg { public float _output; protected Vehicle vehicle; - protected ClusterPrefab brain; + protected Cluster brain; public Neuron sensoryNeuron; protected virtual void Awake() { @@ -34,7 +34,7 @@ namespace NanoBrain.Braitenberg { if (vehicle != null) brain = vehicle.brain; if (brain != null) - sensoryNeuron = brain.GetNucleus(this.name) as Neuron; + sensoryNeuron = brain.GetNeuron(this.name); } void OnEnable() => StartCoroutine(SampleRoutine()); diff --git a/Samples/Braitenberg/Scripts/Vehicle.cs b/Samples/Braitenberg/Scripts/Vehicle.cs index 284c619..1730a33 100644 --- a/Samples/Braitenberg/Scripts/Vehicle.cs +++ b/Samples/Braitenberg/Scripts/Vehicle.cs @@ -4,7 +4,7 @@ namespace NanoBrain.Braitenberg { [RequireComponent(typeof(Rigidbody))] public class Vehicle : MonoBehaviour { - public Unity.ClusterPrefab brain; + public Cluster brain; [Header("Motors")] public Motor motorLeft;