| 1 |
{ |
| 2 |
Copyright 2003-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Quaternions (in particular using them to express 3D rotations). |
| 17 |
@noAutoLinkHere } |
| 18 |
unit CastleQuaternions; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses CastleVectors; |
| 25 |
|
| 26 |
type |
| 27 |
{ Quaternions are useful to represent rotations in 3D |
| 28 |
that can be easily applied and combined with other rotations. |
| 29 |
Note that, while this structure (and some, but not all, of it's methods) |
| 30 |
can handle @italic(any) quaternion, you are usually interested |
| 31 |
@italic(only in @bold(unit quaternions), that can represent a 3D rotation). } |
| 32 |
TQuaternion = record |
| 33 |
Data: packed record |
| 34 |
case Integer of |
| 35 |
0: (Vector: TVector3; |
| 36 |
Real: Single); |
| 37 |
1: ({ Alternative, sometimes comfortable, view on quaternion as one |
| 38 |
4-element vector. } |
| 39 |
Vector4: TVector4); |
| 40 |
end; |
| 41 |
|
| 42 |
{ Quaternion representing @italic("no rotation"). |
| 43 |
Note: this is @italic(not) a quaternion filled with zeros |
| 44 |
(the @code(Data.Real) component is 1.0), instead this is a unit quaternion |
| 45 |
that correctly represents @italic("rotation by zero degrees/radians"). } |
| 46 |
class function ZeroRotation: TQuaternion; static; |
| 47 |
|
| 48 |
{ Calculate axis (will be normalized) and angle (will be in radians) |
| 49 |
of rotation encoded in unit quaternion Q. |
| 50 |
This is the reverse of QuatFromAxisAngle. } |
| 51 |
procedure ToAxisAngle(out Axis: TVector3; out AngleRad: Single); overload; |
| 52 |
|
| 53 |
{ Convert quaternion to a rotation axis and angle encoded in 4D vector. |
| 54 |
Axis is normalized if quaternion was also normalized |
| 55 |
(which is true if working with rotation quaternions). |
| 56 |
Angle is in radians. } |
| 57 |
function ToAxisAngle: TVector4; overload; |
| 58 |
|
| 59 |
{ Calculate matrix doing rotation described by unit quaternion. } |
| 60 |
function ToRotationMatrix: TMatrix4; |
| 61 |
function ToRotationMatrix3: TMatrix3; |
| 62 |
|
| 63 |
{ Rotate a point, treating this quaternion as a representation of 3D rotation. |
| 64 |
For this operation to make sense in 3D, this must be a "unit" quaternion |
| 65 |
(which is created by a function like @link(QuatFromAxisAngle)). |
| 66 |
|
| 67 |
You can pass here TVector4, which is then understood to be a 3D |
| 68 |
position in homogeneous coordinates. |
| 69 |
@groupBegin } |
| 70 |
function Rotate(const Point: TVector4): TVector4; overload; |
| 71 |
function Rotate(const Point: TVector3): TVector3; overload; |
| 72 |
{ @groupEnd } |
| 73 |
|
| 74 |
{ Quaternion conjugation. This is just a fancy name for negating @code(Data.Vector). |
| 75 |
@groupBegin } |
| 76 |
function Conjugate: TQuaternion; |
| 77 |
procedure ConjugateMe; deprecated 'use Q := Q.Conjugate'; |
| 78 |
{ @groupEnd } |
| 79 |
|
| 80 |
{ Make the quaternion normalized. } |
| 81 |
function Normalize: TQuaternion; |
| 82 |
procedure NormalizeMe; deprecated 'use Q := Q.Normalize'; |
| 83 |
|
| 84 |
{ Perform normalization but only if the quaternion is detected to be |
| 85 |
"significantly unnormalized". It checks if the quaternion needs |
| 86 |
normalization using fast VectorLenSqr, that is quaternion length |
| 87 |
is not needed for the check (sqrt not needed). Only if it's significantly |
| 88 |
different that 1.0, sqrt is done and quaternion is normalized. |
| 89 |
|
| 90 |
This may be useful if you fear of eventual |
| 91 |
errors because of floating-point error cumulations, e.g. when you |
| 92 |
repeatedly multiply one quaternion by another, and yet another, and yet |
| 93 |
another etc. Calling this will trigger normalization from time to time |
| 94 |
(although will tolerate very small, epsilon-like, differences that are |
| 95 |
normal). Thus it prevents the quaternion from getting "too unnormalized". |
| 96 |
|
| 97 |
Generally, this is not needed, as quaternions are nicely numerically |
| 98 |
stable (which means that quaternion "very slightly unnormalized" will |
| 99 |
only generate "very slightly wrong" results, so it's not that bad). |
| 100 |
And no, I didn't actually observe the need for this in my programs. |
| 101 |
But you can see it actually called when you use TMatrixExaminer and |
| 102 |
deliberately cause spinning by very very large value (e.g. run |
| 103 |
castle-model-viewer and press and hold right key, this will cause model |
| 104 |
spinning very fast, which causes quat multiplication every frame). |
| 105 |
So possibly this would trigger incorrect quaternions at some point. |
| 106 |
|
| 107 |
Anyway, this remains mostly a paranoid correctness measure. } |
| 108 |
procedure LazyNormalizeMe; |
| 109 |
procedure LazyNormalize; deprecated 'use LazyNormalizeMe'; |
| 110 |
|
| 111 |
{ Multiply two quaternions. |
| 112 |
|
| 113 |
Geometric interpretation: If these are unit quaternions representing |
| 114 |
rotations, multiplying them calculates one rotation that has the same |
| 115 |
effect as rotating by Q2 and then by Q1. |
| 116 |
|
| 117 |
Normal of result is equal to norm of Q1 * norm of Q2 (in particular, |
| 118 |
multiplying unit quaternions (used for rotations) yields another unit |
| 119 |
quaternion for sure). } |
| 120 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Q1, Q2: TQuaternion): TQuaternion; |
| 121 |
end; |
| 122 |
|
| 123 |
const |
| 124 |
QuatIdentityRot: TQuaternion = (Data: (Vector: (X: 0; Y: 0; Z: 0); Real: 1)) |
| 125 |
deprecated 'use TQuaternion.ZeroRotation'; |
| 126 |
|
| 127 |
{ Quaternion with given vector. |
| 128 |
See TQuaternion and SLerp for useful things you can do with quaternions. |
| 129 |
See QuatFromAxisAngle if instead you want to initialize quternion with axis+angle vector. |
| 130 |
@seealso QuatFromAxisAngle } |
| 131 |
function Quaternion(const V: TVector4): TQuaternion; |
| 132 |
|
| 133 |
{ Calculate unit quaternion representing rotation around Axis |
| 134 |
by AngleRad angle (in radians). |
| 135 |
|
| 136 |
Axis must be normalized, or you have to pass NormalizeAxis = true |
| 137 |
(then we'll normalize it ourselves inside). Otherwise you will |
| 138 |
get non-normalized quaternion that doesn't represent rotation, |
| 139 |
and is usually useless for us. } |
| 140 |
function QuatFromAxisAngle(const Axis: TVector3; |
| 141 |
const AngleRad: Single; const NormalizeAxis: boolean = false): TQuaternion; overload; |
| 142 |
function QuatFromAxisAngle(const AxisAngle: TVector4; |
| 143 |
const NormalizeAxis: boolean = false): TQuaternion; overload; |
| 144 |
|
| 145 |
{ Initialize rotation quaternion from a 3x3 matrix that contains only rotation. } |
| 146 |
function QuatFromRotationMatrix(const Matrix: TMatrix3): TQuaternion; |
| 147 |
|
| 148 |
{ Interpolate between two rotations, along the shortest path on the unit sphere, |
| 149 |
with constant speed. |
| 150 |
|
| 151 |
The overloaded version that works with TVector4 takes |
| 152 |
a rotation (not a quaternion) expressed as an axis |
| 153 |
(first 3 elements) and angle (in radians, 4th element). |
| 154 |
Axis does not have to be normalized (we'll normalize it). |
| 155 |
This is nice e.g. to interpolate VRML/X3D rotations. |
| 156 |
|
| 157 |
@groupBegin } |
| 158 |
function SLerp(const A: Single; const Q1, Q2: TQuaternion): TQuaternion; overload; |
| 159 |
function SLerp(const A: Single; const Rot1, Rot2: TVector4): TVector4; overload; |
| 160 |
{ @groupEnd } |
| 161 |
|
| 162 |
{ Interpolate between two rotations, along the straightest path on the unit sphere. |
| 163 |
|
| 164 |
This is faster than SLerp, but does not guarantee the interpolated |
| 165 |
result travels with constant speed. |
| 166 |
Often it's not a noticeable / important problem (see |
| 167 |
http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/) |
| 168 |
|
| 169 |
When ForceShortestPath = @false, this doesn't guarantee choosing |
| 170 |
the shortest path. Although it goes through the @italic(straightest) path, |
| 171 |
there are two such paths, it may go through the shorter or longer one. |
| 172 |
Use ForceShortestPath = @true if you want to interpolate through the |
| 173 |
shortest. |
| 174 |
|
| 175 |
The overloaded version that works with TVector4 takes |
| 176 |
a rotation (not a quaternion) expressed as an axis |
| 177 |
(first 3 elements) and angle (in radians, 4th element). |
| 178 |
Axis does not have to be normalized (we'll normalize it). |
| 179 |
This is nice e.g. to interpolate VRML/X3D rotations. |
| 180 |
|
| 181 |
@groupBegin } |
| 182 |
function NLerp(const A: Single; const Q1, Q2: TQuaternion; |
| 183 |
const ForceShortestPath: boolean = true): TQuaternion; overload; |
| 184 |
function NLerp(const A: Single; const Rot1, Rot2: TVector4; |
| 185 |
const ForceShortestPath: boolean = true): TVector4; overload; |
| 186 |
{ @groupEnd } |
| 187 |
|
| 188 |
implementation |
| 189 |
|
| 190 |
uses Math, CastleUtils; |
| 191 |
|
| 192 |
{ TQuaternion ---------------------------------------------------------------- } |
| 193 |
|
| 194 |
class function TQuaternion.ZeroRotation: TQuaternion; {$ifdef FPC}static;{$endif} |
| 195 |
const |
| 196 |
R: TQuaternion = (Data: (Vector: (X: 0; Y: 0; Z: 0); Real: 1)); |
| 197 |
begin |
| 198 |
Result := R; |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TQuaternion.ToAxisAngle(out Axis: TVector3; |
| 202 |
out AngleRad: Single); |
| 203 |
{ Data is a normalized quaternion, so |
| 204 |
Data.Vector = Sin(AngleRad / 2) * Axis |
| 205 |
Data.Real = Cos(AngleRad / 2) |
| 206 |
} |
| 207 |
var |
| 208 |
HalfAngle, SinHalfAngle: Single; |
| 209 |
begin |
| 210 |
{ Use Clamped to secure against cosinus being slightly outside [-1,1], like in SLerp. } |
| 211 |
HalfAngle := ArcCos(Clamped(Data.Real, -1, 1)); |
| 212 |
|
| 213 |
SinHalfAngle := Sin(HalfAngle); |
| 214 |
AngleRad := HalfAngle * 2; |
| 215 |
if IsZero(SinHalfAngle) then |
| 216 |
begin |
| 217 |
{ Then Data.Vector must be zero also... How could this happen? |
| 218 |
SinHalfAngle = 0 means that HalfAngle = Pi * K (e.g. 0). |
| 219 |
So Angle = 2 * Pi * K so there's no rotation actually happening... |
| 220 |
|
| 221 |
Which means that any Axis is Ok (but return anything normalized, |
| 222 |
to keep assertion that returned Axis is normalized). } |
| 223 |
Axis := Vector3(0, 0, 1); |
| 224 |
end else |
| 225 |
Axis := Data.Vector * (1 / SinHalfAngle); |
| 226 |
end; |
| 227 |
|
| 228 |
function TQuaternion.ToAxisAngle: TVector4; |
| 229 |
var |
| 230 |
Axis: TVector3 absolute Result; |
| 231 |
begin |
| 232 |
ToAxisAngle(Axis, Result.W); |
| 233 |
end; |
| 234 |
|
| 235 |
function TQuaternion.Conjugate: TQuaternion; |
| 236 |
begin |
| 237 |
Result.Data.Vector := -Data.Vector; |
| 238 |
Result.Data.Real := Data.Real; |
| 239 |
end; |
| 240 |
|
| 241 |
procedure TQuaternion.ConjugateMe; |
| 242 |
begin |
| 243 |
Data.Vector := -Data.Vector; |
| 244 |
end; |
| 245 |
|
| 246 |
function TQuaternion.Rotate(const Point: TVector4): TVector4; |
| 247 |
begin |
| 248 |
Result := (Self * TQuaternion(Point)).Data.Vector4; |
| 249 |
Result := (TQuaternion(Result) * Conjugate).Data.Vector4; |
| 250 |
end; |
| 251 |
|
| 252 |
function TQuaternion.Rotate(const Point: TVector3): TVector3; |
| 253 |
var |
| 254 |
P4: TVector4; |
| 255 |
begin |
| 256 |
P4 := Vector4(Point, 0); |
| 257 |
P4 := Rotate(P4); |
| 258 |
Result := P4.XYZ; |
| 259 |
end; |
| 260 |
|
| 261 |
function QuatToRotationMatrix(const X, Y, Z, W: Single): TMatrix4; |
| 262 |
var |
| 263 |
XX, YY, ZZ: Single; |
| 264 |
begin |
| 265 |
XX := Sqr(X); |
| 266 |
YY := Sqr(Y); |
| 267 |
ZZ := Sqr(Z); |
| 268 |
|
| 269 |
{ row 0 } |
| 270 |
Result.Data[0, 0] := 1 - 2 * (YY + ZZ); |
| 271 |
Result.Data[1, 0] := 2 * ( X * Y - W * Z ); |
| 272 |
Result.Data[2, 0] := 2 * ( X * Z + W * Y ); |
| 273 |
Result.Data[3, 0] := 0; |
| 274 |
|
| 275 |
{ row 1 } |
| 276 |
Result.Data[0, 1] := 2 * ( X * Y + W * Z ); |
| 277 |
Result.Data[1, 1] := 1 - 2 * (XX + ZZ); |
| 278 |
Result.Data[2, 1] := 2 * ( Y * Z - W * X ); |
| 279 |
Result.Data[3, 1] := 0; |
| 280 |
|
| 281 |
{ row 2 } |
| 282 |
Result.Data[0, 2] := 2 * ( X * Z - W * Y ); |
| 283 |
Result.Data[1, 2] := 2 * ( Y * Z + W * X ); |
| 284 |
Result.Data[2, 2] := 1 - 2 * (XX + YY); |
| 285 |
Result.Data[3, 2] := 0; |
| 286 |
|
| 287 |
{ row 3 - like in identity matrix, |
| 288 |
only 3x3 matrix is interesting in rotations. } |
| 289 |
Result.Data[0, 3] := 0; |
| 290 |
Result.Data[1, 3] := 0; |
| 291 |
Result.Data[2, 3] := 0; |
| 292 |
Result.Data[3, 3] := 1; |
| 293 |
end; |
| 294 |
|
| 295 |
function TQuaternion.ToRotationMatrix: TMatrix4; |
| 296 |
begin |
| 297 |
Result := QuatToRotationMatrix( |
| 298 |
Data.Vector.X, |
| 299 |
Data.Vector.Y, |
| 300 |
Data.Vector.Z, |
| 301 |
Data.Real); |
| 302 |
end; |
| 303 |
|
| 304 |
function QuatToRotationMatrix3(const X, Y, Z, W: Single): TMatrix3; |
| 305 |
var |
| 306 |
XX, YY, ZZ: Single; |
| 307 |
begin |
| 308 |
XX := Sqr(X); |
| 309 |
YY := Sqr(Y); |
| 310 |
ZZ := Sqr(Z); |
| 311 |
|
| 312 |
{ row 0 } |
| 313 |
Result.Data[0, 0] := 1 - 2 * (YY + ZZ); |
| 314 |
Result.Data[1, 0] := 2 * ( X * Y - W * Z ); |
| 315 |
Result.Data[2, 0] := 2 * ( X * Z + W * Y ); |
| 316 |
|
| 317 |
{ row 1 } |
| 318 |
Result.Data[0, 1] := 2 * ( X * Y + W * Z ); |
| 319 |
Result.Data[1, 1] := 1 - 2 * (XX + ZZ); |
| 320 |
Result.Data[2, 1] := 2 * ( Y * Z - W * X ); |
| 321 |
|
| 322 |
{ row 2 } |
| 323 |
Result.Data[0, 2] := 2 * ( X * Z - W * Y ); |
| 324 |
Result.Data[1, 2] := 2 * ( Y * Z + W * X ); |
| 325 |
Result.Data[2, 2] := 1 - 2 * (XX + YY); |
| 326 |
end; |
| 327 |
|
| 328 |
function TQuaternion.ToRotationMatrix3: TMatrix3; |
| 329 |
begin |
| 330 |
Result := QuatToRotationMatrix3( |
| 331 |
Data.Vector.X, |
| 332 |
Data.Vector.Y, |
| 333 |
Data.Vector.Z, |
| 334 |
Data.Real); |
| 335 |
end; |
| 336 |
|
| 337 |
function TQuaternion.Normalize: TQuaternion; |
| 338 |
var |
| 339 |
Len: Single; |
| 340 |
begin |
| 341 |
Len := Data.Vector4.Length; |
| 342 |
if Len <> 0 then |
| 343 |
begin |
| 344 |
Len := 1/Len; |
| 345 |
Result.Data.Vector.X := Data.Vector.X * Len; |
| 346 |
Result.Data.Vector.Y := Data.Vector.Y * Len; |
| 347 |
Result.Data.Vector.Z := Data.Vector.Z * Len; |
| 348 |
Result.Data.Real := Data.Real * Len; |
| 349 |
end; |
| 350 |
end; |
| 351 |
|
| 352 |
procedure TQuaternion.NormalizeMe; |
| 353 |
var |
| 354 |
Len: Single; |
| 355 |
begin |
| 356 |
Len := Data.Vector4.Length; |
| 357 |
if Len <> 0 then |
| 358 |
begin |
| 359 |
Len := 1/Len; |
| 360 |
Data.Vector.X := Data.Vector.X * Len; |
| 361 |
Data.Vector.Y := Data.Vector.Y * Len; |
| 362 |
Data.Vector.Z := Data.Vector.Z * Len; |
| 363 |
Data.Real := Data.Real * Len; |
| 364 |
end; |
| 365 |
end; |
| 366 |
|
| 367 |
procedure TQuaternion.LazyNormalize; |
| 368 |
begin |
| 369 |
LazyNormalizeMe; |
| 370 |
end; |
| 371 |
|
| 372 |
procedure TQuaternion.LazyNormalizeMe; |
| 373 |
var |
| 374 |
Len: Single; |
| 375 |
begin |
| 376 |
Len := Data.Vector4.LengthSqr; |
| 377 |
if (Len - 1) > 0.001 then |
| 378 |
begin |
| 379 |
{ tests: Writeln('quat lazily normed'); } |
| 380 |
Len := Sqrt(Len); |
| 381 |
if Len <> 0 then |
| 382 |
begin |
| 383 |
Len := 1/Len; |
| 384 |
Data.Vector.X := Data.Vector.X * Len; |
| 385 |
Data.Vector.Y := Data.Vector.Y * Len; |
| 386 |
Data.Vector.Z := Data.Vector.Z * Len; |
| 387 |
Data.Real := Data.Real * Len; |
| 388 |
end; |
| 389 |
end; |
| 390 |
end; |
| 391 |
|
| 392 |
class operator TQuaternion.{$ifdef FPC}*{$else}Multiply{$endif} (const Q1, Q2: TQuaternion): TQuaternion; |
| 393 |
begin |
| 394 |
Result.Data.Vector := |
| 395 |
TVector3.CrossProduct(Q1.Data.Vector, Q2.Data.Vector) + |
| 396 |
(Q1.Data.Vector * Q2.Data.Real) + |
| 397 |
(Q2.Data.Vector * Q1.Data.Real); |
| 398 |
|
| 399 |
Result.Data.Real := Q1.Data.Real * Q2.Data.Real - |
| 400 |
TVector3.DotProduct(Q1.Data.Vector, Q2.Data.Vector); |
| 401 |
end; |
| 402 |
|
| 403 |
{ routines ------------------------------------------------------------------- } |
| 404 |
|
| 405 |
function Quaternion(const V: TVector4): TQuaternion; |
| 406 |
begin |
| 407 |
Result.Data.Vector4 := V; |
| 408 |
end; |
| 409 |
|
| 410 |
function QuatFromAxisAngle(const Axis: TVector3; |
| 411 |
const AngleRad: Single; const NormalizeAxis: boolean): TQuaternion; |
| 412 |
var |
| 413 |
SinHalfAngle, CosHalfAngle: Single; |
| 414 |
begin |
| 415 |
{ The quaternion requires half angles. } |
| 416 |
SinCos(AngleRad / 2, SinHalfAngle, CosHalfAngle); |
| 417 |
|
| 418 |
if NormalizeAxis then |
| 419 |
begin |
| 420 |
{ protect from zero-length axis |
| 421 |
(testcase: castle-model-converter demo-models/castle-anim-frames/simple/cube_opening.castle-anim-frames ) } |
| 422 |
if Axis.IsZero then |
| 423 |
Exit(TQuaternion.ZeroRotation); |
| 424 |
SinHalfAngle := SinHalfAngle / Axis.Length; |
| 425 |
end; |
| 426 |
|
| 427 |
Result.Data.Vector := Axis * SinHalfAngle; |
| 428 |
Result.Data.Real := CosHalfAngle; |
| 429 |
end; |
| 430 |
|
| 431 |
function QuatFromAxisAngle(const AxisAngle: TVector4; |
| 432 |
const NormalizeAxis: boolean): TQuaternion; |
| 433 |
var |
| 434 |
Axis: TVector3 absolute AxisAngle; |
| 435 |
begin |
| 436 |
Result := QuatFromAxisAngle(Axis, AxisAngle.W, NormalizeAxis); |
| 437 |
end; |
| 438 |
|
| 439 |
function QuatFromRotationMatrix(const Matrix: TMatrix3): TQuaternion; |
| 440 |
var |
| 441 |
Trace, S: Single; |
| 442 |
begin |
| 443 |
{ Thanks go to Martin John Baker explanation and example code: |
| 444 |
http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ |
| 445 |
See also Kraft QuaternionFromMatrix4x4 implementation. |
| 446 |
} |
| 447 |
|
| 448 |
Trace := Matrix.Data[0, 0] + Matrix.Data[1, 1] + Matrix.Data[2, 2]; |
| 449 |
if Trace > 0 then |
| 450 |
begin |
| 451 |
S := Sqrt(Trace + 1.0) * 2; // S=4*qw |
| 452 |
Result.Data.Vector4.W := 0.25 * S; |
| 453 |
Result.Data.Vector4.X := (Matrix.Data[1, 2] - Matrix.Data[2, 1]) / S; |
| 454 |
Result.Data.Vector4.Y := (Matrix.Data[2, 0] - Matrix.Data[0, 2]) / S; |
| 455 |
Result.Data.Vector4.Z := (Matrix.Data[0, 1] - Matrix.Data[1, 0]) / S; |
| 456 |
end else |
| 457 |
if (Matrix.Data[0, 0] > Matrix.Data[1, 1]) and (Matrix.Data[0, 0] > Matrix.Data[2, 2]) then |
| 458 |
begin |
| 459 |
S := Sqrt(1.0 + Matrix.Data[0, 0] - Matrix.Data[1, 1] - Matrix.Data[2, 2]) * 2; // S=4*qx |
| 460 |
Result.Data.Vector4.W := (Matrix.Data[1, 2] - Matrix.Data[2, 1]) / S; |
| 461 |
Result.Data.Vector4.X := 0.25 * S; |
| 462 |
Result.Data.Vector4.Y := (Matrix.Data[1, 0] + Matrix.Data[0, 1]) / S; |
| 463 |
Result.Data.Vector4.Z := (Matrix.Data[2, 0] + Matrix.Data[0, 2]) / S; |
| 464 |
end else |
| 465 |
if (Matrix.Data[1, 1] > Matrix.Data[2, 2]) then |
| 466 |
begin |
| 467 |
S := Sqrt(1.0 + Matrix.Data[1, 1] - Matrix.Data[0, 0] - Matrix.Data[2, 2]) * 2; // S=4*qy |
| 468 |
Result.Data.Vector4.W := (Matrix.Data[2, 0] - Matrix.Data[0, 2]) / S; |
| 469 |
Result.Data.Vector4.X := (Matrix.Data[1, 0] + Matrix.Data[0, 1]) / S; |
| 470 |
Result.Data.Vector4.Y := 0.25 * S; |
| 471 |
Result.Data.Vector4.Z := (Matrix.Data[2, 1] + Matrix.Data[1, 2]) / S; |
| 472 |
end else |
| 473 |
begin |
| 474 |
S := Sqrt(1.0 + Matrix.Data[2, 2] - Matrix.Data[0, 0] - Matrix.Data[1, 1]) * 2; // S=4*qz |
| 475 |
Result.Data.Vector4.W := (Matrix.Data[0, 1] - Matrix.Data[1, 0]) / S; |
| 476 |
Result.Data.Vector4.X := (Matrix.Data[2, 0] + Matrix.Data[0, 2]) / S; |
| 477 |
Result.Data.Vector4.Y := (Matrix.Data[2, 1] + Matrix.Data[1, 2]) / S; |
| 478 |
Result.Data.Vector4.Z := 0.25 * S; |
| 479 |
end; |
| 480 |
|
| 481 |
Result := Result.Normalize; |
| 482 |
end; |
| 483 |
|
| 484 |
{ For SLerp and NLerp implementations, see |
| 485 |
http://www.3dkingdoms.com/weekly/weekly.php?a=36 |
| 486 |
http://www.3dkingdoms.com/weekly/quat.h |
| 487 |
http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/ |
| 488 |
http://en.wikipedia.org/wiki/Slerp |
| 489 |
} |
| 490 |
function SLerp(const A: Single; const Q1, Q2: TQuaternion): TQuaternion; |
| 491 |
const |
| 492 |
{ Do not make this Epsilon too large. |
| 493 |
See https://github.com/castle-engine/castle-engine/issues/342 |
| 494 |
and TTestCastleQuaternions.TestSlerpEpsilon } |
| 495 |
Epsilon = 1E-6; |
| 496 |
var |
| 497 |
W1, W2, NegateOneQuaternion: Single; |
| 498 |
CosTheta, Theta: Float; |
| 499 |
SinTheta: Single; |
| 500 |
begin |
| 501 |
CosTheta := TVector4.DotProduct(Q1.Data.Vector4, Q2.Data.Vector4); |
| 502 |
|
| 503 |
{ Following wikipedia: |
| 504 |
Long paths can be prevented by negating one end if the dot product, |
| 505 |
CosTheta, is negative. See also |
| 506 |
http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm |
| 507 |
http://www.shatters.net/forum/viewtopic.php?f=4&t=10955&p=86203 |
| 508 |
|
| 509 |
We do it by NegateOneQuaternion, either 1 (not negate) or -1 (negate). |
| 510 |
Later W1 will be multiplied by this. |
| 511 |
This way the actual negation of Q1 happens when it's multiplied by W1, |
| 512 |
so at ~zero cost. } |
| 513 |
if CosTheta < 0 then |
| 514 |
begin |
| 515 |
NegateOneQuaternion := -1; |
| 516 |
CosTheta := -CosTheta; |
| 517 |
end else |
| 518 |
NegateOneQuaternion := 1; |
| 519 |
|
| 520 |
{ Sometimes CosTheta may get slightly > 1, and then ArcCos fails with |
| 521 |
EInvalidArgument. Testcase: demo_models/x3d/orientation_cos_1.x3d |
| 522 |
with castle-model-viewer. } |
| 523 |
MinVar(CosTheta, 1); |
| 524 |
|
| 525 |
Theta := ArcCos(CosTheta); |
| 526 |
SinTheta := Sin(Theta); |
| 527 |
if SinTheta > Epsilon then |
| 528 |
begin |
| 529 |
W1 := NegateOneQuaternion * Sin( (1-A) * Theta ) / SinTheta; |
| 530 |
W2 := Sin( A * Theta ) / SinTheta; |
| 531 |
end else |
| 532 |
begin |
| 533 |
{ Theta ~= 0, so both rotations equal (or opposite, in which case |
| 534 |
result is undefined anyway). } |
| 535 |
W1 := 1 - A; |
| 536 |
W2 := A; |
| 537 |
end; |
| 538 |
Result.Data.Vector4 := (Q1.Data.Vector4 * W1) + (Q2.Data.Vector4 * W2); |
| 539 |
end; |
| 540 |
|
| 541 |
function SLerp(const A: Single; const Rot1, Rot2: TVector4): TVector4; |
| 542 |
var |
| 543 |
Rot1Axis: TVector3 absolute Rot1; |
| 544 |
Rot2Axis: TVector3 absolute Rot2; |
| 545 |
begin |
| 546 |
{ TODO: Why do we need special code when 2 axis are equal? |
| 547 |
- See TBoneTimelineRotate.BuildNodes |
| 548 |
(although there we fixed it by TOrientationInterpolator2DNode that doesn't |
| 549 |
even use SLerp anymore) |
| 550 |
- See Unholy bug exorcist_hotel_blending_phone - animation transition |
| 551 |
idle <-> use_phone_loop (SLerp called by TSFRotation.AssignLerp) } |
| 552 |
|
| 553 |
if TVector3.PerfectlyEquals(Rot1Axis, Rot2Axis) then |
| 554 |
Result := Vector4(Rot1Axis, AngleLerp(A, Rot1.W, Rot2.W)) |
| 555 |
else |
| 556 |
Result := SLerp(A, |
| 557 |
QuatFromAxisAngle(Rot1, true), |
| 558 |
QuatFromAxisAngle(Rot2, true)).ToAxisAngle; |
| 559 |
end; |
| 560 |
|
| 561 |
function NLerp(const A: Single; const Q1, Q2: TQuaternion; |
| 562 |
const ForceShortestPath: boolean): TQuaternion; |
| 563 |
begin |
| 564 |
if ForceShortestPath and (TVector4.DotProduct(Q1.Data.Vector4, Q2.Data.Vector4) < 0) then |
| 565 |
begin |
| 566 |
{ negate one quaternion } |
| 567 |
Result.Data.Vector4 := Lerp(A, -Q1.Data.Vector4, Q2.Data.Vector4); |
| 568 |
end else |
| 569 |
Result.Data.Vector4 := Lerp(A, Q1.Data.Vector4, Q2.Data.Vector4); |
| 570 |
|
| 571 |
Result := Result.Normalize; |
| 572 |
end; |
| 573 |
|
| 574 |
function NLerp(const A: Single; const Rot1, Rot2: TVector4; |
| 575 |
const ForceShortestPath: boolean): TVector4; |
| 576 |
begin |
| 577 |
Result := NLerp(A, |
| 578 |
QuatFromAxisAngle(Rot1, true), |
| 579 |
QuatFromAxisAngle(Rot2, true), |
| 580 |
ForceShortestPath).ToAxisAngle; |
| 581 |
end; |
| 582 |
|
| 583 |
end. |