|
| 1 | +"use cleint"; |
| 2 | + |
| 3 | +import { useGlobalStore, usePlotStore } from '@/utils/GlobalStates' |
| 4 | +import React, {useState, useMemo} from 'react' |
| 5 | +import { useShallow } from 'zustand/shallow' |
| 6 | +import { Text } from '@react-three/drei' |
| 7 | +import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js'; |
| 8 | +import { LineSegments2 } from 'three/addons/lines/LineSegments2.js'; |
| 9 | +import { LineMaterial } from 'three-stdlib'; |
| 10 | +import { useFrame } from '@react-three/fiber'; |
| 11 | +import { parseLoc } from '@/utils/HelperFuncs'; |
| 12 | + |
| 13 | +const HorizontalAxis = ({flipX, flipY}: {flipX: boolean, flipY: boolean}) =>{ |
| 14 | + const {dimArrays, dimNames, dimUnits} = useGlobalStore(useShallow(state => ({ |
| 15 | + dimArrays: state.dimArrays, |
| 16 | + dimNames: state.dimNames, |
| 17 | + dimUnits: state.dimUnits |
| 18 | + }))) |
| 19 | + |
| 20 | + const {xRange, yRange, zRange} = usePlotStore(useShallow(state => ({ |
| 21 | + xRange: state.xRange, |
| 22 | + yRange: state.yRange, |
| 23 | + zRange: state.zRange |
| 24 | + }))) |
| 25 | + |
| 26 | + const dimLengths = [dimArrays[0].length, dimArrays[1].length, dimArrays[2].length] |
| 27 | + |
| 28 | + const {shape} = useGlobalStore(useShallow(state => ({ |
| 29 | + shape: state.shape |
| 30 | + }))) |
| 31 | + const shapeRatio = shape.y/shape.x |
| 32 | + //@ts-expect-error The THREE people messed up their types in this component. It does take a string |
| 33 | + const lineMat = useMemo(()=>new LineMaterial({color: 'orange', linewidth: 5}),[]) |
| 34 | + const dimResolution = 7; |
| 35 | + |
| 36 | + const xLine = useMemo(()=> { |
| 37 | + const geom = new LineSegmentsGeometry().setPositions([xRange[0], 0, 0, xRange[1], 0, 0]); |
| 38 | + return new LineSegments2(geom, lineMat)},[xRange]) |
| 39 | + |
| 40 | + const yLine = useMemo(() =>{ |
| 41 | + const geom = new LineSegmentsGeometry().setPositions([0, yRange[0]*shapeRatio, 0, 0, yRange[1]*shapeRatio, 0]); |
| 42 | + return new LineSegments2(geom, lineMat)},[yRange]) |
| 43 | + |
| 44 | + const zLine = useMemo(()=> { |
| 45 | + const geom = new LineSegmentsGeometry().setPositions([0, 0, zRange[0], 0, 0, zRange[1]]); |
| 46 | + return new LineSegments2(geom, lineMat)},[zRange]) |
| 47 | + |
| 48 | + const tickLine = useMemo(()=> { |
| 49 | + const geom = new LineSegmentsGeometry().setPositions([0, 0, 0, 0, 0, .05]); |
| 50 | + return new LineSegments2(geom, lineMat)},[]) |
| 51 | + |
| 52 | + |
| 53 | + const dimScale = dimResolution/(dimResolution-1) |
| 54 | + const valDelta = 1/(dimResolution-1) |
| 55 | + |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + {/* Horizontal Group */} |
| 60 | + <group position={[0, shapeRatio*yRange[0], 0]}> |
| 61 | + {/* X Group */} |
| 62 | + <group position={[0, 0, flipX ? zRange[0] : zRange[1]]}> |
| 63 | + <primitive key={'xLine'} object={xLine} /> |
| 64 | + {Array(dimResolution).fill(null).map((_val,idx)=>( |
| 65 | + (((xRange[0] + 1)/2) <= (idx*dimScale)/dimResolution && |
| 66 | + ((xRange[1] + 1)/2) >= (idx*dimScale)/dimResolution) |
| 67 | + && |
| 68 | + <group position={[-1 + idx*dimScale/(dimResolution/2), 0, 0]}> |
| 69 | + <primitive key={idx} object={tickLine.clone()} rotation={[0, flipX ? Math.PI : 0, 0]} /> |
| 70 | + <Text |
| 71 | + key={`textX_${idx}`} |
| 72 | + anchorX={idx == 0 ? (flipX ? 'right' : 'left') : idx == dimResolution-1 ? (flipX ? 'left' : 'right') : 'center'} |
| 73 | + anchorY={'top'} |
| 74 | + fontSize={0.05} |
| 75 | + color={'black'} |
| 76 | + material-depthTest={false} |
| 77 | + rotation={[-Math.PI/2, 0, flipX ? Math.PI : 0]} |
| 78 | + position={[0, 0, flipX ? -0.05 :.05]} |
| 79 | + >{parseLoc(dimArrays[2][Math.floor((dimLengths[2]-1)*idx*valDelta)],dimUnits[2])}</Text> |
| 80 | + </group> |
| 81 | + ))} |
| 82 | + <Text |
| 83 | + key={'xTitle'} |
| 84 | + anchorX={'center'} |
| 85 | + anchorY={'top'} |
| 86 | + fontSize={0.1} |
| 87 | + color={'black'} |
| 88 | + material-depthTest={false} |
| 89 | + rotation={[-Math.PI/2, 0, flipX ? Math.PI : 0]} |
| 90 | + position={[(xRange[0]+xRange[1])/2, 0, flipX ? -0.2 :.2]} |
| 91 | + >{dimNames[2]}</Text> |
| 92 | + </group> |
| 93 | + {/* Z Group */} |
| 94 | + <group position={[flipY ? xRange[1]: xRange[0], 0, 0]}> |
| 95 | + <primitive key={'zLine'} object={zLine} /> |
| 96 | + {Array(dimResolution).fill(null).map((_val,idx)=>( |
| 97 | + (((zRange[0] + 1)/2) <= (idx*dimScale)/dimResolution && |
| 98 | + ((zRange[1] + 1)/2) >= (idx*dimScale)/dimResolution ) |
| 99 | + && |
| 100 | + <group position={[0, 0, -1 + idx*dimScale/(dimResolution/2)]}> |
| 101 | + <primitive key={idx} object={tickLine.clone()} rotation={[0, flipY ? Math.PI/2 : -Math.PI/2 , 0]} /> |
| 102 | + <Text |
| 103 | + key={`textY_${idx}`} |
| 104 | + anchorX={idx == 0 ? (flipY ? 'right' : 'left') : idx == dimResolution-1 ? (flipY ? 'left' : 'right') : 'center'} |
| 105 | + anchorY={'top'} |
| 106 | + fontSize={0.04} |
| 107 | + color={'black'} |
| 108 | + material-depthTest={false} |
| 109 | + rotation={[-Math.PI/2, 0, flipY ? Math.PI/2 : -Math.PI/2]} |
| 110 | + position={[flipY ? 0.05 :-0.05, 0, 0]} |
| 111 | + >{parseLoc(dimArrays[0][Math.floor((dimLengths[0]-1)*idx*valDelta)],dimUnits[0])}</Text> |
| 112 | + </group> |
| 113 | + ))} |
| 114 | + <Text |
| 115 | + key={'xTitle'} |
| 116 | + anchorX={'center'} |
| 117 | + anchorY={'top'} |
| 118 | + fontSize={0.1} |
| 119 | + color={'black'} |
| 120 | + material-depthTest={false} |
| 121 | + rotation={[-Math.PI/2, 0, flipY ? Math.PI/2 : -Math.PI/2]} |
| 122 | + position={[flipY ? 0.2 : -0.2, 0, (zRange[0]+zRange[1])/2]} |
| 123 | + >{dimNames[0]}</Text> |
| 124 | + </group> |
| 125 | + </group> |
| 126 | + |
| 127 | + {/* Vertical Group */} |
| 128 | + <group position={[flipY ? xRange[0] : xRange[1], 0, flipX ? zRange[0] : zRange[1]]}> |
| 129 | + <primitive key={'yLine'} object={yLine} /> |
| 130 | + {Array(dimResolution).fill(null).map((_val,idx)=>( |
| 131 | + (((yRange[0] + 1)/2) <= (idx*dimScale)/dimResolution && |
| 132 | + ((yRange[1] + 1)/2) >= (idx*dimScale)/dimResolution) |
| 133 | + && |
| 134 | + <group position={[0, -shape.y/2 + idx*dimScale/(dimResolution/2)*shapeRatio, 0]}> |
| 135 | + <primitive key={idx} object={tickLine.clone()} rotation={[0, flipY ? -Math.PI/2 :Math.PI/2 , 0]} /> |
| 136 | + <Text |
| 137 | + key={`text_${idx}`} |
| 138 | + anchorX={flipY ? flipX ? 'left' : 'right' : flipX ? 'right' : 'left'} |
| 139 | + anchorY={'middle'} |
| 140 | + fontSize={0.05} |
| 141 | + color={'black'} |
| 142 | + material-depthTest={false} |
| 143 | + rotation={[0, flipX ? Math.PI : 0, 0]} |
| 144 | + position={[flipY ? -0.07 : 0.07, 0, 0]} |
| 145 | + >{parseLoc(dimArrays[1][Math.floor((dimLengths[1]-1)*idx*valDelta)],dimUnits[1])}</Text> |
| 146 | + </group> |
| 147 | + ))} |
| 148 | + <Text |
| 149 | + key={'xTitle'} |
| 150 | + anchorX={flipY ? flipX ? 'left' : 'right' : flipX ? 'right' : 'left'} |
| 151 | + anchorY={'middle'} |
| 152 | + fontSize={0.1} |
| 153 | + color={'black'} |
| 154 | + material-depthTest={false} |
| 155 | + rotation={[0, flipX ? Math.PI : 0, 0]} |
| 156 | + position={[flipY ? -0.25 : 0.25, (yRange[0]+yRange[1])/2, 0]} |
| 157 | + >{dimNames[1]}</Text> |
| 158 | + </group> |
| 159 | + </> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | + |
| 164 | +export const AxisLines = () => { |
| 165 | + const [flipX, setFlipX] = useState<boolean>(false) |
| 166 | + const [flipY, setFlipY] = useState<boolean>(false) |
| 167 | + |
| 168 | + useFrame(({camera})=>{ |
| 169 | + const shouldFlipX = Math.abs(camera.rotation.z) > Math.PI / 2; |
| 170 | + if (flipX !== shouldFlipX) { |
| 171 | + setFlipX(shouldFlipX); |
| 172 | + } |
| 173 | + |
| 174 | + const shouldFlipY = |
| 175 | + (camera.rotation.z > 0 && camera.rotation.x < 0) || |
| 176 | + (camera.rotation.z <= 0 && camera.rotation.x > 0); |
| 177 | + if (flipY !== shouldFlipY){ |
| 178 | + setFlipY(shouldFlipY); |
| 179 | + } |
| 180 | + }) |
| 181 | + |
| 182 | + return ( |
| 183 | + <> |
| 184 | + <HorizontalAxis flipX={flipX} flipY={flipY} /> |
| 185 | + </> |
| 186 | + ) |
| 187 | +} |
| 188 | + |
| 189 | + |
0 commit comments