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

matrix.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 //!\todo Optimize by precomputing and storing m_r * m_c in the matrix.
00024 #define LOOP(OP) for(int i = 0; i < m_r * m_c; i++) OP;
00025 
00026 inline
00027 Matrix::Matrix(int rows, int columns) {
00028     assert(rows > 0);
00029     assert(columns > 0);
00030 
00031     m_r = rows;
00032     m_c = columns;
00033 
00034     m_v = new Real[m_r * m_c];
00035 }
00036 
00037 inline
00038 Matrix::Matrix(int rows, int columns, Real r) {
00039     assert(rows > 0);
00040     assert(columns > 0);
00041 
00042     m_r = rows;
00043     m_c = columns;
00044 
00045     m_v = new Real[m_r * m_c];
00046 
00047     LOOP(m_v[i] = r);
00048 }
00049 
00050 inline
00051 Matrix::Matrix(const Matrix &m) {
00052     m_r = m.m_r;
00053     m_c = m.m_c;
00054 
00055     m_v = new Real[m_r * m_c];
00056 
00057     LOOP(m_v[i] = m.m_v[i]);
00058 }
00059 
00060 inline
00061 Matrix::~Matrix() {
00062     delete [] m_v;
00063 }
00064 
00065 inline
00066 Matrix Matrix::Identity(int n) {
00067     Matrix res(n, n, 0);
00068 
00069     Real *v = res.m_v;
00070 
00071     for(int i = 0; i < n; i++, v += n + 1)
00072         *v = 1;
00073 
00074     return res;
00075 }
00076 
00077 inline
00078 Matrix &Matrix::operator=(Real r) {
00079     LOOP(m_v[i] = r);
00080 
00081     return *this;
00082 }
00083 
00084 inline
00085 Matrix &Matrix::operator=(const Matrix &m) {
00086     assert(m_r == m.m_r);
00087     assert(m_c == m.m_c);
00088 
00089     LOOP(m_v[i] = m.m_v[i]);
00090 
00091     return *this;
00092 }
00093 
00094 inline
00095 int Matrix::GetRows() const {
00096     return m_r;
00097 }
00098 
00099 inline
00100 int Matrix::GetColumns() const {
00101     return m_c;
00102 }
00103 
00104 inline
00105 bool Matrix::IsSquare() const {
00106     return m_r == m_c;
00107 }
00108 
00109 inline
00110 Matrix Matrix::operator+(const Matrix &m) const {
00111     assert(m_r == m.m_r);
00112     assert(m_c == m.m_c);
00113 
00114     Matrix res(m_r, m_c);
00115 
00116     LOOP(res.m_v[i] = m_v[i] + m.m_v[i]);
00117 
00118     return res;
00119 }
00120 
00121 inline
00122 Matrix Matrix::operator-(const Matrix &m) const {
00123     assert(m_r == m.m_r);
00124     assert(m_c == m.m_c);
00125 
00126     Matrix res(m_r, m_c);
00127 
00128     LOOP(res.m_v[i] = m_v[i] - m.m_v[i]);
00129 
00130     return res;
00131 }
00132 
00133 inline
00134 Matrix Matrix::operator-() const {
00135     Matrix res(m_r, m_c);
00136 
00137     LOOP(res.m_v[i] = -m_v[i]);
00138 
00139     return res;
00140 }
00141 
00142 inline
00143 Matrix Matrix::operator*(Real r) const {
00144     Matrix res(m_r, m_c);
00145 
00146     LOOP(res.m_v[i] = m_v[i] * r);
00147 
00148     return res;
00149 }
00150 
00151 inline
00152 Matrix Matrix::operator/(Real r) const {
00153     Matrix res(m_r, m_c);
00154 
00155     LOOP(res.m_v[i] = m_v[i] / r);
00156 
00157     return res;
00158 }
00159 
00160 inline
00161 Matrix &Matrix::operator+=(const Matrix &m) {
00162     assert(m_r == m.m_r);
00163     assert(m_c == m.m_c);
00164 
00165     LOOP(m_v[i] += m.m_v[i]);
00166 
00167     return *this;
00168 }
00169 
00170 inline
00171 Matrix &Matrix::operator-=(const Matrix &m) {
00172     assert(m_r == m.m_r);
00173     assert(m_c == m.m_c);
00174 
00175     LOOP(m_v[i] -= m.m_v[i]);
00176 
00177     return *this;
00178 }
00179 
00180 inline
00181 Matrix &Matrix::operator*=(Real r) {
00182     LOOP(m_v[i] *= r);
00183 
00184     return *this;
00185 }
00186 
00187 inline
00188 Matrix &Matrix::operator/=(Real r) {
00189     LOOP(m_v[i] /= r);
00190 
00191     return *this;
00192 }
00193 
00194 inline
00195 Real &Matrix::operator[](int i) {
00196     assert(i >= 0);
00197     assert(i < m_r * m_c);
00198 
00199     return m_v[i];
00200 }
00201 
00202 inline
00203 const Real &Matrix::operator[](int i) const {
00204     assert(i >= 0);
00205     assert(i < m_r * m_c);
00206 
00207     return m_v[i];
00208 }
00209 
00210 inline
00211 Real &Matrix::operator()(int row, int column) {
00212     assert(row >= 0);
00213     assert(row < m_r);
00214     assert(column >= 0);
00215     assert(column < m_c);
00216 
00217     return m_v[row * m_c + column];
00218 }
00219 
00220 inline
00221 const Real &Matrix::operator()(int row, int column) const {
00222     assert(row >= 0);
00223     assert(row < m_r);
00224     assert(column >= 0);
00225     assert(column < m_c);
00226 
00227     return m_v[row * m_c + column];
00228 }
00229 
00230 inline
00231 Real *Matrix::GetData() {
00232     return m_v;
00233 }
00234 
00235 inline
00236 Real Matrix::Trace() const {
00237     assert(m_r == m_c);
00238 
00239     Real *v = m_v;
00240     Real res = 0;
00241 
00242     for(int i = 0; i < m_c; i++, v += m_c + 1)
00243         res += *v;
00244 
00245     return res;
00246 }
00247 
00248 inline
00249 Matrix operator*(Real r, const Matrix &m) {
00250     return m * r;
00251 }
00252 
00253 #undef LOOP

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