Skip to content

Commit 6811050

Browse files
committed
feat: getting bregma/lambda to load properly, and fixing B-L distance setting
1 parent fb51d43 commit 6811050

File tree

7 files changed

+127
-79
lines changed

7 files changed

+127
-79
lines changed

Assets/Editor/Editor_BuildAll.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void BuildAll()
1010
BuildWindows();
1111
BuildLinux();
1212
BuildWebGL();
13+
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows64);
1314
}
1415

1516
private static void BuildWindows()

Assets/Scenes/TrajectoryPlanner.unity

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,6 +2870,7 @@ MonoBehaviour:
28702870
_atlasMappings:
28712871
- Mouse CCF (25um)
28722872
- Rat Waxholm (39um)
2873+
_allowedOnWebGL: 0100
28732874
_transformDropdown: {fileID: 611159662}
28742875
--- !u!1001 &341190943874437988
28752876
PrefabInstance:

Assets/Scripts/Pinpoint/PinpointAtlasManager.cs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
using BrainAtlas.CoordinateSystems;
33
using CoordinateTransforms;
44
using System;
5-
using System.Collections;
65
using System.Collections.Generic;
76
using TMPro;
87
using UnityEngine;
98
using UnityEngine.SceneManagement;
10-
using Urchin.Managers;
119

1210
public class PinpointAtlasManager : MonoBehaviour
1311
{
12+
[SerializeField] BregmaLambdaBehavior _blBehavior;
13+
1414
[SerializeField] private TMP_Dropdown _atlasDropdown;
1515
[SerializeField] List<string> _atlasNames;
1616
[SerializeField] List<string> _atlasMappings;
17+
[SerializeField] List<bool> _allowedOnWebGL;
1718

1819
[SerializeField] private TMP_Dropdown _transformDropdown;
1920

2021
private Dictionary<string, string> _atlasNameMapping;
22+
private Dictionary<string, bool> _allowedOnWebGLMapping;
2123

2224
public HashSet<OntologyNode> DefaultNodes;
2325

@@ -29,8 +31,13 @@ private void Awake()
2931
throw new Exception("Atlas names and mapped names should be the same length");
3032

3133
_atlasNameMapping = new();
34+
_allowedOnWebGLMapping = new();
3235
for (int i = 0; i < _atlasNames.Count; i++)
36+
{
3337
_atlasNameMapping.Add(_atlasNames[i], _atlasMappings[i]);
38+
_allowedOnWebGLMapping.Add(_atlasNames[i], _allowedOnWebGL[i]);
39+
}
40+
3441

3542
Settings.AtlasTransformChangedEvent += SetNewTransform;
3643
}
@@ -60,7 +67,16 @@ public void PopulateAtlasDropdown()
6067
{
6168
var atlasNames = BrainAtlasManager.AtlasNames;
6269

63-
_atlasDropdown.options = atlasNames.ConvertAll(ConvertAtlas2Userfriendly);
70+
#if UNITY_WEBGL
71+
List<string> allowedNames = new();
72+
for (int i = 0; i < atlasNames.Count; i++)
73+
if (_allowedOnWebGLMapping[atlasNames[i]])
74+
allowedNames.Add(atlasNames[i]);
75+
#else
76+
var allowedNames = atlasNames;
77+
#endif
78+
79+
_atlasDropdown.options = allowedNames.ConvertAll(ConvertAtlas2Userfriendly);
6480
}
6581

6682
public void ResetAtlasDropdownIndex()
@@ -79,7 +95,7 @@ public void SetAtlas(int option)
7995

8096
private void ResetScene(int option)
8197
{
82-
PlayerPrefs.SetInt("scene-reset", 1);
98+
PlayerPrefs.SetInt("scene-atlas-reset", 1);
8399
Settings.AtlasName = BrainAtlasManager.AtlasNames[option];
84100
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
85101
}
@@ -92,9 +108,9 @@ private TMP_Dropdown.OptionData ConvertAtlas2Userfriendly(string atlasName)
92108
return new TMP_Dropdown.OptionData(atlasName);
93109
}
94110

95-
#endregion
111+
#endregion
96112

97-
#region Transforms
113+
#region Transforms
98114

