Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
vpSubMatrix.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Mask on a vpMatrix .
32  */
33 
34 #include <stdlib.h>
35 #include <visp3/core/vpDebug.h>
36 #include <visp3/core/vpException.h>
37 #include <visp3/core/vpMatrixException.h>
38 #include <visp3/core/vpSubMatrix.h>
39 
40 BEGIN_VISP_NAMESPACE
41 vpSubMatrix::vpSubMatrix() : pRowNum(0), pColNum(0), parent(nullptr) { }
42 
51 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
52  const unsigned int &nrows, const unsigned int &ncols)
53  : pRowNum(0), pColNum(0), parent(nullptr)
54 {
55  init(m, row_offset, col_offset, nrows, ncols);
56 }
57 
66 void vpSubMatrix::init(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
67  const unsigned int &nrows, const unsigned int &ncols)
68 {
69 
70  if (!m.data) {
71  throw(vpMatrixException(vpMatrixException::subMatrixError, "SubMatrix parent matrix is not allocated"));
72  }
73 
74  if (((row_offset + nrows) <= m.getRows()) && ((col_offset + ncols) <= m.getCols())) {
75  data = m.data;
76  parent = &m;
77  rowNum = nrows;
78  colNum = ncols;
79  pRowNum = m.getRows();
80  pColNum = m.getCols();
81 
82  if (rowPtrs) {
83  free(rowPtrs);
84  }
85 
86  rowPtrs = (double **)malloc(nrows * sizeof(double *));
87  for (unsigned int r = 0; r < nrows; ++r) {
88  rowPtrs[r] = m.data + col_offset + ((r + row_offset) * pColNum);
89  }
90 
91  dsize = pRowNum * pColNum;
92  }
93  else {
94  throw(
95  vpMatrixException(vpMatrixException::incorrectMatrixSizeError, "Submatrix cannot be contain in parent matrix"));
96  }
97 }
98 
104 {
105  if (!data) {
107  "vpSubMatrix parent vpMatrix has been destroyed"));
108  }
109  if ((pRowNum != parent->getRows()) || (pColNum != parent->getCols())) {
111  "vpSubMatrix size of parent vpMatrix has been changed"));
112  }
113 }
114 
120 {
121 
122  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
124  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
125  }
126 
127  for (unsigned int i = 0; i < rowNum; ++i) {
128  for (unsigned int j = 0; j < colNum; ++j) {
129  rowPtrs[i][j] = B[i][j];
130  }
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 
160  return *this;
161 }
162 
168 {
169  for (unsigned int i = 0; i < rowNum; ++i) {
170  for (unsigned int j = 0; j < colNum; ++j) {
171  rowPtrs[i][j] = x;
172  }
173  }
174 
175  return *this;
176 }
177 
179 END_VISP_NAMESPACE
unsigned int getCols() const
Definition: vpArray2D.h:337
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:148
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:1105
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:1101
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:1107
unsigned int getRows() const
Definition: vpArray2D.h:347
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:1103
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:169
Definition of the vpSubMatrix class that provides a mask on a vpMatrix. All properties of vpMatrix ar...
Definition: vpSubMatrix.h:57
virtual ~vpSubMatrix() VP_OVERRIDE
Destructor.
void checkParentStatus() const
Check is parent vpRowVector has changed since initialization.
unsigned int pRowNum
Definition: vpSubMatrix.h:83
vpMatrix * parent
Definition: vpSubMatrix.h:85
void init(vpMatrix &m, const unsigned int &row, const unsigned int &col, const unsigned int &nrows, const unsigned int &ncols)
Initialisation of vpMatrix.
Definition: vpSubMatrix.cpp:66
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:41
unsigned int pColNum
Definition: vpSubMatrix.h:84
vpSubMatrix & operator=(const vpSubMatrix &B)
Operation such as subA = subB.