@vitessce/layer-controller 2.0.3-beta.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/deflate-75511377.js +13 -0
  2. package/dist/index-841a1af6.js +148439 -0
  3. package/dist/index.js +9 -3
  4. package/dist/jpeg-6023316d.js +840 -0
  5. package/dist/lerc-82182c45.js +2014 -0
  6. package/dist/lzw-2ab01db2.js +128 -0
  7. package/dist/packbits-2633f360.js +30 -0
  8. package/dist/pako.esm-68f84e2a.js +4022 -0
  9. package/dist/raw-51e455c7.js +12 -0
  10. package/dist/webimage-bbb56d18.js +32 -0
  11. package/dist-tsc/BitmaskChannelController.d.ts +21 -0
  12. package/dist-tsc/BitmaskChannelController.d.ts.map +1 -0
  13. package/{dist → dist-tsc}/BitmaskChannelController.js +3 -4
  14. package/dist-tsc/ChannelOptions.d.ts +13 -0
  15. package/dist-tsc/ChannelOptions.d.ts.map +1 -0
  16. package/{dist → dist-tsc}/ChannelOptions.js +4 -5
  17. package/dist-tsc/ColorPalette.d.ts +5 -0
  18. package/dist-tsc/ColorPalette.d.ts.map +1 -0
  19. package/{dist → dist-tsc}/ColorPalette.js +2 -3
  20. package/dist-tsc/ImageAddButton.d.ts +6 -0
  21. package/dist-tsc/ImageAddButton.d.ts.map +1 -0
  22. package/{dist → dist-tsc}/ImageAddButton.js +2 -3
  23. package/dist-tsc/LayerController.d.ts +10 -0
  24. package/dist-tsc/LayerController.d.ts.map +1 -0
  25. package/{dist → dist-tsc}/LayerController.js +7 -15
  26. package/dist-tsc/LayerControllerSubscriber.d.ts +27 -0
  27. package/dist-tsc/LayerControllerSubscriber.d.ts.map +1 -0
  28. package/{dist → dist-tsc}/LayerControllerSubscriber.js +7 -10
  29. package/dist-tsc/LayerOptions.d.ts +46 -0
  30. package/dist-tsc/LayerOptions.d.ts.map +1 -0
  31. package/{dist → dist-tsc}/LayerOptions.js +4 -8
  32. package/dist-tsc/RasterChannelController.d.ts +35 -0
  33. package/dist-tsc/RasterChannelController.d.ts.map +1 -0
  34. package/{dist → dist-tsc}/RasterChannelController.js +7 -17
  35. package/dist-tsc/RasterChannelController.test.d.ts +2 -0
  36. package/dist-tsc/RasterChannelController.test.d.ts.map +1 -0
  37. package/{dist → dist-tsc}/RasterChannelController.test.js +1 -1
  38. package/dist-tsc/VectorLayerController.d.ts +2 -0
  39. package/dist-tsc/VectorLayerController.d.ts.map +1 -0
  40. package/{dist → dist-tsc}/VectorLayerController.js +2 -6
  41. package/dist-tsc/VolumeOptions.d.ts +16 -0
  42. package/dist-tsc/VolumeOptions.d.ts.map +1 -0
  43. package/{dist → dist-tsc}/VolumeOptions.js +3 -10
  44. package/dist-tsc/constants.d.ts +2 -0
  45. package/dist-tsc/constants.d.ts.map +1 -0
  46. package/dist-tsc/index.d.ts +4 -0
  47. package/dist-tsc/index.d.ts.map +1 -0
  48. package/dist-tsc/index.js +3 -0
  49. package/dist-tsc/shared-channel-controls.d.ts +27 -0
  50. package/dist-tsc/shared-channel-controls.d.ts.map +1 -0
  51. package/{dist → dist-tsc}/shared-channel-controls.js +2 -3
  52. package/dist-tsc/styles.d.ts +76 -0
  53. package/dist-tsc/styles.d.ts.map +1 -0
  54. package/{dist → dist-tsc}/styles.js +1 -6
  55. package/dist-tsc/utils.d.ts +24 -0
  56. package/dist-tsc/utils.d.ts.map +1 -0
  57. package/{dist → dist-tsc}/utils.js +8 -0
  58. package/package.json +20 -11
  59. package/src/BitmaskChannelController.js +62 -0
  60. package/src/ChannelOptions.js +61 -0
  61. package/src/ColorPalette.js +48 -0
  62. package/src/ImageAddButton.js +50 -0
  63. package/src/LayerController.js +596 -0
  64. package/src/LayerControllerSubscriber.js +501 -0
  65. package/src/LayerOptions.js +459 -0
  66. package/src/RasterChannelController.js +245 -0
  67. package/src/RasterChannelController.test.js +23 -0
  68. package/src/VectorLayerController.js +61 -0
  69. package/src/VolumeOptions.js +221 -0
  70. package/src/constants.js +14 -0
  71. package/src/index.js +3 -0
  72. package/src/shared-channel-controls.js +54 -0
  73. package/src/styles.js +116 -0
  74. package/src/utils.js +138 -0
  75. /package/{dist → dist-tsc}/constants.js +0 -0
