ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
math/quaternion.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24/** \file
25 * Quaternion algebra.
26 */
27
28#include "utils/Vector.hpp"
29#include "utils/quaternion.hpp"
30
31#include <cmath>
32#include <limits>
33#include <numbers>
34
35namespace Utils {
36
37/** Convert quaternion to director
38 * @return A (non-normalized) director.
39 */
40template <class T>
42 return {2 * (quat[1] * quat[3] + quat[0] * quat[2]),
43 2 * (quat[2] * quat[3] - quat[0] * quat[1]),
44 quat[0] * quat[0] - quat[1] * quat[1] - quat[2] * quat[2] +
45 quat[3] * quat[3]};
46}
47
48/** Convert director to quaternion
49 * @param d Director
50 * @return A (non-normalized) quaternion from the director, or {1, 0, 0, 0}
51 * if the director is the null vector.
52 */
53template <class T>
55
56 auto const dm = d.norm();
57
58 // null vectors cannot be converted to quaternions
59 if (dm < std::numeric_limits<T>::epsilon()) {
60 return {{{{1, 0, 0, 0}}}};
61 }
62
63 // Calculate angles
64 auto const d_xy = std::sqrt(d[0] * d[0] + d[1] * d[1]);
65 T theta2, phi2;
66 if (d_xy == 0.) {
67 // Here the director is co-linear with the z-axis
68 // We need to distinguish between (0, 0, +d_z) and (0, 0, -d_z)
69 theta2 = (d[2] > 0.) ? 0. : std::numbers::pi_v<T> / 2.;
70 phi2 = 0.;
71 } else {
72 // Here we take care of all other directions
73 // We suppose that theta2 = theta/2 and phi2 = (phi - pi/2)/2,
74 // where angles theta and phi are in spherical coordinates
75 theta2 = std::acos(d[2] / dm) / 2.;
76 // here we do not use the signum function due to the edge case d[1] = 0
77 auto const phi = ((d[1] > 0.) ? 1. : -1.) * std::acos(d[0] / d_xy);
78 phi2 = phi / 2. - std::numbers::pi_v<T> / 4.;
79 }
80
81 // Calculate the quaternion from the angles
82 auto const cos_theta2 = std::cos(theta2);
83 auto const sin_theta2 = std::sin(theta2);
84 auto const cos_phi2 = std::cos(phi2);
85 auto const sin_phi2 = std::sin(phi2);
86 return {{{{cos_theta2 * cos_phi2, -sin_theta2 * cos_phi2,
87 -sin_theta2 * sin_phi2, cos_theta2 * sin_phi2}}}};
88}
89
90} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
T norm() const
Definition Vector.hpp:138
Quaternion< T > convert_director_to_quaternion(Vector< T, 3 > const &d)
Convert director to quaternion.
Vector< T, 3 > convert_quaternion_to_director(Quaternion< T > const &quat)
Convert quaternion to director.
Quaternion implementation and trait types for boost qvm interoperability.
Quaternion representation.