File: SpatialMatrix.h

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (538 lines) | stat: -rw-r--r-- 14,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * Copyright (C) 2010 Learning Algorithms and Systems Laboratory, EPFL, Switzerland
 * Author: Eric Sauser
 * email:   eric.sauser@a3.epf.ch
 * website: lasa.epfl.ch
 *
 * Permission is granted to copy, distribute, and/or modify this program
 * under the terms of the GNU General Public License, version 2 or any
 * later version published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details
 */

#ifndef SPATIAL_MATRIX_H
#define SPATIAL_MATRIX_H

#include "Matrix3.h"
#include "SpatialVector.h"
#ifdef USE_MATHLIB_NAMESPACE
namespace MathLib {
#endif
/**
 * \class SpatialMatrix
 * 
 * \ingroup MathLib
 * 
 * \brief An implementation of the template TMatrix class
 * 
 * This template square matrix class can be used for doing various matrix manipulation.
 * This should be combined with the TVector class for doing almost anything
 * you ever dreamt of. 
 */

class SpatialMatrix
{
public:
  Matrix3 m00;
  Matrix3 m01;
  Matrix3 m10;
  Matrix3 m11;
  
public:
  /// Empty constructor
  inline SpatialMatrix(){};
  /// Copy constructor
  inline SpatialMatrix(const SpatialMatrix &matrix){
    Set(matrix);
  };  
  /// Copy Constructor 
  inline SpatialMatrix(const Matrix3& _00,const Matrix3& _01,const Matrix3& _10,const Matrix3& _11){
    Set(_00,_01,_10,_11);
  };
  /// Constructor with values
  inline SpatialMatrix(REALTYPE _00, REALTYPE _01, REALTYPE _02, REALTYPE _03, REALTYPE _04, REALTYPE _05,
                       REALTYPE _10, REALTYPE _11, REALTYPE _12, REALTYPE _13, REALTYPE _14, REALTYPE _15,
                       REALTYPE _20, REALTYPE _21, REALTYPE _22, REALTYPE _23, REALTYPE _24, REALTYPE _25,
                       REALTYPE _30, REALTYPE _31, REALTYPE _32, REALTYPE _33, REALTYPE _34, REALTYPE _35,
                       REALTYPE _40, REALTYPE _41, REALTYPE _42, REALTYPE _43, REALTYPE _44, REALTYPE _45,
                       REALTYPE _50, REALTYPE _51, REALTYPE _52, REALTYPE _53, REALTYPE _54, REALTYPE _55){
    Set(_00,_01,_02,_03,_05,_05,
        _10,_11,_12,_13,_15,_15,
        _20,_21,_22,_23,_25,_25,
        _30,_31,_32,_33,_35,_35,
        _40,_41,_42,_43,_45,_45,
        _50,_51,_52,_53,_55,_55);
  }

  /// Destructor
  inline virtual ~SpatialMatrix(){}

  inline SpatialMatrix& Set(const SpatialMatrix& matrix){
    m00.Set(matrix.m00);  
    m01.Set(matrix.m01);  
    m10.Set(matrix.m10);  
    m11.Set(matrix.m11);
    return *this;  
  }

  inline SpatialMatrix& Set(REALTYPE _00, REALTYPE _01, REALTYPE _02, REALTYPE _03, REALTYPE _04, REALTYPE _05,
                            REALTYPE _10, REALTYPE _11, REALTYPE _12, REALTYPE _13, REALTYPE _14, REALTYPE _15,
                            REALTYPE _20, REALTYPE _21, REALTYPE _22, REALTYPE _23, REALTYPE _24, REALTYPE _25,
                            REALTYPE _30, REALTYPE _31, REALTYPE _32, REALTYPE _33, REALTYPE _34, REALTYPE _35,
                            REALTYPE _40, REALTYPE _41, REALTYPE _42, REALTYPE _43, REALTYPE _44, REALTYPE _45,
                            REALTYPE _50, REALTYPE _51, REALTYPE _52, REALTYPE _53, REALTYPE _54, REALTYPE _55){
    m00.Set(_00,_01,_02,
            _10,_11,_12,
            _20,_21,_22);
    m01.Set(_03,_04,_05,
            _13,_14,_15,
            _23,_24,_25);
    m10.Set(_30,_31,_32,
            _40,_41,_42,
            _50,_51,_52);
    m11.Set(_33,_34,_35,
            _43,_44,_45,
            _53,_54,_55);
    return *this;
  }
  
  inline SpatialMatrix& Set(const Matrix3& _00,const Matrix3& _01,const Matrix3& _10,const Matrix3& _11){
    m00.Set(_00);
    m01.Set(_01);
    m10.Set(_10);
    m11.Set(_11);
    return *this;
  }


  /// Get the sub matrix of size 3 given a block coordinate
  inline Matrix3& GetSubMatrix3(unsigned int blockRow, unsigned int blockCol){
    if(blockRow==0){
      if(blockCol==0){
        return m00;
      }else /*if(blockCol==1)*/{
        return m01;
      }
    }else if(blockRow==1){
      if(blockCol==0){
        return m10;
      }else /*if(blockCol==1)*/{
        return m11;
      }      
    }
  }
  
