Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackballBase.cpp
1 #include "TrackballBase.hh"
2 #include <Base/Math/Vector3.hh>
3 #include <Gui/biasgl.h>
4 
5 using namespace BIAS;
6 using namespace BIAS;
7 using namespace std;
8 
9 
11 {
12  Stepsize_ = 0.2;
13  controlledObject_=NULL;
14  UseLeftDouble_ = true;
15 }
16 
17 void TrackballBase::SetStepsize(double stepsize)
18 {
19  Stepsize_ = stepsize;
20 }
21 
22 bool TrackballBase::
24 {
25  switch(key) {
26  case KEY_PGUP : Stepsize_*=1.1; cout<<"[Trackball] stepsize = "
27  <<Stepsize_<<endl;
28  break;
29  case KEY_PGDOWN : Stepsize_*=0.9; cout<<"[Trackball] stepsize = "
30  <<Stepsize_<<endl;
31  break;
32  }
33  return(true);
34 }
35 
37  const double stepsize,
38  const BIAS::Vector3<double>& viewdir,
39  BIAS::Vector3<double>& res_translation
40  )
41 {
42  Vector3<double> direction = viewdir;
43  double distance;
44  distance = direction.NormL2();
45 
46  if (Equal(distance, 0.0))
47  {
48  distance = 1.0;
49  direction = Vector3<double>(0., 0., 1.0);
50  }
51  else
52  {
53  direction.Normalize();
54  }
55 
56  direction.Multiply(stepsize, direction);
57  res_translation = direction;
58 // cout<<res_translation<<endl;
59 }
60 
62  const int x1, const int x2,
63  const int y1, const int y2,
65  double& angleRAD
66  )
67 {
68  //left hand image coordinate system if extended by viewing direction
69  int viewport[4];
70  glGetIntegerv(GL_VIEWPORT, viewport);
71  double width = viewport[2];
72  double height = viewport[3];
73 
74  Vector3<double> C, up(0,1,0);
76  cam->GetExtrinsics(C, up);
77  R = cam->GetMyselfAsProjectionParameterBase()->GetR();
78 
79  // Calculate first vector
80  // The trackball contains the whole scene.
81  double x = x1 / width * 2.0;
82  double y = y1 / height * 2.0;
83  x = x - 1.0;
84  y = 1.0 - y;
85  x *=-1.0;
86  // calculate clicked point on trackball. Trackball has radius 1
87  double z = sqrt(2.0) - x * x - y * y;
88  if (z > 0)
89  {
90  z = sqrt(z);
91  }
92  else
93  {
94  return(false);
95  }
96 
97  Vector3<double> v1 = Vector3<double>(x, y, z);
98  v1.Normalize();
99  //v1 = R*v1;
100 
101  // calculate second vector.
102  x = x2 / width * 2.0;
103  y = y2 / height * 2.0;
104  x = x - 1.0;
105  y = 1.0 - y;
106  x *=-1.0;
107 
108  z = sqrt(2.0) - x * x - y * y;
109  if (z > 0)
110  {
111  z = sqrt(z);
112  }
113  else
114  {
115  return(false);
116  }
117 
118  Vector3<double> v2 = Vector3<double>(x, y, z);
119  v2.Normalize();
120  // v2 = R*v2;
121 
122  // Calculate axis and angle for quaternion
123  //v2.CrossProduct(v1, axis);
124  v1.CrossProduct(v2, axis);
125  axis = R*axis;
126  angleRAD = acos(v1.ScalarProduct(v2));
127  return(true);
128 }
129 
131  BIAS::Vector3<double>& direction)
132 {
134  Vector3<double> C;
135  cam->GetExtrinsics(C,R);
136 
138  A[0] = R[0][2]; A[1] = R[1][2]; A[2] = R[2][2];
139  //A *= 7.0;
140 
141  direction = A;
142 
143  //cout<<"a: "<<A<<" c: "<<C<<" dir: "<<direction<<endl;
144 
145 }
146 
147 //used in middle mouse button case
149  const int x1, const int x2,
150  const int y1, const int y2,
151  const BIAS::Vector3<double>& viewdir,
152  BIAS::Vector3<double>& transl
153  )
154 {
155  Vector3<double> directionRight, directionUp, up(0,1,0), C;
156  cam->GetExtrinsics(C, up);
157  // Derive vector for moving right / left
158  // PointOfInterest_.Sub(C, view);
159  viewdir.CrossProduct(up, directionRight);
160  directionRight.Normalize();
161  directionUp = up;
162 
163  if (x1-x2 != 0)
164  {
165  if (x1 > x2)
166  {
167  // Move left
168  directionRight.MultIP(Stepsize_);
169  //C.AddIP(directionRight);
170  //PointOfInterest_.AddIP(directionRight);
171  }
172  else
173  {
174  // Move right
175  directionRight.MultIP(-Stepsize_);
176  //C.AddIP(directionRight);
177  //PointOfInterest_.AddIP(directionRight);
178  }
179  }
180  else
181  {
182  directionRight.SetZero();
183  }
184 
185  if (y1-y2 != 0)
186  {
187  if (y1 > y2)
188  {
189  // Move up
190  directionUp.MultIP(-Stepsize_);
191  //C.AddIP(directionUp);
192  //PointOfInterest_.AddIP(directionUp);
193  }
194  else
195  {
196  // Move down
197  directionUp.MultIP(Stepsize_);
198  //C.AddIP(directionUp);
199  //PointOfInterest_.AddIP(directionUp);
200  }
201  }
202  else
203  {
204  directionUp.SetZero();
205  }
206  transl = directionRight + directionUp;
207 }
208 
210  const int x1,
211  const int x2,
212  const int y1,
213  const int y2,
214  const BIAS::Vector3<double> viewdir)
215 {
216  int viewport[4];
217  glGetIntegerv(GL_VIEWPORT, viewport);
218  int width = viewport[2];
219  int height = viewport[3];
220  Vector3<double> C, up(0,1,0);
221  cam->GetExtrinsics(C, up);
222  Vector3<double> rotationAxis;
223  // Compute rotation axis and angle
224  Vector3<double> v1 = Vector3<double>((x1 - (width/2.0)),
225  (y1 - (height/2.0)),
226  0);
227 
228  Vector3<double> v2 = Vector3<double>(x2 - (width/2.0),
229  y2 - (height/2.0),
230  0);
231 
232  v1.Normalize();
233  v2.Normalize();
234  v1.CrossProduct(v2, rotationAxis);
235  double rotationAngle = acos(v1.ScalarProduct(v2));
236  if(viewdir.ScalarProduct(rotationAxis) > 0 )
237  {
238  rotationAngle *= -1.0;
239  }
240  return(rotationAngle);
241 }
242 
void GetMoveinViewDirection(GLProjectionParametersInterface *cam, const double stepsize, const BIAS::Vector3< double > &viewdir, BIAS::Vector3< double > &res_translation)
bool SpecialKeyPressed(int key)
react to press of special keys such as F1-F12, ...
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
void GetTranslation(GLProjectionParametersInterface *cam, const int x1, const int x2, const int y1, const int y2, const BIAS::Vector3< double > &viewdir, BIAS::Vector3< double > &transl)
void SetZero()
set all values to 0
Definition: Vector3.hh:559
void Multiply(const T &scalar, Vector3< T > &dest) const
Multiplication with a scalar, storing results in destination vector.
Definition: Vector3.hh:698
void GetViewDirection(GLProjectionParametersInterface *cam, BIAS::Vector3< double > &direction)
3D rotation matrix
Definition: RMatrix.hh:49
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
virtual int GetExtrinsics(BIAS::Vector3< double > &C, BIAS::Vector3< double > &up) const =0
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
void SetStepsize(double stepsize)
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
void MultIP(const T &scalar)
Definition: Vector3.hh:327
double GetRotationAngle(GLProjectionParametersInterface *cam, const int x1, const int x2, const int y1, const int y2, const BIAS::Vector3< double > viewdir)
bool GetRotationAxisAndAngle(GLProjectionParametersInterface *cam, const int x1, const int x2, const int y1, const int y2, BIAS::Vector3< double > &axis, double &angleRAD)
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