Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkRenderWindowBridge.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkRenderWindowBridge_h
14 #define mitkRenderWindowBridge_h
15 
16 #include <MitkRESTAPIExports.h>
17 #include <mitkAnatomicalPlanes.h>
18 #include <mitkPoint.h>
20 #include <mitkVector.h>
21 #include <mitkWeakPointer.h>
22 
23 #include <functional>
24 #include <mutex>
25 #include <optional>
26 #include <stdexcept>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 namespace mitk
32 {
36  enum class ScreenshotFormat
37  {
38  Png,
39  Jpeg
40  };
41 
49  struct WorldBounds
50  {
53 
55  };
56 
60  {
63 
65  std::optional<WorldBounds> bounds;
66 
68  };
69 
76  class MITKRESTAPI_EXPORT RenderWindowBridgeNoEditorException : public std::runtime_error
77  {
78  public:
79  using std::runtime_error::runtime_error;
80  };
81 
88  {
89  public:
90  using std::runtime_error::runtime_error;
91  };
92 
100  {
101  public:
102  using std::runtime_error::runtime_error;
103  };
104 
112  {
113  public:
114  using std::runtime_error::runtime_error;
115  };
116 
118  struct EditorInfo
119  {
120  std::string alias;
121  std::string pluginId;
122  bool active = false;
123  // Window identifiers exposed by the editor. For MxN, this is the
124  // canonical fully-qualified `id` field of each window leaf in the v2
125  // layout document (e.g. `mxn__widget0`): the same string used as the
126  // URL path segment for sub-resources. For StdMulti, the engine-fixed
127  // names (axial / sagittal / coronal / 3d). Empty when !active.
128  std::vector<std::string> windowIds;
129  };
130 
137  enum class WindowKind
138  {
139  TwoD,
140  ThreeD
141  };
142 
145 
151 
159 
176  struct WindowInfo
177  {
178  std::string id; // "axial" | "sagittal" | "coronal" | "3d"
180  std::optional<AnatomicalPlane> viewDirection;
181  };
182 
212  {
213  std::string id; // canonical fully-qualified window id
214  std::optional<std::string> displayName; // human-readable label; nullopt when absent
216  std::optional<AnatomicalPlane> viewDirection; // always set for 2D cells under v2; nullopt reserved for v3 3D cells. Mapped to v2 strings via AnatomicalPlaneToV2String when present.
217  std::string selectionGroup; // links.selection group label
218  };
219 
221  struct CameraState
222  {
226  std::optional<double> parallelScale; // 2D windows only
227  std::optional<double> perspectiveAngle; // 3D windows only
228 
230  };
231 
233  struct CameraPatch
234  {
235  std::optional<Point3D> position;
236  std::optional<Point3D> focalPoint;
237  std::optional<Vector3D> viewUp;
238  std::optional<double> parallelScale;
239  std::optional<double> perspectiveAngle;
240  std::optional<std::string> standardView; // applied first if present
241  };
242 
244  struct SliceBounds
245  {
246  unsigned int steps = 0;
249  bool hasPositions = false; // false if no geometry loaded
250 
252  };
253 
255  struct SliceState
256  {
257  unsigned int step = 0;
260 
262  };
263 
280  {
281  public:
294  std::function<std::vector<unsigned char>(
295  std::optional<std::pair<int, int>> size,
296  ScreenshotFormat format)>;
297 
311  using PositionGetter = std::function<SelectedPositionInfo()>;
312 
322  using PositionSetter = std::function<void(const Point3D& pos)>;
323 
325  using EditorListProvider = std::function<std::vector<EditorInfo>()>;
326 
328  using StdMultiWindowListProvider = std::function<std::vector<WindowInfo>()>;
329 
332  std::function<std::vector<unsigned char>(
333  std::optional<std::pair<int, int>> size,
334  ScreenshotFormat format)>;
335 
338  std::function<std::vector<unsigned char>(
339  const std::string& windowId,
340  std::optional<std::pair<int, int>> size,
341  ScreenshotFormat format)>;
342 
344  using StdMultiCameraGetter = std::function<CameraState(const std::string& windowId)>;
345 
347  using StdMultiCameraSetter = std::function<void(const std::string& windowId, const CameraPatch& patch)>;
348 
350  using StdMultiSelectedSliceGetter = std::function<SliceState(const std::string& windowId)>;
351 
359  using StdMultiSelectedSliceStepSetter = std::function<void(const std::string& windowId, unsigned int step)>;
360 
362  using MxNWindowListProvider = std::function<std::vector<MxNWindowInfo>()>;
363 
366  std::function<std::vector<unsigned char>(
367  std::optional<std::pair<int, int>> size,
368  ScreenshotFormat format)>;
369 
372  std::function<std::vector<unsigned char>(
373  const std::string& windowId,
374  std::optional<std::pair<int, int>> size,
375  ScreenshotFormat format)>;
376 
378  using MxNCameraGetter = std::function<CameraState(const std::string& windowId)>;
379 
381  using MxNCameraSetter = std::function<void(const std::string& windowId, const CameraPatch& patch)>;
382 
384  using MxNSelectedSliceGetter = std::function<SliceState(const std::string& windowId)>;
385 
393  using MxNSelectedSliceStepSetter = std::function<void(const std::string& windowId, unsigned int step)>;
394 
405  std::function<SelectedPositionInfo(const std::string& windowId)>;
406 
409  std::function<void(const std::string& windowId, const Point3D& position)>;
410 
419  using MxNLayoutGetter = std::function<std::string()>;
420 
432  using MxNLayoutSetter = std::function<std::string(const std::string& layoutJson)>;
433 
443 
450 
460 
470 
473 
476 
479 
482 
485 
488 
491 
494 
497 
500 
503 
506 
509 
512 
515 
518 
521 
524 
527 
541 
543  bool HasScreenshotProvider() const;
544 
546  bool HasPositionGetter() const;
547 
549  bool HasPositionSetter() const;
550 
551  bool HasEditorListProvider() const;
559 
567  bool HasMxNCameraGetter() const;
569  bool HasMxNCameraSetter() const;
579  bool HasMxNLayoutGetter() const;
581  bool HasMxNLayoutSetter() const;
582 
594  std::vector<unsigned char> TakeScreenshot(
595  std::optional<std::pair<int, int>> size,
596  ScreenshotFormat format) const;
597 
608 
618  void SetSelectedPosition(const Point3D& pos) const;
619 
624  std::vector<EditorInfo> ListEditors() const;
625 
631  std::vector<WindowInfo> ListStdMultiWindows() const;
632 
638  std::vector<unsigned char> TakeStdMultiEditorScreenshot(
639  std::optional<std::pair<int, int>> size,
640  ScreenshotFormat format) const;
641 
647  std::vector<unsigned char> TakeStdMultiWindowScreenshot(
648  const std::string& windowId,
649  std::optional<std::pair<int, int>> size,
650  ScreenshotFormat format) const;
651 
652  CameraState GetStdMultiCamera(const std::string& windowId) const;
653  void SetStdMultiCamera(const std::string& windowId, const CameraPatch& patch) const;
654  SliceState GetStdMultiSelectedSlice(const std::string& windowId) const;
655  void SetStdMultiSelectedSliceStep(const std::string& windowId, unsigned int step) const;
656 
662  std::vector<MxNWindowInfo> ListMxNWindows() const;
663 
669  std::vector<unsigned char> TakeMxNEditorScreenshot(
670  std::optional<std::pair<int, int>> size,
671  ScreenshotFormat format) const;
672 
678  std::vector<unsigned char> TakeMxNWindowScreenshot(
679  const std::string& windowId,
680  std::optional<std::pair<int, int>> size,
681  ScreenshotFormat format) const;
682 
692  CameraState GetMxNCamera(const std::string& windowId) const;
693 
704  void SetMxNCamera(const std::string& windowId, const CameraPatch& patch) const;
705 
716  SliceState GetMxNSelectedSlice(const std::string& windowId) const;
717 
729  void SetMxNSelectedSliceStep(const std::string& windowId, unsigned int step) const;
730 
740  SelectedPositionInfo GetMxNSelectedPosition(const std::string& windowId) const;
741 
752  void SetMxNSelectedPosition(const std::string& windowId, const Point3D& position) const;
753 
755  std::string GetMxNLayout() const;
756 
764  std::string SetMxNLayout(const std::string& layoutJson) const;
765 
766  private:
767  ScreenshotProvider m_ScreenshotProvider;
768  PositionGetter m_PositionGetter;
769  PositionSetter m_PositionSetter;
770  EditorListProvider m_EditorListProvider;
771  StdMultiWindowListProvider m_StdMultiWindowListProvider;
772  StdMultiEditorScreenshotProvider m_StdMultiEditorScreenshotProvider;
773  StdMultiWindowScreenshotProvider m_StdMultiWindowScreenshotProvider;
774  StdMultiCameraGetter m_StdMultiCameraGetter;
775  StdMultiCameraSetter m_StdMultiCameraSetter;
776  StdMultiSelectedSliceGetter m_StdMultiSelectedSliceGetter;
777  StdMultiSelectedSliceStepSetter m_StdMultiSelectedSliceStepSetter;
778  MxNWindowListProvider m_MxNWindowListProvider;
779  MxNEditorScreenshotProvider m_MxNEditorScreenshotProvider;
780  MxNWindowScreenshotProvider m_MxNWindowScreenshotProvider;
781  MxNCameraGetter m_MxNCameraGetter;
782  MxNCameraSetter m_MxNCameraSetter;
783  MxNSelectedSliceGetter m_MxNSelectedSliceGetter;
784  MxNSelectedSliceStepSetter m_MxNSelectedSliceStepSetter;
785  MxNSelectedPositionGetter m_MxNSelectedPositionGetter;
786  MxNSelectedPositionSetter m_MxNSelectedPositionSetter;
787  MxNLayoutGetter m_MxNLayoutGetter;
788  MxNLayoutSetter m_MxNLayoutSetter;
789  mutable std::mutex m_Mutex;
791  };
792 }
793 
794 #endif
#define MITKRESTAPI_EXPORT
Thrown by bridge callbacks when the target editor instance is not open in the workbench.
Thrown by bridge callbacks when a window exists but its renderer is currently unavailable (server-sid...
Thrown by bridge callbacks when the window id is not known.
Thrown by bridge callbacks when the requested sub-resource does not apply to the addressed window (e....
Bridge between the REST API server and the Qt/render-window layer.
std::function< std::vector< MxNWindowInfo >()> MxNWindowListProvider
bool HasMxNSelectedSliceStepSetter() const
void SetPositionSetter(PositionSetter setter)
Set or clear the position setter callback.
std::function< SelectedPositionInfo(const std::string &windowId)> MxNSelectedPositionGetter
Callback type: read the per-cell selected position (cell anchor) plus scene bounds atomically.
bool HasStdMultiSelectedSliceStepSetter() const
bool HasStdMultiCameraGetter() const
void SetMxNSelectedPositionGetter(MxNSelectedPositionGetter getter)
std::function< std::vector< unsigned char >(std::optional< std::pair< int, int > > size, ScreenshotFormat format)> StdMultiEditorScreenshotProvider
void SetMxNSelectedSliceStep(const std::string &windowId, unsigned int step) const
Set the selected slice of a 2D MxN cell by step index.
bool HasStdMultiWindowScreenshotProvider() const
void SetMxNSelectedPositionSetter(MxNSelectedPositionSetter setter)
std::function< std::vector< unsigned char >(std::optional< std::pair< int, int > > size, ScreenshotFormat format)> ScreenshotProvider
Callback type for taking a screenshot.
bool HasMxNSelectedPositionSetter() const
std::function< SelectedPositionInfo()> PositionGetter
Callback type for reading the current global crosshair position and scene bounds.
bool HasMxNSelectedSliceGetter() const
void SetMxNSelectedSliceStepSetter(MxNSelectedSliceStepSetter setter)
void SetMxNLayoutSetter(MxNLayoutSetter setter)
std::function< std::vector< WindowInfo >()> StdMultiWindowListProvider
std::function< SliceState(const std::string &windowId)> StdMultiSelectedSliceGetter
void SetStdMultiSelectedSliceGetter(StdMultiSelectedSliceGetter getter)
std::function< void(const std::string &windowId, const CameraPatch &patch)> StdMultiCameraSetter
void SetMxNCameraGetter(MxNCameraGetter getter)
void SetMxNCameraSetter(MxNCameraSetter setter)
std::function< std::string(const std::string &layoutJson)> MxNLayoutSetter
Callback type: apply a v2.0 JSON layout document and return the freshly serialized layout (so callers...
void ResetCallbacks()
Clear all registered callbacks in a single atomic operation.
std::vector< WindowInfo > ListStdMultiWindows() const
Invoke the StdMulti window list provider.
void SetStdMultiCameraSetter(StdMultiCameraSetter setter)
bool HasStdMultiCameraSetter() const
std::vector< unsigned char > TakeScreenshot(std::optional< std::pair< int, int >> size, ScreenshotFormat format) const
Capture a screenshot using the registered provider.
void SetStdMultiCamera(const std::string &windowId, const CameraPatch &patch) const
SelectedPositionInfo GetSelectedPosition() const
Read the current global crosshair position and scene bounds atomically.
std::function< SliceState(const std::string &windowId)> MxNSelectedSliceGetter
std::vector< MxNWindowInfo > ListMxNWindows() const
Invoke the MxN window list provider.
bool HasStdMultiEditorScreenshotProvider() const
void SetEditorListProvider(EditorListProvider provider)
std::function< std::vector< EditorInfo >()> EditorListProvider
void SetStdMultiSelectedSliceStep(const std::string &windowId, unsigned int step) const
bool HasMxNWindowScreenshotProvider() const
std::vector< unsigned char > TakeStdMultiWindowScreenshot(const std::string &windowId, std::optional< std::pair< int, int >> size, ScreenshotFormat format) const
Invoke the StdMulti per-window screenshot provider.
bool HasMxNWindowListProvider() const
std::function< std::vector< unsigned char >(const std::string &windowId, std::optional< std::pair< int, int > > size, ScreenshotFormat format)> MxNWindowScreenshotProvider
std::string GetMxNLayout() const
void SetSelectedPosition(const Point3D &pos) const
Move the crosshair to the given world position.
SliceState GetStdMultiSelectedSlice(const std::string &windowId) const
void SetStdMultiWindowScreenshotProvider(StdMultiWindowScreenshotProvider provider)
std::function< void(const Point3D &pos)> PositionSetter
Callback type for setting the global crosshair position.
void SetMxNEditorScreenshotProvider(MxNEditorScreenshotProvider provider)
std::function< std::vector< unsigned char >(const std::string &windowId, std::optional< std::pair< int, int > > size, ScreenshotFormat format)> StdMultiWindowScreenshotProvider
CameraState GetStdMultiCamera(const std::string &windowId) const
bool HasMxNLayoutGetter() const
void SetMxNWindowScreenshotProvider(MxNWindowScreenshotProvider provider)
std::vector< unsigned char > TakeMxNWindowScreenshot(const std::string &windowId, std::optional< std::pair< int, int >> size, ScreenshotFormat format) const
Invoke the MxN per-window screenshot provider.
std::function< std::vector< unsigned char >(std::optional< std::pair< int, int > > size, ScreenshotFormat format)> MxNEditorScreenshotProvider
std::function< void(const std::string &windowId, const CameraPatch &patch)> MxNCameraSetter
bool HasPositionSetter() const
bool HasScreenshotProvider() const
bool HasMxNEditorScreenshotProvider() const
std::vector< EditorInfo > ListEditors() const
Invoke the editor list provider.
bool HasMxNCameraSetter() const
void SetMxNLayoutGetter(MxNLayoutGetter getter)
void SetMxNWindowListProvider(MxNWindowListProvider provider)
void SetScreenshotProvider(ScreenshotProvider provider)
Set or clear the screenshot provider callback.
bool HasMxNCameraGetter() const
void SetStdMultiEditorScreenshotProvider(StdMultiEditorScreenshotProvider provider)
void SetStdMultiSelectedSliceStepSetter(StdMultiSelectedSliceStepSetter setter)
void SetMxNSelectedSliceGetter(MxNSelectedSliceGetter getter)
std::string SetMxNLayout(const std::string &layoutJson) const
Apply a v2.0 layout document; returns the freshly serialized layout.
std::function< CameraState(const std::string &windowId)> StdMultiCameraGetter
SliceState GetMxNSelectedSlice(const std::string &windowId) const
Read the selected-slice state of a 2D MxN cell.
void SetStdMultiWindowListProvider(StdMultiWindowListProvider provider)
bool HasEditorListProvider() const
std::function< void(const std::string &windowId, const Point3D &position)> MxNSelectedPositionSetter
void SetMxNCamera(const std::string &windowId, const CameraPatch &patch) const
Apply a camera patch to a specific MxN cell.
std::function< CameraState(const std::string &windowId)> MxNCameraGetter
void SetDispatcher(StorageThreadDispatcherBase *dispatcher)
Set the thread dispatcher for UI thread dispatching.
void SetPositionGetter(PositionGetter getter)
Set or clear the position getter callback.
std::function< void(const std::string &windowId, unsigned int step)> MxNSelectedSliceStepSetter
Callback type: set the selected slice of a 2D MxN cell by step index.
std::function< void(const std::string &windowId, unsigned int step)> StdMultiSelectedSliceStepSetter
Callback type: set the selected slice of a StdMulti 2D window by step index.
bool HasPositionGetter() const
std::vector< unsigned char > TakeStdMultiEditorScreenshot(std::optional< std::pair< int, int >> size, ScreenshotFormat format) const
Invoke the StdMulti editor screenshot provider.
bool HasMxNLayoutSetter() const
SelectedPositionInfo GetMxNSelectedPosition(const std::string &windowId) const
Read the per-cell selected position and scene bounds atomically.
bool HasStdMultiWindowListProvider() const
std::function< std::string()> MxNLayoutGetter
Callback type: serialize the editor's current layout to a v2.0 JSON document.
bool HasStdMultiSelectedSliceGetter() const
bool HasMxNSelectedPositionGetter() const
void SetStdMultiCameraGetter(StdMultiCameraGetter getter)
std::vector< unsigned char > TakeMxNEditorScreenshot(std::optional< std::pair< int, int >> size, ScreenshotFormat format) const
Invoke the MxN editor screenshot provider.
CameraState GetMxNCamera(const std::string &windowId) const
Read the camera state of a specific MxN cell.
void SetMxNSelectedPosition(const std::string &windowId, const Point3D &position) const
Set the per-cell selected position (cell anchor).
Abstract base class for dispatching tasks to the thread that owns DataStorage.
A weak pointer that observes an itk::Object without preventing its deletion.
Find image slices visible on a given plane.
ScreenshotFormat
Format for screenshot capture.
MITKRESTAPI_EXPORT const char * WindowKindToString(WindowKind kind)
WindowKind
Closed set of render window kinds reported by the bridge.
MITKRESTAPI_EXPORT const char * AnatomicalPlaneToV2String(AnatomicalPlane plane)
AnatomicalPlane
Enumeration of standard anatomical image planes.
MITKRESTAPI_EXPORT AnatomicalPlane ParseV2ViewDirection(const std::string &s)
Parse the v2 layout schema's view_direction string.
std::optional< Vector3D > viewUp
std::optional< double > parallelScale
std::optional< Point3D > focalPoint
std::optional< Point3D > position
std::optional< double > perspectiveAngle
std::optional< std::string > standardView
std::optional< double > perspectiveAngle
std::optional< double > parallelScale
std::vector< std::string > windowIds
Summary of a single MxN cell exposed via the REST window list.
std::optional< AnatomicalPlane > viewDirection
std::optional< std::string > displayName
Position and world-bounds snapshot read atomically on the UI thread.
std::optional< WorldBounds > bounds
std::optional< AnatomicalPlane > viewDirection