Source

rbdl / src / spatialalgebra.h

  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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#ifndef SPATIALALGEBRA_H
#define SPATIALALGEBRA_H

#include <sstream>
#include <assert.h>
#include "cmlwrapper.h"

/** \brief Namespace for all the spatial algebra quantities
 */
namespace SpatialAlgebra {

namespace Templates {
	template<class T>
		class SpatialVector;

	template<class T>
		class SpatialMatrix;
}

typedef Templates::SpatialVector<double> SpatialVector;
typedef Templates::SpatialMatrix<double> SpatialMatrix;

namespace Templates {

/** \brief Vector class for spatial vectors (both motion and force vectors)
 */
template<class T>
class SpatialVector {
	public:
		SpatialVector() {};
		SpatialVector(const SpatialVector &vector) {
			unsigned int i;
			for (i = 0; i < 6; i++)
				mData[i] = vector.mData[i];
		};
		SpatialVector& operator=(const SpatialVector &vector) {
			if (this != &vector) {
				unsigned int i;
				for (i = 0; i < 6; i++)
					mData[i] = vector.mData[i];
			}
			return *this;
		};
		~SpatialVector() {};

		SpatialVector (const T &v0, const T &v1, const T &v2,
				const T &v3, const T &v4, const T &v5) {
			mData[0] = v0;
			mData[1] = v1;
			mData[2] = v2;
			mData[3] = v3;
			mData[4] = v4;
			mData[5] = v5;
		};

		// comparison
		bool operator==(const SpatialVector &vector) const {
			for (unsigned int i = 0; i < 6; i++) {
				if (mData[i] != vector.mData[i])
					return false;
			}
			return true;
		}

		bool operator!=(const SpatialVector &vector) const {
			return !this->operator==(vector);
		}

		// access operators
		const T& operator[](const unsigned int &index) const {
			assert (index	>= 0 && index < 6);
			return mData[index];
		};
		T& operator[](const unsigned int &index) {
			assert (index	>= 0 && index < 6);
			return mData[index];
		}

		const T& operator()(const unsigned int &index) const {
			assert (index	>= 0 && index < 6);
			return mData[index];
		};
		T& operator()(const unsigned int &index) {
			assert (index	>= 0 && index < 6);
			return mData[index];
		};
		
		void set(const T &v0, const T &v1, const T &v2,
				const T &v3, const T &v4, const T &v5) {
			mData[0] = v0;
			mData[1] = v1;
			mData[2] = v2;
			mData[3] = v3;
			mData[4] = v4;
			mData[5] = v5;		};
		void zero() {
			set(0., 0., 0., 0., 0., 0.);
		}

		// Operators with scalars
		SpatialVector operator*(const T &scalar) const {
			return SpatialVector(
					mData[0] * scalar,
					mData[1] * scalar,
					mData[2] * scalar,
					mData[3] * scalar,
					mData[4] * scalar,
					mData[5] * scalar
					);
		};
		void operator*=(const T &scalar) {
			mData[0] *= scalar;
			mData[1] *= scalar;
			mData[2] *= scalar;
			mData[3] *= scalar;
			mData[4] *= scalar;
			mData[5] *= scalar;
		};
		void operator/=(const T &scalar) {
			mData[0] /= scalar;
			mData[1] /= scalar;
			mData[2] /= scalar;
			mData[3] /= scalar;
			mData[4] /= scalar;
			mData[5] /= scalar;
		};
		SpatialVector operator/(const T& val) {
			return SpatialVector (
					mData[0] / val,
					mData[1] / val,
					mData[2] / val,
					mData[3] / val,
					mData[4] / val,
					mData[5] / val
					);
		}

		// Operators with other spatial vectors
		SpatialVector operator+(const SpatialVector &vector) const {
			return SpatialVector(
					mData[0] + vector.mData[0],
					mData[1] + vector.mData[1],
					mData[2] + vector.mData[2],
					mData[3] + vector.mData[3],
					mData[4] + vector.mData[4],
					mData[5] + vector.mData[5]
					);
		}
		void operator+=(const SpatialVector &vector) {
			mData[0] += vector.mData[0];
			mData[1] += vector.mData[1];
			mData[2] += vector.mData[2];
			mData[3] += vector.mData[3];
			mData[4] += vector.mData[4];
			mData[5] += vector.mData[5];
		}

