cAudio  2.3.0
3d Audio Engine
cVector3.h
1 // Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2 // This file is part of the "cAudio Engine"
3 // For conditions of distribution and use, see copyright notice in cAudio.h
4 
5 #pragma once
6 
7 #include <math.h>
8 #include <limits>
9 
10 namespace cAudio
11 {
13  const float Epsilon = std::numeric_limits<float>::epsilon();
14 
16  inline bool float_equals(const float a, const float b)
17  {
18  return (a + Epsilon >= b) && (a - Epsilon <= b);
19  }
20 
22  class cVector3
23  {
24  public:
25  float x, y, z;
26 
28  cVector3(void) : x(0), y(0), z(0)
29  {
30  }
31 
32  cVector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
33  {
34  }
35 
37  cVector3(float n) : x(n), y(n), z(n)
38  {
39  }
40 
41  cVector3(const cVector3& other) : x(other.x), y(other.y), z(other.z)
42  {
43  }
44 
45  cVector3(float* vector) : x(vector[0]), y(vector[1]), z(vector[2])
46  {
47  }
48 
49  cVector3 operator-() const { return cVector3(-x, -y, -z); }
50  cVector3& operator=(const cVector3& other) { x = other.x; y = other.y; z = other.z; return *this; }
51  cVector3 operator+(const cVector3& other) const { return cVector3(x + other.x, y + other.y, z + other.z); }
52  cVector3& operator+=(const cVector3& other) { x+=other.x; y+=other.y; z+=other.z; return *this; }
53  cVector3 operator+(const float val) const { return cVector3(x + val, y + val, z + val); }
54  cVector3& operator+=(const float val) { x+=val; y+=val; z+=val; return *this; }
55 
56  cVector3 operator-(const cVector3& other) const { return cVector3(x - other.x, y - other.y, z - other.z); }
57  cVector3& operator-=(const cVector3& other) { x-=other.x; y-=other.y; z-=other.z; return *this; }
58  cVector3 operator-(const float val) const { return cVector3(x - val, y - val, z - val); }
59  cVector3& operator-=(const float val) { x-=val; y-=val; z-=val; return *this; }
60 
61  cVector3 operator*(const cVector3& other) const { return cVector3(x * other.x, y * other.y, z * other.z); }
62  cVector3& operator*=(const cVector3& other) { x*=other.x; y*=other.y; z*=other.z; return *this; }
63  cVector3 operator*(const float v) const { return cVector3(x * v, y * v, z * v); }
64  cVector3& operator*=(const float v) { x*=v; y*=v; z*=v; return *this; }
65 
66  cVector3 operator/(const cVector3& other) const { return cVector3(x / other.x, y / other.y, z / other.z); }
67  cVector3& operator/=(const cVector3& other) { x/=other.x; y/=other.y; z/=other.z; return *this; }
68  cVector3 operator/(const float v) const { float i=(float)1.0/v; return cVector3(x * i, y * i, z * i); }
69  cVector3& operator/=(const float v) { float i=(float)1.0/v; x*=i; y*=i; z*=i; return *this; }
70 
71  bool operator<=(const cVector3& other) const { return x<=other.x && y<=other.y && z<=other.z;}
72  bool operator>=(const cVector3& other) const { return x>=other.x && y>=other.y && z>=other.z;}
73  bool operator<(const cVector3& other) const { return x<other.x && y<other.y && z<other.z;}
74  bool operator>(const cVector3& other) const { return x>other.x && y>other.y && z>other.z;}
75 
76  bool operator==(const cVector3& other) const
77  {
78  return float_equals(x, other.x) &&
79  float_equals(y, other.y) &&
80  float_equals(z, other.z);
81  }
82 
83  bool operator!=(const cVector3& other) const
84  {
85  return !(float_equals(x, other.x) &&
86  float_equals(y, other.y) &&
87  float_equals(z, other.z));
88  }
89 
90  operator const float*() const { return &x; }
91 
92  operator float*() { return &x; }
93 
94  float operator[] ( int i ) const { return ( ( float* ) &x ) [i]; }
95 
96  float &operator[] ( int i ) { return ( ( float* ) &x ) [i]; }
97 
99  float length() const
100  {
101  return sqrtf( x*x + y*y + z*z );
102  }
103 
105  void normalize()
106  {
107  float invLen = 1.0f / length();
108  x *= invLen;
109  y *= invLen;
110  z *= invLen;
111  }
112 
114  float dot( const cVector3& other ) const
115  {
116  return ( x * other.x + y * other.y + z * other.z );
117  }
118 
120  cVector3 cross( const cVector3& other ) const
121  {
122  return cVector3( y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x );
123  }
124 
126  void set( float nx, float ny, float nz )
127  {
128  x = nx;
129  y = ny;
130  z = nz;
131  }
132 
134  void set( float n )
135  {
136  x = y = z = n;
137  }
138 
140  void set( const cVector3& other )
141  {
142  x = other.x;
143  y = other.y;
144  z = other.z;
145  }
146 
147  void getAsArray(float* output)
148  {
149  output[0] = x;
150  output[1] = y;
151  output[2] = z;
152  }
153  };
154 };
cAudio::Epsilon
const float Epsilon
Smallest number that can be represented with a 32 bit float (may not match your compiler/os varient)
Definition: cVector3.h:13
cAudio::cVector3::set
void set(float nx, float ny, float nz)
Sets the components of this vector.
Definition: cVector3.h:126
cAudio::cVector3::normalize
void normalize()
Forces the current vector to have a length of 1 while preserving the ratio of components.
Definition: cVector3.h:105
cAudio::cVector3::cVector3
cVector3(float n)
Constructor, initializes all 3 axes to the same value.
Definition: cVector3.h:37
cAudio::cVector3::length
float length() const
Returns the length (magnitude) of the vector.
Definition: cVector3.h:99
cAudio::cVector3
Class for manipulating vectors in 3D space.
Definition: cVector3.h:23
cAudio
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:16
cAudio::float_equals
bool float_equals(const float a, const float b)
Internal function that compares two floats while keeping the Epsilon in mind.
Definition: cVector3.h:16
cAudio::cVector3::dot
float dot(const cVector3 &other) const
Returns the dot product of this vector with the input vector.
Definition: cVector3.h:114
cAudio::cVector3::set
void set(const cVector3 &other)
Sets this vector's components to match the input vector's.
Definition: cVector3.h:140
cAudio::cVector3::set
void set(float n)
Sets all components of this vector to the same number.
Definition: cVector3.h:134
cAudio::cVector3::cVector3
cVector3(void)
Default constructor, initializes everything to 0.
Definition: cVector3.h:28
cAudio::cVector3::cross
cVector3 cross(const cVector3 &other) const
Returns the cross product of this vector with the input vector.
Definition: cVector3.h:120