Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
vpColVector.h
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  * Provide some simple operation on column vectors.
32  */
33 
40 #ifndef VP_COLVECTOR_H
41 #define VP_COLVECTOR_H
42 
43 #include <visp3/core/vpConfig.h>
44 
45 #ifdef VISP_HAVE_NLOHMANN_JSON
46 #include VISP_NLOHMANN_JSON(json.hpp)
47 #endif
48 BEGIN_VISP_NAMESPACE
49 class vpMatrix;
50 class vpRowVector;
51 class vpRotationVector;
53 class vpPoseVector;
54 END_VISP_NAMESPACE
55 
56 #include <visp3/core/vpArray2D.h>
57 #include <visp3/core/vpMath.h>
58 #include <visp3/core/vpPoseVector.h>
59 #include <visp3/core/vpRotationVector.h>
60 #include <visp3/core/vpRowVector.h>
61 
62 BEGIN_VISP_NAMESPACE
190 class VISP_EXPORT vpColVector : public vpArray2D<double>
191 {
192  friend class vpMatrix;
193 
194 public:
198  vpColVector() : vpArray2D<double>() { }
199 
205  VP_EXPLICIT vpColVector(unsigned int n) : vpArray2D<double>(n, 1) { }
206 
210  vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) { }
211 
215  vpColVector(const vpColVector &v) : vpArray2D<double>(v) { }
216 
231  vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
232 
237  VP_EXPLICIT vpColVector(const vpRotationVector &v);
238 
242  VP_EXPLICIT vpColVector(const vpPoseVector &p);
243 
247  VP_EXPLICIT vpColVector(const vpTranslationVector &t);
248 
255  VP_EXPLICIT vpColVector(const vpMatrix &M);
256 
260  vpColVector(const vpMatrix &M, unsigned int j);
261 
265  VP_EXPLICIT vpColVector(const std::vector<double> &v);
266 
270  VP_EXPLICIT vpColVector(const std::vector<float> &v);
271 
272 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
277 #endif
278 
279 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
280  vpColVector(const std::initializer_list<double> &list) : vpArray2D<double>(static_cast<unsigned int>(list.size()), 1)
281  {
282  std::copy(list.begin(), list.end(), data);
283  }
284 #endif
285 
290  void clear()
291  {
292  if (data != nullptr) {
293  free(data);
294  data = nullptr;
295  }
296 
297  if (rowPtrs != nullptr) {
298  free(rowPtrs);
299  rowPtrs = nullptr;
300  }
301  rowNum = 0;
302  colNum = 0;
303  dsize = 0;
304  }
305 
340  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
341 
372  std::ostream &csvPrint(std::ostream &os) const;
373 
381  {
382  double d2r = M_PI / 180.0;
383 
384  (*this) *= d2r;
385  return (*this);
386  }
387 
405  vpColVector extract(unsigned int r, unsigned int colsize) const
406  {
407  if ((r >= rowNum) || ((r + colsize) > rowNum)) {
409  "Cannot extract a (%dx1) column vector from a (%dx1) "
410  "column vector starting at index %d",
411  colsize, rowNum, r));
412  }
413 
414  return vpColVector(*this, r, colsize);
415  }
416 
427  double frobeniusNorm() const;
428 
436  vpColVector hadamard(const vpColVector &v) const;
437 
448  double infinityNorm() const;
449 
490  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
491 
528  void insert(unsigned int i, const vpColVector &v);
529 
561  std::ostream &maplePrint(std::ostream &os) const;
562 
605  std::ostream &matlabPrint(std::ostream &os) const;
606 
617  vpColVector &normalize();
618 
630  vpColVector &normalize(vpColVector &x) const;
631 
635  inline double &operator[](unsigned int n) { return *(data + n); }
636 
640  inline const double &operator[](unsigned int n) const { return *(data + n); }
641 
650  vpColVector &operator=(const vpColVector &v);
651 
656 
661 
666 
672  vpColVector &operator=(const vpMatrix &M);
673 
677  vpColVector &operator=(const std::vector<double> &v);
678 
682  vpColVector &operator=(const std::vector<float> &v);
683 
687  vpColVector &operator=(double x);
688 
689 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
694 
722  vpColVector &operator=(const std::initializer_list<double> &list);
723 #endif
724 
732  bool operator==(const vpColVector &v) const;
733 
741  bool operator==(double v) const;
742 
749  bool operator!=(const vpColVector &v) const;
750 
758  bool operator!=(double v) const;
759 
767  double operator*(const vpColVector &v) const;
768 
776  vpMatrix operator*(const vpRowVector &v) const;
777 
785  vpMatrix operator*(const vpMatrix &M) const;
786 
807  vpColVector operator*(double x) const;
808 
827  vpColVector &operator*=(double x);
828 
848  vpColVector operator/(double x) const;
849 
867  vpColVector &operator/=(double x);
868 
872  vpColVector operator+(const vpColVector &v) const;
873 
895 
900 
904  vpColVector operator-(const vpColVector &v) const;
905 
910 
921  vpColVector operator-() const;
922 
948 
977  vpColVector &operator<<(double *x);
978 
1001  vpColVector &operator<<(double val);
1002 
1025  vpColVector &operator,(double val);
1026 
1045  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
1046 
1054  {
1055  double r2d = 180.0 / M_PI;
1056 
1057  (*this) *= r2d;
1058  return (*this);
1059  }
1060 
1121  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
1122 
1132  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
1133 
1143  void resize(unsigned int i, bool flagNullify = true)
1144  {
1145  vpArray2D<double>::resize(i, 1, flagNullify);
1146  }
1147 
1159  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
1160  {
1161  if (ncols != 1) {
1163  "Cannot resize a column vector to a (%dx%d) "
1164  "dimension vector that has more than one column",
1165  nrows, ncols));
1166  }
1167  vpArray2D<double>::resize(nrows, ncols, flagNullify);
1168  }
1169 
1185  void stack(double d);
1186 
1205  void stack(const vpColVector &v);
1206 
1213  double sum() const;
1214 
1221  double sumSquare() const;
1222 
1226  vpRowVector t() const;
1227 
1232  std::vector<double> toStdVector() const;
1233 
1238  vpRowVector transpose() const;
1239 
1244  void transpose(vpRowVector &v) const;
1245 
1259  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
1260 
1272  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
1273 
1283  static double dotProd(const vpColVector &a, const vpColVector &b);
1284 
1317  static vpColVector invSort(const vpColVector &v);
1318 
1322  static double median(const vpColVector &v);
1323 
1327  static double mean(const vpColVector &v);
1328 
1343  static vpMatrix skew(const vpColVector &v);
1344 
1376  static vpColVector sort(const vpColVector &v);
1377 
1396  static vpColVector stack(const vpColVector &A, const vpColVector &B);
1397 
1416  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
1417 
1421  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
1422 
1423 #ifdef VISP_HAVE_NLOHMANN_JSON
1430  friend void to_json(nlohmann::json &j, const vpColVector &v);
1431 
1438  friend void from_json(const nlohmann::json &j, vpColVector &v);
1439 #endif
1440 
1441 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1450  VP_DEPRECATED void init() { }
1451 
1491  VP_DEPRECATED void insert(const vpColVector &v, unsigned int i);
1492 
1496  VP_DEPRECATED vpColVector rows(unsigned int first_row, unsigned int last_row) const
1497  {
1498  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
1499  }
1500 
1504  VP_DEPRECATED void stackMatrices(const vpColVector &r) { stack(r); }
1505 
1509  VP_DEPRECATED static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
1510 
1514  VP_DEPRECATED static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
1515  {
1516  stack(A, B, C);
1517  }
1518 
1532  VP_DEPRECATED void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
1533 
1543  VP_DEPRECATED double euclideanNorm() const;
1545 #endif
1546 };
1547 
1552 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1553 VISP_EXPORT
1554 #endif
1555 vpColVector operator*(const double &x, const vpColVector &v);
1556 
1557 
1558 #ifdef VISP_HAVE_NLOHMANN_JSON
1559 inline void to_json(nlohmann::json &j, const vpColVector &v)
1560 {
1561  const vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1562  to_json(j, *asArray);
1563  j["type"] = "vpColVector";
1564 }
1565 
1566 inline void from_json(const nlohmann::json &j, vpColVector &v)
1567 {
1568  vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1569  from_json(j, *asArray);
1570  if (v.getCols() != 1) {
1571  throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
1572  }
1573 }
1574 #endif
1575 END_VISP_NAMESPACE
1576 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
unsigned int getCols() const
Definition: vpArray2D.h:337
double * 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
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:497
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:1101
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:614
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:1107
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1166
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1351
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:526
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1150
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:456
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:1103
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1294
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:405
vpColVector(unsigned int n, double val)
Definition: vpColVector.h:210
VP_EXPLICIT vpColVector(unsigned int n)
Definition: vpColVector.h:205
vpColVector & rad2deg()
Definition: vpColVector.h:1053
double & operator[](unsigned int n)
Definition: vpColVector.h:635
vpColVector(const vpColVector &v)
Definition: vpColVector.h:215
const double & operator[](unsigned int n) const
Definition: vpColVector.h:640
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector & deg2rad()
Definition: vpColVector.h:380
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:1159
void clear()
Definition: vpColVector.h:290
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:1259
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Definition: vpMatrix.cpp:1100
vpMatrix operator-() const
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:824
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:998
void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols)
Definition: vpMatrix.cpp:357
vpMatrix operator*(const vpMatrix &B) const
vpMatrix operator/(double x) const
Cij = Aij / x (A is unchanged)
void stack(const vpMatrix &A)
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
vpMatrix & operator*=(double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
vpMatrix operator+(const vpMatrix &B) const
double sum() const
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
double infinityNorm() const
Definition: vpMatrix.cpp:1950
double frobeniusNorm() const
Definition: vpMatrix.cpp:1890
vpMatrix & operator,(double val)
double sumSquare() const
Definition: vpMatrix.cpp:1971
vpMatrix transpose() const
vpMatrix t() const
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:1045
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:947
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:124
Class that consider the case of a translation vector.