Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/227.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remote rendering 5.2d - Batch visibility and selection requests from client
34 changes: 33 additions & 1 deletion src/ansys/visor/viewer/app/trame/scene_trigger_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import urllib.parse
from abc import ABC, abstractmethod
from logging import Logger
from typing import List, Optional, Protocol
from typing import List, Optional, Protocol, Sequence

from pydantic import BaseModel, Field, ValidationError
from trame.app.core import Server
Expand All @@ -16,6 +16,12 @@
from ansys.visor.viewer.models.runtime.requests.geometry_pick_payloads import (
SetGeometryHighlightPayload,
)
from ansys.visor.viewer.models.runtime.requests.part_batch_payloads import (
PartSelection,
PartVisibility,
SetPartsSelectedPayload,
SetPartsVisibilityPayload,
)
from ansys.visor.viewer.models.runtime.requests.sync_camera_payload import SyncCameraPayload
from ansys.visor.viewer.models.runtime.requests.variable_range_payload import SetVariableRangePayload
from ansys.visor.viewer.models.runtime.requests.widget_state_payloads import (
Expand Down Expand Up @@ -55,6 +61,10 @@ def set_part_diffuse_color(self, node_id: int, diffuse_rgb: Optional[List[float]

def set_part_selected(self, node_id: int, selected: bool) -> None: ...

def set_parts_visibility(self, visibilities: Sequence[PartVisibility]) -> None: ...

def set_parts_selected(self, selections: Sequence[PartSelection]) -> None: ...

def set_part_color_variable(
self,
node_id: int,
Expand Down Expand Up @@ -358,6 +368,28 @@ def set_part_selected(self, payload) -> None:
return
api.set_part_selected(payload.node_id, payload.selected)

@trigger("set_parts_visibility")
@parse_payload(SetPartsVisibilityPayload)
def set_parts_visibility(self, payload) -> None:
"""Frontend -> Backend: set whether each of several parts is visible, in one call."""
api = self._mutation_api("set_parts_visibility", payload)
if api is None:
return
api.set_parts_visibility(payload.parts)

@trigger("set_parts_selected")
@parse_payload(SetPartsSelectedPayload)
def set_parts_selected(self, payload) -> None:
"""Frontend -> Backend: select or deselect several parts in one call.

No colour crosses this trigger: the server reads each part's stored
diffuse colour from its own record.
"""
api = self._mutation_api("set_parts_selected", payload)
if api is None:
return
api.set_parts_selected(payload.parts)

@trigger("set_part_color_variable")
@parse_payload(SetPartColorVariablePayload)
def set_part_color_variable(self, payload) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Models for the batched per-part trigger payloads.

A batch carries one absolute target value per part, so the server applies the
whole batch in one critical section and presents it in one frame. One element
that does not validate rejects the whole batch.
"""

from typing import List

from pydantic import BaseModel, ConfigDict, Field


class PartSelection(BaseModel):
"""One part's target selection state within a ``set_parts_selected`` batch."""

model_config = ConfigDict(populate_by_name=True)

node_id: int = Field(alias="nodeId")
selected: bool


class SetPartsSelectedPayload(BaseModel):
"""
Payload of the ``set_parts_selected`` trigger.

No colour crosses this trigger: the server reads each part's stored
diffuse colour from its own record.
"""

model_config = ConfigDict(populate_by_name=True)

parts: List[PartSelection]


class PartVisibility(BaseModel):
"""One part's target visibility within a ``set_parts_visibility`` batch."""

model_config = ConfigDict(populate_by_name=True)

node_id: int = Field(alias="nodeId")
visible: bool


class SetPartsVisibilityPayload(BaseModel):
"""Payload of the ``set_parts_visibility`` trigger."""

model_config = ConfigDict(populate_by_name=True)

parts: List[PartVisibility]
62 changes: 53 additions & 9 deletions src/ansys/visor/viewer/vtk/scene/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, List
from typing import Dict, List, Sequence

from trame_server import Server

Expand All @@ -22,6 +22,10 @@
from ansys.visor.viewer.models.persist.persisted_viewer_state import PersistedViewerStateV1
from ansys.visor.viewer.models.runtime.dataset.runtime_dataset_state import RuntimePartProperties
from ansys.visor.viewer.models.runtime.picking.geometry_pick import GeometryPick
from ansys.visor.viewer.models.runtime.requests.part_batch_payloads import (
PartSelection,
PartVisibility,
)
from ansys.visor.viewer.models.runtime.scene.runtime_app_state import RuntimeAppState
from ansys.visor.viewer.models.runtime.scene.runtime_scene_state import RuntimeSceneState
from ansys.visor.viewer.models.runtime.scene.runtime_ui_state import RuntimeUIState
Expand Down Expand Up @@ -748,13 +752,39 @@ def set_part_visibility(self, node_id: int, visible: bool) -> None:
bounds semantics, not a defect in this fan-out.
"""
with self._vtk_lock:
if not self._dataset_registry.set_part_visibility(node_id, visible):
logger.debug("set_part_visibility: no dataset owns node %s; skipping.", node_id)
if not self._apply_part_visibility(node_id, visible):
return
self._renderer.apply_visibility(node_id, visible)
self._update_widget_bounds()
self._update_actor_count()

def set_parts_visibility(self, visibilities: Sequence[PartVisibility]) -> None:
"""Set the visibility of several parts in one critical section.

Each element is applied as :meth:`set_part_visibility` applies one
part; an unresolvable node id is a logged skip, not a stop. The widget
fan-out runs once after the loop, and only when at least one part was
applied.
"""
with self._vtk_lock:
applied = False
for part in visibilities:
applied = self._apply_part_visibility(part.node_id, part.visible) or applied
if not applied:
return
self._update_widget_bounds()
self._update_actor_count()

def _apply_part_visibility(self, node_id: int, visible: bool) -> bool:
"""Write one part's visibility record and apply it; the caller holds the lock.

Returns ``False``, after a debug log, when no dataset owns *node_id*.
"""
if not self._dataset_registry.set_part_visibility(node_id, visible):
logger.debug("set_part_visibility: no dataset owns node %s; skipping.", node_id)
return False
self._renderer.apply_visibility(node_id, visible)
return True

def set_part_opacity(self, node_id: int, opacity: float) -> None:
"""Set the opacity of the part identified by *node_id*."""
with self._vtk_lock:
Expand Down Expand Up @@ -795,11 +825,25 @@ def set_part_selected(self, node_id: int, selected: bool) -> None:
``get_part_state`` cannot return ``None`` at this point.
"""
with self._vtk_lock:
if not self._dataset_registry.set_part_selected(node_id, selected):
logger.debug("set_part_selected: no dataset owns node %s; skipping.", node_id)
return
diffuse_rgb = self._dataset_registry.get_part_state(node_id).diffuse_rgb
self._renderer.apply_selected(node_id, selected, diffuse_rgb)
self._apply_part_selected(node_id, selected)

def set_parts_selected(self, selections: Sequence[PartSelection]) -> None:
"""Select or deselect several parts in one critical section.

Each element is applied as :meth:`set_part_selected` applies one part;
an unresolvable node id is a logged skip, not a stop.
"""
with self._vtk_lock:
for part in selections:
self._apply_part_selected(part.node_id, part.selected)

def _apply_part_selected(self, node_id: int, selected: bool) -> None:
"""Write one part's selection record and apply it; the caller holds the lock."""
if not self._dataset_registry.set_part_selected(node_id, selected):
logger.debug("set_part_selected: no dataset owns node %s; skipping.", node_id)
return
diffuse_rgb = self._dataset_registry.get_part_state(node_id).diffuse_rgb
self._renderer.apply_selected(node_id, selected, diffuse_rgb)

def set_part_color_variable(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,17 @@ export const Panel_TopLeft: FC<{
const treeViewContainer = document.getElementById(treeViewContainerId) as HTMLDivElement;
treeViewUtil.current.addSelectionChangeListener((selectedNodes) => {
(async () => {
// One call per part with its target value; a part whose value
// is unchanged sends nothing.
const selected = new Set(selectedNodes);
for (const node of visorState.sceneGraph.descendantActorNodesOrSelfArray) {
await node.setSelectedAsync(selected.has(node));
}
// Each part is applied locally in turn, and the parts that
// changed reach the server as one batch.
await visorState.sceneGraph.setSelectedPartsAsync(new Set(selectedNodes));
await visorState.render();
})();
});
treeViewUtil.current.addVisibilityChangeListener((visibleNodes) => {
(async () => {
// One call per part with its target value; a part whose value
// is unchanged sends nothing.
const visible = new Set(visibleNodes);
for (const node of visorState.sceneGraph.descendantActorNodesOrSelfArray) {
await node.setVisibilityAsync(visible.has(node));
}
// Each part is applied locally in turn, and the parts that
// changed reach the server as one batch.
await visorState.sceneGraph.setVisiblePartsAsync(new Set(visibleNodes));
await visorState.render();
})();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,38 +429,36 @@ export const Panel_TopRight: FC<{
if (visorState.getSelectionMode() !== 'part') {
return;
}
if (!ctrlKey && !shiftKey) {
const promises: Promise<void>[] = [];
const replace = !ctrlKey && !shiftKey;
if (replace) {
for (const row of treeViewUtil.rows) {
row.deselectRow(false);
const promise = row.node.setSelectedAsync(false);
promises.push(promise);
}
await Promise.all(promises);
}
if (node != null) {
const row = treeViewUtil.rowUtilsMap.get(node.id)!;
if (node.selected) {
row.deselectRow(false);
await node.setSelectedAsync(false);
} else {
if (replace || !node.selected) {
row.selectRow(false);
await node.setSelectedAsync(true);
} else {
row.deselectRow(false);
}
}
treeViewUtil.updateSelectedNodesArray();
// The parts that changed reach the server as one batch, as
// from the tree.
await visorState.sceneGraph.setSelectedPartsAsync(
new Set(treeViewUtil.selectedNodes)
);
await onSelectionChangeAsync(treeViewUtil.selectedNodes);
});

// Reset part selection when switching away from 'part' mode
visorState.addSelectionModeChangedListener(async (_mode) => {
const promises: Promise<void>[] = [];
for (const row of treeViewUtil.rows) {
row.deselectRow(false);
promises.push(row.node.setSelectedAsync(false));
}
await Promise.all(promises);
treeViewUtil.updateSelectedNodesArray();
await visorState.sceneGraph.setSelectedPartsAsync(new Set());
await onSelectionChangeAsync([]);
await visorState.render();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function makeRendererDouble() {
sendPartOpacityAsync: jest.fn(async () => undefined),
setSelectedAsync: jest.fn(async () => undefined),
sendPartSelectedAsync: jest.fn(async () => undefined),
sendPartsVisibilityAsync: jest.fn(async () => undefined),
sendPartsSelectedAsync: jest.fn(async () => undefined),
setDiffuseColorRgbAsync: jest.fn(async () => undefined),
sendPartDiffuseColorAsync: jest.fn(async () => undefined),
setColorVariableAsync: jest.fn(async () => undefined),
Expand Down Expand Up @@ -228,12 +230,14 @@ describe('the client applies delivered state as given', () => {
});
});

/** How many times each of the six per-part send members was called. */
/** How many times each of the eight per-part send members was called. */
function sendCallCounts(renderer: ReturnType<typeof makeRendererDouble>) {
return {
sendPartVisibilityAsync: renderer.sendPartVisibilityAsync.mock.calls.length,
sendPartOpacityAsync: renderer.sendPartOpacityAsync.mock.calls.length,
sendPartSelectedAsync: renderer.sendPartSelectedAsync.mock.calls.length,
sendPartsVisibilityAsync: renderer.sendPartsVisibilityAsync.mock.calls.length,
sendPartsSelectedAsync: renderer.sendPartsSelectedAsync.mock.calls.length,
sendPartDiffuseColorAsync: renderer.sendPartDiffuseColorAsync.mock.calls.length,
sendPartColorVariableAsync: renderer.sendPartColorVariableAsync.mock.calls.length,
sendClearPartColorVariableAsync: renderer.sendClearPartColorVariableAsync.mock.calls.length,
Expand All @@ -244,6 +248,8 @@ const NO_SENDS = {
sendPartVisibilityAsync: 0,
sendPartOpacityAsync: 0,
sendPartSelectedAsync: 0,
sendPartsVisibilityAsync: 0,
sendPartsSelectedAsync: 0,
sendPartDiffuseColorAsync: 0,
sendPartColorVariableAsync: 0,
sendClearPartColorVariableAsync: 0,
Expand Down
Loading
Loading