-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1390 from silx-kit/buffer-geo
Refactor and optimise creation of `ScatterPoints` geometry
- Loading branch information
Showing
4 changed files
with
85 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,6 @@ | ||
import type { Domain, NumArray, ScaleType } from '@h5web/shared'; | ||
import { rgb } from 'd3-color'; | ||
import { useMemo } from 'react'; | ||
import { createMemo } from '@h5web/shared'; | ||
|
||
import type { ColorMap } from '../heatmap/models'; | ||
import { getInterpolator } from '../heatmap/utils'; | ||
import { useVisCanvasContext } from '../shared/VisCanvasProvider'; | ||
import { createAxisScale } from '../utils'; | ||
import { getValueToColor, getIndexToPosition } from './utils'; | ||
|
||
const CAMERA_FAR = 1000; // R3F's default | ||
|
||
export function useBufferAttributes( | ||
abscissas: NumArray, | ||
ordinates: NumArray, | ||
data: NumArray, | ||
dataToColorScale: (val: number) => [number, number, number] | ||
) { | ||
const { abscissaScale, ordinateScale } = useVisCanvasContext(); | ||
|
||
return useMemo(() => { | ||
const position = new Float32Array(3 * data.length); | ||
const color = new Uint8Array(3 * data.length); | ||
|
||
data.forEach((val, index) => { | ||
color.set(dataToColorScale(val), 3 * index); | ||
|
||
const x = abscissaScale(abscissas[index]); | ||
const y = ordinateScale(ordinates[index]); | ||
|
||
const hasFiniteCoords = Number.isFinite(x) && Number.isFinite(y); | ||
|
||
/* Render points with NaN/Infinity coordinates (i.e. values <= 0 in log) | ||
* at origin to avoid Three warning, and outside of camera's field of view | ||
* to hide them and any segments connecting them. */ | ||
position.set([x, y, hasFiniteCoords ? 0 : CAMERA_FAR], 3 * index); | ||
}); | ||
|
||
return { position, color }; | ||
}, [ | ||
abscissaScale, | ||
abscissas, | ||
dataToColorScale, | ||
data, | ||
ordinateScale, | ||
ordinates, | ||
]); | ||
} | ||
|
||
export function useDataToColorScale( | ||
scaleType: ScaleType, | ||
domain: Domain, | ||
colorMap: ColorMap, | ||
invertColorMap: boolean | ||
): (v: number) => [number, number, number] { | ||
return useMemo(() => { | ||
const numScale = createAxisScale(scaleType, { | ||
domain, | ||
range: [0, 1], | ||
}); | ||
const interpolator = getInterpolator(colorMap, invertColorMap); | ||
return (value: number) => { | ||
const color = rgb(interpolator(numScale(value))); | ||
return [color.r, color.g, color.b]; | ||
}; | ||
}, [colorMap, domain, invertColorMap, scaleType]); | ||
} | ||
export const useIndexToPosition = createMemo(getIndexToPosition); | ||
export const useValueToColor = createMemo(getValueToColor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Domain, NumArray, ScaleType } from '@h5web/shared'; | ||
import type { RGBColor } from 'd3-color'; | ||
import { rgb } from 'd3-color'; | ||
|
||
import type { ColorMap } from '../heatmap/models'; | ||
import { getInterpolator } from '../heatmap/utils'; | ||
import type { AxisScale } from '../models'; | ||
import { createAxisScale } from '../utils'; | ||
|
||
const CAMERA_FAR = 1000; // R3F's default | ||
|
||
export function getIndexToPosition( | ||
abscissas: NumArray, | ||
abscissaScale: AxisScale, | ||
ordinates: NumArray, | ||
ordinateScale: AxisScale | ||
): (index: number) => { x: number; y: number; z: number } { | ||
return (index: number) => { | ||
const x = abscissaScale(abscissas[index]); | ||
const y = ordinateScale(ordinates[index]); | ||
|
||
/* Render points with NaN/Infinity coordinates (i.e. values <= 0 in log) | ||
* at origin to avoid Three warning, and outside of camera's field of view | ||
* to hide them and any segments connecting them. */ | ||
const hasFiniteCoords = Number.isFinite(x) && Number.isFinite(y); | ||
return hasFiniteCoords ? { x, y, z: 0 } : { x: 0, y: 0, z: CAMERA_FAR }; | ||
}; | ||
} | ||
|
||
export function getValueToColor( | ||
scaleType: ScaleType, | ||
domain: Domain, | ||
colorMap: ColorMap, | ||
invertColorMap: boolean | ||
): (v: number) => RGBColor { | ||
const interpolator = getInterpolator(colorMap, invertColorMap); | ||
const numScale = createAxisScale(scaleType, { domain, range: [0, 1] }); | ||
return (value: number) => rgb(interpolator(numScale(value))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters