Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/math/vector.h @ 8538

Last change on this file since 8538 was 8490, checked in by patrick, 18 years ago

merged the bsp branche back to trunk

File size: 7.1 KB
Line 
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
16/*!
17 * @file vector.h
18 * A basic 3D math framework
19 *
20 * Contains classes to handle vectors, lines, rotations and planes
21*/
22
23#ifndef __VECTOR_H_
24#define __VECTOR_H_
25
26#include <math.h>
27#include "compiler.h"
28//! PI the circle-constant
29#define PI 3.14159265359f
30
31
32//! this is a small and performant 3D vector
33typedef float sVec3D[3];
34
35
36//! 3D Vector
37/**
38        Class to handle 3D Vectors
39*/
40class Vector {
41 public:
42  Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor
43  Vector () : x(0), y(0), z(0) {}
44  ~Vector () {}
45
46  /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */
47  inline bool operator== (const Vector& v) const { return (this->x==v.x&&this->y==v.y&&this->z==v.z)?true:false; };
48  /** @param v: the Vecor to compare with this one @returns true, if the Vecors are different, false otherwise */
49  inline bool operator!= (const Vector& v) const { return (this->x!=v.x&&this->y!=v.y&&this->z!=v.z)?true:false; };
50  /** @param index The index of the "array" @returns the x/y/z coordinate */
51  inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; }
52  /** @param v The vector to add @returns the addition between two vectors (this + v) */
53  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); };
54  /** @param v The vector to add @returns the addition between two vectors (this + v) */
55  inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); };
56  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
57  inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; };
58  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
59  inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; };
60  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
61  inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
62  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
63  inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); }
64  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
65  inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; };
66  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
67  inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; };
68  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
69  inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; };
70  /** @todo strange */
71  inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; };
72  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
73  inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); };
74  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
75  inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; };
76  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
77  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); };
78  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
79  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; };
80  /**  copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */
81  inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; };
82  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
83  inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; return *this; }
84  inline const Vector& operator= (const float* v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; return *this; }
85  /** @param v: the other vector \return the dot product of the vectors */
86  float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; };
87  /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */
88  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 ); }
89  /** scales the this vector with v* @param v the vector to scale this with */
90  void scale(const Vector& v) {   x *= v.x;  y *= v.y; z *= v.z; };
91  /** @returns the length of the vector */
92  inline float len() const { return sqrt (x*x+y*y+z*z); }
93  /** normalizes the vector */
94  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; };
95  Vector getNormalized() const;
96  Vector abs();
97
98  /** @param toVec nears this Vector to... @param t how much to advance (0.0: not at all; 1.0: fully) */
99  inline void slerpTo(const Vector& toVec, float t) { *this + (toVec - *this) * t; };
100
101  /** @returns a Null Vector */
102  inline static const Vector& nullVector() { static Vector nullVector; return nullVector; }
103  void debug() const;
104
105 public:
106  float    x;     //!< The x Coordinate of the Vector.
107  float    y;     //!< The y Coordinate of the Vector.
108  float    z;     //!< The z Coordinate of the Vector.
109};
110
111/**
112 *  calculate the angle between two vectors in radiances
113 * @param v1: a vector
114 * @param v2: another vector
115 * @return the angle between the vectors in radians
116*/
117inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); };
118/**
119 *  calculate the angle between two vectors in degrees
120 * @param v1: a vector
121 * @param v2: another vector
122 * @return the angle between the vectors in degrees
123*/
124inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; };
125
126/** an easy way to create a Random Vector @param sideLength the length of the Vector (x not sqrt(x^2...)) */
127#define VECTOR_RAND(sideLength)  (Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
128
129#endif /* __VECTOR_H_ */
130
Note: See TracBrowser for help on using the repository browser.