SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseMatrix.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 #ifndef CORE_DATATYPES_DENSE_MATRIX_H
31 #define CORE_DATATYPES_DENSE_MATRIX_H
32 
33 #include <Core/Datatypes/Matrix.h>
34 #include <Core/GeometryPrimitives/Transform.h> /// @todo
35 #define register
36 #include <Eigen/Dense>
37 #undef register
38 
39 namespace SCIRun {
40 namespace Core {
41 namespace Datatypes {
42 
43  template <typename T>
44  class DenseMatrixGeneric : public MatrixBase<T>, public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>
45  {
46  public:
47  typedef T value_type;
49  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign> EigenBase;
50 
51  /// @todo: don't like this default ctor, since it doesn't act like a vector.
52  //DenseMatrixGeneric() : Base() {}
53  DenseMatrixGeneric(size_t nrows = 0, size_t ncols = 0) : EigenBase(nrows, ncols) {}
54 
55  DenseMatrixGeneric(size_t nrows, size_t ncols, const T& val) : EigenBase(nrows, ncols)
56  {
57  this->fill(val);
58  }
59 
60  /// This constructor allows you to construct DenseMatrixGeneric from Eigen expressions
61  template<typename OtherDerived>
62  DenseMatrixGeneric(const Eigen::MatrixBase<OtherDerived>& other)
63  : EigenBase(other)
64  { }
65 
66  explicit DenseMatrixGeneric(const Geometry::Transform& t);
67 
68  /// This method allows you to assign Eigen expressions to DenseMatrixGeneric
69  template<typename OtherDerived>
70  DenseMatrixGeneric& operator=(const Eigen::MatrixBase<OtherDerived>& other)
71  {
72  this->EigenBase::operator=(other);
73  return *this;
74  }
75 
76  virtual DenseMatrixGeneric* clone() const
77  {
78  return new DenseMatrixGeneric(*this);
79  }
80 
81  virtual size_t nrows() const { return this->rows(); }
82  virtual size_t ncols() const { return this->cols(); }
83 
84  virtual void accept(MatrixVisitorGeneric<T>& visitor)
85  {
86  visitor.visit(*this);
87  }
88 
89  /// Persistent representation...
90  virtual std::string dynamic_type_name() const;
91  virtual void io(Piostream&);
94 
95  bool isSymmetric() const
96  {
97  if (this->nrows() != this->ncols())
98  return false;
99  return this->isApprox(this->transpose());
100  }
101 
102  virtual T get(int i, int j) const override
103  {
104  return (*this)(i,j);
105  }
106  virtual void put(int i, int j, const T& val) override
107  {
108  (*this)(i,j) = val;
109  }
110 
111  private:
112  virtual void print(std::ostream& o) const
113  {
114  /// @todo!!
115  //o << static_cast<const EigenBase&>(m);
116  for (size_t i = 0; i < nrows(); ++i)
117  {
118  for (size_t j = 0; j < ncols(); ++j)
119  {
120  o << (*this)(i,j) << " ";
121  }
122  o << "\n";
123  }
124  }
125  };
126 
127  namespace
128  {
129  template <typename T>
130  Persistent* DenseMatrixGenericMaker()
131  {
132  return new DenseMatrixGeneric<T>;
133  }
134  }
135 
136  template <typename T>
137  PersistentMaker0 DenseMatrixGeneric<T>::maker0(DenseMatrixGenericMaker<T>);
138 
139  template <typename T>
140  std::string DenseMatrixGeneric<T>::dynamic_type_name() const { return type_id.type; }
141 
142  template <typename T>
144 
145  template <typename T>
147  {
148  T data[16];
149  t.get(data);
150  T* ptr = data;
151 
152  for (int i = 0; i < this->nrows(); ++i)
153  {
154  for (int j = 0; j < this->ncols(); ++j)
155  {
156  (*this)(i,j) = static_cast<T>(*ptr++);
157  }
158  }
159  }
160 
161 }}}
162 
163 #include <Core/Datatypes/MatrixIO.h>
164 
165 #endif
virtual size_t nrows() const
Definition: DenseMatrix.h:81
virtual void io(Piostream &)
Definition: MatrixIO.h:107
bool isSymmetric() const
Definition: DenseMatrix.h:95
virtual DenseMatrixGeneric * clone() const
Definition: DenseMatrix.h:76
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > EigenBase
Definition: DenseMatrix.h:49
Definition: Persistent.h:89
virtual void accept(MatrixVisitorGeneric< T > &visitor)
Definition: DenseMatrix.h:84
void get(double *) const
Definition: Transform.cc:584
DenseMatrixGeneric(const Eigen::MatrixBase< OtherDerived > &other)
This constructor allows you to construct DenseMatrixGeneric from Eigen expressions.
Definition: DenseMatrix.h:62
Persistent *(* PersistentMaker0)()
Definition: Persistent.h:62
DenseMatrixGeneric(size_t nrows=0, size_t ncols=0)
Definition: DenseMatrix.h:53
virtual size_t ncols() const
Definition: DenseMatrix.h:82
DenseMatrixGeneric & operator=(const Eigen::MatrixBase< OtherDerived > &other)
This method allows you to assign Eigen expressions to DenseMatrixGeneric.
Definition: DenseMatrix.h:70
DenseMatrixGeneric(size_t nrows, size_t ncols, const T &val)
Definition: DenseMatrix.h:55
dictionary data
Definition: eabLatVolData.py:11
static PersistentTypeID type_id
Definition: DenseMatrix.h:92
T value_type
Definition: DenseMatrix.h:47
virtual void put(int i, int j, const T &val) override
Definition: DenseMatrix.h:106
Definition: Transform.h:53
DenseMatrixGeneric< T > this_type
Definition: DenseMatrix.h:48
virtual void visit(DenseMatrixGeneric< T > &)=0
Definition: Persistent.h:64
MatrixBase< double > Matrix
Definition: MatrixFwd.h:40
static PersistentMaker0 maker0
Definition: DenseMatrix.h:93
virtual std::string dynamic_type_name() const
Persistent representation...
Definition: DenseMatrix.h:140