/usr/include/ITK-4.5/itkVersor.hxx is in libinsighttoolkit4-dev 4.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef __itkVersor_hxx
#define __itkVersor_hxx
#include "itkVersor.h"
#include "itkNumericTraits.h"
#include "itkMath.h"
#include <vnl/vnl_det.h>
namespace itk
{
/** Constructor to initialize entire vector to one value. */
template< typename T >
Versor< T >
::Versor()
{
m_X = NumericTraits< T >::Zero;
m_Y = NumericTraits< T >::Zero;
m_Z = NumericTraits< T >::Zero;
m_W = NumericTraits< T >::One;
}
/** Copy Constructor */
template< typename T >
Versor< T >
::Versor(const Self & v)
{
m_X = v.m_X;
m_Y = v.m_Y;
m_Z = v.m_Z;
m_W = v.m_W;
}
/** Assignment Operator */
template< typename T >
const Versor< T > &
Versor< T >
::operator=(const Self & v)
{
m_X = v.m_X;
m_Y = v.m_Y;
m_Z = v.m_Z;
m_W = v.m_W;
return *this;
}
/** Set to an identity transform */
template< typename T >
void
Versor< T >
::SetIdentity()
{
m_X = NumericTraits< T >::Zero;
m_Y = NumericTraits< T >::Zero;
m_Z = NumericTraits< T >::Zero;
m_W = NumericTraits< T >::One;
}
/** Return a vnl_quaternion */
template< typename T >
vnl_quaternion< T >
Versor< T >
::GetVnlQuaternion(void) const
{
return vnl_quaternion< T >(m_X, m_Y, m_Z, m_W);
}
/** Assignment and Composition Operator */
template< typename T >
const Versor< T > &
Versor< T >
::operator*=(const Self & v)
{
const double mx = m_W * v.m_X - m_Z * v.m_Y + m_Y * v.m_Z + m_X * v.m_W;
const double my = m_Z * v.m_X + m_W * v.m_Y - m_X * v.m_Z + m_Y * v.m_W;
const double mz = -m_Y * v.m_X + m_X * v.m_Y + m_W * v.m_Z + m_Z * v.m_W;
const double mw = -m_X * v.m_X - m_Y * v.m_Y - m_Z * v.m_Z + m_W * v.m_W;
m_X = mx;
m_Y = my;
m_Z = mz;
m_W = mw;
return *this;
}
/** Composition Operator */
template< typename T >
Versor< T >
Versor< T >
::operator*(const Self & v) const
{
Self result;
result.m_X = m_W * v.m_X - m_Z * v.m_Y + m_Y * v.m_Z + m_X * v.m_W;
result.m_Y = m_Z * v.m_X + m_W * v.m_Y - m_X * v.m_Z + m_Y * v.m_W;
result.m_Z = -m_Y * v.m_X + m_X * v.m_Y + m_W * v.m_Z + m_Z * v.m_W;
result.m_W = -m_X * v.m_X - m_Y * v.m_Y - m_Z * v.m_Z + m_W * v.m_W;
return result;
}
/** Division and Assignment Operator */
template< typename T >
const Versor< T > &
Versor< T >
::operator/=(const Self & v)
{
const double mx = -m_W * v.m_X + m_Z * v.m_Y - m_Y * v.m_Z + m_X * v.m_W;
const double my = -m_Z * v.m_X - m_W * v.m_Y + m_X * v.m_Z + m_Y * v.m_W;
const double mz = m_Y * v.m_X - m_X * v.m_Y - m_W * v.m_Z + m_Z * v.m_W;
const double mw = m_X * v.m_X + m_Y * v.m_Y + m_Z * v.m_Z + m_W * v.m_W;
m_X = mx;
m_Y = my;
m_Z = mz;
m_W = mw;
return *this;
}
/** Division Operator */
template< typename T >
Versor< T >
Versor< T >
::operator/(const Self & v) const
{
Self result;
result.m_X = -m_W * v.m_X + m_Z * v.m_Y - m_Y * v.m_Z + m_X * v.m_W;
result.m_Y = -m_Z * v.m_X - m_W * v.m_Y + m_X * v.m_Z + m_Y * v.m_W;
result.m_Z = m_Y * v.m_X - m_X * v.m_Y - m_W * v.m_Z + m_Z * v.m_W;
result.m_W = m_X * v.m_X + m_Y * v.m_Y + m_Z * v.m_Z + m_W * v.m_W;
return result;
}
/** Comparison operator */
template< typename T >
bool
Versor< T >
::operator!=(const Self & v) const
{
return !( *this == v );
}
/** Comparison operator */
template< typename T >
bool
Versor< T >
::operator==(const Self & v) const
{
// Evaluate the quaternion ratio between them
Self ratio = *this * v.GetReciprocal();
const typename itk::NumericTraits< T >::AccumulateType
square = ratio.m_W * ratio.m_W;
const double epsilon = 1e-300;
if ( vcl_fabs(1.0f - square) < epsilon )
{
return true;
}
return false;
}
/** Get Conjugate */
template< typename T >
Versor< T >
Versor< T >
::GetConjugate(void) const
{
Self result;
result.m_X = -m_X;
result.m_Y = -m_Y;
result.m_Z = -m_Z;
result.m_W = m_W;
return result;
}
/** Get Reciprocal */
template< typename T >
Versor< T >
Versor< T >
::GetReciprocal(void) const
{
Self result;
result.m_X = -m_X;
result.m_Y = -m_Y;
result.m_Z = -m_Z;
result.m_W = m_W;
return result;
}
/** Get Tensor part */
template< typename T >
typename Versor< T >::ValueType
Versor< T >
::GetTensor(void) const
{
const ValueType tensor =
static_cast< ValueType >(
vcl_sqrt(m_X * m_X + m_Y * m_Y + m_Z * m_Z + m_W * m_W) );
return tensor;
}
/** Normalize */
template< typename T >
void
Versor< T >
::Normalize(void)
{
const ValueType tensor = this->GetTensor();
if ( vcl_fabs(tensor) < 1e-20 )
{
ExceptionObject except;
except.SetDescription("Attempt to normalize a \
itk::Versor with zero tensor");
except.SetLocation(__FILE__);
throw except;
}
m_X /= tensor;
m_Y /= tensor;
m_Z /= tensor;
m_W /= tensor;
}
/** Get Axis */
template< typename T >
typename Versor< T >::VectorType
Versor< T >
::GetAxis(void) const
{
VectorType axis;
const RealType ax = static_cast< RealType >( m_X );
const RealType ay = static_cast< RealType >( m_Y );
const RealType az = static_cast< RealType >( m_Z );
const RealType vectorNorm = vcl_sqrt(ax * ax + ay * ay + az * az);
if ( vectorNorm == NumericTraits< RealType >::Zero )
{
axis[0] = NumericTraits< T >::Zero;
axis[1] = NumericTraits< T >::Zero;
axis[2] = NumericTraits< T >::Zero;
}
else
{
axis[0] = m_X / vectorNorm;
axis[1] = m_Y / vectorNorm;
axis[2] = m_Z / vectorNorm;
}
return axis;
}
/** Get Right part */
template< typename T >
typename Versor< T >::VectorType
Versor< T >
::GetRight(void) const
{
VectorType axis;
axis[0] = m_X;
axis[1] = m_Y;
axis[2] = m_Z;
return axis;
}
/** Get Scalar part */
template< typename T >
typename Versor< T >::ValueType
Versor< T >
::GetScalar(void) const
{
return m_W;
}
/** Get Angle (in radians) */
template< typename T >
typename Versor< T >::ValueType
Versor< T >
::GetAngle(void) const
{
const RealType ax = static_cast< RealType >( m_X );
const RealType ay = static_cast< RealType >( m_Y );
const RealType az = static_cast< RealType >( m_Z );
const RealType vectorNorm = vcl_sqrt(ax * ax + ay * ay + az * az);
const ValueType angle = 2.0 * vcl_atan2( vectorNorm, static_cast< RealType >( m_W ) );
return angle;
}
/** Get the Square root of the unit quaternion */
template< typename T >
Versor< T >
Versor< T >
::SquareRoot(void) const
{
const ValueType newScalar = vcl_sqrt( static_cast< double >( 1.0 + m_W ) );
const double sqrtOfTwo = vcl_sqrt(2.0f);
const double factor = 1.0f / ( newScalar * sqrtOfTwo );
Self result;
result.m_X = m_X * factor;
result.m_Y = m_Y * factor;
result.m_Z = m_Z * factor;
result.m_W = newScalar / sqrtOfTwo;
return result;
}
/** Compute the Exponential of the quaternion */
template< typename T >
Versor< T >
Versor< T >
::Exponential(ValueType exponent) const
{
Self result;
result.Set(this->GetAxis(),
this->GetAngle() * exponent);
return result;
}
/** Set Axis and Angle (in radians) */
template< typename T >
void
Versor< T >
::Set(const VectorType & axis, ValueType angle)
{
const RealType vectorNorm = axis.GetNorm();
const RealType cosangle2 = vcl_cos(angle / 2.0);
const RealType sinangle2 = vcl_sin(angle / 2.0);
const RealType factor = sinangle2 / vectorNorm;
m_X = axis[0] * factor;
m_Y = axis[1] * factor;
m_Z = axis[2] * factor;
m_W = cosangle2;
}
/** Set using an orthogonal matrix. */
template< typename T >
void
Versor< T >
::Set(const MatrixType & mat)
{
//const double epsilon = 1e-30;
//Keep the epsilon value large enough so that the alternate routes of
//computing the quaternion are used to within floating point precision of the
//math to be used. Using 1e-30 results in degenerate matries for rotations
//near vnl_math::pi due to imprecision of the math. 0.5/vcl_sqrt(trace) is
//not accurate to 1e-30, so the resulting matrices would have very large
//errors. By decreasing this epsilon value to a higher tolerance, the
//alternate stable methods for conversion are used.
//
//The use of vcl_numeric_limits< T >::epsilon() was not consistent with
//the rest of the ITK toolkit with respect to epsilon values for
//determining rotational orthogonality, and it occasionally
//prevented the conversion between different rigid transform types.
const T epsilon = Self::Epsilon(); // vnl_sqrt( vcl_numeric_limits< T >::epsilon() );
// Use a slightly less epsilon for detecting difference
const T epsilonDiff = Self::Epsilon(); //vcl_numeric_limits< T >::epsilon() * 10.0;
const vnl_matrix< T > m( mat.GetVnlMatrix() );
//check for orthonormality and that it isn't a reflection
const vnl_matrix_fixed< T, 3, 3 > & I = m*m.transpose();
if( vcl_abs( I[0][1] ) > epsilon || vcl_abs( I[0][2] ) > epsilon ||
vcl_abs( I[1][0] ) > epsilon || vcl_abs( I[1][2] ) > epsilon ||
vcl_abs( I[2][0] ) > epsilon || vcl_abs( I[2][1] ) > epsilon ||
vcl_abs( I[0][0] - itk::NumericTraits<T>::One ) > epsilonDiff ||
vcl_abs( I[1][1] - itk::NumericTraits<T>::One ) > epsilonDiff ||
vcl_abs( I[2][2] - itk::NumericTraits<T>::One ) > epsilonDiff ||
vnl_det( I ) < 0 )
{
itkGenericExceptionMacro(<< "The following matrix does not represent rotation to within an epsion of "
<< epsilon << "." << std::endl
<< m << std::endl
<< "det(m * m transpose) is: " << vnl_det(I) << std::endl
<< "m * m transpose is:" << std::endl
<< I << std::endl);
}
const double trace = m(0, 0) + m(1, 1) + m(2, 2) + 1.0;
if ( trace > epsilon )
{
const double s = 0.5 / vcl_sqrt(trace);
m_W = 0.25 / s;
m_X = ( m(2, 1) - m(1, 2) ) * s;
m_Y = ( m(0, 2) - m(2, 0) ) * s;
m_Z = ( m(1, 0) - m(0, 1) ) * s;
}
else
{
if ( m(0, 0) > m(1, 1) && m(0, 0) > m(2, 2) )
{
const double s = 2.0 * vcl_sqrt( 1.0 + m(0, 0) - m(1, 1) - m(2, 2) );
m_X = 0.25 * s;
m_Y = ( m(0, 1) + m(1, 0) ) / s;
m_Z = ( m(0, 2) + m(2, 0) ) / s;
m_W = ( m(1, 2) - m(2, 1) ) / s;
}
else
{
if ( m(1, 1) > m(2, 2) )
{
const double s = 2.0 * vcl_sqrt( 1.0 + m(1, 1) - m(0, 0) - m(2, 2) );
m_X = ( m(0, 1) + m(1, 0) ) / s;
m_Y = 0.25 * s;
m_Z = ( m(1, 2) + m(2, 1) ) / s;
m_W = ( m(0, 2) - m(2, 0) ) / s;
}
else
{
const double s = 2.0 * vcl_sqrt( 1.0 + m(2, 2) - m(0, 0) - m(1, 1) );
m_X = ( m(0, 2) + m(2, 0) ) / s;
m_Y = ( m(1, 2) + m(2, 1) ) / s;
m_Z = 0.25 * s;
m_W = ( m(0, 1) - m(1, 0) ) / s;
}
}
}
this->Normalize();
}
/** Set right Part (in radians) */
template< typename T >
void
Versor< T >
::Set(const VectorType & axis)
{
typedef typename VectorType::RealValueType VectorRealValueType;
const ValueType sinangle2 = axis.GetNorm();
if ( sinangle2 > NumericTraits< ValueType >::One )
{
ExceptionObject exception;
exception.SetDescription("Trying to initialize a Versor with " \
"a vector whose magnitude is greater than 1");
exception.SetLocation("itk::Versor::Set( const VectorType )");
throw exception;
}
const ValueType cosangle2 = vcl_sqrt(NumericTraits< double >::One - sinangle2 * sinangle2);
m_X = axis[0];
m_Y = axis[1];
m_Z = axis[2];
m_W = cosangle2;
}
/** Set the Versor from four components.
* After assignment, the quaternion is normalized
* in order to get a consistent Versor (unit quaternion). */
template< typename T >
void
Versor< T >
::Set(T x, T y, T z, T w)
{
//
// We assume in this class that the W component is always non-negative.
// The rotation represented by a Versor remains unchanged if all its
// four components are negated simultaneously. Therefore, if we are
// requested to initialize a Versor with a negative W, we negate the
// signs of all the components.
//
if ( w < 0.0 )
{
m_X = -x;
m_Y = -y;
m_Z = -z;
m_W = -w;
}
else
{
m_X = x;
m_Y = y;
m_Z = z;
m_W = w;
}
this->Normalize();
}
/** Set from a vnl_quaternion
* After assignment, the quaternion is normalized
* in order to get a consistent Versor (unit quaternion). */
template< typename T >
void
Versor< T >
::Set(const VnlQuaternionType & quaternion)
{
m_X = quaternion.x();
m_Y = quaternion.y();
m_Z = quaternion.z();
m_W = quaternion.r();
this->Normalize();
}
/** Set rotation around X axis */
template< typename T >
void
Versor< T >
::SetRotationAroundX(ValueType angle)
{
const ValueType sinangle2 = vcl_sin(angle / 2.0);
const ValueType cosangle2 = vcl_cos(angle / 2.0);
m_X = sinangle2;
m_Y = NumericTraits< T >::Zero;
m_Z = NumericTraits< T >::Zero;
m_W = cosangle2;
}
/** Set rotation around Y axis */
template< typename T >
void
Versor< T >
::SetRotationAroundY(ValueType angle)
{
const ValueType sinangle2 = vcl_sin(angle / 2.0);
const ValueType cosangle2 = vcl_cos(angle / 2.0);
m_X = NumericTraits< T >::Zero;
m_Y = sinangle2;
m_Z = NumericTraits< T >::Zero;
m_W = cosangle2;
}
/** Set rotation around Z axis */
template< typename T >
void
Versor< T >
::SetRotationAroundZ(ValueType angle)
{
const ValueType sinangle2 = vcl_sin(angle / 2.0);
const ValueType cosangle2 = vcl_cos(angle / 2.0);
m_X = NumericTraits< T >::Zero;
m_Y = NumericTraits< T >::Zero;
m_Z = sinangle2;
m_W = cosangle2;
}
namespace {
template< typename InputVectorType, typename ValueType, typename OutputVectorType >
const OutputVectorType localTransformVectorMath(const InputVectorType & VectorObject,
const ValueType & inputX,
const ValueType & inputY,
const ValueType & inputZ,
const ValueType & inputW)
{
const ValueType xx = inputX * inputX;
const ValueType yy = inputY * inputY;
const ValueType zz = inputZ * inputZ;
const ValueType xy = inputX * inputY;
const ValueType xz = inputX * inputZ;
const ValueType xw = inputX * inputW;
const ValueType yz = inputY * inputZ;
const ValueType yw = inputY * inputW;
const ValueType zw = inputZ * inputW;
const ValueType mxx = 1.0 - 2.0 * ( yy + zz );
const ValueType myy = 1.0 - 2.0 * ( xx + zz );
const ValueType mzz = 1.0 - 2.0 * ( xx + yy );
const ValueType mxy = 2.0 * ( xy - zw );
const ValueType mxz = 2.0 * ( xz + yw );
const ValueType myx = 2.0 * ( xy + zw );
const ValueType mzx = 2.0 * ( xz - yw );
const ValueType mzy = 2.0 * ( yz + xw );
const ValueType myz = 2.0 * ( yz - xw );
OutputVectorType result;
result[0] = mxx * VectorObject[0] + mxy * VectorObject[1] + mxz * VectorObject[2];
result[1] = myx * VectorObject[0] + myy * VectorObject[1] + myz * VectorObject[2];
result[2] = mzx * VectorObject[0] + mzy * VectorObject[1] + mzz * VectorObject[2];
return result;
}
}
/** Transform a Vector */
template< typename T >
typename Versor< T >::VectorType
Versor< T >
::Transform(const VectorType & v) const
{
return localTransformVectorMath<VectorType,T,typename Versor< T >::VectorType>(v,this->m_X,this->m_Y,this->m_Z,this->m_W);
}
/** Transform a CovariantVector
* given that this is an orthogonal transformation
* CovariantVectors are transformed as vectors. */
template< typename T >
typename Versor< T >::CovariantVectorType
Versor< T >
::Transform(const CovariantVectorType & v) const
{
return localTransformVectorMath<CovariantVectorType,T,typename Versor< T >::CovariantVectorType>(v,this->m_X,this->m_Y,this->m_Z,this->m_W);
}
/** Transform a Point */
template< typename T >
typename Versor< T >::PointType
Versor< T >
::Transform(const PointType & v) const
{
return localTransformVectorMath<PointType,T,typename Versor< T >::PointType>(v,this->m_X,this->m_Y,this->m_Z,this->m_W);
}
/** Transform a VnlVector */
template< typename T >
typename Versor< T >::VnlVectorType
Versor< T >
::Transform(const VnlVectorType & v) const
{
return localTransformVectorMath<VnlVectorType,T,typename Versor< T >::VnlVectorType>(v,this->m_X,this->m_Y,this->m_Z,this->m_W);
}
/** Get Matrix representation */
template< typename T >
Matrix< T, 3, 3 >
Versor< T >
::GetMatrix(void) const
{
Matrix< T, 3, 3 > matrix;
const ValueType xx = m_X * m_X;
const ValueType yy = m_Y * m_Y;
const ValueType zz = m_Z * m_Z;
const ValueType xy = m_X * m_Y;
const ValueType xz = m_X * m_Z;
const ValueType xw = m_X * m_W;
const ValueType yz = m_Y * m_Z;
const ValueType yw = m_Y * m_W;
const ValueType zw = m_Z * m_W;
matrix[0][0] = 1.0 - 2.0 * ( yy + zz );
matrix[1][1] = 1.0 - 2.0 * ( xx + zz );
matrix[2][2] = 1.0 - 2.0 * ( xx + yy );
matrix[0][1] = 2.0 * ( xy - zw );
matrix[0][2] = 2.0 * ( xz + yw );
matrix[1][0] = 2.0 * ( xy + zw );
matrix[2][0] = 2.0 * ( xz - yw );
matrix[2][1] = 2.0 * ( yz + xw );
matrix[1][2] = 2.0 * ( yz - xw );
return matrix;
}
} // end namespace itk
#endif
|