Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6616 in orxonox.OLD for trunk/src/lib/math/quaternion.cc


Ignore:
Timestamp:
Jan 19, 2006, 12:27:45 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: taken the quaternion outside of Vector.cc to quaternion.cc/h

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/math/quaternion.cc

    r6614 r6616  
    2121#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
    2222
    23 #include "vector.h"
     23#include "quaternion.h"
    2424#ifdef DEBUG
    2525  #include "debug.h"
     
    3030
    3131using namespace std;
    32 
    33 /////////////
    34 /* VECTORS */
    35 /////////////
    36 /**
    37  *  returns the this-vector normalized to length 1.0
    38  * @todo there is some error in this function, that i could not resolve. it just does not, what it is supposed to do.
    39  */
    40 Vector Vector::getNormalized() const
    41 {
    42   float l = this->len();
    43   if(unlikely(l == 1.0 || l == 0.0))
    44     return *this;
    45   else
    46     return (*this / l);
    47 }
    48 
    49 /**
    50  *  Vector is looking in the positive direction on all axes after this
    51 */
    52 Vector Vector::abs()
    53 {
    54   Vector v(fabs(x), fabs(y), fabs(z));
    55   return v;
    56 }
    57 
    58 
    59 
    60 /**
    61  *  Outputs the values of the Vector
    62  */
    63 void Vector::debug() const
    64 {
    65   PRINT(0)("x: %f; y: %f; z: %f", x, y, z);
    66   PRINT(0)(" lenght: %f", len());
    67   PRINT(0)("\n");
    68 }
    6932
    7033/////////////////
     
    274237  PRINT(0)("angle = %f, axis: ax=%f, ay=%f, az=%f\n", this->getSpacialAxisAngle(), axis.x, axis.y, axis.z );
    275238}
    276 
    277 /**
    278  *  create a rotation from a vector
    279  * @param v: a vector
    280 */
    281 Rotation::Rotation (const Vector& v)
    282 {
    283   Vector x = Vector( 1, 0, 0);
    284   Vector axis = x.cross( v);
    285   axis.normalize();
    286   float angle = angleRad( x, v);
    287   float ca = cos(angle);
    288   float sa = sin(angle);
    289   m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f);
    290   m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y;
    291   m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z;
    292   m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y;
    293   m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f);
    294   m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z;
    295   m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z;
    296   m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z;
    297   m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f);
    298 }
    299 
    300 /**
    301  *  creates a rotation from an axis and an angle (radians!)
    302  * @param axis: the rotational axis
    303  * @param angle: the angle in radians
    304 */
    305 Rotation::Rotation (const Vector& axis, float angle)
    306 {
    307   float ca, sa;
    308   ca = cos(angle);
    309   sa = sin(angle);
    310   m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f);
    311   m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y;
    312   m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z;
    313   m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y;
    314   m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f);
    315   m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z;
    316   m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z;
    317   m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z;
    318   m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f);
    319 }
    320 
    321 /**
    322  *  creates a rotation from euler angles (pitch/yaw/roll)
    323  * @param pitch: rotation around z (in radians)
    324  * @param yaw: rotation around y (in radians)
    325  * @param roll: rotation around x (in radians)
    326 */
    327 Rotation::Rotation ( float pitch, float yaw, float roll)
    328 {
    329   float cy, sy, cr, sr, cp, sp;
    330   cy = cos(yaw);
    331   sy = sin(yaw);
    332   cr = cos(roll);
    333   sr = sin(roll);
    334   cp = cos(pitch);
    335   sp = sin(pitch);
    336   m[0] = cy*cr;
    337   m[1] = -cy*sr;
    338   m[2] = sy;
    339   m[3] = cp*sr+sp*sy*cr;
    340   m[4] = cp*cr-sp*sr*sy;
    341   m[5] = -sp*cy;
    342   m[6] = sp*sr-cp*sy*cr;
    343   m[7] = sp*cr+cp*sy*sr;
    344   m[8] = cp*cy;
    345 }
    346 
    347 /**
    348  *  creates a nullrotation (an identity rotation)
    349 */
    350 Rotation::Rotation ()
    351 {
    352   m[0] = 1.0f;
    353   m[1] = 0.0f;
    354   m[2] = 0.0f;
    355   m[3] = 0.0f;
    356   m[4] = 1.0f;
    357   m[5] = 0.0f;
    358   m[6] = 0.0f;
    359   m[7] = 0.0f;
    360   m[8] = 1.0f;
    361 }
    362 
    363 /**
    364  *  fills the specified buffer with a 4x4 glmatrix
    365  * @param buffer: Pointer to an array of 16 floats
    366 
    367    Use this to get the rotation in a gl-compatible format
    368 */
    369 void Rotation::glmatrix (float* buffer)
    370 {
    371         buffer[0] = m[0];
    372         buffer[1] = m[3];
    373         buffer[2] = m[6];
    374         buffer[3] = m[0];
    375         buffer[4] = m[1];
    376         buffer[5] = m[4];
    377         buffer[6] = m[7];
    378         buffer[7] = m[0];
    379         buffer[8] = m[2];
    380         buffer[9] = m[5];
    381         buffer[10] = m[8];
    382         buffer[11] = m[0];
    383         buffer[12] = m[0];
    384         buffer[13] = m[0];
    385         buffer[14] = m[0];
    386         buffer[15] = m[1];
    387 }
    388 
    389 /**
    390  *  multiplies two rotational matrices
    391  * @param r: another Rotation
    392  * @return the matrix product of the Rotations
    393 
    394    Use this to rotate one rotation by another
    395 */
    396 Rotation Rotation::operator* (const Rotation& r)
    397 {
    398         Rotation p;
    399 
    400         p.m[0] = m[0]*r.m[0] + m[1]*r.m[3] + m[2]*r.m[6];
    401         p.m[1] = m[0]*r.m[1] + m[1]*r.m[4] + m[2]*r.m[7];
    402         p.m[2] = m[0]*r.m[2] + m[1]*r.m[5] + m[2]*r.m[8];
    403 
    404         p.m[3] = m[3]*r.m[0] + m[4]*r.m[3] + m[5]*r.m[6];
    405         p.m[4] = m[3]*r.m[1] + m[4]*r.m[4] + m[5]*r.m[7];
    406         p.m[5] = m[3]*r.m[2] + m[4]*r.m[5] + m[5]*r.m[8];
    407 
    408         p.m[6] = m[6]*r.m[0] + m[7]*r.m[3] + m[8]*r.m[6];
    409         p.m[7] = m[6]*r.m[1] + m[7]*r.m[4] + m[8]*r.m[7];
    410         p.m[8] = m[6]*r.m[2] + m[7]*r.m[5] + m[8]*r.m[8];
    411 
    412         return p;
    413 }
    414 
    415 
    416 /**
    417  *  rotates the vector by the given rotation
    418  * @param v: a vector
    419  * @param r: a rotation
    420  * @return the rotated vector
    421 */
    422 Vector rotateVector( const Vector& v, const Rotation& r)
    423 {
    424   Vector t;
    425 
    426   t.x = v.x * r.m[0] + v.y * r.m[1] + v.z * r.m[2];
    427   t.y = v.x * r.m[3] + v.y * r.m[4] + v.z * r.m[5];
    428   t.z = v.x * r.m[6] + v.y * r.m[7] + v.z * r.m[8];
    429 
    430   return t;
    431 }
    432 
    433 /**
    434  *  calculate the distance between two lines
    435  * @param l: the other line
    436  * @return the distance between the lines
    437 */
    438 float Line::distance (const Line& l) const
    439 {
    440   float q, d;
    441   Vector n = a.cross(l.a);
    442   q = n.dot(r-l.r);
    443   d = n.len();
    444   if( d == 0.0) return 0.0;
    445   return q/d;
    446 }
    447 
    448 /**
    449  *  calculate the distance between a line and a point
    450  * @param v: the point
    451  * @return the distance between the Line and the point
    452 */
    453 float Line::distancePoint (const Vector& v) const
    454 {
    455   Vector d = v-r;
    456   Vector u = a * d.dot( a);
    457   return (d - u).len();
    458 }
    459 
    460 /**
    461  *  calculate the distance between a line and a point
    462  * @param v: the point
    463  * @return the distance between the Line and the point
    464  */
    465 float Line::distancePoint (const sVec3D& v) const
    466 {
    467   Vector s(v[0], v[1], v[2]);
    468   Vector d = s - r;
    469   Vector u = a * d.dot( a);
    470   return (d - u).len();
    471 }
    472 
    473 /**
    474  *  calculate the two points of minimal distance of two lines
    475  * @param l: the other line
    476  * @return a Vector[2] (!has to be deleted after use!) containing the two points of minimal distance
    477 */
    478 Vector* Line::footpoints (const Line& l) const
    479 {
    480   Vector* fp = new Vector[2];
    481   Plane p = Plane (r + a.cross(l.a), r, r + a);
    482   fp[1] = p.intersectLine (l);
    483   p = Plane (fp[1], l.a);
    484   fp[0] = p.intersectLine (*this);
    485   return fp;
    486 }
    487 
    488 /**
    489   \brief calculate the length of a line
    490   \return the lenght of the line
    491 */
    492 float Line::len() const
    493 {
    494   return a.len();
    495 }
    496 
    497 /**
    498  *  rotate the line by given rotation
    499  * @param rot: a rotation
    500 */
    501 void Line::rotate (const Rotation& rot)
    502 {
    503   Vector t = a + r;
    504   t = rotateVector( t, rot);
    505   r = rotateVector( r, rot),
    506   a = t - r;
    507 }
    508 
    509 /**
    510  *  create a plane from three points
    511  * @param a: first point
    512  * @param b: second point
    513  * @param c: third point
    514 */
    515 Plane::Plane (const Vector& a, const Vector& b, const Vector& c)
    516 {
    517   n = (a-b).cross(c-b);
    518   k = -(n.x*b.x+n.y*b.y+n.z*b.z);
    519 }
    520 
    521 /**
    522  *  create a plane from anchor point and normal
    523  * @param norm: normal vector
    524  * @param p: anchor point
    525 */
    526 Plane::Plane (const Vector& norm, const Vector& p)
    527 {
    528   n = norm;
    529   k = -(n.x*p.x+n.y*p.y+n.z*p.z);
    530 }
    531 
    532 
    533 /**
    534   *  create a plane from anchor point and normal
    535   * @param norm: normal vector
    536   * @param p: anchor point
    537 */
    538 Plane::Plane (const Vector& norm, const sVec3D& g)
    539 {
    540   Vector p(g[0], g[1], g[2]);
    541   n = norm;
    542   k = -(n.x*p.x+n.y*p.y+n.z*p.z);
    543 }
    544 
    545 
    546 /**
    547  *  returns the intersection point between the plane and a line
    548  * @param l: a line
    549 */
    550 Vector Plane::intersectLine (const Line& l) const
    551 {
    552   if (n.x*l.a.x+n.y*l.a.y+n.z*l.a.z == 0.0) return Vector(0,0,0);
    553   float t = (n.x*l.r.x+n.y*l.r.y+n.z*l.r.z+k) / (n.x*l.a.x+n.y*l.a.y+n.z*l.a.z);
    554   return l.r + (l.a * t);
    555 }
    556 
    557 /**
    558  *  returns the distance between the plane and a point
    559  * @param p: a Point
    560  * @return the distance between the plane and the point (can be negative)
    561 */
    562 float Plane::distancePoint (const Vector& p) const
    563 {
    564   float l = n.len();
    565   if( l == 0.0) return 0.0;
    566   return (n.dot(p) + k) / n.len();
    567 }
    568 
    569 
    570 /**
    571  *  returns the distance between the plane and a point
    572  * @param p: a Point
    573  * @return the distance between the plane and the point (can be negative)
    574  */
    575 float Plane::distancePoint (const sVec3D& p) const
    576 {
    577   Vector s(p[0], p[1], p[2]);
    578   float l = n.len();
    579   if( l == 0.0) return 0.0;
    580   return (n.dot(s) + k) / n.len();
    581 }
    582 
    583 
    584 /**
    585  *  returns the side a point is located relative to a Plane
    586  * @param p: a Point
    587  * @return 0 if the point is contained within the Plane, positive(negative) if the point is in the positive(negative) semi-space of the Plane
    588 */
    589 float Plane::locatePoint (const Vector& p) const
    590 {
    591   return (n.dot(p) + k);
    592 }
    593 
Note: See TracChangeset for help on using the changeset viewer.