1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkSphereWidget.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   vtkSphereWidget
17  * @brief   3D widget for manipulating a sphere
18  *
19  * This 3D widget defines a sphere that can be interactively placed in a
20  * scene.
21  *
22  * To use this object, just invoke SetInteractor() with the argument of the
23  * method a vtkRenderWindowInteractor.  You may also wish to invoke
24  * "PlaceWidget()" to initially position the widget. The interactor will act
25  * normally until the "i" key (for "interactor") is pressed, at which point the
26  * vtkSphereWidget will appear. (See superclass documentation for information
27  * about changing this behavior.)
28  * Events that occur outside of the widget (i.e., no part of
29  * the widget is picked) are propagated to any other registered obsevers
30  * (such as the interaction style).  Turn off the widget by pressing the "i"
31  * key again (or invoke the Off() method).
32  *
33  * The vtkSphereWidget has several methods that can be used in conjunction
34  * with other VTK objects. The Set/GetThetaResolution() and
35  * Set/GetPhiResolution() methods control the number of subdivisions of the
36  * sphere in the theta and phi directions; the GetPolyData() method can be
37  * used to get the polygonal representation and can be used for things like
38  * seeding streamlines. The GetSphere() method returns a sphere implicit
39  * function that can be used for cutting and clipping. Typical usage of the
40  * widget is to make use of the StartInteractionEvent, InteractionEvent, and
41  * EndInteractionEvent events. The InteractionEvent is called on mouse
42  * motion; the other two events are called on button down and button up
43  * (any mouse button).
44  *
45  * Some additional features of this class include the ability to control the
46  * properties of the widget. You can set the properties of the selected and
47  * unselected representations of the sphere.
48  *
49  * @sa
50  * vtk3DWidget vtkLineWidget vtkBoxWidget vtkPlaneWidget
51  */
52 
53 #ifndef vtkSphereWidget_h
54 #define vtkSphereWidget_h
55 
56 #include "vtk3DWidget.h"
57 #include "vtkInteractionWidgetsModule.h" // For export macro
58 #include "vtkSphereSource.h"             // Needed for faster access to the sphere source
59 
60 class vtkActor;
61 class vtkPolyDataMapper;
62 class vtkPoints;
63 class vtkPolyData;
64 class vtkSphereSource;
65 class vtkSphere;
66 class vtkCellPicker;
67 class vtkProperty;
68 
69 #define VTK_SPHERE_OFF 0
70 #define VTK_SPHERE_WIREFRAME 1
71 #define VTK_SPHERE_SURFACE 2
72 
73 class VTKINTERACTIONWIDGETS_EXPORT vtkSphereWidget : public vtk3DWidget
74 {
75 public:
76   /**
77    * Instantiate the object.
78    */
79   static vtkSphereWidget* New();
80 
81   vtkTypeMacro(vtkSphereWidget, vtk3DWidget);
82   void PrintSelf(ostream& os, vtkIndent indent) override;
83 
84   ///@{
85   /**
86    * Methods that satisfy the superclass' API.
87    */
88   void SetEnabled(int) override;
89   void PlaceWidget(double bounds[6]) override;
PlaceWidget()90   void PlaceWidget() override { this->Superclass::PlaceWidget(); }
PlaceWidget(double xmin,double xmax,double ymin,double ymax,double zmin,double zmax)91   void PlaceWidget(
92     double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) override
93   {
94     this->Superclass::PlaceWidget(xmin, xmax, ymin, ymax, zmin, zmax);
95   }
96   ///@}
97 
98   ///@{
99   /**
100    * Set the representation of the sphere. Different representations are
101    * useful depending on the application. The default is
102    * VTK_SPHERE_WIREFRAME.
103    */
104   vtkSetClampMacro(Representation, int, VTK_SPHERE_OFF, VTK_SPHERE_SURFACE);
105   vtkGetMacro(Representation, int);
SetRepresentationToOff()106   void SetRepresentationToOff() { this->SetRepresentation(VTK_SPHERE_OFF); }
SetRepresentationToWireframe()107   void SetRepresentationToWireframe() { this->SetRepresentation(VTK_SPHERE_WIREFRAME); }
SetRepresentationToSurface()108   void SetRepresentationToSurface() { this->SetRepresentation(VTK_SPHERE_SURFACE); }
109   ///@}
110 
111   /**
112    * Set/Get the resolution of the sphere in the Theta direction.
113    * The default is 16.
114    */
SetThetaResolution(int r)115   void SetThetaResolution(int r) { this->SphereSource->SetThetaResolution(r); }
GetThetaResolution()116   int GetThetaResolution() { return this->SphereSource->GetThetaResolution(); }
117 
118   /**
119    * Set/Get the resolution of the sphere in the Phi direction.
120    * The default is 8.
121    */
SetPhiResolution(int r)122   void SetPhiResolution(int r) { this->SphereSource->SetPhiResolution(r); }
GetPhiResolution()123   int GetPhiResolution() { return this->SphereSource->GetPhiResolution(); }
124 
125   ///@{
126   /**
127    * Set/Get the radius of sphere. Default is .5.
128    */
SetRadius(double r)129   void SetRadius(double r)
130   {
131     if (r <= 0)
132     {
133       r = .00001;
134     }
135     this->SphereSource->SetRadius(r);
136   }
GetRadius()137   double GetRadius() { return this->SphereSource->GetRadius(); }
138   ///@}
139 
140   ///@{
141   /**
142    * Set/Get the center of the sphere.
143    */
SetCenter(double x,double y,double z)144   void SetCenter(double x, double y, double z) { this->SphereSource->SetCenter(x, y, z); }
SetCenter(double x[3])145   void SetCenter(double x[3]) { this->SetCenter(x[0], x[1], x[2]); }
GetCenter()146   double* GetCenter() VTK_SIZEHINT(3) { return this->SphereSource->GetCenter(); }
GetCenter(double xyz[3])147   void GetCenter(double xyz[3]) { this->SphereSource->GetCenter(xyz); }
148   ///@}
149 
150   ///@{
151   /**
152    * Enable translation and scaling of the widget. By default, the widget
153    * can be translated and rotated.
154    */
155   vtkSetMacro(Translation, vtkTypeBool);
156   vtkGetMacro(Translation, vtkTypeBool);
157   vtkBooleanMacro(Translation, vtkTypeBool);
158   vtkSetMacro(Scale, vtkTypeBool);
159   vtkGetMacro(Scale, vtkTypeBool);
160   vtkBooleanMacro(Scale, vtkTypeBool);
161   ///@}
162 
163   ///@{
164   /**
165    * The handle sits on the surface of the sphere and may be moved around
166    * the surface by picking (left mouse) and then moving. The position
167    * of the handle can be retrieved, this is useful for positioning cameras
168    * and lights. By default, the handle is turned off.
169    */
170   vtkSetMacro(HandleVisibility, vtkTypeBool);
171   vtkGetMacro(HandleVisibility, vtkTypeBool);
172   vtkBooleanMacro(HandleVisibility, vtkTypeBool);
173   ///@}
174 
175   ///@{
176   /**
177    * Set/Get the direction vector of the handle relative to the center of
178    * the sphere. The direction of the handle is from the sphere center to
179    * the handle position.
180    */
181   vtkSetVector3Macro(HandleDirection, double);
182   vtkGetVector3Macro(HandleDirection, double);
183   ///@}
184 
185   ///@{
186   /**
187    * Get the position of the handle.
188    */
189   vtkGetVector3Macro(HandlePosition, double);
190   ///@}
191 
192   /**
193    * Grab the polydata (including points) that defines the sphere.  The
194    * polydata consists of n+1 points, where n is the resolution of the
195    * sphere. These point values are guaranteed to be up-to-date when either the
196    * InteractionEvent or EndInteraction events are invoked. The user provides
197    * the vtkPolyData and the points and polysphere are added to it.
198    */
199   void GetPolyData(vtkPolyData* pd);
200 
201   /**
202    * Get the spherical implicit function defined by this widget.  Note that
203    * vtkSphere is a subclass of vtkImplicitFunction, meaning that it can be
204    * used by a variety of filters to perform clipping, cutting, and selection
205    * of data.
206    */
207   void GetSphere(vtkSphere* sphere);
208 
209   ///@{
210   /**
211    * Get the sphere properties. The properties of the sphere when selected
212    * and unselected can be manipulated.
213    */
214   vtkGetObjectMacro(SphereProperty, vtkProperty);
215   vtkGetObjectMacro(SelectedSphereProperty, vtkProperty);
216   ///@}
217 
218   ///@{
219   /**
220    * Get the handle properties (the little ball on the sphere is the
221    * handle). The properties of the handle when selected and unselected
222    * can be manipulated.
223    */
224   vtkGetObjectMacro(HandleProperty, vtkProperty);
225   vtkGetObjectMacro(SelectedHandleProperty, vtkProperty);
226   ///@}
227 
228 protected:
229   vtkSphereWidget();
230   ~vtkSphereWidget() override;
231 
232   // Manage the state of the widget
233   int State;
234   enum WidgetState
235   {
236     Start = 0,
237     Moving,
238     Scaling,
239     Positioning,
240     Outside
241   };
242 
243   // handles the events
244   static void ProcessEvents(
245     vtkObject* object, unsigned long event, void* clientdata, void* calldata);
246 
247   // ProcessEvents() dispatches to these methods.
248   void OnLeftButtonDown();
249   void OnLeftButtonUp();
250   void OnRightButtonDown();
251   void OnRightButtonUp();
252   void OnMouseMove();
253 
254   // the sphere
255   vtkActor* SphereActor;
256   vtkPolyDataMapper* SphereMapper;
257   vtkSphereSource* SphereSource;
258   void HighlightSphere(int highlight);
259   void SelectRepresentation();
260 
261   // The representation of the sphere
262   int Representation;
263 
264   // Do the picking
265   vtkCellPicker* Picker;
266 
267   // Register internal Pickers within PickingManager
268   void RegisterPickers() override;
269 
270   // Methods to manipulate the sphere widget
271   vtkTypeBool Translation;
272   vtkTypeBool Scale;
273   void Translate(double* p1, double* p2);
274   void ScaleSphere(double* p1, double* p2, int X, int Y);
275   void MoveHandle(double* p1, double* p2, int X, int Y);
276   void PlaceHandle(double* center, double radius);
277 
278   // Properties used to control the appearance of selected objects and
279   // the manipulator in general.
280   vtkProperty* SphereProperty;
281   vtkProperty* SelectedSphereProperty;
282   vtkProperty* HandleProperty;
283   vtkProperty* SelectedHandleProperty;
284   void CreateDefaultProperties();
285 
286   // Managing the handle
287   vtkActor* HandleActor;
288   vtkPolyDataMapper* HandleMapper;
289   vtkSphereSource* HandleSource;
290   void HighlightHandle(int);
291   vtkTypeBool HandleVisibility;
292   double HandleDirection[3];
293   double HandlePosition[3];
294   void SizeHandles() override;
295 
296 private:
297   vtkSphereWidget(const vtkSphereWidget&) = delete;
298   void operator=(const vtkSphereWidget&) = delete;
299 };
300 
301 #endif
302