GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
VerticalBlockMatrix.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <gtsam/base/Matrix.h>
22 #include <gtsam/base/FastVector.h>
23 
24 namespace gtsam {
25 
26  // Forward declarations
27  class SymmetricBlockMatrix;
28 
42  class GTSAM_EXPORT VerticalBlockMatrix
43  {
44  public:
45  typedef VerticalBlockMatrix This;
46  typedef Eigen::Block<Matrix> Block;
47  typedef Eigen::Block<const Matrix> constBlock;
48 
49  protected:
50  Matrix matrix_;
52 
56 
57  public:
58 
61  rowStart_(0), rowEnd_(0), blockStart_(0)
62  {
63  variableColOffsets_.push_back(0);
64  assertInvariants();
65  }
66 
68  template<typename CONTAINER>
69  VerticalBlockMatrix(const CONTAINER& dimensions, DenseIndex height,
70  bool appendOneDimension = false) :
71  variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
72  rowStart_(0), rowEnd_(height), blockStart_(0) {
73  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
74  matrix_.resize(height, variableColOffsets_.back());
75  assertInvariants();
76  }
77 
79  template<typename CONTAINER, typename DERIVED>
80  VerticalBlockMatrix(const CONTAINER& dimensions,
81  const Eigen::MatrixBase<DERIVED>& matrix, bool appendOneDimension = false) :
82  matrix_(matrix), variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
83  rowStart_(0), rowEnd_(matrix.rows()), blockStart_(0) {
84  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
85  if (variableColOffsets_.back() != matrix_.cols())
86  throw std::invalid_argument(
87  "Requested to create a VerticalBlockMatrix with dimensions that do not sum to the total columns of the provided matrix.");
88  assertInvariants();
89  }
90 
92  template<typename ITERATOR>
93  VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
94  DenseIndex height, bool appendOneDimension = false) :
95  variableColOffsets_((lastBlockDim-firstBlockDim) + (appendOneDimension ? 2 : 1)),
96  rowStart_(0), rowEnd_(height), blockStart_(0) {
97  fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
98  matrix_.resize(height, variableColOffsets_.back());
99  assertInvariants();
100  }
101 
107  static VerticalBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& rhs);
108 
112  static VerticalBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& rhs, DenseIndex height);
113 
115  DenseIndex rows() const { assertInvariants(); return rowEnd_ - rowStart_; }
116 
118  DenseIndex cols() const { assertInvariants(); return variableColOffsets_.back() - variableColOffsets_[blockStart_]; }
119 
121  DenseIndex nBlocks() const { assertInvariants(); return variableColOffsets_.size() - 1 - blockStart_; }
122 
124  Block operator()(DenseIndex block) { return range(block, block+1); }
125 
127  const constBlock operator()(DenseIndex block) const { return range(block, block+1); }
128 
130  Block range(DenseIndex startBlock, DenseIndex endBlock) {
131  assertInvariants();
132  DenseIndex actualStartBlock = startBlock + blockStart_;
133  DenseIndex actualEndBlock = endBlock + blockStart_;
134  if(startBlock != 0 || endBlock != 0) {
135  checkBlock(actualStartBlock);
136  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
137  }
138  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
139  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
140  return matrix_.block(rowStart_, startCol, this->rows(), rangeCols);
141  }
142 
143  const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const {
144  assertInvariants();
145  DenseIndex actualStartBlock = startBlock + blockStart_;
146  DenseIndex actualEndBlock = endBlock + blockStart_;
147  if(startBlock != 0 || endBlock != 0) {
148  checkBlock(actualStartBlock);
149  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
150  }
151  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
152  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
153  return ((const Matrix&)matrix_).block(rowStart_, startCol, this->rows(), rangeCols);
154  }
155 
157  Block full() { return range(0, nBlocks()); }
158 
160  const constBlock full() const { return range(0, nBlocks()); }
161 
162  DenseIndex offset(DenseIndex block) const {
163  assertInvariants();
164  DenseIndex actualBlock = block + blockStart_;
165  checkBlock(actualBlock);
166  return variableColOffsets_[actualBlock];
167  }
168 
170  const DenseIndex& rowStart() const { return rowStart_; }
171 
173  DenseIndex& rowStart() { return rowStart_; }
174 
176  const DenseIndex& rowEnd() const { return rowEnd_; }
177 
179  DenseIndex& rowEnd() { return rowEnd_; }
180 
182  const DenseIndex& firstBlock() const { return blockStart_; }
183 
185  DenseIndex& firstBlock() { return blockStart_; }
186 
188  const Matrix& matrix() const { return matrix_; }
189 
191  Matrix& matrix() { return matrix_; }
192 
193  protected:
194  void assertInvariants() const {
195  assert(matrix_.cols() == variableColOffsets_.back());
196  assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
197  assert(rowStart_ <= matrix_.rows());
198  assert(rowEnd_ <= matrix_.rows());
199  assert(rowStart_ <= rowEnd_);
200  }
201 
202  void checkBlock(DenseIndex block) const {
203  static_cast<void>(block); //Disable unused varibale warnings.
204  assert(matrix_.cols() == variableColOffsets_.back());
205  assert(block < (DenseIndex)variableColOffsets_.size() - 1);
206  assert(variableColOffsets_[block] < matrix_.cols() && variableColOffsets_[block+1] <= matrix_.cols());
207  }
208 
209  template<typename ITERATOR>
210  void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension) {
211  variableColOffsets_[0] = 0;
212  DenseIndex j=0;
213  for(ITERATOR dim=firstBlockDim; dim!=lastBlockDim; ++dim, ++j)
214  variableColOffsets_[j+1] = variableColOffsets_[j] + *dim;
215  if(appendOneDimension)
216  variableColOffsets_[j+1] = variableColOffsets_[j] + 1;
217  }
218 
219  friend class SymmetricBlockMatrix;
220 
221  private:
222 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
223 
224  friend class boost::serialization::access;
225  template<class ARCHIVE>
226  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
227  ar & BOOST_SERIALIZATION_NVP(matrix_);
228  ar & BOOST_SERIALIZATION_NVP(variableColOffsets_);
229  ar & BOOST_SERIALIZATION_NVP(rowStart_);
230  ar & BOOST_SERIALIZATION_NVP(rowEnd_);
231  ar & BOOST_SERIALIZATION_NVP(blockStart_);
232  }
233 #endif
234  };
235 
236 }
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:118
VerticalBlockMatrix(const CONTAINER &dimensions, const Eigen::MatrixBase< DERIVED > &matrix, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:80
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Definition: VerticalBlockMatrix.h:42
DenseIndex nBlocks() const
Block count.
Definition: VerticalBlockMatrix.h:121
const constBlock full() const
Definition: VerticalBlockMatrix.h:160
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:115
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Matrix & matrix()
Definition: VerticalBlockMatrix.h:191
const Matrix & matrix() const
Definition: VerticalBlockMatrix.h:188
Block operator()(DenseIndex block)
Definition: VerticalBlockMatrix.h:124
VerticalBlockMatrix()
Definition: VerticalBlockMatrix.h:60
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:108
Definition: SymmetricBlockMatrix.h:53
Matrix matrix_
The full matrix.
Definition: VerticalBlockMatrix.h:50
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:93
const DenseIndex & rowEnd() const
Definition: VerticalBlockMatrix.h:176
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:53
Serialization for matrices.
Block full()
Definition: VerticalBlockMatrix.h:157
DenseIndex & firstBlock()
Definition: VerticalBlockMatrix.h:185
typedef and functions to augment Eigen&#39;s MatrixXd
Block range(DenseIndex startBlock, DenseIndex endBlock)
Definition: VerticalBlockMatrix.h:130
A thin wrapper around std::vector that uses a custom allocator.
Definition: chartTesting.h:28
const constBlock operator()(DenseIndex block) const
Definition: VerticalBlockMatrix.h:127
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition: VerticalBlockMatrix.h:51
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:69
const DenseIndex & firstBlock() const
Definition: VerticalBlockMatrix.h:182
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:55
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:54
DenseIndex & rowStart()
Definition: VerticalBlockMatrix.h:173
DenseIndex & rowEnd()
Definition: VerticalBlockMatrix.h:179
const DenseIndex & rowStart() const
Definition: VerticalBlockMatrix.h:170