Skip to content

Commit f301a15

Browse files
committed
Moved to hook
1 parent af345b1 commit f301a15

7 files changed

Lines changed: 43 additions & 76 deletions

File tree

src/components/plots/DataCube.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useGlobalStore, usePlotStore } from '@/GlobalStates';
55
import { useShallow } from 'zustand/shallow';
66
import { invalidate, useFrame } from '@react-three/fiber';
77
import { deg2rad } from '@/utils/HelperFuncs';
8+
import { useCoordBounds } from '@/hooks/useCoordBounds';
89

910
interface DataCubeProps {
1011
volTexture: THREE.Data3DTexture[] | THREE.DataTexture[] | null,
@@ -20,8 +21,7 @@ export const DataCube = ({ volTexture }: DataCubeProps ) => {
2021
const {
2122
valueRange, xRange, yRange, zRange, quality, useOrtho,
2223
animProg, cScale, cOffset, useFragOpt, transparency, maskTexture, maskValue,
23-
nanTransparency, nanColor, vTransferRange, vTransferScale, fillValue, lonExtent, latExtent,
24-
lonResolution, latResolution} = usePlotStore(useShallow(state => ({
24+
nanTransparency, nanColor, vTransferRange, vTransferScale, fillValue} = usePlotStore(useShallow(state => ({
2525
valueRange: state.valueRange, xRange: state.xRange,
2626
yRange: state.yRange, zRange: state.zRange,
2727
quality: state.quality, useOrtho: state.useOrtho,
@@ -35,22 +35,12 @@ export const DataCube = ({ volTexture }: DataCubeProps ) => {
3535
vTransferRange: state.vTransferRange,
3636
vTransferScale: state.vTransferScale,
3737
fillValue: state.fillValue,
38-
lonExtent: state.lonExtent,
39-
latExtent: state.latExtent,
40-
lonResolution: state.lonResolution,
41-
latResolution: state.latResolution,
4238
})))
4339
const meshRef = useRef<THREE.Mesh>(null!);
4440
const aspectRatio = shape.y/shape.x
4541
const timeRatio = shape.z/shape.x;
46-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
47-
const newLatStep = latResolution/2;
48-
const newLonStep = lonResolution/2;
49-
const newLonBounds = [lonExtent[0]-newLonStep, lonExtent[1]+newLonStep]
50-
const newLatBounds = [latExtent[0]-newLatStep, latExtent[1]+newLatStep]
51-
return [newLonBounds, newLatBounds]
52-
},[latExtent, lonExtent, lonResolution, latResolution])
53-
console.log(lonBounds)
42+
const {lonBounds, latBounds} = useCoordBounds()
43+
5444
const shaderMaterial = useMemo(()=>new THREE.ShaderMaterial({
5545
glslVersion: THREE.GLSL3,
5646
uniforms: {

src/components/plots/FlatBlocks.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as THREE from 'three'
55
import { sphereBlocksFrag, flatBlocksVert, flatBlocksVert3D } from '../textures/shaders'
66
import { invalidate } from '@react-three/fiber'
77
import { deg2rad } from '@/utils/HelperFuncs'
8+
import { useCoordBounds } from '@/hooks/useCoordBounds'
89

910
const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTexture[] | null}) => {
1011
const {colormap, isFlat, valueScales, flipY,
@@ -17,16 +18,12 @@ const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTe
1718
textureArrayDepths: state.textureArrayDepths
1819
})))
1920
const { animProg, cOffset, cScale, nanColor, nanTransparency, displacement, offsetNegatives, rotateFlat, maskTexture, maskValue,
20-
lonResolution, latResolution, lonExtent, latExtent} = usePlotStore(useShallow(state=> ({
21+
} = usePlotStore(useShallow(state=> ({
2122
animate: state.animate, animProg: state.animProg, cOffset: state.cOffset,
2223
cScale: state.cScale, nanColor: state.nanColor, nanTransparency: state.nanTransparency,
2324
displacement: state.displacement, sphereResolution: state.sphereResolution,
2425
offsetNegatives: state.offsetNegatives, rotateFlat:state.rotateFlat,
2526
maskTexture:state.maskTexture, maskValue:state.maskValue,
26-
lonExtent: state.lonExtent,
27-
latExtent: state.latExtent,
28-
lonResolution: state.lonResolution,
29-
latResolution: state.latResolution,
3027
})))
3128
const {analysisMode, axis} = useAnalysisStore(useShallow(state => ({
3229
analysisMode: state.analysisMode, axis:state.axis
@@ -75,13 +72,7 @@ const FlatBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTe
7572
);
7673
return geo
7774
},[width, height])
78-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
79-
const newLatStep = latResolution/2;
80-
const newLonStep = lonResolution/2;
81-
const newLonBounds = [Math.max(lonExtent[0]-newLonStep, -180), Math.min(lonExtent[1]+newLonStep, 180)]
82-
const newLatBounds = [Math.max(latExtent[0]-newLatStep, -90), Math.min(latExtent[1]+newLatStep, 90)]
83-
return [newLonBounds, newLatBounds]
84-
},[latExtent, lonExtent, lonResolution, latResolution])
75+
const {lonBounds, latBounds} = useCoordBounds()
8576

8677
const shaderMaterial = useMemo(()=>{
8778
const shader = new THREE.ShaderMaterial({

src/components/plots/FlatMap.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useShallow } from 'zustand/shallow'
99
import { ThreeEvent } from '@react-three/fiber';
1010
import { coarsenFlatArray, GetCurrentArray, GetTimeSeries, parseUVCoords, deg2rad } from '@/utils/HelperFuncs';
1111
import { evaluate_cmap } from 'js-colormaps-es';
12+
import { useCoordBounds } from '@/hooks/useCoordBounds';
1213

1314
interface InfoSettersProps{
1415
setLoc: React.Dispatch<React.SetStateAction<number[]>>;
@@ -40,7 +41,6 @@ const FlatMap = ({textures, infoSetters} : {textures : THREE.DataTexture | THREE
4041

4142
const {cScale, cOffset, animProg, nanTransparency, nanColor,
4243
zSlice, ySlice, xSlice, selectTS, coarsen, maskTexture, maskValue,
43-
lonResolution, latResolution, lonExtent, latExtent,
4444
getColorIdx, incrementColorIdx} = usePlotStore(useShallow(state => ({
4545
cOffset: state.cOffset, cScale: state.cScale,
4646
resetAnim: state.resetAnim, animate: state.animate,
@@ -49,10 +49,6 @@ const FlatMap = ({textures, infoSetters} : {textures : THREE.DataTexture | THREE
4949
ySlice: state.ySlice, xSlice: state.xSlice,
5050
selectTS: state.selectTS, coarsen: state.coarsen,
5151
maskTexture:state.maskTexture, maskValue:state.maskValue,
52-
lonExtent: state.lonExtent,
53-
latExtent: state.latExtent,
54-
lonResolution: state.lonResolution,
55-
latResolution: state.latResolution,
5652
getColorIdx: state.getColorIdx,
5753
incrementColorIdx: state.incrementColorIdx
5854
})))
@@ -100,13 +96,7 @@ const FlatMap = ({textures, infoSetters} : {textures : THREE.DataTexture | THREE
10096
const sampleArray = useMemo(()=> analysisMode ? analysisArray : GetCurrentArray(),[analysisMode, analysisArray, textures])
10197
const analysisDims = useMemo(()=>dimArrays.length > 2 ? dimSlices.filter((_e,idx)=> idx != axis) : dimSlices,[dimSlices,axis])
10298

103-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
104-
const newLatStep = latResolution/2;
105-
const newLonStep = lonResolution/2;
106-
const newLonBounds = [Math.max(lonExtent[0]-newLonStep, -180), Math.min(lonExtent[1]+newLonStep, 180)]
107-
const newLatBounds = [Math.max(latExtent[0]-newLatStep, -90), Math.min(latExtent[1]+newLatStep, 90)]
108-
return [newLonBounds, newLatBounds]
109-
},[latExtent, lonExtent, lonResolution, latResolution])
99+
const {lonBounds, latBounds} = useCoordBounds()
110100

111101
useEffect(()=>{
112102
geometry.dispose()

src/components/plots/PointCloud.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useAnalysisStore, useGlobalStore, usePlotStore } from '@/GlobalStates';
55
import { useShallow } from 'zustand/shallow';
66
import { parseUVCoords, getUnitAxis, GetTimeSeries, GetCurrentArray, deg2rad } from '@/utils/HelperFuncs';
77
import { evaluate_cmap } from 'js-colormaps-es';
8+
import { useCoordBounds } from '@/hooks/useCoordBounds';
89

910
interface PCProps {
1011
texture: THREE.Data3DTexture[] | null,
@@ -147,7 +148,7 @@ export const PointCloud = ({textures} : {textures:PCProps} )=>{
147148
})))
148149
const {scalePoints, scaleIntensity, pointSize, cScale, cOffset, valueRange, animProg,
149150
selectTS, timeScale, xRange, yRange, zRange, fillValue,
150-
maskTexture, maskValue, lonExtent, latExtent, lonResolution, latResolution, } = usePlotStore(useShallow(state => ({
151+
maskTexture, maskValue } = usePlotStore(useShallow(state => ({
151152
scalePoints: state.scalePoints,
152153
scaleIntensity: state.scaleIntensity,
153154
pointSize: state.pointSize,
@@ -163,10 +164,6 @@ export const PointCloud = ({textures} : {textures:PCProps} )=>{
163164
fillValue:state.fillValue,
164165
maskTexture: state.maskTexture,
165166
maskValue: state.maskValue,
166-
lonExtent: state.lonExtent,
167-
latExtent: state.latExtent,
168-
lonResolution: state.lonResolution,
169-
latResolution: state.latResolution,
170167
})))
171168

172169
const [pointsObj, setPointsObj] = useState<Record<string, number>>({})
@@ -206,13 +203,8 @@ export const PointCloud = ({textures} : {textures:PCProps} )=>{
206203
geom.setDrawRange(0, arrayLength); // This is used to tell it how many data points are needed since we aren't giving it positions.
207204
return geom;
208205
}, [data]);
209-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
210-
const newLatStep = latResolution/2;
211-
const newLonStep = lonResolution/2;
212-
const newLonBounds = [Math.max(lonExtent[0]-newLonStep, -180), Math.min(lonExtent[1]+newLonStep, 180)]
213-
const newLatBounds = [Math.max(latExtent[0]-newLatStep, -90), Math.min(latExtent[1]+newLatStep, 90)]
214-
return [newLonBounds, newLatBounds]
215-
},[latExtent, lonExtent, lonResolution, latResolution])
206+
const {lonBounds, latBounds} = useCoordBounds()
207+
216208
const shaderMaterial = useMemo(()=> (new THREE.ShaderMaterial({
217209
glslVersion: THREE.GLSL3,
218210
uniforms: {

src/components/plots/Sphere.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useShallow } from 'zustand/shallow'
55
import { sphereVertex, sphereVertexFlat, sphereFrag, flatSphereFrag } from '../textures/shaders'
66
import { parseUVCoords, GetTimeSeries, GetCurrentArray, deg2rad } from '@/utils/HelperFuncs';
77
import { evaluate_cmap } from 'js-colormaps-es';
8+
import { useCoordBounds } from '@/hooks/useCoordBounds'
89

910

1011
function XYZtoUV(xyz : THREE.Vector3, width: number, height : number){
@@ -51,19 +52,14 @@ export const Sphere = ({textures} : {textures: THREE.Data3DTexture[] | THREE.Dat
5152
textureArrayDepths: state.textureArrayDepths
5253
})))
5354

54-
const {animate, animProg, cOffset, cScale, selectTS, lonExtent, latExtent,
55-
lonResolution, latResolution, nanColor, nanTransparency, sphereDisplacement, sphereResolution,
55+
const {animate, animProg, cOffset, cScale, selectTS, nanColor, nanTransparency, sphereDisplacement, sphereResolution,
5656
zSlice, ySlice, xSlice, fillValue, borderTexture, maskTexture, maskValue,
5757
getColorIdx, incrementColorIdx} = usePlotStore(useShallow(state=> ({
5858
animate: state.animate,
5959
animProg: state.animProg,
6060
cOffset: state.cOffset,
6161
cScale: state.cScale,
6262
selectTS: state.selectTS,
63-
lonExtent: state.lonExtent,
64-
latExtent: state.latExtent,
65-
lonResolution: state.lonResolution,
66-
latResolution: state.latResolution,
6763
nanColor: state.nanColor,
6864
nanTransparency: state.nanTransparency,
6965
sphereDisplacement: state.displacement,
@@ -109,13 +105,7 @@ export const Sphere = ({textures} : {textures: THREE.Data3DTexture[] | THREE.Dat
109105
setBoundsObj(prev=>{ return {...newBoundObj, ...prev}})
110106
}
111107

112-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
113-
const newLatStep = latResolution/2;
114-
const newLonStep = lonResolution/2;
115-
const newLonBounds = [lonExtent[0]-newLonStep, lonExtent[1]+newLonStep]
116-
const newLatBounds = [latExtent[0]-newLatStep, latExtent[1]+newLatStep]
117-
return [newLonBounds, newLatBounds]
118-
},[latExtent, lonExtent, lonResolution, latResolution])
108+
const {lonBounds, latBounds} = useCoordBounds()
119109

120110
const geometry = useMemo(() => new THREE.IcosahedronGeometry(1, sphereResolution), [sphereResolution]);
121111
const shaderMaterial = useMemo(()=>{

src/components/plots/SphereBlocks.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as THREE from 'three'
55
import { sphereBlocksVert, sphereBlocksVertFlat, sphereBlocksFrag } from '../textures/shaders'
66
import { invalidate } from '@react-three/fiber'
77
import { deg2rad } from '@/utils/HelperFuncs'
8+
import { useCoordBounds } from '@/hooks/useCoordBounds'
89
const SphereBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.DataTexture[] | null}) => {
910
const {colormap, isFlat, valueScales,
1011
dataShape, textureArrayDepths} = useGlobalStore(useShallow(state=>({
@@ -14,16 +15,11 @@ const SphereBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.Data
1415
dataShape: state.dataShape,
1516
textureArrayDepths: state.textureArrayDepths,
1617
})))
17-
const { animProg, cOffset, cScale, lonExtent, latExtent, lonResolution, latResolution,
18-
nanColor, nanTransparency, sphereDisplacement, offsetNegatives, fillValue, maskTexture, maskValue} = usePlotStore(useShallow(state=> ({
18+
const { animProg, cOffset, cScale, nanColor, nanTransparency, sphereDisplacement, offsetNegatives, fillValue, maskTexture, maskValue} = usePlotStore(useShallow(state=> ({
1919
animate: state.animate,
2020
animProg: state.animProg,
2121
cOffset: state.cOffset,
2222
cScale: state.cScale,
23-
lonExtent: state.lonExtent,
24-
latExtent: state.latExtent,
25-
lonResolution: state.lonResolution,
26-
latResolution: state.latResolution,
2723
nanColor: state.nanColor,
2824
nanTransparency: state.nanTransparency,
2925
sphereDisplacement: state.displacement,
@@ -73,13 +69,8 @@ const SphereBlocks = ({textures} : {textures: THREE.Data3DTexture[] | THREE.Data
7369
return geo
7470
},[dataShape])
7571

76-
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
77-
const newLatStep = latResolution/2;
78-
const newLonStep = lonResolution/2;
79-
const newLonBounds = [Math.max(lonExtent[0]-newLonStep, -180), Math.min(lonExtent[1]+newLonStep, 180)]
80-
const newLatBounds = [Math.max(latExtent[0]-newLatStep, -90), Math.min(latExtent[1]+newLatStep, 90)]
81-
return [newLonBounds, newLatBounds]
82-
},[latExtent, lonExtent, lonResolution, latResolution])
72+
const {lonBounds, latBounds} = useCoordBounds()
73+
8374
const shaderMaterial = useMemo(()=>{
8475
const shader = new THREE.ShaderMaterial({
8576
glslVersion: THREE.GLSL3,

src/hooks/useCoordBounds.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useMemo } from "react";
2+
import { usePlotStore } from "@/GlobalStates";
3+
import { useShallow } from "zustand/shallow";
4+
5+
6+
export const useCoordBounds = ()=>{
7+
const {lonExtent, latExtent, lonResolution, latResolution} = usePlotStore(useShallow(state=>({
8+
lonExtent: state.lonExtent,
9+
latExtent: state.latExtent,
10+
lonResolution: state.lonResolution,
11+
latResolution: state.latResolution,
12+
})))
13+
const [lonBounds, latBounds] = useMemo(()=>{ //The bounds for the shader. It takes the middle point of the furthest coordinate and adds the distance to edge of pixel
14+
const newLatStep = latResolution/2;
15+
const newLonStep = lonResolution/2;
16+
const newLonBounds = [Math.max(lonExtent[0]-newLonStep, -180), Math.min(lonExtent[1]+newLonStep, 180)]
17+
const newLatBounds = [Math.max(latExtent[0]-newLatStep, -90), Math.min(latExtent[1]+newLatStep, 90)]
18+
return [newLonBounds, newLatBounds]
19+
},[latExtent, lonExtent, lonResolution, latResolution])
20+
return {
21+
lonBounds,latBounds
22+
}
23+
}

0 commit comments

Comments
 (0)