trackball.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "trackball.h"
11 #include "camera.h"
12 
13 using namespace Eigen;
14 
15 void Trackball::track(const Vector2i& point2D)
16 {
17  if (mpCamera==0)
18  return;
19  Vector3f newPoint3D;
20  bool newPointOk = mapToSphere(point2D, newPoint3D);
21 
22  if (mLastPointOk && newPointOk)
23  {
24  Vector3f axis = mLastPoint3D.cross(newPoint3D).normalized();
25  float cos_angle = mLastPoint3D.dot(newPoint3D);
26  if ( std::abs(cos_angle) < 1.0 )
27  {
28  float angle = 2. * acos(cos_angle);
29  if (mMode==Around)
30  mpCamera->rotateAroundTarget(Quaternionf(AngleAxisf(angle, axis)));
31  else
32  mpCamera->localRotate(Quaternionf(AngleAxisf(-angle, axis)));
33  }
34  }
35 
36  mLastPoint3D = newPoint3D;
37  mLastPointOk = newPointOk;
38 }
39 
40 bool Trackball::mapToSphere(const Vector2i& p2, Vector3f& v3)
41 {
42  if ((p2.x() >= 0) && (p2.x() <= int(mpCamera->vpWidth())) &&
43  (p2.y() >= 0) && (p2.y() <= int(mpCamera->vpHeight())) )
44  {
45  double x = (double)(p2.x() - 0.5*mpCamera->vpWidth()) / (double)mpCamera->vpWidth();
46  double y = (double)(0.5*mpCamera->vpHeight() - p2.y()) / (double)mpCamera->vpHeight();
47  double sinx = sin(M_PI * x * 0.5);
48  double siny = sin(M_PI * y * 0.5);
49  double sinx2siny2 = sinx * sinx + siny * siny;
50 
51  v3.x() = sinx;
52  v3.y() = siny;
53  v3.z() = sinx2siny2 < 1.0 ? sqrt(1.0 - sinx2siny2) : 0.0;
54 
55  return true;
56  }
57  else
58  return false;
59 }
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
ceres::sin
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:439
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
ceres::acos
Jet< T, N > acos(const Jet< T, N > &f)
Definition: jet.h:432
trackball.h
simple::p2
static Point3 p2
Definition: testInitializePose3.cpp:51
Trackball::track
void track(const Eigen::Vector2i &newPoint2D)
Definition: trackball.cpp:15
Trackball::mapToSphere
bool mapToSphere(const Eigen::Vector2i &p2, Eigen::Vector3f &v3)
Definition: trackball.cpp:40
Eigen::Quaternionf
Quaternion< float > Quaternionf
Definition: 3rdparty/Eigen/Eigen/src/Geometry/Quaternion.h:363
Eigen::AngleAxisf
AngleAxis< float > AngleAxisf
Definition: AngleAxis.h:157
y
Scalar * y
Definition: level1_cplx_impl.h:124
camera.h
abs
#define abs(x)
Definition: datatypes.h:17
v3
Vector v3
Definition: testSerializationBase.cpp:40
M_PI
#define M_PI
Definition: mconf.h:117
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:17:58