Subversion Repositories gelsvn

Rev

Rev 10 | Rev 45 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#ifndef __CGLA_ARITHMATFLOAT_H__
2
#define __CGLA_ARITHMATFLOAT_H__
3
 
4
#include <vector>
5
#include <iostream>
6
 
7
#include "CGLA.h"
8
 
9
 
12 jab 10
namespace CGLA 
11
{
2 bj 12
 
12 jab 13
  /** Basic class template for matrices.
2 bj 14
 
12 jab 15
  In this template a matrix is defined as an array of vectors. This may
16
  not in all cases be the most efficient but it has the advantage that 
17
  it is possible to use the double subscripting notation:
2 bj 18
 
12 jab 19
  T x = m[i][j]
2 bj 20
 
12 jab 21
  This template should be used through inheritance just like the 
22
  vector template */
23
  template <class VVT, class HVT, class MT, int ROWS>
24
    class ArithMatFloat
25
    { 
2 bj 26
#define for_all_i(expr) for(int i=0;i<ROWS;i++) {expr;}
27
 
12 jab 28
    public:
2 bj 29
 
12 jab 30
      /// Horizontal vector type
31
      typedef HVT HVectorType;
2 bj 32
 
12 jab 33
      /// Vertical vector type
34
      typedef VVT VVectorType;
2 bj 35
 
12 jab 36
      /// The type of a matrix element
37
      typedef typename HVT::ScalarType ScalarType;
2 bj 38
 
12 jab 39
    protected:
2 bj 40
 
12 jab 41
      /// The actual contents of the matrix.
42
      HVT data[ROWS];
2 bj 43
 
12 jab 44
    protected:
2 bj 45
 
12 jab 46
      /// Construct 0 matrix
47
      ArithMatFloat() 
48
	{
10 jab 49
#ifndef NDEBUG
12 jab 50
	  for_all_i(data[i]=HVT(CGLA_INIT_VALUE));
10 jab 51
#endif
12 jab 52
	}
2 bj 53
 
12 jab 54
      /// Construct a matrix where all entries are the same.
55
      explicit ArithMatFloat(ScalarType x)
56
	{
57
	  for_all_i(data[i] = HVT(x));
58
	}
2 bj 59
 
12 jab 60
      /// Construct a matrix where all rows are the same.
61
      explicit ArithMatFloat(HVT _a)
62
	{
63
	  for_all_i(data[i] = _a);
64
	}
2 bj 65
 
66
 
12 jab 67
      /// Construct a matrix with two rows.
68
      ArithMatFloat(HVT _a, HVT _b)
69
	{
70
	  assert(ROWS==2);
71
	  data[0] = _a;
72
	  data[1] = _b;
73
	}
2 bj 74
 
12 jab 75
      /// Construct a matrix with three rows.
76
      ArithMatFloat(HVT _a, HVT _b, HVT _c)
77
	{
78
	  assert(ROWS==3);
79
	  data[0] = _a;
80
	  data[1] = _b;
81
	  data[2] = _c;
82
	}
2 bj 83
 
12 jab 84
      /// Construct a matrix with four rows.
85
      ArithMatFloat(HVT _a, HVT _b, HVT _c, HVT _d)
86
	{
87
	  assert(ROWS==4);
88
	  data[0] = _a;
89
	  data[1] = _b;
90
	  data[2] = _c;
91
	  data[3] = _d;
92
	}
2 bj 93
 
12 jab 94
    public:
2 bj 95
 
12 jab 96
      /// Get vertical dimension of matrix 
97
      static int get_v_dim() {return VVT::get_dim();}
2 bj 98
 
12 jab 99
      /// Get horizontal dimension of matrix
100
      static int get_h_dim() {return HVT::get_dim();}
2 bj 101
 
102
 
12 jab 103
      /** Get const pointer to data array.
104
	  This function may be useful when interfacing with some other API 
105
	  such as OpenGL (TM). */
106
      const ScalarType* get() const 
107
	{
108
	  return data[0].get();
109
	}
2 bj 110
 
12 jab 111
      /** Get pointer to data array.
112
	  This function may be useful when interfacing with some other API 
113
	  such as OpenGL (TM). */
114
      ScalarType* get()
115
	{
116
	  return data[0].get();
117
	}
2 bj 118
 
12 jab 119
      //----------------------------------------------------------------------
120
      // index operators
121
      //----------------------------------------------------------------------
2 bj 122
 
12 jab 123
      /// Const index operator. Returns i'th row of matrix.
124
      const HVT& operator [] ( int i ) const
125
	{
126
	  assert(i<ROWS);
127
	  return data[i];
128
	}
2 bj 129
 
12 jab 130
      /// Non-const index operator. Returns i'th row of matrix.
131
      HVT& operator [] ( int i ) 
132
	{
133
	  assert(i<ROWS);
134
	  return data[i];
135
	}
2 bj 136
 
12 jab 137
      //----------------------------------------------------------------------
2 bj 138
 
12 jab 139
      /// Equality operator. 
140
      bool operator==(const MT& v) const 
141
	{
142
	  for_all_i(if (data[i] != v[i]) return false)
143
	    return true;
144
	}
2 bj 145
 
12 jab 146
      /// Inequality operator.
147
      bool operator!=(const MT& v) const 
148
	{
149
	  return !(*this==v);
150
	}
2 bj 151
 
12 jab 152
      //----------------------------------------------------------------------
2 bj 153
 
12 jab 154
      /// Multiply scalar onto matrix. All entries are multiplied by scalar.
155
      const MT operator * (ScalarType k) const
156
	{
157
	  MT v_new;
158
	  for_all_i(v_new[i] = data[i] * k);
159
	  return v_new;
160
	}
2 bj 161
 
12 jab 162
      /// Divide all entries in matrix by scalar.
163
      const MT operator / (ScalarType k) const
164
	{
165
	  MT v_new;
166
	  for_all_i(v_new[i] = data[i] / k);
167
	  return v_new;      
168
	}
2 bj 169
 
12 jab 170
      /// Assignment multiplication of matrix by scalar.
171
      const MT& operator *=(ScalarType k) 
172
	{
173
	  for_all_i(data[i] *= k); 
174
	  return static_cast<const MT&>(*this);
175
	}
2 bj 176
 
12 jab 177
      /// Assignment division of matrix by scalar.
178
      const MT& operator /=(ScalarType k) 
179
	{ 
180
	  for_all_i(data[i] /= k); 
181
	  return static_cast<const MT&>(*this);
182
	}
2 bj 183
 
12 jab 184
      //----------------------------------------------------------------------
2 bj 185
 
12 jab 186
      /// Add two matrices. 
187
      const MT operator + (const MT& m1) const
188
	{
189
	  MT v_new;
190
	  for_all_i(v_new[i] = data[i] + m1[i]);
191
	  return v_new;
192
	}
2 bj 193
 
12 jab 194
      /// Subtract two matrices.
195
      const MT operator - (const MT& m1) const
2 bj 196
	{
12 jab 197
	  MT v_new;
198
	  for_all_i(v_new[i] = data[i] - m1[i]);
199
	  return v_new;
2 bj 200
	}
201
 
12 jab 202
      /// Assigment addition of matrices.
203
      const MT& operator +=(const MT& v) 
2 bj 204
	{
12 jab 205
	  for_all_i(data[i] += v[i]); 
206
	  return static_cast<const MT&>(*this);
2 bj 207
	}
208
 
12 jab 209
      /// Assigment subtraction of matrices.
210
      const MT& operator -=(const MT& v) 
2 bj 211
	{
12 jab 212
	  for_all_i(data[i] -= v[i]); 
213
	  return static_cast<const MT&>(*this);
2 bj 214
	}
215
 
12 jab 216
      //----------------------------------------------------------------------
217
 
218
      /// Negate matrix.
219
      const MT operator - () const
2 bj 220
	{
12 jab 221
	  MT v_new;
222
	  for_all_i(v_new[i] = - data[i]);
223
	  return v_new;
2 bj 224
	}
225
 
12 jab 226
#undef for_all_i  
227
 
228
    };
2 bj 229
 
12 jab 230
  /// Multiply scalar onto matrix
231
  template <class VVT, class HVT, class MT, int ROWS>
232
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
233
    {
234
      return v * k;
235
    }
236
 
237
  /// Multiply scalar onto matrix
238
  template <class VVT, class HVT, class MT, int ROWS>
239
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
240
    {
241
      return v * k;
242
    }
243
 
244
  /// Multiply scalar onto matrix
245
  template <class VVT, class HVT, class MT, int ROWS>
246
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
247
    {
248
      return v * k;
249
    }
250
 
251
  /// Multiply vector onto matrix 
252
  template <class VVT, class HVT, class MT, int ROWS>
253
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
254
    {
255
      VVT v2;
256
      for(int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
257
      return v2;
258
    }
259
 
260
 
2 bj 261
#ifndef WIN32
12 jab 262
  /** Multiply two arbitrary matrices. 
263
      In principle, this function could return a matrix, but in general
264
      the new matrix will be of a type that is different from either of
265
      the two matrices that are multiplied together. We do not want to 
266
      return an ArithMatFloat - so it seems best to let the return value be
267
      a reference arg.
2 bj 268
 
12 jab 269
      This template can only be instantiated if the dimensions of the
270
      matrices match -- i.e. if the multiplication can actually be
271
      carried out. This is more type safe than the win32 version below.
272
  */
2 bj 273
 
12 jab 274
  template <class VVT, class HVT, 
275
    class HV1T, class VV2T,
276
    class MT1, class MT2, class MT,
277
    int ROWS1, int ROWS2>
278
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
279
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
280
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
281
    {
282
      int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
283
      for(int i=0;i<ROWS1;i++)
284
	for(int j=0;j<cols;j++)
285
	  {
286
	    m[i][j] = 0;
287
	    for(int k=0;k<ROWS2;k++)
288
	      m[i][j] += m1[i][k] * m2[k][j]; 
289
	  }
290
    }
2 bj 291
 
292
 
12 jab 293
  /** Transpose. See the discussion on mul if you are curious as to why
294
      I don't simply return the transpose. */
295
  template <class VVT, class HVT, class M1T, class M2T, int ROWS, int COLS>
296
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
297
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
298
    {
299
      for(int i=0;i<M2T::get_v_dim();i++)
300
	for(int j=0;j<M2T::get_h_dim();j++)
301
	  m_new[i][j] = m[j][i];
302
    }
2 bj 303
 
304
#else
305
 
12 jab 306
  //----------------- win32 -------------------------------
307
  // Visual studio is not good at deducing the args. to these template functions.
308
  // This means that you can call the two functions below with 
309
  // matrices of wrong dimension.
2 bj 310
 
12 jab 311
  template <class M1, class M2, class M>
312
    inline void mul(const M1& m1, const M2& m2, M& m)
313
    {
314
      int cols = M::get_h_dim();
315
      int rows1 = M1::get_v_dim();
316
      int rows2 = M2::get_v_dim();
2 bj 317
 
12 jab 318
      for(int i=0;i<rows1;i++)
319
	for(int j=0;j<cols;j++)
320
	  {
321
	    m[i][j] = 0;
322
	    for(int k=0;k<rows2;k++)
323
	      m[i][j] += m1[i][k] * m2[k][j];
324
	  }
325
    }
2 bj 326
 
327
 
12 jab 328
  /** Transpose. See the discussion on mul if you are curious as to why
329
      I don't simply return the transpose. */
330
  template <class M1, class M2>
331
    inline void transpose(const M1& m1, M2& m2)
332
    {
333
      for(int i=0;i<M2::get_v_dim();i++)
334
	for(int j=0;j<M2::get_h_dim();j++)
335
	  m2[i][j] = m1[j][i];
336
    }
2 bj 337
 
338
#endif
339
 
12 jab 340
  /** Compute the outer product of a and b: a * transpose(b). This is 
341
      a matrix with a::rows and b::columns. */
342
  template <class VVT, class HVT, class MT, int ROWS>
343
    void outer_product(const VVT& a, const HVT& b, 
344
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
345
    {
346
      int R = VVT::get_dim();
347
      int C = HVT::get_dim();
348
      for(int i=0;i<R;++i)
349
	for(int j=0;j<C;++j)
350
	  {
351
	    m[i][j] = a[i] * b[j];
352
	  }
353
    }
2 bj 354
 
12 jab 355
  /** Copy a matrix to another matrix, cell by cell.
356
      This conversion that takes a const matrix as first argument
357
      (source) and a non-const matrix as second argument
358
      (destination). The contents of the first matrix is simply copied
359
      to the second matrix. 
2 bj 360
 
12 jab 361
      However, if the first matrix is	larger than the second,
362
      the cells outside the range of the destination are simply not
363
      copied. If the destination is larger, the cells outside the 
364
      range of the source matrix are not touched.
2 bj 365
 
12 jab 366
      An obvious use of this function is to copy a 3x3 rotation matrix
367
      into a 4x4 transformation matrix.
368
  */
2 bj 369
 
12 jab 370
  template <class M1, class M2>
371
    void copy_matrix(const M1& inmat, M2& outmat)
372
    {
373
      const int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
374
      const int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
375
      for(int i=0;i<R;++i)
376
	for(int j=0;j<C;++j)
377
	  outmat[i][j] = inmat[i][j];
378
    }
2 bj 379
 
12 jab 380
  /** Put to operator */
381
  template <class VVT, class HVT, class MT, int ROWS>
382
    inline std::ostream& 
383
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
384
    {
385
      os << "[\n";
386
      for(int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
387
      os << "]\n";
388
      return os;
389
    }
2 bj 390
 
12 jab 391
  /** Get from operator */
392
  template <class VVT, class HVT, class MT, int ROWS>
393
    inline std::istream& operator>>(std::istream&is, 
394
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
395
    {
396
      for(int i=0;i<ROWS;i++) is>>m[i];
397
      return is;
398
    }
2 bj 399
}
400
#endif