Skip to content

Commit 3ef3c8e

Browse files
committed
fix: fixing a confusing bug related to custom atlases
custom atlas code was failing to trigger, causing the AtlasTransform to be undefined in a weird way. Somehow this caused the surface search functionality to fail silently
1 parent 6d90323 commit 3ef3c8e

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

Assets/Scripts/Pinpoint/Probes/ProbeManager.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,46 @@ public void UpdateSurfacePosition()
816816
(Vector3 tipCoordWorldU, _, _, Vector3 tipForwardWorldU) = _probeController.GetTipWorldU();
817817

818818
Vector3 tipAtlasIdxU = BrainAtlasManager.ActiveReferenceAtlas.World2AtlasIdx(tipCoordWorldU);
819+
819820
Vector3 downDir = useDV ? Vector3.down : tipForwardWorldU;
821+
Vector3 downDirAtlas = BrainAtlasManager.ActiveReferenceAtlas.World2Atlas_Vector(downDir);
820822

821823
// Check if we're in the brain
822824
bool probeInBrain = BrainAtlasManager.ActiveReferenceAtlas.GetAnnotationIdx(tipAtlasIdxU) > 0;
823825

824-
Vector3 entryCoordAtlasIdx = FindEntryIdxCoordinate(probeInBrain ? tipAtlasIdxU : tipAtlasIdxU + downDir * 5000f / BrainAtlasManager.ActiveReferenceAtlas.Resolution.x,
825-
BrainAtlasManager.ActiveReferenceAtlas.World2Atlas_Vector(downDir));
826+
Vector3 tipInBrain = tipAtlasIdxU;
827+
828+
// Adjust the tip coordinate to put it into the brain
829+
if (!probeInBrain)
830+
{
831+
bool done = false;
832+
float[] offset = { +1000f, -1000f };
833+
for (int mult = 1; mult <= 16; mult *= 2)
834+
{
835+
for (int i = 0; i < 2; i++)
836+
{
837+
Vector3 temporaryTip = tipAtlasIdxU + downDirAtlas * offset[i] * mult / BrainAtlasManager.ActiveReferenceAtlas.Resolution.z;
838+
Debug.Log(temporaryTip);
839+
if (BrainAtlasManager.ActiveReferenceAtlas.GetAnnotationIdx(temporaryTip) > 0)
840+
{
841+
tipInBrain = temporaryTip;
842+
done = true;
843+
break;
844+
}
845+
}
846+
847+
if (done)
848+
break;
849+
}
850+
851+
if (!done)
852+
{
853+
Debug.LogWarning("Impossible to find brain surface from here");
854+
return (new Vector3(float.NaN, float.NaN, float.NaN), false);
855+
}
856+
}
857+
858+
Vector3 entryCoordAtlasIdx = FindEntryIdxCoordinate(tipInBrain, downDirAtlas);
826859

827860
return (entryCoordAtlasIdx, probeInBrain);
828861
}
@@ -840,9 +873,8 @@ public void UpdateSurfacePosition()
840873
/// <returns></returns>
841874
public Vector3 FindEntryIdxCoordinate(Vector3 bottomIdxCoordU, Vector3 downVector)
842875
{
843-
// search from 10000 um above the top idx
844876
Debug.Log(downVector);
845-
float searchDistance = 10000 / BrainAtlasManager.ActiveReferenceAtlas.Resolution.z;
877+
float searchDistance = BrainAtlasManager.ActiveReferenceAtlas.Dimensions.z * 1000f / BrainAtlasManager.ActiveReferenceAtlas.Resolution.z;
846878
Vector3 topSearchIdxCoordU = bottomIdxCoordU - downVector * searchDistance;
847879

848880
// If by chance we are inside the brain, go farther

Assets/Scripts/Pinpoint/TrajectoryPlannerManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,19 @@ public void ChangeBLRatio(float blRatio)
10401040
if (BrainAtlasManager.ActiveReferenceAtlas == null)
10411041
return;
10421042

1043+
if (blRatio == 1f)
1044+
{
1045+
if (BrainAtlasManager.ActiveReferenceAtlas.Name == "Custom" && _originalTransform != null)
1046+
_pinpointAtlasManager.SetNewTransform((AtlasTransform)_originalTransform);
1047+
_originalTransform = null;
1048+
return;
1049+
}
1050+
10431051
#if UNITY_EDITOR
10441052
Debug.Log($"(BL Distance) Re-scaling to {blRatio}");
10451053
#endif
10461054

1047-
if (BrainAtlasManager.ActiveAtlasTransform.Name != "Custom")
1055+
if (BrainAtlasManager.ActiveAtlasTransform.Name != "Custom") { }
10481056
_originalTransform = BrainAtlasManager.ActiveAtlasTransform;
10491057

10501058
// There's no easy way to implement this without a refactor of the CoordinateTransform code, because you can't pull out the transform matrix.

0 commit comments

Comments
 (0)