56 const double *seed_joints,
58 double *joints_out)
const override
61 !std::isfinite(cartesian.y)) {
65 const double radius_squared = cartesian.x * cartesian.x + cartesian.y * cartesian.y;
66 const double cos_theta2 =
67 (radius_squared - link1_ * link1_ - link2_ * link2_) / (2.0 * link1_ * link2_);
68 if(cos_theta2 > 1.0 + 1e-12 || cos_theta2 < -1.0 - 1e-12) {
71 const double clamped = cos_theta2 > 1.0 ? 1.0 : (cos_theta2 < -1.0 ? -1.0 : cos_theta2);
72 const double magnitude = std::acos(clamped);
76 const double elbow = seed_joints[1] < 0.0 ? -magnitude : magnitude;
77 const double theta2 = elbow;
79 std::atan2(cartesian.y, cartesian.x) -
80 std::atan2(link2_ * std::sin(theta2), link1_ + link2_ * std::cos(theta2));
83 const double two_pi = 6.28318530717958647692;
84 theta1 += two_pi * std::floor((seed_joints[0] - theta1) / two_pi + 0.5);
86 joints_out[0] = theta1;
87 joints_out[1] = theta2;
89 if(!std::isfinite(cartesian.z)) {
92 joints_out[2] = cartesian.z;
102 const double pi = 3.14159265358979323846;
103 double folded = std::fmod(std::fabs(joints[1]), 2.0 * pi);
105 folded = 2.0 * pi - folded;
107 const double stretched_margin = folded;
108 const double folded_margin = pi - folded;
109 return stretched_margin < folded_margin ? stretched_margin : folded_margin;