Subversion Repositories gelsvn

Rev

Rev 43 | Rev 46 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 43 Rev 45
Line 18... Line 18...
18
		
18
		
19
  T x = m[i][j]
19
  T x = m[i][j]
20
 
20
 
21
  This template should be used through inheritance just like the 
21
  This template should be used through inheritance just like the 
22
  vector template */
22
  vector template */
23
  template <class VVT, class HVT, class MT, int ROWS>
23
  template <class VVT, class HVT, class MT, unsigned int ROWS>
24
    class ArithMatFloat
24
    class ArithMatFloat
25
    { 
25
    { 
26
    public:
26
    public:
27
 
27
 
28
      /// Horizontal vector type
28
      /// Horizontal vector type
Line 89... Line 89...
89
	}
89
	}
90
		
90
		
91
    public:
91
    public:
92
 
92
 
93
      /// Get vertical dimension of matrix 
93
      /// Get vertical dimension of matrix 
94
      static int get_v_dim() {return VVT::get_dim();}
94
      static unsigned int get_v_dim() {return VVT::get_dim();}
95
 
95
 
96
      /// Get horizontal dimension of matrix
96
      /// Get horizontal dimension of matrix
97
      static int get_h_dim() {return HVT::get_dim();}
97
      static unsigned int get_h_dim() {return HVT::get_dim();}
98
 
98
 
99
 
99
 
100
      /** Get const pointer to data array.
100
      /** Get const pointer to data array.
101
	  This function may be useful when interfacing with some other API 
101
	  This function may be useful when interfacing with some other API 
102
	  such as OpenGL (TM). */
102
	  such as OpenGL (TM). */
Line 116... Line 116...
116
      //----------------------------------------------------------------------
116
      //----------------------------------------------------------------------
117
      // index operators
117
      // index operators
118
      //----------------------------------------------------------------------
118
      //----------------------------------------------------------------------
119
 
119
 
120
      /// Const index operator. Returns i'th row of matrix.
120
      /// Const index operator. Returns i'th row of matrix.
121
      const HVT& operator [] ( int i ) const
121
      const HVT& operator [] ( unsigned int i ) const
122
	{
122
	{
123
	  assert(i<ROWS);
123
	  assert(i<ROWS);
124
	  return data[i];
124
	  return data[i];
125
	}
125
	}
126
 
126
 
127
      /// Non-const index operator. Returns i'th row of matrix.
127
      /// Non-const index operator. Returns i'th row of matrix.
128
      HVT& operator [] ( int i ) 
128
      HVT& operator [] ( unsigned int i ) 
129
	{
129
	{
130
	  assert(i<ROWS);
130
	  assert(i<ROWS);
131
	  return data[i];
131
	  return data[i];
132
	}
132
	}
133
 
133
 
Line 220... Line 220...
220
	  return v_new;
220
	  return v_new;
221
	}
221
	}
222
    };
222
    };
223
 
223
 
224
  /// Multiply scalar onto matrix
224
  /// Multiply scalar onto matrix
225
  template <class VVT, class HVT, class MT, int ROWS>
225
  template <class VVT, class HVT, class MT, unsigned int ROWS>
226
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
226
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
227
    {
227
    {
228
      return v * k;
228
      return v * k;
229
    }
229
    }
230
 
230
 
231
  /// Multiply scalar onto matrix
231
  /// Multiply scalar onto matrix
232
  template <class VVT, class HVT, class MT, int ROWS>
232
  template <class VVT, class HVT, class MT, unsigned int ROWS>
233
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
233
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
234
    {
234
    {
235
      return v * k;
235
      return v * k;
236
    }
236
    }
237
 
237
 
238
  /// Multiply scalar onto matrix
238
  /// Multiply scalar onto matrix
239
  template <class VVT, class HVT, class MT, int ROWS>
239
  template <class VVT, class HVT, class MT, unsigned int ROWS>
240
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
240
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
241
    {
241
    {
242
      return v * k;
242
      return v * k;
243
    }
243
    }
244
 
244
 
245
  /// Multiply vector onto matrix 
