Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/vector.h @ 7563

Last change on this file since 7563 was 7191, checked in by bensch, 19 years ago

orxonox/trunk: some better and faster slerp functions

File size: 6.7 KB
RevLine 
[5420]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: ...
14*/
15
[4578]16/*!
[5008]17 * @file vector.h
18 * A basic 3D math framework
19 *
20 * Contains classes to handle vectors, lines, rotations and planes
[4578]21*/
[2043]22
[5982]23#ifndef __VECTOR_H_
24#define __VECTOR_H_
[2043]25
26#include <math.h>
[3860]27#include "compiler.h"
[3449]28//! PI the circle-constant
[2043]29#define PI 3.14159265359f
30
[5672]31
32//! this is a small and performant 3D vector
33typedef float sVec3D[3];
34
35
36//! small and performant 2D vector
37typedef float sVec2D[2];
38
39
40
[2190]41//! 3D Vector
[2043]42/**
[4578]43        Class to handle 3D Vectors
[2043]44*/
45class Vector {
[4476]46 public:
[2043]47  Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor
48  Vector () : x(0), y(0), z(0) {}
49  ~Vector () {}
50
[5052]51  /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */
52  inline bool operator== (const Vector& v) const { return (this->x==v.x&&this->y==v.y&&this->z==v.z)?true:false; };
[4836]53  /** @param index The index of the "array" @returns the x/y/z coordinate */
[4562]54  inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; }
[4992]55  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[4476]56  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); };
[4992]57  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[4609]58  inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); };
[4836]59  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
[4476]60  inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; };
[4836]61  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[4609]62  inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; };
[4836]63  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[3819]64  inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
[4836]65  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[4609]66  inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); }
[4836]67  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[4476]68  inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; };
[4836]69  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[4609]70  inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; };
[4836]71  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
[4476]72  inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; };
[4836]73  /** @todo strange */
[4476]74  inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; };
[4836]75  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
[4476]76  inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); };
[4836]77  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
[4476]78  inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; };
[4836]79  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
[4997]80  inline Vector operator/ (float f) const { return (unlikely(f == 0.0))?Vector(0,0,0):Vector(this->x / f, this->y / f, this->z / f); };
[4836]81  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
[4476]82  inline const Vector& operator/= (float f) {if (unlikely(f == 0.0)) {this->x=0;this->y=0;this->z=0;} else {this->x /= f; this->y /= f; this->z /= f;} return *this; };
[4992]83  /**  copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */
[4476]84  inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; };
[4992]85  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
[4545]86  inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; }
[4836]87  /** @param v: the other vector \return the dot product of the vectors */
[4476]88  float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; };
[4836]89  /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */
[3966]90  inline Vector cross (const Vector& v) const { return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x ); }
[4992]91  /** scales the this vector with v* @param v the vector to scale this with */
[4476]92  void scale(const Vector& v) {   x *= v.x;  y *= v.y; z *= v.z; };
[4836]93  /** @returns the length of the vector */
[3819]94  inline float len() const { return sqrt (x*x+y*y+z*z); }
[4992]95  /** normalizes the vector */
[5053]96  inline void normalize() { float l = len(); if( unlikely(l == 0.0))return; this->x=this->x/l; this->y=this->y/l; this->z=this->z/l; };
[4372]97  Vector getNormalized() const;
[2551]98  Vector abs();
[7191]99  /** @param toVec nears this Vector to... @param t how much to advance (0.0: not at all; 1.0: fully) */
100  inline void slerpTo(const Vector& toVec, float t) { *this + (toVec - *this) * t; };
[3541]101
[3966]102  void debug() const;
[4476]103
104 public:
105  float    x;     //!< The x Coordinate of the Vector.
106  float    y;     //!< The y Coordinate of the Vector.
107  float    z;     //!< The z Coordinate of the Vector.
[2043]108};
109
[4476]110/**
[4836]111 *  calculate the angle between two vectors in radiances
112 * @param v1: a vector
113 * @param v2: another vector
114 * @return the angle between the vectors in radians
[4476]115*/
116inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); };
117/**
[4836]118 *  calculate the angle between two vectors in degrees
119 * @param v1: a vector
120 * @param v2: another vector
121 * @return the angle between the vectors in degrees
[4476]122*/
123inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; };
[2043]124
[5008]125/** an easy way to create a Random Vector @param sideLength the length of the Vector (x not sqrt(x^2...)) */
126#define VECTOR_RAND(sideLength)  (Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
[4476]127
[5982]128#endif /* __VECTOR_H_ */
[4997]129
Note: See TracBrowser for help on using the repository browser.