		SpatialVector operator-(const SpatialVector &vector) const {
			return SpatialVector(
					mData[0] - vector.mData[0],
					mData[1] - vector.mData[1],
					mData[2] - vector.mData[2],
					mData[3] - vector.mData[3],
					mData[4] - vector.mData[4],
					mData[5] - vector.mData[5]
					);
		}
		void operator-=(const SpatialVector &vector) {
			mData[0] -= vector.mData[0];
			mData[1] -= vector.mData[1];
			mData[2] -= vector.mData[2];
			mData[3] -= vector.mData[3];
			mData[4] -= vector.mData[4];
			mData[5] -= vector.mData[5];
		}
		T operator*(const SpatialVector &vector) const {
			return mData[0] * vector.mData[0]
				+ mData[1] * vector.mData[1] 
				+ mData[2] * vector.mData[2] 
				+ mData[3] * vector.mData[3] 
				+ mData[4] * vector.mData[4] 
				+ mData[5] * vector.mData[5];
		}
		inline SpatialMatrix<T> outer_product(const SpatialVector<T> &vector) const;

		inline SpatialMatrix<T> crossm() const;
		inline SpatialMatrix<T> crossf() const;
			
	private:
		T mData[6];
};

/** \brief Matrix class for spatial matrices (both spatial transformations and inertias)
 */
template <class T>
class SpatialMatrix {
	public:
		SpatialMatrix() {};
		SpatialMatrix(const SpatialMatrix &matrix) {
			unsigned int i;
			for (i = 0; i < 36; i++)
				mData[i] = matrix.mData[i];
		};
		SpatialMatrix& operator=(const SpatialMatrix &matrix) {
			if (this != &matrix) {
				unsigned int i;
				for (i = 0; i < 36; i++)
					mData[i] = matrix.mData[i];
			}
			return *this;
		};
		~SpatialMatrix() {};

		SpatialMatrix (
				const T &v00, const T &v01, const T &v02,
				const T &v03, const T &v04, const T &v05,

				const T &v10, const T &v11, const T &v12,
				const T &v13, const T &v14, const T &v15,

				const T &v20, const T &v21, const T &v22,
				const T &v23, const T &v24, const T &v25,

				const T &v30, const T &v31, const T &v32,
				const T &v33, const T &v34, const T &v35,

				const T &v40, const T &v41, const T &v42,
				const T &v43, const T &v44, const T &v45,

				const T &v50, const T &v51, const T &v52,
				const T &v53, const T &v54, const T &v55
				) {
			mData[0] = v00;
			mData[1] = v01;
			mData[2] = v02;
			mData[3] = v03;
			mData[4] = v04;
			mData[5] = v05;

			mData[6 + 0] = v10;
			mData[6 + 1] = v11;
			mData[6 + 2] = v12;
			mData[6 + 3] = v13;
			mData[6 + 4] = v14;
			mData[6 + 5] = v15;

			mData[12 + 0] = v20;
			mData[12 + 1] = v21;
			mData[12 + 2] = v22;
			mData[12 + 3] = v23;
			mData[12 + 4] = v24;
			mData[12 + 5] = v25;

			mData[18 + 0] = v30;
			mData[18 + 1] = v31;
			mData[18 + 2] = v32;
			mData[18 + 3] = v33;
			mData[18 + 4] = v34;
			mData[18 + 5] = v35;

			mData[24 + 0] = v40;
			mData[24 + 1] = v41;
			mData[24 + 2] = v42;
			mData[24 + 3] = v43;
			mData[24 + 4] = v44;
			mData[24 + 5] = v45;

			mData[30 + 0] = v50;
			mData[30 + 1] = v51;
			mData[30 + 2] = v52;
			mData[30 + 3] = v53;
			mData[30 + 4] = v54;
			mData[30 + 5] = v55;
		};
		SpatialMatrix(const Matrix3d &upper_left,
				const Matrix3d &upper_right,
				const Matrix3d &lower_left,
				const Matrix3d &lower_right) {
			// upper left
			mData[0] = upper_left(0,0);
			mData[1] = upper_left(0,1);
			mData[2] = upper_left(0,2);

			mData[6 + 0] = upper_left(1,0);
			mData[6 + 1] = upper_left(1,1);
			mData[6 + 2] = upper_left(1,2);
			
			mData[12 + 0] = upper_left(2,0);
			mData[12 + 1] = upper_left(2,1);
			mData[12 + 2] = upper_left(2,2);

			// upper right 
			mData[3] = upper_right(0,0);
			mData[4] = upper_right(0,1);
			mData[5] = upper_right(0,2);

			mData[6 + 3] = upper_right(1,0);
			mData[6 + 4] = upper_right(1,1);
			mData[6 + 5] = upper_right(1,2);
			
			mData[12 + 3] = upper_right(2,0);
			mData[12 + 4] = upper_right(2,1);
			mData[12 + 5] = upper_right(2,2);

			// lower left
			mData[18 + 0] = lower_left(0,0);
			mData[18 + 1] = lower_left(0,1);
			mData[18 + 2] = lower_left(0,2);

			mData[24 + 0] = lower_left(1,0);
			mData[24 + 1] = lower_left(1,1);
			mData[24 + 2] = lower_left(1,2);
			
			mData[30 + 0] = lower_left(2,0);
			mData[30 + 1] = lower_left(2,1);
			mData[30 + 2] = lower_left(2,2);

			// lower right 
			mData[18 + 3] = lower_right(0,0);
			mData[18 + 4] = lower_right(0,1);
			mData[18 + 5] = lower_right(0,2);

			mData[24 + 3] = lower_right(1,0);
			mData[24 + 4] = lower_right(1,1);
			mData[24 + 5] = lower_right(1,2);
			
			mData[30 + 3] = lower_right(2,0);
			mData[30 + 4] = lower_right(2,1);
			mData[30 + 5] = lower_right(2,2);

		}

