Visual Servoing Platform  version 3.6.1 under development (2024-06-19)
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 
46 vpSubMatrix::vpSubMatrix() : pRowNum(0), pColNum(0), parent(nullptr) { }
47 
56 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
57  const unsigned int &nrows, const unsigned int &ncols)
58  : pRowNum(0), pColNum(0), parent(nullptr)
59 {
60  init(m, row_offset, col_offset, nrows, ncols);
61 }
62 
71 void vpSubMatrix::init(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
72  const unsigned int &nrows, const unsigned int &ncols)
73 {
74 
75  if (!m.data) {
76  throw(vpMatrixException(vpMatrixException::subMatrixError, "SubMatrix parent matrix is not allocated"));
77  }
78 
79  if (((row_offset + nrows) <= m.getRows()) && ((col_offset + ncols) <= m.getCols())) {
80  data = m.data;
81  parent = &m;
82  rowNum = nrows;
83  colNum = ncols;
84  pRowNum = m.getRows();
85  pColNum = m.getCols();
86 
87  if (rowPtrs) {
88  free(rowPtrs);
89  }
90 
91  rowPtrs = (double **)malloc(nrows * sizeof(double *));
92  for (unsigned int r = 0; r < nrows; ++r) {
93  rowPtrs[r] = m.data + col_offset + ((r + row_offset) * pColNum);
94  }
95 
96  dsize = pRowNum * pColNum;
97  }
98  else {
99  throw(
100  vpMatrixException(vpMatrixException::incorrectMatrixSizeError, "Submatrix cannot be contain in parent matrix"));
101  }
102 }
103 
109 {
110  if (!data) {
112  "vpSubMatrix parent vpMatrix has been destroyed"));
113  }
114  if ((pRowNum != parent->getRows()) || (pColNum != parent->getCols())) {
116  "vpSubMatrix size of parent vpMatrix has been changed"));
117  }
118 }
119 
125 {
126 
127  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
129  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
130  }
131 
132  for (unsigned int i = 0; i < rowNum; ++i) {
133  for (unsigned int j = 0; j < colNum; ++j) {
134  rowPtrs[i][j] = B[i][j];
135  }
136  }
137 
138  return *this;
139 }
140 
146 {
147 
148  if ((colNum != B.getCols()) || (rowNum != B.getRows())) {
150  "vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix"));
151  }
152 
153  pRowNum = B.pRowNum;
154  pColNum = B.pColNum;
155  parent = B.parent;
156 
157  double **BrowPtrs = B.rowPtrs;
158 
159  for (unsigned int i = 0; i < rowNum; ++i) {
160  for (unsigned int j = 0; j < colNum; ++j) {
161  rowPtrs[i][j] = BrowPtrs[i][j];
162  }
163  }
164 
165  return *this;
166 }
167 
173 {
174  for (unsigned int i = 0; i < rowNum; ++i) {
175  for (unsigned int j = 0; j < colNum; ++j) {
176  rowPtrs[i][j] = x;
177  }
178  }
179 
180  return *this;
181 }
182 
unsigned int getCols() const
Definition: vpArray2D.h:330
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:140
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:134
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:130
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:136
unsigned int getRows() const
Definition: vpArray2D.h:340
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:132
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:151
vp_deprecated void init()
Definition: vpMatrix.h:1006
Definition of the vpSubMatrix class that provides a mask on a vpMatrix. All properties of vpMatrix ar...
Definition: vpSubMatrix.h:57
void checkParentStatus() const
Check is parent vpRowVector has changed since initialization.
unsigned int pRowNum
Definition: vpSubMatrix.h:83
vpMatrix * parent
Definition: vpSubMatrix.h:85
virtual ~vpSubMatrix() vp_override
Destructor.
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:46
unsigned int pColNum
Definition: vpSubMatrix.h:84
vpSubMatrix & operator=(const vpSubMatrix &B)
Operation such as subA = subB.