BornAgain  1.19.79
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 GUI::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(const Vector3D& eye_, const Vector3D& ctr_, const Vector3D& up_,
37  QQuaternion const& rot_)
38  : eye(eye_)
39  , ctr(ctr_)
40  , up(up_)
41  , rot(rot_)
42 {
43 }
44 
46 {
47  return Position(eye.interpolateTo(to.eye, r), ctr.interpolateTo(to.ctr, r),
48  up.interpolateTo(to.up, r), QQuaternion::slerp(rot, to.rot, r));
49 }
50 
51 void Camera::lookAt(const Position& pos_)
52 {
53  pos = pos_;
54  // lightPos = pos.eye;
55  set();
56 }
57 
58 void Camera::lookAt3DAxes(const Position& pos3DAxes_)
59 {
60  pos3DAxes = pos3DAxes_;
61  set();
62 }
63 
64 // recalculate dependent params
66 {
67  // For 3D object
68  matModel.setToIdentity();
69  matModel.lookAt((pos.eye - pos.ctr) * zoom + pos.ctr, pos.ctr, pos.up);
70  QQuaternion rt(pos.rot * addRot);
71  matModel.translate(+pos.ctr);
72  matModel.rotate(rt);
73  matModel.translate(-pos.ctr);
74 
75  // For 3D axes
76  matModel3DAxes.setToIdentity(); //
78  pos3DAxes.up); //
79  QQuaternion rt3DAxes(pos3DAxes.rot * addRot);
80  matModel3DAxes.rotate(rt3DAxes);
81 
82  lightPosRotated1 = rt.inverted().rotatedVector(lightPos1);
83 
84  emit updated(*this);
85 }
86 
87 void Camera::setAspectRatio(float ratio)
88 {
89  matProj.setToIdentity();
90  matProj.perspective(vertAngle, ratio, nearPlane, farPlane);
91 }
92 
93 void Camera::turnBy(QQuaternion const& rot)
94 {
95  addRot = rot;
96  set();
97 }
98 
99 void Camera::zoomBy(float zoom_)
100 {
101  zoom = zoom_;
102  set();
103 }
104 
105 void Camera::endTransform(bool keep)
106 {
107  if (keep) {
108  pos.rot = (pos.rot * addRot).normalized();
109  pos.eye = pos.eye * zoom; // TODO limit
110 
111  pos3DAxes.rot = (pos3DAxes.rot * addRot).normalized(); // no zooming for 3D axes
112  }
113  addRot = {};
114  zoom = 1;
115  set();
116 }
117 
118 } // namespace GUI::RealSpace
Defines Camera class.
void updated(Camera const &)
void endTransform(bool keep)
Definition: camera.cpp:105
QQuaternion addRot
Definition: camera.h:77
QMatrix4x4 matModel
Definition: camera.h:78
Position pos3DAxes
Definition: camera.h:68
Vector3D lightPos1
Definition: camera.h:74
void lookAt(const Position &)
camera position for 3D object
Definition: camera.cpp:51
void turnBy(QQuaternion const &)
Definition: camera.cpp:93
void zoomBy(float)
Definition: camera.cpp:99
Vector3D lightPosRotated1
Definition: camera.h:74
QMatrix4x4 matModel3DAxes
Definition: camera.h:79
void lookAt3DAxes(const Position &)
camera position for 3D axes
Definition: camera.cpp:58
QMatrix4x4 matProj
Definition: camera.h:78
void setAspectRatio(float)
Definition: camera.cpp:87
const Vector3D LIGHT3
Definition: camera.cpp:21
const Vector3D LIGHT1
Definition: camera.cpp:19
const Vector3D LIGHT2
Definition: camera.cpp:20
Position interpolateTo(const Position &, float) const
Definition: camera.cpp:45