		// comparison
		bool operator==(const SpatialMatrix &matrix) const {
			for (unsigned int i = 0; i < 36; i++) {
				if (mData[i] != matrix.mData[i])
					return false;
			}
			return true;
		}

		// access operators
		const T& operator[](const unsigned int &index) const {
			assert (index	>= 0 && index < 36);
			return mData[index];
		};
		T& operator[](const unsigned int &index) {
			assert (index	>= 0 && index < 36);
			return mData[index];
		}

		const T& operator()(const unsigned int &row, const unsigned int &col) const {
			assert (row	>= 0 && row < 6 && row	>= 0 && row < 6);
			return mData[row*6 + col];
		};
		T& operator()(const unsigned int &row, const unsigned int &col) {
			assert (row	>= 0 && row < 6 && row	>= 0 && row < 6);
			return mData[row*6 + col];
		};
		
		void set(
				const T v00, const T v01, const T v02,
				const T v03, const T v04, const T v05,

				const T v10, const T v11, const T v12,
				const T v13, const T v14, const T v15,

				const T v20, const T v21, const T v22,
				const T v23, const T v24, const T v25,

				const T v30, const T v31, const T v32,
				const T v33, const T v34, const T v35,

				const T v40, const T v41, const T v42,
				const T v43, const T v44, const T v45,

				const T v50, const T v51, const T v52,
				const T v53, const T v54, const T v55
				) {
			mData[0] = v00;
			mData[1] = v01;
			mData[2] = v02;
			mData[3] = v03;
			mData[4] = v04;
			mData[5] = v05;

			mData[6 + 0] = v10;
			mData[6 + 1] = v11;
			mData[6 + 2] = v12;
			mData[6 + 3] = v13;
			mData[6 + 4] = v14;
			mData[6 + 5] = v15;

			mData[12 + 0] = v20;
			mData[12 + 1] = v21;
			mData[12 + 2] = v22;
			mData[12 + 3] = v23;
			mData[12 + 4] = v24;
			mData[12 + 5] = v25;

			mData[18 + 0] = v30;
			mData[18 + 1] = v31;
			mData[18 + 2] = v32;
			mData[18 + 3] = v33;
			mData[18 + 4] = v34;
			mData[18 + 5] = v35;

			mData[24 + 0] = v40;
			mData[24 + 1] = v41;
			mData[24 + 2] = v42;
			mData[24 + 3] = v43;
			mData[24 + 4] = v44;
			mData[24 + 5] = v45;

			mData[30 + 0] = v50;
			mData[30 + 1] = v51;
			mData[30 + 2] = v52;
			mData[30 + 3] = v53;
			mData[30 + 4] = v54;
			mData[30 + 5] = v55;
		}

		void zero() {
			set(
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.
				 );
		}

