11"use client" ;
22
3- import React , { useMemo , useEffect , useRef , useState } from 'react'
3+ import React , { useMemo , useEffect , useRef } from 'react'
44import * as THREE from 'three'
55import { useAnalysisStore } from '@/GlobalStates/AnalysisStore' ;
66import { useGlobalStore } from '@/GlobalStates/GlobalStore' ;
@@ -10,6 +10,7 @@ import { vertShader } from '@/components/computation/shaders'
1010import { useShallow } from 'zustand/shallow'
1111import { ThreeEvent } from '@react-three/fiber' ;
1212import { coarsenFlatArray , GetCurrentArray , GetTimeSeries , parseUVCoords , deg2rad } from '@/utils/HelperFuncs' ;
13+ import { sampleCRS } from '../textures/ProjectionTexture' ;
1314import { evaluateColorMap } from '@/components/textures' ;
1415import { useCoordBounds } from '@/hooks/useCoordBounds' ;
1516import { flatFrag } from '../textures/shaders' ;
@@ -92,7 +93,6 @@ const FlatMap = ({textures: propTextures, infoSetters} : {textures : THREE.DataT
9293
9394 const geometry = useMemo ( ( ) => new THREE . PlaneGeometry ( 2 , 2 * shapeRatio ) , [ shapeRatio ] )
9495 const infoRef = useRef < boolean > ( false )
95- const lastUV = useRef < THREE . Vector2 > ( new THREE . Vector2 ( 0 , 0 ) )
9696 const rotateMap = analysisMode && axis == 2 ;
9797 const sampleArray = useMemo ( ( ) => analysisMode ? analysisArray : GetCurrentArray ( ) , [ analysisMode , analysisArray , textures ] )
9898 const analysisDims = useMemo ( ( ) => {
@@ -117,67 +117,88 @@ const FlatMap = ({textures: propTextures, infoSetters} : {textures : THREE.DataT
117117 const eventRef = useRef < ThreeEvent < PointerEvent > | null > ( null ) ;
118118 const handleMove = ( e : ThreeEvent < PointerEvent > ) => {
119119 if ( infoRef . current && e . uv ) {
120- eventRef . current = e ;
120+ let { uv} = e ;
121+ if ( ! uv ) return ;
121122 setLoc ( [ e . clientX , e . clientY ] ) ;
122- lastUV . current = e . uv ;
123- const { x, y } = e . uv ;
123+ eventRef . current = e ;
124+ if ( remapTexture ) {
125+ const [ thisUV , isValid ] = sampleCRS ( remapTexture , uv . x , flipY ? 1 - uv . y : uv . y ) // Weird double flippiing of UVs with flipY. Has something to do with how projected data is done.
126+ if ( flipY ) thisUV . y = 1 - thisUV . y
127+ if ( isValid ) uv = thisUV ;
128+ else {
129+ val . current = NaN ;
130+ coords . current = [ thisUV . y , thisUV . x ]
131+ return ;
132+ }
133+ }
134+
135+ const { x, y } = uv ;
124136 const zSliceIdx = dimSlices . length > 2 ? 2 : 1 ;
125137 const ySliceIdx = dimSlices . length > 2 ? 1 : 0 ;
126138 const xSize = isFlat ? ( analysisMode ? analysisDims [ 1 ] . length : dimSlices [ 1 ] . length ) : dimSlices [ zSliceIdx ] . length ;
127139 const ySize = isFlat ? ( analysisMode ? analysisDims [ 0 ] . length : dimSlices [ 0 ] . length ) : dimSlices [ ySliceIdx ] . length ;
128140
129- const xIdx = Math . round ( x * xSize - .5 )
130- const yIdx = Math . round ( y * ySize - .5 )
131- let dataIdx = xSize * yIdx + xIdx ;
132- dataIdx += isFlat ? 0 : Math . floor ( ( dimSlices [ 0 ] . length - 1 ) * animProg ) * xSize * ySize
141+ const xId = Math . round ( x * xSize - .5 )
142+ const yId = Math . round ( y * ySize - .5 )
143+ let dataIdx = xSize * yId + xId ;
144+ dataIdx += isFlat ? 0 : Math . floor ( ( dimSlices [ zIdx ] . length - 1 ) * animProg ) * xSize * ySize
133145 const dataVal = sampleArray ? sampleArray [ dataIdx ] : 0 ;
134146 val . current = dataVal ;
135- coords . current = isFlat ? analysisMode ? [ analysisDims [ 0 ] [ yIdx ] , analysisDims [ 1 ] [ xIdx ] ] : [ dimSlices [ 0 ] [ yIdx ] , dimSlices [ 1 ] [ xIdx ] ] : [ dimSlices [ ySliceIdx ] [ yIdx ] , dimSlices [ zSliceIdx ] [ xIdx ] ]
147+ coords . current = [ y , x ]
136148 }
137149 }
138150
139-
140151 // ----- TIMESERIES ----- //
141152 function HandleTimeSeries ( event : THREE . Intersection ) {
142- const uv = event . uv ;
143- const normal = new THREE . Vector3 ( 0 , 0 , 1 )
144- if ( uv ) {
145- const tsUV = flipY ? new THREE . Vector2 ( uv . x , 1 - uv . y ) : uv
146- const tempTS = GetTimeSeries ( { data :analysisMode ? analysisArray : GetCurrentArray ( ) , shape :dataShape , stride :strides } , { uv :tsUV , normal} )
147- setPlotDim ( 0 ) //I think this 2 is only if there are 3-dims. Need to rework the logic
148-
149- const coordUV = parseUVCoords ( { normal :normal , uv :uv } )
150- let dimCoords = coordUV . map ( ( val , idx ) => val ? dimSlices [ idx ] [ Math . round ( val * dimSlices [ idx ] . length ) ] : null )
151- const thisDimNames = dimNames . filter ( ( _ , idx ) => dimCoords [ idx ] !== null )
152- const thisDimUnits = dimUnits . filter ( ( _ , idx ) => dimCoords [ idx ] !== null )
153- dimCoords = dimCoords . filter ( val => val !== null )
154- const tsID = `${ dimCoords [ 0 ] } _${ dimCoords [ 1 ] } `
155- const tsObj = {
156- color : evaluateColorMap ( getColorIdx ( ) / 10 , 'Paired' ) ,
157- data : tempTS ,
158- normal,
159- uv : tsUV ,
160- }
161- incrementColorIdx ( ) ;
162- updateTimeSeries ( { [ tsID ] : tsObj } )
163- const dimObj = {
164- first :{
165- name :thisDimNames [ 0 ] ,
166- loc :dimCoords [ 0 ] ?? 0 ,
167- units :thisDimUnits [ 0 ]
168- } ,
169- second :{
170- name :thisDimNames [ 1 ] ,
171- loc :dimCoords [ 1 ] ?? 0 ,
172- units :thisDimUnits [ 1 ]
173- } ,
174- plot :{
175- units :dimUnits [ 0 ]
176- }
177- }
178- updateDimCoords ( { [ tsID ] : dimObj } )
179- }
153+ const uv = event . uv ;
154+ if ( ! uv ) return ;
155+ const tsUV = flipY ? new THREE . Vector2 ( uv . x , 1 - uv . y ) : uv
156+ let newUV : THREE . Vector2 | undefined ;
157+ const normal = new THREE . Vector3 ( 0 , 0 , 1 )
158+ if ( remapTexture ) {
159+ const [ thisUV , isValid ] = sampleCRS ( remapTexture , uv . x , flipY ? 1 - uv . y : uv . y ) // Weird double flippiing of UVs with flipY. Has something to do with how projected data is done.
160+ if ( flipY ) thisUV . y = 1 - thisUV . y
161+ if ( isValid ) newUV = thisUV ;
162+ else {
163+ return ;
180164 }
165+ }
166+
167+ const tempTS = GetTimeSeries ( { data :analysisMode ? analysisArray : GetCurrentArray ( ) , shape :dataShape , stride :strides } , { uv :newUV ?? tsUV , normal} )
168+ setPlotDim ( 0 ) //I think this 2 is only if there are 3-dims. Need to rework the logic
169+
170+ const coordUV = parseUVCoords ( { normal :normal , uv :uv } )
171+ let dimCoords = coordUV . map ( ( val , idx ) => val ? dimSlices [ idx ] [ Math . round ( val * dimSlices [ idx ] . length ) ] : null )
172+ const thisDimNames = dimNames . filter ( ( _ , idx ) => dimCoords [ idx ] !== null )
173+ const thisDimUnits = dimUnits . filter ( ( _ , idx ) => dimCoords [ idx ] !== null )
174+ dimCoords = dimCoords . filter ( val => val !== null )
175+ const tsID = `${ dimCoords [ 0 ] } _${ dimCoords [ 1 ] } `
176+ const tsObj = {
177+ color : evaluateColorMap ( getColorIdx ( ) / 10 , 'Paired' ) ,
178+ data : tempTS ,
179+ normal,
180+ uv : tsUV ,
181+ }
182+ incrementColorIdx ( ) ;
183+ updateTimeSeries ( { [ tsID ] : tsObj } )
184+ const dimObj = {
185+ first :{
186+ name :thisDimNames [ 0 ] ,
187+ loc :dimCoords [ 0 ] ?? 0 ,
188+ units :thisDimUnits [ 0 ]
189+ } ,
190+ second :{
191+ name :thisDimNames [ 1 ] ,
192+ loc :dimCoords [ 1 ] ?? 0 ,
193+ units :thisDimUnits [ 1 ]
194+ } ,
195+ plot :{
196+ units :dimUnits [ 0 ]
197+ }
198+ }
199+ updateDimCoords ( { [ tsID ] : dimObj } )
200+
201+ }
181202 // ----- SHADER MATERIAL ----- //
182203 const shaderMaterial = useMemo ( ( ) => new THREE . ShaderMaterial ( {
183204 glslVersion : THREE . GLSL3 ,
@@ -223,7 +244,10 @@ const FlatMap = ({textures: propTextures, infoSetters} : {textures : THREE.DataT
223244 uniforms . fillValue . value = fillValue ?? NaN
224245 }
225246 } , [ cScale , cOffset , colormap , animProg , nanColor , nanTransparency , latBounds , lonBounds , fillValue , maskValue , valueRange ] )
226-
247+ useEffect ( ( ) => {
248+ // This is duplicated. Probably shoud just move it to Plot.tsx
249+ useGlobalStore . setState ( { timeSeries :{ } , dimCoords :{ } } )
250+ } , [ remapTexture ] )
227251 return (
228252 < >
229253 < SquareMeshes />
0 commit comments