BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
camera.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/view/camera.cpp
6 //! @brief Implements Camera class
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GUI/ba3d/view/camera.h"
16 
17 namespace RealSpace {
18 
19 const Vector3D LIGHT1 = Vector3D(0.5f, 1.0f, 1.0f) * 1000.0f;
20 const Vector3D LIGHT2 = Vector3D(1.0f, 0.0f, 1.0f) * 1000.0f;
21 const Vector3D LIGHT3 = Vector3D(1.0f, 1.0f, 0.0f) * 1000.0f;
22 
24  : pos(Vector3D::_z, Vector3D::_0, Vector3D::_x)
25  , pos3DAxes(Vector3D::_z, Vector3D::_0, Vector3D::_x)
26  , zoom(1)
27  , vertAngle(60)
28  , nearPlane(1)
29  , farPlane(10000)
30  , lightPos1(LIGHT1)
31  , lightPosRotated1(lightPos1)
32 {
33  setAspectRatio(1);
34 }
35 
36 Camera::Position::Position() : eye(), ctr(), up() {}
37 
38 Camera::Position::Position(const Vector3D& eye_, const Vector3D& ctr_, const Vector3D& up_,
39  QQuaternion const& rot_)
40  : eye(eye_), ctr(ctr_), up(up_), rot(rot_)
41 {
42 }
43 
45 {
46  return Position(eye.interpolateTo(to.eye, r), ctr.interpolateTo(to.ctr, r),
47  up.interpolateTo(to.up, r), QQuaternion::slerp(rot, to.rot, r));
48 }
49 
50 void Camera::lookAt(const Position& pos_)
51 {
52  pos = pos_;
53  // lightPos = pos.eye;
54  set();
55 }
56 
57 void Camera::lookAt3DAxes(const Position& pos3DAxes_)
58 {
59  pos3DAxes = pos3DAxes_;
60  set();
61 }
62 
63 // recalculate dependent params
65 {
66  // For 3D object
67  matModel.setToIdentity();
68  matModel.lookAt((pos.eye - pos.ctr) * zoom + pos.ctr, pos.ctr, pos.up);
69  QQuaternion rt(pos.rot * addRot);
70  matModel.translate(+pos.ctr);
71  matModel.rotate(rt);
72  matModel.translate(-pos.ctr);
73 
74  // For 3D axes
75  matModel3DAxes.setToIdentity(); //
77  pos3DAxes.up); //
78  QQuaternion rt3DAxes(pos3DAxes.rot * addRot);
79  matModel3DAxes.rotate(rt3DAxes);
80 
81  lightPosRotated1 = rt.inverted().rotatedVector(lightPos1);
82 
83  emit updated(*this);
84 }
85 
86 void Camera::setAspectRatio(float ratio)
87 {
88  matProj.setToIdentity();
89  matProj.perspective(vertAngle, ratio, nearPlane, farPlane);
90 }
91 
92 void Camera::turnBy(QQuaternion const& rot)
93 {
94  addRot = rot;
95  set();
96 }
97 
98 void Camera::zoomBy(float zoom_)
99 {
100  zoom = zoom_;
101  set();
102 }
103 
104 void Camera::endTransform(bool keep)
105 {
106  if (keep) {
107  pos.rot = (pos.rot * addRot).normalized();
108  pos.eye = pos.eye * zoom; // TODO limit
109 
110  pos3DAxes.rot = (pos3DAxes.rot * addRot).normalized(); // no zooming for 3D axes
111  }
112  addRot = {};
113  zoom = 1;
114  set();
115 }
116 
117 } // namespace RealSpace
Defines Camera class.
Vector3D lightPosRotated1
Definition: camera.h:75
void turnBy(QQuaternion const &)
Definition: camera.cpp:92
void lookAt(const Position &)
Definition: camera.cpp:50
Vector3D lightPos1
Definition: camera.h:75
void zoomBy(float)
Definition: camera.cpp:98
Position pos3DAxes
Definition: camera.h:69
QQuaternion addRot
Definition: camera.h:78
float farPlane
Definition: camera.h:72
void lookAt3DAxes(const Position &)
Definition: camera.cpp:57
Position pos
Definition: camera.h:68
float nearPlane
Definition: camera.h:72
void endTransform(bool keep)
Definition: camera.cpp:104
QMatrix4x4 matModel3DAxes
Definition: camera.h:80
QMatrix4x4 matProj
Definition: camera.h:79
void updated(Camera const &)
void setAspectRatio(float)
Definition: camera.cpp:86
float vertAngle
Definition: camera.h:72
QMatrix4x4 matModel
Definition: camera.h:79
const Vector3D LIGHT3
Definition: camera.cpp:21
const Vector3D LIGHT2
Definition: camera.cpp:20
const Vector3D LIGHT1
Definition: camera.cpp:19
Position interpolateTo(const Position &, float) const
Definition: camera.cpp:44