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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
|
/*
* Modification History
*
* 2000-December-13 Jason Rohrer
* Created.
*
* 2000-December-13 Jason Rohrer
* Added a function for getting the distance between two vectors.
* Added implementations for current functions.
*
* 2000-December-17 Jason Rohrer
* Added a normalize function.
*
* 2000-December-18 Jason Rohrer
* Added a print function.
* Added a length function.
*
* 2000-December-20 Jason Rohrer
* Added a cross product function.
*
* 2001-January-10 Jason Rohrer
* Made class serializable.
*
* 2001-February-3 Jason Rohrer
* Updated serialization code to use new interfaces.
*
* 2001-February-10 Jason Rohrer
* Added a linear sum function.
*
* 2003-June-20 Jason Rohrer
* Added function for getting Z angle between vectors.
*
* 2005-February-4 Jason Rohrer
* Added setCoordinates functions.
*
* 2005-February-6 Jason Rohrer
* Added equals function.
*
* 2005-February-15 Jason Rohrer
* Added function for getting Y angle between vectors.
*
* 2005-February-22 Jason Rohrer
* Added function for getting X angle between vectors.
* Added function for rotating about an arbitrary axis.
*
* 2005-February-25 Jason Rohrer
* Fixed bugs in get_AngleTo functions when vectors are close to equal.
*
* 2005-February-28 Jason Rohrer
* Fixed bug in getXAngleTo.
* Fixed bugs in get_AngleTo functions when vectors are close to opposite.
*
* 2005-March-3 Jason Rohrer
* Fixed bug in getAngleTo when vectors are close to equal or opposite.
*
* 2005-March-18 Jason Rohrer
* Added a getXZDistance function.
*
* 2006-August-6 Jason Rohrer
* Added a no-arg constructor.
*/
#ifndef VECTOR_3D_INCLUDED
#define VECTOR_3D_INCLUDED
#include <math.h>
#include <stdio.h>
#include "Angle3D.h"
#include "minorGems/io/Serializable.h"
/**
* Geometric vector in 3-space.
*
* @author Jason Rohrer
*/
class Vector3D : public Serializable {
public:
double mX, mY, mZ;
/**
* Constructs a Vector3D.
*/
Vector3D( double inX, double inY, double inZ );
/**
* Constructs a zero-filled Vector3D.
*/
Vector3D();
/**
* Constructs a Vector3D by copying the parameters from
* another Vector3D.
*
* @param inOther vector to copy parameters from.
*/
Vector3D( Vector3D *inOther );
/**
* Sets the values in this vector
*/
void setCoordinates( double inX, double inY, double inZ );
/**
* Sets coordinates by copying the parameters from
* another Vector3D.
*
* @param inOther vector to copy parameters from.
* Must be destroyed by caller.
*/
void setCoordinates( Vector3D *inOther );
/**
* Normalizes this vector so that it has a length of 1.
*/
void normalize();
/**
* Tests if another vector is equal to this vector.
*
* @param inOther vector to test for equality with this vector.
*
* @return true if equal, false if not.
*/
char equals( Vector3D *inOther );
/**
* Sums another vector with this vector.
*
* @param inOther vector to add to this vector.
*/
void add( Vector3D *inOther );
/**
* Subtracts a vector from this vector.
*
* @param inOther vector to subtract from this vector.
*/
void subtract( Vector3D *inOther );
/**
* Computes a dot product of this vector with another.
*
* @param inOther vector to perform the dot product with.
*
* @return the dot product of the two vectors.
*/
double dot( Vector3D *inOther );
/**
* Computes a cross product of this vector with another
* ( this x other ). The cross computed is right handed.
*
* @param inOther vector to perform the cross product with.
*
* @return the cross product of the two vectors. Must be
* destroyed by caller.
*/
Vector3D *cross( Vector3D *inOther );
/**
* Computes the angle between this vector and another vector.
*
* @param inOther vector to find the angle to.
*
* @return the angle between the two vectors in radians.
* This angle is around the axis given by the cross of the two
* vectors.
* Must be destroyed by caller.
*/
double getAngleTo( Vector3D *inOther );
/**
* Rotates this vector around an arbitrary axis.
*
* @param inAxis the axis of rotation. Must be normalized.
* Must be destroyed by caller.
* @param the angle in radians.
*/
void rotate( Vector3D *inAxis, double inAngle );
/**
* Computes the angle between this vector and another vector in
* the x-y plane (in other words, the z-axis rotation angle).
*
* @param inOther vector to find the angle to.
*
* @return the angle between the two vectors in the x-y plane.
* Must be destroyed by caller.
*/
Angle3D *getZAngleTo( Vector3D *inOther );
/**
* Computes the angle between this vector and another vector in
* the x-z plane (in other words, the y-axis rotation angle).
*
* @param inOther vector to find the angle to.
*
* @return the angle between the two vectors in the x-z plane.
* Must be destroyed by caller.
*/
Angle3D *getYAngleTo( Vector3D *inOther );
/**
* Computes the angle between this vector and another vector in
* the y-z plane (in other words, the x-axis rotation angle).
*
* @param inOther vector to find the angle to.
*
* @return the angle between the two vectors in the y-z plane.
* Must be destroyed by caller.
*/
Angle3D *getXAngleTo( Vector3D *inOther );
/**
* Computes the linear weighted sum of two vectors.
*
* @param inFirst the first vector.
* @param inSecond the second vector.
* @param inFirstWeight the weight given to the first vector in the
* sum. The second vector is weighted (1-inFirstWeight).
*
* @return the sum vector. Must be destroyed by caller.
*/
static Vector3D *linearSum( Vector3D *inFirst, Vector3D *inSecond,
double inFirstWeight );
/**
* Gets the length of this vector.
*
* @return this vector's length.
*/
double getLength();
/**
* Multiplies this vector by a scalar.
*
* @param inScalar scalar to multiply this vector by.
*/
void scale( double inScalar );
/**
* Gets the scalar distance between two vectors.
*
* @param inOther vector to compute the distance with.
*
* @return the distance between the two vectors.
*/
double getDistance( Vector3D *inOther );
/**
* Gets the scalar distance between two vectors ignoring the y
* components.
*
* @param inOther vector to compute the distance with.
*
* @return the xz distance between the two vectors.
*/
double getXZDistance( Vector3D *inOther );
/**
* Rotates the vector about the origin.
*
* @param inAngle the angle to rotate the vector by.
*/
void rotate( Angle3D *inAngle );
/**
* Rotates the vector about the origin in reverse direction.
*
* @param inAngle the angle to rotate the object by
* in reverse direction.
*/
void reverseRotate( Angle3D *inAngle );
/**
* Prints this vector to standard out.
*/
void print();
// implement the Serializable interface
virtual int serialize( OutputStream *inOutputStream );
virtual int deserialize( InputStream *inInputStream );
};
inline Vector3D::Vector3D( double inX, double inY, double inZ )
: mX( inX ), mY( inY ), mZ( inZ ) {
}
inline Vector3D::Vector3D()
: mX( 0 ), mY( 0 ), mZ( 0 ) {
}
inline Vector3D::Vector3D( Vector3D *inOther )
: mX( inOther->mX ), mY( inOther->mY ), mZ( inOther->mZ ) {
}
inline void Vector3D::setCoordinates( double inX, double inY, double inZ ) {
mX = inX;
mY = inY;
mZ = inZ;
}
inline void Vector3D::setCoordinates( Vector3D *inOther ) {
setCoordinates( inOther->mX,
inOther->mY,
inOther->mZ );
}
inline void Vector3D::normalize() {
scale( 1/sqrt( dot( this ) ) );
}
inline char Vector3D::equals( Vector3D *inOther ) {
return
mX == inOther->mX &&
mY == inOther->mY &&
mZ == inOther->mZ;
}
inline void Vector3D::add( Vector3D *inOther ) {
mX += inOther->mX;
mY += inOther->mY;
mZ += inOther->mZ;
}
inline void Vector3D::subtract( Vector3D *inOther ) {
mX -= inOther->mX;
mY -= inOther->mY;
mZ -= inOther->mZ;
}
inline double Vector3D::dot( Vector3D *inOther ) {
return mX * inOther->mX + mY * inOther->mY + mZ * inOther->mZ;
}
inline Vector3D *Vector3D::cross( Vector3D *inOther ) {
double i = this->mY * inOther->mZ - this->mZ * inOther->mY;
double j = this->mZ * inOther->mX - this->mX * inOther->mZ;
double k = this->mX * inOther->mY - this->mY * inOther->mX;
return new Vector3D( i, j, k );
}
inline double Vector3D::getAngleTo( Vector3D *inOther ) {
// normalize and remove z component
Vector3D *normalThis = new Vector3D( this );
normalThis->normalize();
Vector3D *normalOther = new Vector3D( inOther );
normalOther->normalize();
double cosineOfAngle = normalThis->dot( normalOther );
// cosine is ambiguous (same for negative and positive angles)
// compute cross product of vectors
// the magnitude of the cross is the sine of the angle between the two
// vectors
Vector3D *crossVector = normalThis->cross( normalOther );
double sineOfAngle = crossVector->getLength();
delete crossVector;
delete normalThis;
delete normalOther;
double angle = acos( cosineOfAngle );
if( sineOfAngle > 0 ) {
angle = -angle;
}
// if vectors are very close, our dot product above might give
// a value greater than 1 due to round-off errors
// this causes acos to return NAN
if( cosineOfAngle >= 1 ) {
angle = 0;
}
// also need to worry if vectors are complete opposites
else if( cosineOfAngle <= -1 ) {
angle = M_PI;
}
return angle;
}
inline void Vector3D::rotate( Vector3D *inAxis, double inAngle ) {
// this formula found here:
// http://www.gamedev.net/reference/articles/article1199.asp
double c = cos( inAngle );
double s = sin( inAngle );
double t = 1 - c;
// we assume inAxis is a unit vector (normalized)
double x = inAxis->mX;
double y = inAxis->mY;
double z = inAxis->mZ;
double sx = s * x;
double sy = s * y;
double sz = s * z;
double tx = t * x;
double ty = t * y;
double txx = tx * x;
double txy = tx * y;
double txz = tx * z;
double tyy = ty * y;
double tyz = ty * z;
double tzz = t * z * z;
/*
The rotation matrix is:
(txx + c) (txy + sz) (txz - sy)
(txy - sz) (tyy + c) (tyz + sx)
(txz + sy) (tyz - sx) (tzz + c)
*/
double newX =
(txx + c) * mX +
(txy + sz) * mY +
(txz - sy) * mZ;
double newY =
(txy - sz) * mX +
(tyy + c) * mY +
(tyz + sx) * mZ;
double newZ =
(txz + sy) * mX +
(tyz - sx) * mY +
(tzz + c) * mZ;
mX = newX;
mY = newY;
mZ = newZ;
}
inline Angle3D *Vector3D::getZAngleTo( Vector3D *inOther ) {
// normalize and remove z component
Vector3D *normalThis = new Vector3D( this );
normalThis->mZ = 0;
normalThis->normalize();
Vector3D *normalOther = new Vector3D( inOther );
normalOther->mZ = 0;
normalOther->normalize();
double cosineOfZAngle = normalThis->dot( normalOther );
// cosine is ambiguous (same for negative and positive angles)
// compute dot product with perpendicular vector to get sine
// sign of sine will tell us whether angle is positive or negative
Angle3D *rightAngleZ = new Angle3D( 0, 0, M_PI / 2 );
normalThis->rotate( rightAngleZ );
delete rightAngleZ;
double sineOfZAngle = normalThis->dot( normalOther );
delete normalThis;
delete normalOther;
double zAngle = acos( cosineOfZAngle );
if( sineOfZAngle < 0 ) {
zAngle = -zAngle;
}
// if vectors are very close, our dot product above might give
// a value greater than 1 due to round-off errors
// this causes acos to return NAN
if( cosineOfZAngle >= 1 ) {
zAngle = 0;
}
// also need to worry if vectors are complete opposites
else if( cosineOfZAngle <= -1 ) {
zAngle = M_PI;
}
return new Angle3D( 0, 0, zAngle );
}
inline Angle3D *Vector3D::getYAngleTo( Vector3D *inOther ) {
// normalize and remove y component
Vector3D *normalThis = new Vector3D( this );
normalThis->mY = 0;
normalThis->normalize();
Vector3D *normalOther = new Vector3D( inOther );
normalOther->mY = 0;
normalOther->normalize();
double cosineOfYAngle = normalThis->dot( normalOther );
// cosine is ambiguous (same for negative and positive angles)
// compute dot product with perpendicular vector to get sine
// sign of sine will tell us whether angle is positive or negative
Angle3D *rightAngleY = new Angle3D( 0, M_PI / 2, 0 );
normalThis->rotate( rightAngleY );
delete rightAngleY;
double sineOfYAngle = normalThis->dot( normalOther );
delete normalThis;
delete normalOther;
double yAngle = acos( cosineOfYAngle );
if( sineOfYAngle < 0 ) {
yAngle = -yAngle;
}
// if vectors are very close, our dot product above might give
// a value greater than 1 due to round-off errors
// this causes acos to return NAN
if( cosineOfYAngle >= 1 ) {
yAngle = 0;
}
// also need to worry if vectors are complete opposites
else if( cosineOfYAngle <= -1 ) {
yAngle = M_PI;
}
return new Angle3D( 0, yAngle, 0 );
}
inline Angle3D *Vector3D::getXAngleTo( Vector3D *inOther ) {
// normalize and remove y component
Vector3D *normalThis = new Vector3D( this );
normalThis->mX = 0;
normalThis->normalize();
Vector3D *normalOther = new Vector3D( inOther );
normalOther->mX = 0;
normalOther->normalize();
double cosineOfXAngle = normalThis->dot( normalOther );
// cosine is ambiguous (same for negative and positive angles)
// compute dot product with perpendicular vector to get sine
// sign of sine will tell us whether angle is positive or negative
Angle3D *rightAngleX = new Angle3D( M_PI / 2, 0, 0 );
normalThis->rotate( rightAngleX );
delete rightAngleX;
double sineOfXAngle = normalThis->dot( normalOther );
delete normalThis;
delete normalOther;
double xAngle = acos( cosineOfXAngle );
if( sineOfXAngle < 0 ) {
xAngle = -xAngle;
}
// if vectors are very close, our dot product above might give
// a value greater than 1 due to round-off errors
// this causes acos to return NAN
if( cosineOfXAngle >= 1 ) {
xAngle = 0;
}
// also need to worry if vectors are complete opposites
else if( cosineOfXAngle <= -1 ) {
xAngle = M_PI;
}
return new Angle3D( xAngle, 0, 0 );
}
inline Vector3D *Vector3D::linearSum( Vector3D *inFirst, Vector3D *inSecond,
double inFirstWeight ) {
double secondWeight = 1 - inFirstWeight;
double x = inFirstWeight * inFirst->mX + secondWeight * inSecond->mX;
double y = inFirstWeight * inFirst->mY + secondWeight * inSecond->mY;
double z = inFirstWeight * inFirst->mZ + secondWeight * inSecond->mZ;
return new Vector3D( x, y, z );
}
inline double Vector3D::getLength() {
return sqrt( dot( this ) );
}
inline void Vector3D::scale( double inScalar ) {
mX *= inScalar;
mY *= inScalar;
mZ *= inScalar;
}
inline double Vector3D::getDistance( Vector3D *inOther ) {
double delX = mX - inOther->mX;
double delY = mY - inOther->mY;
double delZ = mZ - inOther->mZ;
return sqrt( delX * delX + delY * delY + delZ * delZ );
}
inline double Vector3D::getXZDistance( Vector3D *inOther ) {
double delX = mX - inOther->mX;
double delZ = mZ - inOther->mZ;
return sqrt( delX * delX + delZ * delZ );
}
inline void Vector3D::rotate( Angle3D *inAngle ) {
if( inAngle->mX != 0 ) {
double cosTheta = cos( inAngle->mX );
double sinTheta = sin( inAngle->mX );
double oldY = mY;
mY = mY * cosTheta - mZ * sinTheta;
mZ = oldY * sinTheta + mZ * cosTheta;
}
if( inAngle->mY != 0 ) {
double cosTheta = cos( inAngle->mY );
double sinTheta = sin( inAngle->mY );
double oldX = mX;
mX = cosTheta * mX + sinTheta * mZ;
mZ = -sinTheta * oldX + cosTheta * mZ;
}
if( inAngle->mZ != 0 ) {
double cosTheta = cos( inAngle->mZ );
double sinTheta = sin( inAngle->mZ );
double oldX = mX;
mX = cosTheta * mX - sinTheta * mY;
mY = sinTheta * oldX + cosTheta * mY;
}
}
inline void Vector3D::reverseRotate( Angle3D *inAngle ) {
Angle3D *actualAngle =
new Angle3D( -inAngle->mX, -inAngle->mY, -inAngle->mZ );
rotate( actualAngle );
delete actualAngle;
}
inline void Vector3D::print() {
printf( "(%f, %f, %f)", mX, mY, mZ );
}
inline int Vector3D::serialize( OutputStream *inOutputStream ) {
int numBytes = 0;
numBytes += inOutputStream->writeDouble( mX );
numBytes += inOutputStream->writeDouble( mY );
numBytes += inOutputStream->writeDouble( mZ );
return numBytes;
}
inline int Vector3D::deserialize( InputStream *inInputStream ) {
int numBytes = 0;
numBytes += inInputStream->readDouble( &( mX ) );
numBytes += inInputStream->readDouble( &( mY ) );
numBytes += inInputStream->readDouble( &( mZ ) );
return numBytes;
}
#endif
|