Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpSubMatrix.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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(nullptr) { }
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(nullptr)
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 
90  rowPtrs = (double **)malloc(nrows * sizeof(double *));
91  for (unsigned int r = 0; r < nrows; ++r) {
92  rowPtrs[r] = m.data + col_offset + ((r + row_offset) * pColNum);
93  }
94 
95  dsize = pRowNum * pColNum;
96  }
97  else {
98  throw(
99  vpMatrixException(vpMatrixException::incorrectMatrixSizeError, "Submatrix cannot be contain in parent matrix"));
100  }
101 }
102 
108 {
109  if (!data) {
111  "vpSubMatrix parent vpMatrix has been destroyed"));
112  }
113  if ((pRowNum != parent->getRows()) || (pColNum != parent->getCols())) {
115  "vpSubMatrix size of parent vpMatrix has been changed"));
116  }
117 }
118 
124 {
125 
126  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
128  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
129  }
130 
131  for (unsigned int i = 0; i < rowNum; ++i) {
132  for (unsigned int j = 0; j < colNum; ++j) {
133  rowPtrs[i][j] = B[i][j];
134  }
135  }
136 
137  return *this;
138 }
139 
145 {
146 
147  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
149  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
150  }
151 
152  pRowNum = B.pRowNum;
153  pColNum = B.pColNum;
154  parent = B.parent;
155 
156  double **BrowPtrs = B.rowPtrs;
157 
158  for (unsigned int i = 0; i < rowNum; ++i) {
159  for (unsigned int j = 0; j < colNum; ++j) {
160  rowPtrs[i][j] = BrowPtrs[i][j];
161  }
162  }
163 
164  return *this;
165 }
166 
172 {
173  for (unsigned int i = 0; i < rowNum; ++i) {
174  for (unsigned int j = 0; j < colNum; ++j) {
175  rowPtrs[i][j] = x;
176  }
177  }
178 
179  return *this;
180 }
181 
unsigned int getCols() const
Definition: vpArray2D.h:327
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:133
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:129
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:135
unsigned int getRows() const
Definition: vpArray2D.h:337
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:131
error that can be emitted by the vpMatrix class and its derivatives
@ incorrectMatrixSizeError
Incorrect matrix size.
@ subMatrixError
Sub operation matrix error.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
vp_deprecated void init()
Definition: vpMatrix.h:989
Definition of the vpSubMatrix class that provides a mask on a vpMatrix. All properties of vpMatrix ar...
Definition: vpSubMatrix.h:55
void checkParentStatus() const
Check is parent vpRowVector has changed since initialization.
unsigned int pRowNum
Definition: vpSubMatrix.h:65
vpMatrix * parent
Definition: vpSubMatrix.h:67
virtual ~vpSubMatrix() vp_override
Destructor.
unsigned int pColNum
Definition: vpSubMatrix.h:66
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:45
vpSubMatrix & operator=(const vpSubMatrix &B)
Operation such as subA = subB.