cheshirekow  v0.1.0
cross_matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of fiber.
5  *
6  * fiber is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * fiber is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with fiber. If not, see <http://www.gnu.org/licenses/>.
18  */
25 #ifndef FIBER_CROSS_MATRIX_H_
26 #define FIBER_CROSS_MATRIX_H_
27 
28 
29 namespace fiber {
30 namespace cross_matrix {
31 
33 const int kIDX[9] = {
34  0, 2, 1,
35  2, 0, 0,
36  1, 0, 0
37 };
38 
40 const int kSIGN[9] = {
41  0, -1, 1,
42  1, 0, -1,
43  -1, 1, 0
44 };
45 } // namespace cross_matrix
46 
48 
57 template <typename Scalar, class Exp>
58 class _CrossMatrix : public _RValue<Scalar, _CrossMatrix<Scalar, Exp> > {
59  private:
60  Exp const& v_;
61 
62  public:
63  enum {
64  ROWS_ = 3,
65  COLS_ = 3,
66  SIZE_ = 9
67  };
68 
69  _CrossMatrix(Exp const& A) : v_(A) {
70  static_assert(Exp::SIZE_ == 3,
71  "Cross-product matrices are only defined for size = 3"
72  "vectors");
73  }
74 
75  Size size() const { return 9; }
76  Size rows() const { return 3; }
77  Size cols() const { return 3; }
78 
80  Scalar operator[](Size i) const {
81  return (*this)(i / 3, i % 3);
82  }
83 
85  Scalar operator()(Size i, Size j) const {
86  assert(0 <= i && i <= 2);
87  assert(0 <= j && j <= 2);
88  return cross_matrix::kSIGN[3 * i + j] * v_[cross_matrix::kIDX[3 * i + j]];
89  }
90 };
91 
93 
100 template <typename Scalar, class Exp>
102  return _CrossMatrix<Scalar, Exp>(static_cast<Exp const&>(A));
103 }
104 
105 } // namespace fiber
106 
107 #endif // FIBER_CROSS_MATRIX_H_
_CrossMatrix< Scalar, Exp > CrossMatrix(_RValue< Scalar, Exp > const &A)
cross-product matrix of a vector
Definition: cross_matrix.h:101
Scalar operator[](Size i) const
return the evaluated i'th element of a vector expression
Definition: cross_matrix.h:80
expression template for rvalues
Definition: rvalue.h:33
Size rows() const
Definition: cross_matrix.h:76
unsigned int Size
Definition: fiber.h:32
Size size() const
Definition: cross_matrix.h:75
Exp const & v_
wrapped expression
Definition: cross_matrix.h:60
const int kIDX[9]
indices of a vector in it's cross-product representation
Definition: cross_matrix.h:33
cross-product matrix of a vector
Definition: cross_matrix.h:58
Scalar operator()(Size i, Size j) const
return the evaluated (j,i)'th element of a matrix expression
Definition: cross_matrix.h:85
const int kSIGN[9]
signs in the cross-product representation
Definition: cross_matrix.h:40
_CrossMatrix(Exp const &A)
Definition: cross_matrix.h:69
Size cols() const
Definition: cross_matrix.h:77