SuperTuxKart
vec3.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2011-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 #ifndef HEADER_VEC3_HPP
21 #define HEADER_VEC3_HPP
22 
23 #include <vector3d.h>
24 #include <vector2d.h>
25 using namespace irr;
26 
27 #include "LinearMath/btVector3.h"
28 #include "LinearMath/btMatrix3x3.h"
29 
30 #include "utils/constants.hpp"
31 
34 class Vec3 : public btVector3
35 {
36 private:
37  void setPitchRoll(const Vec3 &normal);
38 
39 public:
48  inline Vec3(const core::vector3df &v) : btVector3(v.X, v.Y, v.Z) {}
49  // ------------------------------------------------------------------------
51  inline Vec3(const btVector3& a) : btVector3(a) {}
52  // ------------------------------------------------------------------------
54  inline Vec3() : btVector3(0, 0, 0) {}
55  // ------------------------------------------------------------------------
57  inline Vec3(float x, float y, float z) : btVector3(x,y,z) {}
58  // ------------------------------------------------------------------------
60  inline Vec3(float x, float y, float z, float w) : btVector3(x,y,z)
61  { setW(w); }
62  // ------------------------------------------------------------------------
65  inline Vec3(float x) : btVector3(x,x,x) {}
66  // ------------------------------------------------------------------------
72  inline Vec3(float heading, const Vec3& normal)
73  {
74  setHeading(heading);
75  setPitchRoll(normal);
76  } // Vec3(heading, normal)
77 
78  // ------------------------------------------------------------------------
81  void setHPR(const btQuaternion& q);
82  // ------------------------------------------------------------------------
84  inline const float& operator[](int n) const { return m_floats[n]; }
85  // ------------------------------------------------------------------------
87  inline float& operator[](int n) { return m_floats[n]; }
88  // ------------------------------------------------------------------------
90  inline const float getHeading() const { return m_floats[1]; }
91  // ------------------------------------------------------------------------
93  inline const float getPitch() const { return m_floats[0]; }
94  // ------------------------------------------------------------------------
96  inline const float getRoll() const { return m_floats[2]; }
97  // ------------------------------------------------------------------------
100  inline const float getW() const { return m_floats[3]; }
101  // ------------------------------------------------------------------------
103  inline const void setHeading(float f) { m_floats[1] = f; }
104  // ------------------------------------------------------------------------
106  inline const void setPitch(float f) { m_floats[0] = f; }
107  // ------------------------------------------------------------------------
109  inline const void setRoll(float f) { m_floats[2] = f; }
110  // ------------------------------------------------------------------------
113  const core::vector3df& toIrrVector() const
114  {
115  return (const core::vector3df&)*this;
116  } // toIrrVector
117  // ------------------------------------------------------------------------
119  const core::vector3df toIrrHPR() const
120  {
121  return core::vector3df(RAD_TO_DEGREE*(getX()), // pitch
122  RAD_TO_DEGREE*(getY()), // heading
123  RAD_TO_DEGREE*(getZ()) ); // roll
124  } // toIrrHPR
125  // ------------------------------------------------------------------------
127  const core::vector2df toIrrVector2d() const
128  {
129  return core::vector2df(m_floats[0], m_floats[2]);
130  } // toIrrVector2d
131  // ------------------------------------------------------------------------
133  void degreeToRad()
134  {
135  m_floats[0]*=DEGREE_TO_RAD;
136  m_floats[1]*=DEGREE_TO_RAD;
137  m_floats[2]*=DEGREE_TO_RAD;
138  } // degreeToRad
139  // ------------------------------------------------------------------------
141  Vec3& operator=(const btVector3& a) {*(btVector3*)this=a; return *this;}
142  // ------------------------------------------------------------------------
144  Vec3& operator=(const btQuaternion& q) {setHPR(q); return *this;}
145 
146  // ------------------------------------------------------------------------
149  bool operator==(const Vec3& other) const
150  {
151  return ((m_floats[2]==other.m_floats[2]) &&
152  (m_floats[1]==other.m_floats[1]) &&
153  (m_floats[0]==other.m_floats[0]) );
154  }
155 
156  // ------------------------------------------------------------------------
159  bool operator!=(const Vec3& other) const
160  {
161  return ((m_floats[2]!=other.m_floats[2]) ||
162  (m_floats[1]!=other.m_floats[1]) ||
163  (m_floats[0]!=other.m_floats[0]) );
164  }
165 
166  // ------------------------------------------------------------------------
168  Vec3 operator-(const Vec3& v1) const {return (Vec3)(*(btVector3*)this
169  -(btVector3)v1); }
170  // ------------------------------------------------------------------------
174  Vec3 operator-(const btVector3 v1) const
175  {
176  return *(btVector3*)this - v1;
177  }
178  // ------------------------------------------------------------------------
181  float length2_2d() const { return m_floats[0]*m_floats[0]
182  + m_floats[2]*m_floats[2]; }
183  // ------------------------------------------------------------------------
186  // ------------------------------------------------------------------------
188  float length_2d() const {return sqrtf( m_floats[0]*m_floats[0]
189  + m_floats[2]*m_floats[2]);}
190  // ------------------------------------------------------------------------
193  void max(const Vec3& a) {if(a.getX()>m_floats[0]) m_floats[0]=a.getX();
194  if(a.getY()>m_floats[1]) m_floats[1]=a.getY();
195  if(a.getZ()>m_floats[2]) m_floats[2]=a.getZ();}
196  // ------------------------------------------------------------------------
199  void min(const Vec3& a) {if(a.getX()<m_floats[0]) m_floats[0]=a.getX();
200  if(a.getY()<m_floats[1]) m_floats[1]=a.getY();
201  if(a.getZ()<m_floats[2]) m_floats[2]=a.getZ();}
202  // ------------------------------------------------------------------------
209  float sideOfLine2D(const Vec3& start, const Vec3& end) const
210  {
211  return (end.getX()-start.getX())*(m_floats[2]-start.getZ()) -
212  (end.getZ()-start.getZ())*(m_floats[0]-start.getX());
213 
214  } // sideOfLine2D
215 
216  float sideofPlane(const Vec3& x1, const Vec3& x2, const Vec3& x3) const
217  {
218  return ((x2 - x1).cross(x3 - x1)).dot(*this - x1);
219  } // sideOfPlane
220 }; // Vec3
221 
222 #endif
Vec3::Vec3
Vec3(const core::vector3df &v)
Convert an irrlicht vector3df into the internal (bullet) format.
Definition: vec3.hpp:48
Vec3::Vec3
Vec3(float x)
Initialises a 3d vector from one scalar value, which is used to initialise all components.
Definition: vec3.hpp:65
Vec3::operator-
Vec3 operator-(const btVector3 v1) const
Computes this = this - v1.
Definition: vec3.hpp:174
Vec3::Vec3
Vec3()
Empty constructor.
Definition: vec3.hpp:54
Vec3::getPitch
const float getPitch() const
Returns the pitch of a vector that is used to store a rotation.
Definition: vec3.hpp:93
Vec3::operator[]
float & operator[](int n)
Returns a reference to the n-th element (x=0, y=1, z=2, w=3).
Definition: vec3.hpp:87
Vec3::operator-
Vec3 operator-(const Vec3 &v1) const
Computes this = this - v1.
Definition: vec3.hpp:168
Vec3::operator!=
bool operator!=(const Vec3 &other) const
Operator!= of btQuadWord also compares m_floats[3], which is not useful (and wrong in certain circums...
Definition: vec3.hpp:159
Vec3::toIrrVector
const core::vector3df & toIrrVector() const
Converts a vec3 into an irrlicht vector (which is a simple type cast).
Definition: vec3.hpp:113
Vec3::getRoll
const float getRoll() const
Returns the roll of a vector that is used to store a rotation.
Definition: vec3.hpp:96
Vec3::setPitch
const void setPitch(float f)
Sets the pitch of a vector that is used to store a rotation.
Definition: vec3.hpp:106
Vec3::Vec3
Vec3(float heading, const Vec3 &normal)
Sets the heading, and computes pitch and roll dependent on the normal it is displayed on.
Definition: vec3.hpp:72
Vec3::operator=
Vec3 & operator=(const btVector3 &a)
Sets this = a.
Definition: vec3.hpp:141
Vec3::length_2d
float length_2d() const
Returns the length of this vector in the plane, i.e.
Definition: vec3.hpp:188
Vec3::toIrrVector2d
const core::vector2df toIrrVector2d() const
Returns the X and Z component as an irrlicht 2d vector.
Definition: vec3.hpp:127
Vec3::operator=
Vec3 & operator=(const btQuaternion &q)
Sets the rotation given by the quaternion as HPR vector.
Definition: vec3.hpp:144
Vec3::min
void min(const Vec3 &a)
Sets this = min(this, a) componentwise.
Definition: vec3.hpp:199
Vec3::getHeading
const float getHeading() const
Returns the heading of a vector that is used to store a rotation.
Definition: vec3.hpp:90
Vec3::Vec3
Vec3(float x, float y, float z)
Creates a 3d vector from three scalars.
Definition: vec3.hpp:57
Vec3::Vec3
Vec3(float x, float y, float z, float w)
Creates a 3d vector from three scalars.
Definition: vec3.hpp:60
Vec3::Vec3
Vec3(const btVector3 &a)
Initialises a vector from a btVector3 (or a Vec3).
Definition: vec3.hpp:51
Vec3::operator==
bool operator==(const Vec3 &other) const
Operator== of btQuadWord also compares m_floats[3], which is not useful (and wrong in certain circums...
Definition: vec3.hpp:149
Vec3::operator[]
const float & operator[](int n) const
Returns a reference to the n-th element (x=0, y=1, z=2, w=3).
Definition: vec3.hpp:84
Vec3::setRoll
const void setRoll(float f)
Sets the roll of a vector that is used to store a rotation.
Definition: vec3.hpp:109
Vec3::max
void max(const Vec3 &a)
Sets this = max(this, a) componentwise.
Definition: vec3.hpp:193
Vec3
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
Vec3::length2_2d
float length2_2d() const
Helper functions to treat this vec3 as a 2d vector.
Definition: vec3.hpp:181
Vec3::sideOfLine2D
float sideOfLine2D(const Vec3 &start, const Vec3 &end) const
Determines which side of a line this point is.
Definition: vec3.hpp:209
Vec3::getW
const float getW() const
Returns the W component (bullet vectors contain 4 elements, the last element is usually unused).
Definition: vec3.hpp:100
Vec3::degreeToRad
void degreeToRad()
Converts degree values stored in this vec3 to radians.
Definition: vec3.hpp:133
Vec3::toIrrHPR
const core::vector3df toIrrHPR() const
Converts a bullet HPR value into an irrlicht HPR value.
Definition: vec3.hpp:119
Vec3::setHeading
const void setHeading(float f)
Sets the heading of a vector that is used to store a rotation.
Definition: vec3.hpp:103