00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 inline
00024 Vector3::Vector3() {}
00025
00026 inline
00027 Vector3::Vector3(Real r) :
00028 m_x(r),
00029 m_y(r),
00030 m_z(r) {}
00031
00032 inline
00033 Vector3::Vector3(const Vector3 &v) :
00034 m_x(v.m_x),
00035 m_y(v.m_y),
00036 m_z(v.m_z) {}
00037
00038 inline
00039 Vector3::Vector3(Real x, Real y, Real z) :
00040 m_x(x),
00041 m_y(y),
00042 m_z(z) {}
00043
00044 inline
00045 Vector3 &Vector3::operator=(Real rhs) {
00046 m_x = m_y = m_z = rhs;
00047
00048 return *this;
00049 }
00050
00051 inline
00052 Vector3 &Vector3::operator=(const Vector3 &rhs) {
00053 m_x = rhs.m_x;
00054 m_y = rhs.m_y;
00055 m_z = rhs.m_z;
00056
00057 return *this;
00058 }
00059
00060 inline
00061 Real &Vector3::operator[](int i) {
00062 assert(i >= 0 && i < 3);
00063
00064 return *(&m_x + i);
00065 }
00066
00067 inline
00068 Real Vector3::SquareNorm() const {
00069 return m_x * m_x + m_y * m_y + m_z * m_z;
00070 }
00071
00072 inline
00073 Real Vector3::Norm() const {
00074 return sqrt(SquareNorm());
00075 }
00076
00077 inline
00078 Vector3 Vector3::Normalized() const {
00079 const Real n = Norm();
00080 return n == 0.0 ? Vector3(1.0, 0.0, 0.0) : *this / n;
00081 }
00082
00083 inline
00084 void Vector3::Normalize() {
00085 const Real n = Norm();
00086
00087 if(n == 0.0) {
00088 m_x = 1.0;
00089 m_y = m_z = 0.0;
00090 } else {
00091 const Real inv_n = 1.0 / n;
00092 m_x *= inv_n;
00093 m_y *= inv_n;
00094 m_z *= inv_n;
00095 }
00096 }
00097
00098 inline
00099 bool Vector3::IsUnitLength(Real e ) const {
00100 return feq(SquareNorm(), 1.0, e);
00101 }
00102
00103 inline
00104 Vector3 operator+(const Vector3 &lhs, const Vector3 &rhs) {
00105 return Vector3(
00106 lhs.m_x + rhs.m_x,
00107 lhs.m_y + rhs.m_y,
00108 lhs.m_z + rhs.m_z);
00109 }
00110
00111 inline
00112 Vector3 operator-(const Vector3 &lhs, const Vector3 &rhs) {
00113 return Vector3(
00114 lhs.m_x - rhs.m_x,
00115 lhs.m_y - rhs.m_y,
00116 lhs.m_z - rhs.m_z);
00117 }
00118
00119 inline
00120 Vector3 operator-(const Vector3 &v) {
00121 return Vector3(-v.m_x, -v.m_y, -v.m_z);
00122 }
00123
00124 inline
00125 Vector3 operator*(const Vector3 &lhs, Real rhs) {
00126 return Vector3(
00127 lhs.m_x * rhs,
00128 lhs.m_y * rhs,
00129 lhs.m_z * rhs);
00130 }
00131
00132 inline
00133 Vector3 operator*(Real lhs, const Vector3 &rhs) {
00134 return Vector3(
00135 lhs * rhs.m_x,
00136 lhs * rhs.m_y,
00137 lhs * rhs.m_z);
00138 }
00139
00140 inline
00141 Vector3 operator/(const Vector3 &lhs, Real rhs) {
00142 const Real inv_rhs = 1.0 / rhs;
00143
00144 return Vector3(
00145 lhs.m_x * inv_rhs,
00146 lhs.m_y * inv_rhs,
00147 lhs.m_z * inv_rhs);
00148 }
00149
00150 inline
00151 Vector3 &operator+=(Vector3 &lhs, const Vector3 &rhs) {
00152 lhs.m_x += rhs.m_x;
00153 lhs.m_y += rhs.m_y;
00154 lhs.m_z += rhs.m_z;
00155
00156 return lhs;
00157 }
00158
00159 inline
00160 Vector3 &operator-=(Vector3 &lhs, const Vector3 &rhs) {
00161 lhs.m_x -= rhs.m_x;
00162 lhs.m_y -= rhs.m_y;
00163 lhs.m_z -= rhs.m_z;
00164
00165 return lhs;
00166 }
00167
00168 inline
00169 Vector3 &operator*=(Vector3 &lhs, Real rhs) {
00170 lhs.m_x *= rhs;
00171 lhs.m_y *= rhs;
00172 lhs.m_z *= rhs;
00173
00174 return lhs;
00175 }
00176
00177 inline
00178 Vector3 &operator/=(Vector3 &lhs, Real rhs) {
00179 const Real inv_rhs = 1.0 / rhs;
00180
00181 lhs.m_x *= inv_rhs;
00182 lhs.m_y *= inv_rhs;
00183 lhs.m_z *= inv_rhs;
00184
00185 return lhs;
00186 }
00187
00188 inline
00189 Real operator*(const Vector3 &lhs, const Vector3 &rhs) {
00190 return
00191 lhs.m_x * rhs.m_x +
00192 lhs.m_y * rhs.m_y +
00193 lhs.m_z * rhs.m_z;
00194 }
00195
00196 inline
00197 Vector3 operator^(const Vector3 &lhs, const Vector3 &rhs) {
00198 return Vector3(
00199 lhs.m_y * rhs.m_z - rhs.m_y * lhs.m_z,
00200 lhs.m_z * rhs.m_x - rhs.m_z * lhs.m_x,
00201 lhs.m_x * rhs.m_y - rhs.m_x * lhs.m_y);
00202 }
00203
00204 inline
00205 bool feq(const Vector3 &a, const Vector3 &b, Real e ) {
00206 return
00207 feq(a.m_x, b.m_x, e) &&
00208 feq(a.m_y, b.m_y, e) &&
00209 feq(a.m_z, b.m_z, e);
00210 }
00211
00212 inline
00213 bool fneq(const Vector3 &a, const Vector3 &b, Real e ) {
00214 return
00215 fneq(a.m_x, b.m_x, e) ||
00216 fneq(a.m_y, b.m_y, e) ||
00217 fneq(a.m_z, b.m_z, e);
00218 }
00219
00220 inline
00221 bool fz(const Vector3 &a, Real e ) {
00222 return
00223 fz(a.m_x, e) &&
00224 fz(a.m_y, e) &&
00225 fz(a.m_z, e);
00226 }
00227
00228 inline
00229 bool fnz(const Vector3 &a, Real e ) {
00230 return
00231 fnz(a.m_x, e) ||
00232 fnz(a.m_y, e) ||
00233 fnz(a.m_z, e);
00234 }