-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModulateScalar.tsx
162 lines (149 loc) · 4.73 KB
/
ModulateScalar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { NiivueCanvas, NVROptions, NVRVolume } from "../../src";
import { useImmer } from "use-immer";
import { Niivue, SLICE_TYPE } from "@niivue/niivue";
import React, { useState } from "react";
import styles from "./upstream.module.css";
const FIXED_OPTIONS: NVROptions = {
isColorbar: true,
backColor: [0.2, 0.2, 0.3, 1],
isNearestInterpolation: true,
sliceType: SLICE_TYPE.MULTIPLANAR,
};
const ModulateScalar = () => {
const [crosshairLocation, setCrosshairLocation] = useState("");
const [volumes, setVolumes] = useImmer<{ [key: string]: NVRVolume }>({
func: {
url: "/images/mean_func.nii.gz",
opacity: 1,
colormap: "gray",
},
cope: {
url: "/images/cope1.nii.gz",
colormap: "winter",
opacity: 1,
cal_min: 0.0,
cal_max: 100,
modulationImageUrl: "/images/tstat1.nii.gz",
modulateAlpha: 1,
},
tstat: {
url: "/images/tstat1.nii.gz",
opacity: 0,
colormap: "warm",
cal_min: 0,
cal_max: 4.5,
},
});
const modeNames = Object.keys(volumes).concat(["modulate"]);
const getMode = () => {
if (volumes.cope?.modulationImageUrl) {
return "modulate";
}
const opaqueVolume = Object.entries(volumes)
.filter(([key, _value]) => key !== "func") // always opacity=1
.find(([_key, value]) => value.opacity > 0);
return opaqueVolume === undefined ? "func" : opaqueVolume[0];
};
/**
* Equivalent to
* https://github.com/niivue/niivue/blob/41b134123870fb0b69540a2d8155e75ec8e06339/demos/features/modulateScalar.html#L122-L138
*/
const onModeSelected = (e: React.SyntheticEvent) => setMode(e.target.value);
const setMode = (selectedMode: string) => {
setVolumes((draft) => {
draft.func.opacity = 1.0; // background image
draft.tstat.opacity = 0.0; // hide tstat
draft.cope.opacity = 0.0; // hide cope
if (selectedMode === "modulate") {
draft.cope.opacity = 1.0; // show cope
draft.tstat.opacity = 0.0; // hide tstat
draft.cope.modulationImageUrl = volumes.tstat.url;
} else {
// https://github.com/niivue/niivue/blob/41b134123870fb0b69540a2d8155e75ec8e06339/demos/features/modulateScalar.html#L127
draft[selectedMode].opacity = 1.0;
// https://github.com/niivue/niivue/blob/41b134123870fb0b69540a2d8155e75ec8e06339/demos/features/modulateScalar.html#L137
draft.cope.modulationImageUrl = null;
}
});
};
/**
* Equivalent to
* https://github.com/niivue/niivue/blob/41b134123870fb0b69540a2d8155e75ec8e06339/demos/features/modulateScalar.html#L131C15-L131C28
*/
const onAlphaToggled = (_e: React.SyntheticEvent) =>
setModulateAlpha(volumes.cope.modulateAlpha === 0 ? 1 : 0);
const setModulateAlpha = (alpha: number) => {
setVolumes((draft) => {
draft.cope.modulateAlpha = alpha;
});
};
const configNiivue = (nv: Niivue) => {
// @ts-ignore
nv.onLocationChange = setCrosshairLocation;
};
return (
<div className={styles.root}>
<header>
<label htmlFor="mode">Display:</label>
<select
name="mode"
id="mode"
value={getMode()}
onChange={onModeSelected}
>
{modeNames.map((mode) => (
<option value={mode} key={mode}>
{mode}
</option>
))}
</select>
{/*<label htmlFor="slideT"> tMax</label>*/}
{/*<input*/}
{/* type="range"*/}
{/* min="1"*/}
{/* max="50"*/}
{/* value="45"*/}
{/* className="slider"*/}
{/* id="slideT"*/}
{/*/>*/}
{/*<label htmlFor="slideC"> cMax</label>*/}
{/*<input*/}
{/* type="range"*/}
{/* min="1"*/}
{/* max="200"*/}
{/* value="100"*/}
{/* className="slider"*/}
{/* id="slideC"*/}
{/*/>*/}
{/*<label htmlFor="slide2"> Outline</label>*/}
{/*<input*/}
{/* type="range"*/}
{/* min="0"*/}
{/* max="4"*/}
{/* value="1"*/}
{/* className="slider"*/}
{/* id="slide2"*/}
{/*/>*/}
{/*<label htmlFor="check">ClipDark</label>*/}
{/*<input type="checkbox" id="check" unchecked />*/}
<label>
ModulateAlpha
<input
type="checkbox"
onChange={onAlphaToggled}
checked={volumes.cope.modulateAlpha === 1}
/>
</label>
</header>
<main>
<NiivueCanvas
volumes={React.useMemo(() => Object.values(volumes), [volumes])}
options={FIXED_OPTIONS}
onStart={configNiivue}
/>
</main>
<footer>{crosshairLocation.string}</footer>
</div>
);
};
export default ModulateScalar;