1 /* 2 Copyright (c) 2015-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Dual quaternions 31 * 32 * Copyright: Timur Gafarov 2015-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.math.dualquaternion; 37 38 import std.math; 39 import std.range; 40 import std.format; 41 42 import dlib.math.vector; 43 import dlib.math.matrix; 44 import dlib.math.quaternion; 45 import dlib.math.transformation; 46 import dlib.math.dual; 47 48 /** 49 * Dual quaternion representation. 50 * Dual quaternion is a generalization of quaternion to dual numbers field. 51 * Similar to the way that simple quaternion represents rotation in 3D space, 52 * dual quaternion represents rigid 3D transformation (translation + rotation), 53 * so it can be used in kinematics. 54 */ 55 struct DualQuaternion(T) 56 { 57 this(Quaternion!(T) q1, Quaternion!(T) q2) 58 { 59 this.q1 = q1; 60 this.q2 = q2; 61 } 62 63 this(Quaternion!(T) r, Vector!(T,3) t) 64 { 65 this.q1 = r; 66 this.q2 = Quaternion!(T)(t * 0.5, 0.0) * r; 67 } 68 69 this(Quaternion!(T) r) 70 { 71 this.q1 = r; 72 this.q2 = Quaternion!(T).identity * r; 73 } 74 75 this(Vector!(T,3) t) 76 { 77 this.q1 = Quaternion!(T).identity; 78 this.q2 = Quaternion!(T)(t * 0.5, 0.0); 79 } 80 81 Vector!(T,3) transform(Vector!(T,3) v) 82 { 83 auto vq = DualQuaternion!(T)( 84 Quaternion!(T).identity, 85 Quaternion!(T)(v.x, v.y, v.z, 0.0)); 86 auto q = this * vq * this.fullConjugate; 87 return q.q2.xyz; 88 } 89 90 Vector!(T,3) rotate(Vector!(T,3) v) 91 { 92 return q1.rotate(v); 93 } 94 95 DualQuaternion!(T) conjugate() 96 { 97 return DualQuaternion!(T)(q1.conj, q2.conj); 98 } 99 100 DualQuaternion!(T) dualConjugate() 101 { 102 return DualQuaternion!(T)(q1, q2 * -1.0); 103 } 104 105 DualQuaternion!(T) fullConjugate() 106 { 107 return DualQuaternion!(T)(q1.conj, q2.conj * -1.0); 108 } 109 110 DualQuaternion!(T) opBinary(string op)(DualQuaternion!(T) d) if (op == "*") 111 { 112 return DualQuaternion!(T)(q1 * d.q1, q1 * d.q2 + q2 * d.q1); 113 } 114 115 DualQuaternion!(T) opBinary(string op)(DualQuaternion!(T) d) if (op == "+") 116 { 117 return DualQuaternion!(T)(q1 + d.q1, q2 + d.q2); 118 } 119 120 DualQuaternion!(T) opBinary(string op)(DualQuaternion!(T) d) if (op == "-") 121 { 122 return DualQuaternion!(T)(q1 - d.q1, q2 - d.q2); 123 } 124 125 /** 126 * Rotation part 127 */ 128 Quaternion!(T) rotation() 129 { 130 return q1; 131 } 132 133 /** 134 * Translation part 135 */ 136 Vector!(T,3) translation() 137 { 138 return (2.0 * q2 * q1.conj).xyz; 139 } 140 141 /** 142 * Convert to 4x4 matrix 143 */ 144 Matrix!(T,4) toMatrix4x4() 145 { 146 // TODO: Can this be done without matrix multiplication? 147 return translationMatrix(translation) * rotation.toMatrix4x4; 148 } 149 150 /** 151 * Dual quaternion norm 152 */ 153 Dual!(T) norm() 154 { 155 auto qq = this * this.conjugate; 156 return Dual!(T)(qq.q1.lengthsqr, qq.q2.lengthsqr).sqrt; 157 } 158 159 /** 160 * Set norm to 1 161 */ 162 DualQuaternion!(T) normalized() 163 { 164 Dual!(T) n = norm; 165 return DualQuaternion!(T)(q1 / n.re, q2 / n.re); 166 } 167 168 /** 169 * Convert to string 170 */ 171 string toString() 172 { 173 auto writer = appender!string(); 174 formattedWrite(writer, "[%s, %s]", q1.arrayof, q2.arrayof); 175 return writer.data; 176 } 177 178 /** 179 * Elements union 180 */ 181 union 182 { 183 struct 184 { 185 /// Rotation part 186 Quaternion!(T) q1; 187 188 /// Translation part 189 Quaternion!(T) q2; 190 } 191 192 /// Elements as static array 193 T[8] arrayof; 194 } 195 } 196 197 /// Alias for single precision DualQuaternion specialization 198 alias DualQuaternionf = DualQuaternion!(float); 199 200 /// Alias for double precision DualQuaternion specialization 201 alias DualQuaterniond = DualQuaternion!(double);