Audaspace  1.3.0
A high level audio library.
Math3D.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #pragma once
18 
25 #include "Audaspace.h"
26 
27 #include <cmath>
28 #include <cstring>
29 
31 
36 {
37 private:
41  union
42  {
43  float m_v[3];
44  struct
45  {
46  float m_x;
47  float m_y;
48  float m_z;
49  };
50  };
51 
52 public:
59  inline Vector3(float x = 0, float y = 0, float z = 0) :
60  m_x(x), m_y(y), m_z(z)
61  {
62  }
63 
68  inline const float& x() const
69  {
70  return m_x;
71  }
72 
77  inline const float& y() const
78  {
79  return m_y;
80  }
81 
86  inline const float& z() const
87  {
88  return m_z;
89  }
90 
95  inline void get(float* destination) const
96  {
97  std::memcpy(destination, m_v, sizeof(m_v));
98  }
99 
104  inline float* get()
105  {
106  return m_v;
107  }
108 
113  inline const float* get() const
114  {
115  return m_v;
116  }
117 
122  inline float length() const
123  {
124  return std::sqrt(m_x*m_x + m_y*m_y + m_z*m_z);
125  }
126 
132  inline Vector3 cross(const Vector3& op) const
133  {
134  return Vector3(m_y * op.m_z - m_z * op.m_y,
135  m_z * op.m_x - m_x * op.m_z,
136  m_x * op.m_y - m_y * op.m_x);
137  }
138 
144  inline float operator*(const Vector3& op) const
145  {
146  return m_x * op.m_x + m_y * op.m_y + m_z * op.m_z;
147  }
148 
154  inline Vector3 operator*(const float& op) const
155  {
156  return Vector3(m_x * op, m_y * op, m_z * op);
157  }
158 
164  inline Vector3 operator+(const Vector3& op) const
165  {
166  return Vector3(m_x + op.m_x, m_y + op.m_y, m_z + op.m_z);
167  }
168 
174  inline Vector3 operator-(const Vector3& op) const
175  {
176  return Vector3(m_x - op.m_x, m_y - op.m_y, m_z - op.m_z);
177  }
178 
183  inline Vector3 operator-() const
184  {
185  return Vector3(-m_x, -m_y, -m_z);
186  }
187 
193  inline Vector3& operator-=(const Vector3& op)
194  {
195  m_x -= op.m_x;
196  m_y -= op.m_y;
197  m_z -= op.m_z;
198  return *this;
199  }
200 };
201 
206 {
207 private:
211  union
212  {
213  float m_v[4];
214  struct
215  {
216  float m_w;
217  float m_x;
218  float m_y;
219  float m_z;
220  };
221  };
222 
223 public:
231  inline Quaternion(float w = 1, float x = 0, float y = 0, float z = 0) :
232  m_w(w), m_x(x), m_y(y), m_z(z)
233  {
234  }
235 
240  inline const float& w() const
241  {
242  return m_w;
243  }
244 
249  inline const float& x() const
250  {
251  return m_x;
252  }
253 
258  inline const float& y() const
259  {
260  return m_y;
261  }
262 
267  inline const float& z() const
268  {
269  return m_z;
270  }
271 
276  inline void get(float* destination) const
277  {
278  std::memcpy(destination, m_v, sizeof(m_v));
279  }
280 
285  inline float* get()
286  {
287  return m_v;
288  }
289 
294  inline const float* get() const
295  {
296  return m_v;
297  }
298 
304  inline Vector3 getLookAt() const
305  {
306  return Vector3(-2 * (m_w * m_y + m_x * m_z),
307  2 * (m_x * m_w - m_z * m_y),
308  2 * (m_x * m_x + m_y * m_y) - 1);
309  }
310 
316  inline Vector3 getUp() const
317  {
318  return Vector3(2 * (m_x * m_y - m_w * m_z),
319  1 - 2 * (m_x * m_x + m_z * m_z),
320  2 * (m_w * m_x + m_y * m_z));
321  }
322 };
323 
This class represents a quaternion used for 3D rotations.
Definition: Math3D.h:205
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition: Audaspace.h:116
Vector3 operator+(const Vector3 &op) const
Adds two vectors.
Definition: Math3D.h:164
const float & y() const
Retrieves the y component of the vector.
Definition: Math3D.h:77
Vector3 operator-() const
Negates the vector.
Definition: Math3D.h:183
const float & y() const
Retrieves the y component of the quarternion.
Definition: Math3D.h:258
const float & z() const
Retrieves the z component of the quarternion.
Definition: Math3D.h:267
#define AUD_API
Used for exporting symbols in the shared library.
Definition: Audaspace.h:93
Quaternion(float w=1, float x=0, float y=0, float z=0)
Creates a new quaternion.
Definition: Math3D.h:231
Vector3 & operator-=(const Vector3 &op)
Subtracts the second vector.
Definition: Math3D.h:193
const float & x() const
Retrieves the x component of the vector.
Definition: Math3D.h:68
float length() const
Retrieves the length of the vector.
Definition: Math3D.h:122
Vector3(float x=0, float y=0, float z=0)
Creates a new 3 dimensional vector.
Definition: Math3D.h:59
const float & x() const
Retrieves the x component of the quarternion.
Definition: Math3D.h:249
Vector3 operator-(const Vector3 &op) const
Subtracts two vectors.
Definition: Math3D.h:174
float operator*(const Vector3 &op) const
Retrieves the dot product.
Definition: Math3D.h:144
Vector3 getLookAt() const
When the quaternion represents an orientation, this returns the negative z axis vector.
Definition: Math3D.h:304
Vector3 operator*(const float &op) const
Retrieves the product with a scalar.
Definition: Math3D.h:154
const float & z() const
Retrieves the z component of the vector.
Definition: Math3D.h:86
Vector3 cross(const Vector3 &op) const
Retrieves the cross product.
Definition: Math3D.h:132
Vector3 getUp() const
When the quaternion represents an orientation, this returns the y axis vector.
Definition: Math3D.h:316
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition: Audaspace.h:119
const float & w() const
Retrieves the w component of the quarternion.
Definition: Math3D.h:240
This class represents a 3 dimensional vector.
Definition: Math3D.h:35
The main header file of the library defining the namespace and basic data types.