1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkParallelopipedRepresentation.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   vtkParallelopipedRepresentation
17  * @brief   Default representation for vtkParallelopipedWidget
18  *
19  * This class provides the default geometrical representation for
20  * vtkParallelopipedWidget. As a result of interactions of the widget, this
21  * representation can take on of the following shapes:
22  * <p>1) A parallelopiped. (8 handles, 6 faces)
23  * <p>2) Paralleopiped with a chair depression on any one handle. (A chair
24  * is a depression on one of the handles that carves inwards so as to allow
25  * the user to visualize cuts in the volume). (14 handles, 9 faces).
26  *
27  * @sa
28  * vtkParallelopipedWidget
29 */
30 
31 #ifndef vtkParallelopipedRepresentation_h
32 #define vtkParallelopipedRepresentation_h
33 
34 #include "vtkInteractionWidgetsModule.h" // For export macro
35 #include "vtkWidgetRepresentation.h"
36 
37 class vtkActor;
38 class vtkPlane;
39 class vtkPoints;
40 class vtkPolyData;
41 class vtkPolyDataMapper;
42 class vtkProperty;
43 class vtkCellArray;
44 class vtkTransform;
45 class vtkHandleRepresentation;
46 class vtkClosedSurfacePointPlacer;
47 class vtkPlaneCollection;
48 class vtkParallelopipedTopology;
49 
50 class VTKINTERACTIONWIDGETS_EXPORT vtkParallelopipedRepresentation
51                               : public vtkWidgetRepresentation
52 {
53 public:
54   /**
55    * Instantiate the class.
56    */
57   static vtkParallelopipedRepresentation *New();
58 
59   //@{
60   /**
61    * Standard methods for instances of this class.
62    */
63   vtkTypeMacro(vtkParallelopipedRepresentation,vtkWidgetRepresentation);
64   void PrintSelf(ostream& os, vtkIndent indent) override;
65   //@}
66 
67   /**
68    * Methods to satisfy the superclass.
69    */
70   void GetActors(vtkPropCollection *pc) override;
71 
72   //@{
73   /**
74    * Place the widget in the scene. You can use either of the two APIs :
75    * 1) PlaceWidget( double bounds[6] )
76    * Creates a cuboid conforming to the said bounds.
77    * 2) PlaceWidget( double corners[8][3] )
78    * Creates a parallelopiped with corners specified. The order in
79    * which corners are specified must obey the following rule:
80    * Corner 0 - 1 - 2 - 3 - 0  forms a face
81    * Corner 4 - 5 - 6 - 7 - 4  forms a face
82    * Corner 0 - 4 - 5 - 1 - 0  forms a face
83    * Corner 1 - 5 - 6 - 2 - 1  forms a face
84    * Corner 2 - 6 - 7 - 3 - 2  forms a face
85    * Corner 3 - 7 - 4 - 0 - 3  forms a face
86    */
87   virtual void PlaceWidget(double corners[8][3]);
88   void PlaceWidget(double bounds[6]) override;
89   //@}
90 
91   //@{
92   /**
93    * The interaction state may be set from a widget (e.g., PointWidget)
94    * or other object. This controls how the interaction with the
95    * widget proceeds.
96    */
97   vtkSetMacro(InteractionState,int);
98   //@}
99 
100   /**
101    * Get the bounding planes of the object. The first 6 planes will
102    * be bounding planes of the parallelopiped. If in chair mode, three
103    * additional planes will be present. The last three planes will be those
104    * of the chair. The normals of all the planes will point into the object.
105    */
106   void GetBoundingPlanes( vtkPlaneCollection *pc );
107 
108   /**
109    * The parallelopiped polydata.
110    */
111   void GetPolyData(vtkPolyData *pd);
112 
113   /**
114    * The parallelopiped polydata.
115    */
116   double *GetBounds() VTK_SIZEHINT(6) override;
117 
118   //@{
119   /**
120    * Set/Get the handle properties.
121    */
122   virtual void SetHandleProperty           (vtkProperty *);
123   virtual void SetHoveredHandleProperty    (vtkProperty *);
124   virtual void SetSelectedHandleProperty   (vtkProperty *);
125   vtkGetObjectMacro(HandleProperty,         vtkProperty  );
126   vtkGetObjectMacro(HoveredHandleProperty,  vtkProperty  );
127   vtkGetObjectMacro(SelectedHandleProperty, vtkProperty  );
128   //@}
129 
130   void SetHandleRepresentation(vtkHandleRepresentation *handle);
131   vtkHandleRepresentation* GetHandleRepresentation(int index);
132 
133   //@{
134   /**
135    * Turns the visibility of the handles on/off. Sometimes they may get in
136    * the way of visualization.
137    */
138   void HandlesOn();
139   void HandlesOff();
140   //@}
141 
142   //@{
143   /**
144    * Get the face properties. When a face is being translated, the face gets
145    * highlighted with the SelectedFaceProperty.
146    */
147   vtkGetObjectMacro(FaceProperty,vtkProperty);
148   vtkGetObjectMacro(SelectedFaceProperty,vtkProperty);
149   //@}
150 
151   //@{
152   /**
153    * Get the outline properties. These are the properties with which the
154    * parallelopiped wireframe is rendered.
155    */
156   vtkGetObjectMacro(OutlineProperty,vtkProperty);
157   vtkGetObjectMacro(SelectedOutlineProperty,vtkProperty);
158   //@}
159 
160   /**
161    * This actually constructs the geometry of the widget from the various
162    * data parameters.
163    */
164   void BuildRepresentation() override;
165 
166   //@{
167   /**
168    * Methods required by vtkProp superclass.
169    */
170   void ReleaseGraphicsResources(vtkWindow *w) override;
171   int  RenderOverlay(vtkViewport *viewport) override;
172   int  RenderOpaqueGeometry(vtkViewport *viewport) override;
173   //@}
174 
175   /**
176    * Given and x-y display coordinate, compute the interaction state of
177    * the widget.
178    */
179   int ComputeInteractionState(int X, int Y, int modify=0) override;
180 
181   // Manage the state of the widget
182   enum _InteractionState
183   {
184     Outside = 0,
185     Inside,
186     RequestResizeParallelopiped,
187     RequestResizeParallelopipedAlongAnAxis,
188     RequestChairMode,
189     RequestTranslateParallelopiped,
190     RequestScaleParallelopiped,
191     RequestRotateParallelopiped,
192     ResizingParallelopiped,
193     ResizingParallelopipedAlongAnAxis,
194     ChairMode,
195     TranslatingParallelopiped,
196     ScalingParallelopiped,
197     RotatingParallelopiped
198   };
199 
200   // Methods to manipulate the piped.
201   virtual void Translate( double translation[3] );
202   virtual void Translate( int X, int Y );
203   virtual void Scale( int X, int Y );
204 
205   /**
206    * Synchronize the parallelopiped handle positions with the
207    * Polygonal datastructure.
208    */
209   virtual void PositionHandles();
210 
211   //@{
212   /**
213    * Minimum thickness for the parallelopiped. User interactions cannot make
214    * any individual axis of the parallopiped thinner than this value.
215    * Default is 0.05 expressed as a fraction of the diagonal of the bounding
216    * box used in the PlaceWidget() invocation.
217    */
218   vtkSetMacro( MinimumThickness, double );
219   vtkGetMacro( MinimumThickness, double );
220   //@}
221 
222 protected:
223   vtkParallelopipedRepresentation();
224   ~vtkParallelopipedRepresentation() override;
225 
226   /**
227    * Translate the nth PtId (0 <= n <= 15) by the specified amount.
228    */
229   void TranslatePoint( int n, const double motionVector[3] );
230 
231   /**
232    * Set the highlight state of a handle.
233    * If handleIdx is -1, the property is applied to all handles.
234    */
235   void SetHandleHighlight( int handleIdx, vtkProperty *property );
236 
237   //@{
238   /**
239    * Highlight face defined by the supplied ptids with the specified property.
240    */
241   void SetFaceHighlight( vtkCellArray * face, vtkProperty * );
242   void HighlightAllFaces();
243   void UnHighlightAllFaces();
244   //@}
245 
246   // Node can be a value within [0,7]. This will create a chair one one of
247   // the handle corners. '0 < InitialChairDepth < 1' value dicates the starting
248   // depth of the cavity.
249   void UpdateChairAtNode( int node );
250 
251   // Removes any existing chairs.
252   void RemoveExistingChairs();
253 
254   // Convenience method to get just the planes that define the parallelopiped.
255   // If we aren't in chair mode, this will be the same as GetBoundingPlanes().
256   // If we are in chair mode, this will be the first 6 planes from amongst
257   // those returned by "GetBoundingPlanes".
258   // All planes have their normals pointing inwards.
259   void GetParallelopipedBoundingPlanes( vtkPlaneCollection * pc );
260 
261   // Convenience method to edefine a plane passing through 3 points.
262   void DefinePlane( vtkPlane *, double p[3][3]);
263 
264   // Convenience method to edefine a plane passing through 3 pointIds of the
265   // parallelopiped. The point Ids must like in the range [0,15], ie the
266   // 15 points comprising the parallelopiped and the chair (also modelled
267   // as a parallelopiped)
268   void DefinePlane( vtkPlane *, vtkIdType, vtkIdType, vtkIdType);
269 
270   vtkActor                          * HexActor;
271   vtkPolyDataMapper                 * HexMapper;
272   vtkPolyData                       * HexPolyData;
273   vtkPoints                         * Points;
274   vtkActor                          * HexFaceActor;
275   vtkPolyDataMapper                 * HexFaceMapper;
276   vtkPolyData                       * HexFacePolyData;
277 
278   double                              LastEventPosition[2];
279 
280   // Cache the axis index used for face aligned resize.
281   int                                 LastResizeAxisIdx;
282 
283   vtkHandleRepresentation           * HandleRepresentation;
284   vtkHandleRepresentation          ** HandleRepresentations;
285   int                                 CurrentHandleIdx;
286   int                                 ChairHandleIdx;
287 
288 
289   // When a chair is carved out for the first time, this is the initial
290   // depth of the chair
291   double                              InitialChairDepth;
292 
293   vtkProperty                       * HandleProperty;
294   vtkProperty                       * HoveredHandleProperty;
295   vtkProperty                       * FaceProperty;
296   vtkProperty                       * OutlineProperty;
297   vtkProperty                       * SelectedHandleProperty;
298   vtkProperty                       * SelectedFaceProperty;
299   vtkProperty                       * SelectedOutlineProperty;
300   vtkClosedSurfacePointPlacer       * ChairPointPlacer;
301   vtkParallelopipedTopology         * Topology;
302   double                              MinimumThickness;
303   double                              AbsoluteMinimumThickness;
304 
305 private:
306   vtkParallelopipedRepresentation(const vtkParallelopipedRepresentation&) = delete;
307   void operator=(const vtkParallelopipedRepresentation&) = delete;
308 };
309 
310 #endif
311