amino
Lightweight Robot Utility Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mat.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2010-2011, Georgia Tech Research Corporation
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ntd@gatech.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 
43 #ifndef AMINO_MAT_HPP
44 #define AMINO_MAT_HPP
45 
46 // in namespace amino
47 
56 template <size_t ROWS, size_t COLS = 1>
57 class Mat {
58 public:
59  double _array[ROWS*COLS];
60  Mat() {};
61  Mat( const double * src) {
62  load(src);
63  };
64  Mat( const Mat<ROWS,COLS> &src) {
65  load(&src[0]);
66  };
67  ~Mat(){};
68  void load(const double *src) {
69  aa_fcpy( _array, src, ROWS*COLS );
70  }
71  void store(double *dst) const {
72  aa_fcpy( dst, _array, ROWS*COLS );
73  }
74  double &operator()( size_t i ) const { return _array[i]; }
75  double &operator()( size_t i ) { return _array[i]; }
76  double &operator[]( size_t i ) { return _array[i]; }
77  const double &operator[]( size_t i ) const { return _array[i]; }
78  double &operator()( size_t i, size_t j ) const {
79  return AA_MATREF(_array, ROWS, i, j);
80  }
81  double &operator()( size_t i, size_t j ) {
82  return AA_MATREF(_array, ROWS, i, j);
83  }
84  size_t size() const { return ROWS*COLS; }
85  bool eq( const Mat<ROWS, COLS> &other ) {
86  return aa_veq( ROWS*COLS, &_array[0], &other[0], AA_EPSILON );
87  }
88  Mat<COLS,ROWS> transpose() const {
89  Mat<COLS,ROWS> mt;
90  aa_la_transpose2( ROWS, COLS, _array, &mt[0] );
91  }
92 
93  double norm() const { return aa_la_norm( ROWS*COLS, _array ); }
94  double dot(Mat<ROWS,COLS> &other) const {
95  return aa_la_dot( ROWS*COLS, _array, other );
96  }
97  double ssd(Mat<ROWS,COLS> &other) const {
98  return aa_la_ssd( ROWS*COLS, _array, other );
99  }
100  double dist(Mat<ROWS,COLS> &other) const {
101  return aa_la_dist( ROWS*COLS, _array, other );
102  }
103 };
104 
105 
106 template <size_t ROWS >
107 class Vec : public Mat<ROWS,1> {
108 public:
109  Vec() {};
110  Vec(const Mat<ROWS,1> &other) :
111  Mat<ROWS,1>(other)
112  {};
113  Vec( const double * src) : Mat<ROWS,1>(src) {};
114  ~Vec(){};
115  Vec<ROWS> operator=( const Mat<ROWS,1> &other ) {
116  aa_fcpy( &this->_array[0], &other[0], ROWS );
117  return *this;
118  }
119 };
120 
121 // Matrix Operators
122 template <size_t ROWS, size_t COLS>
123 static inline Mat<ROWS,COLS> operator*( double alpha,
124  const Mat<ROWS,COLS> b ) {
125  Mat<ROWS, COLS> c;
126  aa_la_smul( ROWS*COLS, alpha, &b[0], &c[0] );
127  return c;
128 }
129 template <size_t ROWS, size_t COLS>
130 static inline Mat<ROWS,COLS> operator*( const Mat<ROWS,COLS> b ,
131  double alpha ) {
132  return alpha * b;
133 }
134 template <size_t ROWS, size_t COLS>
135 static inline Mat<ROWS,COLS> operator/( const Mat<ROWS,COLS> b ,
136  double alpha ) {
137  return 1.0/alpha * b;
138 }
139 template <size_t ROWS, size_t COLS>
140 static inline Mat<ROWS,COLS> operator/( double alpha, const Mat<ROWS,COLS> b ) {
141  Mat<ROWS, COLS> c;
142  aa_la_sdiv( ROWS*COLS, alpha, &b[0], &c[0] );
143  return c;
144 }
145 
146 
147 template <size_t ROWS, size_t COLS>
148 static inline Mat<ROWS,COLS> operator+( const Mat<ROWS,COLS> a,
149  const Mat<ROWS,COLS> b ) {
150  Mat<ROWS, COLS> c;
151  aa_la_vadd( ROWS*COLS, &a[0], &b[0], &c[0] );
152  return c;
153 }
154 
155 template <size_t ROWS, size_t COLS>
156 static inline Mat<ROWS,COLS> operator-( const Mat<ROWS,COLS> a,
157  const Mat<ROWS,COLS> b ) {
158  Mat<ROWS, COLS> c;
159  aa_la_vsub( ROWS*COLS, &a[0], &b[0], &c[0] );
160  return c;
161 }
162 
163 
164 class Tf {
165 public:
166  double _array[12];
167  Tf() { load(AA_TF_IDENT); }
168  Tf(const double src[12] ) { load(src); }
169  Tf( double R11, double R12, double R13, double v1,
170  double R21, double R22, double R23, double v2,
171  double R31, double R32, double R33, double v3 ) {
172  _array[0] = R11;
173  _array[1] = R21;
174  _array[2] = R31;
175  _array[3] = R12;
176  _array[4] = R22;
177  _array[5] = R32;
178  _array[6] = R13;
179  _array[7] = R23;
180  _array[8] = R33;
181  _array[9] = v1;
182  _array[10] = v2;
183  _array[11] = v3;
184  }
185  Tf(const Tf &other) { load(other._array); }
186  ~Tf() {};
187 
188  double &operator[]( size_t i ) { return _array[i]; }
189  const double &operator[]( size_t i ) const { return _array[i]; }
190 
192  void load(const double src[12]) {
193  aa_fcpy( _array, src, 12 );
194  }
195  void load_rotmat(const double src[9]) {
196  aa_fcpy( _array, src, 9 );
197  }
198  void load_vec(const double src[3]) {
199  aa_fcpy( _array+9, src, 3 );
200  }
201  void store(double dst[12]) const {
202  aa_fcpy( dst, _array, 12 );
203  }
204  void store_rotmat(double dst[9]) const {
205  aa_fcpy( dst, _array, 9 );
206  }
207  void store_vec(double dst[3]) const {
208  aa_fcpy( dst, _array+9, 3 );
209  }
210 
211  Tf inv() const {
212  Tf tfi;
213  aa_tf_12inv( _array, &tfi[0] );
214  return tfi;
215  }
216 
217 };
218 
219 
220 static inline Mat<3> operator*( const Tf tf,
221  const Mat<3> v ) {
222  Mat<3> v1;
223  aa_tf_12(tf._array, &v[0], &v1[0]);
224  return v1;
225 }
226 
227 
228 static inline Tf operator*( const Tf tf1,
229  const Tf tf2 ) {
230  Tf tf;
231  aa_tf_12chain(&tf1[0], &tf2[0], &tf[0]);
232  return tf;
233 }
234 
235 
236 
237 #endif //AMINO_MAT_HPP
AA_API double aa_la_norm(size_t n, const double *x)
Euclidean norm of x.
static void aa_fcpy(double *dst, const double *src, size_t n) AA_DEPRECATED
copy n double floats from src to dst
Definition: mem.h:533
#define AA_MATREF(A, lda, row, col)
Reference an element in a column-major matrix.
Definition: math.h:347
AA_API void aa_la_vsub(size_t n, const double *x, const double *y, double *r)
Elementwise subtraction.
void load(const double src[12])
load store ops
Definition: mat.hpp:192
AA_API double aa_la_ssd(size_t n, const double *x, const double *y)
Sum of Squared Differences.
AA_API void aa_la_vadd(size_t n, const double *x, const double *y, double *r)
Elementwise addition.
AA_API double aa_la_dist(size_t n, const double *x, const double *y)
Euclidean Distance.
AA_API int aa_veq(size_t n, const double *a, const double *b, double tol)
Fuzzy equals.
AA_API void aa_la_transpose2(size_t m, size_t n, const double *A, double *At)
transpose m*n matrix A into n*m matrix At
Definition: mat.hpp:107
AA_API void aa_tf_12(const double T[AA_RESTRICT 12], const double p0[AA_RESTRICT 3], double p1[AA_RESTRICT 3])
apply a euclidean transform
#define AA_TF_IDENT
Identity transform.
Definition: tf.h:425
Definition: mat.hpp:57
AA_API double aa_la_dot(size_t n, const double *x, const double *y)
Dot product.
AA_API void aa_tf_12chain(const double T1[AA_RESTRICT 12], const double T2[AA_RESTRICT 12], double T[AA_RESTRICT 12])
chain two transforms
AA_API void aa_la_sdiv(size_t n, double alpha, const double *x, double *r)
vector-scalar division.
AA_API void aa_la_smul(size_t n, double alpha, const double *x, double *r)
vector-scalar multiplication.
Definition: mat.hpp:164
#define AA_EPSILON
a small number
Definition: math.h:59
AA_API void aa_tf_12inv(const double T[AA_RESTRICT 12], double Ti[AA_RESTRICT 12])
invert transform