Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpSubRowVector.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 vpRowVector .
32  */
33 
34 #include <stdlib.h>
35 
36 #include <visp3/core/vpException.h>
37 #include <visp3/core/vpSubRowVector.h>
38 
41 vpSubRowVector::vpSubRowVector() : vpRowVector(), m_pColNum(0), m_parent(nullptr) { }
42 
49 vpSubRowVector::vpSubRowVector(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
50  : vpRowVector(), m_pColNum(0), m_parent(nullptr)
51 {
52  init(v, offset, ncols);
53 }
54 
61 void vpSubRowVector::init(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
62 {
63  if (!v.data) {
64  throw(vpException(vpException::fatalError, "Cannot initialize a sub-row "
65  "vector from an empty parent row vector"));
66  }
67 
68  if ((offset + ncols) <= v.getCols()) {
69  data = v.data + offset;
70 
71  rowNum = 1;
72  colNum = ncols;
73 
74  m_pColNum = v.getCols();
75  m_parent = &v;
76 
77  if (rowPtrs) {
78  free(rowPtrs);
79  }
80 
81  rowPtrs = (double **)malloc(1 * sizeof(double *));
82  for (unsigned int i = 0; i < 1; ++i) {
83  rowPtrs[i] = v.data + i + offset;
84  }
85 
86  dsize = colNum;
87  }
88  else {
89  throw(vpException(vpException::dimensionError, "Cannot create a sub-row vector that is not completely "
90  "contained in the parent row vector"));
91  }
92 }
93 
98 
105 {
106  if (!data) {
107  throw(vpException(vpException::fatalError, "The parent of the current sub-row vector has been destroyed"));
108  }
109  if (m_pColNum != m_parent->getCols()) {
110  throw(vpException(vpException::dimensionError, "The size of the parent sub-row vector has changed"));
111  }
112 }
113 
121 {
122  if (colNum != B.getCols()) {
123  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-row vector from (1x%d) sub-row vector",
124  colNum, B.getCols()));
125  }
126  m_pColNum = B.m_pColNum;
127  m_parent = B.m_parent;
128  for (unsigned int i = 0; i < rowNum; ++i) {
129  data[i] = B[i];
130  }
131 
132  return *this;
133 }
134 
142 {
143  if (colNum != B.getCols()) {
144  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-row vector from (1x%d) row vector",
145  colNum, B.getCols()));
146  }
147 
148  for (unsigned int i = 0; i < rowNum; ++i) {
149  data[i] = B[i];
150  }
151 
152  return *this;
153 }
154 
162 {
163  if ((B.getRows() != 1) || (colNum != B.getCols())) {
164  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-column vector from (%dx%d) matrix",
165  colNum, B.getRows(), B.getCols()));
166  }
167 
168  for (unsigned int i = 0; i < rowNum; ++i) {
169  data[i] = B[i][1];
170  }
171  return *this;
172 }
173 
179 {
180  for (unsigned int i = 0; i < rowNum; ++i) {
181  data[i] = x;
182  }
183  return *this;
184 }
185 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:1102
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:1098
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:1104
unsigned int getRows() const
Definition: vpArray2D.h:347
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:1100
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:124
VP_DEPRECATED void init()
Definition: vpRowVector.h:335
vpRowVector * m_parent
Parent vpColVector.
vpSubRowVector & operator=(const vpSubRowVector &B)
virtual ~vpSubRowVector() VP_OVERRIDE
void checkParentStatus() const
vpSubRowVector()
Default constructor that creates an empty vector.
unsigned int m_pColNum
Number of row of parent vpColVector at initialization.