Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpSubRowVector.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 
40 vpSubRowVector::vpSubRowVector() : vpRowVector(), m_pColNum(0), m_parent(nullptr) { }
41 
48 vpSubRowVector::vpSubRowVector(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
49  : vpRowVector(), m_pColNum(0), m_parent(nullptr)
50 {
51  init(v, offset, ncols);
52 }
53 
60 void vpSubRowVector::init(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
61 {
62  if (!v.data) {
63  throw(vpException(vpException::fatalError, "Cannot initialize a sub-row "
64  "vector from an empty parent row vector"));
65  }
66 
67  if ((offset + ncols) <= v.getCols()) {
68  data = v.data + offset;
69 
70  rowNum = 1;
71  colNum = ncols;
72 
73  m_pColNum = v.getCols();
74  m_parent = &v;
75 
76  if (rowPtrs) {
77  free(rowPtrs);
78  }
79 
80  rowPtrs = (double **)malloc(1 * sizeof(double *));
81  for (unsigned int i = 0; i < 1; ++i) {
82  rowPtrs[i] = v.data + i + offset;
83  }
84 
85  dsize = colNum;
86  }
87  else {
88  throw(vpException(vpException::dimensionError, "Cannot create a sub-row vector that is not completely "
89  "contained in the parent row vector"));
90  }
91 }
92 
97 
104 {
105  if (!data) {
106  throw(vpException(vpException::fatalError, "The parent of the current sub-row vector has been destroyed"));
107  }
108  if (m_pColNum != m_parent->getCols()) {
109  throw(vpException(vpException::dimensionError, "The size of the parent sub-row vector has changed"));
110  }
111 }
112 
120 {
121  if (colNum != B.getCols()) {
122  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-row vector from (1x%d) sub-row vector",
123  colNum, B.getCols()));
124  }
125  m_pColNum = B.m_pColNum;
126  m_parent = B.m_parent;
127  for (unsigned int i = 0; i < rowNum; ++i) {
128  data[i] = B[i];
129  }
130 
131  return *this;
132 }
133 
141 {
142  if (colNum != B.getCols()) {
143  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-row vector from (1x%d) row vector",
144  colNum, B.getCols()));
145  }
146 
147  for (unsigned int i = 0; i < rowNum; ++i) {
148  data[i] = B[i];
149  }
150 
151  return *this;
152 }
153 
161 {
162  if ((B.getRows() != 1) || (colNum != B.getCols())) {
163  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-column vector from (%dx%d) matrix",
164  colNum, B.getRows(), B.getCols()));
165  }
166 
167  for (unsigned int i = 0; i < rowNum; ++i) {
168  data[i] = B[i][1];
169  }
170  return *this;
171 }
172 
178 {
179  for (unsigned int i = 0; i < rowNum; ++i) {
180  data[i] = x;
181  }
182  return *this;
183 }
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 ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
vp_deprecated void init()
Definition: vpRowVector.h:306
vpRowVector * m_parent
Parent vpColVector.
virtual ~vpSubRowVector() vp_override
vpSubRowVector & operator=(const vpSubRowVector &B)
vpSubRowVector()
Default constructor that creates an empty vector.
void checkParentStatus() const
unsigned int m_pColNum
Number of row of parent vpColVector at initialization.