#
source:
foam/trunk/vision/src/Matrix.h
@
86

Revision 86, 6.4 KB checked in by dave, 11 years ago (diff) |
---|

Line | |
---|---|

1 | // Copyright (C) 2009 foam |

2 | // |

3 | // This program is free software; you can redistribute it and/or modify |

4 | // it under the terms of the GNU General Public License as published by |

5 | // the Free Software Foundation; either version 2 of the License, or |

6 | // (at your option) any later version. |

7 | // |

8 | // This program is distributed in the hope that it will be useful, |

9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |

10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |

11 | // GNU General Public License for more details. |

12 | // |

13 | // You should have received a copy of the GNU General Public License |

14 | // along with this program; if not, write to the Free Software |

15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |

16 | |

17 | #include <assert.h> |

18 | #include <iostream> |

19 | #include "Vector.h" |

20 | |

21 | #ifndef FOAM_MATRIX |

22 | #define FOAM_MATRIX |

23 | |

24 | template<class T> |

25 | class Matrix |

26 | { |

27 | public: |

28 | Matrix(); |

29 | Matrix(unsigned int r, unsigned int c); |

30 | ~Matrix(); |

31 | Matrix(const Matrix &other); |

32 | Matrix(unsigned int r, unsigned int c, float *data); |

33 | |

34 | // Row proxy classes to allow matrix[r][c] notation |

35 | class Row |

36 | { |

37 | public: |

38 | Row(Matrix *owner, unsigned int r) |

39 | { |

40 | m_Data=&owner->GetRawData()[r*owner->GetCols()]; |

41 | m_Cols=owner->GetCols(); |

42 | } |

43 | |

44 | T &operator[](unsigned int c) |

45 | { |

46 | assert(c<m_Cols); |

47 | return m_Data[c]; |

48 | } |

49 | |

50 | private: |

51 | T *m_Data; |

52 | unsigned int m_Cols; |

53 | }; |

54 | |

55 | class ConstRow |

56 | { |

57 | public: |

58 | ConstRow(const Matrix *owner, unsigned int r) |

59 | { |

60 | m_Data=&owner->GetRawDataConst()[r*owner->GetCols()]; |

61 | m_Cols=owner->GetCols(); |

62 | } |

63 | |

64 | const T &operator[](unsigned int c) const |

65 | { |

66 | assert(c<m_Cols); |

67 | return m_Data[c]; |

68 | } |

69 | |

70 | private: |

71 | const T *m_Data; |

72 | unsigned int m_Cols; |

73 | }; |

74 | |

75 | |

76 | Row operator[](unsigned int r) |

77 | { |

78 | assert(r<m_Rows); |

79 | return Row(this,r); |

80 | } |

81 | |

82 | ConstRow operator[](unsigned int r) const |

83 | { |

84 | assert(r<m_Rows); |

85 | return ConstRow(this,r); |

86 | } |

87 | |

88 | unsigned int GetRows() const { return m_Rows; } |

89 | unsigned int GetCols() const { return m_Cols; } |

90 | T *GetRawData() { return m_Data; } |

91 | const T *GetRawDataConst() const { return m_Data; } |

92 | Vector<T> GetRowVector(unsigned int r) const; |

93 | Vector<T> GetColVector(unsigned int c) const; |

94 | |

95 | void Print() const; |

96 | void SetAll(T s); |

97 | void Zero() { SetAll(0); } |

98 | bool IsInf(); |

99 | Matrix Transposed(); |

100 | |

101 | Matrix &operator=(const Matrix &other); |

102 | Matrix operator+(const Matrix &other) const; |

103 | Matrix operator-(const Matrix &other) const; |

104 | Matrix operator*(const Matrix &other) const; |

105 | Matrix &operator+=(const Matrix &other); |

106 | Matrix &operator-=(const Matrix &other); |

107 | Matrix &operator*=(const Matrix &other); |

108 | |

109 | static void RunTests(); |

110 | |

111 | private: |

112 | |

113 | unsigned int m_Rows; |

114 | unsigned int m_Cols; |

115 | |

116 | T *m_Data; |

117 | |

118 | }; |

