Skip to content
Draft
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
156 changes: 156 additions & 0 deletions opengeodeweb_viewer_schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions opengeodeweb_viewer_typed_schemas.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1401,6 +1412,7 @@ export interface Schemas {
readonly set_z_scaling: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_z_scaling"] & TypedSchema<ViewerSetZScalingParams, ViewerSetZScalingResponse>;
readonly set_text_style: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_text_style"] & TypedSchema<ViewerSetTextStyleParams, ViewerSetTextStyleResponse>;
readonly set_orientation_widget: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_orientation_widget"] & TypedSchema<ViewerSetOrientationWidgetParams, ViewerSetOrientationWidgetResponse>;
readonly set_highlight_colors: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_highlight_colors"] & TypedSchema<ViewerSetHighlightColorsParams, ViewerSetHighlightColorsResponse>;
readonly set_background_color: (typeof json)["opengeodeweb_viewer"]["viewer"]["set_background_color"] & TypedSchema<ViewerSetBackgroundColorParams, ViewerSetBackgroundColorResponse>;
readonly ruler: (typeof json)["opengeodeweb_viewer"]["viewer"]["ruler"] & TypedSchema<ViewerRulerParams, ViewerRulerResponse>;
readonly reset_visualization: (typeof json)["opengeodeweb_viewer"]["viewer"]["reset_visualization"] & TypedSchema<ViewerResetVisualizationParams, ViewerResetVisualizationResponse>;
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ wslink==2.6.0
yarl==1.*
# via aiohttp

opengeodeweb-microservice==1.*,>=1.5.0
165 changes: 146 additions & 19 deletions src/opengeodeweb_viewer/object/object_methods.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

Expand All @@ -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__()
Expand Down Expand Up @@ -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()
Expand Down
Loading
Loading