cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctDynamicMatrixRef.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5  Author(s): Ofri Sadowsky, Anton Deguet
6  Created on: 2004-07-01
7 
8  (C) Copyright 2004-2015 Johns Hopkins University (JHU), All Rights Reserved.
9 
10 --- begin cisst license - do not edit ---
11 
12 This software is provided "as is" under an open source license, with
13 no warranty. The complete license can be found in license.txt and
14 http://www.cisst.org/cisst/license.txt.
15 
16 --- end cisst license ---
17 */
18 
19 #pragma once
20 #ifndef _vctDynamicMatrixRef_h
21 #define _vctDynamicMatrixRef_h
22 
30 
73 template <class _elementType>
74 class vctDynamicMatrixRef: public vctDynamicMatrixBase<vctDynamicMatrixRefOwner<_elementType>, _elementType>
75 {
76 public:
77  /* documented in base class */
78  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
79  enum {DIMENSION = 2};
81 
89 
91  SetRef(0, 0, 1, 1, 0);
92  }
93 
101  BaseType()
102  {
103  SetRef(other.sizes(), other.strides(), const_cast<pointer>(other.Pointer()));
104  }
105 
112  pointer dataPointer)
113  {
114  SetRef(rows, cols, rowStride, colStride, dataPointer);
115  }
116 
117  vctDynamicMatrixRef(const nsize_type & matrixSize, stride_type rowStride, stride_type colStride,
118  pointer dataPointer)
119  {
120  SetRef(matrixSize, rowStride, colStride, dataPointer);
121  }
122 
123  vctDynamicMatrixRef(const nsize_type & matrixSize, const nstride_type & matrixStride,
124  pointer dataPointer)
125  {
126  SetRef(matrixSize, matrixStride, dataPointer);
127  }
129 
130 
136  vctDynamicMatrixRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder = VCT_DEFAULT_STORAGE)
137  {
138  SetRef(rows, cols, dataPointer, storageOrder);
139  }
140 
141  vctDynamicMatrixRef(const nsize_type & matrixSize, pointer dataPointer, bool storageOrder = VCT_DEFAULT_STORAGE)
142  {
143  SetRef(matrixSize, dataPointer, storageOrder);
144  }
146 
147 
153  template <size_type __rows, size_type __cols, stride_type __rowStride, stride_type __colStride, typename __dataPtrType>
155  {
156  SetRef(otherMatrix);
157  }
158 
169  template <size_type __rows, size_type __cols, stride_type __rowStride, stride_type __colStride, typename __dataPtrType>
171  size_type startRow, size_type startCol, size_type rows, size_type cols)
172  {
173  SetRef(otherMatrix, startRow, startCol, rows, cols);
174  }
175 
181  template <class __ownerType>
183  {
184  SetRef(otherMatrix);
185  }
186 
197  //@
198  template <class __ownerType>
200  size_type startRow, size_type startCol, size_type rows, size_type cols)
201  {
202  SetRef(otherMatrix, startRow, startCol, rows, cols);
203  }
204 
205  template <class __ownerType>
207  const nsize_type & start, const nsize_type & matrixSize)
208  {
209  SetRef(otherMatrix, start, matrixSize);
210  }
212 
218  stride_type rowStride, stride_type colStride,
219  pointer dataPointer) {
220  this->Matrix.SetRef(rows, cols, rowStride, colStride, dataPointer);
221  }
222 
223  void SetRef(const nsize_type & matrixSize,
224  stride_type rowStride, stride_type colStride,
225  pointer dataPointer) {
226  this->Matrix.SetRef(matrixSize, rowStride, colStride, dataPointer);
227  }
228 
229  void SetRef(const nsize_type & matrixSize,
230  const nstride_type & matrixStride,
231  pointer dataPointer) {
232  this->Matrix.SetRef(matrixSize, matrixStride, dataPointer);
233  }
235 
236 
242  void SetRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder = VCT_DEFAULT_STORAGE)
243  {
244  this->Matrix.SetRef(rows, cols, dataPointer, storageOrder);
245  }
246 
247  void SetRef(const nsize_type & sizes, pointer dataPointer, bool storageOrder = VCT_DEFAULT_STORAGE)
248  {
249  this->Matrix.SetRef(sizes, dataPointer, storageOrder);
250  }
252 
253 
257  template <size_type __rows, size_type __cols, stride_type __rowStride, stride_type __colStride, typename __dataPtrType>
259  {
260  SetRef(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.row_stride(), otherMatrix.col_stride(),
261  otherMatrix.Pointer());
262  }
263 
276  template <size_type __rows, size_type __cols, stride_type __rowStride, stride_type __colStride, typename __dataPtrType>
278  size_type startRow, size_type startCol, size_type rows, size_type cols)
279  {
280  if ( (startRow + rows > otherMatrix.rows()) || (startCol + cols > otherMatrix.cols()) ) {
281  cmnThrow(std::out_of_range("vctDynamicMatrixRef SetRef out of range"));
282  }
283  SetRef(rows, cols, otherMatrix.row_stride(), otherMatrix.col_stride(), otherMatrix.Pointer(startRow, startCol));
284  }
285 
289  template <class __ownerType>
291  {
292  SetRef(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.row_stride(), otherMatrix.col_stride(),
293  otherMatrix.Pointer());
294  }
295 
309  template <class __ownerType>
311  size_type startRow, size_type startCol, size_type rows, size_type cols)
312  {
313  if ( (startRow + rows > otherMatrix.rows()) || (startCol + cols > otherMatrix.cols()) ) {
314  cmnThrow(std::out_of_range("vctDynamicMatrixRef SetRef out of range"));
315  }
316  SetRef(rows, cols, otherMatrix.row_stride(), otherMatrix.col_stride(),
317  otherMatrix.Pointer(startRow, startCol));
318  }
319 
320  template <class __ownerType>
322  const nsize_type & start, const nsize_type & matrixSize)
323  {
324  SetRef(otherMatrix,
325  start.Element(0), start.Element(1), matrixSize.Element(0), matrixSize.Element(1));
326  }
328 
335  inline ThisType & operator = (const ThisType & other) {
336  return reinterpret_cast<ThisType &>(this->Assign(other));
337  }
338 
340  return reinterpret_cast<ThisType &>(this->Assign(other));
341  }
342 
343  template <class __matrixOwnerType, typename __elementType>
345  return reinterpret_cast<ThisType &>(this->Assign(other));
346  }
347 
348  template <size_type __rows, size_type __cols, stride_type __rowStride, stride_type __colStride, typename __dataPtrType>
350  return reinterpret_cast<ThisType &>(this->Assign(other));
351  }
353 
355  inline ThisType & operator = (const value_type & value) {
356  this->SetAll(value);
357  return *this;
358  }
359 
363  void DeSerializeRaw(std::istream & inputStream) throw(std::runtime_error)
364  {
365  // get and set size
366  size_type myRows;
367  size_type myCols;
368  cmnDeSerializeRaw(inputStream, myRows);
369  cmnDeSerializeRaw(inputStream, myCols);
370 
371  if ((myRows != this->rows()) || (myCols != this->cols())) {
372  cmnThrow(std::runtime_error("vctDynamicMatrixRef::DeSerializeRaw: Sizes of matrices don't match"));
373  }
374 
375  // get data
376  size_type indexRow, indexCol;
377  for (indexRow = 0; indexRow < myRows; ++indexRow) {
378  for (indexCol = 0; indexCol < myCols; ++indexCol) {
379  cmnDeSerializeRaw(inputStream, this->Element(indexRow, indexCol));
380  }
381  }
382  }
383 
384 };
385 
386 
387 /*
388  Methods declared and use for vctDynamicVectorBase. Implemented here
389  since these need the const-matrix base.
390 */
391 #ifndef DOXYGEN
392 template <class _resultVectorOwnerType, class _matrixOwnerType, class _vectorOwnerType, class _elementType>
393 inline
397  const vctDynamicConstMatrixRef<_elementType> inputVectorRef(vector.size(), 1,
398  vector.stride(), 1,
399  vector.Pointer());
400  vctDynamicMatrixRef<_elementType> resultRef(result.size(), 1,
401  result.stride(), 1,
402  result.Pointer());
403  resultRef.ProductOf(matrix, inputVectorRef);
404 }
405 
406 
407 template <class _resultVectorOwnerType, class _vectorOwnerType, class _matrixOwnerType, class _elementType>
408 inline
412  const vctDynamicConstMatrixRef<_elementType> inputVectorRef(1, vector.size() * vector.stride(),
413  1, vector.stride(),
414  vector.Pointer());
415  vctDynamicMatrixRef<_elementType> resultRef(1, result.size() * result.stride(),
416  1, result.stride(),
417  result.Pointer());
418  resultRef.ProductOf(inputVectorRef, matrix);
419 }
420 #endif // DOXYGEN
421 
422 #endif // _vctDynamicMatrixRef_h
A template for a fixed size matrix with fixed spacing in memory.
Definition: vctFixedSizeConstMatrixBase.h:103
void vctMultiplyVectorMatrix(vctDynamicVectorBase< _resultVectorOwnerType, _elementType > &result, const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector, const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &matrix)
Definition: vctDynamicMatrixRef.h:409
void vctMultiplyMatrixVector(vctDynamicVectorBase< _resultVectorOwnerType, _elementType > &result, const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &matrix, const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector)
Definition: vctDynamicMatrixRef.h:394
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
vctDynamicMatrixRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixRef.h:136
Definition: vctDynamicMatrixBase.h:42
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
vctDynamicMatrixBase< vctDynamicMatrixRefOwner< _elementType >, _elementType > BaseType
Definition: vctDynamicMatrixRef.h:84
vctDynamicMatrixRef(const ThisType &other)
Definition: vctDynamicMatrixRef.h:100
void SetRef(vctFixedSizeMatrixBase< __rows, __cols, __rowStride, __colStride, _elementType, __dataPtrType > &otherMatrix)
Definition: vctDynamicMatrixRef.h:258
vctDynamicMatrixRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix, size_type startRow, size_type startCol, size_type rows, size_type cols)
Definition: vctDynamicMatrixRef.h:199
size_t size_type
Definition: vctContainerTraits.h:35
const bool VCT_DEFAULT_STORAGE
Definition: vctForwardDeclarations.h:49
const nstride_type & strides(void) const
Definition: vctDynamicConstMatrixBase.h:258
Dynamic matrix referencing existing memory.
Definition: vctDynamicMatrixRef.h:74
Declaration of vctDynamicConstMatrixRef.
ThisType & operator=(const ThisType &other)
Definition: vctDynamicMatrixRef.h:335
ThisType & ProductOf(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &matrix, const value_type scalar)
Definition: vctDynamicMatrixBase.h:971
Definition: vctDynamicConstMatrixBase.h:77
vctDynamicMatrixRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix, const nsize_type &start, const nsize_type &matrixSize)
Definition: vctDynamicMatrixRef.h:206
vctDynamicMatrixRef()
Definition: vctDynamicMatrixRef.h:90
vctDynamicMatrixRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:111
void SetRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixRef.h:242
size_type size(void) const
Definition: vctDynamicConstVectorBase.h:164
void SetRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:217
A template for a fixed size matrix with fixed spacings in memory.
Definition: vctFixedSizeMatrixBase.h:58
MatrixOwnerType::reverse_iterator reverse_iterator
Definition: vctDynamicMatrixRef.h:87
vctDynamicMatrixRef(const nsize_type &matrixSize, const nstride_type &matrixStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:123
MatrixOwnerType::const_iterator const_iterator
Definition: vctDynamicMatrixRef.h:86
vctDynamicMatrixRef(const nsize_type &matrixSize, pointer dataPointer, bool storageOrder=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixRef.h:141
Definition: vctDynamicMatrixRef.h:79
pointer Pointer(size_type rowIndex, size_type colIndex)
Definition: vctDynamicMatrixBase.h:143
void DeSerializeRaw(std::istream &inputStream)
Definition: vctDynamicMatrixRef.h:363
Dynamic matrix referencing existing memory (const)
Definition: vctDynamicConstMatrixRef.h:79
void SetRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix, size_type startRow, size_type startCol, size_type rows, size_type cols)
Definition: vctDynamicMatrixRef.h:310
value_type SetAll(const value_type value)
Definition: vctDynamicMatrixBase.h:452
void SetRef(const nsize_type &matrixSize, const nstride_type &matrixStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:229
vctDynamicMatrixRef(vctFixedSizeMatrixBase< __rows, __cols, __rowStride, __colStride, _elementType, __dataPtrType > &otherMatrix, size_type startRow, size_type startCol, size_type rows, size_type cols)
Definition: vctDynamicMatrixRef.h:170
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data)
Definition: cmnDeSerializer.h:82
size_type rows() const
Definition: vctDynamicConstMatrixBase.h:238
size_type cols() const
Definition: vctDynamicConstMatrixBase.h:243
Declaration of vctDynamicMatrixBase.
difference_type stride() const
Definition: vctDynamicConstVectorBase.h:169
const_pointer Pointer(index_type rowIndex, index_type colIndex) const
Definition: vctDynamicConstMatrixBase.h:306
#define cmnThrow(a)
Definition: MinimalCmn.h:4
reference Element(size_type rowIndex, size_type colIndex)
Definition: vctDynamicMatrixBase.h:214
vctDynamicMatrixRef< _elementType > ThisType
Definition: vctDynamicMatrixRef.h:82
const_pointer Pointer(index_type index=0) const
Definition: vctDynamicConstVectorBase.h:221
pointer Pointer(index_type index=0)
Definition: vctDynamicVectorBase.h:155
Definition: vctDynamicMatrixRefOwner.h:39
MatrixOwnerType::const_reverse_iterator const_reverse_iterator
Definition: vctDynamicMatrixRef.h:88
ThisType & Assign(const vctDynamicConstMatrixBase< __matrixOwnerType, value_type > &other)
Definition: vctDynamicMatrixBase.h:509
void SetRef(const nsize_type &sizes, pointer dataPointer, bool storageOrder=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixRef.h:247
void SetRef(vctFixedSizeMatrixBase< __rows, __cols, __rowStride, __colStride, _elementType, __dataPtrType > &otherMatrix, size_type startRow, size_type startCol, size_type rows, size_type cols)
Definition: vctDynamicMatrixRef.h:277
ptrdiff_t stride_type
Definition: vctContainerTraits.h:37
Definition: vctDynamicConstVectorBase.h:77
difference_type row_stride() const
Definition: vctDynamicConstMatrixBase.h:263
difference_type col_stride() const
Definition: vctDynamicConstMatrixBase.h:268
void SetRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix)
Definition: vctDynamicMatrixRef.h:290
vctDynamicMatrixRef(const nsize_type &matrixSize, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:117
void SetRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder)
Definition: vctDynamicMatrixRefOwner.h:74
Definition: vctVarStrideMatrixIterator.h:40
void SetRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix, const nsize_type &start, const nsize_type &matrixSize)
Definition: vctDynamicMatrixRef.h:321
vctDynamicMatrixRefOwner< _elementType > MatrixOwnerType
Definition: vctDynamicMatrixRef.h:83
const nsize_type & sizes(void) const
Definition: vctDynamicConstMatrixBase.h:233
MatrixOwnerType::iterator iterator
Definition: vctDynamicMatrixRef.h:85
Definition: vctVarStrideMatrixIterator.h:287
vctDynamicMatrixRef(vctDynamicMatrixBase< __ownerType, _elementType > &otherMatrix)
Definition: vctDynamicMatrixRef.h:182
pointer Pointer(size_type rowIndex, size_type colIndex)
Definition: vctFixedSizeMatrixBase.h:161
void SetRef(const nsize_type &matrixSize, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:223
Definition: vctDynamicVectorBase.h:61
vctDynamicMatrixRef(vctFixedSizeMatrixBase< __rows, __cols, __rowStride, __colStride, _elementType, __dataPtrType > &otherMatrix)
Definition: vctDynamicMatrixRef.h:154