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;
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 
158  Quaternionf qa(mViewMatrix.linear());
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  {
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;
223  mProjectionMatrix(2,2) = -(mNearDist + mFarDist) / range;
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 }
uint mVpHeight
Definition: camera.h:100
float mFarDist
Definition: camera.h:115
Scalar * b
Definition: benchVecAdd.cpp:17
Camera & operator=(const Camera &other)
Definition: camera.cpp:34
void rotateAroundTarget(const Eigen::Quaternionf &q)
Definition: camera.cpp:145
GpuHelper gpu
Definition: gpuhelper.cpp:18
static double depth
void zoom(float d)
Definition: camera.cpp:174
Eigen::Vector3f right(void) const
Definition: camera.cpp:97
EIGEN_DEVICE_FUNC Transform inverse(TransformTraits traits=(TransformTraits) Mode) const
Definition: Transform.h:1223
#define M_PI
Definition: main.h:106
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
uint vpHeight(void) const
Definition: camera.h:55
Eigen::Vector3f up(void) const
Definition: camera.cpp:93
static char trans
Eigen::Vector3f direction(void) const
Definition: camera.cpp:89
uint mVpY
Definition: camera.h:99
void localRotate(const Eigen::Quaternionf &q)
Definition: camera.cpp:166
Translation< float, 3 > Translation3f
Definition: Translation.h:170
void setOrientation(const Eigen::Quaternionf &q)
Definition: camera.cpp:133
bool mProjIsUptodate
Definition: camera.h:108
const Eigen::Quaternionf & orientation(void) const
Definition: camera.h:64
EIGEN_DEVICE_FUNC const MatrixType & matrix() const
Definition: Transform.h:389
void setDirection(const Eigen::Vector3f &newDirection)
Definition: camera.cpp:102
float mFovY
Definition: camera.h:113
EIGEN_DEVICE_FUNC ConstTranslationPart translation() const
Definition: Transform.h:404
Eigen::Vector3f mTarget
Definition: camera.h:111
EIGEN_DEVICE_FUNC Quaternion< Scalar > conjugate() const
EIGEN_DEVICE_FUNC ConstLinearPart linear() const
Definition: Transform.h:394
void activateGL(void)
Definition: camera.cpp:238
const Eigen::Matrix4f & projectionMatrix(void) const
Definition: camera.cpp:232
Eigen::Quaternionf orientation
Definition: camera.h:32
void loadMatrix(const Eigen::Matrix< Scalar, 4, 4, _Flags, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:157
Frame mFrame
Definition: camera.h:102
const Eigen::Vector3f & position(void) const
Definition: camera.h:61
bool mViewIsUptodate
Definition: camera.h:107
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
EIGEN_DEVICE_FUNC void setIdentity()
Definition: Transform.h:527
EIGEN_DEVICE_FUNC const Scalar & q
void updateViewMatrix(void) const
Definition: camera.cpp:193
float mNearDist
Definition: camera.h:114
const Eigen::Affine3f & viewMatrix(void) const
Definition: camera.cpp:205
const Eigen::Vector3f & target(void)
Definition: camera.h:76
void setTarget(const Eigen::Vector3f &target)
Definition: camera.cpp:117
void setFovY(float value)
Definition: camera.cpp:83
Eigen::Affine3f mViewMatrix
Definition: camera.h:104
uint mVpWidth
Definition: camera.h:100
uint vpY(void) const
Definition: camera.h:53
The quaternion class used to represent 3D orientations and rotations.
Double_ range(const Point2_ &p, const Point2_ &q)
EIGEN_DEVICE_FUNC const TanReturnType tan() const
uint vpX(void) const
Definition: camera.h:52
float * p
Eigen::Matrix4f mProjectionMatrix
Definition: camera.h:105
Definition: camera.h:17
uint mVpX
Definition: camera.h:99
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Camera(void)
Definition: camera.cpp:18
void setViewport(uint offsetx, uint offsety, uint width, uint height)
Definition: camera.cpp:65
void localTranslate(const Eigen::Vector3f &t)
Definition: camera.cpp:184
uint vpWidth(void) const
Definition: camera.h:54
EIGEN_DEVICE_FUNC Matrix3 toRotationMatrix() const
Eigen::Vector3f unProject(const Eigen::Vector2f &uv, float depth, const Eigen::Matrix4f &invModelview) const
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: camera.h:36
void setFrame(const Frame &f)
Definition: camera.cpp:139
void updateProjectionMatrix(void) const
Definition: camera.cpp:211
Represents an homogeneous transformation in a N dimensional space.
Eigen::Vector3f position
Definition: camera.h:33
Point2 t(10, 10)
void setPosition(const Eigen::Vector3f &pos)
Definition: camera.cpp:127
virtual ~Camera()
Definition: camera.cpp:60


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:00