Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpColVector.h
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  * Provide some simple operation on column vectors.
32  */
33 
34 #ifndef _vpColVector_h_
35 #define _vpColVector_h_
36 
37 #include <visp3/core/vpArray2D.h>
38 #include <visp3/core/vpMath.h>
39 #include <visp3/core/vpPoseVector.h>
40 #include <visp3/core/vpRotationVector.h>
41 #include <visp3/core/vpRowVector.h>
42 
43 class vpMatrix;
44 class vpRowVector;
45 class vpRotationVector;
47 class vpPoseVector;
48 
166 class VISP_EXPORT vpColVector : public vpArray2D<double>
167 {
168  friend class vpMatrix;
169 
170 public:
172  vpColVector() : vpArray2D<double>() { }
176  explicit vpColVector(unsigned int n) : vpArray2D<double>(n, 1) { }
178  vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) { }
181  vpColVector(const vpColVector &v) : vpArray2D<double>(v) { }
182  vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
185  vpColVector(const vpRotationVector &v);
187  vpColVector(const vpPoseVector &p);
191  vpColVector(const vpMatrix &M);
192  vpColVector(const vpMatrix &M, unsigned int j);
193  vpColVector(const std::vector<double> &v);
194  vpColVector(const std::vector<float> &v);
195 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
197  vpColVector(const std::initializer_list<double> &list) : vpArray2D<double>(static_cast<unsigned int>(list.size()), 1)
198  {
199  std::copy(list.begin(), list.end(), data);
200  }
201 #endif
205  virtual ~vpColVector() { }
206 
211  void clear()
212  {
213  if (data != NULL) {
214  free(data);
215  data = NULL;
216  }
217 
218  if (rowPtrs != NULL) {
219  free(rowPtrs);
220  rowPtrs = NULL;
221  }
222  rowNum = colNum = dsize = 0;
223  }
224 
225  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
226  std::ostream &csvPrint(std::ostream &os) const;
227 
234  {
235  double d2r = M_PI / 180.0;
236 
237  (*this) *= d2r;
238  return (*this);
239  }
240 
257  vpColVector extract(unsigned int r, unsigned int colsize) const
258  {
259  if (r >= rowNum || r + colsize > rowNum) {
261  "Cannot extract a (%dx1) column vector from a (%dx1) "
262  "column vector starting at index %d",
263  colsize, rowNum, r));
264  }
265 
266  return vpColVector(*this, r, colsize);
267  }
268 
269  double frobeniusNorm() const;
270  vpColVector hadamard(const vpColVector &v) const;
271 
272  double infinityNorm() const;
273  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
274  void insert(unsigned int i, const vpColVector &v);
275  void insert(const vpColVector &v, unsigned int i);
276 
277  std::ostream &maplePrint(std::ostream &os) const;
278  std::ostream &matlabPrint(std::ostream &os) const;
279 
280  vpColVector &normalize();
281  vpColVector &normalize(vpColVector &x) const;
282 
284  inline double &operator[](unsigned int n) { return *(data + n); }
286  inline const double &operator[](unsigned int n) const { return *(data + n); }
288  vpColVector &operator=(const vpColVector &v);
292  vpColVector &operator=(const vpMatrix &M);
293  vpColVector &operator=(const std::vector<double> &v);
294  vpColVector &operator=(const std::vector<float> &v);
295  vpColVector &operator=(double x);
296 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
298  vpColVector &operator=(const std::initializer_list<double> &list);
299 #endif
301  bool operator==(const vpColVector &v) const;
302  bool operator==(double v) const;
303  bool operator!=(const vpColVector &v) const;
304  bool operator!=(double v) const;
305 
306  double operator*(const vpColVector &x) const;
307  vpMatrix operator*(const vpRowVector &v) const;
308  vpColVector operator*(double x) const;
309  vpColVector &operator*=(double x);
310 
311  vpColVector operator/(double x) const;
312  vpColVector &operator/=(double x);
313 
314  vpColVector operator+(const vpColVector &v) const;
317 
318  vpColVector operator-(const vpColVector &v) const;
320  vpColVector operator-() const;
321 
323  vpColVector &operator<<(double *);
324  vpColVector &operator<<(double val);
325  vpColVector &operator,(double val);
326 
327  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
328 
335  {
336  double r2d = 180.0 / M_PI;
337 
338  (*this) *= r2d;
339  return (*this);
340  }
341 
342  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
343  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
344 
351  void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(i, 1, flagNullify); }
362  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
363  {
364  if (ncols != 1) {
366  "Cannot resize a column vector to a (%dx%d) "
367  "dimension vector that has more than one column",
368  nrows, ncols));
369  }
370  vpArray2D<double>::resize(nrows, ncols, flagNullify);
371  }
372 
373  void stack(double d);
374  void stack(const vpColVector &v);
375 
376  double sum() const;
377  double sumSquare() const;
378  vpRowVector t() const;
379  std::vector<double> toStdVector() const;
380  vpRowVector transpose() const;
381  void transpose(vpRowVector &v) const;
382 
391  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
392  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
393 
394  static double dotProd(const vpColVector &a, const vpColVector &b);
395  static vpColVector invSort(const vpColVector &v);
396  static double median(const vpColVector &v);
397  static double mean(const vpColVector &v);
398  // Compute the skew matrix [v]x
399  static vpMatrix skew(const vpColVector &v);
400 
401  static vpColVector sort(const vpColVector &v);
402 
403  static vpColVector stack(const vpColVector &A, const vpColVector &B);
404  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
405 
406  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
407 
408 #ifdef VISP_HAVE_NLOHMANN_JSON
409  friend void to_json(nlohmann::json &j, const vpColVector &cam);
410  friend void from_json(const nlohmann::json &j, vpColVector &cam);
411 #endif
412 
413 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
422  vp_deprecated void init() { }
426  vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
427  {
428  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
429  }
433  vp_deprecated void setIdentity(const double &val = 1.0);
437  vp_deprecated void stackMatrices(const vpColVector &r) { stack(r); }
442  vp_deprecated static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
447  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
448  {
449  stack(A, B, C);
450  }
451 
452  vp_deprecated void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
453  vp_deprecated double euclideanNorm() const;
455 #endif
456 };
457 
458 #ifndef DOXYGEN_SHOULD_SKIP_THIS
459 VISP_EXPORT
460 #endif
461 vpColVector operator*(const double &x, const vpColVector &v);
462 
463 #ifdef VISP_HAVE_NLOHMANN_JSON
464 inline void to_json(nlohmann::json &j, const vpColVector &v)
465 {
466  const vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
467  to_json(j, *asArray);
468  j["type"] = "vpColVector";
469 }
470 inline void from_json(const nlohmann::json &j, vpColVector &v)
471 {
472  vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
473  from_json(j, *asArray);
474  if (v.getCols() != 1) {
475  throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
476  }
477 }
478 
479 
480 #endif
481 
482 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:131
unsigned int getCols() const
Definition: vpArray2D.h:280
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:144
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:138
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:417
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:134
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:529
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:140
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:292
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1059
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1227
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:445
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1043
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:383
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:136
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1173
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:257
vpColVector(unsigned int n, double val)
Construct a column vector of size n. Each element is set to val.
Definition: vpColVector.h:178
vpColVector & rad2deg()
Definition: vpColVector.h:334
vpColVector(unsigned int n)
Definition: vpColVector.h:176
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
Definition: vpColVector.h:442
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
Definition: vpColVector.h:447
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpColVector.h:284
vpColVector(const vpColVector &v)
Definition: vpColVector.h:181
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpColVector.h:286
vp_deprecated void init()
Definition: vpColVector.h:422
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:172
vpColVector & deg2rad()
Definition: vpColVector.h:233
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:362
virtual ~vpColVector()
Definition: vpColVector.h:205
vp_deprecated void setIdentity(const double &val=1.0)
void clear()
Definition: vpColVector.h:211
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
Definition: vpColVector.h:426
vpColVector(const std::initializer_list< double > &list)
Definition: vpColVector.h:197
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:391
vp_deprecated void stackMatrices(const vpColVector &r)
Definition: vpColVector.h:437
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:351
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
@ fatalError
Fatal error.
Definition: vpException.h:84
VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:152
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Definition: vpMatrix.cpp:5859
vpMatrix operator-() const
Definition: vpMatrix.cpp:1555
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
Definition: vpMatrix.cpp:1686
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:5606
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:5769
vpMatrix operator*(const vpMatrix &B) const
Definition: vpMatrix.cpp:1164
vpMatrix operator/(double x) const
Cij = Aij / x (A is unchanged)
Definition: vpMatrix.cpp:1626
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:5884
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
Definition: vpMatrix.cpp:1514
vpMatrix & operator*=(double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
Definition: vpMatrix.cpp:1672
vp_deprecated double euclideanNorm() const
Definition: vpMatrix.cpp:6815
vpMatrix operator+(const vpMatrix &B) const
Definition: vpMatrix.cpp:1408
double sum() const
Definition: vpMatrix.cpp:1562
vpMatrix t() const
Definition: vpMatrix.cpp:461
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
Definition: vpMatrix.cpp:1497
double infinityNorm() const
Definition: vpMatrix.cpp:6770
double frobeniusNorm() const
Definition: vpMatrix.cpp:6710
vpMatrix & operator,(double val)
Definition: vpMatrix.cpp:799
double sumSquare() const
Definition: vpMatrix.cpp:6791
vp_deprecated void init()
Definition: vpMatrix.h:1004
vpMatrix transpose() const
Definition: vpMatrix.cpp:468
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5810
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5724
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:192
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:111
Class that consider the case of a translation vector.