@@ -770,12 +770,12 @@ public void DropProbeToBrainSurface()
770770 return ;
771771 }
772772
773- Vector3 worldU = BrainAtlasManager . ActiveReferenceAtlas . AtlasIdx2World ( entryCoordAtlasIdx ) ;
774- Vector3 entryCoordAtlasT = BrainAtlasManager . WorldU2WorldT ( worldU ) ;
773+ // Vector3 worldU = BrainAtlasManager.ActiveReferenceAtlas.AtlasIdx2World(entryCoordAtlasIdx);
774+ // Vector3 entryCoordAtlasT = BrainAtlasManager.WorldU2WorldT(worldU);
775775
776- // var entryCoordAtlasT = BrainAtlasManager.ActiveAtlasTransform.U2T(
777- // BrainAtlasManager.ActiveReferenceAtlas.World2Atlas(
778- // BrainAtlasManager.ActiveReferenceAtlas.AtlasIdx2World(entryCoordAtlasIdx)));
776+ var entryCoordAtlasT = BrainAtlasManager . ActiveAtlasTransform . U2T (
777+ BrainAtlasManager . ActiveReferenceAtlas . World2Atlas (
778+ BrainAtlasManager . ActiveReferenceAtlas . AtlasIdx2World ( entryCoordAtlasIdx ) ) ) ;
779779
780780 _probeController . SetProbePosition ( entryCoordAtlasT ) ;
781781 }
@@ -820,38 +820,42 @@ public void UpdateSurfacePosition()
820820 ( Vector3 tipCoordWorldU , _ , _ , Vector3 tipForwardWorldU ) = _probeController . GetTipWorldU ( ) ;
821821
822822 Vector3 tipAtlasIdxU = BrainAtlasManager . ActiveReferenceAtlas . World2AtlasIdx ( tipCoordWorldU ) ;
823-
823+
824824 Vector3 downDir = useDV ? Vector3 . down : tipForwardWorldU ;
825825 Vector3 downDirAtlas = BrainAtlasManager . ActiveReferenceAtlas . World2Atlas_Vector ( downDir ) ;
826- Debug . Log ( downDirAtlas ) ;
827826
828827 // Check if we're in the brain
829828 bool probeInBrain = BrainAtlasManager . ActiveReferenceAtlas . GetAnnotationIdx ( tipAtlasIdxU ) > 0 ;
830829
831830 Vector3 tipInBrain = tipAtlasIdxU ;
832831
833832 // Adjust the tip coordinate to put it into the brain
833+ // This is kind of a dumb algorithm, we
834834 if ( ! probeInBrain )
835835 {
836+ // Get which direction we need to go
837+ ( int ap , int ml , int dv ) = BrainAtlasManager . ActiveReferenceAtlas . DimensionsIdx ;
838+ Vector3 center = new Vector3 ( ap , ml , dv ) / 2f ;
839+
840+ Vector3 towardBox = downDirAtlas ;
841+
842+ // Invert if the vector points the wrong way
843+ if ( Vector3 . Dot ( downDirAtlas , tipAtlasIdxU - center ) > 0 )
844+ towardBox = - towardBox ;
845+
846+ // Step in 1mm increments
847+ float stepSize = 1000f / BrainAtlasManager . ActiveReferenceAtlas . Resolution . z ;
848+
836849 bool done = false ;
837- float [ ] offset = { + 1000f , - 1000f } ;
838- for ( int mult = 1 ; mult <= 16 ; mult *= 2 )
850+ for ( int steps = 1 ; steps < Mathf . RoundToInt ( BrainAtlasManager . ActiveReferenceAtlas . Dimensions . z * 2f ) ; steps ++ )
839851 {
840- for ( int i = 0 ; i < 2 ; i ++ )
852+ Vector3 tempTip = tipAtlasIdxU + towardBox * stepSize * steps ;
853+ if ( BrainAtlasManager . ActiveReferenceAtlas . GetAnnotationIdx ( tempTip ) > 0 )
841854 {
842- Vector3 temporaryTip = tipAtlasIdxU + downDirAtlas * offset [ i ] * mult / BrainAtlasManager . ActiveReferenceAtlas . Resolution . z ;
843- Debug . Log ( temporaryTip ) ;
844- if ( BrainAtlasManager . ActiveReferenceAtlas . GetAnnotationIdx ( temporaryTip ) > 0 )
845- {
846- tipInBrain = temporaryTip ;
847- Debug . Log ( tipInBrain ) ;
848- done = true ;
849- break ;
850- }
851- }
852-
853- if ( done )
855+ tipInBrain = tempTip ;
856+ done = true ;
854857 break ;
858+ }
855859 }
856860
857861 if ( ! done )
@@ -901,7 +905,7 @@ public Vector3 FindEntryIdxCoordinate(Vector3 bottomIdxCoordU, Vector3 downVecto
901905
902906 if ( finalPerc > - 1f )
903907 {
904- for ( float perc = finalPerc ; perc >= 0f ; perc -= 0.0001f )
908+ for ( float perc = finalPerc ; perc >= 0f ; perc -= 0.001f )
905909 {
906910 Vector3 point = Vector3 . Lerp ( topSearchIdxCoordU , bottomIdxCoordU , perc ) ;
907911 if ( BrainAtlasManager . ActiveReferenceAtlas . GetAnnotationIdx ( point ) <= 0 )
0 commit comments