Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

vector.inl

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #define LOOP(OP) for(int i = 0; i < m_d; ++i) OP;
00024 
00025 inline
00026 Vector::Vector(int d) {
00027     assert(d > 0);
00028 
00029     m_d = d;
00030     m_v = new Real[m_d];
00031 }
00032 
00033 inline
00034 Vector::Vector(int d, Real r) {
00035     assert(d > 0);
00036 
00037     m_d = d;
00038     m_v = new Real[m_d];
00039 
00040     LOOP(m_v[i] = r);
00041 }
00042 
00043 inline
00044 Vector::Vector(const Vector &v) {
00045     m_d = v.m_d;
00046     m_v = new Real[m_d];
00047 
00048     LOOP(m_v[i] = v.m_v[i]);
00049 }
00050 
00051 inline
00052 Vector::~Vector() {
00053     delete [] m_v;
00054 }
00055 
00056 inline
00057 Vector &Vector::operator=(Real r) {
00058     LOOP(m_v[i] = r);
00059 
00060     return *this;
00061 }
00062 
00063 inline
00064 Vector &Vector::operator=(const Vector &v) {
00065     assert(m_d == v.m_d);
00066 
00067     LOOP(m_v[i] = v.m_v[i]);
00068 
00069     return *this;
00070 }
00071 
00072 inline
00073 int Vector::GetDimension() const {
00074     return m_d;
00075 }
00076 
00077 inline
00078 Vector Vector::operator+(const Vector &v) const {
00079     assert(m_d == v.m_d);
00080 
00081     Vector res(m_d);
00082 
00083     LOOP(res.m_v[i] = m_v[i] + v.m_v[i]);
00084 
00085     return res;
00086 }
00087 
00088 inline
00089 Vector Vector::operator-(const Vector &v) const {
00090     assert(m_d == v.m_d);
00091 
00092     Vector res(m_d);
00093 
00094     LOOP(res.m_v[i] = m_v[i] - v.m_v[i]);
00095 
00096     return res;
00097 }
00098 
00099 inline
00100 Vector Vector::operator-() const {
00101     Vector res(m_d);
00102 
00103     LOOP(res.m_v[i] = -m_v[i]);
00104 
00105     return res;
00106 }
00107 
00108 inline
00109 Vector Vector::operator*(Real r) const {
00110     Vector res(m_d);
00111 
00112     LOOP(res.m_v[i] = m_v[i] * r);
00113 
00114     return res;
00115 }
00116 
00117 inline
00118 Vector Vector::operator/(Real r) const {
00119     Real inv_r = 1.0 / r;
00120 
00121     Vector res(m_d);
00122 
00123     LOOP(res.m_v[i] = m_v[i] * inv_r);
00124 
00125     return res;
00126 }
00127 
00128 inline
00129 Vector &Vector::operator+=(const Vector &v) {
00130     assert(m_d == v.m_d);
00131 
00132     LOOP(m_v[i] += v.m_v[i]);
00133 
00134     return *this;
00135 }
00136 
00137 inline
00138 Vector &Vector::operator-=(const Vector &v) {
00139     assert(m_d == v.m_d);
00140 
00141     LOOP(m_v[i] -= v.m_v[i]);
00142 
00143     return *this;
00144 }
00145 
00146 inline
00147 Vector &Vector::operator*=(Real r) {
00148     LOOP(m_v[i] *= r);
00149 
00150     return *this;
00151 }
00152 
00153 inline
00154 Vector &Vector::operator/=(Real r) {
00155     Real inv_r = 1.0 / r;
00156 
00157     LOOP(m_v[i] *= inv_r);
00158 
00159     return *this;
00160 }
00161 
00162 inline
00163 Real &Vector::operator[](int i) {
00164     assert(i >= 0);
00165     assert(i < m_d);
00166 
00167     return m_v[i];
00168 }
00169 
00170 inline
00171 const Real &Vector::operator[](int i) const {
00172     assert(i >= 0);
00173     assert(i < m_d);
00174 
00175     return m_v[i];
00176 }
00177 
00178 inline
00179 Real *Vector::GetData() {
00180     return m_v;
00181 }
00182 
00183 inline
00184 Real Vector::SquareNorm() const {
00185     Real res = 0.0;
00186 
00187     LOOP(res += m_v[i] * m_v[i]);
00188 
00189     return res;
00190 }
00191 
00192 inline
00193 Real Vector::Norm() const {
00194     return sqrt(SquareNorm());
00195 }
00196 
00197 inline
00198 Vector Vector::Normalized() const {
00199     const Real n = Norm();
00200 
00201     if(n == 0.0) {
00202         Vector res(m_d);
00203         res.m_v[0] = 1.0;
00204         for(int i = 1; i < m_d; ++i)
00205             res.m_v[i] = 0.0;
00206         return res;
00207     } else return *this / n;
00208 }
00209 
00210 inline
00211 void Vector::Normalize() {
00212     const Real n = Norm();
00213 
00214     if(n == 0.0) {
00215         m_v[0] = 1.0;
00216         for(int i = 1; i < m_d; ++i)
00217             m_v[i] = 0.0;
00218     } else {
00219         const Real inv_n = 1.0 / n;
00220         LOOP(m_v[i] *= inv_n);
00221     }
00222 }
00223 
00224 inline
00225 bool Vector::IsUnitLength(Real e /*= EPS*/) const {
00226     return feq(SquareNorm(), 1.0, e);
00227 }
00228 
00229 inline
00230 Vector operator*(Real r, const Vector &v) {
00231     return v * r;
00232 }
00233 
00234 #undef LOOP

Generated on Tue May 11 01:31:52 2004 for toxic by doxygen 1.3.6