99115
public void PopulateTransformDropdown()
100116
{
@@ -104,7 +120,10 @@ public void PopulateTransformDropdown()
104120
public void ResetTransformDropdownIndex()
105121
{
106122
string activeTransformName = BrainAtlasManager.ActiveAtlasTransform.Name;
107-
_transformDropdown.SetValueWithoutNotify(BrainAtlasManager.AtlasTransforms.FindIndex(x => x.Name.Equals(activeTransformName)));
123+
if (activeTransformName == "Custom")
124+
_transformDropdown.SetValueWithoutNotify(-1);
125+
else
126+
_transformDropdown.SetValueWithoutNotify(BrainAtlasManager.AtlasTransforms.FindIndex(x => x.Name.Equals(activeTransformName)));
108127
}
109128

110129
public void SetTransform(int idx)
@@ -117,26 +136,18 @@ public void SetNewTransform(string transformName)
117136
#if UNITY_EDITOR
118137
Debug.Log($"Atlas transform set to {transformName}");
119138
#endif
120-
BrainAtlasManager.ActiveAtlasTransform = BrainAtlasManager.AtlasTransforms.Find(x => x.Name.Equals(transformName));
139+
SetNewTransform(BrainAtlasManager.AtlasTransforms.Find(x => x.Name.Equals(transformName)));
140+
}
141+
142+
public void SetNewTransform(AtlasTransform newTransform)
143+
{
144+
BrainAtlasManager.ActiveAtlasTransform = newTransform;
121145
ResetTransformDropdownIndex();
122146

123147
// Check all probes for mis-matches
124148
foreach (ProbeManager probeManager in ProbeManager.Instances)
125149
probeManager.Update2ActiveTransform();
126150

127-
// custom transforms disabled for now...
128-
//if (Settings.BregmaLambdaDistance == 4.15f)
129-
//{
130-
// // if the BL distance is the default, just set the transform
131-
// SetNewTransform(coordinateTransformOpts.Values.ElementAt(invivoOption));
132-
//}
133-
//else
134-
//{
135-
// // if isn't the default, then we have to adjust the transform now
136-
// SetNewTransform(coordinateTransformOpts.Values.ElementAt(invivoOption));
137-
// ChangeBLDistance(Settings.BregmaLambdaDistance);
138-
//}
139-
140151
WarpBrain();
141152
}
142153

@@ -145,10 +156,10 @@ private string ConverTransform2UserFriendly(string transformName)
145156
return $"Atlas transform: {transformName}";
146157
}
147158

148-
#endregion
159+
#endregion
149160

150161

151-
#region Warping
162+
#region Warping
152163

153164
public void WarpBrain()
154165
{
@@ -175,5 +186,5 @@ private Vector3 WorldU2WorldT_Wrapper(Vector3 input)
175186
}
176187

177188

178-
#endregion
189+
#endregion
179190
}

Assets/Scripts/Pinpoint/TrajectoryPlannerManager.cs

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class TrajectoryPlannerManager : MonoBehaviour
109109
// Local tracking variables
110110
private bool _movedThisFrame;
111111

112-
private const string SCENE_RESET_KEY = "scene-rest";
112+
private const string SCENE_NOT_RESET = "scene-not-reset";
113113
private bool _sceneWasReset;
114114

115115
// Track who got clicked on, probe, camera, or brain
@@ -151,31 +151,39 @@ private void Awake()
151151