		void identity() {
			set(
					1., 0., 0., 0., 0., 0.,
					0., 1., 0., 0., 0., 0.,
					0., 0., 1., 0., 0., 0.,
					0., 0., 0., 1., 0., 0.,
					0., 0., 0., 0., 1., 0.,
					0., 0., 0., 0., 0., 1.
				 );
		}

		// Operators with scalars
		SpatialMatrix operator*(const T &scalar) const {
			return SpatialMatrix(
					mData[0] * scalar,
					mData[1] * scalar,
					mData[2] * scalar,
					mData[3] * scalar,
					mData[4] * scalar,
					mData[5] * scalar,

					mData[6 + 0] * scalar,
					mData[6 + 1] * scalar,
					mData[6 + 2] * scalar,
					mData[6 + 3] * scalar,
					mData[6 + 4] * scalar,
					mData[6 + 5] * scalar,

					mData[12 + 0] * scalar,
					mData[12 + 1] * scalar,
					mData[12 + 2] * scalar,
					mData[12 + 3] * scalar,
					mData[12 + 4] * scalar,
					mData[12 + 5] * scalar,

					mData[18 + 0] * scalar,
					mData[18 + 1] * scalar,
					mData[18 + 2] * scalar,
					mData[18 + 3] * scalar,
					mData[18 + 4] * scalar,
					mData[18 + 5] * scalar,

					mData[24 + 0] * scalar,
					mData[24 + 1] * scalar,
					mData[24 + 2] * scalar,
					mData[24 + 3] * scalar,
					mData[24 + 4] * scalar,
					mData[24 + 5] * scalar,

					mData[30 + 0] * scalar,
					mData[30 + 1] * scalar,
					mData[30 + 2] * scalar,
					mData[30 + 3] * scalar,
					mData[30 + 4] * scalar,
					mData[30 + 5] * scalar
					);
		}
		void operator*=(const T &scalar) {
			for (unsigned int i = 0; i < 36; i++)
				mData[i] *= scalar;
		};
		void operator/=(const T &scalar) {
			for (unsigned int i = 0; i < 36; i++)
				mData[i] /= scalar;
		}
		SpatialMatrix operator/(const T& val) const {
			return SpatialMatrix (
					mData[0 * 6 + 0] / val,
					mData[0 * 6 + 1] / val,
					mData[0 * 6 + 2] / val,
					mData[0 * 6 + 3] / val,
					mData[0 * 6 + 4] / val,
					mData[0 * 6 + 5] / val,

					mData[1 * 6 + 0] / val,
					mData[1 * 6 + 1] / val,
					mData[1 * 6 + 2] / val,
					mData[1 * 6 + 3] / val,
					mData[1 * 6 + 4] / val,
					mData[1 * 6 + 5] / val,

					mData[2 * 6 + 0] / val,
					mData[2 * 6 + 1] / val,
					mData[2 * 6 + 2] / val,
					mData[2 * 6 + 3] / val,
					mData[2 * 6 + 4] / val,
					mData[2 * 6 + 5] / val,

					mData[3 * 6 + 0] / val,
					mData[3 * 6 + 1] / val,
					mData[3 * 6 + 2] / val,
					mData[3 * 6 + 3] / val,
					mData[3 * 6 + 4] / val,
					mData[3 * 6 + 5] / val,

					mData[4 * 6 + 0] / val,
					mData[4 * 6 + 1] / val,
					mData[4 * 6 + 2] / val,
					mData[4 * 6 + 3] / val,
					mData[4 * 6 + 4] / val,
					mData[4 * 6 + 5] / val,

					mData[5 * 6 + 0] / val,
					mData[5 * 6 + 1] / val,
					mData[52] / val,
					mData[53] / val,
					mData[54] / val,
					mData[55] / val
						);
		}

