fw4spl
fwVtkBoxRepresentation.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 /*=========================================================================
8  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11  Patched version of vtkBoxRepresentation,
12  see http://public.kitware.com/pipermail/vtkusers/2010-July/110328.html
13  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16  =========================================================================*/
17 
18 /*=========================================================================
19 
20  Program: Visualization Toolkit
21  Module: $RCSfile: fwVtkBoxRepresentation.cxx,v $
22 
23  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
24  All rights reserved.
25  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
26 
27  This software is distributed WITHOUT ANY WARRANTY; without even
28  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
29  PURPOSE. See the above copyright notice for more information.
30 
31  =========================================================================*/
32 
33 #include <vtkBoxRepresentation.h>
34 #include <vtkActor.h>
35 #include <vtkSphereSource.h>
36 #include <vtkPolyDataMapper.h>
37 #include <vtkPolyData.h>
38 #include <vtkCallbackCommand.h>
39 #include <vtkBox.h>
40 #include <vtkPolyData.h>
41 #include <vtkProperty.h>
42 #include <vtkRenderWindowInteractor.h>
43 #include <vtkRenderer.h>
44 #include <vtkInteractorObserver.h>
45 #include <vtkMath.h>
46 #include <vtkCellArray.h>
47 #include <vtkCellPicker.h>
48 #include <vtkTransform.h>
49 #include <vtkDoubleArray.h>
50 #include <vtkBox.h>
51 #include <vtkPlanes.h>
52 #include <vtkCamera.h>
53 #include <vtkAssemblyPath.h>
54 #include <vtkWindow.h>
55 #include <vtkObjectFactory.h>
56 
57 #include "fwRenderVTK/vtk/fwVtkBoxRepresentation.hpp"
58 
59 vtkStandardNewMacro(fwVtkBoxRepresentation);
60 
61 //----------------------------------------------------------------------------
62 fwVtkBoxRepresentation::fwVtkBoxRepresentation() : vtkBoxRepresentation()
63 {
64  // Enable/disable the translation, rotation, and scaling of the widget
65  this->TranslationEnabled = 1;
66  this->RotationEnabled = 1;
67  this->ScalingEnabled = 1;
68 }
69 
70 //----------------------------------------------------------------------
71 void fwVtkBoxRepresentation::WidgetInteraction(double e[2])
72 {
73  // Convert events to appropriate coordinate systems
74  vtkCamera* camera = this->Renderer->GetActiveCamera();
75  if ( !camera )
76  {
77  return;
78  }
79  double focalPoint[4], pickPoint[4], prevPickPoint[4];
80  double z, vpn[3];
81  camera->GetViewPlaneNormal(vpn);
82 
83  // Compute the two points defining the motion vector
84  double pos[3];
85  if ( this->LastPicker == this->HexPicker )
86  {
87  this->HexPicker->GetPickPosition(pos);
88  }
89  else
90  {
91  this->HandlePicker->GetPickPosition(pos);
92  }
93  vtkInteractorObserver::ComputeWorldToDisplay(this->Renderer,
94  pos[0], pos[1], pos[2],
95  focalPoint);
96  z = focalPoint[2];
97  vtkInteractorObserver::ComputeDisplayToWorld(this->Renderer,this->LastEventPosition[0],
98  this->LastEventPosition[1], z, prevPickPoint);
99  vtkInteractorObserver::ComputeDisplayToWorld(this->Renderer, e[0], e[1], z, pickPoint);
100 
101  // Process the motion
102  if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF0 )
103  {
104  this->MoveMinusXFace(prevPickPoint,pickPoint);
105  }
106 
107  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF1 )
108  {
109  this->MovePlusXFace(prevPickPoint,pickPoint);
110  }
111 
112  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF2 )
113  {
114  this->MoveMinusYFace(prevPickPoint,pickPoint);
115  }
116 
117  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF3 )
118  {
119  this->MovePlusYFace(prevPickPoint,pickPoint);
120  }
121 
122  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF4 )
123  {
124  this->MoveMinusZFace(prevPickPoint,pickPoint);
125  }
126 
127  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::MoveF5 )
128  {
129  this->MovePlusZFace(prevPickPoint,pickPoint);
130  }
131 
132  else if ( this->TranslationEnabled && this->InteractionState == fwVtkBoxRepresentation::Translating )
133  {
134  this->Translate(prevPickPoint, pickPoint);
135  }
136 
137  else if ( this->ScalingEnabled && this->InteractionState == fwVtkBoxRepresentation::Scaling )
138  {
139  this->Scale(prevPickPoint, pickPoint,
140  static_cast<int>(e[0]), static_cast<int>(e[1]));
141  }
142 
143  else if ( this->RotationEnabled && this->InteractionState == fwVtkBoxRepresentation::Rotating )
144  {
145  this->Rotate(static_cast<int>(e[0]), static_cast<int>(e[1]), prevPickPoint, pickPoint, vpn);
146  }
147 
148  // Store the start position
149  this->LastEventPosition[0] = e[0];
150  this->LastEventPosition[1] = e[1];
151  this->LastEventPosition[2] = 0.0;
152 }
153 
154 //----------------------------------------------------------------------------
155 void fwVtkBoxRepresentation::PrintSelf(ostream& os, vtkIndent indent)
156 {
157  this->Superclass::PrintSelf(os,indent);
158 
159  os << indent << "Translation Enabled: " << (this->TranslationEnabled ? "On\n" : "Off\n");
160  os << indent << "Scaling Enabled: " << (this->ScalingEnabled ? "On\n" : "Off\n");
161  os << indent << "Rotation Enabled: " << (this->RotationEnabled ? "On\n" : "Off\n");
162 }