| 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 |
#ifndef FREECAD_BASE_DUAL_QUATERNION_H |
| 26 |
#define FREECAD_BASE_DUAL_QUATERNION_H |
| 27 |
|
| 28 |
#include "DualNumber.h" |
| 29 |
#include <FCGlobal.h> |
| 30 |
|
| 31 |
// NOLINTBEGIN(readability-identifier-length) |
| 32 |
namespace Base |
| 33 |
{ |
| 34 |
|
| 35 |
/** |
| 36 |
* @brief The DualQuat class represents a dual quaternion, as a quaternion of |
| 37 |
* dual number components. Dual quaternions are useful for placement |
| 38 |
* interpolation, see pow method. |
| 39 |
* |
| 40 |
* Rotation is stored as non-dual part of DualQ. Translation is encoded into |
| 41 |
* dual part of DualQuat: |
| 42 |
* DualQuat.dual() = 0.5 * t * r, |
| 43 |
* where t is quaternion with x,y,z of translation and w of 0, and r is the |
| 44 |
* rotation quaternion. |
| 45 |
*/ |
| 46 |
class BaseExport DualQuat |
| 47 |
{ |
| 48 |
public: |
| 49 |
DualNumber x; |
| 50 |
DualNumber y; |
| 51 |
DualNumber z; |
| 52 |
DualNumber w; |
| 53 |
|
| 54 |
public: |
| 55 |
/// default constructor: init with zeros |
| 56 |
DualQuat() = default; |
| 57 |
DualQuat(DualNumber x, DualNumber y, DualNumber z, DualNumber w) |
| 58 |
: x(x) |
| 59 |
, y(y) |
| 60 |
, z(z) |
| 61 |
, w(w) |
| 62 |
{} |
| 63 |
DualQuat(double x, double y, double z, double w, double dx, double dy, double dz, double dw) |
| 64 |
: x(x, dx) |
| 65 |
, y(y, dy) |
| 66 |
, z(z, dz) |
| 67 |
, w(w, dw) |
| 68 |
{} |
| 69 |
DualQuat(double x, double y, double z, double w) |
| 70 |
: x(x) |
| 71 |
, y(y) |
| 72 |
, z(z) |
| 73 |
, w(w) |
| 74 |
{} |
| 75 |
|
| 76 |
/// Builds a dual quaternion from real and dual parts provided as pure real quaternions |
| 77 |
DualQuat(DualQuat re, DualQuat du); |
| 78 |
|
| 79 |
/// returns dual quaternion for identity placement |
| 80 |
static DualQuat identity() |
| 81 |
{ |
| 82 |
return {0.0, 0.0, 0.0, 1.0}; |
| 83 |
} |
| 84 |
|
| 85 |
/// return a copy with dual part zeroed out |
| 86 |
DualQuat real() const |
| 87 |
{ |
| 88 |
return {x.re, y.re, z.re, w.re}; |
| 89 |
} |
| 90 |
|
| 91 |
/// return a real-only quaternion made from dual part of this quaternion. |
| 92 |
DualQuat dual() const |
| 93 |
{ |
| 94 |
return {x.du, y.du, z.du, w.du}; |
| 95 |
} |
| 96 |
|
| 97 |
/// conjugate |
| 98 |
DualQuat conj() const |
| 99 |
{ |
| 100 |
return {-x, -y, -z, w}; |
| 101 |
} |
| 102 |
|
| 103 |
/// return vector part (with scalar part zeroed out) |
| 104 |
DualQuat vec() const |
| 105 |
{ |
| 106 |
return {x, y, z, 0.0}; |
| 107 |
} |
| 108 |
|
| 109 |
/// magnitude of the quaternion |
| 110 |
double length() const |
| 111 |
{ |
| 112 |
return sqrt(x.re * x.re + y.re * y.re + z.re * z.re + w.re * w.re); |
| 113 |
} |
| 114 |
|
| 115 |
/// angle of rotation represented by this quaternion, in radians |
| 116 |
double theta() const |
| 117 |
{ |
| 118 |
return 2.0 * atan2(vec().length(), w.re); |
| 119 |
} |
| 120 |
|
| 121 |
/// dot product between real (rotation) parts of two dual quaternions (to determine if one of |
| 122 |
/// them should be negated for shortest interpolation) |
| 123 |
static double dot(DualQuat a, DualQuat b); |
| 124 |
|
| 125 |
/// ScLERP. t=0.0 returns identity, t=1.0 returns this. t can also be outside of 0..1 bounds. |
| 126 |
DualQuat pow(double t, bool shorten = true) const; |
| 127 |
|
| 128 |
DualQuat operator-() const |
| 129 |
{ |
| 130 |
return {-x, -y, -z, -w}; |
| 131 |
} |
| 132 |
|
| 133 |
// DEBUG |
| 134 |
// void print() const { |
| 135 |
// Console().log("%f, %f, %f, %f; %f, %f, %f, %f", x.re,y.re,z.re,w.re, x.du,y.du,z.du, |
| 136 |
// w.du); |
| 137 |
// } |
| 138 |
}; |
| 139 |
|
| 140 |
BaseExport DualQuat operator+(DualQuat a, DualQuat b); |
| 141 |
BaseExport DualQuat operator-(DualQuat a, DualQuat b); |
| 142 |
BaseExport DualQuat operator*(DualQuat a, DualQuat b); |
| 143 |
|
| 144 |
BaseExport DualQuat operator*(DualQuat a, double b); |
| 145 |
BaseExport DualQuat operator*(double a, DualQuat b); |
| 146 |
BaseExport DualQuat operator*(DualQuat a, DualNumber b); |
| 147 |
BaseExport DualQuat operator*(DualNumber a, DualQuat b); |
| 148 |
|
| 149 |
|
| 150 |
} // namespace Base |
| 151 |
// NOLINTEND(readability-identifier-length) |
| 152 |
|
| 153 |
#endif |