Skip to content

Commit 2024602

Browse files
authored
Merge pull request #47 from EarthyScience/Context
Context
2 parents d69c798 + cf29c9d commit 2024602

File tree

13 files changed

+313
-463
lines changed

13 files changed

+313
-463
lines changed

src/components/CanvasGeometry.tsx

Lines changed: 55 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@ import * as THREE from 'three'
22
THREE.Cache.enabled = true;
33
import { Canvas } from '@react-three/fiber';
44
import { Center, OrbitControls, Environment } from '@react-three/drei'
5-
import { variables, ZarrDataset, parseUVCoords } from '@/components/ZarrLoaderLRU'
5+
import { variables, ZarrDataset } from '@/components/ZarrLoaderLRU'
66
import { useEffect, useState, useMemo } from 'react';
77
import { useControls } from 'leva'
8-
import { DataCube, PointCloud, UVCube, PlotLine, PlotArea, FixedTicks } from './PlotObjects';
8+
import { PointCloud, UVCube, PlotArea, DataCube } from './PlotObjects';
99
import { GetColorMapTexture, ArrayToTexture, DefaultCube, colormaps } from './Textures';
10-
import { Metadata, ResizeBar } from './UI';
10+
import { Metadata } from './UI';
11+
import { plotContext, DimCoords } from './Contexts';
1112

1213
const storeURL = "https://s3.bgc-jena.mpg.de:9000/esdl-esdc-v3.0.2/esdc-16d-2.5deg-46x72x1440-3.0.2.zarr"
1314

