1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkImageCroppingRegionsWidget.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkImageCroppingRegionsWidget
17  * @brief   widget for cropping an image
18  *
19  * This widget displays a set of axis aligned lines that can be interactively
20  * manipulated to crop a volume. The region to be cropped away is displayed
21  * in a different highlight. Much like the vtkVolumeMapper, this widget
22  * supports 27 possible configurations of cropping planes. (See
23  * CroppingRegionFlags). If a volume mapper is set, the cropping planes
24  * are directly propagated to the volume mapper. The widget invokes a
25  * CroppingPlanesPositionChangedEvent when the position of any of the
26  * cropping planes is changed. The widget also invokes an InteractionEvent
27  * in response to user interaction.
28  */
29 
30 #ifndef vtkImageCroppingRegionsWidget_h
31 #define vtkImageCroppingRegionsWidget_h
32 
33 #include "vtk3DWidget.h"
34 #include "vtkInteractionWidgetsModule.h" // For export macro
35 
36 class vtkActor2D;
37 class vtkImageData;
38 class vtkLineSource;
39 class vtkVolumeMapper;
40 class vtkPolyData;
41 
42 class VTKINTERACTIONWIDGETS_EXPORT vtkImageCroppingRegionsWidget : public vtk3DWidget
43 {
44 public:
45   ///@{
46   /**
47    * Standard VTK methods.
48    */
49   static vtkImageCroppingRegionsWidget* New();
50   vtkTypeMacro(vtkImageCroppingRegionsWidget, vtk3DWidget);
51   void PrintSelf(ostream& os, vtkIndent indent) override;
52   ///@}
53 
54   ///@{
55   /**
56    * Place/Adjust widget within bounds
57    */
58   using vtk3DWidget::PlaceWidget;
59   void PlaceWidget(double bounds[6]) override;
60   ///@}
61 
62   /**
63    * Enable/disable the widget
64    */
65   void SetEnabled(int enabling) override;
66 
67   ///@{
68   /**
69    * Set/Get the plane positions that represent the cropped region.
70    */
71   vtkGetVector6Macro(PlanePositions, double);
SetPlanePositions(double pos[6])72   virtual void SetPlanePositions(double pos[6])
73   {
74     this->SetPlanePositions(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
75   }
SetPlanePositions(float pos[6])76   virtual void SetPlanePositions(float pos[6])
77   {
78     this->SetPlanePositions(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
79   }
80   virtual void SetPlanePositions(
81     double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
82   ///@}
83 
84   ///@{
85   /**
86    * Set/Get the cropping region flags
87    */
88   virtual void SetCroppingRegionFlags(int flags);
89   vtkGetMacro(CroppingRegionFlags, int);
90   ///@}
91 
92   /**
93    * Set/get the slice orientation
94    */
95 
96   enum
97   {
98     SLICE_ORIENTATION_YZ = 0,
99     SLICE_ORIENTATION_XZ = 1,
100     SLICE_ORIENTATION_XY = 2
101   };
102 
103   vtkGetMacro(SliceOrientation, int);
104   virtual void SetSliceOrientation(int orientation);
SetSliceOrientationToXY()105   virtual void SetSliceOrientationToXY()
106   {
107     this->SetSliceOrientation(vtkImageCroppingRegionsWidget::SLICE_ORIENTATION_XY);
108   }
SetSliceOrientationToYZ()109   virtual void SetSliceOrientationToYZ()
110   {
111     this->SetSliceOrientation(vtkImageCroppingRegionsWidget::SLICE_ORIENTATION_YZ);
112   }
SetSliceOrientationToXZ()113   virtual void SetSliceOrientationToXZ()
114   {
115     this->SetSliceOrientation(vtkImageCroppingRegionsWidget::SLICE_ORIENTATION_XZ);
116   }
117 
118   ///@{
119   /**
120    * Set/Get the slice number
121    */
122   virtual void SetSlice(int num);
123   vtkGetMacro(Slice, int);
124   ///@}
125 
126   ///@{
127   /**
128    * Set/Get line 1 color
129    */
130   virtual void SetLine1Color(double r, double g, double b);
SetLine1Color(double rgb[3])131   virtual void SetLine1Color(double rgb[3]) { this->SetLine1Color(rgb[0], rgb[1], rgb[2]); }
132   virtual double* GetLine1Color();
133   virtual void GetLine1Color(double rgb[3]);
134   ///@}
135 
136   ///@{
137   /**
138    * Set/Get line 2 color
139    */
140   virtual void SetLine2Color(double r, double g, double b);
SetLine2Color(double rgb[3])141   virtual void SetLine2Color(double rgb[3]) { this->SetLine2Color(rgb[0], rgb[1], rgb[2]); }
142   virtual double* GetLine2Color();
143   virtual void GetLine2Color(double rgb[3]);
144   ///@}
145 
146   ///@{
147   /**
148    * Set/Get line 3 color
149    */
150   virtual void SetLine3Color(double r, double g, double b);
SetLine3Color(double rgb[3])151   virtual void SetLine3Color(double rgb[3]) { this->SetLine3Color(rgb[0], rgb[1], rgb[2]); }
152   virtual double* GetLine3Color();
153   virtual void GetLine3Color(double rgb[3]);
154   ///@}
155 
156   ///@{
157   /**
158    * Set/Get line 4 color
159    */
160   virtual void SetLine4Color(double r, double g, double b);
SetLine4Color(double rgb[3])161   virtual void SetLine4Color(double rgb[3]) { this->SetLine4Color(rgb[0], rgb[1], rgb[2]); }
162   virtual double* GetLine4Color();
163   virtual void GetLine4Color(double rgb[3]);
164   ///@}
165 
166   ///@{
167   /**
168    * Set/Get the input volume mapper
169    * Update the widget according to its mapper
170    */
171   virtual void SetVolumeMapper(vtkVolumeMapper* mapper);
172   vtkGetObjectMacro(VolumeMapper, vtkVolumeMapper);
173   virtual void UpdateAccordingToInput();
174   ///@}
175 
176   ///@{
177   /**
178    * Callbacks for user interaction.
179    */
180   void MoveHorizontalLine();
181   void MoveVerticalLine();
182   void MoveIntersectingLines();
183   void UpdateCursorIcon();
184   void OnButtonPress();
185   void OnButtonRelease();
186   void OnMouseMove();
187   ///@}
188 
189   /**
190    * Events invoked by this widget
191    */
192   enum WidgetEventIds
193   {
194     CroppingPlanesPositionChangedEvent = 10050
195   };
196 
197 protected:
198   vtkImageCroppingRegionsWidget();
199   ~vtkImageCroppingRegionsWidget() override;
200 
201   vtkVolumeMapper* VolumeMapper;
202 
203   vtkLineSource* LineSources[4];
204   vtkActor2D* LineActors[4];
205   vtkPolyData* RegionPolyData[9];
206   vtkActor2D* RegionActors[9];
207 
208   double PlanePositions[6];
209 
210   int SliceOrientation;
211   int Slice;
212 
213   double GetSlicePosition();
214 
215   int CroppingRegionFlags;
216 
217   int MouseCursorState;
218   int Moving;
219 
220   // Handles the events
221 
222   static void ProcessEvents(
223     vtkObject* object, unsigned long event, void* clientdata, void* calldata);
224 
225   void SetMouseCursor(int state);
226 
227   enum WidgetStates
228   {
229     NoLine = 0,
230     MovingH1AndV1,
231     MovingH2AndV1,
232     MovingH1AndV2,
233     MovingH2AndV2,
234     MovingV1,
235     MovingV2,
236     MovingH1,
237     MovingH2
238   };
239 
240   int ComputeWorldCoordinate(int x, int y, double* coord);
241 
242   void UpdateOpacity();
243   void UpdateGeometry();
244   void ConstrainPlanePositions(double positions[6]);
245 
246 private:
247   vtkImageCroppingRegionsWidget(const vtkImageCroppingRegionsWidget&) = delete;
248   void operator=(const vtkImageCroppingRegionsWidget&) = delete;
249 };
250 
251 #endif
252