152152
public async void Startup()
153153
{
154-
// Clear the "Reset" PlayerPrefs flag
155-
_sceneWasReset = PlayerPrefs.GetInt(SCENE_RESET_KEY, 0) == 1;
154+
// Determine startup flags, we check two things:
155+
// (1) Are the PlayerPrefs cleared? If they are, we will load the null transform and bregma reference coordinate
156+
// (2) If set, check if this is an atlas reset, if it is, load the null transform and bregma reference coordinate
157+
// Otherwise, load all the previous settings
158+
bool _firstTime = !PlayerPrefs.HasKey("scene-atlas-reset");
159+
bool _atlasReset = PlayerPrefs.GetInt("scene-atlas-reset", 0) == 1;
156160

157161
// STARTUP SEQUENCE
158162
StartupEvent_MetaLoaded.Invoke();
159163

160-
// Startup CCF
164+
// Load Atlas
165+
// Settings.AtlasName returns CCF if PlayerPrefs is cleared, otherwise returns the previous atlas setting
161166
await BrainAtlasManager.LoadAtlas(Settings.AtlasName);
162167
ReferenceAtlas referenceAtlas = BrainAtlasManager.ActiveReferenceAtlas;
163168

164169
// Set the reference coordinate before anything else happen
165-
// if the scene was reset we should use the default coordinates
166-
if (_sceneWasReset)
170+
// if this is the first time, load bregma
171+
Debug.Log((_firstTime, _atlasReset));
172+
if (_firstTime || _atlasReset)
167173
{
168174
if (Utils.BregmaDefaults.ContainsKey(Settings.AtlasName))
169175
referenceAtlas.AtlasSpace.ReferenceCoord = Utils.BregmaDefaults[Settings.AtlasName];
170-
else
171-
referenceAtlas.AtlasSpace.ReferenceCoord = Vector3.zero;
172-
Settings.ReferenceCoord = referenceAtlas.AtlasSpace.ReferenceCoord;
173176
}
174177
else
175178
{
176179
referenceAtlas.AtlasSpace.ReferenceCoord = Settings.ReferenceCoord;
177180
}
181+
Settings.ReferenceCoord = referenceAtlas.AtlasSpace.ReferenceCoord;
182+
183+
// Now that the scene is loaded we can also set the BLDistance values
184+
SetBLUI();
178185

186+
// there is only one default set right now, tbd if we want to add others (beryl/cosmos/etc)
179187
var nodeTask = _atlasManager.LoadDefaultAreas("");
180188

181189
referenceAtlas.LoadAnnotations();
@@ -207,7 +215,7 @@ public async void Startup()
207215
ProbeManager.ActiveProbeUIUpdateEvent.AddListener(() => SetSurfaceDebugColor(ProbeManager.ActiveProbeManager.Color));
208216

209217
// Complete
210-
PlayerPrefs.SetInt(SCENE_RESET_KEY, 0);
218+
PlayerPrefs.SetInt("scene-atlas-reset", 0);
211219
StartupEvent_Complete.Invoke();
212220

213221
// After annotation loads, check if the user wants to load previously used probes
@@ -446,8 +454,6 @@ public ProbeManager AddNewProbe(ProbeProperties.ProbeType probeType, ProbeInsert
446454

447455
probeManager.ProbeController.SetProbePosition(insertion.apmldv);
448456
probeManager.ProbeController.SetProbeAngles(insertion.angles);
449-
// [TODO]
450-
//probeManager.ProbeController.SetSpaceTransform(insertion.ReferenceAtlas, insertion.AtlasTransform);
451457

452458
return probeManager;
453459
}
@@ -459,8 +465,6 @@ public ProbeManager AddNewProbe(ProbeProperties.ProbeType probeType, ProbeInsert
459465

460466
probeManager.ProbeController.SetProbePosition(insertion.apmldv);
461467
probeManager.ProbeController.SetProbeAngles(insertion.angles);
462-
// [TODO]
463-
//probeManager.ProbeController.SetSpaceTransform(insertion.ReferenceAtlas, insertion.AtlasTransform);
464468

465469
// Return data if there is no current Ephys Link data
466470
if (Settings.IsEphysLinkDataExpired()) return probeManager;
@@ -471,8 +475,7 @@ public ProbeManager AddNewProbe(ProbeProperties.ProbeType probeType, ProbeInsert
471475
probeManager.ManipulatorBehaviorController.BrainSurfaceOffset = brainSurfaceOffset;
472476
probeManager.ManipulatorBehaviorController.IsSetToDropToSurfaceWithDepth = dropToSurfaceWithDepth;
473477
probeManager.ManipulatorBehaviorController.IsSetToDropToSurfaceWithDepth = dropToSurfaceWithDepth;
474-
// [TODO]
475-
//probeManager.ManipulatorBehaviorController.IsRightHanded = isRightHanded;
478+
probeManager.ManipulatorBehaviorController.IsRightHanded = isRightHanded;
476479
var communicationManager = GameObject.Find("EphysLink").GetComponent<CommunicationManager>();
477480

478481
if (communicationManager.IsConnected && !string.IsNullOrEmpty(manipulatorId))
@@ -1007,54 +1010,68 @@ private void AccountsNewProbeHelper((Vector3 apmldv, Vector3 angles, int type, s
10071010

10081011
#region BLDistance
10091012

1013+
public void SetBLUI()
1014+
{
1015+
string atlasName = BrainAtlasManager.ActiveReferenceAtlas.Name;
1016+
float defaultBLDistance = Utils.LambdaDefaults[atlasName].x - Utils.BregmaDefaults[atlasName].x;
1017+
float min = Mathf.Max(0f, Mathf.FloorToInt(defaultBLDistance / 2f));
1018+
float max = Mathf.CeilToInt(defaultBLDistance * 1.5f);
1019+
1020+
_blDistance.SetBLRange(min, max, defaultBLDistance);
1021+
}
1022+
1023+
private CoordinateTransform _originalTransform;
1024+
10101025
/// <summary>
10111026
/// Change the bregma-lamba distance. By default this is 4.15f, so if it isn't that value, then we need to add an isometric scaling to the current transform
10121027
/// </summary>
1013-
/// <param name="blDistance"></param>
1014-
public void ChangeBLDistance(float blDistance)
1028+
/// <param name="newBLDistance"></param>
1029+
public void ChangeBLDistance(float newBLDistance)
10151030
{
1016-
// [TODO]
1031+
if (BrainAtlasManager.ActiveReferenceAtlas == null)
1032+
return;
10171033

1018-
// float blRatio = blDistance / 4.15f;
1019-
//#if UNITY_EDITOR
1020-
// Debug.Log($"(BL Distance) Re-scaling to {blRatio}");
1021-
//#endif
1034+
float blRatio = newBLDistance / _blDistance.DefaultBLDistance;
10221035

1023-
// if (CoordinateSpaceManager.ActiveCoordinateTransform.Name != "Custom")
1024-
// CoordinateSpaceManager.OriginalTransform = CoordinateSpaceManager.ActiveCoordinateTransform;
1036+
#if UNITY_EDITOR
1037+
Debug.Log($"(BL Distance) Re-scaling to {blRatio}");
1038+
#endif
10251039

1026-
// // There's no easy way to implement this without a refactor of the CoordinateTransform code, because you can't pull out the transform matrix.
1040+
if (BrainAtlasManager.ActiveAtlasTransform.Name != "Custom")
1041+
_originalTransform = BrainAtlasManager.ActiveAtlasTransform;
10271042

1028-
// // For now what we'll do is switch through the current transform, and replace it with a new version that's been scaled
1043+
// There's no easy way to implement this without a refactor of the CoordinateTransform code, because you can't pull out the transform matrix.
10291044

1030-
// CoordinateTransform newTransform;
1045+
// For now what we'll do is switch through the current transform, and replace it with a new version that's been scaled
10311046

1032-
// switch (CoordinateSpaceManager.OriginalTransform.Prefix)
1033-
// {
1034-
// case "ccf":
1035-
// // the ccf transform is the unity transform, so just build a new affine transform that scales
1036-
// newTransform = new CustomAffineTransform(blRatio * Vector3.one, Vector3.zero);
1037-
// break;
1047+
AtlasTransform newTransform;
10381048

1039-
// case "q18":
1040-
// newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.031f, 0.952f, -0.885f), new Vector3(0f, -5f, 0f));
1041-
// break;
1049+
switch (_originalTransform.Prefix)
1050+
{
1051+
case "":
1052+
// the null transform is the unity transform, so just build a new affine transform that scales
1053+
newTransform = new CustomAffineTransform(blRatio * Vector3.one, Vector3.zero);
1054+
break;
10421055

1043-
// case "d08":
1044-
// newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.087f, 1f, -0.952f), new Vector3(0f, -5f, 0f));
1045-
// break;
1056+
case "q18":
1057+
newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.031f, 0.952f, -0.885f), new Vector3(0f, -5f, 0f));
1058+
break;
10461059

1047-
// case "i-d08":
1048-
// newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.087f, 1f, -0.952f), new Vector3(0f, 0f, 0f));
1049-
// break;
1060+
case "d08":
1061+
newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.087f, 1f, -0.952f), new Vector3(0f, -5f, 0f));
1062+
break;
10501063

1051-
// default:
1052-
// Debug.LogError("Previous transform is not scalable");
1053-
// return;
1054-
// }
1064+
case "i-d08":
1065+
newTransform = new CustomAffineTransform(blRatio * new Vector3(-1.087f, 1f, -0.952f), new Vector3(0f, 0f, 0f));
1066+
break;
1067+
1068+
default:
1069+
Debug.LogError("Previous transform is not scalable");
1070+
return;
1071+
}
10551072

1056-
// // Apply the new transform
1057-
// SetNewTransform(newTransform);
1073+
// Apply the new transform
1074+
_pinpointAtlasManager.SetNewTransform(newTransform);
10581075
}
10591076

10601077
#endregion
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
using UnityEngine;
2-
using UnityEngine.Events;
32
using UnityEngine.UI;
43

54
public class BregmaLambdaBehavior : MonoBehaviour
65
{
7-
private const float DEFAULT_BREGMA_LAMBDA_DISTANCE = 4.15f;
6+
private float _blDistance = 4.15f;
7+
public float DefaultBLDistance { get { return _blDistance; } }
88

99
[SerializeField] Slider _blSlider;
1010

1111
public void ResetBLDistance()
1212
{
13-
_blSlider.value = DEFAULT_BREGMA_LAMBDA_DISTANCE;
13+
_blSlider.value = _blDistance;
14+
}
15+
16+
/// <summary>
17+
/// Set the slider range to a specific min/max and floatDistance
18+
/// </summary>
19+
/// <param name="min"></param>
20+
/// <param name="max"></param>
21+
/// <param name="blDistance"></param>
22+
public void SetBLRange(float min, float max, float blDistance)
23+
{
24+
_blDistance = blDistance;
25+
26+
_blSlider.minValue = min;
27+
_blSlider.maxValue = max;
28+
_blSlider.value = blDistance;
1429
}
1530
}

0 commit comments

Comments
 (0)