		// Operators with other spatial matrices
		SpatialMatrix operator+(const SpatialMatrix &matrix) const {
			return SpatialMatrix(
					mData[0] + matrix.mData[0],
					mData[1] + matrix.mData[1],
					mData[2] + matrix.mData[2],
					mData[3] + matrix.mData[3],
					mData[4] + matrix.mData[4],
					mData[5] + matrix.mData[5],

					mData[6 + 0] + matrix.mData[6 + 0],
					mData[6 + 1] + matrix.mData[6 + 1],
					mData[6 + 2] + matrix.mData[6 + 2],
					mData[6 + 3] + matrix.mData[6 + 3],
					mData[6 + 4] + matrix.mData[6 + 4],
					mData[6 + 5] + matrix.mData[6 + 5],

					mData[12 + 0] + matrix.mData[12 + 0],
					mData[12 + 1] + matrix.mData[12 + 1],
					mData[12 + 2] + matrix.mData[12 + 2],
					mData[12 + 3] + matrix.mData[12 + 3],
					mData[12 + 4] + matrix.mData[12 + 4],
					mData[12 + 5] + matrix.mData[12 + 5],

					mData[18 + 0] + matrix.mData[18 + 0],
					mData[18 + 1] + matrix.mData[18 + 1],
					mData[18 + 2] + matrix.mData[18 + 2],
					mData[18 + 3] + matrix.mData[18 + 3],
					mData[18 + 4] + matrix.mData[18 + 4],
					mData[18 + 5] + matrix.mData[18 + 5],

					mData[24 + 0] + matrix.mData[24 + 0],
					mData[24 + 1] + matrix.mData[24 + 1],
					mData[24 + 2] + matrix.mData[24 + 2],
					mData[24 + 3] + matrix.mData[24 + 3],
					mData[24 + 4] + matrix.mData[24 + 4],
					mData[24 + 5] + matrix.mData[24 + 5],

					mData[30 + 0] + matrix.mData[30 + 0],
					mData[30 + 1] + matrix.mData[30 + 1],
					mData[30 + 2] + matrix.mData[30 + 2],
					mData[30 + 3] + matrix.mData[30 + 3],
					mData[30 + 4] + matrix.mData[30 + 4],
					mData[30 + 5] + matrix.mData[30 + 5]
					);
		}
		void operator+=(const SpatialMatrix &matrix) {
			for (unsigned int i = 0; i < 36; i++)
				mData[i] += matrix.mData[i];
		}
		SpatialMatrix operator-(const SpatialMatrix &matrix) const {
			return SpatialMatrix(
					mData[0] - matrix.mData[0],
					mData[1] - matrix.mData[1],
					mData[2] - matrix.mData[2],
					mData[3] - matrix.mData[3],
					mData[4] - matrix.mData[4],
					mData[5] - matrix.mData[5],

					mData[6 + 0] - matrix.mData[6 + 0],
					mData[6 + 1] - matrix.mData[6 + 1],
					mData[6 + 2] - matrix.mData[6 + 2],
					mData[6 + 3] - matrix.mData[6 + 3],
					mData[6 + 4] - matrix.mData[6 + 4],
					mData[6 + 5] - matrix.mData[6 + 5],

					mData[12 + 0] - matrix.mData[12 + 0],
					mData[12 + 1] - matrix.mData[12 + 1],
					mData[12 + 2] - matrix.mData[12 + 2],
					mData[12 + 3] - matrix.mData[12 + 3],
					mData[12 + 4] - matrix.mData[12 + 4],
					mData[12 + 5] - matrix.mData[12 + 5],

					mData[18 + 0] - matrix.mData[18 + 0],
					mData[18 + 1] - matrix.mData[18 + 1],
					mData[18 + 2] - matrix.mData[18 + 2],
					mData[18 + 3] - matrix.mData[18 + 3],
					mData[18 + 4] - matrix.mData[18 + 4],
					mData[18 + 5] - matrix.mData[18 + 5],

					mData[24 + 0] - matrix.mData[24 + 0],
					mData[24 + 1] - matrix.mData[24 + 1],
					mData[24 + 2] - matrix.mData[24 + 2],
					mData[24 + 3] - matrix.mData[24 + 3],
					mData[24 + 4] - matrix.mData[24 + 4],
					mData[24 + 5] - matrix.mData[24 + 5],

					mData[30 + 0] - matrix.mData[30 + 0],
					mData[30 + 1] - matrix.mData[30 + 1],
					mData[30 + 2] - matrix.mData[30 + 2],
					mData[30 + 3] - matrix.mData[30 + 3],
					mData[30 + 4] - matrix.mData[30 + 4],
					mData[30 + 5] - matrix.mData[30 + 5]
					);
		}
		void operator-=(const SpatialMatrix &matrix) {
			for (unsigned int i = 0; i < 36; i++)
				mData[i] -= matrix.mData[i];
		}
		SpatialMatrix operator*(const SpatialMatrix &matrix) {
			SpatialMatrix result (
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.,
					0., 0., 0., 0., 0., 0.
					);

			unsigned int i,j, k;
			for (i = 0; i < 6; i++) {
				for (j = 0; j < 6; j++) {
					for (k = 0; k < 6; k++) {
						result(i,j) += mData[i * 6 + k] * matrix.mData[k * 6 + j];
					}
				}
			}
				
			return result;
		}

