Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3814 in orxonox.OLD


Ignore:
Timestamp:
Apr 13, 2005, 11:39:33 PM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: working on vector class, making it more performant by reducing nr of objects created in operators and inlining them

Location:
orxonox/trunk/src/lib/math
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/math/vector.cc

    r3607 r3814  
    2323#include "vector.h"
    2424#include "debug.h"
     25#include "stdincl.h"
    2526
    2627using namespace std;
     
    3132   \return the sum of both vectors
    3233*/
     34/*
    3335Vector Vector::operator+ (const Vector& v) const
    3436{
    35   Vector r;
    36 
    37   r.x = x + v.x;
    38   r.y = y + v.y;
    39   r.z = z + v.z;
    40  
    41   return r;
    42 }
     37  return Vector(x + v.x, y + v.y, z + v.z);
     38}
     39*/
    4340
    4441/**
     
    4946Vector Vector::operator- (const Vector& v) const
    5047{
    51   Vector r;
    52  
    53   r.x = x - v.x;
    54   r.y = y - v.y;
    55   r.z = z - v.z;
    56  
    57   return r;
     48  return Vector(x - v.x, y - v.y, z - v.z);
    5849}
    5950
     
    7566Vector Vector::operator* (float f) const
    7667
    77   Vector r;
    78  
    79   r.x = x * f;
    80   r.y = y * f;
    81   r.z = z * f;
    82  
    83   return r;
     68  return Vector(x * f, y * f, z * f);
    8469}
    8570
     
    9176Vector Vector::operator/ (float f) const
    9277{
    93   Vector r;
    94  
    95   if( f == 0.0)
     78  __UNLIKELY_IF( f == 0.0)
    9679  {
    9780    // Prevent divide by zero
    9881    return Vector (0,0,0);
    9982  }
    100  
    101   r.x = x / f;
    102   r.y = y / f;
    103   r.z = z / f;
    104  
    105   return r;
     83  return Vector(x / f, y / f, z / f);
    10684}
    10785
     
    123101Vector Vector::cross (const Vector& v) const
    124102{
    125   Vector r;
    126  
    127   r.x = y * v.z - z * v.y;
    128   r.y = z * v.x - x * v.z;
    129   r.z = x * v.y - y * v.x;
    130  
    131   return r;
     103  return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x );
    132104}
    133105 
     
    139111  float l = len();
    140112 
    141   if( l == 0.0)
     113  __UNLIKELY_IF( l == 0.0)
    142114  {
    143115    // Prevent divide by zero
     
    158130{
    159131  float l = len();
    160   if(l != 1.0)
     132  __UNLIKELY_IF(l != 1.0)
    161133    {
    162134      return this;
    163135    }
    164   else if(l == 0.0)
     136  else __UNLIKELY_IF(l == 0.0)
    165137    {
    166138      return 0;
    167139    }
    168  
    169   Vector *normalizedVector = new Vector(x / l, y /l, z / l);
    170   return normalizedVector;
     140
     141  return new Vector(x / l, y /l, z / l);
    171142}
    172143
     
    365336Quaternion Quaternion::operator+(const Quaternion& q) const
    366337{
    367         Quaternion r(*this);
    368         r.w = r.w + q.w;
    369         r.v = r.v + q.v;
    370         return r;
    371 }
    372 
    373 /**
    374         \brief subtract two Quaternions
    375         \param q: another Quaternion
    376         \return the difference of both Quaternions
     338  Quaternion r(*this);
     339  r.w = r.w + q.w;
     340  r.v = r.v + q.v;
     341  return r;
     342}
     343
     344/**
     345   \brief subtract two Quaternions
     346   \param q: another Quaternion
     347   \return the difference of both Quaternions
    377348*/
    378349Quaternion Quaternion::operator- (const Quaternion& q) const
    379350{
    380         Quaternion r(*this);
    381         r.w = r.w - q.w;
    382         r.v = r.v - q.v;
    383         return r;
    384 }
    385 
    386 /**
    387         \brief rotate a Vector by a Quaternion
    388         \param v: the Vector
    389         \return a new Vector representing v rotated by the Quaternion
     351  Quaternion r(*this);
     352  r.w = r.w - q.w;
     353  r.v = r.v - q.v;
     354  return r;
     355}
     356
     357/**
     358   \brief rotate a Vector by a Quaternion
     359   \param v: the Vector
     360   \return a new Vector representing v rotated by the Quaternion
    390361*/
    391362Vector Quaternion::apply (Vector& v) const
    392363{
    393         Quaternion q;
    394         q.v = v;
    395         q.w = 0;
    396         q = *this * q * conjugate();
    397         return q.v;
    398 }
    399 
    400 /**
    401         \brief multiply a Quaternion with a real value
    402         \param f: a real value
    403         \return a new Quaternion containing the product
     364  Quaternion q;
     365  q.v = v;
     366  q.w = 0;
     367  q = *this * q * conjugate();
     368  return q.v;
     369}
     370
     371/**
     372   \brief multiply a Quaternion with a real value
     373   \param f: a real value
     374   \return a new Quaternion containing the product
    404375*/
    405376Quaternion Quaternion::operator*(const float& f) const
    406377{
    407         Quaternion r(*this);
    408         r.w = r.w*f;
    409         r.v = r.v*f;
    410         return r;
    411 }
    412 
    413 /**
    414         \brief divide a Quaternion by a real value
    415         \param f: a real value
    416         \return a new Quaternion containing the quotient
     378  Quaternion r(*this);
     379  r.w = r.w*f;
     380  r.v = r.v*f;
     381  return r;
     382}
     383
     384/**
     385   \brief divide a Quaternion by a real value
     386   \param f: a real value
     387   \return a new Quaternion containing the quotient
    417388*/
    418389Quaternion Quaternion::operator/(const float& f) const
    419390{
    420         if( f == 0) return Quaternion();
    421         Quaternion r(*this);
    422         r.w = r.w/f;
    423         r.v = r.v/f;
    424         return r;
    425 }
    426 
    427 /**
    428         \brief calculate the conjugate value of the Quaternion
    429         \return the conjugate Quaternion
     391  if( f == 0) return Quaternion();
     392  Quaternion r(*this);
     393  r.w = r.w/f;
     394  r.v = r.v/f;
     395  return r;
     396}
     397
     398/**
     399   \brief calculate the conjugate value of the Quaternion
     400   \return the conjugate Quaternion
    430401*/
    431402Quaternion Quaternion::conjugate() const
    432403{
    433         Quaternion r(*this);
    434         r.v = Vector() - r.v;
    435         return r;
    436 }
    437 
    438 /**
    439         \brief calculate the norm of the Quaternion
    440         \return the norm of The Quaternion
     404  Quaternion r(*this);
     405  r.v = Vector() - r.v;
     406  return r;
     407}
     408
     409/**
     410   \brief calculate the norm of the Quaternion
     411   \return the norm of The Quaternion
    441412*/
    442413float Quaternion::norm() const
    443414{
    444         return w*w + v.x*v.x + v.y*v.y + v.z*v.z;
    445 }
    446 
    447 /**
    448         \brief calculate the inverse value of the Quaternion
    449         \return the inverse Quaternion
    450        
     415  return w*w + v.x*v.x + v.y*v.y + v.z*v.z;
     416}
     417
     418/**
     419   \brief calculate the inverse value of the Quaternion
     420   \return the inverse Quaternion
     421   
    451422        Note that this is equal to conjugate() if the Quaternion's norm is 1
    452423*/
    453424Quaternion Quaternion::inverse() const
    454425{
    455         float n = norm();
    456         if (n != 0)
    457         {
    458                 return conjugate() / norm();
    459         }
    460         else return Quaternion();
    461 }
    462 
    463 /**
    464         \brief convert the Quaternion to a 4x4 rotational glMatrix
    465         \param m: a buffer to store the Matrix in
     426  float n = norm();
     427  if (n != 0)
     428    {
     429      return conjugate() / norm();
     430    }
     431  else return Quaternion();
     432}
     433
     434/**
     435   \brief convert the Quaternion to a 4x4 rotational glMatrix
     436   \param m: a buffer to store the Matrix in
    466437*/
    467438void Quaternion::matrix (float m[4][4]) const
  • orxonox/trunk/src/lib/math/vector.h

    r3541 r3814  
    2929  ~Vector () {}
    3030
    31   Vector operator+ (const Vector& v) const;
     31  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
    3232  Vector operator- (const Vector& v) const;
    3333  float operator* (const Vector& v) const;
Note: See TracChangeset for help on using the changeset viewer.