Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpSubColVector.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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(), pRowNum(0), parent(NULL) {}
46 
53 vpSubColVector::vpSubColVector(vpColVector &v, const unsigned int &offset, const unsigned int &nrows)
54  : vpColVector(), pRowNum(0), parent(NULL)
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  pRowNum = v.getRows();
80  parent = &v;
81 
82  if (rowPtrs) {
83  free(rowPtrs);
84  }
85 
86  rowPtrs = (double **)malloc(parent->getRows() * sizeof(double *));
87  for (unsigned int i = 0; i < nrows; i++)
88  rowPtrs[i] = v.data + i + offset;
89 
90  dsize = rowNum;
91  } else {
92  throw(vpException(vpException::dimensionError, "Cannot create a sub-column vector that is not "
93  "completely containt in the parrent column vector"));
94  }
95 }
96 
99 
106 {
107  if (!data) {
108  throw(vpException(vpException::fatalError, "The parent of the current sub-column vector has been destroyed"));
109  }
110  if (pRowNum != parent->getRows()) {
111  throw(vpException(vpException::dimensionError, "The size of the parent sub-column vector has changed"));
112  }
113 }
114 
123 {
124  if (rowNum != B.getRows()) {
126  "Cannot initialize (%dx1) sub-column vector from "
127  "(%dx1) sub-column vector",
128  rowNum, B.getRows()));
129  }
130  pRowNum = B.pRowNum;
131  for (unsigned int i = 0; i < rowNum; i++)
132  data[i] = B[i];
133  return *this;
134 }
135 
142 {
143  if (rowNum != B.getRows()) {
145  "Cannot initialize (%dx1) sub-column vector from "
146  "(%dx1) column vector",
147  rowNum, B.getRows()));
148  }
149 
150  for (unsigned int i = 0; i < rowNum; i++)
151  data[i] = B[i];
152 
153  return *this;
154 }
155 
162 {
163  if ((B.getCols() != 1) || (rowNum != B.getRows())) {
164  throw(vpException(vpException::dimensionError, "Cannot initialize (%dx1) sub-column vector from (%dx%d) matrix",
165  rowNum, B.getRows(), B.getCols()));
166  }
167 
168  for (unsigned int i = 0; i < rowNum; i++)
169  data[i] = B[i][1];
170  return *this;
171 }
172 
178 {
179  for (unsigned int i = 0; i < rowNum; i++)
180  data[i] = x;
181  return *this;
182 }
183 
188 {
189  unsigned int k = tv.getRows();
190  if (rowNum != k) {
191  try {
192  resize(k);
193  } catch (...) {
194  throw;
195  }
196  }
197 
198  memcpy(data, tv.data, rowNum * sizeof(double));
199  return *this;
200 }
205 {
206  unsigned int k = rv.getRows();
207  if (rowNum != k) {
208  try {
209  resize(k);
210  } catch (...) {
211  throw;
212  }
213  }
214 
215  memcpy(data, rv.data, rowNum * sizeof(double));
216  return *this;
217 }
222 {
223  unsigned int k = p.getRows();
224  if (rowNum != k) {
225  try {
226  resize(k);
227  } catch (...) {
228  throw;
229  }
230  }
231 
232  memcpy(data, p.data, rowNum * sizeof(double));
233  return *this;
234 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
Implementation of a generic rotation vector.
vp_deprecated void init()
Definition: vpColVector.h:311
unsigned int pRowNum
Number of row of parent vpColvector at initialization.
error that can be emited by ViSP classes.
Definition: vpException.h:71
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int getCols() const
Definition: vpArray2D.h:146
void checkParentStatus() const
virtual ~vpSubColVector()
Destructor that set the pointer to the parrent column vector to NULL.
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
vpColVector * parent
Parent vpColvector.
unsigned int getRows() const
Definition: vpArray2D.h:156
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:76
vpSubColVector & operator=(const vpSubColVector &B)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
vpSubColVector()
Default constructor that creates an empty vector.
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:80
Class that consider the case of a translation vector.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:244