Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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
43class vpMatrix;
44class vpRowVector;
47class vpPoseVector;
48
166class VISP_EXPORT vpColVector : public vpArray2D<double>
167{
168 friend class vpMatrix;
169
170public:
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);
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); }
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;
315 vpTranslationVector operator+(const vpTranslationVector &t) const;
316 vpColVector &operator+=(vpColVector v);
317
318 vpColVector operator-(const vpColVector &v) const;
319 vpColVector &operator-=(vpColVector v);
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
459VISP_EXPORT
460#endif
461vpColVector operator*(const double &x, const vpColVector &v);
462
463#ifdef VISP_HAVE_NLOHMANN_JSON
464inline 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}
470inline 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
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition vpArray2D.h:445
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
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition vpArray2D.h:529
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
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 > 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
bool operator==(const vpArray2D< Type > &A) const
Definition vpArray2D.h:1173
Implementation of column vector and the associated operations.
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
vpColVector extract(unsigned int r, unsigned int colsize) const
vpColVector(unsigned int n, double val)
Construct a column vector of size n. Each element is set to val.
vpColVector(unsigned int n)
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
vpColVector & rad2deg()
vpColVector(const vpColVector &v)
vp_deprecated void init()
vpColVector()
Basic constructor that creates an empty 0-size column vector.
vpColVector & deg2rad()
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
virtual ~vpColVector()
vp_deprecated void setIdentity(const double &val=1.0)
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
vpColVector(const std::initializer_list< double > &list)
static vpColVector cross(const vpColVector &a, const vpColVector &b)
vp_deprecated void stackMatrices(const vpColVector &r)
void resize(unsigned int i, bool flagNullify=true)
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
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:152
vpMatrix operator*(const vpMatrix &B) const
Implementation of a pose vector and operations on poses.
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Class that consider the case of a translation vector.
vpColVector operator*(const double &x, const vpColVector &v)