245
  /// Multiply vector onto matrix 
246
  template <class VVT, class HVT, class MT, int ROWS>
246
  template <class VVT, class HVT, class MT, unsigned int ROWS>
247
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
247
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
248
    {
248
    {
249
      VVT v2;
249
      VVT v2;
250
      for(int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
250
      for(unsigned int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
251
      return v2;
251
      return v2;
252
    }
252
    }
253
 
253
 
254
 
254
 
255
#ifndef WIN32
255
#ifndef WIN32
Line 266... Line 266...
266
  */
266
  */
267
 
267
 
268
  template <class VVT, class HVT, 
268
  template <class VVT, class HVT, 
269
    class HV1T, class VV2T,
269
    class HV1T, class VV2T,
270
    class MT1, class MT2, class MT,
270
    class MT1, class MT2, class MT,
271
    int ROWS1, int ROWS2>
271
    unsigned int ROWS1, unsigned int ROWS2>
272
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
272
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
273
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
273
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
274
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
274
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
275
    {
275
    {
276
      int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
276
      unsigned int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
277
      for(int i=0;i<ROWS1;i++)
277
      for(unsigned int i=0;i<ROWS1;i++)
278
	for(int j=0;j<cols;j++)
278
	for(unsigned int j=0;j<cols;j++)
279
	  {
279
	  {
280
	    m[i][j] = 0;
280
	    m[i][j] = 0;
281
	    for(int k=0;k<ROWS2;k++)
281
	    for(unsigned int k=0;k<ROWS2;k++)
282
	      m[i][j] += m1[i][k] * m2[k][j]; 
282
	      m[i][j] += m1[i][k] * m2[k][j]; 
283
	  }
283
	  }
284
    }
284
    }
285
 
285
 
286
 
286
 
287
  /** Transpose. See the discussion on mul if you are curious as to why
287
  /** Transpose. See the discussion on mul if you are curious as to why
288
      I don't simply return the transpose. */
288
      I don't simply return the transpose. */
289
  template <class VVT, class HVT, class M1T, class M2T, int ROWS, int COLS>
289
  template <class VVT, class HVT, class M1T, class M2T, unsigned int ROWS, unsigned int COLS>
290
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
290
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
291
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
291
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
292
    {
292
    {
293
      for(int i=0;i<M2T::get_v_dim();++i)
293
      for(unsigned int i=0;i<M2T::get_v_dim();++i)
294
	for(int j=0;j<M2T::get_h_dim();++j)
294
	for(unsigned int j=0;j<M2T::get_h_dim();++j)
295
	  m_new[i][j] = m[j][i];
295
	  m_new[i][j] = m[j][i];
296
    }
296
    }
297
 
297
 
298
#else
298
#else
299
 
299
 
Line 303... Line 303...
303
  // matrices of wrong dimension.
303
  // matrices of wrong dimension.
304
 
304
 
305
  template <class M1, class M2, class M>
305
  template <class M1, class M2, class M>
306
    inline void mul(const M1& m1, const M2& m2, M& m)
306
    inline void mul(const M1& m1, const M2& m2, M& m)
307
    {
307
    {
308
      int cols = M::get_h_dim();
308
      unsigned int cols = M::get_h_dim();
309
      int rows1 = M1::get_v_dim();
309
      unsigned int rows1 = M1::get_v_dim();
310
      int rows2 = M2::get_v_dim();
310
      unsigned int rows2 = M2::get_v_dim();
311
 
311
 
312
      for(int i=0;i<rows1;++i)
312
      for(unsigned int i=0;i<rows1;++i)
313
	for(int j=0;j<cols;++j)
313
	for(unsigned int j=0;j<cols;++j)
314
	  {
314
	  {
315
	    m[i][j] = 0;
315
	    m[i][j] = 0;
316
	    for(int k=0;k<rows2;++k)
316
	    for(unsigned int k=0;k<rows2;++k)
317
	      m[i][j] += m1[i][k] * m2[k][j];
317
	      m[i][j] += m1[i][k] * m2[k][j];
318
	  }
318
	  }
319
    }
319
    }
320
 
320
 
321
 
321
 
322
  /** Transpose. See the discussion on mul if you are curious as to why
322
  /** Transpose. See the discussion on mul if you are curious as to why
323
      I don't simply return the transpose. */
323
      I don't simply return the transpose. */
324
  template <class M1, class M2>
324
  template <class M1, class M2>
325
    inline void transpose(const M1& m1, M2& m2)
325
    inline void transpose(const M1& m1, M2& m2)
326
    {
326
    {
327
      for(int i=0;i<M2::get_v_dim();++i)
327
      for(unsigned int i=0;i<M2::get_v_dim();++i)
328
	for(int j=0;j<M2::get_h_dim();++j)
328
	for(unsigned int j=0;j<M2::get_h_dim();++j)
329
	  m2[i][j] = m1[j][i];
329
	  m2[i][j] = m1[j][i];
330
    }
330
    }
331
 
331
 
332
#endif
332
#endif
333
 
333
 
334
  /** Compute the outer product of a and b: a * transpose(b). This is 
334
  /** Compute the outer product of a and b: a * transpose(b). This is 
335
      a matrix with a::rows and b::columns. */
335
      a matrix with a::rows and b::columns. */
336
  template <class VVT, class HVT, class MT, int ROWS>
336
  template <class VVT, class HVT, class MT, unsigned int ROWS>
337
    void outer_product(const VVT& a, const HVT& b, 
337
    void outer_product(const VVT& a, const HVT& b, 
338
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
338
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
339
    {
339
    {
340
      int R = VVT::get_dim();
340
      unsigned int R = VVT::get_dim();
341
      int C = HVT::get_dim();
341
      unsigned int C = HVT::get_dim();
342
      for(int i=0;i<R;++i)
342
      for(unsigned int i=0;i<R;++i)
343
	for(int j=0;j<C;++j)
343
	for(unsigned int j=0;j<C;++j)
344
	  {
344
	  {
345
	    m[i][j] = a[i] * b[j];
345
	    m[i][j] = a[i] * b[j];
346
	  }
346
	  }
347
    }
347
    }
348
 
348
 
Line 362... Line 362...
362
  */
362
  */
363
 
363
 
364
  template <class M1, class M2>
364
  template <class M1, class M2>
365
    void copy_matrix(const M1& inmat, M2& outmat)
365
    void copy_matrix(const M1& inmat, M2& outmat)
366
    {
366
    {
367
      const int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
367
      const unsigned int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
368
      const int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
368
      const unsigned int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
369
      for(int i=0;i<R;++i)
369
      for(unsigned int i=0;i<R;++i)
370
	for(int j=0;j<C;++j)
370
	for(unsigned int j=0;j<C;++j)
371
	  outmat[i][j] = inmat[i][j];
371
	  outmat[i][j] = inmat[i][j];
372
    }
372
    }
373
 
373
 
374
  /** Put to operator */
374
  /** Put to operator */
375
  template <class VVT, class HVT, class MT, int ROWS>
375
  template <class VVT, class HVT, class MT, unsigned int ROWS>
376
    inline std::ostream& 
376
    inline std::ostream& 
377
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
377
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
378
    {
378
    {
379
      os << "[\n";
379
      os << "[\n";
380
      for(int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
380
      for(unsigned int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
381
      os << "]\n";
381
      os << "]\n";
382
      return os;
382
      return os;
383
    }
383
    }
384
 
384
 
385
  /** Get from operator */
385
  /** Get from operator */
386
  template <class VVT, class HVT, class MT, int ROWS>
386
  template <class VVT, class HVT, class MT, unsigned int ROWS>
387
    inline std::istream& operator>>(std::istream&is, 
387
    inline std::istream& operator>>(std::istream&is, 
388
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
388
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
389
    {
389
    {
390
      for(int i=0;i<ROWS;i++) is>>m[i];
390
      for(unsigned int i=0;i<ROWS;i++) is>>m[i];
391
      return is;
391
      return is;
392
    }
392
    }
393
}
393
}
394
#endif
394
#endif