1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef GLWIDGET_CAMERA2D_H
18 #define GLWIDGET_CAMERA2D_H
19 
20 #include <Eigen/Core>
21 #include <Eigen/Geometry>
22 
23 // for now, no rotation
24 
25 class GLWidget_Camera2D
26 {
27 public:
28     // initialization: correctly positionned to see a unit cube
GLWidget_Camera2D()29     GLWidget_Camera2D() : x_(0), y_(0), zoom_(1.0)
30     {
31         setDirty_();
32     }
33 
34     // Getters
x()35     double x()    const { return x_;    }
y()36     double y()    const { return y_;    }
zoom()37     double zoom() const { return zoom_; }
38 
39     // Setters
setX(double x)40     void setX(double x)       { x_=x;       setDirty_(); }
setY(double y)41     void setY(double y)       { y_=y;       setDirty_(); }
setZoom(double zoom)42     void setZoom(double zoom) { zoom_=zoom; setDirty_(); }
43 
44     // Get matrices
viewMatrix()45     Eigen::Affine3d viewMatrix() const
46     {
47         computeViewMatrix_();
48         return viewMatrix_;
49     }
viewMatrixData()50     double * viewMatrixData()
51     {
52         computeViewMatrix_();
53         return viewMatrix_.data();
54     }
55 
viewMatrixInverse()56     Eigen::Affine3d viewMatrixInverse() const
57     {
58         computeViewMatrixInverse_();
59         return viewMatrixInverse_;
60     }
61 
62 private:
63     // Camera parameters
64     double x_;
65     double y_;
66     double zoom_;
67 
68     // Matrices
69     mutable bool viewMatrixDirty_;
70     mutable bool viewMatrixInverseDirty_;
71     mutable Eigen::Affine3d viewMatrix_;
72     mutable Eigen::Affine3d viewMatrixInverse_;
setDirty_()73     void setDirty_()
74     {
75         viewMatrixDirty_ = true;
76         viewMatrixInverseDirty_ = true;
77     }
computeViewMatrix_()78     void computeViewMatrix_() const
79     {
80         if(viewMatrixDirty_)
81         {
82             viewMatrix_ = Eigen::Translation<double,3>(x_, y_, 0) * Eigen::Scaling(zoom_);
83             viewMatrixDirty_ = false;
84         }
85     }
computeViewMatrixInverse_()86     void computeViewMatrixInverse_() const
87     {
88         if(viewMatrixInverseDirty_)
89         {
90             computeViewMatrix_();
91             viewMatrixInverse_ = viewMatrix_.inverse();
92             viewMatrixInverseDirty_ = false;
93         }
94     }
95 
96 public:
97     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
98 };
99 
100 #endif // GLWIDGET_CAMERA2D_H
101