Antkeeper  0.0.1
swing-twist-ik-constraint.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
21 #include <cmath>
22 
23 void swing_twist_ik_constraint::set_twist_limit(float min_angle, float max_angle)
24 {
25  m_cos_half_twist_min = std::cos(min_angle * 0.5f);
26  m_sin_half_twist_min = std::sin(min_angle * 0.5f);
27  m_cos_half_twist_max = std::cos(max_angle * 0.5f);
28  m_sin_half_twist_max = std::sin(max_angle * 0.5f);
29 }
30 
32 {
33  constexpr math::fvec3 twist_axis{0.0f, 0.0f, 1.0f};
34 
35  // Decompose rotation into swing and twist components
36  auto [swing, twist] = math::swing_twist(q, twist_axis);
37 
38  // Limit swing
39  // ...
40 
41  // Limit twist
42  if (twist.w() < m_cos_half_twist_min)
43  {
44  twist.w() = m_cos_half_twist_min;
45  twist.z() = m_sin_half_twist_min;
46  }
47  else if (twist.w() > m_cos_half_twist_max)
48  {
49  twist.w() = m_cos_half_twist_max;
50  twist.z() = m_sin_half_twist_max;
51  }
52 
53  // Re-compose rotation from swing and twist components
54  q = math::normalize(swing * twist);
55 }
void solve(math::fquat &q) override
Solves the constraint.
void set_twist_limit(float min_angle, float max_angle)
Sets the twist rotation limit.
quaternion< T > normalize(const quaternion< T > &q)
Normalizes a quaternion.
Definition: quaternion.hpp:679
std::array< quaternion< T >, 2 > swing_twist(const quaternion< T > &q, const vec3< T > &twist_axis, T tolerance=T{1e-6})
Decomposes a quaternion into swing and twist rotation components.
Definition: quaternion.hpp:768
Quaternion composed of a real scalar part and imaginary vector part.
Definition: quaternion.hpp:39
n-dimensional vector.
Definition: vector.hpp:44