		// Operators with SpatialVectors
		SpatialVector<T> operator*(const SpatialVector<T> &vector) const {
			SpatialVector<T> result (
					0., 0., 0., 0., 0., 0.
					);

			unsigned i,j;
			for (i = 0; i < 6; i++) {
				for (j = 0; j < 6; j++) {
					result[i] += mData[i * 6 + j] * vector[j];
				}
			}

			return result;
		}

		// Special operators
		
		// regular transpose of a 6 dimensional matrix
		SpatialMatrix transpose() const {
			return SpatialMatrix (
					mData[0 * 6 + 0], mData[1 * 6 + 0], mData[2 * 6 + 0], mData[3 * 6 + 0], mData[4 * 6 + 0], mData[5 * 6 + 0],
					mData[0 * 6 + 1], mData[1 * 6 + 1], mData[2 * 6 + 1], mData[3 * 6 + 1], mData[4 * 6 + 1], mData[5 * 6 + 1],
					mData[0 * 6 + 2], mData[1 * 6 + 2], mData[2 * 6 + 2], mData[3 * 6 + 2], mData[4 * 6 + 2], mData[5 * 6 + 2],
					mData[0 * 6 + 3], mData[1 * 6 + 3], mData[2 * 6 + 3], mData[3 * 6 + 3], mData[4 * 6 + 3], mData[5 * 6 + 3],
					mData[0 * 6 + 4], mData[1 * 6 + 4], mData[2 * 6 + 4], mData[3 * 6 + 4], mData[4 * 6 + 4], mData[5 * 6 + 4],
					mData[0 * 6 + 5], mData[1 * 6 + 5], mData[2 * 6 + 5], mData[3 * 6 + 5], mData[4 * 6 + 5], mData[5 * 6 + 5]
					);
		}

		/** \brief Computes the adjoint matrix of a spatial transformation
		 *
		 * For a given transformation {}^BX_A it computes {}^B^*_{X} as in RBDA
		 * p. 22 by swapping the lower left with the upper right 3x3 matrix.
		 */
		SpatialMatrix adjoint() const {
			SpatialMatrix result (*this);

			// swap lower left with upper right
			Matrix3d upper_right = get_upper_right();
			unsigned int i;
			unsigned int j;
			for (i = 0; i < 3; i++) {
				for (j = 0; j < 3; j++) {
					result(i, j + 3) = this->operator()(i+3, j);
					result(i+3,j) = upper_right (i,j);
				}
			}

			return result;
		}

		/** \brief Returns the inverse of a transformation 
		 *
		 * Each of the four 3x3 blocks is transposed.
		 */
		SpatialMatrix inverse() const {
				return SpatialMatrix (
					mData[0 * 6 + 0], mData[1 * 6 + 0], mData[2 * 6 + 0], mData[0 * 6 + 3], mData[1 * 6 + 3], mData[2 * 6 + 3],
					mData[0 * 6 + 1], mData[1 * 6 + 1], mData[2 * 6 + 1], mData[0 * 6 + 4], mData[1 * 6 + 4], mData[2 * 6 + 4],
					mData[0 * 6 + 2], mData[1 * 6 + 2], mData[2 * 6 + 2], mData[0 * 6 + 5], mData[1 * 6 + 5], mData[2 * 6 + 5],

					mData[3 * 6 + 0], mData[4 * 6 + 0], mData[5 * 6 + 0], mData[3 * 6 + 3], mData[4 * 6 + 3], mData[5 * 6 + 3],
					mData[3 * 6 + 1], mData[4 * 6 + 1], mData[5 * 6 + 1], mData[3 * 6 + 4], mData[4 * 6 + 4], mData[5 * 6 + 4],
					mData[3 * 6 + 2], mData[4 * 6 + 2], mData[5 * 6 + 2], mData[3 * 6 + 5], mData[4 * 6 + 5], mData[5 * 6 + 5]
					);
		}