119 | |

120 | template<class T> |

121 | Matrix<T>::Matrix(unsigned int r, unsigned int c) : |

122 | m_Rows(r), |

123 | m_Cols(c) |

124 | { |

125 | m_Data=new T[r*c]; |

126 | } |

127 | |

128 | template<class T> |

129 | Matrix<T>::Matrix() : |

130 | m_Rows(0), |

131 | m_Cols(0), |

132 | m_Data(NULL) |

133 | { |

134 | } |

135 | |

136 | template<class T> |

137 | Matrix<T>::Matrix(unsigned int r, unsigned int c, float *data) : |

138 | m_Rows(r), |

139 | m_Cols(c), |

140 | m_Data(data) |

141 | { |

142 | } |

143 | |

144 | template<class T> |

145 | Matrix<T>::~Matrix() |

146 | { |

147 | delete[] m_Data; |

148 | } |

149 | |

150 | template<class T> |

151 | Matrix<T>::Matrix(const Matrix &other) |

152 | { |

153 | m_Rows = other.m_Rows; |

154 | m_Cols = other.m_Cols; |

155 | m_Data=new T[m_Rows*m_Cols]; |

156 | memcpy(m_Data,other.m_Data,m_Rows*m_Cols*sizeof(T)); |

157 | } |

158 | |

159 | template<class T> |

160 | Matrix<T> &Matrix<T>::operator=(const Matrix &other) |

161 | { |

162 | if (m_Data!=NULL) |

163 | { |

164 | delete[] m_Data; |

165 | } |

166 | |

167 | m_Rows = other.m_Rows; |

168 | m_Cols = other.m_Cols; |

169 | m_Data=new T[m_Rows*m_Cols]; |

170 | memcpy(m_Data,other.m_Data,m_Rows*m_Cols*sizeof(T)); |

171 | |

172 | return *this; |

173 | } |

174 | |

175 | template<class T> |

176 | void Matrix<T>::Print() const |

177 | { |

178 | for (unsigned int i=0; i<m_Rows; i++) |

179 | { |

180 | for (unsigned int j=0; j<m_Cols; j++) |

181 | { |

182 | std::cerr<<(*this)[i][j]<<" "; |

183 | } |

184 | std::cerr<<std::endl; |

185 | } |

186 | } |

187 | |

188 | template<class T> |

189 | void Matrix<T>::SetAll(T s) |

190 | { |

191 | for (unsigned int i=0; i<m_Rows; i++) |

192 | { |

193 | for (unsigned int j=0; j<m_Cols; j++) |

194 | { |

195 | (*this)[i][j]=s; |

196 | } |

197 | } |

198 | } |

199 | |

200 | template<class T> |

201 | bool Matrix<T>::IsInf() |

202 | { |

203 | for (unsigned int i=0; i<m_Rows; i++) |

204 | { |

205 | for (unsigned int j=0; j<m_Cols; j++) |

206 | { |

207 | if (isinf((*this)[i][j])) return true; |

208 | if (isnan((*this)[i][j])) return true; |

209 | } |

210 | } |

211 | return false; |

212 | } |

213 | |

214 | template<class T> |

215 | Matrix<T> Matrix<T>::Transposed() |

216 | { |

217 | Matrix<T> copy(*this); |

218 | for (unsigned int i=0; i<m_Rows; i++) |

219 | { |

220 | for (unsigned int j=0; j<m_Cols; j++) |

221 | { |

222 | copy[i][j]=(*this)[j][i]; |

223 | } |

224 | } |

225 | return copy; |

226 | } |

227 | |

228 | |

229 | template<class T> |

230 | Matrix<T> Matrix<T>::operator+(const Matrix &other) const |

231 | { |

232 | assert(m_Rows=other.m_Rows); |

233 | assert(m_Cols=other.m_Cols); |

234 | |

235 | Matrix<T> ret(m_Rows,m_Cols); |

236 | for (unsigned int i=0; i<m_Rows; i++) |

237 | { |

238 | for (unsigned int j=0; j<m_Cols; j++) |

239 | { |

240 | ret[i][j]=(*this)[i][j]+other[i][j]; |

241 | } |

242 | } |

243 | return ret; |

244 | } |

