Debian Codegraph @

avogadro/core/unitcell.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "unitcell.h"
7
8 #include <cmath>
9
10 namespace Avogadro::Core {
11
12 bool UnitCell::isRegular(const Matrix3& m)
13 {
14 constexpr double tiny = 1e-6;
15 const auto a = m.col(0).norm();
16 const auto b = m.col(1).norm();
17 const auto c = m.col(2).norm();
18 const auto w = a * b * c;
19 return w > 0.0 && std::fabs(m.determinant()) > tiny * w;
20 }
21
22 void UnitCell::setCellParameters(Real a_, Real b_, Real c_, Real al, Real be,
23 Real ga)
24 {
25 // Convert parameters to matrix. See "Appendix 2: Coordinate Systems and
26 // Transformations" of the PDB guide (ref v2.2, 4/23/13,
27 // http://www.bmsc.washington.edu/CrystaLinks/man/pdb/guide2.2_frame.html)
28 const Real cosAlpha = std::cos(al);
29 const Real cosBeta = std::cos(be);
30 const Real cosGamma = std::cos(ga);
31 const Real sinGamma = std::sin(ga);
32
33 m_cellMatrix(0, 0) = a_;
34 m_cellMatrix(1, 0) = static_cast<Real>(0.0);
35 m_cellMatrix(2, 0) = static_cast<Real>(0.0);
36
37 m_cellMatrix(0, 1) = b_ * cosGamma;
38 m_cellMatrix(1, 1) = b_ * sinGamma;
39 m_cellMatrix(2, 1) = static_cast<Real>(0.0);
40
41 m_cellMatrix(0, 2) = c_ * cosBeta;
42 m_cellMatrix(1, 2) = c_ * (cosAlpha - cosBeta * cosGamma) / sinGamma;
43 m_cellMatrix(2, 2) =
44 (c_ / sinGamma) *
45 std::sqrt(
46 static_cast<Real>(1.0) -
47 ((cosAlpha * cosAlpha) + (cosBeta * cosBeta) + (cosGamma * cosGamma)) +
48 (static_cast<Real>(2.0) * cosAlpha * cosBeta * cosGamma));
49 computeFractionalMatrix();
50 }
51
52 Real UnitCell::signedAngleRadians(const Vector3& v1, const Vector3& v2,
53 const Vector3& axis)
54 {
55 const Vector3 crossProduct(v1.cross(v2));
56 const Real crossProductNorm(crossProduct.norm());
57 const Real dotProduct(v1.dot(v2));
58 const Real signDet(crossProduct.dot(axis));
59 const Real angle(std::atan2(crossProductNorm, dotProduct));
60 return signDet > 0.f ? angle : -angle;
61 }
62
63 } // namespace Avogadro::Core