		/** \brief Returns the upper left 3x3 matrix of the spatial matrix
		 */
		Matrix3d get_upper_left() const {
			return Matrix3d (
					mData[0],  mData[1],  mData[2],
					mData[6],  mData[7],  mData[8],
					mData[12], mData[13], mData[14]
					);
		}

		/** \brief Returns the upper right 3x3 matrix of the spatial matrix
		 */
		Matrix3d get_upper_right() const {
			return Matrix3d (
					mData[3],  mData[4],  mData[5],
					mData[9],  mData[10],  mData[11],
					mData[15], mData[16],  mData[17]
					);
		}

		/** \brief Returns the lower left 3x3 matrix of the spatial matrix
		 */
		Matrix3d get_lower_left() const {
			return Matrix3d (
					mData[18],  mData[19],  mData[20],
					mData[24],  mData[25],  mData[26],
					mData[30],  mData[31],  mData[32]
					);
		}

		/** \brief Returns the lower right 3x3 matrix of the spatial matrix
		 */
		Matrix3d get_lower_right() const {
			return Matrix3d (
					mData[21],  mData[22],  mData[23],
					mData[27],  mData[28],  mData[29],
					mData[33],  mData[34],  mData[35]
					);
		}

		/** \brief Returns the rotation part of the transformation (top left part
		 *
		 */
		Matrix3d get_rotation() const {
			return Matrix3d (
					mData[0],  mData[1],  mData[2],
					mData[6],  mData[7],  mData[8],
					mData[12], mData[13], mData[14]
					);
		}

		/** \brief Returns the translation that is described by the bottom left part
		 *
		 * \todo How can we make sure the matrix is a motion
		 * \todo transformation matrix?
		 *
		 * This computes the r vector from the matrix
		 *
		 *   ( R   0 )
		 *   ( rx  R )
		 *
		 * \note Please note that this vector is already oriented to the local space. If
		 * \note one wants to compute the origin of the global body frame one has to
		 * \note compute: - R^T * r
		 */
		Vector3d get_translation() const {
			return Vector3d (
					-mData[26], mData[20], -mData[19]
					);
		}

