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