  inline Matrix3& GetSubMatrix3(unsigned int blockRow, unsigned int blockCol, Matrix3 &result) const
  {
    if(blockRow==0){
      if(blockCol==0){
        result.Set(m00);
      }else if(blockCol==1){
        result.Set(m01);
      }
    }else if(blockRow==1){
      if(blockCol==0){
        result.Set(m10);
      }else if(blockCol==1){
        result.Set(m11);
      }      
    }
    return result;
  }

  /// Set the sub matrix of size 3 given a block coordinate
  inline SpatialMatrix SetSubMatrix3(unsigned int blockRow, unsigned int blockCol, const Matrix3 &matrix)
  {
    if(blockRow==0){
      if(blockCol==0){
        m00.Set(matrix);
      }else if(blockCol==1){
        m01.Set(matrix);
      }
    }else if(blockRow==1){
      if(blockCol==0){
        m10.Set(matrix);
      }else if(blockCol==1){
        m11.Set(matrix);
      }      
    }
    return *this;
  }

  /// Assignment operator
  inline SpatialMatrix& operator = (const SpatialMatrix &matrix)
  {
    return Set(matrix);    
  }  

  /// Inverse operator
  inline SpatialMatrix operator - () const
  {
    SpatialMatrix result;
    return Minus(result);
  }

  /// Inverse operator
  inline SpatialMatrix& Minus(SpatialMatrix& result) const
  {
    m00.Minus(result.m00);
    m01.Minus(result.m01);
    m10.Minus(result.m10);
    m11.Minus(result.m11);
    return result;
  }
  
  
    /// Assignment data-wise operations
    inline SpatialMatrix& operator += (const SpatialMatrix &matrix) {
        m00 += matrix.m00;
        m01 += matrix.m01;
        m10 += matrix.m10;
        m11 += matrix.m11;
        return *this;
    }
    inline SpatialMatrix& operator -= (const SpatialMatrix &matrix) {
        m00 -= matrix.m00;
        m01 -= matrix.m01;
        m10 -= matrix.m10;
        m11 -= matrix.m11;
        return *this;
    }
    inline SpatialMatrix& operator ^= (const SpatialMatrix &matrix) {
        m00 ^= matrix.m00;
        m01 ^= matrix.m01;
        m10 ^= matrix.m10;
        m11 ^= matrix.m11;
        return *this;
    }
    inline SpatialMatrix& operator /= (const SpatialMatrix &matrix) {
        m00 /= matrix.m00;
        m01 /= matrix.m01;
        m10 /= matrix.m10;
        m11 /= matrix.m11;
        return *this;
    }
    inline SpatialMatrix& operator *= (const SpatialMatrix &matrix){
        SpatialMatrix copy(*this);
        copy.Mult(matrix,*this);
        return *this;
    }



    inline SpatialMatrix& operator += (REALTYPE scalar) {
        m00 += scalar;
        m01 += scalar;
        m10 += scalar;
        m11 += scalar;
        return *this;
    }

    inline SpatialMatrix& operator -= (REALTYPE scalar) {
        m00 -= scalar;
        m01 -= scalar;
        m10 -= scalar;
        m11 -= scalar;
        return *this;
    }

    inline SpatialMatrix& operator *= (REALTYPE scalar) {
        m00 *= scalar;
        m01 *= scalar;
        m10 *= scalar;
        m11 *= scalar;
        return *this;
    }

    inline SpatialMatrix& operator /= (REALTYPE scalar) {
        scalar = R_ONE/scalar;
        m00 *= scalar;
        m01 *= scalar;
        m10 *= scalar;
        m11 *= scalar;
        return *this;
    }

  
  
  inline SpatialMatrix operator + (const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Add(matrix,result);
  }

  inline SpatialMatrix operator - (const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Sub(matrix,result);
  }

  inline SpatialMatrix operator ^ (const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return PMult(matrix,result);
  }

  inline SpatialMatrix operator / (const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return PDiv(matrix,result);
  }

  inline SpatialMatrix operator * (const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Mult(matrix,result);
  }

  inline SpatialMatrix Add(const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Add(matrix,result);
  }

  inline SpatialMatrix Sub(const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Sub(matrix,result);
  }

  inline SpatialMatrix PMult(const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return PMult(matrix,result);
  }

  inline SpatialMatrix PDiv(const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return PDiv(matrix,result);
  }

  inline SpatialMatrix Mult(const SpatialMatrix &matrix) const
  {
    SpatialMatrix result;
    return Mult(matrix,result);
  }

  inline SpatialMatrix& Add(const SpatialMatrix &matrix, SpatialMatrix & result) const
  {
    m00.Add(matrix.m00,result.m00);
    m01.Add(matrix.m01,result.m01);
    m10.Add(matrix.m10,result.m10);
    m11.Add(matrix.m11,result.m11);
    return result;
  }

  inline SpatialMatrix& Sub(const SpatialMatrix &matrix, SpatialMatrix & result) const
  {
    m00.Sub(matrix.m00,result.m00);
    m01.Sub(matrix.m01,result.m01);
    m10.Sub(matrix.m10,result.m10);
    m11.Sub(matrix.m11,result.m11);
    return result;
  }