@@ -0,0 +1,23 @@
1
+ import { toRgbUIString } from './utils.js';
2
+
3
+ const GREY = [220, 220, 220];
4
+
5
+ describe('layer-controller/RasterChannelController.js', () => {
6
+ describe('toRgbUIString()', () => {
7
+ it('Maps color value to itself when colormap off and not white with dark theme', () => {
8
+ expect(toRgbUIString(false, [200, 200, 200], 'dark')).toEqual(`rgb(${[200, 200, 200]})`);
9
+ });
10
+ it('Maps color value to grey when colormap on with dark theme', () => {
11
+ expect(toRgbUIString(true, [200, 200, 200], 'dark')).toEqual(`rgb(${GREY})`);
12
+ });
13
+ it('Maps color value to grey when colormap on with light theme', () => {
14
+ expect(toRgbUIString(true, [200, 200, 200], 'light')).toEqual(`rgb(${GREY})`);
15
+ });
16
+ it('Maps color value to grey when colormap off and white with light theme', () => {
17
+ expect(toRgbUIString(false, [255, 255, 255], 'light')).toEqual(`rgb(${GREY})`);
18
+ });
19
+ it('Maps color value to grey when colormap off and white with light theme', () => {
20
+ expect(toRgbUIString(false, [200, 200, 200], 'light')).toEqual(`rgb(${[200, 200, 200]})`);
21
+ });
22
+ });
23
+ });
@@ -0,0 +1,61 @@
1
+ import React from 'react';
2
+
3
+ import { Grid, Checkbox, Paper, Typography, Slider } from '@material-ui/core';
4
+ import { useControllerSectionStyles } from './styles.js';
5
+
6
+ export default function VectorLayerController(props) {
7
+ const {
8
+ label,
9
+ layer,
10
+ layerType,
11
+ handleLayerChange,
12
+ } = props;
13
+
14
+ const slider = layer.opacity;
15
+ const isOn = layer.visible;
16
+
17
+ function handleSliderChange(v) {
18
+ if (layerType === 'cells') {
19
+ const stroked = v < 0.7;
20
+ handleLayerChange({ ...layer, opacity: v, stroked });
21
+ } else {
22
+ handleLayerChange({ ...layer, opacity: v });
23
+ }
24
+ }
25
+
26
+ function handleCheckBoxChange(v) {
27
+ handleLayerChange({ ...layer, visible: v });
28
+ }
29
+
30
+ const classes = useControllerSectionStyles();
31
+ return (
32
+ <Grid item style={{ marginTop: '10px' }}>
33
+ <Paper className={classes.root}>
34
+ <Typography
35
+ style={{
36
+ padding: '15px 8px 0px 8px',
37
+ marginBottom: '-5px',
38
+ }}
39
+ >
40
+ {label}
41
+ </Typography>
42
+ <Grid container direction="row" justifyContent="space-between">
43
+ <Grid item xs={2}>
44
+ <Checkbox color="primary" checked={isOn} onChange={(e, v) => handleCheckBoxChange(v)} />
45
+ </Grid>
46
+ <Grid item xs={9} style={{ paddingRight: '8px' }}>
47
+ <Slider
48
+ value={slider}
49
+ min={0}
50
+ max={1}
51
+ step={0.001}
52
+ onChange={(e, v) => handleSliderChange(v)}
53
+ style={{ marginTop: '7px' }}
54
+ orientation="horizontal"
55
+ />
56
+ </Grid>
57
+ </Grid>
58
+ </Paper>
59
+ </Grid>
60
+ );
61
+ }
@@ -0,0 +1,221 @@
1
+ import React from 'react';
2
+ import { Matrix4 } from 'math.gl';
3
+ import {
4
+ Grid,
5
+ Typography,
6
+ Button,
7
+ makeStyles,
8
+ createStyles,
9
+ FormControl,
10
+ Select,
11
+ InputLabel,
12
+ Slider,
13
+ } from '@material-ui/core';
14
+ import { viv } from '@vitessce/gl';
15
+ import { abbreviateNumber, getBoundingCube } from './utils.js';
16
+ import { useSelectStyles } from './styles.js';
17
+
18
+ const useSlicerStyles = makeStyles(theme => createStyles({
19
+ enabled: {},
20
+ disabled: {
21
+ color: theme.palette.text.disabled,
22
+ // Because of the .5 opacity of the disabled color in the theme, and the fact
23
+ // that there are multiple overlaid parts to the slider,
24
+ // this needs to be set manually for the desired effect.
25
+ '& .MuiSlider-thumb': {
26
+ color: 'rgb(100, 100, 100, 1.0)',
27
+ },
28
+ '& .MuiSlider-track': {
29
+ color: 'rgb(100, 100, 100, 1.0)',
30
+ },
31
+ },
32
+ }));
33
+
34
+ const Slicer = ({
35
+ xSlice,
36
+ ySlice,
37
+ zSlice,
38
+ handleSlicerSetting,
39
+ loader,
40
+ use3d,
41
+ }) => {
42
+ const [xSliceInit, ySliceInit, zSliceInit] = getBoundingCube(loader.data);
43
+ const sliceValuesAndSetSliceFunctions = [
44
+ [
45
+ xSlice,
46
+ xSliceNew => handleSlicerSetting('x', xSliceNew),
47
+ 'x',
48
+ xSliceInit,
49
+ ],
50
+ [
51
+ ySlice,
52
+ ySliceNew => handleSlicerSetting('y', ySliceNew),
53
+ 'y',
54
+ ySliceInit,
55
+ ],
56
+ [
57
+ zSlice,
58
+ zSliceNew => handleSlicerSetting('z', zSliceNew),
59
+ 'z',
60
+ zSliceInit,
61
+ ],
62
+ ];
63
+ const classes = useSlicerStyles();
64
+ const Slicers = sliceValuesAndSetSliceFunctions.map(
65
+ ([val, setVal, label, [min, max]]) => (
66
+ <Grid
67
+ container
68
+ direction="row"
69
+ justifyContent="flex-start"
70
+ alignItems="center"
71
+ key={label}
72
+ >
73
+ <Grid item xs={1}>
74
+ <Typography
75
+ className={!use3d ? classes.disabled : classes.enabled}
76
+ style={{ marginBottom: 0 }}
77
+ >
78
+ {label}:
79
+ </Typography>
80
+ </Grid>
81
+ <Grid item xs={11}>
82
+ <Slider
83
+ disabled={!use3d}
84
+ className={!use3d ? classes.disabled : classes.enabled}
85
+ value={val}
86
+ onChange={(e, v) => setVal(v)}
87
+ valueLabelDisplay="auto"
88
+ valueLabelFormat={v => abbreviateNumber(v)}
89
+ getAriaLabel={() => `${label} slider`}
90
+ min={min}
91
+ max={max}
92
+ step={0.005}
93
+ orientation="horizontal"
94
+ />
95
+ </Grid>
96
+ </Grid>
97
+ ),
98
+ );
99
+ return (
100
+ <>
101
+ <Typography
102
+ className={!use3d ? classes.disabled : classes.enabled}
103
+ style={{ marginTop: 16, marginBottom: 0 }}
104
+ >
105
+ Clipping Planes:{' '}
106
+ </Typography>{' '}
107
+ {Slicers}
108
+ </>
109
+ );
110
+ };
111
+
112
+ const renderingOptions = Object.values(viv.RENDERING_MODES);
113
+
114
+ function RenderingModeSelect({
115
+ handleRenderingModeChange,
116
+ renderingMode,
117
+ use3d,
118
+ }) {
119
+ const classes = useSelectStyles();
120
+ // Empty option allows for displaying the title of the dropdown fully in the UI.
121
+ const options = !use3d ? [...renderingOptions, ''] : renderingOptions;
122
+ return (
123
+ <FormControl fullWidth>
124
+ <InputLabel htmlFor="rendering-mode-select">Rendering Mode</InputLabel>
125
+ <Select
126
+ native
127
+ onChange={e => handleRenderingModeChange(e.target.value)}
128
+ value={use3d ? renderingMode : ''}
129
+ inputProps={{
130
+ name: 'rendering-mode',
131
+ id: 'rendering-mode-select',
132
+ }}
133
+ disabled={!use3d}
134
+ classes={{ root: classes.selectRoot }}
135
+ >
136
+ {options.map(name => (
137
+ <option key={name} value={name}>
138
+ {name}
139
+ </option>
140
+ ))}
141
+ </Select>
142
+ </FormControl>
143
+ );
144
+ }
145
+
146
+ const ReCenterButton = ({
147
+ setViewState,
148
+ use3d,
149
+ spatialHeight,
150
+ spatialWidth,
151
+ loader,
152
+ modelMatrix,
153
+ }) => (
154
+ <Grid item xs="auto" key="recenter">
155
+ <Button
156
+ onClick={() => {
157
+ const defaultViewState = viv.getDefaultInitialViewState(
158
+ loader.data,
159
+ { height: spatialHeight, width: spatialWidth },
160
+ 1.5,
161
+ use3d,
162
+ new Matrix4(modelMatrix),
163
+ );
164
+ setViewState({
165
+ ...defaultViewState,
166
+ rotationX: 0,
167
+ rotationOrbit: 0,
168
+ });
169
+ }
170
+ }
171
+ disabled={!use3d}
172
+ style={{
173
+ padding: 0,
174
+ marginBottom: 6,
175
+ }}
176
+ >
177
+ Re-Center
178
+ </Button>
179
+ </Grid>
180
+ );
181
+
182
+ const VolumeOptions = ({
183
+ handleSlicerSetting,
184
+ handleRenderingModeChange,
185
+ renderingMode,
186
+ xSlice,
187
+ ySlice,
188
+ zSlice,
189
+ use3d,
190
+ loader,
191
+ setViewState,
192
+ spatialHeight,
193
+ spatialWidth,
194
+ modelMatrix,
195
+ }) => (
196
+ <>
197
+ <RenderingModeSelect
198
+ handleRenderingModeChange={handleRenderingModeChange}
199
+ renderingMode={renderingMode}
200
+ use3d={use3d}
201
+ />
202
+ <Slicer
203
+ xSlice={xSlice}
204
+ ySlice={ySlice}
205
+ zSlice={zSlice}
206
+ handleSlicerSetting={handleSlicerSetting}
207
+ use3d={use3d}
208
+ loader={loader}
209
+ />
210
+ <ReCenterButton
211
+ setViewState={setViewState}
212
+ use3d={use3d}
213
+ spatialHeight={spatialHeight}
214
+ spatialWidth={spatialWidth}
215
+ loader={loader}
216
+ modelMatrix={modelMatrix}
217
+ />
218
+ </>
219
+ );
220
+
221
+ export default VolumeOptions;
@@ -0,0 +1,14 @@
1
+ import { viv } from '@vitessce/gl';
2
+
3
+ function getDomains() {
4
+ const domains = {};
5
+ const needMin = ['Int8', 'Int16', 'Int32'];
6
+ Object.keys(viv.DTYPE_VALUES).forEach((dtype) => {
7
+ const { max } = viv.DTYPE_VALUES[dtype];
8
+ const min = needMin.includes(dtype) ? -(max + 1) : 0;
9
+ domains[dtype] = [min, max];
10
+ });
11
+ return domains;
12
+ }
13
+
14
+ export const DOMAINS = getDomains();
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { LayerControllerSubscriber } from './LayerControllerSubscriber.js';
2
+ export { default as LayerController } from './LayerController.js';
3
+ export { default as VectorLayerController } from './VectorLayerController.js';
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+
3
+ import { Checkbox, Select } from '@material-ui/core';
4
+ import { useSelectStyles } from './styles.js';
5
+
6
+ /**
7
+ * Dropdown for selecting a channel.
8
+ * @prop {function} handleChange Callback for each new selection.
9
+ * @prop {array} channelOptions List of available selections, like ['DAPI', 'FITC', ...].
10
+ * @prop {boolean} disabled Whether or not the component is disabled.
11
+ * @prop {number} selectionIndex Current numeric index of a selection.
12
+ */
13
+ export function ChannelSelectionDropdown({
14
+ handleChange,
15
+ disabled,
16
+ channelOptions,
17
+ selectionIndex,
18
+ }) {
19
+ const classes = useSelectStyles();
20
+ return (
21
+ <Select
22
+ classes={{ root: classes.selectRoot }}
23
+ native
24
+ value={selectionIndex}
25
+ onChange={e => handleChange(Number(e.target.value))}
26
+ >
27
+ {channelOptions.map((opt, i) => (
28
+ <option disabled={disabled} key={opt} value={i}>
29
+ {opt}
30
+ </option>
31
+ ))}
32
+ </Select>
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Checkbox for toggling on/off of a channel.
38
+ * @prop {string} color Current color for this channel.
39
+ * @prop {boolean} checked Whether or not this channel is "on".
40
+ * @prop {boolean} disabled Whether or not the component is disabled.
41
+ * @prop {function} toggle Callback for toggling on/off.
42
+ */
43
+ export function ChannelVisibilityCheckbox({
44
+ color, checked, toggle, disabled,
45
+ }) {
46
+ return (
47
+ <Checkbox
48
+ onChange={toggle}
49
+ checked={checked}
50
+ disabled={disabled}
51
+ style={{ color, '&$checked': { color } }}
52
+ />
53
+ );
54
+ }
package/src/styles.js ADDED
@@ -0,0 +1,116 @@
1
+ import {
2
+ makeStyles,
3
+ withStyles,
4
+ AccordionSummary,
5
+ AccordionDetails,
6
+ InputLabel,
7
+ Slider,
8
+ Grid,
9
+ } from '@material-ui/core';
10
+
11
+ const useSpanStyles = makeStyles(() => ({
12
+ span: {
13
+ width: '70px',
14
+ textAlign: 'center',
15
+ paddingLeft: '2px',
16
+ paddingRight: '2px',
17
+ },
18
+ }));
19
+
20
+ export function MuiSpan(props) {
21
+ const { children } = props;
22
+ const classes = useSpanStyles();
23
+ return <span className={classes.span}>{children}</span>;
24
+ }
25
+
26
+ export const useSelectStyles = makeStyles(() => ({
27
+ selectRoot: {
28
+ padding: 0,
29
+ height: 'auto',
30
+ margin: '4px 0',
31
+ },
32
+ }));
33
+
34
+ export const useOptionStyles = withStyles(theme => ({
35
+ paper: {
36
+ backgroundColor: theme.palette.background.paper,
37
+ },
38
+ span: {
39
+ width: '70px',
40
+ textAlign: 'center',
41
+ paddingLeft: '2px',
42
+ paddingRight: '2px',
43
+ },
44
+ colors: {
45
+ '&:hover': {
46
+ backgroundColor: 'transparent',
47
+ },
48
+ paddingLeft: '2px',
49
+ paddingRight: '2px',
50
+ },
51
+ popper: {
52
+ zIndex: 4,
53
+ },
54
+ }));
55
+
56
+ const sharedControllerStyles = {
57
+ width: '100%',
58
+ flexDirection: 'column',
59
+ };
60
+
61
+ export const useControllerSectionStyles = makeStyles(() => ({
62
+ root: {
63
+ ...sharedControllerStyles,
64
+ padding: '0px 8px',
65
+ },
66
+ }));
67
+
68
+ export const StyledAccordionDetails = withStyles(() => ({
69
+ root: {
70
+ ...sharedControllerStyles,
71
+ padding: '8px 8px 24px 8px',
72
+ },
73
+ }))(AccordionDetails);
74
+
75
+ export const StyledAccordionSummary = withStyles(theme => ({
76
+ root: {
77
+ padding: '0px 8px',
78
+ },
79
+ content: {
80
+ margin: '4px 0px',
81
+ minWidth: '0px',
82
+ },
83
+ expanded: {
84
+ marginBottom: theme.spacing(-3),
85
+ top: theme.spacing(-1),
86
+ },
87
+ expandIcon: {
88
+ '&$expanded': {
89
+ top: theme.spacing(-1.3),
90
+ },
91
+ },
92
+ }))(AccordionSummary);
93
+
94
+ export const StyledInputLabel = withStyles(() => ({
95
+ root: {
96
+ fontSize: '14px',
97
+ },
98
+ }))(InputLabel);
99
+
100
+ export const OverflowEllipsisGrid = withStyles(() => ({
101
+ item: {
102
+ width: '100%',
103
+ overflow: 'hidden',
104
+ whiteSpace: 'nowrap',
105
+ textOverflow: 'ellipsis',
106
+ },
107
+ }))(Grid);
108
+
109
+ export const StyledSelectionSlider = withStyles(() => ({
110
+ root: {
111
+ marginTop: '7px',
112
+ },
113
+ markActive: {
114
+ backgroundColor: 'rgba(128, 128, 128, 0.7)',
115
+ },
116
+ }))(Slider);
package/src/utils.js ADDED
@@ -0,0 +1,138 @@
1
+ import { viv } from '@vitessce/gl';
2
+ import { Matrix4 } from 'math.gl';
3
+
4
+ // Returns an rgb string for display, and changes the color (arr)
5
+ // to use a grey for light theme + white color or if the colormap is on.
6
+ export const toRgbUIString = (on, arr, theme) => {
7
+ const color = on || (theme === 'light' && arr.every(i => i === 255))
8
+ ? [220, 220, 220]
9
+ : arr;
10
+ return `rgb(${color})`;
11
+ };
12
+
13
+ async function getSingleSelectionStats2D({ loader, selection }) {
14
+ const data = Array.isArray(loader) ? loader[loader.length - 1] : loader;
15
+ const raster = await data.getRaster({ selection });
16
+ const selectionStats = viv.getChannelStats(raster.data);
17
+ const { domain, contrastLimits: slider } = selectionStats;
18
+ return { domain, slider };
19
+ }
20
+
21
+ async function getSingleSelectionStats3D({ loader, selection }) {
22
+ const lowResSource = loader[loader.length - 1];
23
+ const { shape, labels } = lowResSource;
24
+ // eslint-disable-next-line no-bitwise
25
+ const sizeZ = shape[labels.indexOf('z')] >> (loader.length - 1);
26
+ const raster0 = await lowResSource.getRaster({
27
+ selection: { ...selection, z: 0 },
28
+ });
29
+ const rasterMid = await lowResSource.getRaster({
30
+ selection: { ...selection, z: Math.floor(sizeZ / 2) },
31
+ });
32
+ const rasterTop = await lowResSource.getRaster({
33
+ selection: { ...selection, z: Math.max(0, sizeZ - 1) },
34
+ });
35
+ const stats0 = viv.getChannelStats(raster0.data);
36
+ const statsMid = viv.getChannelStats(rasterMid.data);
37
+ const statsTop = viv.getChannelStats(rasterTop.data);
38
+ return {
39
+ domain: [
40
+ Math.min(stats0.domain[0], statsMid.domain[0], statsTop.domain[0]),
41
+ Math.max(stats0.domain[1], statsMid.domain[1], statsTop.domain[1]),
42
+ ],
43
+ slider: [
44
+ Math.min(
45
+ stats0.contrastLimits[0],
46
+ statsMid.contrastLimits[0],
47
+ statsTop.contrastLimits[0],
48
+ ),
49
+ Math.max(
50
+ stats0.contrastLimits[1],
51
+ statsMid.contrastLimits[1],
52
+ statsTop.contrastLimits[1],
53
+ ),
54
+ ],
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Get bounding cube for a given loader i.e [[0, width], [0, height], [0, depth]]
60
+ * @param {Object} loader PixelSource|PixelSource[]
61
+ * @param {[]} selection Selection for stats.
62
+ * @param {boolean} use3d Whether or not to get 3d stats.
63
+ * @returns {Object} { domains, sliders }
64
+ */
65
+ export const getSingleSelectionStats = async ({ loader, selection, use3d }) => {
66
+ const getStats = use3d
67
+ ? getSingleSelectionStats3D
68
+ : getSingleSelectionStats2D;
69
+ return getStats({ loader, selection });
70
+ };
71
+
72
+ export const getMultiSelectionStats = async ({ loader, selections, use3d }) => {
73
+ const stats = await Promise.all(
74
+ selections.map(selection => getSingleSelectionStats({ loader, selection, use3d })),
75
+ );
76
+ const domains = stats.map(stat => stat.domain);
77
+ const sliders = stats.map(stat => stat.slider);
78
+ return { domains, sliders };
79
+ };
80
+
81
+ /**
82
+ * Get physical size scaling Matrix4
83
+ * @param {Object} loader PixelSource
84
+ * @returns {Object} matrix
85
+ */
86
+ export function getPhysicalSizeScalingMatrix(loader) {
87
+ const { x, y, z } = loader?.meta?.physicalSizes ?? {};
88
+ if (x?.size && y?.size && z?.size) {
89
+ const min = Math.min(z.size, x.size, y.size);
90
+ const ratio = [x.size / min, y.size / min, z.size / min];
91
+ return new Matrix4().scale(ratio);
92
+ }
93
+ return new Matrix4().identity();
94
+ }
95
+
96
+ /**
97
+ * Get bounding cube for a given loader
98
+ * @param {Object} loader PixelSource|PixelSource[]
99
+ * @returns {Array} [0, width], [0, height], [0, depth]]
100
+ */
101
+ export function getBoundingCube(loader) {
102
+ const source = Array.isArray(loader) ? loader[0] : loader;
103
+ const { shape, labels } = source;
104
+ const physicalSizeScalingMatrix = getPhysicalSizeScalingMatrix(source);
105
+ const xSlice = [0, physicalSizeScalingMatrix[0] * shape[labels.indexOf('x')]];
106
+ const ySlice = [0, physicalSizeScalingMatrix[5] * shape[labels.indexOf('y')]];
107
+ const zSlice = [
108
+ 0,
109
+ physicalSizeScalingMatrix[10] * shape[labels.indexOf('z')],
110
+ ];
111
+ return [xSlice, ySlice, zSlice];
112
+ }
113
+
114
+ export function abbreviateNumber(value) {
115
+ // Return an abbreviated representation of value, in 5 characters or less.
116
+
117
+ const maxLength = 5;
118
+ let maxNaiveDigits = maxLength;
119
+
120
+ /* eslint-disable no-plusplus */
121
+ if (!Number.isInteger(value)) {
122
+ --maxNaiveDigits;
123
+ } // Wasted on "."
124
+ if (value < 1) {
125
+ --maxNaiveDigits;
126
+ } // Wasted on "0."
127
+ /* eslint-disable no-plusplus */
128
+
129
+ const naive = Intl.NumberFormat('en-US', {
130
+ maximumSignificantDigits: maxNaiveDigits,
131
+ useGrouping: false,
132
+ }).format(value);
133
+ if (naive.length <= maxLength) return naive;
134
+
135
+ // "e+9" consumes 3 characters, so if we even had two significant digits,
136
+ // it would take take us to six characters, including the decimal point.
137
+ return value.toExponential(0);
138
+ }
File without changes