Visual Servoing Platform  version 3.4.0
vpSubMatrix.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software 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 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Mask on a vpMatrix .
33  *
34  * Authors:
35  * Laneurit Jean
36  *
37  *****************************************************************************/
38 
39 #include <stdlib.h>
40 #include <visp3/core/vpDebug.h>
41 #include <visp3/core/vpException.h>
42 #include <visp3/core/vpMatrixException.h>
43 #include <visp3/core/vpSubMatrix.h>
44 
45 vpSubMatrix::vpSubMatrix() : pRowNum(0), pColNum(0), parent(NULL) {}
46 
55 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
56  const unsigned int &nrows, const unsigned int &ncols)
57  : pRowNum(0), pColNum(0), parent(NULL)
58 {
59  init(m, row_offset, col_offset, nrows, ncols);
60 }
61 
70 void vpSubMatrix::init(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
71  const unsigned int &nrows, const unsigned int &ncols)
72 {
73 
74  if (!m.data) {
75  throw(vpMatrixException(vpMatrixException::subMatrixError, "SubMatrix parent matrix is not allocated"));
76  }
77 
78  if (row_offset + nrows <= m.getRows() && col_offset + ncols <= m.getCols()) {
79  data = m.data;
80  parent = &m;
81  rowNum = nrows;
82  colNum = ncols;
83  pRowNum = m.getRows();
84  pColNum = m.getCols();
85 
86  if (rowPtrs)
87  free(rowPtrs);
88 
89  rowPtrs = (double **)malloc(nrows * sizeof(double *));
90  for (unsigned int r = 0; r < nrows; r++)
91  rowPtrs[r] = m.data + col_offset + (r + row_offset) * pColNum;
92 
93  dsize = pRowNum * pColNum;
94  } else {
95  throw(
96  vpMatrixException(vpMatrixException::incorrectMatrixSizeError, "Submatrix cannot be contain in parent matrix"));
97  }
98 }
99 
105 {
106  if (!data) {
108  "vpSubMatrix parent vpMatrix has been destroyed"));
109  }
110  if (pRowNum != parent->getRows() || pColNum != parent->getCols()) {
112  "vpSubMatrix size of parent vpMatrix has been changed"));
113  }
114 }
115 
121 {
122 
123  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
125  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
126  }
127 
128  for (unsigned int i = 0; i < rowNum; i++) {
129  for (unsigned int j = 0; j < colNum; j++)
130  rowPtrs[i][j] = B[i][j];
131  }
132 
133  return *this;
134 }
135 
141 {
142 
143  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
145  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
146  }
147 
148  pRowNum = B.pRowNum;
149  pColNum = B.pColNum;
150  parent = B.parent;
151 
152  double **BrowPtrs = B.rowPtrs;
153 
154  for (unsigned int i = 0; i < rowNum; i++) {
155  for (unsigned int j = 0; j < colNum; j++)
156  rowPtrs[i][j] = BrowPtrs[i][j];
157  }
158 
159  return *this;
160 }
161 
167 {
168  for (unsigned int i = 0; i < rowNum; i++) {
169  for (unsigned int j = 0; j < colNum; j++)
170  rowPtrs[i][j] = x;
171  }
172 
173  return *this;
174 }
175 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
void checkParentStatus() const
Check is parent vpRowVector has changed since initialization.
unsigned int pColNum
Definition: vpSubMatrix.h:75
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:145
unsigned int getCols() const
Definition: vpArray2D.h:279
Definition of the vpSubMatrix vpSubMatrix class provides a mask on a vpMatrix all properties of vpMat...
Definition: vpSubMatrix.h:62
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:135
virtual ~vpSubMatrix()
Destructor.
vp_deprecated void init()
Definition: vpMatrix.h:781
unsigned int getRows() const
Definition: vpArray2D.h:289
unsigned int pRowNum
Definition: vpSubMatrix.h:74
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:137
vpMatrix * parent
Definition: vpSubMatrix.h:76
vpSubMatrix & operator=(const vpSubMatrix &B)
Operation such as subA = subB.
error that can be emited by the vpMatrix class and its derivates
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:141
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:139
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:45