volume.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Adam Midlik <midlik@gmail.com>
  6. */
  7. import { PluginUIComponent } from '../base';
  8. import { StateTransformParameters } from '../state/common';
  9. import { VolumeStreaming } from '../../mol-plugin/behavior/dynamic/volume-streaming/behavior';
  10. import { ExpandableControlRow, IconButton } from '../controls/common';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { ParameterControls, ParamOnChange } from '../controls/parameters';
  13. import { Slider } from '../controls/slider';
  14. import { Volume, Grid } from '../../mol-model/volume';
  15. import { Vec3 } from '../../mol-math/linear-algebra';
  16. import { ColorNames } from '../../mol-util/color/names';
  17. import { toPrecision } from '../../mol-util/number';
  18. import { StateSelection, StateObjectCell } from '../../mol-state';
  19. import { setSubtreeVisibility } from '../../mol-plugin/behavior/static/state';
  20. import { VisibilityOutlinedSvg, VisibilityOffOutlinedSvg } from '../controls/icons';
  21. const ChannelParams = {
  22. color: PD.Color(ColorNames.black, { description: 'Display color of the volume.' }),
  23. wireframe: PD.Boolean(false, { description: 'Control display of the volume as a wireframe.' }),
  24. opacity: PD.Numeric(0.3, { min: 0, max: 1, step: 0.01 }, { description: 'Opacity of the volume.' })
  25. };
  26. type ChannelParams = PD.Values<typeof ChannelParams>
  27. const Bounds = new Map<VolumeStreaming.ChannelType, [number, number]>([
  28. ['em', [-5, 5]],
  29. ['2fo-fc', [0, 3]],
  30. ['fo-fc(+ve)', [1, 5]],
  31. ['fo-fc(-ve)', [-5, -1]],
  32. ]);
  33. class Channel extends PluginUIComponent<{
  34. label: string,
  35. name: VolumeStreaming.ChannelType,
  36. channels: { [k: string]: VolumeStreaming.ChannelParams },
  37. isRelative: boolean,
  38. params: StateTransformParameters.Props,
  39. stats: Grid['stats'],
  40. changeIso: (name: string, value: number, isRelative: boolean) => void,
  41. changeParams: (name: string, param: string, value: any) => void,
  42. bCell: StateObjectCell,
  43. isDisabled?: boolean,
  44. isUnbounded?: boolean
  45. }> {
  46. private ref = StateSelection.findTagInSubtree(this.plugin.state.data.tree, this.props.bCell!.transform.ref, this.props.name);
  47. componentDidUpdate() {
  48. this.ref = StateSelection.findTagInSubtree(this.plugin.state.data.tree, this.props.bCell!.transform.ref, this.props.name);
  49. }
  50. componentDidMount() {
  51. this.subscribe(this.plugin.state.data.events.cell.stateUpdated, e => {
  52. if (this.ref === e.ref) this.forceUpdate();
  53. });
  54. }
  55. getVisible = () => {
  56. const state = this.plugin.state.data;
  57. const ref = this.ref;
  58. if (!ref) return false;
  59. return !state.cells.get(ref)!.state.isHidden;
  60. };
  61. toggleVisible = () => {
  62. const state = this.plugin.state.data;
  63. const ref = this.ref;
  64. if (!ref) return;
  65. setSubtreeVisibility(state, ref, !state.cells.get(ref)!.state.isHidden);
  66. };
  67. render() {
  68. const props = this.props;
  69. const { isRelative, stats } = props;
  70. const channel = props.channels[props.name]!;
  71. const { min, max, mean, sigma } = stats;
  72. const value = Math.round(100 * (channel.isoValue.kind === 'relative' ? channel.isoValue.relativeValue : channel.isoValue.absoluteValue)) / 100;
  73. let relMin = (min - mean) / sigma;
  74. let relMax = (max - mean) / sigma;
  75. if (!this.props.isUnbounded) {
  76. const bounds = Bounds.get(this.props.name)!;
  77. if (this.props.name === 'em') {
  78. relMin = Math.max(bounds[0], relMin);
  79. relMax = Math.min(bounds[1], relMax);
  80. } else {
  81. relMin = bounds[0];
  82. relMax = bounds[1];
  83. }
  84. }
  85. const vMin = mean + sigma * relMin, vMax = mean + sigma * relMax;
  86. const step = toPrecision(isRelative ? Math.round(((vMax - vMin) / sigma)) / 100 : sigma / 100, 2);
  87. const ctrlMin = isRelative ? relMin : vMin;
  88. const ctrlMax = isRelative ? relMax : vMax;
  89. return <ExpandableControlRow
  90. label={props.label + (props.isRelative ? ' \u03C3' : '')}
  91. colorStripe={channel.color}
  92. pivot={<div className='msp-volume-channel-inline-controls'>
  93. <Slider value={value} min={ctrlMin} max={ctrlMax} step={step}
  94. onChange={v => props.changeIso(props.name, v, isRelative)} onChangeImmediate={v => props.changeIso(props.name, v, isRelative)} disabled={props.params.isDisabled} onEnter={props.params.events.onEnter} />
  95. <IconButton svg={this.getVisible() ? VisibilityOutlinedSvg : VisibilityOffOutlinedSvg} onClick={this.toggleVisible} toggleState={false} disabled={props.params.isDisabled} />
  96. </div>}
  97. controls={<ParameterControls onChange={({ name, value }) => props.changeParams(props.name, name, value)} params={ChannelParams} values={channel} onEnter={props.params.events.onEnter} isDisabled={props.params.isDisabled} />}
  98. />;
  99. }
  100. }
  101. export class VolumeStreamingCustomControls extends PluginUIComponent<StateTransformParameters.Props> {
  102. private areInitial(params: any) {
  103. return PD.areEqual(this.props.info.params, params, this.props.info.initialValues);
  104. }
  105. private newParams(params: VolumeStreaming.Params) {
  106. this.props.events.onChange(params, this.areInitial(params));
  107. }
  108. changeIso = (name: string, value: number, isRelative: boolean) => {
  109. const old = this.props.params as VolumeStreaming.Params;
  110. this.newParams({
  111. ...old,
  112. entry: {
  113. name: old.entry.name,
  114. params: {
  115. ...old.entry.params,
  116. channels: {
  117. ...old.entry.params.channels,
  118. [name]: {
  119. ...(old.entry.params.channels as any)[name],
  120. isoValue: isRelative ? Volume.IsoValue.relative(value) : Volume.IsoValue.absolute(value)
  121. }
  122. }
  123. }
  124. }
  125. });
  126. };
  127. changeParams = (name: string, param: string, value: any) => {
  128. const old = this.props.params;
  129. this.newParams({
  130. ...old,
  131. entry: {
  132. name: old.entry.name,
  133. params: {
  134. ...old.entry.params,
  135. channels: {
  136. ...old.entry.params.channels,
  137. [name]: {
  138. ...(old.entry.params.channels as any)[name],
  139. [param]: value
  140. }
  141. }
  142. }
  143. }
  144. });
  145. };
  146. convert(channel: any, stats: Grid['stats'], isRelative: boolean) {
  147. return {
  148. ...channel, isoValue: isRelative
  149. ? Volume.IsoValue.toRelative(channel.isoValue, stats)
  150. : Volume.IsoValue.toAbsolute(channel.isoValue, stats)
  151. };
  152. }
  153. changeOption: ParamOnChange = ({ name, value }) => {
  154. const old = this.props.params as VolumeStreaming.Params;
  155. if (name === 'entry') {
  156. this.newParams({
  157. ...old,
  158. entry: {
  159. name: value,
  160. params: old.entry.params,
  161. }
  162. });
  163. } else {
  164. const b = (this.props.b as VolumeStreaming).data;
  165. const isEM = b.info.kind === 'em';
  166. const isRelative = value.params.isRelative;
  167. const sampling = b.info.header.sampling[0];
  168. const oldChannels = old.entry.params.channels as any;
  169. const oldView = old.entry.params.view.name === value.name
  170. ? old.entry.params.view.params
  171. : (((this.props.info.params as VolumeStreaming.ParamDefinition)
  172. .entry.map(old.entry.name) as PD.Group<VolumeStreaming.EntryParamDefinition>)
  173. .params as VolumeStreaming.EntryParamDefinition)
  174. .view.map(value.name).defaultValue;
  175. const viewParams = { ...oldView };
  176. if (value.name === 'selection-box') {
  177. viewParams.radius = value.params.radius;
  178. } else if (value.name === 'camera-target') {
  179. viewParams.radius = value.params.radius;
  180. viewParams.dynamicDetailLevel = value.params.dynamicDetailLevel;
  181. } else if (value.name === 'box') {
  182. viewParams.bottomLeft = value.params.bottomLeft;
  183. viewParams.topRight = value.params.topRight;
  184. } else if (value.name === 'auto') {
  185. viewParams.radius = value.params.radius;
  186. viewParams.selectionDetailLevel = value.params.selectionDetailLevel;
  187. }
  188. viewParams.isUnbounded = !!value.params.isUnbounded;
  189. this.newParams({
  190. ...old,
  191. entry: {
  192. name: old.entry.name,
  193. params: {
  194. ...old.entry.params,
  195. view: {
  196. name: value.name,
  197. params: viewParams
  198. },
  199. detailLevel: value.params.detailLevel,
  200. channels: isEM
  201. ? { em: this.convert(oldChannels.em, sampling.valuesInfo[0], isRelative) }
  202. : {
  203. '2fo-fc': this.convert(oldChannels['2fo-fc'], sampling.valuesInfo[0], isRelative),
  204. 'fo-fc(+ve)': this.convert(oldChannels['fo-fc(+ve)'], sampling.valuesInfo[1], isRelative),
  205. 'fo-fc(-ve)': this.convert(oldChannels['fo-fc(-ve)'], sampling.valuesInfo[1], isRelative)
  206. }
  207. }
  208. }
  209. });
  210. }
  211. };
  212. render() {
  213. if (!this.props.b) return null;
  214. const b = (this.props.b as VolumeStreaming).data;
  215. const isEM = b.info.kind === 'em';
  216. const pivot = isEM ? 'em' : '2fo-fc';
  217. const params = this.props.params as VolumeStreaming.Params;
  218. const entry = (this.props.info.params as VolumeStreaming.ParamDefinition)
  219. .entry.map(params.entry.name) as PD.Group<VolumeStreaming.EntryParamDefinition>;
  220. const detailLevel = entry.params.detailLevel;
  221. const dynamicDetailLevel = {
  222. ...detailLevel,
  223. label: 'Dynamic Detail',
  224. defaultValue: (entry.params.view as any).map('camera-target').params.dynamicDetailLevel.defaultValue,
  225. };
  226. const selectionDetailLevel = {
  227. ...detailLevel,
  228. label: 'Selection Detail',
  229. defaultValue: (entry.params.view as any).map('auto').params.selectionDetailLevel.defaultValue,
  230. };
  231. const sampling = b.info.header.sampling[0];
  232. const isRelative = ((params.entry.params.channels as any)[pivot].isoValue as Volume.IsoValue).kind === 'relative';
  233. const isRelativeParam = PD.Boolean(isRelative, { description: 'Use normalized or absolute isocontour scale.', label: 'Normalized' });
  234. const isUnbounded = !!(params.entry.params.view.params as any).isUnbounded;
  235. const isUnboundedParam = PD.Boolean(isUnbounded, { description: 'Show full/limited range of iso-values for more fine-grained control.', label: 'Unbounded' });
  236. const isOff = params.entry.params.view.name === 'off';
  237. // TODO: factor common things out, cache
  238. const OptionsParams = {
  239. entry: PD.Select(params.entry.name, b.data.entries.map(info => [info.dataId, info.dataId] as [string, string]), { isHidden: isOff, description: 'Which entry with volume data to display.' }),
  240. view: PD.MappedStatic(params.entry.params.view.name, {
  241. 'off': PD.Group({
  242. isRelative: PD.Boolean(isRelative, { isHidden: true }),
  243. isUnbounded: PD.Boolean(isUnbounded, { isHidden: true }),
  244. }, { description: 'Display off.' }),
  245. 'box': PD.Group({
  246. bottomLeft: PD.Vec3(Vec3.zero()),
  247. topRight: PD.Vec3(Vec3.zero()),
  248. detailLevel,
  249. isRelative: isRelativeParam,
  250. isUnbounded: isUnboundedParam,
  251. }, { description: 'Static box defined by cartesian coords.' }),
  252. 'selection-box': PD.Group({
  253. radius: PD.Numeric(5, { min: 0, max: 50, step: 0.5 }, { description: 'Radius in \u212B within which the volume is shown.' }),
  254. detailLevel,
  255. isRelative: isRelativeParam,
  256. isUnbounded: isUnboundedParam,
  257. }, { description: 'Box around focused element.' }),
  258. 'camera-target': PD.Group({
  259. radius: PD.Numeric(0.5, { min: 0, max: 1, step: 0.05 }, { description: 'Radius within which the volume is shown (relative to the field of view).' }),
  260. detailLevel: { ...detailLevel, isHidden: true },
  261. dynamicDetailLevel: dynamicDetailLevel,
  262. isRelative: isRelativeParam,
  263. isUnbounded: isUnboundedParam,
  264. }, { description: 'Box around camera target.' }),
  265. 'cell': PD.Group({
  266. detailLevel,
  267. isRelative: isRelativeParam,
  268. isUnbounded: isUnboundedParam,
  269. }, { description: 'Box around the structure\'s bounding box.' }),
  270. 'auto': PD.Group({
  271. radius: PD.Numeric(5, { min: 0, max: 50, step: 0.5 }, { description: 'Radius in \u212B within which the volume is shown.' }),
  272. detailLevel,
  273. selectionDetailLevel: selectionDetailLevel,
  274. isRelative: isRelativeParam,
  275. isUnbounded: isUnboundedParam,
  276. }, { description: 'Box around focused element.' }),
  277. }, { options: VolumeStreaming.ViewTypeOptions, description: 'Controls what of the volume is displayed. "Off" hides the volume alltogether. "Bounded box" shows the volume inside the given box. "Around Focus" shows the volume around the element/atom last interacted with. "Around Camera" shows the volume around the point the camera is targeting. "Whole Structure" shows the volume for the whole structure.' })
  278. };
  279. const options = {
  280. entry: params.entry.name,
  281. view: {
  282. name: params.entry.params.view.name,
  283. params: {
  284. detailLevel: params.entry.params.detailLevel,
  285. radius: (params.entry.params.view.params as any).radius,
  286. bottomLeft: (params.entry.params.view.params as any).bottomLeft,
  287. topRight: (params.entry.params.view.params as any).topRight,
  288. selectionDetailLevel: (params.entry.params.view.params as any).selectionDetailLevel,
  289. dynamicDetailLevel: (params.entry.params.view.params as any).dynamicDetailLevel,
  290. isRelative,
  291. isUnbounded
  292. }
  293. }
  294. };
  295. if (isOff) {
  296. return <ParameterControls onChange={this.changeOption} params={OptionsParams} values={options} onEnter={this.props.events.onEnter} isDisabled={this.props.isDisabled} />;
  297. }
  298. return <>
  299. {!isEM && <Channel label='2Fo-Fc' name='2fo-fc' bCell={this.props.bCell!} channels={params.entry.params.channels} changeIso={this.changeIso} changeParams={this.changeParams} isRelative={isRelative} params={this.props} stats={sampling.valuesInfo[0]} isUnbounded={isUnbounded} />}
  300. {!isEM && <Channel label='Fo-Fc(+ve)' name='fo-fc(+ve)' bCell={this.props.bCell!} channels={params.entry.params.channels} changeIso={this.changeIso} changeParams={this.changeParams} isRelative={isRelative} params={this.props} stats={sampling.valuesInfo[1]} isUnbounded={isUnbounded} />}
  301. {!isEM && <Channel label='Fo-Fc(-ve)' name='fo-fc(-ve)' bCell={this.props.bCell!} channels={params.entry.params.channels} changeIso={this.changeIso} changeParams={this.changeParams} isRelative={isRelative} params={this.props} stats={sampling.valuesInfo[1]} isUnbounded={isUnbounded} />}
  302. {isEM && <Channel label='EM' name='em' bCell={this.props.bCell!} channels={params.entry.params.channels} changeIso={this.changeIso} changeParams={this.changeParams} isRelative={isRelative} params={this.props} stats={sampling.valuesInfo[0]} isUnbounded={isUnbounded} />}
  303. <ParameterControls onChange={this.changeOption} params={OptionsParams} values={options} onEnter={this.props.events.onEnter} isDisabled={this.props.isDisabled} />
  304. </>;
  305. }
  306. }