	private:
		T mData[36];
};

template <class T>
inline std::ostream& operator<<(std::ostream& output, const SpatialVector<T> &vector) {
	output << vector[0] << " " << vector[1] << " " << vector[2] << " "
		<< vector[3] << " " << vector[4] << " " << vector[5];
	return output;
}

template <class T>
inline std::ostream& operator<<(std::ostream& output, const SpatialMatrix<T> &matrix) {
	output << std::endl;
	output << "[ " <<matrix(0,0) << " " << matrix(0,1) << " " << matrix(0,2) << " "
		<< matrix(0,3) << " " << matrix(0,4) << " " << matrix(0,5) << " ]" << std::endl;

	output << "[ " <<matrix(1,0) << " " << matrix(1,1) << " " << matrix(1,2) << " "
		<< matrix(1,3) << " " << matrix(1,4) << " " << matrix(1,5) << " ]" << std::endl;

	output << "[ " <<matrix(2,0) << " " << matrix(2,1) << " " << matrix(2,2) << " "
		<< matrix(2,3) << " " << matrix(2,4) << " " << matrix(2,5) << " ]" << std::endl;

	output << "[ " <<matrix(3,0) << " " << matrix(3,1) << " " << matrix(3,2) << " "
		<< matrix(3,3) << " " << matrix(3,4) << " " << matrix(3,5) << " ]" << std::endl;

	output << "[ " <<matrix(4,0) << " " << matrix(4,1) << " " << matrix(4,2) << " "
		<< matrix(4,3) << " " << matrix(4,4) << " " << matrix(4,5) << " ]" << std::endl;

	output << "[ " <<matrix(5,0) << " " << matrix(5,1) << " " << matrix(5,2) << " "
		<< matrix(5,3) << " " << matrix(5,4) << " " << matrix(5,5) << " ]" << std::endl;

	return output;
}

template <class T>
inline SpatialVector<T> operator*(const T& val, const SpatialVector<T> &vec) {
	return SpatialVector<T> (
			vec[0] * val,
			vec[1] * val,
			vec[2] * val,
			vec[3] * val,
			vec[4] * val,
			vec[5] * val
			);
}

template <class T>
inline SpatialMatrix<T> operator*(const T& val, const SpatialMatrix<T> &mat) {
	return SpatialMatrix<T> (
			mat(0,0) * val,
			mat(0,1) * val,
			mat(0,2) * val,
			mat(0,3) * val,
			mat(0,4) * val,
			mat(0,5) * val,

			mat(1,0) * val,
			mat(1,1) * val,
			mat(1,2) * val,
			mat(1,3) * val,
			mat(1,4) * val,
			mat(1,5) * val,

			mat(2,0) * val,
			mat(2,1) * val,
			mat(2,2) * val,
			mat(2,3) * val,
			mat(2,4) * val,
			mat(2,5) * val,

			mat(3,0) * val,
			mat(3,1) * val,
			mat(3,2) * val,
			mat(3,3) * val,
			mat(3,4) * val,
			mat(3,5) * val,

			mat(4,0) * val,
			mat(4,1) * val,
			mat(4,2) * val,
			mat(4,3) * val,
			mat(4,4) * val,
			mat(4,5) * val,

			mat(5,0) * val,
			mat(5,1) * val,
			mat(5,2) * val,
			mat(5,3) * val,
			mat(5,4) * val,
			mat(5,5) * val
			);
}

// Functions of SpatialVector that are dependent on the declaration of
// SpatialMatrix.
template <class T>
SpatialMatrix<T> SpatialVector<T>::crossm() const
{
	return SpatialMatrix<T> (
		        0,  -mData[2],  mData[1],         0,          0,         0,
		 mData[2],          0, -mData[0],         0,          0,         0, 
		-mData[1],   mData[0],         0,         0,          0,         0,
		        0,  -mData[5],  mData[4],         0,  -mData[2],  mData[1],
		 mData[5],          0, -mData[3],  mData[2],          0, -mData[0],
		-mData[4],   mData[3],         0, -mData[1],   mData[0],         0
		);
}

// Functions of SpatialVector that are dependent on the declaration of
// SpatialMatrix.
template <class T>
SpatialMatrix<T> SpatialVector<T>::crossf() const
{
	return this->crossm().adjoint();
}

template <class T>
inline SpatialMatrix<T> SpatialVector<T>::outer_product(const SpatialVector<T> &vec) const {
	return SpatialMatrix<T> (
			mData[0] * vec[0], mData[0] * vec[1], mData[0] * vec[2], mData[0] * vec[3], mData[0] * vec[4], mData[0] * vec[5],
			mData[1] * vec[0], mData[1] * vec[1], mData[1] * vec[2], mData[1] * vec[3], mData[1] * vec[4], mData[1] * vec[5],
			mData[2] * vec[0], mData[2] * vec[1], mData[2] * vec[2], mData[2] * vec[3], mData[2] * vec[4], mData[2] * vec[5],
			mData[3] * vec[0], mData[3] * vec[1], mData[3] * vec[2], mData[3] * vec[3], mData[3] * vec[4], mData[3] * vec[5],
			mData[4] * vec[0], mData[4] * vec[1], mData[4] * vec[2], mData[4] * vec[3], mData[4] * vec[4], mData[4] * vec[5],
			mData[5] * vec[0], mData[5] * vec[1], mData[5] * vec[2], mData[5] * vec[3], mData[5] * vec[4], mData[5] * vec[5]
			);
}

/** \brief Solves a 6D linear system of equations by using elimination of variables
 *
 * Computes the solution x of a 6D system of linear equations of the form Ax = b.
 *
 * \param A Spatial matrix that describes the left hand matrix
 * \param b Spatial vector that is the right hand side
 *
 * \returns solution x
 */
template <class T>
inline SpatialVector<T> SpatialLinSolve (SpatialMatrix<T> A, SpatialVector<T> b) {
	SpatialVector<T> x(0., 0., 0., 0., 0., 0.);
	
	int i,j;
	for (j = 0; j < 6; j++) {
		for (i = j + 1; i < 6; i++) {
			T d = A(i,j)/A(j,j);

			b[i] -= b[j] * d;

			int k;
			for (k = j; k < 6; k++) {
				A(i,k) -= A(j,k) * d;
			}
		}
	}

	for (i = 5; i >= 0; i--) {
		for (j = i + 1; j < 6; j++) {
			x[i] += A(i,j) * x(j);
		}
		x[i] = (b[i] - x[i]) / A(i,i);
	}

	return x;
}

const SpatialVector<double> SpatialVectorZero (0., 0., 0., 0., 0., 0.);

}
}

#endif /* SPATIALALGEBRA_H */