| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.1.1.1 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SbRotation |
| 47 |
| |
| 48 |
| Author(s) : Nick Thompson |
| 49 |
| |
| 50 |
| Heavily based on code by Ken Shoemake and code by Gavin Bell |
| 51 |
| |
| 52 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 53 |
_______________________________________________________________________ |
| 54 |
*/ |
| 55 |
|
| 56 |
#include <Inventor/SbLinear.h> |
| 57 |
#include <Inventor/errors/SoDebugError.h> |
| 58 |
|
| 59 |
// amount squared to figure if two floats are equal |
| 60 |
#define DELTA 1e-6 |
| 61 |
|
| 62 |
//////////////////////////////////////////////////////////////////////// |
| 63 |
// |
| 64 |
// Description: |
| 65 |
// Returns 4 individual components of rotation quaternion. |
| 66 |
// |
| 67 |
// Use: public |
| 68 |
|
| 69 |
void |
| 70 |
SbRotation::getValue(float &q0, float &q1, float &q2, float &q3) const |
| 71 |
// |
| 72 |
//////////////////////////////////////////////////////////////////////// |
| 73 |
{ |
| 74 |
q0 = quat[0]; |
| 75 |
q1 = quat[1]; |
| 76 |
q2 = quat[2]; |
| 77 |
q3 = quat[3]; |
| 78 |
} |
| 79 |
|
| 80 |
//////////////////////////////////////////////////////////////////////// |
| 81 |
// |
| 82 |
// Description: |
| 83 |
// Returns corresponding 3D rotation axis vector and angle in radians. |
| 84 |
// |
| 85 |
// Use: public |
| 86 |
|
| 87 |
void |
| 88 |
SbRotation::getValue(SbVec3f &axis, float &radians) const |
| 89 |
// |
| 90 |
//////////////////////////////////////////////////////////////////////// |
| 91 |
{ |
| 92 |
float len; |
| 93 |
SbVec3f q; |
| 94 |
|
| 95 |
q[0] = quat[0]; |
| 96 |
q[1] = quat[1]; |
| 97 |
q[2] = quat[2]; |
| 98 |
|
| 99 |
if ((len = q.length()) > 0.00001) { |
| 100 |
axis = q * (1.0 / len); |
| 101 |
radians = 2.0 * acosf(quat[3]); |
| 102 |
} |
| 103 |
|
| 104 |
else { |
| 105 |
axis.setValue(0.0, 0.0, 1.0); |
| 106 |
radians = 0.0; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
//////////////////////////////////////////////////////////////////////// |
| 111 |
// |
| 112 |
// Description: |
| 113 |
// Returns corresponding 4x4 rotation matrix. |
| 114 |
// |
| 115 |
// Use: public |
| 116 |
|
| 117 |
void |
| 118 |
SbRotation::getValue(SbMatrix &matrix) const |
| 119 |
// |
| 120 |
//////////////////////////////////////////////////////////////////////// |
| 121 |
{ |
| 122 |
SbMat m; |
| 123 |
|
| 124 |
m[0][0] = 1 - 2.0 * (quat[1] * quat[1] + quat[2] * quat[2]); |
| 125 |
m[0][1] = 2.0 * (quat[0] * quat[1] + quat[2] * quat[3]); |
| 126 |
m[0][2] = 2.0 * (quat[2] * quat[0] - quat[1] * quat[3]); |
| 127 |
m[0][3] = 0.0; |
| 128 |
|
| 129 |
m[1][0] = 2.0 * (quat[0] * quat[1] - quat[2] * quat[3]); |
| 130 |
m[1][1] = 1 - 2.0 * (quat[2] * quat[2] + quat[0] * quat[0]); |
| 131 |
m[1][2] = 2.0 * (quat[1] * quat[2] + quat[0] * quat[3]); |
| 132 |
m[1][3] = 0.0; |
| 133 |
|
| 134 |
m[2][0] = 2.0 * (quat[2] * quat[0] + quat[1] * quat[3]); |
| 135 |
m[2][1] = 2.0 * (quat[1] * quat[2] - quat[0] * quat[3]); |
| 136 |
m[2][2] = 1 - 2.0 * (quat[1] * quat[1] + quat[0] * quat[0]); |
| 137 |
m[2][3] = 0.0; |
| 138 |
|
| 139 |
m[3][0] = 0.0; |
| 140 |
m[3][1] = 0.0; |
| 141 |
m[3][2] = 0.0; |
| 142 |
m[3][3] = 1.0; |
| 143 |
|
| 144 |
matrix = m; |
| 145 |
} |
| 146 |
|
| 147 |
//////////////////////////////////////////////////////////////////////// |
| 148 |
// |
| 149 |
// Description: |
| 150 |
// Changes a rotation to be its inverse. |
| 151 |
// |
| 152 |
// Use: public |
| 153 |
|
| 154 |
SbRotation & |
| 155 |
SbRotation::invert() |
| 156 |
// |
| 157 |
//////////////////////////////////////////////////////////////////////// |
| 158 |
{ |
| 159 |
float invNorm = 1.0 / norm(); |
| 160 |
|
| 161 |
quat[0] = -quat[0] * invNorm; |
| 162 |
quat[1] = -quat[1] * invNorm; |
| 163 |
quat[2] = -quat[2] * invNorm; |
| 164 |
quat[3] = quat[3] * invNorm; |
| 165 |
|
| 166 |
return *this; |
| 167 |
} |
| 168 |
|
| 169 |
//////////////////////////////////////////////////////////////////////// |
| 170 |
// |
| 171 |
// Description: |
| 172 |
// Sets value of rotation from array of 4 components of a |
| 173 |
// quaternion. |
| 174 |
// |
| 175 |
// Use: public |
| 176 |
|
| 177 |
SbRotation & |
| 178 |
SbRotation::setValue(const float q[4]) |
| 179 |
// |
| 180 |
//////////////////////////////////////////////////////////////////////// |
| 181 |
{ |
| 182 |
quat[0] = q[0]; |
| 183 |
quat[1] = q[1]; |
| 184 |
quat[2] = q[2]; |
| 185 |
quat[3] = q[3]; |
| 186 |
normalize(); |
| 187 |
|
| 188 |
return (*this); |
| 189 |
} |
| 190 |
|
| 191 |
//////////////////////////////////////////////////////////////////////// |
| 192 |
// |
| 193 |
// Description: |
| 194 |
// Sets value of rotation from 4 individual components of a |
| 195 |
// quaternion. |
| 196 |
// |
| 197 |
// Use: public |
| 198 |
|
| 199 |
SbRotation & |
| 200 |
SbRotation::setValue(float q0, float q1, float q2, float q3) |
| 201 |
// |
| 202 |
//////////////////////////////////////////////////////////////////////// |
| 203 |
{ |
| 204 |
quat[0] = q0; |
| 205 |
quat[1] = q1; |
| 206 |
quat[2] = q2; |
| 207 |
quat[3] = q3; |
| 208 |
normalize(); |
| 209 |
|
| 210 |
return (*this); |
| 211 |
} |
| 212 |
|
| 213 |
//////////////////////////////////////////////////////////////////////// |
| 214 |
// |
| 215 |
// Description: |
| 216 |
// Sets value of rotation from a rotation matrix. |
| 217 |
// This algorithm is from "Quaternions and 4x4 Matrices", Ken |
| 218 |
// Shoemake, Graphics Gems II. |
| 219 |
// |
| 220 |
// Here's the logic: |
| 221 |
// We're trying to find a quaterion 'q' that represents the same |
| 222 |
// rotation as the given matrix 'm'. |
| 223 |
// We know how to convert a quaterion to a rotation matrix; the |
| 224 |
// matrix is given by (where x,y,z,w are the quaterion elements): |
| 225 |
// |
| 226 |
// x^2-y^2-z^2+w^2 2xy+2zw 2wx - 2yw 0 |
| 227 |
// 2xy-2zw y^2-z^2-x^2+w^2 2yz + 2xw 0 |
| 228 |
// 2zx+2zw 2yz-2xw z^2-x^2-y^2+w^2 0 |
| 229 |
// 0 0 0 x^2+y^2+z^2+w^2 |
| 230 |
// (note that x^2+y^2+z^2+w^2==1 for a normalized quaterion) |
| 231 |
// |
| 232 |
// We know m, we want to find x,y,z,w. If you don't mind doing |
| 233 |
// square roots, then it is easy; for example: |
| 234 |
// m[0][0]+m[1][1]+m[2][2]+m[3][3] = 4w^2 |
| 235 |
// (write it all out, and see all the terms cancel) |
| 236 |
// Or, w = sqrt(m[0][0]+m[1][1]+m[2][2]+m[3][3])/2 |
| 237 |
// Similarly, |
| 238 |
// x = sqrt(m[0][0]-m[1][1]-m[2][2]+m[3][3])/2 |
| 239 |
// y = sqrt(m[1][1]-m[2][2]-m[0][0]+m[3][3])/2 |
| 240 |
// z = sqrt(m[2][2]-m[0][0]-m[1][1]+m[3][3])/2 |
| 241 |
// However, you only really need to do one sqrt and find one of |
| 242 |
// x,y,z,w, because using the other elements of the matrix you |
| 243 |
// find, for example, that: |
| 244 |
// m[0][1]+m[1][0] = 2xy+2zw+2xy-2zw = 4xy |
| 245 |
// So if you know either x or y, you can find the other. |
| 246 |
// |
| 247 |
// That is assuming that the first thing you find isn't zero, of |
| 248 |
// course. In fact, you want the first element you find to be as |
| 249 |
// large as possible, to get more accuracy in the division. You |
| 250 |
// can rewrite the diagonal elements as: |
| 251 |
// (w^2 - x^2 - y^2 - z^2) + 2x^2 = m[0][0] |
| 252 |
// (w^2 - x^2 - y^2 - z^2) + 2y^2 = m[1][1] |
| 253 |
// (w^2 - x^2 - y^2 - z^2) + 2z^2 = m[2][2] |
| 254 |
// ... and write the sum of the diagonals as: |
| 255 |
// (w^2 - x^2 - y^2 - z^2) + 2w^2 = m[0][0]+m[1][1]+m[2][2]+m[3][3] |
| 256 |
// |
| 257 |
// Why do this? Because now it is easy to see that if x is greater |
| 258 |
// than y, z, or w, then m[0][0] will be greater than the other |
| 259 |
// diagonals or the sum of the diagonals. |
| 260 |
// |
| 261 |
// So, the overall strategy is: Figure out which if x,y,z, or w |
| 262 |
// will be greatest by looking at the diagonals. Compute that |
| 263 |
// value using the sqrt() formula. Then compute the other values |
| 264 |
// using the other set of formulas. |
| 265 |
// |
| 266 |
// Use: public |
| 267 |
|
| 268 |
SbRotation & |
| 269 |
SbRotation::setValue(const SbMatrix &m) |
| 270 |
// |
| 271 |
//////////////////////////////////////////////////////////////////////// |
| 272 |
{ |
| 273 |
int i, j, k; |
| 274 |
|
| 275 |
// First, find largest diagonal in matrix: |
| 276 |
if (m[0][0] > m[1][1]) { |
| 277 |
if (m[0][0] > m[2][2]) { |
| 278 |
i = 0; |
| 279 |
} |
| 280 |
else i = 2; |
| 281 |
} |
| 282 |
else { |
| 283 |
if (m[1][1] > m[2][2]) { |
| 284 |
i = 1; |
| 285 |
} |
| 286 |
else i = 2; |
| 287 |
} |
| 288 |
if (m[0][0]+m[1][1]+m[2][2] > m[i][i]) { |
| 289 |
// Compute w first: |
| 290 |
quat[3] = sqrt(m[0][0]+m[1][1]+m[2][2]+m[3][3])/2.0; |
| 291 |
|
| 292 |
// And compute other values: |
| 293 |
quat[0] = (m[1][2]-m[2][1])/(4*quat[3]); |
| 294 |
quat[1] = (m[2][0]-m[0][2])/(4*quat[3]); |
| 295 |
quat[2] = (m[0][1]-m[1][0])/(4*quat[3]); |
| 296 |
} |
| 297 |
else { |
| 298 |
// Compute x, y, or z first: |
| 299 |
j = (i+1)%3; k = (i+2)%3; |
| 300 |
|
| 301 |
// Compute first value: |
| 302 |
quat[i] = sqrt(m[i][i]-m[j][j]-m[k][k]+m[3][3])/2.0; |
| 303 |
|
| 304 |
// And the others: |
| 305 |
quat[j] = (m[i][j]+m[j][i])/(4*quat[i]); |
| 306 |
quat[k] = (m[i][k]+m[k][i])/(4*quat[i]); |
| 307 |
|
| 308 |
quat[3] = (m[j][k]-m[k][j])/(4*quat[i]); |
| 309 |
} |
| 310 |
|
| 311 |
#ifdef DEBUG |
| 312 |
// Check to be sure output matches input: |
| 313 |
SbMatrix check; |
| 314 |
getValue(check); |
| 315 |
SbBool ok = TRUE; |
| 316 |
for (i = 0; i < 4 && ok; i++) { |
| 317 |
for (j = 0; j < 4 && ok; j++) { |
| 318 |
if (fabsf(m[i][j]-check[i][j]) > 1.0e-5) |
| 319 |
ok = FALSE; |
| 320 |
} |
| 321 |
} |
| 322 |
if (!ok) { |
| 323 |
SoDebugError::post("SbRotation::setValue(const SbMatrix &)", |
| 324 |
"Rotation does not agree with matrix; " |
| 325 |
"this routine only works with rotation " |
| 326 |
"matrices!"); |
| 327 |
} |
| 328 |
#endif |
| 329 |
|
| 330 |
return (*this); |
| 331 |
} |
| 332 |
|
| 333 |
//////////////////////////////////////////////////////////////////////// |
| 334 |
// |
| 335 |
// Description: |
| 336 |
// Sets value of rotation from 3D rotation axis vector and angle in |
| 337 |
// radians. |
| 338 |
// |
| 339 |
// Use: public |
| 340 |
|
| 341 |
SbRotation & |
| 342 |
SbRotation::setValue(const SbVec3f &axis, float radians) |
| 343 |
// |
| 344 |
//////////////////////////////////////////////////////////////////////// |
| 345 |
{ |
| 346 |
SbVec3f q; |
| 347 |
|
| 348 |
q = axis; |
| 349 |
q.normalize(); |
| 350 |
|
| 351 |
q *= sinf(radians / 2.0); |
| 352 |
|
| 353 |
quat[0] = q[0]; |
| 354 |
quat[1] = q[1]; |
| 355 |
quat[2] = q[2]; |
| 356 |
|
| 357 |
quat[3] = cosf(radians / 2.0); |
| 358 |
|
| 359 |
return(*this); |
| 360 |
} |
| 361 |
|
| 362 |
//////////////////////////////////////////////////////////////////////// |
| 363 |
// |
| 364 |
// Description: |
| 365 |
// Sets rotation to rotate one direction vector to another. |
| 366 |
// |
| 367 |
// Use: public |
| 368 |
|
| 369 |
SbRotation & |
| 370 |
SbRotation::setValue(const SbVec3f &rotateFrom, const SbVec3f &rotateTo) |
| 371 |
// |
| 372 |
//////////////////////////////////////////////////////////////////////// |
| 373 |
{ |
| 374 |
SbVec3f from = rotateFrom; |
| 375 |
SbVec3f to = rotateTo; |
| 376 |
SbVec3f axis; |
| 377 |
float cost; |
| 378 |
|
| 379 |
from.normalize(); |
| 380 |
to.normalize(); |
| 381 |
cost = from.dot(to); |
| 382 |
|
| 383 |
// check for degeneracies |
| 384 |
if (cost > 0.99999) { // vectors are parallel |
| 385 |
quat[0] = quat[1] = quat[2] = 0.0; |
| 386 |
quat[3] = 1.0; |
| 387 |
return *this; |
| 388 |
} |
| 389 |
else if (cost < -0.99999) { // vectors are opposite |
| 390 |
// find an axis to rotate around, which should be |
| 391 |
// perpendicular to the original axis |
| 392 |
// Try cross product with (1,0,0) first, if that's one of our |
| 393 |
// original vectors then try (0,1,0). |
| 394 |
SbVec3f tmp = from.cross(SbVec3f(1.0, 0.0, 0.0)); |
| 395 |
if (tmp.length() < 0.00001) |
| 396 |
tmp = from.cross(SbVec3f(0.0, 1.0, 0.0)); |
| 397 |
|
| 398 |
tmp.normalize(); |
| 399 |
setValue(tmp[0], tmp[1], tmp[2], 0.0); |
| 400 |
return *this; |
| 401 |
} |
| 402 |
|
| 403 |
axis = rotateFrom.cross(rotateTo); |
| 404 |
axis.normalize(); |
| 405 |
|
| 406 |
// use half-angle formulae |
| 407 |
// sin^2 t = ( 1 - cos (2t) ) / 2 |
| 408 |
axis *= sqrt(0.5 * (1.0 - cost)); |
| 409 |
|
| 410 |
// scale the axis by the sine of half the rotation angle to get |
| 411 |
// the normalized quaternion |
| 412 |
quat[0] = axis[0]; |
| 413 |
quat[1] = axis[1]; |
| 414 |
quat[2] = axis[2]; |
| 415 |
|
| 416 |
// cos^2 t = ( 1 + cos (2t) ) / 2 |
| 417 |
// w part is cosine of half the rotation angle |
| 418 |
quat[3] = sqrt(0.5 * (1.0 + cost)); |
| 419 |
|
| 420 |
return (*this); |
| 421 |
} |
| 422 |
|
| 423 |
//////////////////////////////////////////////////////////////////////// |
| 424 |
// |
| 425 |
// Description: |
| 426 |
// Multiplies by another rotation. |
| 427 |
// |
| 428 |
// Use: public |
| 429 |
|
| 430 |
SbRotation & |
| 431 |
SbRotation::operator *=(const SbRotation &q) |
| 432 |
// |
| 433 |
//////////////////////////////////////////////////////////////////////// |
| 434 |
{ |
| 435 |
float p0, p1, p2, p3; |
| 436 |
|
| 437 |
p0 = (q.quat[3] * quat[0] + q.quat[0] * quat[3] + |
| 438 |
q.quat[1] * quat[2] - q.quat[2] * quat[1]); |
| 439 |
p1 = (q.quat[3] * quat[1] + q.quat[1] * quat[3] + |
| 440 |
q.quat[2] * quat[0] - q.quat[0] * quat[2]); |
| 441 |
p2 = (q.quat[3] * quat[2] + q.quat[2] * quat[3] + |
| 442 |
q.quat[0] * quat[1] - q.quat[1] * quat[0]); |
| 443 |
p3 = (q.quat[3] * quat[3] - q.quat[0] * quat[0] - |
| 444 |
q.quat[1] * quat[1] - q.quat[2] * quat[2]); |
| 445 |
quat[0] = p0; |
| 446 |
quat[1] = p1; |
| 447 |
quat[2] = p2; |
| 448 |
quat[3] = p3; |
| 449 |
|
| 450 |
normalize(); |
| 451 |
|
| 452 |
return(*this); |
| 453 |
} |
| 454 |
|
| 455 |
//////////////////////////////////////////////////////////////////////// |
| 456 |
// |
| 457 |
// Description: |
| 458 |
// Equality comparison operator. |
| 459 |
// |
| 460 |
// Use: public |
| 461 |
|
| 462 |
int |
| 463 |
operator ==(const SbRotation &q1, const SbRotation &q2) |
| 464 |
// |
| 465 |
//////////////////////////////////////////////////////////////////////// |
| 466 |
{ |
| 467 |
return (q1.quat[0] == q2.quat[0] && |
| 468 |
q1.quat[1] == q2.quat[1] && |
| 469 |
q1.quat[2] == q2.quat[2] && |
| 470 |
q1.quat[3] == q2.quat[3]); |
| 471 |
} |
| 472 |
|
| 473 |
//////////////////////////////////////////////////////////////////////// |
| 474 |
// |
| 475 |
// Description: |
| 476 |
// Equality comparison operator within given tolerance - the square |
| 477 |
// of the length of the maximum distance between the two vectors. |
| 478 |
// |
| 479 |
// Use: public |
| 480 |
|
| 481 |
SbBool |
| 482 |
SbRotation::equals(const SbRotation &r, float tolerance) const |
| 483 |
// |
| 484 |
//////////////////////////////////////////////////////////////////////// |
| 485 |
{ |
| 486 |
return SbVec4f(quat).equals(SbVec4f(r.quat), tolerance); |
| 487 |
} |
| 488 |
|
| 489 |
//////////////////////////////////////////////////////////////////////// |
| 490 |
// |
| 491 |
// Description: |
| 492 |
// Binary multiplication operator. |
| 493 |
// |
| 494 |
// Use: public |
| 495 |
|
| 496 |
SbRotation |
| 497 |
operator *(const SbRotation &q1, const SbRotation &q2) |
| 498 |
// |
| 499 |
//////////////////////////////////////////////////////////////////////// |
| 500 |
{ |
| 501 |
SbRotation q(q2.quat[3] * q1.quat[0] + q2.quat[0] * q1.quat[3] + |
| 502 |
q2.quat[1] * q1.quat[2] - q2.quat[2] * q1.quat[1], |
| 503 |
|
| 504 |
q2.quat[3] * q1.quat[1] + q2.quat[1] * q1.quat[3] + |
| 505 |
q2.quat[2] * q1.quat[0] - q2.quat[0] * q1.quat[2], |
| 506 |
|
| 507 |
q2.quat[3] * q1.quat[2] + q2.quat[2] * q1.quat[3] + |
| 508 |
q2.quat[0] * q1.quat[1] - q2.quat[1] * q1.quat[0], |
| 509 |
|
| 510 |
q2.quat[3] * q1.quat[3] - q2.quat[0] * q1.quat[0] - |
| 511 |
q2.quat[1] * q1.quat[1] - q2.quat[2] * q1.quat[2]); |
| 512 |
q.normalize(); |
| 513 |
|
| 514 |
return (q); |
| 515 |
} |
| 516 |
|
| 517 |
//////////////////////////////////////////////////////////////////////// |
| 518 |
// |
| 519 |
// Description: |
| 520 |
// Puts the given vector through this rotation |
| 521 |
// (Multiplies the given vector by the matrix of this rotation). |
| 522 |
// |
| 523 |
// Use: public |
| 524 |
|
| 525 |
void |
| 526 |
SbRotation::multVec(const SbVec3f &src, SbVec3f &dst) const |
| 527 |
// |
| 528 |
//////////////////////////////////////////////////////////////////////// |
| 529 |
{ |
| 530 |
SbMatrix myMat; |
| 531 |
getValue( myMat ); |
| 532 |
|
| 533 |
myMat.multVecMatrix( src, dst ); |
| 534 |
} |
| 535 |
|
| 536 |
//////////////////////////////////////////////////////////////////////// |
| 537 |
// |
| 538 |
// Description: |
| 539 |
// Keep the axis the same. Multiply the angle of rotation by |
| 540 |
// the amount 'scaleFactor' |
| 541 |
// |
| 542 |
// Use: public |
| 543 |
|
| 544 |
void |
| 545 |
SbRotation::scaleAngle(float scaleFactor ) |
| 546 |
// |
| 547 |
//////////////////////////////////////////////////////////////////////// |
| 548 |
{ |
| 549 |
SbVec3f myAxis; |
| 550 |
float myAngle; |
| 551 |
|
| 552 |
// Get the Axis and angle. |
| 553 |
getValue( myAxis, myAngle ); |
| 554 |
|
| 555 |
setValue( myAxis, (myAngle * scaleFactor) ); |
| 556 |
} |
| 557 |
|
| 558 |
// |
| 559 |
// |
| 560 |
// |
| 561 |
//////////////////////////////////////////////////////////////////////// |
| 562 |
// |
| 563 |
// Description: |
| 564 |
// Spherical linear interpolation: as t goes from 0 to 1, returned |
| 565 |
// value goes from rot0 to rot1. |
| 566 |
// |
| 567 |
// Use: public |
| 568 |
|
| 569 |
SbRotation |
| 570 |
SbRotation::slerp(const SbRotation &rot0, const SbRotation &rot1, float t) |
| 571 |
// |
| 572 |
//////////////////////////////////////////////////////////////////////// |
| 573 |
{ |
| 574 |
const float* r1q = rot1.getValue(); |
| 575 |
|
| 576 |
SbRotation rot; |
| 577 |
float rot1q[4]; |
| 578 |
double omega, cosom, sinom; |
| 579 |
double scalerot0, scalerot1; |
| 580 |
int i; |
| 581 |
|
| 582 |
// Calculate the cosine |
| 583 |
cosom = rot0.quat[0]*rot1.quat[0] + rot0.quat[1]*rot1.quat[1] |
| 584 |
+ rot0.quat[2]*rot1.quat[2] + rot0.quat[3]*rot1.quat[3]; |
| 585 |
|
| 586 |
// adjust signs if necessary |
| 587 |
if ( cosom < 0.0 ) { |
| 588 |
cosom = -cosom; |
| 589 |
for ( int j = 0; j < 4; j++ ) |
| 590 |
rot1q[j] = -r1q[j]; |
| 591 |
} else { |
| 592 |
for ( int j = 0; j < 4; j++ ) |
| 593 |
rot1q[j] = r1q[j]; |
| 594 |
} |
| 595 |
|
| 596 |
// calculate interpolating coeffs |
| 597 |
if ( (1.0 - cosom) > 0.00001 ) { |
| 598 |
// standard case |
| 599 |
omega = acos(cosom); |
| 600 |
sinom = sin(omega); |
| 601 |
scalerot0 = sin((1.0 - t) * omega) / sinom; |
| 602 |
scalerot1 = sin(t * omega) / sinom; |
| 603 |
} else { |
| 604 |
// rot0 and rot1 very close - just do linear interp. |
| 605 |
scalerot0 = 1.0 - t; |
| 606 |
scalerot1 = t; |
| 607 |
} |
| 608 |
|
| 609 |
// build the new quarternion |
| 610 |
for (i = 0; i <4; i++) |
| 611 |
rot.quat[i] = scalerot0 * rot0.quat[i] + scalerot1 * rot1q[i]; |
| 612 |
|
| 613 |
return rot; |
| 614 |
} |
| 615 |
|
| 616 |
//////////////////////////////////////////////////////////////////////// |
| 617 |
// |
| 618 |
// Description: |
| 619 |
// Returns the norm (square of the 4D length) of the quaternion |
| 620 |
// defining the rotation. |
| 621 |
// |
| 622 |
// Use: private |
| 623 |
|
| 624 |
float |
| 625 |
SbRotation::norm() const |
| 626 |
// |
| 627 |
//////////////////////////////////////////////////////////////////////// |
| 628 |
{ |
| 629 |
return (quat[0] * quat[0] + |
| 630 |
quat[1] * quat[1] + |
| 631 |
quat[2] * quat[2] + |
| 632 |
quat[3] * quat[3]); |
| 633 |
} |
| 634 |
|
| 635 |
//////////////////////////////////////////////////////////////////////// |
| 636 |
// |
| 637 |
// Description: |
| 638 |
// Normalizes a rotation quaternion to unit 4D length. |
| 639 |
// |
| 640 |
// Use: private |
| 641 |
|
| 642 |
void |
| 643 |
SbRotation::normalize() |
| 644 |
// |
| 645 |
//////////////////////////////////////////////////////////////////////// |
| 646 |
{ |
| 647 |
float dist = 1.0 / sqrt(norm()); |
| 648 |
|
| 649 |
quat[0] *= dist; |
| 650 |
quat[1] *= dist; |
| 651 |
quat[2] *= dist; |
| 652 |
quat[3] *= dist; |
| 653 |
} |
| 654 |
|