Visual Servoing Platform  version 3.6.1 under development (2025-02-17)
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
286  static vpColVector view(double *data, unsigned int rows);
287 
292  void clear()
293  {
294  if (!isMemoryOwner) {
295  throw vpException(vpException::fatalError, "Cannot clear a vector view");
296  }
297  if (data != nullptr) {
298  free(data);
299  data = nullptr;
300  }
301 
302  if (rowPtrs != nullptr) {
303  free(rowPtrs);
304  rowPtrs = nullptr;
305  }
306  rowNum = 0;
307  colNum = 0;
308  dsize = 0;
309  }
310 
345  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
346 
377  std::ostream &csvPrint(std::ostream &os) const;
378 
386  {
387  double d2r = M_PI / 180.0;
388 
389  (*this) *= d2r;
390  return (*this);
391  }
392 
410  vpColVector extract(unsigned int r, unsigned int colsize) const
411  {
412  if ((r >= rowNum) || ((r + colsize) > rowNum)) {
414  "Cannot extract a (%dx1) column vector from a (%dx1) "
415  "column vector starting at index %d",
416  colsize, rowNum, r));
417  }
418 
419  return vpColVector(*this, r, colsize);
420  }
421 
432  double frobeniusNorm() const;
433 
441  vpColVector hadamard(const vpColVector &v) const;
442 
453  double infinityNorm() const;
454 
495  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
496 
533  void insert(unsigned int i, const vpColVector &v);
534 
566  std::ostream &maplePrint(std::ostream &os) const;
567 
610  std::ostream &matlabPrint(std::ostream &os) const;
611 
622  vpColVector &normalize();
623 
635  vpColVector &normalize(vpColVector &x) const;
636 
640  inline double &operator[](unsigned int n) { return *(data + n); }
641 
645  inline const double &operator[](unsigned int n) const { return *(data + n); }
646 
655  vpColVector &operator=(const vpColVector &v);
656 
661 
666 
671 
677  vpColVector &operator=(const vpMatrix &M);
678 
682  vpColVector &operator=(const std::vector<double> &v);
683 
687  vpColVector &operator=(const std::vector<float> &v);
688 
692  vpColVector &operator=(double x);
693 
694 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
699 
727  vpColVector &operator=(const std::initializer_list<double> &list);
728 #endif
729 
737  bool operator==(const vpColVector &v) const;
738 
746  bool operator==(double v) const;
747 
754  bool operator!=(const vpColVector &v) const;
755 
763  bool operator!=(double v) const;
764 
772  double operator*(const vpColVector &v) const;
773 
781  vpMatrix operator*(const vpRowVector &v) const;
782 
790  vpMatrix operator*(const vpMatrix &M) const;
791 
812  vpColVector operator*(double x) const;
813 
832  vpColVector &operator*=(double x);
833 
853  vpColVector operator/(double x) const;
854 
872  vpColVector &operator/=(double x);
873 
877  vpColVector operator+(const vpColVector &v) const;
878 
900 
905 
909  vpColVector operator-(const vpColVector &v) const;
910 
915 
926  vpColVector operator-() const;
927 
953 
982  vpColVector &operator<<(double *x);
983 
1006  vpColVector &operator<<(double val);
1007 
1030  vpColVector &operator,(double val);
1031 
1050  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
1051 
1059  {
1060  double r2d = 180.0 / M_PI;
1061 
1062  (*this) *= r2d;
1063  return (*this);
1064  }
1065 
1126  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
1127 
1137  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
1138 
1148  void resize(unsigned int i, bool flagNullify = true)
1149  {
1150  vpArray2D<double>::resize(i, 1, flagNullify);
1151  }
1152 
1164  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
1165  {
1166  if (ncols != 1) {
1168  "Cannot resize a column vector to a (%dx%d) "
1169  "dimension vector that has more than one column",
1170  nrows, ncols));
1171  }
1172  vpArray2D<double>::resize(nrows, ncols, flagNullify);
1173  }
1174 
1190  void stack(double d);
1191 
1210  void stack(const vpColVector &v);
1211 
1218  double sum() const;
1219 
1226  double sumSquare() const;
1227 
1231  vpRowVector t() const;
1232 
1237  std::vector<double> toStdVector() const;
1238 
1243  vpRowVector transpose() const;
1244 
1249  void transpose(vpRowVector &v) const;
1250 
1264  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
1265 
1277  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
1278 
1288  static double dotProd(const vpColVector &a, const vpColVector &b);
1289 
1322  static vpColVector invSort(const vpColVector &v);
1323 
1327  static double median(const vpColVector &v);
1328 
1332  static double mean(const vpColVector &v);
1333 
1348  static vpMatrix skew(const vpColVector &v);
1349 
1381  static vpColVector sort(const vpColVector &v);
1382 
1401  static vpColVector stack(const vpColVector &A, const vpColVector &B);
1402 
1421  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
1422 
1426  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
1427 
1428 #ifdef VISP_HAVE_NLOHMANN_JSON
1435  friend void to_json(nlohmann::json &j, const vpColVector &v);
1436 
1443  friend void from_json(const nlohmann::json &j, vpColVector &v);
1444 #endif
1445 
1446 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1455  VP_DEPRECATED void init() { }
1456 
1496  VP_DEPRECATED void insert(const vpColVector &v, unsigned int i);
1497 
1501  VP_DEPRECATED vpColVector rows(unsigned int first_row, unsigned int last_row) const
1502  {
1503  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
1504  }
1505 
1509  VP_DEPRECATED void stackMatrices(const vpColVector &r) { stack(r); }
1510 
1514  VP_DEPRECATED static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
1515 
1519  VP_DEPRECATED static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
1520  {
1521  stack(A, B, C);
1522  }
1523 
1537  VP_DEPRECATED void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
1538 
1548  VP_DEPRECATED double euclideanNorm() const;
1550 #endif
1551 };
1552 
1557 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1558 VISP_EXPORT
1559 #endif
1560 vpColVector operator*(const double &x, const vpColVector &v);
1561 
1562 
1563 #ifdef VISP_HAVE_NLOHMANN_JSON
1564 inline void to_json(nlohmann::json &j, const vpColVector &v)
1565 {
1566  const vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1567  to_json(j, *asArray);
1568  j["type"] = "vpColVector";
1569 }
1570 
1571 inline void from_json(const nlohmann::json &j, vpColVector &v)
1572 {
1573  vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1574  from_json(j, *asArray);
1575  if (v.getCols() != 1) {
1576  throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
1577  }
1578 }
1579 #endif
1580 END_VISP_NAMESPACE
1581 #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:417
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:1190
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:580
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:442
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:1186
static vpArray2D< Type > view(const vpArray2D< Type > &A)
Creates a view of the Matrix A. A view shares the same underlying memory as the original array....
Definition: vpArray2D.h:323
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:699
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:1192
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:429
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1256
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1441
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:609
bool isMemoryOwner
Whether this array owns the memory it points to.
Definition: vpArray2D.h:1194
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1240
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:539
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:1188
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1384
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:410
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:1058
double & operator[](unsigned int n)
Definition: vpColVector.h:640
vpColVector(const vpColVector &v)
Definition: vpColVector.h:215
const double & operator[](unsigned int n) const
Definition: vpColVector.h:645
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector & deg2rad()
Definition: vpColVector.h:385
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:1164
void clear()
Definition: vpColVector.h:292
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:1264
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1148
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:1106
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:830
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:1004
void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols)
Definition: vpMatrix.cpp:363
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:1956
double frobeniusNorm() const
Definition: vpMatrix.cpp:1896
vpMatrix & operator,(double val)
double sumSquare() const
Definition: vpMatrix.cpp:1977
vpMatrix transpose() const
vpMatrix t() const
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:1051
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:953
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.