diff --git a/opengeodeweb_viewer_schemas.json b/opengeodeweb_viewer_schemas.json index ae66e43c..b5b33d3a 100644 --- a/opengeodeweb_viewer_schemas.json +++ b/opengeodeweb_viewer_schemas.json @@ -5487,6 +5487,162 @@ "additionalProperties": false } }, + "set_highlight_colors": { + "$id": "opengeodeweb_viewer.viewer.set_highlight_colors", + "rpc": "set_highlight_colors", + "type": "object", + "properties": { + "color": { + "title": "ColorRGB", + "type": "object", + "properties": { + "red": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "green": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "blue": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + }, + "required": [ + "red", + "green", + "blue" + ], + "additionalProperties": false + }, + "outline_color": { + "title": "ColorRGB", + "type": "object", + "properties": { + "red": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "green": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "blue": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + }, + "required": [ + "red", + "green", + "blue" + ], + "additionalProperties": false + }, + "surfaces_color": { + "title": "ColorRGB", + "type": "object", + "properties": { + "red": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "green": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "blue": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + }, + "required": [ + "red", + "green", + "blue" + ], + "additionalProperties": false + }, + "lines_color": { + "title": "ColorRGB", + "type": "object", + "properties": { + "red": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "green": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "blue": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + }, + "required": [ + "red", + "green", + "blue" + ], + "additionalProperties": false + }, + "corners_color": { + "title": "ColorRGB", + "type": "object", + "properties": { + "red": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "green": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "blue": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + }, + "required": [ + "red", + "green", + "blue" + ], + "additionalProperties": false + } + }, + "required": [ + "color", + "outline_color", + "surfaces_color", + "lines_color", + "corners_color" + ], + "additionalProperties": false, + "response": { + "type": "object", + "properties": {}, + "required": [], + "additionalProperties": false + } + }, "set_background_color": { "$id": "opengeodeweb_viewer.viewer.set_background_color", "rpc": "set_background_color", diff --git a/opengeodeweb_viewer_typed_schemas.d.ts b/opengeodeweb_viewer_typed_schemas.d.ts index da71c1b6..0108207a 100644 --- a/opengeodeweb_viewer_typed_schemas.d.ts +++ b/opengeodeweb_viewer_typed_schemas.d.ts @@ -1036,6 +1036,17 @@ export type Marker = "axes" | "compass" | "none"; export interface ViewerSetOrientationWidgetResponse { } +export interface ViewerSetHighlightColorsParams { + color: ColorRGB; + corners_color: ColorRGB; + lines_color: ColorRGB; + outline_color: ColorRGB; + surfaces_color: ColorRGB; +} + +export interface ViewerSetHighlightColorsResponse { +} + export interface ViewerSetBackgroundColorParams { color: ColorRGB; } @@ -1401,6 +1412,7 @@ export interface Schemas { readonly set_z_scaling: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_z_scaling"] & TypedSchema; readonly set_text_style: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_text_style"] & TypedSchema; readonly set_orientation_widget: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_orientation_widget"] & TypedSchema; + readonly set_highlight_colors: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_highlight_colors"] & TypedSchema; readonly set_background_color: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_background_color"] & TypedSchema; readonly ruler: (typeof json)["opengeodeweb_viewer"]["viewer"]["ruler"] & TypedSchema; readonly reset_visualization: (typeof json)["opengeodeweb_viewer"]["viewer"]["reset_visualization"] & TypedSchema; diff --git a/requirements.txt b/requirements.txt index 64bd7093..0e8e27eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -61,4 +61,3 @@ wslink==2.6.0 yarl==1.* # via aiohttp -opengeodeweb-microservice==1.*,>=1.5.0 diff --git a/src/opengeodeweb_viewer/object/object_methods.py b/src/opengeodeweb_viewer/object/object_methods.py index 98c8a08b..b45c07b8 100644 --- a/src/opengeodeweb_viewer/object/object_methods.py +++ b/src/opengeodeweb_viewer/object/object_methods.py @@ -1,13 +1,19 @@ import logging +from dataclasses import dataclass from pathlib import Path from typing import TYPE_CHECKING, Any # Third party imports -from vtkmodules.vtkCommonCore import VTK_COLOR_MODE_DIRECT_SCALARS, reference +from vtkmodules.vtkCommonCore import ( + VTK_COLOR_MODE_DIRECT_SCALARS, + reference, + vtkUnsignedCharArray, +) from vtkmodules.vtkCommonDataModel import ( vtkDataSet, vtkMultiBlockDataSet, ) +from vtkmodules.vtkFiltersCore import vtkAppendDataSets from vtkmodules.vtkRenderingAnnotation import vtkScalarBarActor from vtkmodules.vtkRenderingCore import ( vtkActor, @@ -17,12 +23,15 @@ ) # Local application imports -from opengeodeweb_viewer.vtk_pipeline import VtkPipeline +from opengeodeweb_viewer.vtk_pipeline import HighlightPipeline, VtkPipeline from opengeodeweb_viewer.vtk_protocol import VtkView if TYPE_CHECKING: + from collections.abc import Iterable, Mapping + from vtkmodules.vtkIOXML import vtkXMLReader + from opengeodeweb_viewer.schema_types import ColorRGB from opengeodeweb_viewer.utils_functions import ColorClassProtocol from opengeodeweb_viewer.vtk_pipeline import ColorMapKey @@ -32,6 +41,140 @@ EDGES_DEPTH_FACTOR_SHARE = 0.2 +RGB_MAX = 255 + +Rgb = tuple[float, float, float] + + +@dataclass +class HighlightColors: + """The highlight colors of a session, VTK RGB (0-1).""" + + # Some components, a picked cell or point + color: Rgb + # The outline of a picked cell + outline_color: Rgb + # A whole model highlighted shows each component type in its own color; blocks take surfaces' + surfaces_color: Rgb + lines_color: Rgb + corners_color: Rgb + + +def vtk_rgb(color: ColorRGB) -> Rgb: + return (color.red / RGB_MAX, color.green / RGB_MAX, color.blue / RGB_MAX) + + +_TEAL = (60 / RGB_MAX, 153 / RGB_MAX, 131 / RGB_MAX) +_DARK_TEAL = (31 / RGB_MAX, 89 / RGB_MAX, 77 / RGB_MAX) +# Without colors set by the app, the viewer stays brand-neutral +DEFAULT_HIGHLIGHT_COLORS = HighlightColors( + color=_TEAL, + outline_color=_DARK_TEAL, + surfaces_color=_TEAL, + lines_color=_DARK_TEAL, + corners_color=_DARK_TEAL, +) + + +class _SessionHighlight: + """The highlight colors set by the app for this viewer, else the defaults.""" + + colors = DEFAULT_HIGHLIGHT_COLORS + + +def set_highlight_colors(colors: HighlightColors) -> None: + _SessionHighlight.colors = colors + + +def reset_highlight_colors() -> None: + _SessionHighlight.colors = DEFAULT_HIGHLIGHT_COLORS + + +def apply_highlight_colors(actor: vtkActor) -> None: + """Give actor the session highlight colors.""" + prop = actor.GetProperty() + prop.SetColor(*_SessionHighlight.colors.color) + prop.SetEdgeColor(*_SessionHighlight.colors.outline_color) + + +def _component_type_color(component_type: str) -> Rgb: + colors = _SessionHighlight.colors + return { + "surfaces": colors.surfaces_color, + "lines": colors.lines_color, + "corners": colors.corners_color, + }.get(component_type, colors.surfaces_color) + + +HIGHLIGHT_COLOR_ARRAY = "highlight_color" + + +def apply_highlight_style(actor: vtkActor, mapper: vtkDataSetMapper) -> None: + mapper.ScalarVisibilityOff() + mapper.SetResolveCoincidentTopologyToPolygonOffset() + mapper.SetRelativeCoincidentTopologyPolygonOffsetParameters(-2, -2) + prop = actor.GetProperty() + prop.SetLineWidth(4) + prop.SetPointSize(15) + prop.RenderPointsAsSpheresOn() + prop.LightingOff() + prop.EdgeVisibilityOn() + apply_highlight_colors(actor) + actor.SetMapper(mapper) + actor.VisibilityOff() + actor.UseBoundsOff() + + +def is_whole_model(block_ids: Iterable[int], component_types: Mapping[int, str]) -> bool: + """Whether the highlighted blocks are every component of the model.""" + return bool(component_types) and component_types.keys() <= set(block_ids) + + +def _show_whole_components(highlight: HighlightPipeline, append: vtkAppendDataSets) -> None: + """Outlining each cell of whole components would hide their color.""" + append.Update() + highlight.mapper.SetInputDataObject(append.GetOutput()) + highlight.actor.GetProperty().EdgeVisibilityOff() + + +def highlight_whole_components(highlight: HighlightPipeline, blocks: Iterable[vtkDataSet]) -> None: + """Show some components of a model in the highlight color.""" + append = vtkAppendDataSets() + for block in blocks: + append.AddInputData(block) + highlight.mapper.ScalarVisibilityOff() + _show_whole_components(highlight, append) + + +def _with_cell_color(block: vtkDataSet, color: tuple[float, float, float]) -> vtkDataSet: + """A shallow copy of block whose cells all have color, the block itself left untouched.""" + colored = block.NewInstance() + colored.ShallowCopy(block) + colors = vtkUnsignedCharArray() + colors.SetName(HIGHLIGHT_COLOR_ARRAY) + colors.SetNumberOfComponents(3) + colors.SetNumberOfTuples(colored.GetNumberOfCells()) + for component, value in enumerate(color): + colors.FillComponent(component, round(value * RGB_MAX)) + colored.GetCellData().AddArray(colors) + return colored + + +def highlight_whole_model( + highlight: HighlightPipeline, typed_blocks: Iterable[tuple[vtkDataSet, str]] +) -> None: + """Show every component of a model, each in the color of its component type.""" + append = vtkAppendDataSets() + for block, component_type in typed_blocks: + append.AddInputData(_with_cell_color(block, _component_type_color(component_type))) + mapper = highlight.mapper + mapper.ScalarVisibilityOn() + mapper.SetScalarModeToUseCellFieldData() + mapper.SelectColorArray(HIGHLIGHT_COLOR_ARRAY) + mapper.SetColorModeToDirectScalars() + _show_whole_components(highlight, append) + + class VtkObjectView(VtkView): def __init__(self) -> None: super().__init__() @@ -410,25 +553,9 @@ def clear_colors(self, data_id: str) -> None: scalar_bar.actor.VisibilityOff() self.update_scalar_bars_layout() - def _apply_highlight_style(self, actor: vtkActor, mapper: vtkDataSetMapper) -> None: - mapper.ScalarVisibilityOff() - mapper.SetResolveCoincidentTopologyToPolygonOffset() - mapper.SetRelativeCoincidentTopologyPolygonOffsetParameters(-2, -2) - prop = actor.GetProperty() - prop.SetColor(0.235, 0.6, 0.514) - prop.SetLineWidth(4) - prop.SetPointSize(15) - prop.RenderPointsAsSpheresOn() - prop.LightingOff() - prop.EdgeVisibilityOn() - prop.SetEdgeColor(0.12, 0.35, 0.30) - actor.SetMapper(mapper) - actor.VisibilityOff() - actor.UseBoundsOff() - def highlight(self, pipeline: VtkPipeline) -> None: highlight = pipeline.highlight - self._apply_highlight_style(highlight.actor, highlight.mapper) + apply_highlight_style(highlight.actor, highlight.mapper) if pipeline.filter.GetNumberOfInputConnections(0) == 0: pipeline.filter.SetInputConnection(pipeline.reader.GetOutputPort()) input_port = pipeline.filter.GetOutputPort() diff --git a/src/opengeodeweb_viewer/rpc/model/model_protocols.py b/src/opengeodeweb_viewer/rpc/model/model_protocols.py index 3e762160..052ac9c6 100644 --- a/src/opengeodeweb_viewer/rpc/model/model_protocols.py +++ b/src/opengeodeweb_viewer/rpc/model/model_protocols.py @@ -12,7 +12,6 @@ vtkDataSet, vtkMultiBlockDataSet, ) -from vtkmodules.vtkFiltersCore import vtkAppendDataSets from vtkmodules.vtkIOXML import vtkXMLMultiBlockDataReader from vtkmodules.vtkRenderingCore import ( vtkActor, @@ -21,7 +20,12 @@ ) # Local application imports -from opengeodeweb_viewer.object.object_methods import VtkObjectView +from opengeodeweb_viewer.object.object_methods import ( + VtkObjectView, + highlight_whole_components, + highlight_whole_model, + is_whole_model, +) from opengeodeweb_viewer.typed_rpc import typed_rpc from opengeodeweb_viewer.utils_functions import ( AttributeProtocol, @@ -537,14 +541,19 @@ def set_model_visibility(self, params: schemas.Visibility) -> schemas.Visibility def set_model_highlight(self, params: schemas.Highlight) -> schemas.HighlightResponse: pipeline = self.get_vtk_pipeline(params.id) if params.visibility and params.block_ids: - append = vtkAppendDataSets() - for i in params.block_ids: - block = pipeline.block_data_sets[i] if i < len(pipeline.block_data_sets) else None - if isinstance(block, vtkDataSet): - append.AddInputData(block) - append.Update() - pipeline.highlight.mapper.SetInputDataObject(append.GetOutput()) + component_types = pipeline.block_component_types() + blocks = [ + (block, component_types.get(i, "")) + for i in params.block_ids + if i < len(pipeline.block_data_sets) + and isinstance(block := pipeline.block_data_sets[i], vtkDataSet) + ] + if is_whole_model(params.block_ids, component_types): + highlight_whole_model(pipeline.highlight, blocks) + else: + highlight_whole_components(pipeline.highlight, [block for block, _ in blocks]) else: + pipeline.highlight.mapper.ScalarVisibilityOff() pipeline.highlight.mapper.SetInputConnection( pipeline.highlight.extract_selection.GetOutputPort() ) diff --git a/src/opengeodeweb_viewer/rpc/viewer/schemas/__init__.py b/src/opengeodeweb_viewer/rpc/viewer/schemas/__init__.py index e16c1379..28334dd3 100644 --- a/src/opengeodeweb_viewer/rpc/viewer/schemas/__init__.py +++ b/src/opengeodeweb_viewer/rpc/viewer/schemas/__init__.py @@ -7,6 +7,7 @@ from .set_z_scaling import * from .set_text_style import * from .set_orientation_widget import * +from .set_highlight_colors import * from .set_background_color import * from .ruler import * from .reset_visualization import * diff --git a/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.json b/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.json new file mode 100644 index 00000000..0acda2f6 --- /dev/null +++ b/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.json @@ -0,0 +1,35 @@ +{ + "rpc": "set_highlight_colors", + "type": "object", + "properties": { + "color": { + "title": "ColorRGB" + }, + "outline_color": { + "title": "ColorRGB" + }, + "surfaces_color": { + "title": "ColorRGB" + }, + "lines_color": { + "title": "ColorRGB" + }, + "corners_color": { + "title": "ColorRGB" + } + }, + "required": [ + "color", + "outline_color", + "surfaces_color", + "lines_color", + "corners_color" + ], + "additionalProperties": false, + "response": { + "type": "object", + "properties": {}, + "required": [], + "additionalProperties": false + } +} diff --git a/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.py b/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.py new file mode 100644 index 00000000..c8aed972 --- /dev/null +++ b/src/opengeodeweb_viewer/rpc/viewer/schemas/set_highlight_colors.py @@ -0,0 +1,35 @@ +from opengeodeweb_viewer.schema_types import ColorRGB +from pathlib import Path +from opengeodeweb_microservice.schemas import Route, load_schema +from dataclasses_json import DataClassJsonMixin +from opengeodeweb_microservice.schemas import print_dataclass +from dataclasses import dataclass + + +@dataclass +class SetHighlightColors(DataClassJsonMixin): + def __post_init__(self) -> None: + print_dataclass(self) + + color: ColorRGB + corners_color: ColorRGB + lines_color: ColorRGB + outline_color: ColorRGB + surfaces_color: ColorRGB + + +@dataclass +class SetHighlightColorsResponse(DataClassJsonMixin): + def __post_init__(self) -> None: + print_dataclass(self) + + pass + + +set_highlight_colors_route = Route( + schema=load_schema(Path(__file__)), + params=SetHighlightColors, + response=SetHighlightColorsResponse, +) + +__all__ = ["ColorRGB", "ColorRGB", "ColorRGB", "ColorRGB", "ColorRGB", "SetHighlightColors", "SetHighlightColorsResponse", "set_highlight_colors_route"] diff --git a/src/opengeodeweb_viewer/rpc/viewer/viewer_protocols.py b/src/opengeodeweb_viewer/rpc/viewer/viewer_protocols.py index bf200f16..3e17f8bf 100644 --- a/src/opengeodeweb_viewer/rpc/viewer/viewer_protocols.py +++ b/src/opengeodeweb_viewer/rpc/viewer/viewer_protocols.py @@ -36,6 +36,13 @@ ) from vtkmodules.vtkRenderingUI import vtkGenericRenderWindowInteractor +from opengeodeweb_viewer.object.object_methods import ( + HighlightColors, + apply_highlight_colors, + reset_highlight_colors, + set_highlight_colors, + vtk_rgb, +) from opengeodeweb_viewer.orientation_marker import NO_MARKER, build_marker, style_marker from opengeodeweb_viewer.rpc.viewer import schemas from opengeodeweb_viewer.rpc.viewer.schemas.explode import ExplodedBound @@ -67,6 +74,8 @@ def reset_visualization( renderer = render_window.GetRenderers().GetFirstRenderer() renderer.RemoveAllViewProps() self._clipping_plane_previews = [] + # A reset viewer draws its own highlight colors, until the app sets its own again + reset_highlight_colors() grid_scale = vtkCubeAxesActor() grid_scale.SetCamera(renderer.GetActiveCamera()) @@ -122,6 +131,25 @@ def set_background_color( renderer.SetBackground([color.red / 255, color.green / 255, color.blue / 255]) return schemas.SetBackgroundColorResponse() + @typed_rpc(viewer_prefix, schemas.set_highlight_colors_route) + def update_highlight_colors( + self, params: schemas.SetHighlightColors + ) -> schemas.SetHighlightColorsResponse: + set_highlight_colors( + HighlightColors( + color=vtk_rgb(params.color), + outline_color=vtk_rgb(params.outline_color), + surfaces_color=vtk_rgb(params.surfaces_color), + lines_color=vtk_rgb(params.lines_color), + corners_color=vtk_rgb(params.corners_color), + ) + ) + # Highlights of the data already loaded take the new colors too + for pipeline in self.get_data_base().values(): + apply_highlight_colors(pipeline.highlight.actor) + self.render(-1) + return schemas.SetHighlightColorsResponse() + @typed_rpc(viewer_prefix, schemas.set_text_style_route) def update_text_style(self, params: schemas.SetTextStyle) -> schemas.SetTextStyleResponse: color = params.color diff --git a/src/opengeodeweb_viewer/vtk_protocol.py b/src/opengeodeweb_viewer/vtk_protocol.py index 892dcf22..fd78b1a3 100644 --- a/src/opengeodeweb_viewer/vtk_protocol.py +++ b/src/opengeodeweb_viewer/vtk_protocol.py @@ -244,6 +244,10 @@ def update_highlight( pipeline.highlight.extract_selection.SetInputData(0, target_dataset) pipeline.highlight.extract_selection.Modified() pipeline.highlight.extract_selection.Update() + # The picked cell is outlined in the highlight color, even after a whole model or whole + # components were highlighted without edges or in their component type colors + pipeline.highlight.mapper.ScalarVisibilityOff() + pipeline.highlight.actor.GetProperty().EdgeVisibilityOn() pipeline.highlight.actor.VisibilityOn() def clear_highlights(self, data_ids: list[str]) -> None: diff --git a/tests/conftest.py b/tests/conftest.py index 5ec82428..4cdc23c3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,6 +18,7 @@ from xprocess import ProcessStarter, XProcess # type: ignore[import-untyped] from opengeodeweb_viewer.config import TestConfig +from opengeodeweb_viewer.object.object_methods import reset_highlight_colors from opengeodeweb_viewer.rpc.viewer.viewer_protocols import VtkViewerView if TYPE_CHECKING: @@ -183,6 +184,13 @@ class Starter(ProcessStarter): # type: ignore[misc] TEST_PROJECT_FOLDER_PATH = tempfile.mkdtemp(prefix="ogw_test_data_") +@pytest.fixture(autouse=True) +def default_highlight_colors() -> Generator[None]: + """Highlight colors set by a test are the session's: each test starts from the defaults.""" + yield + reset_highlight_colors() + + @pytest.fixture def server(xprocess: XProcess) -> Generator[ServerMonitor]: name, starter, monitor_class = HELPER.get_xprocess_args(TEST_PROJECT_FOLDER_PATH) diff --git a/tests/data/images/viewer/set_highlight_colors.jpeg b/tests/data/images/viewer/set_highlight_colors.jpeg new file mode 100644 index 00000000..4243c9fa Binary files /dev/null and b/tests/data/images/viewer/set_highlight_colors.jpeg differ diff --git a/tests/helpers/__init__.py b/tests/helpers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/helpers/colors.py b/tests/helpers/colors.py new file mode 100644 index 00000000..479eb5b4 --- /dev/null +++ b/tests/helpers/colors.py @@ -0,0 +1,6 @@ +RGB_MAX = 255 + + +def rgb(red: int, green: int, blue: int) -> tuple[float, float, float]: + """A color from its 0-255 channels, as VTK takes it.""" + return (red / RGB_MAX, green / RGB_MAX, blue / RGB_MAX) diff --git a/tests/test_highlight_style.py b/tests/test_highlight_style.py new file mode 100644 index 00000000..4d18d26f --- /dev/null +++ b/tests/test_highlight_style.py @@ -0,0 +1,180 @@ +# Standard library imports + +# Third party imports +import pytest +from vtkmodules.vtkFiltersSources import vtkCubeSource, vtkLineSource, vtkPointSource +from vtkmodules.vtkIOXML import vtkXMLMultiBlockDataReader +from vtkmodules.vtkRenderingCore import vtkActor, vtkCompositePolyDataMapper, vtkDataSetMapper + +# Local application imports +from opengeodeweb_viewer.object.object_methods import ( + HighlightColors, + apply_highlight_colors, + apply_highlight_style, + highlight_whole_components, + highlight_whole_model, + is_whole_model, + reset_highlight_colors, + set_highlight_colors, +) +from opengeodeweb_viewer.vtk_pipeline import VtkPipeline +from opengeodeweb_viewer.vtk_protocol import VtkView +from tests.helpers.colors import RGB_MAX, rgb + +TEAL = rgb(60, 153, 131) +DARK_TEAL = rgb(31, 89, 77) +ORANGE = rgb(0xF4, 0x88, 0x30) +AUBURN = rgb(0x47, 0x25, 0x0A) +ANTHRACITE = rgb(0x1E, 0x1E, 0x1E) +BRAND_COLORS = HighlightColors( + color=ORANGE, + outline_color=AUBURN, + surfaces_color=ORANGE, + lines_color=ANTHRACITE, + corners_color=AUBURN, +) + + +def test_highlight_has_neutral_colors_by_default() -> None: + actor = vtkActor() + + apply_highlight_style(actor, vtkDataSetMapper()) + + assert actor.GetProperty().GetColor() == pytest.approx(TEAL) + assert actor.GetProperty().GetEdgeColor() == pytest.approx(DARK_TEAL) + + +def test_highlight_takes_the_colors_set_by_the_app() -> None: + actor = vtkActor() + + set_highlight_colors(BRAND_COLORS) + apply_highlight_style(actor, vtkDataSetMapper()) + + assert actor.GetProperty().GetColor() == pytest.approx(ORANGE) + assert actor.GetProperty().GetEdgeColor() == pytest.approx(AUBURN) + + +def test_an_existing_highlight_takes_the_new_colors() -> None: + actor = vtkActor() + apply_highlight_style(actor, vtkDataSetMapper()) + + set_highlight_colors(BRAND_COLORS) + apply_highlight_colors(actor) + + assert actor.GetProperty().GetColor() == pytest.approx(ORANGE) + assert actor.GetProperty().GetEdgeColor() == pytest.approx(AUBURN) + + +def test_resetting_highlight_colors_brings_back_the_defaults() -> None: + actor = vtkActor() + set_highlight_colors(BRAND_COLORS) + + reset_highlight_colors() + apply_highlight_style(actor, vtkDataSetMapper()) + + assert actor.GetProperty().GetColor() == pytest.approx(TEAL) + + +def test_whole_components_are_highlighted_without_edges() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + cube = vtkCubeSource() + cube.Update() + + highlight_whole_components(pipeline.highlight, [cube.GetOutput()]) + + assert not pipeline.highlight.actor.GetProperty().GetEdgeVisibility() + assert pipeline.highlight.mapper.GetInputDataObject(0, 0).GetNumberOfCells() == 6 + + +def test_a_picked_cell_is_outlined_after_a_component_highlight() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + cube = vtkCubeSource() + cube.Update() + highlight_whole_components(pipeline.highlight, [cube.GetOutput()]) + + VtkView().update_highlight(pipeline, 0, "CELL", cube.GetOutput()) + + assert pipeline.highlight.actor.GetProperty().GetEdgeVisibility() + + +def test_a_highlight_of_every_component_is_the_whole_model() -> None: + types = {1: "corners", 2: "lines", 3: "surfaces"} + + assert is_whole_model([1, 2, 3], types) + assert not is_whole_model([2, 3], types) + assert not is_whole_model([], types) + + +def test_the_whole_model_is_highlighted_with_a_color_per_component_type() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + sources = [vtkCubeSource(), vtkLineSource(), vtkPointSource()] + for source in sources: + source.Update() + surface, line, corner = (source.GetOutput() for source in sources) + + set_highlight_colors(BRAND_COLORS) + highlight_whole_model( + pipeline.highlight, [(surface, "surfaces"), (line, "lines"), (corner, "corners")] + ) + + mapper = pipeline.highlight.mapper + assert mapper.GetScalarVisibility() + assert not pipeline.highlight.actor.GetProperty().GetEdgeVisibility() + colors = mapper.GetInputDataObject(0, 0).GetCellData().GetArray(mapper.GetArrayName()) + first_cells = {"surfaces": 0, "lines": 6, "corners": 7} + assert [ + tuple(component / RGB_MAX for component in colors.GetTuple3(cell)) + for cell in first_cells.values() + ] == [ + pytest.approx(ORANGE, abs=0.5 / RGB_MAX), + pytest.approx(ANTHRACITE, abs=0.5 / RGB_MAX), + pytest.approx(AUBURN, abs=0.5 / RGB_MAX), + ] + + +def test_the_whole_model_has_neutral_colors_by_default() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + sources = [vtkCubeSource(), vtkLineSource()] + for source in sources: + source.Update() + surface, line = (source.GetOutput() for source in sources) + + highlight_whole_model(pipeline.highlight, [(surface, "surfaces"), (line, "lines")]) + + mapper = pipeline.highlight.mapper + colors = mapper.GetInputDataObject(0, 0).GetCellData().GetArray(mapper.GetArrayName()) + assert [ + tuple(component / RGB_MAX for component in colors.GetTuple3(cell)) for cell in (0, 6) + ] == [ + pytest.approx(TEAL, abs=0.5 / RGB_MAX), + pytest.approx(DARK_TEAL, abs=0.5 / RGB_MAX), + ] + + +def test_some_components_take_the_highlight_color_after_the_whole_model() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + cube = vtkCubeSource() + cube.Update() + highlight_whole_model(pipeline.highlight, [(cube.GetOutput(), "surfaces")]) + + highlight_whole_components(pipeline.highlight, [cube.GetOutput()]) + + assert not pipeline.highlight.mapper.GetScalarVisibility() + assert pipeline.highlight.actor.GetProperty().GetColor() == pytest.approx(TEAL) + + +def test_a_picked_cell_takes_the_highlight_color_after_the_whole_model() -> None: + pipeline = VtkPipeline(vtkXMLMultiBlockDataReader(), vtkCompositePolyDataMapper()) + apply_highlight_style(pipeline.highlight.actor, pipeline.highlight.mapper) + cube = vtkCubeSource() + cube.Update() + highlight_whole_model(pipeline.highlight, [(cube.GetOutput(), "surfaces")]) + + VtkView().update_highlight(pipeline, 0, "CELL", cube.GetOutput()) + + assert not pipeline.highlight.mapper.GetScalarVisibility() diff --git a/tests/test_viewer_protocols.py b/tests/test_viewer_protocols.py index 1198f82e..5b41235d 100644 --- a/tests/test_viewer_protocols.py +++ b/tests/test_viewer_protocols.py @@ -58,6 +58,30 @@ def test_set_viewer_background_color(server: ServerMonitor) -> None: assert server.compare_image("viewer/set_background_color.jpeg") +BRAND_HIGHLIGHT_COLORS = { + "color": {"red": 244, "green": 136, "blue": 48}, + "outline_color": {"red": 71, "green": 37, "blue": 10}, + "surfaces_color": {"red": 244, "green": 136, "blue": 48}, + "lines_color": {"red": 30, "green": 30, "blue": 30}, + "corners_color": {"red": 71, "green": 37, "blue": 10}, +} +SET_HIGHLIGHT_COLORS = ( + VtkViewerView.viewer_prefix + VtkViewerView.viewer_schemas_dict["set_highlight_colors"]["rpc"] +) +MESH_HIGHLIGHT = VtkMeshView.mesh_prefix + VtkMeshView.mesh_schemas_dict["highlight"]["rpc"] + + +def test_set_highlight_colors_restyles_loaded_data( + server: ServerMonitor, dataset_factory: Callable[..., str] +) -> None: + test_register_mesh(server, dataset_factory) + server.call(MESH_HIGHLIGHT, [{"id": mesh_id, "visibility": True}]) + + server.call(SET_HIGHLIGHT_COLORS, [BRAND_HIGHLIGHT_COLORS]) + + assert server.compare_image("viewer/set_highlight_colors.jpeg") + + def test_set_text_style(server: ServerMonitor, dataset_factory: Callable[..., str]) -> None: test_grid_scale(server, dataset_factory) server.call(