Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpSubColVector.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 vpColVector.
32  */
33 
34 #include <stdlib.h>
35 
36 #include <visp3/core/vpException.h>
37 #include <visp3/core/vpSubColVector.h>
38 
39 BEGIN_VISP_NAMESPACE
41 vpSubColVector::vpSubColVector() : vpColVector(), m_pRowNum(0), m_parent(nullptr) { }
42 
49 vpSubColVector::vpSubColVector(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
50  : vpColVector(), m_pRowNum(0), m_parent(nullptr)
51 {
52  init(v, offset, nrows);
53 }
54 
61 void vpSubColVector::init(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
62 {
63  if (!v.data) {
64  throw(vpException(vpException::fatalError, "Cannot initialize a "
65  "sub-column vector from an "
66  "empty parent column vector"));
67  }
68 
69  if ((offset + nrows) <= v.getRows()) {
70  data = v.data + offset;
71 
72  rowNum = nrows;
73  colNum = 1;
74 
75  m_pRowNum = v.getRows();
76  m_parent = &v;
77 
78  if (rowPtrs) {
79  free(rowPtrs);
80  }
81 
82  rowPtrs = (double **)malloc(m_parent->getRows() * sizeof(double *));
83  for (unsigned int i = 0; i < nrows; ++i) {
84  rowPtrs[i] = v.data + (i + offset);
85  }
86 
87  dsize = rowNum;
88  }
89  else {
90  throw(vpException(vpException::dimensionError, "Cannot create a sub-column vector that is not "
91  "completely contained in the parent column vector"));
92  }
93 }
94 
99 
106 {
107  if (!data) {
108  throw(vpException(vpException::fatalError, "The parent of the current sub-column vector has been destroyed"));
109  }
110  if (m_pRowNum != m_parent->getRows()) {
111  throw(vpException(vpException::dimensionError, "The size of the parent sub-column vector has changed"));
112  }
113 }
114 
122 {
123  if (rowNum != B.getRows()) {
125  "Cannot initialize (%dx1) sub-column vector from "
126  "(%dx1) sub-column vector",
127  rowNum, B.getRows()));
128  }
129  m_pRowNum = B.m_pRowNum;
130  for (unsigned int i = 0; i < rowNum; ++i) {
131  data[i] = B[i];
132  }
133  return *this;
134 }
135 
143 {
144  if (rowNum != B.getRows()) {
146  "Cannot initialize (%dx1) sub-column vector from "
147  "(%dx1) column vector",
148  rowNum, B.getRows()));
149  }
150 
151  for (unsigned int i = 0; i < rowNum; ++i) {
152  data[i] = B[i];
153  }
154 
155  return *this;
156 }
157 
165 {
166  if ((B.getCols() != 1) || (rowNum != B.getRows())) {
167  throw(vpException(vpException::dimensionError, "Cannot initialize (%dx1) sub-column vector from (%dx%d) matrix",
168  rowNum, B.getRows(), B.getCols()));
169  }
170 
171  for (unsigned int i = 0; i < rowNum; ++i) {
172  data[i] = B[i][1];
173  }
174  return *this;
175 }
176 
183 {
184  for (unsigned int i = 0; i < rowNum; ++i) {
185  data[i] = x;
186  }
187  return *this;
188 }
189 
194 {
195  unsigned int k = tv.getRows();
196  if (rowNum != k) {
197  try {
198  resize(k);
199  }
200  catch (...) {
201  throw;
202  }
203  }
204 
205  memcpy(data, tv.data, rowNum * sizeof(double));
206  return *this;
207 }
208 
213 {
214  unsigned int k = rv.getRows();
215  if (rowNum != k) {
216  try {
217  resize(k);
218  }
219  catch (...) {
220  throw;
221  }
222  }
223 
224  memcpy(data, rv.data, rowNum * sizeof(double));
225  return *this;
226 }
227 
232 {
233  unsigned int k = p.getRows();
234  if (rowNum != k) {
235  try {
236  resize(k);
237  }
238  catch (...) {
239  throw;
240  }
241  }
242 
243  memcpy(data, p.data, rowNum * sizeof(double));
244  return *this;
245 }
246 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
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
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 a pose vector and operations on poses.
Definition: vpPoseVector.h:203
Implementation of a generic rotation vector.
void checkParentStatus() const
vpSubColVector & operator=(const vpSubColVector &B)
vpColVector * m_parent
Parent vpColVector.
vpSubColVector()
Default constructor that creates an empty vector.
virtual ~vpSubColVector() VP_OVERRIDE
void init(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
unsigned int m_pRowNum
Number of row of parent vpColVector at initialization.
Class that consider the case of a translation vector.