Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpSubColVector.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 vpColVector.
33  *
34  * Authors:
35  * Laneurit Jean
36  *
37 *****************************************************************************/
38 
39 #include <stdlib.h>
40 
41 #include <visp3/core/vpException.h>
42 #include <visp3/core/vpSubColVector.h>
43 
45 vpSubColVector::vpSubColVector() : vpColVector(), m_pRowNum(0), m_parent(nullptr) { }
46 
53 vpSubColVector::vpSubColVector(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
54  : vpColVector(), m_pRowNum(0), m_parent(nullptr)
55 {
56  init(v, offset, nrows);
57 }
58 
65 void vpSubColVector::init(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
66 {
67  if (!v.data) {
68  throw(vpException(vpException::fatalError, "Cannot initialize a "
69  "sub-column vector from an "
70  "empty parent column vector"));
71  }
72 
73  if ((offset + nrows) <= v.getRows()) {
74  data = v.data + offset;
75 
76  rowNum = nrows;
77  colNum = 1;
78 
79  m_pRowNum = v.getRows();
80  m_parent = &v;
81 
82  if (rowPtrs) {
83  free(rowPtrs);
84  }
85 
86  rowPtrs = (double **)malloc(m_parent->getRows() * sizeof(double *));
87  for (unsigned int i = 0; i < nrows; ++i) {
88  rowPtrs[i] = v.data + (i + offset);
89  }
90 
91  dsize = rowNum;
92  }
93  else {
94  throw(vpException(vpException::dimensionError, "Cannot create a sub-column vector that is not "
95  "completely contained in the parent column vector"));
96  }
97 }
98 
103 
110 {
111  if (!data) {
112  throw(vpException(vpException::fatalError, "The parent of the current sub-column vector has been destroyed"));
113  }
114  if (m_pRowNum != m_parent->getRows()) {
115  throw(vpException(vpException::dimensionError, "The size of the parent sub-column vector has changed"));
116  }
117 }
118 
126 {
127  if (rowNum != B.getRows()) {
129  "Cannot initialize (%dx1) sub-column vector from "
130  "(%dx1) sub-column vector",
131  rowNum, B.getRows()));
132  }
133  m_pRowNum = B.m_pRowNum;
134  for (unsigned int i = 0; i < rowNum; ++i) {
135  data[i] = B[i];
136  }
137  return *this;
138 }
139 
147 {
148  if (rowNum != B.getRows()) {
150  "Cannot initialize (%dx1) sub-column vector from "
151  "(%dx1) column vector",
152  rowNum, B.getRows()));
153  }
154 
155  for (unsigned int i = 0; i < rowNum; ++i) {
156  data[i] = B[i];
157  }
158 
159  return *this;
160 }
161 
169 {
170  if ((B.getCols() != 1) || (rowNum != B.getRows())) {
171  throw(vpException(vpException::dimensionError, "Cannot initialize (%dx1) sub-column vector from (%dx%d) matrix",
172  rowNum, B.getRows(), B.getCols()));
173  }
174 
175  for (unsigned int i = 0; i < rowNum; ++i) {
176  data[i] = B[i][1];
177  }
178  return *this;
179 }
180 
187 {
188  for (unsigned int i = 0; i < rowNum; ++i) {
189  data[i] = x;
190  }
191  return *this;
192 }
193 
198 {
199  unsigned int k = tv.getRows();
200  if (rowNum != k) {
201  try {
202  resize(k);
203  }
204  catch (...) {
205  throw;
206  }
207  }
208 
209  memcpy(data, tv.data, rowNum * sizeof(double));
210  return *this;
211 }
212 
217 {
218  unsigned int k = rv.getRows();
219  if (rowNum != k) {
220  try {
221  resize(k);
222  }
223  catch (...) {
224  throw;
225  }
226  }
227 
228  memcpy(data, rv.data, rowNum * sizeof(double));
229  return *this;
230 }
231 
236 {
237  unsigned int k = p.getRows();
238  if (rowNum != k) {
239  try {
240  resize(k);
241  }
242  catch (...) {
243  throw;
244  }
245  }
246 
247  memcpy(data, p.data, rowNum * sizeof(double));
248  return *this;
249 }
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
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vp_deprecated void init()
Definition: vpColVector.h:1355
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
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 a pose vector and operations on poses.
Definition: vpPoseVector.h:189
Implementation of a generic rotation vector.
virtual ~vpSubColVector() vp_override
void checkParentStatus() const
vpSubColVector & operator=(const vpSubColVector &B)
vpColVector * m_parent
Parent vpColVector.
vpSubColVector()
Default constructor that creates an empty vector.
unsigned int m_pRowNum
Number of row of parent vpColVector at initialization.
Class that consider the case of a translation vector.