@@ -10,39 +10,13 @@ import type {
1010 CustomMessagePayload ,
1111 MeshModel ,
1212 Model ,
13+ NiivueObject3D ,
1314 Scene ,
1415 VolumeModel ,
1516} from "./types.ts" ;
1617
1718let nv : niivue . Niivue ;
1819
19- function deserializeOptions (
20- options : Partial < Record < keyof niivue . NVConfigOptions , unknown > > ,
21- ) : niivue . NVConfigOptions {
22- const result : Partial < niivue . NVConfigOptions > = { } ;
23- const specialValues : Record < string , number > = {
24- Infinity : Number . POSITIVE_INFINITY ,
25- "-Infinity" : Number . NEGATIVE_INFINITY ,
26- NaN : Number . NaN ,
27- "-0" : - 0 ,
28- } ;
29-
30- for ( const [ key , value ] of Object . entries ( options ) as [
31- keyof niivue . NVConfigOptions ,
32- unknown ,
33- ] [ ] ) {
34- if ( typeof value === "string" && value in specialValues ) {
35- // biome-ignore lint/suspicious/noExplicitAny: NVConfigOptions
36- ( result as any ) [ key ] = specialValues [ value ] ;
37- } else {
38- // biome-ignore lint/suspicious/noExplicitAny: NVConfigOptions
39- ( result as any ) [ key ] = value ;
40- }
41- }
42-
43- return result as niivue . NVConfigOptions ;
44- }
45-
4620// Attach model event handlers
4721function attachModelEventHandlers (
4822 nv : niivue . Niivue ,
@@ -60,13 +34,13 @@ function attachModelEventHandlers(
6034 }
6135 } ) ;
6236
63- // Any time we change the options, we need to update the nv gl
37+ // Any time the backend changes the options, we need to update the nv gl
38+ // but...need to filter out changes that are *actually* from the frontend
39+ // so, we don't call updateGLVolume here (instead, it's explicitly called from the backend)
6440 model . on ( "change:opts" , ( ) => {
6541 const serializedOpts = model . get ( "opts" ) ;
66- const opts = deserializeOptions ( serializedOpts ) ;
67-
42+ const opts = lib . deserializeOptions ( serializedOpts ) ;
6843 nv . document . opts = { ...nv . opts , ...opts } ;
69- nv . updateGLVolume ( ) ;
7044 } ) ;
7145
7246 // Other nv prop changes
@@ -111,9 +85,6 @@ function attachModelEventHandlers(
11185 // biome-ignore lint/suspicious/noExplicitAny: Update graph vals, only clear out old vals when needed
11286 ( nv . graph as any ) [ key ] = value ;
11387 }
114- if ( nv . _gl ) {
115- nv . updateGLVolume ( ) ;
116- }
11788 }
11889
11990 function scene_changed ( ) {
@@ -214,6 +185,10 @@ function attachModelEventHandlers(
214185 nv . drawScene ( ) ;
215186 break ;
216187 }
188+ case "update_gl_volume" : {
189+ nv . updateGLVolume ( ) ;
190+ break ;
191+ }
217192 case "set_volume_render_illumination" : {
218193 if ( nv . _gl ) {
219194 let [ gradientAmount ] = data ;
@@ -333,6 +308,29 @@ function attachNiivueEventHandlers(nv: niivue.Niivue, model: Model) {
333308 } ,
334309 } ) ;
335310
311+ const originalRefreshLayersMethod = nv . refreshLayers ;
312+ nv . refreshLayers = new Proxy ( originalRefreshLayersMethod , {
313+ apply : ( target , thisArg , argumentsList ) => {
314+ Reflect . apply ( target , thisArg , argumentsList ) ;
315+
316+ if ( nv . volumeObject3D ) {
317+ const currentVolumeObject3D : NiivueObject3D = {
318+ id : nv . volumeObject3D . id ,
319+ extents_min : nv . volumeObject3D . extentsMin ,
320+ extents_max : nv . volumeObject3D . extentsMax ,
321+ scale : nv . volumeObject3D . scale ,
322+ furthest_vertex_from_origin :
323+ nv . volumeObject3D . furthestVertexFromOrigin ,
324+ field_of_view_de_oblique_mm : nv . volumeObject3D
325+ . fieldOfViewDeObliqueMM as number [ ] ,
326+ } ;
327+ model . set ( "_volume_object_3d_data" , currentVolumeObject3D ) ;
328+ model . save_changes ( ) ;
329+ console . log ( "_volume_object_3d_data set" ) ;
330+ }
331+ } ,
332+ } ) ;
333+
336334 nv . onImageLoaded = async ( volume : niivue . NVImage ) => {
337335 // Check if the volume is already in the backend
338336 const volumeID = volume . id ;
@@ -652,7 +650,7 @@ export default {
652650 if ( ! nv ) {
653651 console . log ( "Creating new Niivue instance" ) ;
654652 const serializedOpts = model . get ( "opts" ) ?? { } ;
655- const opts = deserializeOptions ( serializedOpts ) ;
653+ const opts = lib . deserializeOptions ( serializedOpts ) ;
656654 nv = new niivue . Niivue ( opts ) ;
657655 }
658656
0 commit comments