14-
interface TimeSeriesLocs{
15-
uv:THREE.Vector2;
16-
normal:THREE.Vector3
17-
}
18-
interface Coord {
19-
name: string;
20-
loc: number;
21-
units: string;
22-
}
23-
24-
interface DimCoords {
25-
first: Coord;
26-
second: Coord;
27-
plot: Pick<Coord, "units">; // Only units
15+
function Loading({showLoading}:{showLoading:boolean}){
16+
return(
17+
<div className='messages'>
18+
{showLoading && <div className='loading'>
19+
Loading...
20+
</div>}
21+
</div>
22+
)
2823
}
2924

3025
export function CanvasGeometry() {
@@ -53,22 +48,18 @@ export function CanvasGeometry() {
5348

5449
const [texture, setTexture] = useState<THREE.DataTexture | THREE.Data3DTexture | null>(null) //Main Texture
5550
const [shape, setShape] = useState<THREE.Vector3 | THREE.Vector3>(new THREE.Vector3(2, 2, 2))
56-
const [timeSeriesLocs,setTimeSeriesLocs] = useState<TimeSeriesLocs>({uv:new THREE.Vector2(.5,.5), normal:new THREE.Vector3(0,0,1)})
5751
const [valueScales,setValueScales] = useState({maxVal:1,minVal:-1})
5852
const [colormap,setColormap] = useState<THREE.DataTexture>(GetColorMapTexture())
5953
const [timeSeries, setTimeSeries] = useState<number[]>([0]);
6054
const [showLoading, setShowLoading] = useState<boolean>(false);
61-
const [metadata,setMetadata] = useState<Object[]>([{},{}])
55+
const [metadata,setMetadata] = useState<object[] | null>(null)
6256

6357
//Timeseries Plotting Information
6458
const [dimArrays,setDimArrays] = useState([[0],[0],[0]])
6559
const [dimNames,setDimNames] = useState<string[]>(["default"])
6660
const [dimUnits,setDimUnits] = useState<string[]>(["Default"]);
67-
const [dimCoords, setDimCoords] = useState<Object | null>(null);
61+
const [dimCoords, setDimCoords] = useState<DimCoords>();
6862
const [plotDim,setPlotDim] = useState<number>(0)
69-
const [height, setHeight] = useState<number>(Math.round(window.innerHeight-(window.innerHeight*0.15)-48))
70-
71-
7263
const ZarrDS = useMemo(()=>new ZarrDataset(storeURL),[])
7364

7465
useEffect(()=>{
@@ -104,6 +95,7 @@ export function CanvasGeometry() {
10495
setShape(new THREE.Vector3(2, shapeRatio, 2));
10596
setShowLoading(false)
10697
})
98+
//Get Metadata
10799
ZarrDS.GetAttributes(variable).then((result)=>{
108100
setMetadata(result);
109101
const [dimArrs, dimMetas] = ZarrDS.GetDimArrays()
@@ -126,51 +118,48 @@ export function CanvasGeometry() {
126118
setTexture(texture);
127119
}
128120
setShape(new THREE.Vector3(2, 2, 2))
129-
setMetadata([{}])
121+
setMetadata(null)
130122
}
131123
}, [variable])
132124

133-
//TIMESERIES
134-
useEffect(()=>{
135-
if(ZarrDS && metadata){
136-
ZarrDS.GetTimeSeries(timeSeriesLocs).then((e)=> setTimeSeries(e))
137-
const plotDim = (timeSeriesLocs.normal.toArray()).map((val, idx) => {
138-
if (Math.abs(val) > 0) {
139-
return idx;
140-
}
141-
return null;}).filter(idx => idx !== null);
142-
setPlotDim(2-plotDim[0]) //I think this 2 is only if there are 3-dims. Need to rework the logic
143-
144-
const coordUV = parseUVCoords({normal:timeSeriesLocs.normal,uv:timeSeriesLocs.uv})
145-
let dimCoords = coordUV.map((val,idx)=>val ? dimArrays[idx][Math.round(val*dimArrays[idx].length)] : null)
146-
const thisDimNames = dimNames.filter((_,idx)=> dimCoords[idx] !== null)
147-
const thisDimUnits = dimUnits.filter((_,idx)=> dimCoords[idx] !== null)
148-
dimCoords = dimCoords.filter(val => val !== null)
149-
const dimObj = {
150-
first:{
151-
name:thisDimNames[0],
152-
loc:dimCoords[0],
153-
units:thisDimUnits[0]
154-
},
155-
second:{
156-
name:thisDimNames[1],
157-
loc:dimCoords[1],
158-
units:thisDimUnits[1]
159-
},
160-
plot:{
161-
units:dimUnits[2-plotDim[0]]
162-
}
163-
}
164-
setDimCoords(dimObj)
125+
//These are passed to the UVCube (will be renamed) to extract the timeseries info
126+
const timeSeriesObj ={
127+
setters:{
128+
setTimeSeries,
129+
setPlotDim,
130+
setDimCoords
131+
},
132+
values:{
133+
ZarrDS,
134+
shape,
135+
dimArrays,
136+
dimNames,
137+
dimUnits
165138
}
166-
},[timeSeriesLocs])
139+
}
140+
141+
const defaultDimCoords: DimCoords = {
142+
first: { name: "", loc: 0, units: "" },
143+
second: { name: "", loc: 0, units: "" },
144+
plot: { units: "" }
145+
};
146+
147+
//This is the data being passed down the plot tree
148+
const plotObj = {
149+
coords: dimCoords ?? defaultDimCoords,
150+
plotDim,
151+
dimNames,
152+
dimUnits,
153+
dimArrays,
154+
plotUnits:metadata ? (metadata as any).units : "Default",
155+
yRange:[valueScales.minVal,valueScales.maxVal],
156+
timeSeries,
157+
scaling:{...valueScales,colormap}
158+
}
159+
167160
return (
168161
<>
169-
<div className='messages'>
170-
{showLoading && <div className='loading'>
171-
Loading...
172-
</div>}
173-
</div>
162+
<Loading showLoading={showLoading} />
174163
<div className='canvas'>
175164
<Canvas shadows camera={{ position: [-4.5, 3, 4.5], fov: 50 }}
176165
frameloop="demand"
@@ -180,7 +169,7 @@ export function CanvasGeometry() {
180169
{/* Volume Plots */}
181170
{plotter == "volume" && <>
182171
<DataCube volTexture={texture} shape={shape} colormap={colormap}/>
183-
<UVCube shape={shape} setTimeSeriesLocs={setTimeSeriesLocs}/>
172+
<UVCube {...timeSeriesObj} />
184173
</>}
185174
{/* Point Clouds Plots */}
186175
{plotter == "point-cloud" && <PointCloud textures={{texture,colormap}} />}
@@ -194,16 +183,9 @@ export function CanvasGeometry() {
194183

195184
{metadata && <Metadata data={metadata} /> }
196185

197-
<ResizeBar height={height} setHeight={setHeight} />
198-
<PlotArea height={height} coords={dimCoords as DimCoords }>
199-
<PlotLine
200-
data={timeSeries}
201-
lineWidth={20}
202-
scaling={{...valueScales,colormap}}
203-
height={height}
204-
/>
205-
{dimCoords && <FixedTicks color='white' xDimArray={dimArrays[plotDim]} yRange={[valueScales.minVal,valueScales.maxVal]} coords={dimCoords as DimCoords} height={height}/>}
206-
</PlotArea>
186+
<plotContext.Provider value={plotObj} >
187+
<PlotArea />
188+
</plotContext.Provider>
207189

208190
{/* <Leva theme={lightTheme} /> */}
209191
</>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createContext } from "react";
2+
import * as THREE from 'three'
3+
4+
interface Coord {
5+
name: string;
6+
loc: number;
7+
units: string;
8+
}
9+
export interface DimCoords {
10+
first: Coord;
11+
second: Coord;
12+
plot: Pick<Coord, "units">; // Only units
13+
}
14+
interface PlotContextType {
15+
coords: DimCoords;
16+
plotDim: number;
17+
dimNames:string[],
18+
dimUnits:string[],
19+
dimArrays: number[][];
20+
plotUnits:string;
21+
yRange: number[];
22+
timeSeries: number[];
23+
scaling: {
24+
colormap: THREE.DataTexture;
25+
maxVal: number;
26+
minVal: number;
27+
};
28+
}
29+
const plotObj = {
30+
coords: {
31+
first: {
32+
name: "DefaultName1",
33+
loc: 0,
34+
units: "meters",
35+
},
36+
second: {
37+
name: "DefaultName2",
38+
loc: 1,
39+
units: "meters",
40+
},
41+
plot: {
42+
units: "pixels",
43+
},
44+
},
45+
plotDim: 100, // Example dimension value
46+
dimNames:["Default","Default","Default"],
47+
dimUnits:["Default","Default","Default"],
48+
dimArrays: [[0, 1, 2], [3, 4, 5]], // Example array of arrays
49+
plotUnits:"Default",
50+
yRange: [0, 10], // Example range
51+
timeSeries: [1, 2, 3, 4, 5], // Example time series
52+
scaling: {
53+
colormap: new THREE.DataTexture(), // Example DataTexture instance
54+
maxVal: 100, // Example max value
55+
minVal: 0, // Example min value
56+
},
57+
};
58+
59+
export const plotContext = createContext<PlotContextType>(plotObj)
60+

src/components/Contexts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {plotContext} from './PlotContext'
2+
export type {DimCoords} from './PlotContext'

src/components/PlotObjects/FixedTicks.tsx

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Text, OrbitControls } from '@react-three/drei'
22
import { useThree, useFrame } from '@react-three/fiber'
3-
import { useState, useMemo, useEffect, useRef } from 'react'
3+
import { useState, useMemo, useEffect, useRef, useContext } from 'react'
44
import { parseTimeUnit } from '@/utils/HelperFuncs'
5+
import { plotContext } from '../Contexts/PlotContext'
56

67

78
interface ViewportBounds {
@@ -17,23 +18,6 @@ interface FixedTicksProps {
1718
fontSize?: number;
1819
showGrid?: boolean;
1920
gridOpacity?: number;
20-
xDimArray: number[];
21-
yRange: number[];
22-
coords:{
23-
first:{
24-
name:string,
25-
loc:number,
26-
units:string
27-
},
28-
second:{
29-
name:string,
30-
loc:number,
31-
units:string
32-
},
33-
plot:{
34-
units: string
35-
}
36-
};
3721
height:number
3822
}
3923

@@ -43,28 +27,12 @@ export function FixedTicks({
4327
fontSize = 18,
4428
showGrid = true,
4529
gridOpacity = 0.5,
46-
xDimArray = [0,0,0,0,0],
47-
yRange = [0,1],
48-
coords = {
49-
first:{
50-
name:"Default",
51-
loc:0.5,
52-
units:"Default"
53-
},
54-
second:{
55-
name:"Default",
56-
loc:0.5,
57-
units:"Default"
58-
},
59-
plot:{
60-
units:"Default"
61-
}
62-
},
6330
height
6431
}: FixedTicksProps) {
6532
const { camera } = useThree()
6633
const [bounds, setBounds] = useState<ViewportBounds>({ left: 0, right: 0, top: 0, bottom: 0 })
67-
34+
const {coords, yRange, dimArrays, plotDim} = useContext(plotContext)
35+
const xDimArray = dimArrays[plotDim]
6836
const xTickCount = 10;
6937
const yTickCount = 8;
7038

@@ -221,7 +189,7 @@ export function FixedTicks({
221189
anchorX="center"
222190
anchorY="top"
223191
>
224-
{textArray?.[Math.round(normX*xDimSize)] ?? ''}
192+
{textArray?.[Math.round(normX*xDimSize-.5)] ?? ''}
225193
</Text>
226194
)}
227195
</group>
@@ -237,8 +205,8 @@ export function FixedTicks({
237205
const x = horX
238206
return (
239207
<>
240-
<group position={[x,y,0]}>
241-
<line key={`hgrid-${i}`} >
208+
<group key={`hgrid-${i}`} position={[x,y,0]}>
209+
<line >
242210
<bufferGeometry>
243211
<float32BufferAttribute
244212
attach="attributes-position"

src/components/PlotObjects/PlotArea/PlotArea.css

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,18 @@
22
position: relative;
33
width: 200px;
44
bottom:50%;
5-
left:2%
5+
left:2%;
6+
pointer-events: none;
7+
}
8+
9+
.point-info{
10+
position: fixed;
11+
z-index: 9;
12+
color: white;
13+
background: rgb(66, 66, 66);
14+
pointer-events: none;
15+
transform: translate(10px,20px);
16+
padding: 5px;
17+
border-radius: 5px;
18+
box-shadow: 1px 2px 5px rgba(255, 255, 255, 0.37);
619
}

0 commit comments

Comments
 (0)