Slicer  5.0
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkSlicerMarkupsWidgetRepresentation.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Copyright (c) ProxSim ltd., Kwun Tong, Hong Kong. All Rights Reserved.
4 
5  See COPYRIGHT.txt
6  or http://www.slicer.org/copyright/copyright.txt for details.
7 
8  Unless required by applicable law or agreed to in writing, software
9  distributed under the License is distributed on an "AS IS" BASIS,
10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  See the License for the specific language governing permissions and
12  limitations under the License.
13 
14  This file was originally developed by Davide Punzo, punzodavide@hotmail.it,
15  and development was supported by ProxSim ltd.
16 
17 =========================================================================*/
18 
41 #ifndef vtkSlicerMarkupsRepresentation_h
42 #define vtkSlicerMarkupsRepresentation_h
43 
44 #include "vtkSlicerMarkupsModuleVTKWidgetsExport.h"
45 
48 #include "vtkMRMLMarkupsNode.h"
49 
50 #include "vtkActor2D.h"
51 #include "vtkAppendPolyData.h"
52 #include "vtkArcSource.h"
53 #include "vtkArrowSource.h"
54 #include "vtkGlyph3D.h"
55 #include "vtkLookupTable.h"
57 #include "vtkPointPlacer.h"
58 #include "vtkPointSetToLabelHierarchy.h"
59 #include "vtkPolyDataMapper2D.h"
60 #include "vtkProperty2D.h"
61 #include "vtkSmartPointer.h"
62 #include "vtkSphereSource.h"
63 #include "vtkTextActor.h"
64 #include "vtkTextProperty.h"
65 #include "vtkTensorGlyph.h"
66 #include "vtkTransform.h"
67 #include "vtkTransformPolyDataFilter.h"
68 #include "vtkTubeFilter.h"
69 
71 
72 class VTK_SLICER_MARKUPS_MODULE_VTKWIDGETS_EXPORT vtkSlicerMarkupsWidgetRepresentation : public vtkMRMLAbstractWidgetRepresentation
73 {
74 public:
75  enum
76  {
82  NumberOfControlPointTypes
83  };
84 
87  void PrintSelf(ostream& os, vtkIndent indent) override;
88 
90  void UpdateFromMRML(vtkMRMLNode* caller, unsigned long event, void *callData = nullptr) override;
91 
93  void GetActors(vtkPropCollection*) override;
94  void ReleaseGraphicsResources(vtkWindow*) override;
95  int RenderOverlay(vtkViewport* viewport) override;
96  int RenderOpaqueGeometry(vtkViewport* viewport) override;
97  int RenderTranslucentPolygonalGeometry(vtkViewport* viewport) override;
98  vtkTypeBool HasTranslucentPolygonalGeometry() override;
99 
101  virtual int GetNumberOfControlPoints();
102 
106  virtual int GetNthControlPointDisplayPosition(int n, double pos[2]);
107 
109  virtual vtkMRMLMarkupsNode::ControlPoint *GetNthControlPoint(int n);
110 
112  virtual void SetMarkupsDisplayNode(vtkMRMLMarkupsDisplayNode *markupsDisplayNode);
113  virtual vtkMRMLMarkupsDisplayNode* GetMarkupsDisplayNode();
114  virtual vtkMRMLMarkupsNode* GetMarkupsNode();
115 
117  virtual void UpdateCenterOfRotation();
118 
120  virtual bool GetTransformationReferencePoint(double referencePointWorld[3]);
121 
125  virtual void CanInteract(vtkMRMLInteractionEventData* interactionEventData,
126  int &foundComponentType, int &foundComponentIndex, double &closestDistance2);
127 
128  virtual int FindClosestPointOnWidget(const int displayPos[2], double worldPos[3], int *idx);
129 
130  virtual vtkPointPlacer* GetPointPlacer();
131 
134  virtual vtkPolyData* GetControlPointsPolyData(int controlPointType);
137  virtual vtkPolyData* GetLabelControlPointsPolyData(int pipeline);
140  virtual vtkStringArray* GetLabels(int pipeline);
141 
143 
147  bool IsDisplayable();
149 
151  virtual void GetInteractionHandleAxisWorld(int type, int index, double axis[3]);
153  virtual void GetInteractionHandleOriginWorld(double origin[3]);
155  virtual void GetInteractionHandlePositionWorld(int type, int index, double position[3]);
156 
157 protected:
160 
161  // Convert glyph types from display node enums to 2D glyph source enums
162  static int GetGlyphTypeSourceFromDisplay(int glyphTypeDisplay);
163 
165  {
166  public:
168  virtual ~ControlPointsPipeline();
169 
174  //vtkSmartPointer<vtkPolyData> PointMarkerShape;
175  vtkSmartPointer<vtkMarkupsGlyphSource2D> GlyphSource2D;
176  vtkSmartPointer<vtkSphereSource> GlyphSourceSphere;
177 
178  vtkSmartPointer<vtkPolyData> ControlPointsPolyData;
179  vtkSmartPointer<vtkPoints> ControlPoints;
180  vtkSmartPointer<vtkPolyData> LabelControlPointsPolyData;
181  vtkSmartPointer<vtkPoints> LabelControlPoints;
182  vtkSmartPointer<vtkPointSetToLabelHierarchy> PointSetToLabelHierarchyFilter;
183  vtkSmartPointer<vtkStringArray> Labels;
184  vtkSmartPointer<vtkStringArray> LabelsPriority;
185  vtkSmartPointer<vtkTextProperty> TextProperty;
186  };
187 
188  class VTK_SLICER_MARKUPS_MODULE_VTKWIDGETS_EXPORT MarkupsInteractionPipeline
189  {
190  public:
192  virtual ~MarkupsInteractionPipeline();
193 
194  vtkWeakPointer<vtkMRMLAbstractWidgetRepresentation> Representation;
195 
196  vtkSmartPointer<vtkSphereSource> AxisRotationHandleSource;
197  vtkSmartPointer<vtkArcSource> AxisRotationArcSource;
198  vtkSmartPointer<vtkTubeFilter> AxisRotationTubeFilter;
199  vtkSmartPointer<vtkPolyData> AxisRotationInteriorAnglePolyData;
200  vtkSmartPointer<vtkTubeFilter> AxisRotationInterorAngleTubeFilter;
201  vtkSmartPointer<vtkPolyData> RotationHandlePoints;
202  vtkSmartPointer<vtkTransformPolyDataFilter> RotationScaleTransform;
203  vtkSmartPointer<vtkAppendPolyData> AxisRotationGlyphSource;
204  vtkSmartPointer<vtkTensorGlyph> AxisRotationGlypher;
205 
206  vtkSmartPointer<vtkArrowSource> AxisTranslationGlyphSource;
207  vtkSmartPointer<vtkTransformPolyDataFilter> AxisTranslationGlyphTransformer;
208  vtkSmartPointer<vtkPolyData> TranslationHandlePoints;
209  vtkSmartPointer<vtkTransformPolyDataFilter> TranslationScaleTransform;
210  vtkSmartPointer<vtkGlyph3D> AxisTranslationGlypher;
211 
212  vtkSmartPointer<vtkSphereSource> AxisScaleHandleSource;
213  vtkSmartPointer<vtkPolyData> ScaleHandlePoints;
214  vtkSmartPointer<vtkTransformPolyDataFilter> ScaleScaleTransform;
215  vtkSmartPointer<vtkGlyph3D> AxisScaleGlypher;
216 
217  vtkSmartPointer<vtkAppendPolyData> Append;
218  vtkSmartPointer<vtkTransformPolyDataFilter> HandleToWorldTransformFilter;
219  vtkSmartPointer<vtkTransform> HandleToWorldTransform;
220  vtkSmartPointer<vtkLookupTable> ColorTable;
221  vtkSmartPointer<vtkPolyDataMapper2D> Mapper;
222  vtkSmartPointer<vtkActor2D> Actor;
223  vtkSmartPointer<vtkProperty2D> Property;
224 
225  double StartFadeAngle{30};
226  double EndFadeAngle{20};
227  double InteractionHandleSize{1.0};
228 
229  virtual void InitializePipeline();
230  virtual void CreateRotationHandles();
231  virtual void CreateTranslationHandles();
232  virtual void CreateScaleHandles();
233  virtual void UpdateHandleVisibility();
234  virtual void UpdateHandleColors();
235 
237  virtual void SetWidgetScale(double scale);
240  virtual void GetHandleColor(int type, int index, double color[4]);
242  virtual double GetHandleOpacity(int type, int index);
243 
245  virtual void GetViewPlaneNormal(double normal[3]);
246 
249  virtual void GetInteractionHandlePositionWorld(int type, int index, double position[3]);
252  virtual void GetInteractionHandleAxis(int type, int index, double axis[3]);
254  virtual void GetInteractionHandleAxisWorld(int type, int index, double axis[3]);
256  virtual void GetInteractionHandleOriginWorld(double origin[3]);
257 
258  struct HandleInfo
259  {
260  HandleInfo(int index, int componentType, double positionWorld[3], double positionLocal[3], double color[4])
261  : Index(index)
262  , ComponentType(componentType)
263  {
264  for (int i = 0; i < 3; ++i)
265  {
266  this->PositionWorld[i] = positionWorld[i];
267  }
268  this->PositionWorld[3] = 1.0;
269  for (int i = 0; i < 3; ++i)
270  {
271  this->PositionLocal[i] = positionLocal[i];
272  }
273  this->PositionLocal[3] = 1.0;
274  for (int i = 0; i < 4; ++i)
275  {
276  this->Color[i] = color[i];
277  }
278  }
279  int Index;
281  double PositionLocal[4];
282  double PositionWorld[4];
283  double Color[4];
284  bool IsVisible()
285  {
286  double epsilon = 0.001;
287  return this->Color[3] > epsilon;
288  }
289  };
290 
292  virtual std::vector<HandleInfo> GetHandleInfoList();
293  };
294  typedef std::vector<MarkupsInteractionPipeline::HandleInfo> HandleInfoList;
295 
296  // Calculate view size and scale factor
297  virtual void UpdateViewScaleFactor() = 0;
298 
299  virtual void UpdateControlPointSize() = 0;
300 
301  // Update the size of the interaction handle based on screen size + vtkMRMLMarkupsDisplayNode::InteractionHandleScale parameter.
302  virtual void UpdateInteractionHandleSize();
303 
305  double ScreenSizePixel; // diagonal size of the screen
306 
307  // Control point size, specified in renderer world coordinate system.
308  // For slice views, renderer world coordinate system is the display coordinate system, so it is measured in pixels.
309  // For 3D views, renderer world coordinate system is the Slicer world coordinate system, so it is measured in the
310  // scene length unit (typically millimeters).
312 
313  virtual void SetMarkupsNode(vtkMRMLMarkupsNode *markupsNode);
314 
315  vtkWeakPointer<vtkMRMLMarkupsDisplayNode> MarkupsDisplayNode;
316  vtkWeakPointer<vtkMRMLMarkupsNode> MarkupsNode;
317 
318  vtkSmartPointer<vtkPointPlacer> PointPlacer;
319 
320  vtkSmartPointer<vtkTextActor> TextActor;
321 
322  vtkTypeBool CurveClosed;
323 
325  virtual bool GetAllControlPointsVisible();
326 
328  bool GetAllControlPointsSelected();
329 
330  // Utility function to build straight lines between control points.
331  // If displayPosition is true then positions will be computed in display coordinate system,
332  // otherwise in world coordinate system.
333  // displayPosition is normally set to true in 2D, and to false in 3D representations.
334  void BuildLine(vtkPolyData* linePolyData, bool displayPosition);
335 
337 
338  double* GetWidgetColor(int controlPointType);
339 
340  ControlPointsPipeline* ControlPoints[NumberOfControlPointTypes]; // Unselected, Selected, Active, Project, ProjectBehind
341 
342  virtual void SetupInteractionPipeline();
344 
346  virtual void UpdateInteractionPipeline();
347 
348 private:
350  void operator=(const vtkSlicerMarkupsWidgetRepresentation&) = delete;
351 };
352 
353 #endif
HandleInfo(int index, int componentType, double positionWorld[3], double positionLocal[3], double color[4])
vtkWeakPointer< vtkMRMLMarkupsDisplayNode > MarkupsDisplayNode
int RenderOpaqueGeometry(vtkViewport *vtkNotUsed(viewport)) override
void PrintSelf(ostream &os, vtkIndent indent) override
int RenderOverlay(vtkViewport *vtkNotUsed(viewport)) override
std::vector< MarkupsInteractionPipeline::HandleInfo > HandleInfoList
vtkSmartPointer< vtkPointSetToLabelHierarchy > PointSetToLabelHierarchyFilter
virtual void UpdateFromMRML(vtkMRMLNode *caller, unsigned long event, void *callData=nullptr)
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:167
int RenderTranslucentPolygonalGeometry(vtkViewport *vtkNotUsed(viewport)) override