Blue Brain BioExplorer
Transformation.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #pragma once
24 
27 
28 SERIALIZATION_ACCESS(Transformation)
29 
30 namespace core
31 {
36 class Transformation : public BaseObject
37 {
38 public:
39  Transformation() = default;
40 
41  Transformation(const Vector3d& translation, const Vector3d& scale, const Quaterniond& rotation,
42  const Vector3d& rotationCenter)
43  : _translation(translation)
44  , _scale(scale)
45  , _rotation(rotation)
46  , _rotationCenter(rotationCenter)
47  {
48  }
49 
50  const Vector3d& getTranslation() const { return _translation; }
51  void setTranslation(const Vector3d& value) { _updateValue(_translation, value); }
52  const Vector3d& getScale() const { return _scale; }
53  void setScale(const Vector3d& value) { _updateValue(_scale, value); }
54  const Quaterniond& getRotation() const { return _rotation; }
55  void setRotation(const Quaterniond& value) { _updateValue(_rotation, value); }
56  const Vector3d& getRotationCenter() const { return _rotationCenter; }
57  void setRotationCenter(const Vector3d& value) { _updateValue(_rotationCenter, value); }
58 
59  bool operator==(const Transformation& rhs) const
60  {
61  return _translation == rhs._translation && _rotation == rhs._rotation && _scale == rhs._scale &&
62  _rotationCenter == rhs._rotationCenter;
63  }
64  bool operator!=(const Transformation& rhs) const { return !(*this == rhs); }
65  // only applies rotation and translation, use scaling separately if needed
66  Matrix4d toMatrix(bool withScale = false) const
67  {
68  Matrix4d matrix;
69  matrix = matrix * glm::translate(_translation);
70  matrix = matrix * glm::translate(_rotationCenter);
71  matrix = matrix * glm::toMat4(_rotation);
72  matrix = matrix * glm::translate(-1.0 * _rotationCenter);
73  if (withScale)
74  {
75  matrix = glm::scale(matrix, _scale);
76  matrix[3][0] *= _scale.x;
77  matrix[3][1] *= _scale.y;
78  matrix[3][2] *= _scale.z;
79  }
80  return matrix;
81  }
82 
83 private:
84  Vector3d _translation{0, 0, 0};
85  Vector3d _scale{1, 1, 1};
86  Quaterniond _rotation{1, 0, 0, 0};
87  Vector3d _rotationCenter{0, 0, 0};
88 
90 };
92 {
93  const auto matrix = a.toMatrix() * b.toMatrix();
94  return {matrix[3], a.getScale() * b.getScale(), matrix, a.getRotationCenter()};
95 }
96 
97 inline Boxd transformBox(const Boxd& box, const Transformation& transformation)
98 {
99  const auto& scale = transformation.getScale();
100  return {transformation.toMatrix() * Vector4d(box.getMin(), 1.) * Vector4d(scale, 1.),
101  transformation.toMatrix() * Vector4d(box.getMax(), 1.) * Vector4d(scale, 1.)};
102 }
103 } // namespace core
#define SERIALIZATION_FRIEND(type)
Definition: Macros.h:32
#define SERIALIZATION_ACCESS(type)
Definition: Macros.h:25
void _updateValue(T &member, const T &newValue, const bool triggerCallback=true)
Definition: BaseObject.h:87
const vec & getMin() const
Definition: MathTypes.h:92
const vec & getMax() const
Definition: MathTypes.h:93
Defines the translation, rotation and scale parameters to be applied to a scene asset.
bool operator==(const Transformation &rhs) const
void setScale(const Vector3d &value)
Transformation(const Vector3d &translation, const Vector3d &scale, const Quaterniond &rotation, const Vector3d &rotationCenter)
Transformation()=default
const Vector3d & getTranslation() const
const Vector3d & getRotationCenter() const
const Vector3d & getScale() const
void setRotation(const Quaterniond &value)
void setTranslation(const Vector3d &value)
void setRotationCenter(const Vector3d &value)
const Quaterniond & getRotation() const
Matrix4d toMatrix(bool withScale=false) const
bool operator!=(const Transformation &rhs) const
glm::mat< 4, 4, double > Matrix4d
Definition: MathTypes.h:124
glm::vec< 3, double > Vector3d
Definition: MathTypes.h:143
Transformation operator*(const Transformation &a, const Transformation &b)
glm::tquat< double, glm::highp > Quaterniond
Double quaternion.
Definition: MathTypes.h:153
glm::vec< 4, double > Vector4d
Definition: MathTypes.h:144
Boxd transformBox(const Boxd &box, const Transformation &transformation)