@@ -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
0 commit comments