Visual Servoing Platform  version 3.6.1 under development (2024-04-23)
vpTranslationVector.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Translation vector.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include <visp3/core/vpTranslationVector.h>
39 
52 vpTranslationVector::vpTranslationVector(double tx, double ty, double tz) : vpArray2D<double>(3, 1), m_index(0)
53 {
54  (*this)[0] = tx;
55  (*this)[1] = ty;
56  (*this)[2] = tz;
57 }
58 
67 {
68  M.extract(*this);
69 }
70 
79 {
80  (*this)[0] = p[0];
81  (*this)[1] = p[1];
82  (*this)[2] = p[2];
83 }
84 
96 
112 {
113  if (v.size() != 3) {
115  "Cannot construct a translation vector from a "
116  "%d-dimension column vector",
117  v.size()));
118  }
119 }
120 
133 {
134  M.extract(*this);
135  return *this;
136 }
137 
148 {
149  (*this)[0] = p[0];
150  (*this)[1] = p[1];
151  (*this)[2] = p[2];
152  return *this;
153 }
154 
165 {
166  if (v.size() != 3) {
168  "Cannot build a translation vector from a %d-dimension column vector", v.size()));
169  }
170 
171  (*this)[0] = v[0];
172  (*this)[1] = v[1];
173  (*this)[2] = v[2];
174  return *this;
175 }
176 
185 vpTranslationVector vpTranslationVector::buildFrom(double tx, double ty, double tz)
186 {
187  set(tx, ty, tz);
188  return *this;
189 }
190 
197 void vpTranslationVector::set(double tx, double ty, double tz)
198 {
199  (*this)[0] = tx;
200  (*this)[1] = ty;
201  (*this)[2] = tz;
202 }
203 
223 {
225 
226  for (unsigned int i = 0; i < 3; ++i) {
227  s[i] = (*this)[i] + tv[i];
228  }
229 
230  return s;
231 }
254 {
255  if (v.size() != 3) {
256  throw(vpException(vpException::dimensionError, "Cannot add translation vector to a %d-dimension column vector",
257  v.size()));
258  }
260 
261  for (unsigned int i = 0; i < 3; ++i) {
262  s[i] = (*this)[i] + v[i];
263  }
264 
265  return s;
266 }
267 
287 {
289 
290  for (unsigned int i = 0; i < 3; ++i) {
291  sub[i] = (*this)[i] - tv[i];
292  }
293 
294  return sub;
295 }
296 
312 {
314  for (unsigned int i = 0; i < dsize; ++i) {
315  *(tv.data + i) = -*(data + i);
316  }
317 
318  return tv;
319 }
320 
338 {
340  for (unsigned int i = 0; i < dsize; ++i) {
341  *(tv.data + i) = (*(data + i)) * x;
342  }
343 
344  return tv;
345 }
346 
357 {
358  vpMatrix M(rowNum, v.getCols());
359  unsigned int v_col = v.getCols();
360  for (unsigned int i = 0; i < rowNum; ++i) {
361  for (unsigned int j = 0; j < v_col; ++j) {
362  M[i][j] = (*this)[i] * v[j];
363  }
364  }
365  return M;
366 }
367 
377 {
378  for (unsigned int i = 0; i < rowNum; ++i) {
379  (*this)[i] *= x;
380  }
381  return *this;
382 }
392 {
393  for (unsigned int i = 0; i < rowNum; ++i) {
394  (*this)[i] /= x;
395  }
396  return *this;
397 }
398 
416 {
418  for (unsigned int i = 0; i < dsize; ++i) {
419  *(tv.data + i) = (*(data + i)) / x;
420  }
421 
422  return tv;
423 }
424 
442 {
443  if (tv.size() != 3) {
445  "Cannot initialize a translation vector from a "
446  "%d-dimension col vector",
447  tv.size()));
448  }
449  unsigned int k = tv.size();
450  if (rowNum != k) {
451  try {
452  resize(k, 1);
453  }
454  catch (...) {
455  throw;
456  }
457  }
458 
459  memcpy(data, tv.data, rowNum * sizeof(double));
460 
461  return *this;
462 }
477 {
478  unsigned int k = tv.rowNum;
479  if (rowNum != k) {
480  try {
481  resize(k, 1);
482  }
483  catch (...) {
484  throw;
485  }
486  }
487 
488  memcpy(data, tv.data, rowNum * sizeof(double));
489 
490  return *this;
491 }
492 
505 {
506  double *d = data;
507 
508  for (int i = 0; i < 3; ++i) {
509  *(d++) = x;
510  }
511 
512  return *this;
513 }
514 
515 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
533 vpTranslationVector &vpTranslationVector::operator=(const std::initializer_list<double> &list)
534 {
535  if (list.size() > size()) {
536  throw(vpException(
538  "Cannot set translation vector out of bounds. It has only %d values while you try to initialize with %d values",
539  size(), list.size()));
540  }
541  std::copy(list.begin(), list.end(), data);
542  return *this;
543 }
544 #endif
545 
570 {
571  m_index = 0;
572  data[m_index] = val;
573  return *this;
574 }
575 
600 {
601  m_index++;
602  if (m_index >= size()) {
603  throw(vpException(
605  "Cannot set translation vector out of bounds. It has only %d values while you try to initialize with %d values",
606  size(), m_index + 1));
607  }
608  data[m_index] = val;
609  return *this;
610 }
611 
630 {
631  M.resize(3, 3);
632  M[0][0] = 0;
633  M[0][1] = -tv[2];
634  M[0][2] = tv[1];
635  M[1][0] = tv[2];
636  M[1][1] = 0;
637  M[1][2] = -tv[0];
638  M[2][0] = -tv[1];
639  M[2][1] = tv[0];
640  M[2][2] = 0;
641 }
642 
662 {
663  vpMatrix M(3, 3);
664  skew(tv, M);
665  return M;
666 }
667 
687 {
688  vpMatrix M(3, 3);
689  skew(*this, M);
690  return M;
691 }
692 
703 {
705  return static_cast<vpTranslationVector>(skew_a * b);
706 }
707 
712 {
713  vpRowVector v(rowNum);
714  memcpy(v.data, data, rowNum * sizeof(double));
715  return v;
716 }
717 
718 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
719 
731 
732 #endif
733 
740 {
741  double norm = sumSquare();
742 
743  return sqrt(norm);
744 }
745 
753 {
754  double sum_square = 0.0;
755 
756  for (unsigned int i = 0; i < rowNum; ++i) {
757  double x = rowPtrs[i][0];
758  sum_square += x * x;
759  }
760 
761  return sum_square;
762 }
763 
772 vpTranslationVector vpTranslationVector::mean(const std::vector<vpHomogeneousMatrix> &vec_M)
773 {
774  vpColVector meanT(3);
775  size_t vec_m_size = vec_M.size();
776  for (size_t i = 0; i < vec_m_size; ++i) {
777  meanT += static_cast<vpColVector>(vec_M[i].getTranslationVector());
778  }
779  meanT /= static_cast<double>(vec_M.size());
780 
781  vpTranslationVector t(meanT);
782  return t;
783 }
784 
793 vpTranslationVector vpTranslationVector::mean(const std::vector<vpTranslationVector> &vec_t)
794 {
795  vpColVector meanT(3);
796  size_t l_vec_t_size = vec_t.size();
797  for (size_t i = 0; i < l_vec_t_size; ++i) {
798  meanT += static_cast<vpColVector>(vec_t[i]);
799  }
800  meanT /= static_cast<double>(vec_t.size());
801 
802  vpTranslationVector t(meanT);
803  return t;
804 }
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
unsigned int getCols() const
Definition: vpArray2D.h:327
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:133
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:352
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:129
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:135
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
Class that consider the case of a translation vector.
void set(double tx, double ty, double tz)
vpTranslationVector & operator=(const vpColVector &tv)
static vpTranslationVector mean(const std::vector< vpHomogeneousMatrix > &vec_M)
vp_deprecated double euclideanNorm() const
vpTranslationVector operator/(double x) const
vpTranslationVector & operator/=(double x)
vpTranslationVector & operator*=(double x)
vpMatrix operator*(const vpRowVector &v) const
vpTranslationVector operator-() const
vpTranslationVector operator+(const vpTranslationVector &tv) const
vpTranslationVector & operator<<(double val)
vpTranslationVector & operator,(double val)
static vpTranslationVector cross(const vpTranslationVector &a, const vpTranslationVector &b)
vpTranslationVector buildFrom(double tx, double ty, double tz)
vpRowVector t() const
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)