Debian Codegraph @

avogadro/core/angletools.h

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 #ifndef AVOGADRO_CORE_ANGLETOOLS_H
7 #define AVOGADRO_CORE_ANGLETOOLS_H
8
9 #include <avogadro/core/vector.h>
10
11 #include <algorithm>
12 #include <cmath>
13
14 namespace Avogadro {
15
16 /**
17 * Calculate the bond angle between two bond vectors.
18 * @param b0 The first bond vector (a-b)
19 * @param b1 The second bond vector (b-c)
20 * @return The bond angle in degrees.
21 */
22 inline Real bondAngle(const Vector3& b0, const Vector3& b1)
23 {
24 // standard formula, e.g.
25 // https://scicomp.stackexchange.com/q/27689/14517
26 // Since we're using bonds, v. small angles are okay
27 // only problem is if bond lengths are v. v. small
28 // but that's unlikely in practice
29 const Real dot = -1.0 * b0.dot(b1);
30 const Real norms = b0.norm() * b1.norm();
31 return std::acos(dot / norms) * RAD_TO_DEG_D;
32 }
33
34 /**
35 * Calculate the dihedral angle between three bond vectors.
36 * @param b0 The first bond vector (a-b)
37 * @param b1 The second bond vector (b-c)
38 * @param b2 The third bond vector (c-d)
39 * @return The dihedral angle in degrees.
40 */
41 inline Real dihedralAngle(const Vector3& b0, const Vector3& b1,
42 const Vector3& b2)
43 {
44 // See Praxeolitic https://stackoverflow.com/a/34245697/131896
45 const Vector3 n0 = -1.0 * b0;
46 const Vector3 b1n = b1.normalized();
47
48 // v = projection of b0 onto plane perpendicular to b1
49 // = n0 minus component that aligns with b1
50 // w = projection of b2 onto plane perpendicular to b1
51 // = b2 minus component that aligns with b1
52 const Vector3 v = n0 - n0.dot(b1n) * b1n;
53 const Vector3 w = b2 - b2.dot(b1n) * b1n;
54
55 // angle between v and w in a plane is the torsion angle
56 // v and w may not be normalized but that's fine since tan is y/x
57 const Real x(v.dot(w));
58 const Real y(b1n.cross(v).dot(w));
59 return std::atan2(y, x) * RAD_TO_DEG_D;
60 }
61
62 /**
63 * Calculate the angle between three points in space.
64 * @param v1 The first point.
65 * @param v2 The second point (vertex).
66 * @param v3 The third point.
67 * @return The angle in degrees.
68 */
69 inline Real calculateAngle(const Vector3& v1, const Vector3& v2,
70 const Vector3& v3)
71 {
72 Vector3 v12 = v1 - v2;
73 Vector3 v23 = v2 - v3;
74 return bondAngle(v12, v23);
75 }
76
77 /**
78 * Calculate the dihedral angle between four points in space.
79 * @param v1 The first point.
80 * @param v2 The second point.
81 * @param v3 The third point.
82 * @param v4 The fourth point.
83 * @return The dihedral angle in degrees.
84 */
85 inline Real calculateDihedral(const Vector3& v1, const Vector3& v2,
86 const Vector3& v3, const Vector3& v4)
87 {
88 Vector3 v12 = v2 - v1;
89 Vector3 v23 = v3 - v2;
90 Vector3 v34 = v4 - v3;
91 return dihedralAngle(v12, v23, v34);
92 }
93
94 /**
95 * Calculate the Wilson out-of-plane angle for a central atom.
96 * Uses the Wilson angle definition:
97 * sin(chi) = (u1 x u2) . u3 / |u1 x u2|
98 * where u1, u2, u3 are unit vectors from the central atom to b, c, d.
99 * @param point The central atom.
100 * @param b The first surrounding atom.
101 * @param c The second surrounding atom.
102 * @param d The third surrounding atom (out-of-plane atom).
103 * @return The out-of-plane angle in degrees.
104 */
105 inline Real outOfPlaneAngle(const Vector3& point, const Vector3& b,
106 const Vector3& c, const Vector3& d)
107 {
108 Vector3 u1 = b - point;
109 Vector3 u2 = c - point;
110 Vector3 u3 = d - point;
111
112 const Real r1 = u1.norm();
113 const Real r2 = u2.norm();
114 const Real r3 = u3.norm();
115 if (r1 < 1e-10 || r2 < 1e-10 || r3 < 1e-10)
116 return 0.0;
117
118 u1 /= r1;
119 u2 /= r2;
120 u3 /= r3;
121
122 const Vector3 n = u1.cross(u2);
123 const Real sinTheta = n.norm();
124 if (sinTheta < 1e-10)
125 return 0.0;
126
127 Real sinChi = std::clamp(n.dot(u3) / sinTheta, -1.0, 1.0);
128 return std::asin(sinChi) * RAD_TO_DEG_D;
129 }
130
131 } // namespace Avogadro
132
133 #endif // AVOGADRO_CORE_ANGLETOOLS_H