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