Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef OCTOMATH_VECTOR3_H
00035 #define OCTOMATH_VECTOR3_H
00036
00037 #include <iostream>
00038 #include <math.h>
00039
00040
00041 namespace octomath {
00042
00050 class Vector3 {
00051 public:
00052
00056 Vector3 () { data[0] = data[1] = data[2] = 0.0; }
00057
00063 Vector3 (const Vector3& other) {
00064 data[0] = other(0);
00065 data[1] = other(1);
00066 data[2] = other(2);
00067 }
00068
00075 Vector3 (float x, float y, float z) {
00076 data[0] = x;
00077 data[1] = y;
00078 data[2] = z;
00079 }
00080
00081
00082
00083
00084
00085
00091 inline Vector3& operator= (const Vector3& other) {
00092 data[0] = other(0);
00093 data[1] = other(1);
00094 data[2] = other(2);
00095 return *this;
00096 }
00097
00098
00107 inline Vector3 cross (const Vector3& other) const
00108 {
00109
00110
00111 return Vector3(y()*other.z() - z()*other.y(),
00112 z()*other.x() - x()*other.z(),
00113 x()*other.y() - y()*other.x());
00114 }
00115
00117 inline double dot (const Vector3& other) const
00118 {
00119 return x()*other.x() + y()*other.y() + z()*other.z();
00120 }
00121
00122 inline const float& operator() (unsigned int i) const
00123 {
00124 return data[i];
00125 }
00126 inline float& operator() (unsigned int i)
00127 {
00128 return data[i];
00129 }
00130
00131 inline float& x()
00132 {
00133 return operator()(0);
00134 }
00135
00136 inline float& y()
00137 {
00138 return operator()(1);
00139 }
00140
00141 inline float& z()
00142 {
00143 return operator()(2);
00144 }
00145
00146 inline const float& x() const
00147 {
00148 return operator()(0);
00149 }
00150
00151 inline const float& y() const
00152 {
00153 return operator()(1);
00154 }
00155
00156 inline const float& z() const
00157 {
00158 return operator()(2);
00159 }
00160
00161 inline float& roll()
00162 {
00163 return operator()(0);
00164 }
00165
00166 inline float& pitch()
00167 {
00168 return operator()(1);
00169 }
00170
00171 inline float& yaw()
00172 {
00173 return operator()(2);
00174 }
00175
00176 inline const float& roll() const
00177 {
00178 return operator()(0);
00179 }
00180
00181 inline const float& pitch() const
00182 {
00183 return operator()(1);
00184 }
00185
00186 inline const float& yaw() const
00187 {
00188 return operator()(2);
00189 }
00190
00191 inline Vector3 operator- () const
00192 {
00193 Vector3 result;
00194 result(0) = -data[0];
00195 result(1) = -data[1];
00196 result(2) = -data[2];
00197 return result;
00198 }
00199
00200 inline Vector3 operator+ (const Vector3 &other) const
00201 {
00202 Vector3 result(*this);
00203 result(0) += other(0);
00204 result(1) += other(1);
00205 result(2) += other(2);
00206 return result;
00207 }
00208
00209 inline Vector3 operator* (float x) const {
00210 Vector3 result(*this);
00211 result(0) *= x;
00212 result(1) *= x;
00213 result(2) *= x;
00214 return result;
00215 }
00216
00217 inline Vector3 operator- (const Vector3 &other) const
00218 {
00219 Vector3 result(*this);
00220 result(0) -= other(0);
00221 result(1) -= other(1);
00222 result(2) -= other(2);
00223 return result;
00224 }
00225
00226 inline void operator+= (const Vector3 &other)
00227 {
00228 data[0] += other(0);
00229 data[1] += other(1);
00230 data[2] += other(2);
00231 }
00232
00233 inline void operator-= (const Vector3& other) {
00234 data[0] -= other(0);
00235 data[1] -= other(1);
00236 data[2] -= other(2);
00237 }
00238
00239 inline void operator/= (float x) {
00240 data[0] /= x;
00241 data[1] /= x;
00242 data[2] /= x;
00243 }
00244
00245 inline void operator*= (float x) {
00246 data[0] *= x;
00247 data[1] *= x;
00248 data[2] *= x;
00249 }
00250
00251 inline bool operator== (const Vector3 &other) const {
00252 for (unsigned int i=0; i<3; i++) {
00253 if (operator()(i) != other(i))
00254 return false;
00255 }
00256 return true;
00257 }
00258
00260 inline double norm () const {
00261 return sqrt(norm_sq());
00262 }
00263
00265 inline double norm_sq() const {
00266 return (x()*x() + y()*y() + z()*z());
00267 }
00268
00270 inline Vector3& normalize () {
00271 double len = norm();
00272 if (len > 0)
00273 *this /= (float) len;
00274 return *this;
00275 }
00276
00278 inline Vector3 normalized () const {
00279 Vector3 result(*this);
00280 result.normalize ();
00281 return result;
00282 }
00283
00284 inline double angleTo(const Vector3& other) const {
00285 double dot_prod = this->dot(other);
00286 double len1 = this->norm();
00287 double len2 = other.norm();
00288 return acos(dot_prod / (len1*len2));
00289 }
00290
00291
00292 inline double distance (const Vector3& other) const {
00293 double dist_x = x() - other.x();
00294 double dist_y = y() - other.y();
00295 double dist_z = z() - other.z();
00296 return sqrt(dist_x*dist_x + dist_y*dist_y + dist_z*dist_z);
00297 }
00298
00299 inline double distanceXY (const Vector3& other) const {
00300 double dist_x = x() - other.x();
00301 double dist_y = y() - other.y();
00302 return sqrt(dist_x*dist_x + dist_y*dist_y);
00303 }
00304
00305 Vector3& rotate_IP (double roll, double pitch, double yaw);
00306
00307
00308 std::istream& read(std::istream &s);
00309 std::ostream& write(std::ostream &s) const;
00310 std::istream& readBinary(std::istream &s);
00311 std::ostream& writeBinary(std::ostream &s) const;
00312
00313
00314 protected:
00315 float data[3];
00316
00317 };
00318
00319
00321 std::ostream& operator<<(std::ostream& out, octomath::Vector3 const& v);
00322
00323 }
00324
00325
00326 #endif