Skip to content

Commit 32f511f

Browse files
feat: [Remote rendering 3.5b] client uses server owned variables (#153)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent 15563b0 commit 32f511f

12 files changed

Lines changed: 1282 additions & 662 deletions

‎doc/changelog.d/153.added.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Remote rendering 3.5b] client uses server owned variables

‎src/ansys/visor/visor-client/src/App.tsx‎

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,50 +113,59 @@ function App() {
113113

114114
wasmView.current.clearObserversAndEventListeners();
115115
const oldFrontend: VisorFrontend = (window as any).__visorState;
116-
const sceneDetailsJson = await wasmView.current.trameTriggerAsync(
117-
'get_visor_scene_details_json'
118-
);
119-
let sceneDetails: VisorSceneDetails;
116+
let newFrontend: VisorFrontend;
117+
// The listeners cleared above are registered again however this
118+
// block ends, so a failed rebuild still receives the next server
119+
// update, and `set_state` keeps reaching the frontend named by
120+
// `__visorState`, which is switched only after the first pass.
120121
try {
121-
sceneDetails = new VisorSceneDetails(sceneDetailsJson);
122-
} catch (err) {
123-
if (err instanceof SchemaVersionMismatchError) {
124-
setSchemaMismatchError(err);
125-
return;
122+
const sceneDetailsJson = await wasmView.current.trameTriggerAsync(
123+
'get_visor_scene_details_json'
124+
);
125+
let sceneDetails: VisorSceneDetails;
126+
try {
127+
sceneDetails = new VisorSceneDetails(sceneDetailsJson);
128+
} catch (err) {
129+
if (err instanceof SchemaVersionMismatchError) {
130+
setSchemaMismatchError(err);
131+
return;
132+
}
133+
throw err;
134+
}
135+
const renderer = await WasmRenderer.createAsync(
136+
wasmView.current.vtkScene,
137+
requireWasmAnnotation(sceneDetails.vtkInfo.rendererAnnotation),
138+
wasmView.current.trameTriggerAsync
139+
);
140+
newFrontend = new VisorFrontend(
141+
renderer,
142+
sceneDetails.vtkInfo.sceneGraph,
143+
wasmView.current.trameTriggerAsync
144+
);
145+
// Release the frontend being replaced, here and not later: the
146+
// `VtkScene` behind both renderers is the same object across a
147+
// rebuild, so an unreleased subscription stays live and the next
148+
// gesture is reported once per surviving frontend. There is
149+
// deliberately no `await` between the new frontend subscribing (the
150+
// constructor above) and the old one releasing, so the window in
151+
// which two subscriptions coexist contains no suspension point.
152+
if (oldFrontend != null) {
153+
oldFrontend.releaseCameraSettledListener();
126154
}
127-
throw err;
128-
}
129-
const renderer = await WasmRenderer.createAsync(
130-
wasmView.current.vtkScene,
131-
requireWasmAnnotation(sceneDetails.vtkInfo.rendererAnnotation),
132-
wasmView.current.trameTriggerAsync
133-
);
134-
const newFrontend = new VisorFrontend(
135-
renderer,
136-
sceneDetails.vtkInfo.sceneGraph,
137-
wasmView.current.trameTriggerAsync
138-
);
139-
// Release the frontend being replaced, here and not later: the
140-
// `VtkScene` behind both renderers is the same object across a
141-
// rebuild, so an unreleased subscription stays live and the next
142-
// gesture is reported once per surviving frontend. There is
143-
// deliberately no `await` between the new frontend subscribing (the
144-
// constructor above) and the old one releasing, so the window in
145-
// which two subscriptions coexist contains no suspension point.
146-
if (oldFrontend != null) {
147-
oldFrontend.releaseCameraSettledListener();
148-
}
149155

150-
if (visorArgs.current.darkMode != null) {
151-
// Explicit Dash prop takes precedence over the server's dark_mode value.
152-
sceneDetails.appState.ui.setDarkTheme(visorArgs.current.darkMode);
156+
if (visorArgs.current.darkMode != null) {
157+
// Explicit Dash prop takes precedence over the server's dark_mode value.
158+
sceneDetails.appState.ui.setDarkTheme(visorArgs.current.darkMode);
159+
}
160+
await newFrontend.setAppStateAsync(sceneDetails.appState, false);
161+
(window as any).__visorState = newFrontend;
162+
setSchemaMismatchError(null);
163+
setVisorFrontend(newFrontend); // rebuild/remount React UI
164+
} finally {
165+
wasmView.current.addServerUpdatedListener(onServerUpdateAsync);
166+
wasmView.current.addGetStateListener(onGetState);
167+
wasmView.current.addSetStateListener(onSetStateAsync);
153168
}
154-
(window as any).__visorState = newFrontend;
155-
await newFrontend.setAppStateAsync(sceneDetails.appState, false);
156-
setVisorFrontend(newFrontend); // rebuild/remount React UI
157-
wasmView.current.addServerUpdatedListener(onServerUpdateAsync);
158-
wasmView.current.addGetStateListener(onGetState);
159-
wasmView.current.addSetStateListener(onSetStateAsync);
160169
if (oldFrontend != null) {
161170
const oldAppState = await oldFrontend.getAppStateAsync();
162171
await newFrontend.setAppStateAsync(oldAppState);

‎src/ansys/visor/visor-client/src/VisorFrontend.tsx‎

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { getPromiseResolver } from './utils/JsHelpers';
44
import VisorAppState from './state/appstate/VisorAppState.tsx';
55
import VisorDatasetState from './state/appstate/VisorDatasetState.tsx';
66
import VisorPartState from './state/appstate/VisorPartState.tsx';
7-
import VisorVariableState from './state/appstate/VisorVariableState.tsx';
87
import { TreeViewUtil } from './treeview/TreeView.tsx';
98
import { StateInput } from './state/appstate/VisorStateCommon.tsx';
109
import VisorVtkSceneNode from './state/appstate/vtkInfo/VisorVtkSceneNode.tsx';
@@ -66,7 +65,6 @@ export class VisorFrontend {
6665
// Respect base path prefix set by the Dash component (for reverse-proxy deployments).
6766
const bp: string = ((window as any).__visorArgs?.basePath ?? '').replace(/\/$/, '');
6867

69-
const self = this;
7068
this.globalVariableCollection = variableManager.globalVariableCollection;
7169
this.#unit = '';
7270
this.darkMode = darkMode;
@@ -100,6 +98,18 @@ export class VisorFrontend {
10098
remover?.();
10199
};
102100
this.defaultActorColor = [];
101+
// Reports one variable slot's range; errors are logged, not rethrown,
102+
// since the client already applied the range locally.
103+
this.sendVariableRangeAsync = async (variableId, component, min, max) => {
104+
try {
105+
await triggerSender('set_variable_range', { variableId, component, min, max });
106+
} catch (err) {
107+
console.error(
108+
`[VISOR] variable range trigger send failed: variableId='${variableId}' component=${component}`,
109+
err
110+
);
111+
}
112+
};
103113
this.setVariableRangeAsync = async (variableId, component, min, max) => {
104114
const variable = variableManager.globalVariableCollection.getVariable(variableId);
105115
if (variable == null) {
@@ -114,6 +124,7 @@ export class VisorFrontend {
114124
await actorNode.setScalarRangeAsync(min, max);
115125
}
116126
}
127+
await this.sendVariableRangeAsync(variableId, component, min, max);
117128
};
118129
this.addCameraChangedListener = (callback) => {
119130
return renderer.addCameraChangedListener(callback);
@@ -305,29 +316,9 @@ export class VisorFrontend {
305316
}
306317
sceneState.copyDataset(datasetState);
307318
}
308-
const variableInfos = variableManager.globalVariableCollection;
309-
for (const variableInfo of variableInfos.array) {
310-
const variableState = new VisorVariableState();
311-
variableState.setId(variableInfo.id.toString());
312-
variableState.setArrayName(variableInfo.name);
313-
variableState.setType(variableInfo.type);
314-
variableState.setNumComponents(variableInfo.numComponents);
315-
const magnitudeRange = variableInfo.getRangeInfo(-1);
316-
if (magnitudeRange == null) {
317-
throw new Error(`range at component ${-1} not found`);
318-
}
319-
variableState.setMagnitudeRange(magnitudeRange.customRange);
320-
const ranges: number[][] = [];
321-
for (let i = 0; i < variableInfo.numComponents; i++) {
322-
const range = variableInfo.getRangeInfo(i);
323-
if (range == null) {
324-
throw new Error(`range at component ${i} not found`);
325-
}
326-
ranges.push(range.customRange);
327-
}
328-
variableState.setRanges(ranges);
329-
sceneState.copyVariable(variableState);
330-
}
319+
// No variable block: the server owns the variable records and
320+
// delivers them on every push. Carrying a copy across a rebuild
321+
// would replace the delivered records with this one.
331322
return appState;
332323
};
333324
this.setAppStateAsync = async (state, updateUI) => {
@@ -460,6 +451,13 @@ export class VisorFrontend {
460451
const promise = renderer.setCrossSectionNormalAsync(crossSectionState.normal);
461452
promises.push(promise);
462453
}
454+
// Held before the part loop: a part coloured below reads its
455+
// range from these records. Guarded on presence, so a state with
456+
// no variable block leaves the held records alone, while an empty
457+
// block clears them.
458+
if (sceneState.hasVariableStates) {
459+
variableManager.setRecords(sceneState.getVariableStates());
460+
}
463461
for (const dataset_state of sceneState.getDatasetStates()) {
464462
for (const part_state of dataset_state.getPartStates()) {
465463
let node;
@@ -522,24 +520,7 @@ export class VisorFrontend {
522520
}
523521
}
524522
}
525-
// wait for all the parts to be updated before updating the variable ranges
526-
await Promise.all(promises);
527-
promises.length = 0;
528-
for (const variable_state of sceneState.getVariableStates()) {
529-
const idStr = variable_state.id;
530-
if (variable_state.magnitudeRange !== undefined) {
531-
const range = variable_state.magnitudeRange;
532-
const promise = self.setVariableRangeAsync(idStr, -1, range[0], range[1]);
533-
promises.push(promise);
534-
}
535-
for (let i = 0; i < variable_state.ranges.length; i++) {
536-
const range = variable_state.ranges[i];
537-
if (range !== undefined) {
538-
const promise = self.setVariableRangeAsync(idStr, i, range[0], range[1]);
539-
promises.push(promise);
540-
}
541-
}
542-
}
523+
// Every part is applied before the tree and the panel re-read them.
543524
await Promise.all(promises);
544525
if (updateUI) {
545526
await panelTopRightUtilPromise;
@@ -549,6 +530,13 @@ export class VisorFrontend {
549530
// above; the tree derives its rows from them here.
550531
treeViewUtil.synchronize();
551532
}
533+
// After the tree has taken the delivered selection, so the panel
534+
// re-reads that selection's ranges from the records just held.
535+
// Skipped until the panel exists; its mount reads them itself.
536+
if (isPanelTopRightUtilSet()) {
537+
const panelTopRight = await panelTopRightUtilPromise;
538+
await panelTopRight.refreshSelectionAsync();
539+
}
552540
await renderer.resizeAsync();
553541
};
554542
Object.freeze(this);
@@ -587,6 +575,14 @@ export class VisorFrontend {
587575
toggleFullScreenAsync: () => Promise<void>;
588576
addCameraChangedListener: (callback: (cameraState: VisorCameraState) => void) => () => void;
589577
globalVariableCollection: VisorVariableCollection;
578+
/** Reports one variable slot's range to the server, -1 being the magnitude; a failed send is logged, not rethrown. */
579+
sendVariableRangeAsync: (
580+
variableId: string,
581+
component: number,
582+
min: number,
583+
max: number
584+
) => Promise<void>;
585+
/** Writes a variable slot's range to the held record, applies it to every part coloured by that slot, then reports it to the server. */
590586
setVariableRangeAsync: (
591587
variableId: string,
592588
component: number,

‎src/ansys/visor/visor-client/src/components/ui-panels/Panel_TopRight.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ export const Panel_TopRight: FC<{
493493
legendTabElem.click();
494494
}
495495
},
496-
() => tabIndex
496+
() => tabIndex,
497+
() => onSelectionChangeAsync(treeViewUtil.selectedNodes)
497498
);
498499
visorState.setPanelTopRightUtil(util);
499500
// Must stay on the line after the util handoff: it suppresses the three mount writes

‎src/ansys/visor/visor-client/src/components/ui-panels/Panel_TopRight_Util.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export class Panel_TopRight_Util {
77
getIsPanelCollapsed: () => boolean,
88
getIsLegendCollapsed: () => boolean,
99
selectTab: (tabIndex: number) => void,
10-
getTabIndex: () => number
10+
getTabIndex: () => number,
11+
refreshSelectionAsync: () => Promise<void>
1112
) {
1213
this.expandPanel = expandPanel;
1314
this.collapsePanel = collapsePanel;
@@ -17,6 +18,7 @@ export class Panel_TopRight_Util {
1718
this.#getIsLegendCollapsed = getIsLegendCollapsed;
1819
this.selectTab = selectTab;
1920
this.#getTabIndex = getTabIndex;
21+
this.refreshSelectionAsync = refreshSelectionAsync;
2022
}
2123

2224
readonly collapsePanel: () => void;
@@ -40,4 +42,7 @@ export class Panel_TopRight_Util {
4042
get tabIndex() {
4143
return this.#getTabIndex();
4244
}
45+
46+
/** Re-reads the current selection into the properties and legend panels. */
47+
readonly refreshSelectionAsync: () => Promise<void>;
4348
}

0 commit comments

Comments
 (0)