Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
vector.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include "math/const.h"
29 #include "math/func.h"
30 
31 
32 #include <cmath>
33 #include <sstream>
34 
35 
36 // Math module namespace
37 namespace Math {
38 
39 
52 struct Vector
53 {
55  float x;
57  float y;
59  float z;
60 
62  inline Vector()
63  : x(0.0f)
64  , y(0.0f)
65  , z(0.0f)
66  {}
67 
69  inline explicit Vector(float _x, float _y, float _z)
70  : x(_x)
71  , y(_y)
72  , z(_z)
73  {}
74 
76  inline void LoadZero()
77  {
78  x = y = z = 0.0f;
79  }
80 
82  inline float* Array()
83  {
84  return reinterpret_cast<float*>(this);
85  }
86 
88  inline const float* Array() const
89  {
90  return reinterpret_cast<const float*>(this);
91  }
92 
94  inline float Length() const
95  {
96  return sqrtf(x*x + y*y + z*z);
97  }
98 
100  inline void Normalize()
101  {
102  float l = Length();
103  if (IsZero(l))
104  return;
105 
106  x /= l;
107  y /= l;
108  z /= l;
109  }
110 
112 
116  inline Vector CrossMultiply(const Vector &right) const
117  {
118  float px = y * right.z - z * right.y;
119  float py = z * right.x - x * right.z;
120  float pz = x * right.y - y * right.x;
121  return Vector(px, py, pz);
122  }
123 
125 
129  inline float DotMultiply(const Vector &right) const
130  {
131  return x * right.x + y * right.y + z * right.z;
132  }
133 
135  inline float CosAngle(const Vector &right) const
136  {
137  return DotMultiply(right) / (Length() * right.Length());
138  }
139 
141  inline float Angle(const Vector &right) const
142  {
143  return acos(CosAngle(right));
144  }
145 
146 
147  /* Operators */
148 
150  inline Vector operator-() const
151  {
152  return Vector(-x, -y, -z);
153  }
154 
156  inline const Vector& operator+=(const Vector &right)
157  {
158  x += right.x;
159  y += right.y;
160  z += right.z;
161  return *this;
162  }
163 
165  inline friend const Vector operator+(const Vector &left, const Vector &right)
166  {
167  return Vector(left.x + right.x, left.y + right.y, left.z + right.z);
168  }
169 
171  inline const Vector& operator-=(const Vector &right)
172  {
173  x -= right.x;
174  y -= right.y;
175  z -= right.z;
176  return *this;
177  }
178 
180  inline friend const Vector operator-(const Vector &left, const Vector &right)
181  {
182  return Vector(left.x - right.x, left.y - right.y, left.z - right.z);
183  }
184 
186  inline const Vector& operator*=(const float &right)
187  {
188  x *= right;
189  y *= right;
190  z *= right;
191  return *this;
192  }
193 
195  inline friend const Vector operator*(const float &left, const Vector &right)
196  {
197  return Vector(left * right.x, left * right.y, left * right.z);
198  }
199 
201  inline friend const Vector operator*(const Vector &left, const float &right)
202  {
203  return Vector(left.x * right, left.y * right, left.z * right);
204  }
205 
207  inline const Vector& operator/=(const float &right)
208  {
209  x /= right;
210  y /= right;
211  z /= right;
212  return *this;
213  }
214 
216  inline friend const Vector operator/(const Vector &left, const float &right)
217  {
218  return Vector(left.x / right, left.y / right, left.z / right);
219  }
220 
221 
223  inline std::string ToString() const
224  {
225  std::stringstream s;
226  s.precision(3);
227  s << "[" << x << ", " << y << ", " << z << "]";
228  return s.str();
229  }
230 
231 }; // struct Vector
232 
234 inline bool VectorsEqual(const Math::Vector &a, const Math::Vector &b, float tolerance = TOLERANCE)
235 {
236  return IsEqual(a.x, b.x, tolerance)
237  && IsEqual(a.y, b.y, tolerance)
238  && IsEqual(a.z, b.z, tolerance);
239 }
240 
242 inline Vector Normalize(const Math::Vector &v)
243 {
244  Vector result = v;
245  result.Normalize();
246  return result;
247 }
248 
250 inline float DotProduct(const Math::Vector &left, const Math::Vector &right)
251 {
252  return left.DotMultiply(right);
253 }
254 
256 inline Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
257 {
258  return left.CrossMultiply(right);
259 }
260 
262 inline float Angle(const Math::Vector &a, const Math::Vector &b)
263 {
264  return a.Angle(b);
265 }
266 
268 inline float Distance(const Math::Vector &a, const Math::Vector &b)
269 {
270  return sqrtf( (a.x-b.x)*(a.x-b.x) +
271  (a.y-b.y)*(a.y-b.y) +
272  (a.z-b.z)*(a.z-b.z) );
273 }
274 
276 inline Vector Clamp(const Vector &vec, const Vector &min, const Vector &max)
277 {
278  Vector clamped;
279  clamped.x = Min(Max(min.x, vec.x), max.x);
280  clamped.y = Min(Max(min.y, vec.y), max.y);
281  clamped.z = Min(Max(min.z, vec.z), max.z);
282  return clamped;
283 }
284 
285 
286 } // namespace Math
287 
Vector CrossMultiply(const Vector &right) const
Calculates the cross product with another vector.
Definition: vector.h:116
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:36
friend const Vector operator/(const Vector &left, const float &right)
Divides vector by scalar.
Definition: vector.h:216
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:46
friend const Vector operator*(const float &left, const Vector &right)
Multiplies vector by scalar.
Definition: vector.h:195
float Angle(const Math::Vector &a, const Math::Vector &b)
Convenience function for calculating angle (in radians) between two vectors.
Definition: vector.h:262
friend const Vector operator-(const Vector &left, const Vector &right)
Subtracts two vectors.
Definition: vector.h:180
float * Array()
Returns the struct cast to float* array; use with care!
Definition: vector.h:82
float Max(float a, float b)
Maximum.
Definition: func.h:74
float CosAngle(const Vector &right) const
Returns the cosine of angle between this and another vector.
Definition: vector.h:135
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:189
const Vector & operator+=(const Vector &right)
Adds the given vector.
Definition: vector.h:156
Vector Normalize(const Math::Vector &v)
Convenience function for getting normalized vector.
Definition: vector.h:242
void Normalize()
Normalizes the vector.
Definition: vector.h:100
float x
X - 1st coord.
Definition: vector.h:55
Vector(float _x, float _y, float _z)
Creates a vector from given values.
Definition: vector.h:69
float DotProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating dot product.
Definition: vector.h:250
Vector operator-() const
Returns the inverted vector.
Definition: vector.h:150
float Angle(const Vector &right) const
Returns angle (in radians) between this and another vector.
Definition: vector.h:141
std::string ToString() const
Returns a string "[x, y, z]".
Definition: vector.h:223
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: vector.h:88
Vector Clamp(const Vector &vec, const Vector &min, const Vector &max)
Clamps the vector vec to range between min and max.
Definition: vector.h:276
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:40
friend const Vector operator*(const Vector &left, const float &right)
Multiplies vector by scalar.
Definition: vector.h:201
float DotMultiply(const Vector &right) const
Calculates the dot product with another vector.
Definition: vector.h:129
Namespace for (new) math code.
Definition: const.h:32
float Min(float a, float b)
Minimum.
Definition: func.h:52
const Vector & operator/=(const float &right)
Divides by given scalar.
Definition: vector.h:207
bool VectorsEqual(const Math::Vector &a, const Math::Vector &b, float tolerance=TOLERANCE)
Checks if two vectors are equal within given tolerance.
Definition: vector.h:234
float z
Z - 3rd coord.
Definition: vector.h:59
float Length() const
Returns the vector length.
Definition: vector.h:94
Constants used in math functions.
Vector()
Creates a zero vector (0, 0, 0)
Definition: vector.h:62
Common math functions.
friend const Vector operator+(const Vector &left, const Vector &right)
Adds two vectors.
Definition: vector.h:165
const Vector & operator-=(const Vector &right)
Subtracts the given vector.
Definition: vector.h:171
void LoadZero()
Loads the zero vector (0, 0, 0)
Definition: vector.h:76
Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating cross product.
Definition: vector.h:256
3D (3x1) vector
Definition: vector.h:52
const Vector & operator*=(const float &right)
Multiplies by given scalar.
Definition: vector.h:186
float y
Y - 2nd coord.
Definition: vector.h:57