cisst-saw
Loading...
Searching...
No Matches
vctDynamicMatrix.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
6 Author(s): Ofri Sadowsky, Anton Deguet
7 Created on: 2004-07-01
8
9 (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19*/
20
21#pragma once
22#ifndef _vctDynamicMatrix_h
23#define _vctDynamicMatrix_h
24
29
34
135template <class _elementType>
136class vctDynamicMatrix : public vctDynamicMatrixBase<vctDynamicMatrixOwner<_elementType>, _elementType>
137{
138
139 friend class vctReturnDynamicMatrix<_elementType>;
140
141public:
142 VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
143 enum {DIMENSION = 2};
145
148
149
152 // The default initialization of vctDynamicMatrixOwner is empty.
153 {}
154
155
156 /* Constructor: Create a matrix of the specified size. Elements
157 initialized with default constructor. The storage order can be
158 either #VCT_ROW_MAJOR or #VCT_COL_MAJOR. */
160 vctDynamicMatrix(size_type rows, size_type cols, bool storageOrder = VCT_DEFAULT_STORAGE) {
161 this->SetSize(rows, cols, storageOrder);
162 }
163
164 vctDynamicMatrix(const nsize_type & matrixSize, bool storageOrder = VCT_DEFAULT_STORAGE) {
165 this->SetSize(matrixSize, storageOrder);
166 }
167
168
173 vctDynamicMatrix(size_type rows, size_type cols, value_type value, bool storageOrder = VCT_DEFAULT_STORAGE) {
174 this->SetSize(rows, cols, storageOrder);
175 this->SetAll(value);
176 }
177
178 vctDynamicMatrix(const nsize_type & matrixSize, value_type value, bool storageOrder = VCT_DEFAULT_STORAGE) {
179 this->SetSize(matrixSize, storageOrder);
180 this->SetAll(value);
181 }
182
183
189
190
195 vctDynamicMatrix(const ThisType & otherMatrix):
196 BaseType()
197 {
198 this->SetSize(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.StorageOrder());
199 this->Assign(otherMatrix);
200 }
201
202
206 template <class __matrixOwnerType, typename __otherMatrixElementType>
208 this->SetSize(otherMatrix.rows(), otherMatrix.cols(), storageOrder);
209 this->Assign(otherMatrix);
210 }
211
212
217 template <class __matrixOwnerType>
219 this->SetSize(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.StorageOrder());
220 this->Assign(otherMatrix);
221 }
222
223
229 template <class __matrixOwnerType, typename __otherMatrixElementType>
231 this->SetSize(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.StorageOrder());
232 this->Assign(otherMatrix);
233 }
234
235
237 template <size_type __rows, size_type __cols,
238 stride_type __rowStride, stride_type __colStride,
239 class __elementType, class __dataPtrType>
240 explicit vctDynamicMatrix(const vctFixedSizeConstMatrixBase<__rows, __cols,
241 __rowStride, __colStride,
242 __elementType, __dataPtrType> & other) {
243 this->ForceAssign(other);
244 }
245
246
254 template <class __matrixOwnerType, typename __elementType>
256 this->SetSize(otherMatrix.rows(), otherMatrix.cols());
257 this->Assign(otherMatrix);
258 return *this;
259 }
260
261
267 ThisType & operator = (const ThisType & otherMatrix) {
268 this->SetSize(otherMatrix.rows(), otherMatrix.cols(), otherMatrix.StorageOrder());
269 this->Assign(otherMatrix);
270 return *this;
271 }
272
276 template <size_type __rows, size_type __cols,
277 stride_type __rowStride, stride_type __colStride,
278 class __elementType, class __dataPtrType>
280 __rowStride, __colStride,
281 __elementType, __dataPtrType> & other) {
282 this->ForceAssign(other);
283 return *this;
284 }
285
286
297
299 inline ThisType & operator = (const value_type & value) {
300 this->SetAll(value);
301 return *this;
302 }
303
304 // documented in base class
305 template <class __matrixOwnerType, typename __elementType>
307 this->SetSize(other.sizes(), other.StorageOrder());
308 this->Assign(other);
309 return *this;
310 }
311
312 // documented in base class
313 template <size_type __rows, size_type __cols,
314 stride_type __rowStride, stride_type __colStride,
315 class __elementType, class __dataPtrType>
316 inline ThisType & ForceAssign(const vctFixedSizeConstMatrixBase<__rows, __cols,
317 __rowStride, __colStride,
318 __elementType, __dataPtrType> & other) {
319 this->SetSize(other.sizes(), other.StorageOrder());
320 this->Assign(other);
321 return *this;
322 }
323
338 void resize(size_type rows, size_type cols) {
339 this->resize(nsize_type(rows, cols));
340 }
341
342 void resize(const nsize_type & newSizes) {
343 if (newSizes == this->sizes())
344 return;
345
346 const bool isRowMajor = this->IsRowMajor();
347 ThisType newData(newSizes, isRowMajor);
348 const nsize_type corner(0);
349 nsize_type minSizes;
350 minSizes.ElementwiseMinOf(this->sizes(), newSizes);
351 vctDynamicConstMatrixRef<value_type> myDataMinSpaceRef(*this, corner, minSizes);
352 vctDynamicMatrixRef<value_type> newDataMinSpaceRef(newData, corner, minSizes);
353 newDataMinSpaceRef.Assign(myDataMinSpaceRef);
354 this->Matrix.Disown();
355 this->Matrix.Own(newSizes, isRowMajor, newData.Matrix.Release());
356 }
357
358
364 void SetSize(size_type rows, size_type cols, bool storageOrder) {
365 this->Matrix.SetSize(rows, cols, storageOrder);
366 }
367
368 void SetSize(const nsize_type & matrixSize, bool storageOrder) {
369 this->Matrix.SetSize(matrixSize, storageOrder);
370 }
371
372 void SetSize(size_type rows, size_type cols) {
373 this->Matrix.SetSize(rows, cols, this->StorageOrder());
374 }
375
376 void SetSize(const nsize_type & matrixSize) {
377 this->Matrix.SetSize(matrixSize, this->StorageOrder());
378 }
379
380
382 void DeSerializeRaw(std::istream & inputStream)
383 {
384 // get and set size
385 size_type myRows = 0;
386 size_type myCols = 0;
387 cmnDeSerializeSizeRaw(inputStream, myRows);
388 cmnDeSerializeSizeRaw(inputStream, myCols);
389 this->SetSize(myRows, myCols);
390
391 // get data
392 size_type indexRow, indexCol;
393 for (indexRow = 0; indexRow < myRows; ++indexRow) {
394 for (indexCol = 0; indexCol < myCols; ++indexCol) {
395 cmnDeSerializeRaw(inputStream, this->Element(indexRow, indexCol));
396 }
397 }
398 }
399
400};
401
402
418template <class _elementType>
419class vctReturnDynamicMatrix : public vctDynamicMatrix<_elementType> {
420public:
423
425
426 explicit vctReturnDynamicMatrix(const BaseType & other)
427 {
428 BaseType & nonConstOther = const_cast<BaseType &>(other);
429 // if we don't save it in a variable, it will be destroyed in the Release operation
430 const size_type rows = other.rows();
431 const size_type cols = other.cols();
432 const bool storageOrder = other.StorageOrder();
433 this->Matrix.Own(rows, cols, storageOrder, nonConstOther.Matrix.Release());
434 }
435};
436
437
438// implementation of the special copy constuctor of vctDynamicMatrix
439template <class _elementType>
442 const_cast< vctReturnDynamicMatrix<_elementType> & >(other);
443 // if we don't save it in a variable, it will be destroyed in the Release operation
444 const size_type rows = other.rows();
445 const size_type cols = other.cols();
446 const bool storageOrder = other.StorageOrder();
447 this->Matrix.Own(rows, cols, storageOrder, nonConstOther.Matrix.Release());
448}
449
450
451// implementation of the special assignment operator from vctReturnDynamicMatrix to vctDynamicMatrix
452template <class _elementType>
456 const_cast< vctReturnDynamicMatrix<_elementType> & >(other);
457 // if we don't save it in a variable, it will be destroyed in the Release operation
458 const size_type rows = other.rows();
459 const size_type cols = other.cols();
460 const bool storageOrder = other.StorageOrder();
461 this->Matrix.Disown();
462 this->Matrix.Own(rows, cols, storageOrder, nonConstOther.Matrix.Release());
463 return *this;
464}
465
466
469
474template <class _matrixOwnerType1, class _matrixOwnerType2, class _elementType>
478 typedef _elementType value_type;
479 vctDynamicMatrix<value_type> resultStorage(inputMatrix1);
480 resultStorage.Add(inputMatrix2);
481 return vctReturnDynamicMatrix<value_type>(resultStorage);
482}
483
484/* documented above */
485template <class _matrixOwnerType1, class _matrixOwnerType2, class _elementType>
489 typedef _elementType value_type;
490 vctDynamicMatrix<value_type> resultStorage(inputMatrix1);
491 resultStorage.Subtract(inputMatrix2);
492 return vctReturnDynamicMatrix<value_type>(resultStorage);
493}
494
495
496
497
501
506template <class _matrixOwnerType, class _elementType>
509 const _elementType & inputScalar) {
510 typedef _elementType value_type;
511 vctDynamicMatrix<value_type> resultStorage(inputMatrix);
512 resultStorage.Add(inputScalar);
513 return vctReturnDynamicMatrix<value_type>(resultStorage);
514}
515
516/* documented above */
517template <class _matrixOwnerType, class _elementType>
520 const _elementType & inputScalar) {
521 typedef _elementType value_type;
522 vctDynamicMatrix<value_type> resultStorage(inputMatrix);
523 resultStorage.Subtract(inputScalar);
524 return vctReturnDynamicMatrix<value_type>(resultStorage);
525}
526
527/* documented above */
528template <class _matrixOwnerType, class _elementType>
531 const _elementType & inputScalar) {
532 typedef _elementType value_type;
533 vctDynamicMatrix<value_type> resultStorage(inputMatrix);
534 resultStorage.Multiply(inputScalar);
535 return vctReturnDynamicMatrix<value_type>(resultStorage);
536}
537
538/* documented above */
539template <class _matrixOwnerType, class _elementType>
542 const _elementType & inputScalar) {
543 typedef _elementType value_type;
544 vctDynamicMatrix<value_type> resultStorage(inputMatrix);
545 resultStorage.Divide(inputScalar);
546 return vctReturnDynamicMatrix<value_type>(resultStorage);
547}
548
549
550
551
555
560template <class _matrixOwnerType, class _elementType>
562operator + (const _elementType & inputScalar,
564 typedef _elementType value_type;
565 vctDynamicMatrix<value_type> resultStorage(inputMatrix.rows(), inputMatrix.cols());
566 resultStorage.SumOf(inputScalar, inputMatrix);
567 return vctReturnDynamicMatrix<value_type>(resultStorage);
568}
569
570/* documented above */
571template <class _matrixOwnerType, class _elementType>
573operator - (const _elementType & inputScalar,
575 typedef _elementType value_type;
576 vctDynamicMatrix<value_type> resultStorage(inputMatrix.rows(), inputMatrix.cols());
577 resultStorage.DifferenceOf(inputScalar, inputMatrix);
578 return vctReturnDynamicMatrix<value_type>(resultStorage);
579}
580
581/* documented above */
582template <class _matrixOwnerType, class _elementType>
584operator * (const _elementType & inputScalar,
586 typedef _elementType value_type;
587 vctDynamicMatrix<value_type> resultStorage(inputMatrix.rows(), inputMatrix.cols());
588 resultStorage.ProductOf(inputScalar, inputMatrix);
589 return vctReturnDynamicMatrix<value_type>(resultStorage);
590}
591
592/* documented above */
593template <class _matrixOwnerType, class _elementType>
595operator / (const _elementType & inputScalar,
597 typedef _elementType value_type;
598 vctDynamicMatrix<value_type> resultStorage(inputMatrix.rows(), inputMatrix.cols());
599 resultStorage.RatioOf(inputScalar, inputMatrix);
600 return vctReturnDynamicMatrix<value_type>(resultStorage);
601}
602
603
604
607
611template <class _matrixOwnerType, class _elementType>
614 typedef _elementType value_type;
615 vctDynamicMatrix<value_type> resultStorage(inputMatrix.rows(), inputMatrix.cols());
616 resultStorage.NegationOf(inputMatrix);
617 return vctReturnDynamicMatrix<value_type>(resultStorage);
618}
619
620
621
622
623#ifndef DOXYGEN
624template <class _matrixOwnerType1, class _matrixOwnerType2, class _elementType>
628 typedef _elementType value_type;
629 vctDynamicMatrix<value_type> resultStorage(inputMatrix1.rows(), inputMatrix2.cols());
630 resultStorage.ProductOf(inputMatrix1, inputMatrix2);
631 return vctReturnDynamicMatrix<value_type>(resultStorage);
632}
633
634
635template <class _matrixOwnerType, class _vectorOwnerType, class _elementType>
639 typedef _elementType value_type;
640 vctDynamicVector<value_type> resultStorage(inputMatrix.rows());
641 resultStorage.ProductOf(inputMatrix, inputVector);
642 return vctReturnDynamicVector<value_type>(resultStorage);
643}
644
645
646template <class _vectorOwnerType, class _matrixOwnerType, class _elementType>
650 typedef _elementType value_type;
651 vctDynamicVector<value_type> resultStorage(inputMatrix.cols());
652 resultStorage.ProductOf(inputVector, inputMatrix);
653 return vctReturnDynamicVector<value_type>(resultStorage);
654}
655
656
657
658/*
659 Methods declared previously and implemented here because they require vctReturnDynamicMatrix
660*/
661
662
663/* documented in class vctDynamicConstMatrixBase */
664template <class _matrixOwnerType, class _elementType>
667 typedef _elementType value_type;
668 vctDynamicMatrix<value_type> resultStorage(this->rows(), this->cols());
669 vctDynamicMatrixLoopEngines::
670 MoMi<typename vctUnaryOperations<value_type>::AbsValue>::
671 Run(resultStorage, *this);
672 return vctReturnDynamicMatrix<value_type>(resultStorage);
673}
674
675/* documented in class vctDynamicConstMatrixBase */
676template <class _matrixOwnerType, class _elementType>
679 typedef _elementType value_type;
680 vctDynamicMatrix<value_type> resultStorage(this->rows(), this->cols());
681 vctDynamicMatrixLoopEngines::
682 MoMi<typename vctUnaryOperations<value_type>::Negation>::
683 Run(resultStorage, *this);
684 return vctReturnDynamicMatrix<value_type>(resultStorage);
685}
686
687/* documented in class vctDynamicConstMatrixBase */
688template <class _matrixOwnerType, class _elementType>
691 typedef _elementType value_type;
692 vctDynamicMatrix<value_type> resultStorage(this->rows(), this->cols());
693 vctDynamicMatrixLoopEngines::
694 MoMi<typename vctUnaryOperations<value_type>::Floor>::
695 Run(resultStorage, *this);
696 return vctReturnDynamicMatrix<value_type>(resultStorage);
697}
698
699/* documented in class vctDynamicConstMatrixBase */
700template <class _matrixOwnerType, class _elementType>
703 typedef _elementType value_type;
704 vctDynamicMatrix<value_type> resultStorage(this->rows(), this->cols());
705 vctDynamicMatrixLoopEngines::
706 MoMi<typename vctUnaryOperations<value_type>::Ceil>::
707 Run(resultStorage, *this);
708 return vctReturnDynamicMatrix<value_type>(resultStorage);
709}
710
711/* documented in class vctDynamicConstMatrixBase */
712template <class _matrixOwnerType, class _elementType>
715 vctDynamicMatrix<value_type> resultStorage(size, size);
716 resultStorage.SetAll(_elementType(0));
717 resultStorage.Diagonal().SetAll(_elementType(1));
718 return vctReturnDynamicMatrix<value_type>(resultStorage);
719}
720
721/* Documented in class vctDynamicConstMatrixBase */
722template <class _matrixOwnerType, class __matrixOwnerType, class _elementType,
723 class _elementOperationType>
732
733/* documented in class vctDynamicConstMatrixBase */
734template <class _matrixOwnerType, class _elementType,
735 class _elementOperationType>
738 const _elementType & scalar) {
739 vctDynamicMatrix<bool> result(matrix.rows(), matrix.cols());
741 MoMiSi<_elementOperationType>::Run(result, matrix, scalar);
742 return vctReturnDynamicMatrix<bool>(result);
743}
744
745#endif // DOXYGEN
746
747
748#endif // _vctDynamicMatrix_h
749
Definition vctForwardDeclarations.h:145
Dynamic matrix referencing existing memory (const).
Definition vctDynamicConstMatrixRef.h:81
Definition vctForwardDeclarations.h:119
Definition vctDynamicMatrixBase.h:43
ThisType & Assign(const vctDynamicConstMatrixBase< __matrixOwnerType, value_type > &other)
Definition vctDynamicMatrixBase.h:509
Definition vctForwardDeclarations.h:157
static void Run(_outputMatrixType &outputMatrix, const _input1MatrixType &input1Matrix, const _input2MatrixType &input2Matrix)
Definition vctDynamicMatrixLoopEngines.h:95
static void Run(_outputMatrixType &outputMatrix, const _inputMatrixType &inputMatrix, const _inputScalarType inputScalar)
Definition vctDynamicMatrixLoopEngines.h:347
Dynamic matrix referencing existing memory.
Definition vctDynamicMatrixRef.h:75
Definition vctForwardDeclarations.h:131
A template for a fixed size matrix with fixed spacing in memory.
Definition vctFixedSizeConstMatrixBase.h:104
Definition vctDynamicMatrix.h:419
vctDynamicMatrix< BoolType > BaseType
Definition vctDynamicMatrix.h:422
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
vctReturnDynamicMatrix(const BaseType &other)
Definition vctDynamicMatrix.h:426
Definition vctDynamicVector.h:395
vctReturnDynamicVector(const BaseType &other)
Definition vctDynamicVector.h:399
void cmnDeSerializeSizeRaw(std::istream &inputStream, size_t &data) CISST_THROW(std
Definition cmnDeSerializer.h:96
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data) CISST_THROW(std
Definition cmnDeSerializer.h:82
vctDynamicMatrix()
A matrix object of dynamic size.
Definition vctDynamicMatrix.h:151
size_t size_type
Definition vctContainerTraits.h:35
ptrdiff_t stride_type
Definition vctContainerTraits.h:37
#define VCT_CONTAINER_TRAITS_TYPEDEFS(type)
Definition vctContainerTraits.h:50
#define VCT_NARRAY_TRAITS_TYPEDEFS(dimension)
Definition vctContainerTraits.h:68
bool StorageOrder(void) const
Definition vctDynamicConstMatrixBase.h:656
vctReturnDynamicMatrix< bool > vctDynamicMatrixElementwiseCompareScalar(const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &matrix, const _elementType &scalar)
Definition vctDynamicMatrix.h:737
bool IsRowMajor(void) const
Definition vctDynamicConstMatrixBase.h:635
size_type cols() const
Definition vctDynamicConstMatrixBase.h:243
OwnerType Matrix
Definition vctDynamicConstMatrixBase.h:167
@ DIMENSION
Definition vctDynamicConstMatrixBase.h:82
size_type size(void) const
Definition vctDynamicConstMatrixBase.h:228
const nsize_type & sizes(void) const
Definition vctDynamicConstMatrixBase.h:233
const_reference Element(size_type rowIndex, size_type colIndex) const
Definition vctDynamicConstMatrixBase.h:362
vctReturnDynamicMatrix< bool > vctDynamicMatrixElementwiseCompareMatrix(const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &matrix1, const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &matrix2)
Definition vctDynamicMatrix.h:725
size_type rows() const
Definition vctDynamicConstMatrixBase.h:238
vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > ThisType
Definition vctDynamicConstMatrixBase.h:86
vctReturnDynamicMatrix< _elementType > operator/(const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &inputMatrix, const _elementType &inputScalar)
Definition vctDynamicMatrix.h:541
vctReturnDynamicMatrix< _elementType > operator*(const vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > &inputMatrix, const _elementType &inputScalar)
Definition vctDynamicMatrix.h:530
ThisType & ForceAssign(const vctDynamicConstMatrixBase< __matrixOwnerType, __elementType > &other)
Definition vctDynamicMatrix.h:306
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition vctDynamicMatrix.h:364
vctReturnDynamicMatrix< _elementType > operator-(const vctDynamicConstMatrixBase< _matrixOwnerType1, _elementType > &inputMatrix1, const vctDynamicConstMatrixBase< _matrixOwnerType2, _elementType > &inputMatrix2)
Definition vctDynamicMatrix.h:487
void resize(size_type rows, size_type cols)
Definition vctDynamicMatrix.h:338
void DeSerializeRaw(std::istream &inputStream)
Definition vctDynamicMatrix.h:382
vctReturnDynamicMatrix< _elementType > operator+(const vctDynamicConstMatrixBase< _matrixOwnerType1, _elementType > &inputMatrix1, const vctDynamicConstMatrixBase< _matrixOwnerType2, _elementType > &inputMatrix2)
Definition vctDynamicMatrix.h:476
ThisType & operator=(const vctDynamicConstMatrixBase< __matrixOwnerType, __elementType > &otherMatrix)
Definition vctDynamicMatrix.h:255
Declaration of vctDynamicMatrixBase.
Declaration of vctDynamicMatrixOwner.
Declaration of vctDynamicMatrixRef.
vctDynamicNArrayBase< vctDynamicNArrayOwner< _elementType, _dimension >, _elementType, _dimension > BaseType
Definition vctDynamicNArray.h:133
Declaration of vctDynamicVector.
const bool VCT_DEFAULT_STORAGE
Definition vctForwardDeclarations.h:47