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#ifndef EIGEN_CAMERA_H
11#define EIGEN_CAMERA_H
12
13#include <Eigen/Geometry>
14#include <QObject>
15// #include <frame.h>
16
17class Frame
18{
19  public:
20    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21
22    inline Frame(const Eigen::Vector3f& pos = Eigen::Vector3f::Zero(),
23                 const Eigen::Quaternionf& o = Eigen::Quaternionf())
24      : orientation(o), position(pos)
25    {}
26    Frame lerp(float alpha, const Frame& other) const
27    {
28      return Frame((1.f-alpha)*position + alpha * other.position,
29                   orientation.slerp(alpha,other.orientation));
30    }
31
32    Eigen::Quaternionf orientation;
33    Eigen::Vector3f position;
34};
35
36class Camera
37{
38  public:
39    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
40
41    Camera(void);
42
43    Camera(const Camera& other);
44
45    virtual ~Camera();
46
47    Camera& operator=(const Camera& other);
48
49    void setViewport(uint offsetx, uint offsety, uint width, uint height);
50    void setViewport(uint width, uint height);
51
52    inline uint vpX(void) const { return mVpX; }
53    inline uint vpY(void) const { return mVpY; }
54    inline uint vpWidth(void) const { return mVpWidth; }
55    inline uint vpHeight(void) const { return mVpHeight; }
56
57    inline float fovY(void) const { return mFovY; }
58    void setFovY(float value);
59
60    void setPosition(const Eigen::Vector3f& pos);
61    inline const Eigen::Vector3f& position(void) const { return mFrame.position; }
62
63    void setOrientation(const Eigen::Quaternionf& q);
64    inline const Eigen::Quaternionf& orientation(void) const { return mFrame.orientation; }
65
66    void setFrame(const Frame& f);
67    const Frame& frame(void) const { return mFrame; }
68
69    void setDirection(const Eigen::Vector3f& newDirection);
70    Eigen::Vector3f direction(void) const;
71    void setUp(const Eigen::Vector3f& vectorUp);
72    Eigen::Vector3f up(void) const;
73    Eigen::Vector3f right(void) const;
74
75    void setTarget(const Eigen::Vector3f& target);
76    inline const Eigen::Vector3f& target(void) { return mTarget; }
77
78    const Eigen::Affine3f& viewMatrix(void) const;
79    const Eigen::Matrix4f& projectionMatrix(void) const;
80
81    void rotateAroundTarget(const Eigen::Quaternionf& q);
82    void localRotate(const Eigen::Quaternionf& q);
83    void zoom(float d);
84
85    void localTranslate(const Eigen::Vector3f& t);
86
87    /** Setup OpenGL matrices and viewport */
88    void activateGL(void);
89
90    Eigen::Vector3f unProject(const Eigen::Vector2f& uv, float depth, const Eigen::Matrix4f& invModelview) const;
91    Eigen::Vector3f unProject(const Eigen::Vector2f& uv, float depth) const;
92
93  protected:
94    void updateViewMatrix(void) const;
95    void updateProjectionMatrix(void) const;
96
97  protected:
98
99    uint mVpX, mVpY;
100    uint mVpWidth, mVpHeight;
101
102    Frame mFrame;
103
104    mutable Eigen::Affine3f mViewMatrix;
105    mutable Eigen::Matrix4f mProjectionMatrix;
106
107    mutable bool mViewIsUptodate;
108    mutable bool mProjIsUptodate;
109
110    // used by rotateAroundTarget
111    Eigen::Vector3f mTarget;
112
113    float mFovY;
114    float mNearDist;
115    float mFarDist;
116};
117
118#endif // EIGEN_CAMERA_H
119