-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
vtklayout.ts
535 lines (482 loc) · 17.1 KB
/
vtklayout.ts
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
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
225
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
270
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import type * as p from "@bokehjs/core/properties"
import {div, canvas} from "@bokehjs/core/dom"
import {clone} from "@bokehjs/core/util/object"
import {ColorMapper} from "@bokehjs/models/mappers/color_mapper"
import {Enum} from "@bokehjs/core/kinds"
import {HTMLBox, HTMLBoxView, set_size} from "../layout"
import type {VolumeType, CSSProperties, Annotation} from "./util"
import {vtkns, setup_vtkns, majorAxis, applyStyle} from "./util"
import {VTKColorBar} from "./vtkcolorbar"
import {VTKAxes} from "./vtkaxes"
const INFO_DIV_STYLE: CSSProperties = {
padding: "0px 2px 0px 2px",
maxHeight: "150px",
height: "auto",
backgroundColor: "rgba(255, 255, 255, 0.4)",
borderRadius: "10px",
margin: "2px",
boxSizing: "border-box",
overflow: "hidden",
overflowY: "auto",
transition: "width 0.1s linear",
bottom: "0px",
position: "absolute",
}
const textPositions = Enum("LowerLeft", "LowerRight", "UpperLeft", "UpperRight", "LowerEdge", "RightEdge", "LeftEdge", "UpperEdge")
export abstract class AbstractVTKView extends HTMLBoxView {
declare model: AbstractVTKPlot
protected _axes: any
protected _camera_callbacks: any[]
protected _orientationWidget: any
protected _renderable: boolean
protected _setting_camera: boolean
protected _vtk_container: HTMLDivElement
protected _vtk_renwin: any
protected _widgetManager: any
protected _annotations_container: HTMLDivElement
protected _rendered: boolean
override initialize(): void {
super.initialize()
this._camera_callbacks = []
this._renderable = true
this._setting_camera = false
this._rendered = false
}
_add_colorbars(): void {
//construct colorbars
const old_info_div = this.shadow_el.querySelector(".vtk_info")
if (old_info_div) {
this.shadow_el.removeChild(old_info_div)
}
if (this.model.color_mappers.length < 1) {
return
}
const info_div = document.createElement("div")
const expand_width = "350px"
const collapsed_width = "30px"
info_div.classList.add("vtk_info")
applyStyle(info_div, INFO_DIV_STYLE)
applyStyle(info_div, {width: expand_width})
this.shadow_el.appendChild(info_div)
//construct colorbars
const colorbars: VTKColorBar[] = []
this.model.color_mappers.forEach((mapper) => {
const cb = new VTKColorBar(info_div, mapper)
colorbars.push(cb)
})
//content when collapsed
const dots = document.createElement("div")
applyStyle(dots, {textAlign: "center", fontSize: "20px"})
dots.innerText = "..."
info_div.addEventListener("click", () => {
if (info_div.style.width === collapsed_width) {
info_div.removeChild(dots)
applyStyle(info_div, {height: "auto", width: expand_width})
colorbars.forEach((cb) => info_div.appendChild(cb.canvas))
} else {
colorbars.forEach((cb) => info_div.removeChild(cb.canvas))
applyStyle(info_div, {height: collapsed_width, width: collapsed_width})
info_div.appendChild(dots)
}
})
info_div.click()
}
_init_annotations_container(): void {
if (!this._annotations_container) {
this._annotations_container = document.createElement("div")
this._annotations_container.style.position = "absolute"
this._annotations_container.style.width = "100%"
this._annotations_container.style.height = "100%"
this._annotations_container.style.top = "0"
this._annotations_container.style.left = "0"
this._annotations_container.style.pointerEvents = "none"
}
}
_clean_annotations(): void {
if (this._annotations_container) {
while (this._annotations_container.firstElementChild) {
this._annotations_container.firstElementChild.remove()
}
}
}
_add_annotations(): void {
this._clean_annotations()
const {annotations} = this.model
if (annotations != null) {
for (const annotation of annotations) {
const {viewport, color, fontSize, fontFamily} = annotation
textPositions.values.forEach((pos) => {
const text = annotation[pos]
if (text) {
const div = document.createElement("div")
div.textContent = text
const {style} = div
style.position = "absolute"
style.color = `rgb(${color.map((val)=>255*val).join(",")})`
style.fontSize = `${fontSize}px`
style.padding = "5px"
style.fontFamily = fontFamily
style.width = "fit-content"
if (pos == "UpperLeft") {
style.top = `${(1 - viewport[3])*100}%`
style.left = `${viewport[0]*100}%`
}
if (pos == "UpperRight") {
style.top = `${(1 - viewport[3])*100}%`
style.right = `${(1-viewport[2])*100}%`
}
if (pos == "LowerLeft") {
style.bottom = `${viewport[1]*100}%`
style.left = `${viewport[0]*100}%`
}
if (pos == "LowerRight") {
style.bottom = `${viewport[1]*100}%`
style.right = `${(1-viewport[2])*100}%`
}
if (pos == "UpperEdge") {
style.top = `${(1 - viewport[3])*100}%`
style.left = `${(viewport[0] + (viewport[2] - viewport[0])/2) *100}%`
style.transform = "translateX(-50%)"
}
if (pos == "LowerEdge") {
style.bottom = `${viewport[1]*100}%`
style.left = `${(viewport[0] + (viewport[2] - viewport[0])/2) *100}%`
style.transform = "translateX(-50%)"
}
if (pos == "LeftEdge") {
style.left = `${viewport[0]*100}%`
style.top = `${(1 - viewport[3] + (viewport[3] - viewport[1])/2) *100}%`
style.transform = "translateY(-50%)"
}
if (pos == "RightEdge") {
style.right = `${(1-viewport[2])*100}%`
style.top = `${(1 - viewport[3] + (viewport[3] - viewport[1])/2) *100}%`
style.transform = "translateY(-50%)"
}
this._annotations_container.appendChild(div)
}
})
}
}
}
override connect_signals(): void {
super.connect_signals()
this.on_change(this.model.properties.orientation_widget, () => {
this._orientation_widget_visibility(this.model.orientation_widget)
})
this.on_change(this.model.properties.camera, () => this._set_camera_state())
this.on_change(this.model.properties.axes, () => {
this._delete_axes()
if (this.model.axes) {
this._set_axes()
}
this._vtk_render()
})
this.on_change(this.model.properties.color_mappers, () => this._add_colorbars())
this.on_change(this.model.properties.annotations, () => this._add_annotations())
}
override render(): void {
super.render()
this._rendered = false
this._orientationWidget = null
this._axes = null
this._vtk_container = div()
this.init_vtk_renwin()
this._init_annotations_container()
set_size(this._vtk_container, this.model)
this.shadow_el.appendChild(this._vtk_container)
// update camera model state only at the end of the interaction
// with the scene (avoid bouncing events and large amount of events)
this._vtk_renwin.getInteractor().onEndAnimation(() => this._get_camera_state())
this._remove_default_key_binding()
this._bind_key_events()
this.plot()
this.model.renderer_el = this._vtk_renwin
this.shadow_el.appendChild(this._annotations_container)
}
override after_layout(): void {
super.after_layout()
if (this._renderable) {
this._vtk_renwin.resize() // resize call render method
}
this._vtk_render()
if (!this._rendered) {
this._add_colorbars()
this._add_annotations()
this._rendered = true
}
}
override invalidate_render(): void {
this._unsubscribe_camera_cb()
super.invalidate_render()
}
override remove(): void {
this._unsubscribe_camera_cb()
window.removeEventListener("resize", this._vtk_renwin.resize)
if (this._orientationWidget != null) {
this._orientationWidget.delete()
}
this._vtk_renwin.getRenderWindow().getInteractor().delete()
this._vtk_renwin.delete()
super.remove()
}
abstract init_vtk_renwin(): void
abstract plot(): void //here goes the specific implementation pour all concrete model based on vtk-js
get _vtk_camera_state(): any {
const vtk_camera = this._vtk_renwin.getRenderer().getActiveCamera()
let state: any
if (vtk_camera) {
state = clone(vtk_camera.get())
delete state.cameraLightTransform
delete state.classHierarchy
delete state.vtkObject
delete state.vtkCamera
delete state.viewPlaneNormal
delete state.flattenedDepIds
delete state.managedInstanceId
delete state.directionOfProjection
}
return state
}
get _axes_canvas(): HTMLCanvasElement {
let axes_canvas = this._vtk_container.querySelector(".axes-canvas") as HTMLCanvasElement
if (!axes_canvas) {
axes_canvas = canvas({
style: {
position: "absolute",
top: "0",
left: "0",
width: "100%",
height: "100%",
},
})
axes_canvas.classList.add("axes-canvas")
this._vtk_container.appendChild(axes_canvas)
this._vtk_renwin.setResizeCallback(() => {
if (this._axes_canvas) {
const dims = this._vtk_container.getBoundingClientRect()
const width = Math.floor(dims.width * window.devicePixelRatio)
const height = Math.floor(dims.height * window.devicePixelRatio)
this._axes_canvas.setAttribute("width", width.toFixed())
this._axes_canvas.setAttribute("height", height.toFixed())
}
})
}
return axes_canvas
}
_bind_key_events(): void {
this.el.addEventListener("mouseenter", () => {
const interactor = this._vtk_renwin.getInteractor()
if (this.model.enable_keybindings) {
document
.querySelector("body")!
.addEventListener("keypress", interactor.handleKeyPress)
document
.querySelector("body")!
.addEventListener("keydown", interactor.handleKeyDown)
document
.querySelector("body")!
.addEventListener("keyup", interactor.handleKeyUp)
}
})
this.el.addEventListener("mouseleave", () => {
const interactor = this._vtk_renwin.getInteractor()
document
.querySelector("body")!
.removeEventListener("keypress", interactor.handleKeyPress)
document
.querySelector("body")!
.removeEventListener("keydown", interactor.handleKeyDown)
document
.querySelector("body")!
.removeEventListener("keyup", interactor.handleKeyUp)
})
}
_create_orientation_widget(): void {
const axes = vtkns.AxesActor.newInstance()
// add orientation widget
this._orientationWidget = vtkns.OrientationMarkerWidget.newInstance({
actor: axes,
interactor: this._vtk_renwin.getInteractor(),
})
this._orientationWidget.setEnabled(true)
this._orientationWidget.setViewportCorner(vtkns.OrientationMarkerWidget.Corners.BOTTOM_RIGHT)
this._orientationWidget.setViewportSize(0.15)
this._orientationWidget.setMinPixelSize(75)
this._orientationWidget.setMaxPixelSize(300)
if (this.model.interactive_orientation_widget) {
this._make_orientation_widget_interactive()
}
this._orientation_widget_visibility(this.model.orientation_widget)
}
_make_orientation_widget_interactive(): void {
this._widgetManager = vtkns.WidgetManager.newInstance()
this._widgetManager.setRenderer(this._orientationWidget.getRenderer())
const axes = this._orientationWidget.getActor()
const widget = vtkns.InteractiveOrientationWidget.newInstance()
widget.placeWidget(axes.getBounds())
widget.setBounds(axes.getBounds())
widget.setPlaceFactor(1)
const vw = this._widgetManager.addWidget(widget)
// Manage user interaction
vw.onOrientationChange(({direction}: any) => {
const camera = this._vtk_renwin.getRenderer().getActiveCamera()
const focalPoint = camera.getFocalPoint()
const position = camera.getPosition()
const viewUp = camera.getViewUp()
const distance = Math.sqrt(
(position[0] - focalPoint[0])**2 +
(position[1] - focalPoint[1])**2 +
(position[2] - focalPoint[2])**2,
)
camera.setPosition(
focalPoint[0] + direction[0] * distance,
focalPoint[1] + direction[1] * distance,
focalPoint[2] + direction[2] * distance,
)
if (direction[0]) {
camera.setViewUp(majorAxis(viewUp, 1, 2))
}
if (direction[1]) {
camera.setViewUp(majorAxis(viewUp, 0, 2))
}
if (direction[2]) {
camera.setViewUp(majorAxis(viewUp, 0, 1))
}
this._vtk_renwin.getRenderer().resetCameraClippingRange()
this._vtk_render()
this._get_camera_state()
})
}
_delete_axes(): void {
if (this._axes) {
Object.keys(this._axes).forEach((key) => {
this._vtk_renwin.getRenderer().removeActor(this._axes[key])
})
this._axes = null
const textCtx = this._axes_canvas.getContext("2d")
if (textCtx) {
textCtx.clearRect(
0,
0,
this._axes_canvas.clientWidth * window.devicePixelRatio,
this._axes_canvas.clientHeight * window.devicePixelRatio,
)
}
}
}
_get_camera_state(): void {
if (!this._setting_camera) {
this._setting_camera = true
this.model.camera = this._vtk_camera_state
this._setting_camera = false
}
}
_orientation_widget_visibility(visibility: boolean): void {
this._orientationWidget.setEnabled(visibility)
if (this._widgetManager != null) {
if (visibility) {
this._widgetManager.enablePicking()
} else {
this._widgetManager.disablePicking()
}
}
this._vtk_render()
}
_remove_default_key_binding(): void {
const interactor = this._vtk_renwin.getInteractor()
document
.querySelector("body")!
.removeEventListener("keypress", interactor.handleKeyPress)
document
.querySelector("body")!
.removeEventListener("keydown", interactor.handleKeyDown)
document
.querySelector("body")!
.removeEventListener("keyup", interactor.handleKeyUp)
}
_set_axes(): void {
if (this.model.axes && this._vtk_renwin.getRenderer()) {
const {psActor, axesActor, gridActor} = this.model.axes.create_axes(this._axes_canvas)
this._axes = {psActor, axesActor, gridActor}
if (psActor) {
this._vtk_renwin.getRenderer().addActor(psActor)
}
if (axesActor) {
this._vtk_renwin.getRenderer().addActor(axesActor)
}
if (gridActor) {
this._vtk_renwin.getRenderer().addActor(gridActor)
}
}
}
_set_camera_state(): void {
if (!this._setting_camera && this._vtk_renwin.getRenderer() !== undefined) {
this._setting_camera = true
if (
this.model.camera &&
JSON.stringify(this.model.camera) != JSON.stringify(this._vtk_camera_state)
) {
this._vtk_renwin
.getRenderer()
.getActiveCamera()
.set(this.model.camera)
}
this._vtk_renwin.getRenderer().resetCameraClippingRange()
this._setting_camera = false
}
}
_unsubscribe_camera_cb(): void {
this._camera_callbacks
.splice(0, this._camera_callbacks.length)
.map((cb) => cb.unsubscribe())
}
_vtk_render(): void {
if (this._renderable) {
if (this._orientationWidget) {
this._orientationWidget.updateMarkerOrientation()
}
this._vtk_renwin.getRenderWindow().render()
}
}
}
export namespace AbstractVTKPlot {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
axes: p.Property<VTKAxes | null>
camera: p.Property<any>
data: p.Property<string | VolumeType | ArrayBuffer | null>
enable_keybindings: p.Property<boolean>
orientation_widget: p.Property<boolean>
color_mappers: p.Property<ColorMapper[]>
interactive_orientation_widget: p.Property<boolean>
annotations: p.Property<Annotation[] | null>
}
}
export interface AbstractVTKPlot extends AbstractVTKPlot.Attrs {}
export abstract class AbstractVTKPlot extends HTMLBox {
declare properties: AbstractVTKPlot.Props
renderer_el: any
static override __module__ = "panel.models.vtk"
constructor(attrs?: Partial<AbstractVTKPlot.Attrs>) {
setup_vtkns()
super(attrs)
}
getActors(): any[] {
return this.renderer_el.getRenderer().getActors()
}
static {
this.define<AbstractVTKPlot.Props>(({Any, Ref, Array, Boolean, Nullable}) => ({
axes: [ Nullable(Ref(VTKAxes)), null ],
camera: [ Any, {} ],
color_mappers: [ Array(Ref(ColorMapper)), [] ],
orientation_widget: [ Boolean, false ],
interactive_orientation_widget: [ Boolean, false ],
annotations: [ Nullable(Array(Any)), null ],
}))
this.override<AbstractVTKPlot.Props>({
height: 300,
width: 300,
})
}
}