1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkBoxRepresentation.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 vtkBoxRepresentation - a class defining the representation for the vtkBoxWidget2
16 // .SECTION Description
17 // This class is a concrete representation for the vtkBoxWidget2. It
18 // represents a box with seven handles: one on each of the six faces, plus a
19 // center handle. Through interaction with the widget, the box
20 // representation can be arbitrarily positioned in the 3D space.
21 //
22 // To use this representation, you normally use the PlaceWidget() method
23 // to position the widget at a specified region in space.
24 //
25 // .SECTION Caveats
26 // This class, and vtkBoxWidget2, are second generation VTK
27 // widgets. An earlier version of this functionality was defined in the
28 // class vtkBoxWidget.
29 
30 // .SECTION See Also
31 // vtkBoxWidget2 vtkBoxWidget
32 
33 
34 #ifndef vtkBoxRepresentation_h
35 #define vtkBoxRepresentation_h
36 
37 #include "vtkInteractionWidgetsModule.h" // For export macro
38 #include "vtkWidgetRepresentation.h"
39 
40 class vtkActor;
41 class vtkPolyDataMapper;
42 class vtkLineSource;
43 class vtkSphereSource;
44 class vtkCellPicker;
45 class vtkProperty;
46 class vtkPolyData;
47 class vtkPoints;
48 class vtkPolyDataAlgorithm;
49 class vtkPointHandleRepresentation3D;
50 class vtkTransform;
51 class vtkPlanes;
52 class vtkBox;
53 class vtkDoubleArray;
54 class vtkMatrix4x4;
55 
56 
57 class VTKINTERACTIONWIDGETS_EXPORT vtkBoxRepresentation : public vtkWidgetRepresentation
58 {
59 public:
60   // Description:
61   // Instantiate the class.
62   static vtkBoxRepresentation *New();
63 
64   // Description:
65   // Standard methods for the class.
66   vtkTypeMacro(vtkBoxRepresentation,vtkWidgetRepresentation);
67   void PrintSelf(ostream& os, vtkIndent indent);
68 
69   // Description:
70   // Get the planes describing the implicit function defined by the box
71   // widget. The user must provide the instance of the class vtkPlanes. Note
72   // that vtkPlanes is a subclass of vtkImplicitFunction, meaning that it can
73   // be used by a variety of filters to perform clipping, cutting, and
74   // selection of data.  (The direction of the normals of the planes can be
75   // reversed enabling the InsideOut flag.)
76   void GetPlanes(vtkPlanes *planes);
77 
78   // Description:
79   // Set/Get the InsideOut flag. This data member is used in conjunction
80   // with the GetPlanes() method. When off, the normals point out of the
81   // box. When on, the normals point into the hexahedron.  InsideOut is off
82   // by default.
83   vtkSetMacro(InsideOut,int);
84   vtkGetMacro(InsideOut,int);
85   vtkBooleanMacro(InsideOut,int);
86 
87   // Description:
88   // Retrieve a linear transform characterizing the transformation of the
89   // box. Note that the transformation is relative to where PlaceWidget()
90   // was initially called. This method modifies the transform provided. The
91   // transform can be used to control the position of vtkProp3D's, as well as
92   // other transformation operations (e.g., vtkTranformPolyData).
93   virtual void GetTransform(vtkTransform *t);
94 
95   // Description:
96   // Set the position, scale and orientation of the box widget using the
97   // transform specified. Note that the transformation is relative to
98   // where PlaceWidget() was initially called (i.e., the original bounding
99   // box).
100   virtual void SetTransform(vtkTransform* t);
101 
102   // Description:
103   // Grab the polydata (including points) that define the box widget. The
104   // polydata consists of 6 quadrilateral faces and 15 points. The first
105   // eight points define the eight corner vertices; the next six define the
106   // -x,+x, -y,+y, -z,+z face points; and the final point (the 15th out of 15
107   // points) defines the center of the box. These point values are guaranteed
108   // to be up-to-date when either the widget's corresponding InteractionEvent
109   // or EndInteractionEvent events are invoked. The user provides the
110   // vtkPolyData and the points and cells are added to it.
111   void GetPolyData(vtkPolyData *pd);
112 
113   // Description:
114   // Get the handle properties (the little balls are the handles). The
115   // properties of the handles, when selected or normal, can be
116   // specified.
117   vtkGetObjectMacro(HandleProperty,vtkProperty);
118   vtkGetObjectMacro(SelectedHandleProperty,vtkProperty);
119 
120   // Description:
121   // Get the face properties (the faces of the box). The
122   // properties of the face when selected and normal can be
123   // set.
124   vtkGetObjectMacro(FaceProperty,vtkProperty);
125   vtkGetObjectMacro(SelectedFaceProperty,vtkProperty);
126 
127   // Description:
128   // Get the outline properties (the outline of the box). The
129   // properties of the outline when selected and normal can be
130   // set.
131   vtkGetObjectMacro(OutlineProperty,vtkProperty);
132   vtkGetObjectMacro(SelectedOutlineProperty,vtkProperty);
133 
134   // Description:
135   // Control the representation of the outline. This flag enables
136   // face wires. By default face wires are off.
137   void SetOutlineFaceWires(int);
138   vtkGetMacro(OutlineFaceWires,int);
OutlineFaceWiresOn()139   void OutlineFaceWiresOn() {this->SetOutlineFaceWires(1);}
OutlineFaceWiresOff()140   void OutlineFaceWiresOff() {this->SetOutlineFaceWires(0);}
141 
142   // Description:
143   // Control the representation of the outline. This flag enables
144   // the cursor lines running between the handles. By default cursor
145   // wires are on.
146   void SetOutlineCursorWires(int);
147   vtkGetMacro(OutlineCursorWires,int);
OutlineCursorWiresOn()148   void OutlineCursorWiresOn() {this->SetOutlineCursorWires(1);}
OutlineCursorWiresOff()149   void OutlineCursorWiresOff() {this->SetOutlineCursorWires(0);}
150 
151   // Description:
152   // Switches handles (the spheres) on or off by manipulating the underlying
153   // actor visibility.
154   virtual void HandlesOn();
155   virtual void HandlesOff();
156 
157   // Description:
158   // These are methods that satisfy vtkWidgetRepresentation's API.
159   virtual void PlaceWidget(double bounds[6]);
160   virtual void BuildRepresentation();
161   virtual int  ComputeInteractionState(int X, int Y, int modify=0);
162   virtual void StartWidgetInteraction(double e[2]);
163   virtual void WidgetInteraction(double e[2]);
164   virtual double *GetBounds();
165 
166   // Description:
167   // Methods supporting, and required by, the rendering process.
168   virtual void ReleaseGraphicsResources(vtkWindow*);
169   virtual int  RenderOpaqueGeometry(vtkViewport*);
170   virtual int  RenderTranslucentPolygonalGeometry(vtkViewport*);
171   virtual int  HasTranslucentPolygonalGeometry();
172 
173 //BTX - used to manage the state of the widget
174   enum {Outside=0,MoveF0,MoveF1,MoveF2,MoveF3,MoveF4,MoveF5,Translating,Rotating,Scaling};
175 //ETX
176 
177   // Description:
178   // The interaction state may be set from a widget (e.g., vtkBoxWidget2) or
179   // other object. This controls how the interaction with the widget
180   // proceeds. Normally this method is used as part of a handshaking
181   // process with the widget: First ComputeInteractionState() is invoked that
182   // returns a state based on geometric considerations (i.e., cursor near a
183   // widget feature), then based on events, the widget may modify this
184   // further.
185   void SetInteractionState(int state);
186 
187 protected:
188   vtkBoxRepresentation();
189   ~vtkBoxRepresentation();
190 
191   // Manage how the representation appears
192   double LastEventPosition[3];
193 
194   // the hexahedron (6 faces)
195   vtkActor          *HexActor;
196   vtkPolyDataMapper *HexMapper;
197   vtkPolyData       *HexPolyData;
198   vtkPoints         *Points;  //used by others as well
199   double             N[6][3]; //the normals of the faces
200 
201   // A face of the hexahedron
202   vtkActor          *HexFace;
203   vtkPolyDataMapper *HexFaceMapper;
204   vtkPolyData       *HexFacePolyData;
205 
206   // glyphs representing hot spots (e.g., handles)
207   vtkActor          **Handle;
208   vtkPolyDataMapper **HandleMapper;
209   vtkSphereSource   **HandleGeometry;
210   virtual void PositionHandles();
211   int HighlightHandle(vtkProp *prop); //returns cell id
212   void HighlightFace(int cellId);
213   void HighlightOutline(int highlight);
214   virtual void ComputeNormals();
215   virtual void SizeHandles();
216 
217   // wireframe outline
218   vtkActor          *HexOutline;
219   vtkPolyDataMapper *OutlineMapper;
220   vtkPolyData       *OutlinePolyData;
221 
222   // Do the picking
223   vtkCellPicker *HandlePicker;
224   vtkCellPicker *HexPicker;
225   vtkActor *CurrentHandle;
226   int      CurrentHexFace;
227   vtkCellPicker *LastPicker;
228 
229   // Register internal Pickers within PickingManager
230   virtual void RegisterPickers();
231 
232   // Transform the hexahedral points (used for rotations)
233   vtkTransform *Transform;
234 
235   // Support GetBounds() method
236   vtkBox *BoundingBox;
237 
238   // Properties used to control the appearance of selected objects and
239   // the manipulator in general.
240   vtkProperty *HandleProperty;
241   vtkProperty *SelectedHandleProperty;
242   vtkProperty *FaceProperty;
243   vtkProperty *SelectedFaceProperty;
244   vtkProperty *OutlineProperty;
245   vtkProperty *SelectedOutlineProperty;
246   virtual void CreateDefaultProperties();
247 
248   // Control the orientation of the normals
249   int InsideOut;
250   int OutlineFaceWires;
251   int OutlineCursorWires;
252   void GenerateOutline();
253 
254   // Helper methods
255   virtual void Translate(double *p1, double *p2);
256   virtual void Scale(double *p1, double *p2, int X, int Y);
257   virtual void Rotate(int X, int Y, double *p1, double *p2, double *vpn);
258   void MovePlusXFace(double *p1, double *p2);
259   void MoveMinusXFace(double *p1, double *p2);
260   void MovePlusYFace(double *p1, double *p2);
261   void MoveMinusYFace(double *p1, double *p2);
262   void MovePlusZFace(double *p1, double *p2);
263   void MoveMinusZFace(double *p1, double *p2);
264 
265   // Internal ivars for performance
266   vtkPoints      *PlanePoints;
267   vtkDoubleArray *PlaneNormals;
268   vtkMatrix4x4   *Matrix;
269 
270   //"dir" is the direction in which the face can be moved i.e. the axis passing
271   //through the center
272   void MoveFace(double *p1, double *p2, double *dir,
273                 double *x1, double *x2, double *x3, double *x4,
274                 double *x5);
275   //Helper method to obtain the direction in which the face is to be moved.
276   //Handles special cases where some of the scale factors are 0.
277   void GetDirection(const double Nx[3],const double Ny[3],
278                     const double Nz[3], double dir[3]);
279 
280 
281 private:
282   vtkBoxRepresentation(const vtkBoxRepresentation&);  //Not implemented
283   void operator=(const vtkBoxRepresentation&);  //Not implemented
284 };
285 
286 #endif
287