  inline SpatialMatrix& PMult(const SpatialMatrix &matrix, SpatialMatrix & result) const
  {
    m00.PMult(matrix.m00,result.m00);
    m01.PMult(matrix.m01,result.m01);
    m10.PMult(matrix.m10,result.m10);
    m11.PMult(matrix.m11,result.m11);
    return result;
  }

  inline SpatialMatrix& PDiv(const SpatialMatrix &matrix, SpatialMatrix & result) const
  {
    m00.PDiv(matrix.m00,result.m00);
    m01.PDiv(matrix.m01,result.m01);
    m10.PDiv(matrix.m10,result.m10);
    m11.PDiv(matrix.m11,result.m11);
    return result;
  }

  inline SpatialMatrix& Mult(const SpatialMatrix &matrix, SpatialMatrix & result) const
  {
    Matrix3 tmp;
    m00.Mult(matrix.m00,result.m00);
    result.m00 += (m01.Mult(matrix.m10,tmp));
    m00.Mult(matrix.m10,result.m01);
    result.m01 += (m01.Mult(matrix.m11,tmp));
    m10.Mult(matrix.m00,result.m10);
    result.m10 += (m11.Mult(matrix.m10,tmp));
    m10.Mult(matrix.m01,result.m11);
    result.m11 += (m11.Mult(matrix.m11,tmp));
    return result;
  }











  inline SpatialMatrix operator + (REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Add(scalar,result);
  }

  inline SpatialMatrix operator - (REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Sub(scalar,result);
  }

  inline SpatialMatrix operator * (REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Mult(scalar,result);
  }

  inline SpatialMatrix operator / (REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Div(scalar,result);
  }

  inline SpatialMatrix Add(REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Add(scalar,result);
  }

  inline SpatialMatrix Sub(REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Sub(scalar,result);
  }

  inline SpatialMatrix Mult(REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Mult(scalar,result);
  }

  inline SpatialMatrix Div(REALTYPE scalar) const
  {
    SpatialMatrix result;
    return Div(scalar,result);
  }


  inline SpatialMatrix& Add(REALTYPE scalar, SpatialMatrix & result) const
  {
    m00.Add(scalar,result.m00);
    m01.Add(scalar,result.m01);
    m10.Add(scalar,result.m10);
    m11.Add(scalar,result.m11);
    return result;
  }

  inline SpatialMatrix& Sub(REALTYPE scalar, SpatialMatrix & result) const
  {
    m00.Sub(scalar,result.m00);
    m01.Sub(scalar,result.m01);
    m10.Sub(scalar,result.m10);
    m11.Sub(scalar,result.m11);
    return result;
  }

  inline SpatialMatrix& Mult(REALTYPE scalar, SpatialMatrix & result) const
  {
    m00.Mult(scalar,result.m00);
    m01.Mult(scalar,result.m01);
    m10.Mult(scalar,result.m10);
    m11.Mult(scalar,result.m11);
    return result;
  }

  inline SpatialMatrix& Div(REALTYPE scalar, SpatialMatrix & result) const
  {
    scalar = R_ONE / scalar;
    m00.Mult(scalar,result.m00);
    m01.Mult(scalar,result.m01);
    m10.Mult(scalar,result.m10);
    m11.Mult(scalar,result.m11);
    return result;
  }



  inline SpatialVector operator * (const SpatialVector &vector) const
  {
    SpatialVector result;
    return Mult(vector,result);
  }

  inline SpatialVector Mult(const SpatialVector &vector) const
  {
    SpatialVector result;
    return Mult(vector,result);
  }

  inline SpatialVector& Mult(const SpatialVector &vector, SpatialVector &result) const
  {
    Vector3 tmp;
    m00.Mult(result.mAngular,result.mAngular);
    result.mAngular += (m01.Mult(vector.mLinear,tmp));
    m10.Mult(result.mAngular,result.mLinear);
    result.mLinear += (m11.Mult(vector.mLinear,tmp));
    return result;
  }  


  /// Tests equality of two matrices
  inline bool operator == (const SpatialMatrix& matrix) const
  {
    return ((m00==matrix.m00)&&(m01==matrix.m01)&&(m10==matrix.m10)&&(m11==matrix.m11));
  }

  /// tests inequality of two vectors
  inline bool operator != (const SpatialMatrix& matrix) const
  {
    return !(*this ==  matrix);
  }

  

  /// Self transpose
  inline SpatialMatrix& STranspose()
  {
    SpatialMatrix copy(*this);
    return copy.Transpose(*this);
  }

  /// Returns the transpose
  inline SpatialMatrix Transpose() const
  {
    SpatialMatrix result;
    return Transpose(result);
  }

  /// Returns the transpose in the result
  inline SpatialMatrix& Transpose(SpatialMatrix& result) const
  {
    m00.Transpose(result.m00);
    m01.Transpose(result.m10);
    m10.Transpose(result.m01);
    m11.Transpose(result.m11);
    return result;
  }


};


#ifdef USE_MATHLIB_NAMESPACE
}
#endif
#endif