Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FlyControl.cpp
1 #include <GLviewer/GLProjectionParametersBase.hh>
2 #include <Base/Math/Vector3.hh>
3 #include <Base/Geometry/RMatrixBase.hh>
4 #include "FlyControl.hh"
5 
6 
7 #include <Gui/biasgl.h>
8 
9 using namespace BIAS;
10 using namespace BIAS;
11 using namespace std;
12 
13 
15 {
16  Stepsize_ = 0.2;
17 }
18 
19 void FlyControl::SetStepsize(double stepsize)
20 {
21  Stepsize_ = stepsize;
22 }
23 
24 void FlyControl::
26 {
27  MoveDirection(Vector3<double>(0.0,0.0,Stepsize_));
28 }
29 
30 void FlyControl::
32 {
33  MoveDirection(Vector3<double>(0.0,0.0,-Stepsize_));
34 }
35 
36 void FlyControl::
38 {
39  MoveDirection(Vector3<double>(Stepsize_,0.0,0.0));
40 }
41 
42 void FlyControl::
44 {
45  MoveDirection(Vector3<double>(-Stepsize_,0.0,0.0));
46 }
47 
48 void FlyControl::
50 {
51  MoveDirection(Vector3<double>(0.0,Stepsize_,0.0));
52 }
53 
54 void FlyControl::
56 {
57  MoveDirection(Vector3<double>(0.0,-Stepsize_,0.0));
58 }
59 
60 void FlyControl::
62 {
63  Vector3<double> C, temp; RMatrix R;
64  controlledObject_->GetExtrinsics(C,R);
65  R.Mult(direction, temp);
66  C.AddIP(temp);
67 
68  controlledObject_->SetExtrinsics(HomgPoint3D(C), R);
69 }
70 
71 bool FlyControl::
72 LeftMouseMoved(int x1, int x2, int y1, int y2)
73 {
74 
75  if (x1-x2 == 0 && y1-y2 == 0) {
76  return true;
77  }
78 
79  //left hand image coordinate system if extended by viewing direction
80 
81  int viewport[4];
82  glGetIntegerv(GL_VIEWPORT, viewport);
83  double width = viewport[2];
84  double height = viewport[3];
85 
86  Vector3<double> C, up, A;
87  controlledObject_->GetExtrinsics(C, up);
88  RMatrix R = controlledObject_->GetMyselfAsProjectionParameterBase()->GetR();
89  A = Vector3<double>(R[0][2], R[1][2], R[2][2]);
90 
91  // Calculate first vector
92  // The trackball contains the whole scene.
93  double x = x1 / width * 2.0;
94  double y = y1 / height * 2.0;
95  x = x - 1.0;
96  y = 1.0 - y;
97  x *=-1.0;
98  // calculate clicked point on trackball. Trackball has radius 1
99  double z = sqrt(2.0) - x * x - y * y;
100  if (z > 0)
101  {
102  z = sqrt(z);
103  }
104  else
105  {
106  return false;
107  }
108  Vector3<double> v1 = Vector3<double>(x, y, z);
109  v1.Normalize();
110  v1 = R*v1;
111 
112  // calculate second vector.
113  x = x2 / width * 2.0;
114  y = y2 / height * 2.0;
115  x = x - 1.0;
116  y = 1.0 - y;
117  x *=-1.0;
118 
119  z = sqrt(2.0) - x * x - y * y;
120  if (z > 0)
121  {
122  z = sqrt(z);
123  }
124  else
125  {
126  return false;
127  }
128 
129  Vector3<double> v2 = Vector3<double>(x, y, z);
130  v2.Normalize();
131  v2 = R*v2;
132 
133  // Calculate axis and angle for quaternion
134  Vector3<double> axis;
135  // old: v1.CrossProduct(v2, axis);
136  v2.CrossProduct(v1, axis);
137  double angleRAD = acos(v1.ScalarProduct(v2));
138  controlledObject_->GetExtrinsics(C, up);
139 
140  // prohibit rolling around viewing direction
141  Vector3<double> temp;
142  R.Transpose().Mult(axis, temp);
143  temp[2] = 0.0;
144  R.Mult(temp, axis);
145  axis.Normalize();
146 
148  rotation.SetValueAsAxisRad(axis, angleRAD);
149 
150  // Derive direction vector and rotate direction and up vector.
151  rotation.MultVec(A, A);
152  rotation.MultVec(up, up);
153 
154  controlledObject_->SetExtrinsics(C, C + A, up);
155 
156  return true;
157 }
158 
159 bool FlyControl::
160 RightMouseMoved(int /*x1*/, int /*x2*/, int y1, int y2)
161 {
162  if (y1-y2 != 0)
163  {
164  if (y1 < y2)
165  {
166  MoveForward();
167  }
168  else
169  {
170  MoveBackward();
171  }
172  }
173  return true;
174 }
175 
176 bool FlyControl::
177 MiddleMouseMoved(int x1, int x2, int y1, int y2)
178 {
179  if (x1-x2 != 0)
180  {
181  if (x1 > x2)
182  {
183  MoveRight();
184  }
185  else
186  {
187  MoveLeft();
188  }
189  }
190  if (y1-y2 != 0)
191  {
192  if (y1 > y2)
193  {
194  MoveUp();
195  }
196  else
197  {
198  MoveDown();
199  }
200  }
201  return true;
202 }
203 
204 bool FlyControl::
206 {
207  if (! UseLeftDouble_) return false;
208 
209  // Get from 2D mouse position 3D point in scene.
210  BIAS::HomgPoint3D homgPoint =
211  controlledObject_->UnProject(x, y);
212  if (homgPoint[3] == 0.0)
213  {
214  BIASERR("Error: Point of interest / 3D point could not be set.");
215  }
216  else
217  {
218  // Set new PoI.
219  Vector3<double> C, up, poi;
220  poi = Vector3<double>(homgPoint[0], homgPoint[1], homgPoint[2]);
221  controlledObject_->GetExtrinsics(C, up);
222  controlledObject_->SetExtrinsics(C, poi, up);
223 
224  // Compute stepsize which is 5% from distance to PoI
225  Vector3<double> distance = C - poi;
226  double stepsize = distance.NormL2()*0.05;
227  SetStepsize(stepsize);
228 
229  cout<<"New stepsize is: "<<stepsize<<endl;
230  }
231  return true;
232 }
233 
234 bool FlyControl::
235 LeftAndRightMouseMoved(int x1, int x2, int y1, int y2)
236 {
237 
238  RMatrix R = controlledObject_->GetMyselfAsProjectionParameterBase()->GetR();
239 
240  int viewport[4];
241  glGetIntegerv(GL_VIEWPORT, viewport);
242  int width = viewport[2];
243  int height = viewport[3];
244 
245  Vector3<double> C, up, viewingDirection;
246  controlledObject_->GetExtrinsics(C, up);
247  viewingDirection = Vector3<double>(R[0][2], R[1][2], R[2][2]);
248 
249  Vector3<double> rotationAxis;
250  // Compute rotation axis and angle
251  Vector3<double> v1 = Vector3<double>((x1 - (width/2.0)),
252  (y1 - (height/2.0)),
253  0);
254 
255  Vector3<double> v2 = Vector3<double>(x2 - (width/2.0),
256  y2 - (height/2.0),
257  0);
258 
259  v1.Normalize();
260  v2.Normalize();
261 
262  v1.CrossProduct(v2, rotationAxis);
263  double rotationAngle = acos(v1.ScalarProduct(v2));
264 
265  if(viewingDirection.ScalarProduct(rotationAxis) > 0 )
266  rotationAngle *= -1.0;
267 
268  RotateAroundViewingDirection(rotationAngle);
269 
270  return true;
271 }
272 
273 void FlyControl::
274 RotateAroundViewingDirection(double rotationAngleRAD)
275 {
276  RMatrix R = controlledObject_->GetMyselfAsProjectionParameterBase()->GetR();
277  Vector3<double> C, up, rotationAxis, A;
278 
279  rotationAxis = Vector3<double>(R[0][2], R[1][2], R[2][2]);
280  controlledObject_->GetExtrinsics(C, up);
281 
282  // Create quaternion and derive new up vector
284  delta.SetValueAsAxisRad(rotationAxis, rotationAngleRAD);
285  delta.MultVec(up, up);
286 
287  controlledObject_->SetExtrinsics(C, C + rotationAxis, up);
288 }
289 
void RotateAroundViewingDirection(double rotationAngleRAD)
Definition: FlyControl.cpp:274
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector3.hh:603
bool LeftAndRightMouseMoved(int x1, int x2, int y1, int y2)
react to mouse movement while right and middle button held down overwrite in derived class of desired...
Definition: FlyControl.cpp:235
bool MiddleMouseMoved(int x1, int x2, int y1, int y2)
react to mouse movement while middle button held down overwrite in derived class of desired ...
Definition: FlyControl.cpp:177
bool LeftMouseMoved(int x1, int x2, int y1, int y2)
react to mouse movement while left button held down overwrite in derived class of desired ...
Definition: FlyControl.cpp:72
bool RightMouseMoved(int x1, int x2, int y1, int y2)
react to mouse movement while right button held down overwrite in derived class of desired ...
Definition: FlyControl.cpp:160
int MultVec(const Vector3< QUAT_TYPE > &vec, Vector3< QUAT_TYPE > &res) const
rotates the given Vector qith the quaternion ( q v q* ) the resulting vector is given in res ...
Definition: Quaternion.hh:136
void SetStepsize(double stepsize)
Stepsize_ defines how fast the camera moves, will be set to 5% of distance to Point of Interest...
Definition: FlyControl.cpp:19
3D rotation matrix
Definition: RMatrix.hh:49
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
void Mult(const Vector3< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector3, storing the result in destvec calculates: dest...
Definition: Matrix3x3.hh:302
bool LeftMouseDoubleClicked(int x, int y)
Definition: FlyControl.cpp:205
void AddIP(const T &scalar)
Addition (in place) of an scalar.
Definition: Vector3.hh:310
void MoveDirection(BIAS::Vector3< double > direction)
Computes from direction the new C vector of the camera.
Definition: FlyControl.cpp:61
void Mult(const T &scalar, Vector3< T > &dest) const
Definition: Vector3.hh:332
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
void MoveForward()
Moves the camera in the named directions, calls MoveDirection.
Definition: FlyControl.cpp:25
class for rotation with axis and angle
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
class BIASGeometryBase_EXPORT HomgPoint3D
void SetValueAsAxisRad(const Vector3< QUAT_TYPE > &axis, QUAT_TYPE angle)
sets the quaternion with given rotation axis and angle (in rad)
Definition: Quaternion.hh:183