File size: 5,227 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2019 Viktor Titov (DeepSOIC) <vv.titov@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <cassert>
#include "DualQuaternion.h"
// NOLINTBEGIN(readability-identifier-length)
Base::DualQuat Base::operator+(Base::DualQuat a, Base::DualQuat b)
{
return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}
Base::DualQuat Base::operator-(Base::DualQuat a, Base::DualQuat b)
{
return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}
Base::DualQuat Base::operator*(Base::DualQuat a, Base::DualQuat b)
{
return {
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x,
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
};
}
Base::DualQuat Base::operator*(Base::DualQuat a, double b)
{
return {a.x * b, a.y * b, a.z * b, a.w * b};
}
Base::DualQuat Base::operator*(double a, Base::DualQuat b)
{
return {b.x * a, b.y * a, b.z * a, b.w * a};
}
Base::DualQuat Base::operator*(Base::DualQuat a, Base::DualNumber b)
{
return {a.x * b, a.y * b, a.z * b, a.w * b};
}
Base::DualQuat Base::operator*(Base::DualNumber a, Base::DualQuat b)
{
return {b.x * a, b.y * a, b.z * a, b.w * a};
}
Base::DualQuat::DualQuat(Base::DualQuat re, Base::DualQuat du)
: x(re.x.re, du.x.re)
, y(re.y.re, du.y.re)
, z(re.z.re, du.z.re)
, w(re.w.re, du.w.re)
{
assert(re.dual().length() < 1e-12);
assert(du.dual().length() < 1e-12);
}
double Base::DualQuat::dot(Base::DualQuat a, Base::DualQuat b)
{
return a.x.re * b.x.re + a.y.re * b.y.re + a.z.re * b.z.re + a.w.re * b.w.re;
}
Base::DualQuat Base::DualQuat::pow(double t, bool shorten) const
{
/* implemented after "Dual-Quaternions: From Classical Mechanics to
* Computer Graphics and Beyond" by Ben Kenwright www.xbdev.net
* bkenwright@xbdev.net
* http://www.xbdev.net/misc_demos/demos/dual_quaternions_beyond/paper.pdf
*
* There are some differences:
*
* * Special handling of no-rotation situation (because normalization
* multiplier becomes infinite in this situation, breaking the algorithm).
*
* * Dual quaternions are implemented as a collection of dual numbers,
* rather than a collection of two quaternions like it is done in suggested
* implementation in the paper.
*
* * acos replaced with atan2 for improved angle accuracy for small angles
*
* */
double le = this->vec().length();
if (le < 1e-12) {
// special case of no rotation. Interpolate position
return {this->real(), this->dual() * t};
}
double normmult = 1.0 / le;
DualQuat self = *this;
if (shorten) {
if (dot(self, identity()) < -1e-12) { // using negative tolerance instead of zero, for
// stability in situations the choice is ambiguous
// (180-degree rotations)
self = -self;
}
}
// to screw coordinates
double theta = self.theta();
double pitch = -2.0 * self.w.du * normmult;
DualQuat l = self.real().vec()
* normmult; // abusing DualQuat to store vectors. Very handy in this case.
DualQuat m = (self.dual().vec() - pitch / 2 * cos(theta / 2) * l) * normmult;
// interpolate
theta *= t;
pitch *= t;
// back to quaternion
return {
l * sin(theta / 2) + DualQuat(0, 0, 0, cos(theta / 2)),
m * sin(theta / 2) + pitch / 2 * cos(theta / 2) * l
+ DualQuat(0, 0, 0, -pitch / 2 * sin(theta / 2))
};
}
// NOLINTEND(readability-identifier-length)
|