Newer
Older
import { Component, Input, Output, EventEmitter, ElementRef, OnChanges, OnDestroy, AfterViewInit, Inject, Optional } from "@angular/core";
import { EnumViewerEvt, IViewer, TViewerEvent } from "src/viewerModule/viewer.interface";
import { TThreeSurferConfig, TThreeSurferMode } from "../types";
import { retry, flattenRegions } from 'common/util'
import { BehaviorSubject, Observable, Subject } from "rxjs";
import { debounceTime, filter, map, switchMap } from "rxjs/operators";
import { ComponentStore } from "src/viewerModule/componentStore";
import { select, Store } from "@ngrx/store";
import { viewerStateChangeNavigation, viewerStateSetSelectedRegions } from "src/services/state/viewerState/actions";
import { viewerStateSelectorNavigation } from "src/services/state/viewerState/selectors";
import { ClickInterceptor, CLICK_INTERCEPTOR_INJECTOR } from "src/util";
import { REGION_OF_INTEREST } from "src/util/interfaces";
import { MatSnackBar } from "@angular/material/snack-bar";
import { CONST } from 'common/constants'
import { API_SERVICE_SET_VIEWER_HANDLE_TOKEN, TSetViewerHandle } from "src/atlasViewer/atlasViewer.apiService.service";
import { switchMapWaitFor } from "src/util/fn";
const pZoomFactor = 5e3
type THandlingCustomEv = {
regions: ({ name?: string, error?: string })[]
evMesh?: {
faceIndex: number
verticesIndicies: number[]
}
}
type TCameraOrientation = {
perspectiveOrientation: [number, number, number, number]
perspectiveZoom: number
}
const threshold = 1e-3
function getHemisphereKey(region: { name: string }){
return /left/.test(region.name)
? 'left'
: /right/.test(region.name)
? 'right'
: null
}
function cameraNavsAreSimilar(c1: TCameraOrientation, c2: TCameraOrientation){
if (c1 === c2) return true
if (!!c1 && !!c2) return true
if (!c1 && !!c2) return false
if (!c2 && !!c1) return false
if (Math.abs(c1.perspectiveZoom - c2.perspectiveZoom) > threshold) return false
if ([0, 1, 2, 3].some(
idx => Math.abs(c1.perspectiveOrientation[idx] - c2.perspectiveOrientation[idx]) > threshold
)) {
return false
}
return true
}
@Component({
selector: 'three-surfer-glue-cmp',
templateUrl: './threeSurfer.template.html',
styleUrls: [
'./threeSurfer.style.css'
export class ThreeSurferGlueCmp implements IViewer<'threeSurfer'>, OnChanges, AfterViewInit, OnDestroy {
@Input()
selectedTemplate: any
@Input()
selectedParcellation: any
@Output()
viewerEvent = new EventEmitter<TViewerEvent<'threeSurfer'>>()
private domEl: HTMLElement
private config: TThreeSurferConfig
public modes: TThreeSurferMode[] = []
public selectedMode: string
private mainStoreCameraNav: TCameraOrientation = null
private localCameraNav: TCameraOrientation = null
public allKeys: {name: string, checked: boolean}[] = []
private regionMap: Map<string, Map<number, any>> = new Map()
private store$: Store<any>,
private navStateStoreRelay: ComponentStore<{ perspectiveOrientation: [number, number, number, number], perspectiveZoom: number }>,
private snackbar: MatSnackBar,
@Optional() @Inject(REGION_OF_INTEREST) private roi$: Observable<any>,
@Optional() @Inject(CLICK_INTERCEPTOR_INJECTOR) clickInterceptor: ClickInterceptor,
@Optional() @Inject(API_SERVICE_SET_VIEWER_HANDLE_TOKEN) setViewerHandle: TSetViewerHandle,
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// set viewer handle
// the API won't be 100% compatible with ngviewer
if (setViewerHandle) {
const nyi = () => {
throw new Error(`Not yet implemented`)
}
setViewerHandle({
add3DLandmarks: nyi,
loadLayer: nyi,
applyLayersColourMap: (map: Map<string, Map<number, { red: number, green: number, blue: number }>>) => {
const applyCm = new Map()
for (const [hem, m] of map.entries()) {
const nMap = new Map()
applyCm.set(hem, nMap)
for (const [lbl, vals] of m.entries()) {
const { red, green, blue } = vals
nMap.set(lbl, [red/255, green/255, blue/255])
}
}
this.externalHemisphLblColorMap = applyCm
},
getLayersSegmentColourMap: () => {
const map = this.getColormapCopy()
const outmap = new Map<string, Map<number, { red: number, green: number, blue: number }>>()
for (const [ hem, m ] of map.entries()) {
const nMap = new Map<number, {red: number, green: number, blue: number}>()
outmap.set(hem, nMap)
for (const [ lbl, vals ] of m.entries()) {
nMap.set(lbl, {
red: vals[0] * 255,
green: vals[1] * 255,
blue: vals[2] * 255,
})
}
}
return outmap
},
getNgHash: nyi,
hideAllSegments: nyi,
hideSegment: nyi,
mouseEvent: null,
mouseOverNehuba: null,
mouseOverNehubaLayers: null,
mouseOverNehubaUI: null,
moveToNavigationLoc: null,
moveToNavigationOri: null,
remove3DLandmarks: null,
removeLayer: null,
setLayerVisibility: null,
setNavigationLoc: null,
setNavigationOri: null,
showAllSegments: nyi,
showSegment: nyi,
})
}
if (this.roi$) {
const sub = this.roi$.pipe(
switchMap(switchMapWaitFor({
condition: () => this.colormapLoaded
}))
).subscribe(r => {
try {
if (!r) throw new Error(`No region selected.`)
const cmap = this.getColormapCopy()
const hemisphere = getHemisphereKey(r)
if (!hemisphere) {
this.snackbar.open(CONST.CANNOT_DECIPHER_HEMISPHERE, 'Dismiss', {
duration: 3000
})
throw new Error(CONST.CANNOT_DECIPHER_HEMISPHERE)
}
for (const [ hem, m ] of cmap.entries()) {
for (const lbl of m.keys()) {
if (hem !== hemisphere || lbl !== r.labelIndex) {
m.set(lbl, [1, 1, 1])
}
}
}
this.internalHemisphLblColorMap = cmap
} catch (e) {
this.internalHemisphLblColorMap = null
}
this.applyColorMap()
})
this.onDestroyCb.push(
() => sub.unsubscribe()
)
}
/**
* intercept click and act
*/
if (clickInterceptor) {
const handleClick = (ev: MouseEvent) => {
// if does not click inside container, ignore
if (!(this.el.nativeElement as HTMLElement).contains(ev.target as HTMLElement)) {
return true
}
if (this.mouseoverRegions.length === 0) return true
if (this.mouseoverRegions.length > 1) {
this.snackbar.open(CONST.DOES_NOT_SUPPORT_MULTI_REGION_SELECTION, 'Dismiss', {
duration: 3000
})
return true
}
this.store$.dispatch(
viewerStateSetSelectedRegions({
selectRegions: this.mouseoverRegions
})
)
return true
}
const { register, deregister } = clickInterceptor
register(handleClick)
this.onDestroyCb.push(
() => deregister(register)
)
}
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* subscribe to camera custom event
*/
const cameraSub = this.cameraEv$.pipe(
filter(v => !!v),
debounceTime(160)
).subscribe(ev => {
const { position } = ev
const { x, y, z } = position
const THREE = (window as any).ThreeSurfer.THREE
const q = new THREE.Quaternion()
const t = new THREE.Vector3()
const s = new THREE.Vector3()
const cameraM = this.tsRef.camera.matrix
cameraM.decompose(t, q, s)
try {
this.navStateStoreRelay.setState({
perspectiveOrientation: q.toArray(),
perspectiveZoom: t.length()
})
} catch (e) {
// LockError, ignore
}
})
this.onDestroyCb.push(
() => cameraSub.unsubscribe()
)
/**
* subscribe to navstore relay store and negotiate setting global state
*/
const navStateSub = this.navStateStoreRelay.select(s => s).subscribe(v => {
this.store$.dispatch(
viewerStateChangeNavigation({
navigation: {
position: [0, 0, 0],
orientation: [0, 0, 0, 1],
zoom: 1,
perspectiveOrientation: v.perspectiveOrientation,
perspectiveZoom: v.perspectiveZoom * pZoomFactor
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
}
})
)
})
this.onDestroyCb.push(
() => navStateSub.unsubscribe()
)
/**
* subscribe to main store and negotiate with relay to set camera
*/
const navSub = this.store$.pipe(
select(viewerStateSelectorNavigation)
).subscribe(nav => {
const { perspectiveOrientation, perspectiveZoom } = nav
this.mainStoreCameraNav = {
perspectiveOrientation,
perspectiveZoom
}
if (!cameraNavsAreSimilar(this.mainStoreCameraNav, this.localCameraNav)) {
this.relayStoreLock = this.navStateStoreRelay.getLock()
const THREE = (window as any).ThreeSurfer.THREE
const cameraQuat = new THREE.Quaternion(...this.mainStoreCameraNav.perspectiveOrientation)
const cameraPos = new THREE.Vector3(0, 0, this.mainStoreCameraNav.perspectiveZoom / pZoomFactor)
cameraPos.applyQuaternion(cameraQuat)
this.toTsRef(tsRef => {
tsRef.camera.position.copy(cameraPos)
if (this.relayStoreLock) this.relayStoreLock()
})
}
})
this.onDestroyCb.push(
() => navSub.unsubscribe()
)
threeSurfer: any
mesh: string
colormap: string
hemisphere: string
private hemisphLblColorMap: Map<string, Map<number, [number, number, number]>> = new Map()
private internalHemisphLblColorMap: Map<string, Map<number, [number, number, number]>>
private externalHemisphLblColorMap: Map<string, Map<number, [number, number, number]>>
get activeColorMap() {
if (this.externalHemisphLblColorMap) return this.externalHemisphLblColorMap
if (this.internalHemisphLblColorMap) return this.internalHemisphLblColorMap
return this.hemisphLblColorMap
}
private relayStoreLock: () => void = null
private tsRefInitCb: ((tsRef: any) => void)[] = []
private toTsRef(callback: (tsRef: any) => void) {
if (this.tsRef) {
callback(this.tsRef)
return
}
this.tsRefInitCb.push(callback)
}
while(this.loadedMeshes.length > 0) {
const m = this.loadedMeshes.pop()
this.hemisphLblColorMap.clear()
this.colormapLoaded = false
}
public async loadMode(mode: TThreeSurferMode) {
this.unloadAllMeshes()
this.selectedMode = mode.name
const { meshes } = mode
await retry(async () => {
for (const singleMesh of meshes) {
const { hemisphere } = singleMesh
if (!this.regionMap.has(hemisphere)) throw new Error(`regionmap does not have hemisphere defined!`)
}
}, {
timeout: 32,
retries: 10
})
this.allKeys.push({name: hemisphere, checked: true})
const tsM = await this.tsRef.loadMesh(
parseContext(mesh, [this.config['@context']])
)
if (!this.regionMap.has(hemisphere)) continue
const rMap = this.regionMap.get(hemisphere)
const applyCM = new Map()
for (const [ lblIdx, region ] of rMap.entries()) {
applyCM.set(lblIdx, (region.rgb || [200, 200, 200]).map(v => v/255))
}
const tsC = await this.tsRef.loadColormap(
parseContext(colormap, [this.config['@context']])
)
let colorIdx = tsC[0].getData()
if (tsC[0].attributes.DataType === 'NIFTI_TYPE_INT16') {
colorIdx = (window as any).ThreeSurfer.GiftiBase.castF32UInt16(colorIdx)
}
this.loadedMeshes.push({
threeSurfer: tsM,
colormap,
mesh,
hemisphere,
vIdxArr: colorIdx
})
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
this.hemisphLblColorMap.set(hemisphere, applyCM)
}
this.colormapLoaded = true
this.applyColorMap()
}
private colormapLoaded = false
private getColormapCopy(): Map<string, Map<number, [number, number, number]>> {
const outmap = new Map()
for (const [key, value] of this.hemisphLblColorMap.entries()) {
outmap.set(key, new Map(value))
}
return outmap
}
/**
* TODO perhaps debounce calls to applycolormap
* so that the colormap doesn't "flick"
*/
private applyColorMap(){
for (const mesh of this.loadedMeshes) {
const { hemisphere, threeSurfer, vIdxArr } = mesh
const applyCM = this.activeColorMap.get(hemisphere)
this.tsRef.applyColorMap(threeSurfer, vIdxArr,
if (this.tsRef) {
this.ngOnDestroy()
this.ngAfterViewInit()
}
/**
* wait until threesurfer is defined in window
*/
await retry(async () => {
if (typeof (window as any).ThreeSurfer === 'undefined') throw new Error('ThreeSurfer not yet defined')
}, {
timeout: 160,
retries: 10,
})
this.config = this.selectedTemplate['three-surfer']
// somehow curv ... cannot be parsed properly by gifti parser... something about points missing
this.modes = this.config.modes.filter(m => !/curv/.test(m.name))
if (!this.tsRef) {
this.tsRef = new (window as any).ThreeSurfer(this.domEl, {highlightHovered: true})
this.onDestroyCb.push(
() => {
this.tsRef.dispose()
this.tsRef = null
}
);
(window as any).tsRef = this.tsRef
while (this.tsRefInitCb.length > 0) this.tsRefInitCb.pop()(this.tsRef)
const flattenedRegions = flattenRegions(this.selectedParcellation.regions)
for (const region of flattenedRegions) {
if (region.labelIndex) {
const hemisphere = getHemisphereKey(region)
if (!hemisphere) throw new Error(`region ${region.name} does not have hemisphere defined`)
if (!this.regionMap.has(hemisphere)) {
this.regionMap.set(hemisphere, new Map())
}
const rMap = this.regionMap.get(hemisphere)
rMap.set(region.labelIndex, region)
// load mode0 by default
this.loadMode(this.config.modes[0])
this.viewerEvent.emit({
type: EnumViewerEvt.VIEWERLOADED,
private handleCustomMouseEv(detail: any){
const evMesh = detail.mesh && {
faceIndex: detail.mesh.faceIndex,
// typo in three-surfer
verticesIndicies: detail.mesh.verticesIdicies
}
const custEv: THandlingCustomEv = {
regions: [],
evMesh
}
if (!detail.mesh) {
return this.handleMouseoverEvent(custEv)
}
const evGeom = detail.mesh.geometry
const evVertIdx = detail.mesh.verticesIdicies
const found = this.loadedMeshes.find(({ threeSurfer }) => threeSurfer === evGeom)
if (!found) return this.handleMouseoverEvent(custEv)
/**
* check if the mesh is toggled off
* if so, do not proceed
*/
const checkKey = this.allKeys.find(key => key.name === found.hemisphere)
if (checkKey && !checkKey.checked) return
const { hemisphere: key, vIdxArr } = found
if (!key || !evVertIdx) {
return this.handleMouseoverEvent(custEv)
}
const labelIdxSet = new Set<number>()
for (const vIdx of evVertIdx) {
labelIdxSet.add(
vIdxArr[vIdx]
)
}
if (labelIdxSet.size === 0) {
return this.handleMouseoverEvent(custEv)
}
const hemisphereMap = this.regionMap.get(key)
if (!hemisphereMap) {
custEv.regions = Array.from(labelIdxSet).map(v => {
return {
error: `unknown#${v}`
}
})
return this.handleMouseoverEvent(custEv)
}
custEv.regions = Array.from(labelIdxSet)
.map(lblIdx => {
const ontoR = hemisphereMap.get(lblIdx)
if (ontoR) {
return ontoR
} else {
}
})
return this.handleMouseoverEvent(custEv)
}
private cameraEv$ = new Subject<{ position: { x: number, y: number, z: number }, zoom: number }>()
private handleCustomCameraEvent(detail: any){
this.cameraEv$.next(detail)
}
ngAfterViewInit(){
const customEvHandler = (ev: CustomEvent) => {
const { type, data } = ev.detail
if (type === 'mouseover') {
return this.handleCustomMouseEv(data)
}
if (type === 'camera') {
return this.handleCustomCameraEvent(data)
}
}
this.domEl.addEventListener((window as any).ThreeSurfer.CUSTOM_EVENTNAME_UPDATED, customEvHandler)
() => this.domEl.removeEventListener((window as any).ThreeSurfer.CUSTOM_EVENTNAME_UPDATED, customEvHandler)
private handleMouseoverEvent(ev: THandlingCustomEv){
const { regions: mouseover, evMesh } = ev
this.viewerEvent.emit({
type: EnumViewerEvt.VIEWER_CTX,
data: {
viewerType: 'threeSurfer',
payload: {
fsversion: this.selectedMode,
faceIndex: evMesh?.faceIndex,
vertexIndices: evMesh?.verticesIndicies,
position: [],
_mouseoverRegion: mouseover.filter(el => !el.error)
}
}
})
this.mouseoverText = mouseover.length === 0 ?
null :
mouseover.map(
el => el.name || el.error
).join(' / ')
public handleCheckBox(key: { name: string, checked: boolean }, flag: boolean){
const foundMesh = this.loadedMeshes.find(m => m.hemisphere === key.name)
if (!foundMesh) {
throw new Error(`Cannot find mesh with name: ${key.name}`)
}
const meshObj = this.tsRef.customColormap.get(foundMesh.threeSurfer)
if (!meshObj) {
throw new Error(`mesh obj not found!`)
}
meshObj.mesh.visible = flag
}