camera.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 "camera.h"
11 
12 #include "gpuhelper.h"
13 #include <GL/glu.h>
14 
15 #include "Eigen/LU"
16 using namespace Eigen;
17 
19  : mViewIsUptodate(false), mProjIsUptodate(false)
20 {
22 
23  mFovY = M_PI/3.;
24  mNearDist = 1.;
25  mFarDist = 50000.;
26 
27  mVpX = 0;
28  mVpY = 0;
29 
30  setPosition(Vector3f::Constant(100.));
31  setTarget(Vector3f::Zero());
32 }
33 
35 {
36  mViewIsUptodate = false;
37  mProjIsUptodate = false;
38 
39  mVpX = other.mVpX;
40  mVpY = other.mVpY;
41  mVpWidth = other.mVpWidth;
42  mVpHeight = other.mVpHeight;
43 
44  mTarget = other.mTarget;
45  mFovY = other.mFovY;
46  mNearDist = other.mNearDist;
47  mFarDist = other.mFarDist;
48 
49  mViewMatrix = other.mViewMatrix;
50  mProjectionMatrix = other.mProjectionMatrix;
51 
52  return *this;
53 }
54 
56 {
57  *this = other;
58 }
59 
61 {
62 }
63 
64 
65 void Camera::setViewport(uint offsetx, uint offsety, uint width, uint height)
66 {
67  mVpX = offsetx;
68  mVpY = offsety;
69  mVpWidth = width;
70  mVpHeight = height;
71 
72  mProjIsUptodate = false;
73 }
74 
75 void Camera::setViewport(uint width, uint height)
76 {
77  mVpWidth = width;
78  mVpHeight = height;
79 
80  mProjIsUptodate = false;
81 }
82 
84 {
85  mFovY = value;
86  mProjIsUptodate = false;
87 }
88 
89 Vector3f Camera::direction(void) const
90 {
91  return - (orientation() * Vector3f::UnitZ());
92 }
93 Vector3f Camera::up(void) const
94 {
95  return orientation() * Vector3f::UnitY();
96 }
97 Vector3f Camera::right(void) const
98 {
99  return orientation() * Vector3f::UnitX();
100 }
101 
102 void Camera::setDirection(const Vector3f& newDirection)
103 {
104  // TODO implement it computing the rotation between newDirection and current dir ?
105  Vector3f up = this->up();
106 
107  Matrix3f camAxes;
108 
109  camAxes.col(2) = (-newDirection).normalized();
110  camAxes.col(0) = up.cross( camAxes.col(2) ).normalized();
111  camAxes.col(1) = camAxes.col(2).cross( camAxes.col(0) ).normalized();
112  setOrientation(Quaternionf(camAxes));
113 
114  mViewIsUptodate = false;
115 }
116 
117 void Camera::setTarget(const Vector3f& target)
118 {
119  mTarget = target;
120  if (!mTarget.isApprox(position()))
121  {
122  Vector3f newDirection = mTarget - position();
123  setDirection(newDirection.normalized());
124  }
125 }
126 
127 void Camera::setPosition(const Vector3f& p)
128 {
129  mFrame.position = p;
130  mViewIsUptodate = false;
131 }
132 
134 {
135  mFrame.orientation = q;
136  mViewIsUptodate = false;
137 }
138 
140 {
141  mFrame = f;
142  mViewIsUptodate = false;
143 }
144 
146 {
147  Matrix4f mrot, mt, mtm;
148 
149  // update the transform matrix
151  Vector3f t = mViewMatrix * mTarget;
152 
154  * q
155  * Translation3f(-t)
156  * mViewMatrix;
157 
159  qa = qa.conjugate();
160  setOrientation(qa);
161  setPosition(- (qa * mViewMatrix.translation()) );
162 
163  mViewIsUptodate = true;
164 }
165 
167 {
168  float dist = (position() - mTarget).norm();
170  mTarget = position() + dist * direction();
171  mViewIsUptodate = false;
172 }
173 
174 void Camera::zoom(float d)
175 {
176  float dist = (position() - mTarget).norm();
177  if(dist > d)
178  {
179  setPosition(position() + direction() * d);
180  mViewIsUptodate = false;
181  }
182 }
183 
184 void Camera::localTranslate(const Vector3f& t)
185 {
186  Vector3f trans = orientation() * t;
187  setPosition( position() + trans );
188  setTarget( mTarget + trans );
189 
190  mViewIsUptodate = false;
191 }
192 
193 void Camera::updateViewMatrix(void) const
194 {
195  if(!mViewIsUptodate)
196  {
197  Quaternionf q = orientation().conjugate();
198  mViewMatrix.linear() = q.toRotationMatrix();
200 
201  mViewIsUptodate = true;
202  }
203 }
204 
205 const Affine3f& Camera::viewMatrix(void) const
206 {
208  return mViewMatrix;
209 }
210 
212 {
213  if(!mProjIsUptodate)
214  {
215  mProjectionMatrix.setIdentity();
216  float aspect = float(mVpWidth)/float(mVpHeight);
217  float theta = mFovY*0.5;
218  float range = mFarDist - mNearDist;
219  float invtan = 1./tan(theta);
220 
221  mProjectionMatrix(0,0) = invtan / aspect;
222  mProjectionMatrix(1,1) = invtan;
224  mProjectionMatrix(3,2) = -1;
225  mProjectionMatrix(2,3) = -2 * mNearDist * mFarDist / range;
226  mProjectionMatrix(3,3) = 0;
227 
228  mProjIsUptodate = true;
229  }
230 }
231 
232 const Matrix4f& Camera::projectionMatrix(void) const
233 {
235  return mProjectionMatrix;
236 }
237 
239 {
240  glViewport(vpX(), vpY(), vpWidth(), vpHeight());
241  gpu.loadMatrix(projectionMatrix(),GL_PROJECTION);
242  gpu.loadMatrix(viewMatrix().matrix(),GL_MODELVIEW);
243 }
244 
245 
246 Vector3f Camera::unProject(const Vector2f& uv, float depth) const
247 {
248  Matrix4f inv = mViewMatrix.inverse().matrix();
249  return unProject(uv, depth, inv);
250 }
251 
252 Vector3f Camera::unProject(const Vector2f& uv, float depth, const Matrix4f& invModelview) const
253 {
256 
257  Vector3f a(2.*uv.x()/float(mVpWidth)-1., 2.*uv.y()/float(mVpHeight)-1., 1.);
258  a.x() *= depth/mProjectionMatrix(0,0);
259  a.y() *= depth/mProjectionMatrix(1,1);
260  a.z() = -depth;
261  // FIXME /\/|
262  Vector4f b = invModelview * Vector4f(a.x(), a.y(), a.z(), 1.);
263  return Vector3f(b.x(), b.y(), b.z());
264 }
Eigen::Translation3f
Translation< float, 3 > Translation3f
Definition: Translation.h:170
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Camera::target
const Eigen::Vector3f & target(void)
Definition: camera.h:76
Camera::activateGL
void activateGL(void)
Definition: camera.cpp:238
Camera::right
Eigen::Vector3f right(void) const
Definition: camera.cpp:97
Camera::projectionMatrix
const Eigen::Matrix4f & projectionMatrix(void) const
Definition: camera.cpp:232
Eigen::Transform::linear
EIGEN_DEVICE_FUNC ConstLinearPart linear() const
Definition: Transform.h:394
Camera::updateViewMatrix
void updateViewMatrix(void) const
Definition: camera.cpp:193
Eigen::Transform
Represents an homogeneous transformation in a N dimensional space.
Definition: ForwardDeclarations.h:294
Camera::~Camera
virtual ~Camera()
Definition: camera.cpp:60
d
static const double d[K][N]
Definition: igam.h:11
Frame::position
Eigen::Vector3f position
Definition: camera.h:33
Camera
Definition: camera.h:36
b
Scalar * b
Definition: benchVecAdd.cpp:17
Camera::mViewMatrix
Eigen::Affine3f mViewMatrix
Definition: camera.h:104
Camera::mVpY
uint mVpY
Definition: camera.h:99
trans
static char trans
Definition: blas_interface.hh:58
Frame
Definition: camera.h:17
Camera::zoom
void zoom(float d)
Definition: camera.cpp:174
Camera::setViewport
void setViewport(uint offsetx, uint offsety, uint width, uint height)
Definition: camera.cpp:65
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
Camera::setFrame
void setFrame(const Frame &f)
Definition: camera.cpp:139
Frame::orientation
Eigen::Quaternionf orientation
Definition: camera.h:32
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
Camera::unProject
Eigen::Vector3f unProject(const Eigen::Vector2f &uv, float depth, const Eigen::Matrix4f &invModelview) const
Camera::setPosition
void setPosition(const Eigen::Vector3f &pos)
Definition: camera.cpp:127
Camera::setTarget
void setTarget(const Eigen::Vector3f &target)
Definition: camera.cpp:117
Camera::mFarDist
float mFarDist
Definition: camera.h:115
Camera::mNearDist
float mNearDist
Definition: camera.h:114
Eigen::Transform::setIdentity
EIGEN_DEVICE_FUNC void setIdentity()
Definition: Transform.h:527
Eigen::Quaternionf
Quaternion< float > Quaternionf
Definition: 3rdparty/Eigen/Eigen/src/Geometry/Quaternion.h:363
Camera::operator=
Camera & operator=(const Camera &other)
Definition: camera.cpp:34
Camera::rotateAroundTarget
void rotateAroundTarget(const Eigen::Quaternionf &q)
Definition: camera.cpp:145
Camera::setFovY
void setFovY(float value)
Definition: camera.cpp:83
Camera::mVpHeight
uint mVpHeight
Definition: camera.h:100
tan
const EIGEN_DEVICE_FUNC TanReturnType tan() const
Definition: ArrayCwiseUnaryOps.h:269
Camera::mFrame
Frame mFrame
Definition: camera.h:102
Eigen::Transform::inverse
EIGEN_DEVICE_FUNC Transform inverse(TransformTraits traits=(TransformTraits) Mode) const
Definition: Transform.h:1223
Camera::position
const Eigen::Vector3f & position(void) const
Definition: camera.h:61
Camera::vpY
uint vpY(void) const
Definition: camera.h:53
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
camera.h
Eigen::Transform::matrix
const EIGEN_DEVICE_FUNC MatrixType & matrix() const
Definition: Transform.h:389
Camera::mViewIsUptodate
bool mViewIsUptodate
Definition: camera.h:107
Camera::vpHeight
uint vpHeight(void) const
Definition: camera.h:55
Eigen::Quaternion
The quaternion class used to represent 3D orientations and rotations.
Definition: ForwardDeclarations.h:293
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Camera::direction
Eigen::Vector3f direction(void) const
Definition: camera.cpp:89
Camera::Camera
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Camera(void)
Definition: camera.cpp:18
Camera::localTranslate
void localTranslate(const Eigen::Vector3f &t)
Definition: camera.cpp:184
Camera::viewMatrix
const Eigen::Affine3f & viewMatrix(void) const
Definition: camera.cpp:205
Camera::up
Eigen::Vector3f up(void) const
Definition: camera.cpp:93
Camera::mVpX
uint mVpX
Definition: camera.h:99
Eigen::Transform::translation
EIGEN_DEVICE_FUNC ConstTranslationPart translation() const
Definition: Transform.h:404
Camera::localRotate
void localRotate(const Eigen::Quaternionf &q)
Definition: camera.cpp:166
p
float * p
Definition: Tutorial_Map_using.cpp:9
Camera::setOrientation
void setOrientation(const Eigen::Quaternionf &q)
Definition: camera.cpp:133
Camera::mVpWidth
uint mVpWidth
Definition: camera.h:100
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
Camera::setDirection
void setDirection(const Eigen::Vector3f &newDirection)
Definition: camera.cpp:102
gpuhelper.h
Camera::mFovY
float mFovY
Definition: camera.h:113
Camera::mProjectionMatrix
Eigen::Matrix4f mProjectionMatrix
Definition: camera.h:105
M_PI
#define M_PI
Definition: mconf.h:117
Camera::updateProjectionMatrix
void updateProjectionMatrix(void) const
Definition: camera.cpp:211
gpu
GpuHelper gpu
Definition: gpuhelper.cpp:18
depth
static double depth
Definition: testSphericalCamera.cpp:64
Camera::vpX
uint vpX(void) const
Definition: camera.h:52
align_3::t
Point2 t(10, 10)
Camera::mTarget
Eigen::Vector3f mTarget
Definition: camera.h:111
Camera::orientation
const Eigen::Quaternionf & orientation(void) const
Definition: camera.h:64
Camera::vpWidth
uint vpWidth(void) const
Definition: camera.h:54
test_callbacks.value
value
Definition: test_callbacks.py:160
Camera::mProjIsUptodate
bool mProjIsUptodate
Definition: camera.h:108
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
GpuHelper::loadMatrix
void loadMatrix(const Eigen::Matrix< Scalar, 4, 4, _Flags, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:157


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:04