import { useMemo } from 'react' ; import { Vector3 } from 'three' ; import { useAxisSystemContext } from '../shared/AxisSystemContext' ; const CAMERA_FAR = 1000 ; export function useCanvasPoints ( abscissas , ordinates , errors ? ) { const { abscissaScale , ordinateScale } = useAxisSystemContext ( ) ; return useMemo ( ( ) => { const dataPoints = [ ] ; const errorBarSegments = [ ] ; const errorCapPoints = [ ] ; ordinates . forEach ( ( val , index ) => { const x = abscissaScale ( abscissas [ index ] ) ; const y = ordinateScale ( val ) ; const hasFiniteCoords = Number . isFinite ( x ) && Number . isFinite ( y ) ; const dataVector = hasFiniteCoords ? new Vector3 ( x , y , 0 ) : new Vector3 ( 0 , 0 , CAMERA_FAR ) ; dataPoints . push ( dataVector ) ; const error = errors ?. [ index ] ; if ( ! error || ! hasFiniteCoords ) { return ; } const yErrBottom = ordinateScale ( val - error ) ; const yErrTop = ordinateScale ( val + error ) ; if ( Number . isFinite ( yErrBottom ) ) { const errBottomVector = new Vector3 ( x , yErrBottom , 0 ) ; errorBarSegments . push ( errBottomVector , dataVector ) ; errorCapPoints . push ( errBottomVector ) ; } if ( Number . isFinite ( yErrTop ) ) { const errTopVector = new Vector3 ( x , yErrTop , 0 ) ; errorBarSegments . push ( dataVector , errTopVector ) ; errorCapPoints . push ( errTopVector ) ; } } ) ; return { data : dataPoints , bars : errorBarSegments , caps : errorCapPoints } ; } , [ abscissaScale , abscissas , errors , ordinateScale , ordinates ] ) ; }