00001 #ifndef _VECTOR_3D_H_
00002 #define _VECTOR_3D_H_
00003
00004 #include <cassert>
00005 #include <cmath>
00006 #include "../utils/fv_assert.h"
00007 #include "Point3D.h"
00008 namespace FemViewer {
00009
00010 class Vec3D {
00011 private:
00012 float x[3];
00013 public:
00014 Vec3D() { x[0] = x[1] = x[2] = 0.0f; }
00015 ~Vec3D(){}
00016 Vec3D(float v_) { x[0] = x[1] = x[2] = v_; }
00017 Vec3D(float x_, float y_, float z_ = 0.0f) {
00018 x[0] = x_; x[1] = y_, x[2] = z_; }
00019 Vec3D(const float *x_) {
00020 x[0] = x_[0]; x[1] = x_[1]; x[2] = x_[2]; }
00021 Vec3D(const Vec3D& vec_){
00022 x[0] = vec_.x[0]; x[1] = vec_.x[1]; x[2] = vec_.x[2]; }
00023 template<class T>
00024 Vec3D(const Point3D<T>& p)
00025 {
00026 x[0] = (float)p.x;
00027 x[1] = (float)p.y;
00028 x[2] = (float)p.z;
00029 }
00030 inline float operator [](int idx) const {
00031 assert((idx>=0) && (idx<3));
00032 return x[idx];
00033 }
00034
00035 inline float& operator [](int idx){
00036 assert((idx>=0) && (idx<3));
00037 return x[idx];
00038 }
00039
00040 inline const float* coord()const { return &x[0]; }
00041 inline void Set(float _x, float _y, float _z)
00042 { x[0] = _x; x[1] = _y; x[2] = _z; }
00043 inline float _x()const { return x[0]; }
00044 inline float _y()const { return x[1]; }
00045 inline float _z()const { return x[2]; }
00046 inline bool IsZero() const;
00047
00048 Vec3D& operator =(const Vec3D& vec_);
00049 Vec3D& operator +=(const Vec3D& vec_);
00050 Vec3D& operator -=(const Vec3D& vec_);
00051 Vec3D& operator *=(const float v_);
00052 Vec3D& operator /=(const float v_);
00053 Vec3D& operator +(const float v_);
00054 Vec3D& operator -(const float v_);
00055 Vec3D& operator %=(const Vec3D& vec_);
00056
00057 Vec3D getOXY() const;
00058 Vec3D getOYZ() const;
00059 Vec3D getOZX() const;
00060
00061 Vec3D operator +(const Vec3D& vec_) const;
00062 Vec3D operator -(const Vec3D& vec_) const;
00063 Vec3D operator -() const;
00064 float getDistance(const Vec3D& vec) const;
00065 float getAngle(const Vec3D& vec) const;
00066
00067 friend Vec3D operator *(const Vec3D& vec_, const float rhv_);
00068 friend Vec3D operator *(const float lhv_, const Vec3D& vec_);
00069 friend Vec3D operator /(const Vec3D& vec_, const float rhv_);
00070 friend Vec3D operator %(const Vec3D& vec_1, const Vec3D& vec_2);
00071 friend float dotProd(const Vec3D& vec_1, const Vec3D& vec_2);
00072 friend Vec3D operator -(const Vec3D& vec_);
00073 friend bool operator ==(const Vec3D& vec_1, const Vec3D& vec_2);
00074 friend bool operator !=(const Vec3D& vec_1, const Vec3D& vec_2);
00075 friend float getAngle(const Vec3D& vec_1, const Vec3D& vec_2);
00076
00077 float quadNorm() const;
00078 float Norm() const;
00079 void normalize();
00080
00081 static Vec3D mini(const Vec3D& vec_1, const Vec3D& vec_2);
00082 static Vec3D maxi(const Vec3D& vec_1, const Vec3D& vec_2);
00083 void out(const char* vname = "") const;
00084
00085
00086 };
00087
00088 inline bool Vec3D::IsZero() const
00089 {
00090 return( this->Norm() == 0.0f);
00091
00092 }
00093 inline Vec3D& Vec3D::operator =(const Vec3D& vec_)
00094 {
00095 x[0] = vec_.x[0];
00096 x[1] = vec_.x[1];
00097 x[2] = vec_.x[2];
00098 return(*this);
00099 }
00100
00101 inline Vec3D& Vec3D::operator +=(const Vec3D& vec_)
00102 {
00103 x[0] += vec_.x[0]; x[1] += vec_.x[1]; x[2] += vec_.x[2];
00104 return (*this);
00105 }
00106
00107 inline Vec3D& Vec3D::operator -=(const Vec3D& vec_)
00108 {
00109 x[0] -= vec_.x[0]; x[1] -= vec_.x[1]; x[2] -= vec_.x[2];
00110 return (*this);
00111 }
00112
00113 inline Vec3D& Vec3D::operator *=(const float v_)
00114 {
00115 x[0] *= v_;
00116 x[1] *= v_;
00117 x[2] *= v_;
00118 return(*this);
00119 }
00120
00121 inline Vec3D& Vec3D::operator /=(const float v_)
00122 {
00123 x[0] /= v_;
00124 x[1] /= v_;
00125 x[2] /= v_;
00126 return(*this);
00127 }
00128
00129 inline Vec3D& Vec3D::operator +(const float v_)
00130 {
00131 x[0] += v_;
00132 x[1] += v_;
00133 x[2] += v_;
00134 return (*this);
00135 }
00136
00137 inline Vec3D& Vec3D::operator -(const float v_)
00138 {
00139 return this->operator+(-v_);
00140 }
00141
00142 inline Vec3D& Vec3D::operator %=(const Vec3D& vec_)
00143 {
00144 float tmp_xyz[3];
00145 tmp_xyz[0] = x[0]; tmp_xyz[1] = x[1]; tmp_xyz[2] = x[2];
00146 x[0] = tmp_xyz[1]*vec_.x[2] - tmp_xyz[2]*vec_.x[1];
00147 x[1] = tmp_xyz[2]*vec_.x[0] - tmp_xyz[0]*vec_.x[2];
00148 x[2] = tmp_xyz[0]*vec_.x[1] - tmp_xyz[1]*vec_.x[0];
00149 return(*this);
00150 }
00151
00152 inline Vec3D Vec3D::getOXY() const
00153 {
00154 return Vec3D(x[0],x[1],0.0f);
00155 }
00156
00157 inline Vec3D Vec3D::getOYZ() const
00158 {
00159 return Vec3D(0.0f,x[1],x[2]);
00160 }
00161
00162 inline Vec3D Vec3D::getOZX() const
00163 {
00164 return Vec3D(x[0],0.0f,x[2]);
00165 }
00166
00167 inline Vec3D Vec3D::operator +(const Vec3D& vec_) const
00168 {
00169 return Vec3D(x[0]+vec_.x[0],x[1]+vec_.x[1],x[2]+vec_.x[2]);
00170 }
00171
00172 inline Vec3D Vec3D::operator -(const Vec3D& vec_) const
00173 {
00174 return Vec3D(x[0]-vec_.x[0],x[1]-vec_.x[1],x[2]-vec_.x[2]);
00175 }
00176
00177 inline Vec3D Vec3D::operator -() const
00178 {
00179 return Vec3D(-x[0], -x[1], -x[2]);
00180 }
00181
00182
00183 inline float Vec3D::getDistance(const Vec3D& vec) const
00184 {
00185 return (*this - vec).Norm();
00186 }
00187
00188 inline float Vec3D::getAngle(const Vec3D& vec) const
00189 {
00190
00191
00192 float dot = dotProd(*this,vec);
00193 return acos(dot/(Norm()*vec.Norm()));
00194
00195 }
00196
00197
00198 inline Vec3D operator *(const Vec3D& vec_, const float rhv_)
00199 {
00200 return Vec3D(vec_.x[0]*rhv_,vec_.x[1]*rhv_,vec_.x[2]*rhv_);
00201 }
00202
00203 inline Vec3D operator *(const float lhv_, const Vec3D& vec_)
00204 {
00205 return Vec3D(vec_.x[0]*lhv_,vec_.x[1]*lhv_,vec_.x[2]*lhv_);
00206 }
00207
00208 inline Vec3D operator /(const Vec3D& vec_, const float rhv_)
00209 {
00210 return Vec3D(vec_.x[0]/rhv_,vec_.x[1]/rhv_,vec_.x[2]/rhv_);
00211 }
00212
00213 inline Vec3D operator %(const Vec3D& vec_1, const Vec3D& vec_2)
00214 {
00215
00216 return Vec3D(vec_1.x[1]*vec_2.x[2]-vec_1.x[2]*vec_2.x[1],
00217 vec_1.x[2]*vec_2.x[0]-vec_1.x[0]*vec_2.x[2],
00218 vec_1.x[0]*vec_2.x[1]-vec_1.x[1]*vec_2.x[0]);
00219 }
00220
00221 inline float dotProd(const Vec3D &vec_1, const Vec3D &vec_2)
00222 {
00223 return(vec_1.x[0]*vec_2.x[0] + vec_1.x[1]*vec_2.x[1] + vec_1.x[2]*vec_2.x[2]);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 }
00236
00237 #endif