245 | |

246 | template<class T> |

247 | Matrix<T> Matrix<T>::operator-(const Matrix &other) const |

248 | { |

249 | assert(m_Rows=other.m_Rows); |

250 | assert(m_Cols=other.m_Cols); |

251 | |

252 | Matrix<T> ret(m_Rows,m_Cols); |

253 | for (unsigned int i=0; i<m_Rows; i++) |

254 | { |

255 | for (unsigned int j=0; j<m_Cols; j++) |

256 | { |

257 | ret[i][j]=(*this)[i][j]-other[i][j]; |

258 | } |

259 | } |

260 | return ret; |

261 | } |

262 | |

263 | template<class T> |

264 | Matrix<T> Matrix<T>::operator*(const Matrix &other) const |

265 | { |

266 | assert(m_Cols==other.m_Rows); |

267 | |

268 | Matrix<T> ret(m_Rows,other.m_Cols); |

269 | |

270 | for (unsigned int i=0; i<m_Rows; i++) |

271 | { |

272 | for (unsigned int j=0; j<other.m_Cols; j++) |

273 | { |

274 | ret[i][j]=0; |

275 | for (unsigned int k=0; k<m_Cols; k++) |

276 | { |

277 | ret[i][j]+=(*this)[i][k]*other[k][j]; |

278 | } |

279 | } |

280 | } |

281 | return ret; |

282 | } |

283 | |

284 | template<class T> |

285 | Matrix<T> &Matrix<T>::operator+=(const Matrix &other) |

286 | { |

287 | (*this)=(*this)+other; |

288 | return *this; |

289 | } |

290 | |

291 | template<class T> |

292 | Matrix<T> &Matrix<T>::operator-=(const Matrix &other) |

293 | { |

294 | (*this)=(*this)-other; |

295 | return *this; |

296 | } |

297 | |

298 | template<class T> |

299 | Matrix<T> &Matrix<T>::operator*=(const Matrix &other) |

300 | { |

301 | (*this)=(*this)*other; |

302 | return *this; |

303 | } |

304 | |

305 | template<class T> |

306 | Vector<T> Matrix<T>::GetRowVector(unsigned int r) const |

307 | { |

308 | assert(r<m_Rows); |

309 | Vector<T> ret(m_Cols); |

310 | for (unsigned int j=0; j<m_Cols; j++) |

311 | { |

312 | ret[j]=(*this)[r][j]; |

313 | } |

314 | return ret; |

315 | } |

316 | |

317 | template<class T> |

318 | Vector<T> Matrix<T>::GetColVector(unsigned int c) const |

319 | { |

320 | assert(c<m_Cols); |

321 | Vector<T> ret(m_Rows); |

322 | for (unsigned int i=0; i<m_Rows; i++) |

323 | { |

324 | ret[i]=(*this)[i][c]; |

325 | } |

326 | return ret; |

327 | } |

328 | |

329 | template<class T> |

330 | void Matrix<T>::RunTests() |

331 | { |

332 | Matrix<T> m(10,10); |

333 | m.SetAll(0); |

334 | assert(m[0][0]==0); |

335 | m[5][2]=0.5; |

336 | assert(m[5][2]==0.5); |

337 | Matrix<T> om(m); |

338 | assert(om[5][2]==0.5); |

339 | Matrix<T> a(2,3); |

340 | a[0][0]=1; a[0][1]=2; a[0][2]=3; |

341 | a[1][0]=4; a[1][1]=5; a[1][2]=6; |

342 | Matrix<T> b(3,1); |

343 | b[0][0]=3; |

344 | b[1][0]=1; |

345 | b[2][0]=2; |

346 | Matrix<T> c=a*b; |

347 | assert(c[0][0]==11 && c[1][0]==29); |

348 | } |

349 | |

350 | #endif |

**Note:**See TracBrowser for help on using the repository browser.