Colobot
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-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.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 
40 
53 struct Vector
54 {
56  float x;
58  float y;
60  float z;
61 
63  inline Vector()
64  : x(0.0f)
65  , y(0.0f)
66  , z(0.0f)
67  {}
68 
70  inline explicit Vector(float _x, float _y, float _z)
71  : x(_x)
72  , y(_y)
73  , z(_z)
74  {}
75 
77  inline void LoadZero()
78  {
79  x = y = z = 0.0f;
80  }
81 
83  inline float* Array()
84  {
85  return reinterpret_cast<float*>(this);
86  }
87 
89  inline const float* Array() const
90  {
91  return reinterpret_cast<const float*>(this);
92  }
93 
95  inline float Length() const
96  {
97  return sqrtf(x*x + y*y + z*z);
98  }
99 
101  inline void Normalize()
102  {
103  float l = Length();
104  if (IsZero(l))
105  return;
106 
107  x /= l;
108  y /= l;
109  z /= l;
110  }
111 
113 
117  inline Vector CrossMultiply(const Vector &right) const
118  {
119  float px = y * right.z - z * right.y;
120  float py = z * right.x - x * right.z;
121  float pz = x * right.y - y * right.x;
122  return Vector(px, py, pz);
123  }
124 
126 
130  inline float DotMultiply(const Vector &right) const
131  {
132  return x * right.x + y * right.y + z * right.z;
133  }
134 
136  inline float CosAngle(const Vector &right) const
137  {
138  return DotMultiply(right) / (Length() * right.Length());
139  }
140 
142  inline float Angle(const Vector &right) const
143  {
144  return acos(CosAngle(right));
145  }
146 
147 
148  /* Operators */
149 
151  inline Vector operator-() const
152  {
153  return Vector(-x, -y, -z);
154  }
155 
157  inline const Vector& operator+=(const Vector &right)
158  {
159  x += right.x;
160  y += right.y;
161  z += right.z;
162  return *this;
163  }
164 
166  inline friend const Vector operator+(const Vector &left, const Vector &right)
167  {
168  return Vector(left.x + right.x, left.y + right.y, left.z + right.z);
169  }
170 
172  inline const Vector& operator-=(const Vector &right)
173  {
174  x -= right.x;
175  y -= right.y;
176  z -= right.z;
177  return *this;
178  }
179 
181  inline friend const Vector operator-(const Vector &left, const Vector &right)
182  {
183  return Vector(left.x - right.x, left.y - right.y, left.z - right.z);
184  }
185 
187  inline const Vector& operator*=(const float &right)
188  {
189  x *= right;
190  y *= right;
191  z *= right;
192  return *this;
193  }
194 
196  inline friend const Vector operator*(const float &left, const Vector &right)
197  {
198  return Vector(left * right.x, left * right.y, left * right.z);
199  }
200 
202  inline friend const Vector operator*(const Vector &left, const float &right)
203  {
204  return Vector(left.x * right, left.y * right, left.z * right);
205  }
206 
208  inline const Vector& operator/=(const float &right)
209  {
210  x /= right;
211  y /= right;
212  z /= right;
213  return *this;
214  }
215 
217  inline friend const Vector operator/(const Vector &left, const float &right)
218  {
219  return Vector(left.x / right, left.y / right, left.z / right);
220  }
221 
222 
224  inline std::string ToString() const
225  {
226  std::stringstream s;
227  s.precision(3);
228  s << "[" << x << ", " << y << ", " << z << "]";
229  return s.str();
230  }
231 
232 }; // struct Vector
233 
235 inline bool VectorsEqual(const Math::Vector &a, const Math::Vector &b, float tolerance = TOLERANCE)
236 {
237  return IsEqual(a.x, b.x, tolerance)
238  && IsEqual(a.y, b.y, tolerance)
239  && IsEqual(a.z, b.z, tolerance);
240 }
241 
243 inline Vector Normalize(const Math::Vector &v)
244 {
245  Vector result = v;
246  result.Normalize();
247  return result;
248 }
249 
251 inline float DotProduct(const Math::Vector &left, const Math::Vector &right)
252 {
253  return left.DotMultiply(right);
254 }
255 
257 inline Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
258 {
259  return left.CrossMultiply(right);
260 }
261 
263 inline float Angle(const Math::Vector &a, const Math::Vector &b)
264 {
265  return a.Angle(b);
266 }
267 
269 inline float Distance(const Math::Vector &a, const Math::Vector &b)
270 {
271  return sqrtf( (a.x-b.x)*(a.x-b.x) +
272  (a.y-b.y)*(a.y-b.y) +
273  (a.z-b.z)*(a.z-b.z) );
274 }
275 
277 inline Vector Clamp(const Vector &vec, const Vector &min, const Vector &max)
278 {
279  Vector clamped;
280  clamped.x = Min(Max(min.x, vec.x), max.x);
281  clamped.y = Min(Max(min.y, vec.y), max.y);
282  clamped.z = Min(Max(min.z, vec.z), max.z);
283  return clamped;
284 }
285 
286 
287 } // namespace Math
288 
Vector CrossMultiply(const Vector &right) const
Calculates the cross product with another vector.
Definition: vector.h:117
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:37
friend const Vector operator/(const Vector &left, const float &right)
Divides vector by scalar.
Definition: vector.h:217
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:47
friend const Vector operator*(const float &left, const Vector &right)
Multiplies vector by scalar.
Definition: vector.h:196
friend const Vector operator-(const Vector &left, const Vector &right)
Subtracts two vectors.
Definition: vector.h:181
float * Array()
Returns the struct cast to float* array; use with care!
Definition: vector.h:83
float Max(float a, float b)
Maximum.
Definition: func.h:75
float CosAngle(const Vector &right) const
Returns the cosine of angle between this and another vector.
Definition: vector.h:136
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:190
const Vector & operator+=(const Vector &right)
Adds the given vector.
Definition: vector.h:157
void Normalize()
Normalizes the vector.
Definition: vector.h:101
float x
X - 1st coord.
Definition: vector.h:56
Vector(float _x, float _y, float _z)
Creates a vector from given values.
Definition: vector.h:70
float DotProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating dot product.
Definition: vector.h:251
Vector operator-() const
Returns the inverted vector.
Definition: vector.h:151
float Angle(const Vector &right) const
Returns angle (in radians) between this and another vector.
Definition: vector.h:142
std::string ToString() const
Returns a string "[x, y, z]".
Definition: vector.h:224
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: vector.h:89
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:41
friend const Vector operator*(const Vector &left, const float &right)
Multiplies vector by scalar.
Definition: vector.h:202
float DotMultiply(const Vector &right) const
Calculates the dot product with another vector.
Definition: vector.h:130
Namespace for (new) math code.
Definition: device.h:39
float Min(float a, float b)
Minimum.
Definition: func.h:53
const Vector & operator/=(const float &right)
Divides by given scalar.
Definition: vector.h:208
T Clamp(T value, T min, T max)
Clamps the value to a range specified by min and max.
Definition: func.h:98
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:235
float z
Z - 3rd coord.
Definition: vector.h:60
float Length() const
Returns the vector length.
Definition: vector.h:95
Constants used in math functions.
Vector()
Creates a zero vector (0, 0, 0)
Definition: vector.h:63
Common math functions.
friend const Vector operator+(const Vector &left, const Vector &right)
Adds two vectors.
Definition: vector.h:166
const Vector & operator-=(const Vector &right)
Subtracts the given vector.
Definition: vector.h:172
void LoadZero()
Loads the zero vector (0, 0, 0)
Definition: vector.h:77
Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating cross product.
Definition: vector.h:257
3D (3x1) vector
Definition: vector.h:53
const Vector & operator*=(const float &right)
Multiplies by given scalar.
Definition: vector.h:187
float y
Y - 2nd coord.
Definition: vector.h:58