Visual Servoing Platform  version 3.4.0
vpSubRowVector.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 vpRowVector .
33  *
34  * Authors:
35  * Laneurit Jean
36  *
37  *****************************************************************************/
38 
39 #include <stdlib.h>
40 
41 #include <visp3/core/vpException.h>
42 #include <visp3/core/vpSubRowVector.h>
43 
45 vpSubRowVector::vpSubRowVector() : vpRowVector(), pColNum(0), parent(NULL) {}
46 
53 vpSubRowVector::vpSubRowVector(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
54  : vpRowVector(), pColNum(0), parent(NULL)
55 {
56  init(v, offset, ncols);
57 }
58 
65 void vpSubRowVector::init(vpRowVector &v, const unsigned int &offset, const unsigned int &ncols)
66 {
67  if (!v.data) {
68  throw(vpException(vpException::fatalError, "Cannot initialize a sub-row "
69  "vector from an empty parent "
70  "row vector"));
71  }
72 
73  if (offset + ncols <= v.getCols()) {
74  data = v.data + offset;
75 
76  rowNum = 1;
77  colNum = ncols;
78 
79  pColNum = v.getCols();
80  parent = &v;
81 
82  if (rowPtrs)
83  free(rowPtrs);
84 
85  rowPtrs = (double **)malloc(1 * sizeof(double *));
86  for (unsigned int i = 0; i < 1; i++)
87  rowPtrs[i] = v.data + i + offset;
88 
89  dsize = colNum;
90  } else {
91  throw(vpException(vpException::dimensionError, "Cannot create a sub-row vector that is not completely "
92  "containt in the parrent row vector"));
93  }
94 }
95 
98 
105 {
106  if (!data) {
107  throw(vpException(vpException::fatalError, "The parent of the current sub-row vector has been destroyed"));
108  }
109  if (pColNum != 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  pColNum = B.pColNum;
127  parent = B.parent;
128  for (unsigned int i = 0; i < rowNum; i++)
129  data[i] = B[i];
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  return *this;
151 }
152 
160 {
161  if ((B.getRows() != 1) || (colNum != B.getCols())) {
162  throw(vpException(vpException::dimensionError, "Cannot initialize (1x%d) sub-column vector from (%dx%d) matrix",
163  colNum, B.getRows(), B.getCols()));
164  }
165 
166  for (unsigned int i = 0; i < rowNum; i++)
167  data[i] = B[i][1];
168  return *this;
169 }
175 {
176  for (unsigned int i = 0; i < rowNum; i++)
177  data[i] = x;
178  return *this;
179 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:115
error that can be emited by ViSP classes.
Definition: vpException.h:71
void checkParentStatus() const
vp_deprecated void init()
Definition: vpRowVector.h:321
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:145
unsigned int getCols() const
Definition: vpArray2D.h:279
vpSubRowVector & operator=(const vpSubRowVector &B)
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:135
vpRowVector * parent
Parent vpColvector.
unsigned int pColNum
Number of row of parent vpColvector at initialization.
unsigned int getRows() const
Definition: vpArray2D.h:289
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:137
virtual ~vpSubRowVector()
Destructor that set the pointer to the parrent row vector to NULL.
vpSubRowVector()
Default constructor that creates an empty vector.
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:141
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:139