Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpTranslationVector.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include <visp3/core/vpTranslationVector.h>
44 
57 vpTranslationVector::vpTranslationVector(const double tx, const double ty, const double tz) : vpArray2D<double>(3, 1)
58 {
59  (*this)[0] = tx;
60  (*this)[1] = ty;
61  (*this)[2] = tz;
62 }
63 
72 
81 {
82  (*this)[0] = p[0];
83  (*this)[1] = p[1];
84  (*this)[2] = p[2];
85 }
86 
98 
114 {
115  if (v.size() != 3) {
117  "Cannot construct a translation vector from a "
118  "%d-dimension column vector",
119  v.size()));
120  }
121 }
122 
135 {
136  M.extract(*this);
137  return *this;
138 }
139 
150 {
151  (*this)[0] = p[0];
152  (*this)[1] = p[1];
153  (*this)[2] = p[2];
154  return *this;
155 }
156 
167 {
168  if (v.size() != 3) {
170  "Cannot build a translation vector from a %d-dimension column vector", v.size()));
171  }
172 
173  (*this)[0] = v[0];
174  (*this)[1] = v[1];
175  (*this)[2] = v[2];
176  return *this;
177 }
178 
187 vpTranslationVector vpTranslationVector::buildFrom(const double tx, const double ty, const double tz)
188 {
189  set(tx, ty, tz);
190  return *this;
191 }
192 
199 void vpTranslationVector::set(const double tx, const double ty, const double tz)
200 {
201  (*this)[0] = tx;
202  (*this)[1] = ty;
203  (*this)[2] = tz;
204 }
205 
225 {
227 
228  for (unsigned int i = 0; i < 3; i++)
229  s[i] = (*this)[i] + tv[i];
230 
231  return s;
232 }
255 {
256  if (v.size() != 3) {
257  throw(vpException(vpException::dimensionError, "Cannot add translation vector to a %d-dimension column vector",
258  v.size()));
259  }
261 
262  for (unsigned int i = 0; i < 3; i++)
263  s[i] = (*this)[i] + v[i];
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  return sub;
294 }
295 
311 {
313  for (unsigned int i = 0; i < dsize; i++) {
314  *(tv.data + i) = -*(data + i);
315  }
316 
317  return tv;
318 }
319 
337 {
339  for (unsigned int i = 0; i < dsize; i++) {
340  *(tv.data + i) = (*(data + i)) * x;
341  }
342 
343  return tv;
344 }
345 
356 {
357  vpMatrix M(rowNum, v.getCols());
358  for (unsigned int i = 0; i < rowNum; i++) {
359  for (unsigned int j = 0; j < v.getCols(); j++) {
360  M[i][j] = (*this)[i] * v[j];
361  }
362  }
363  return M;
364 }
365 
375 {
376  for (unsigned int i = 0; i < rowNum; i++)
377  (*this)[i] *= x;
378  return (*this);
379 }
389 {
390  for (unsigned int i = 0; i < rowNum; i++)
391  (*this)[i] /= x;
392  return (*this);
393 }
394 
412 {
414  for (unsigned int i = 0; i < dsize; i++) {
415  *(tv.data + i) = (*(data + i)) / x;
416  }
417 
418  return tv;
419 }
420 
438 {
439  if (tv.size() != 3) {
441  "Cannot initialize a translation vector from a "
442  "%d-dimension col vector",
443  tv.size()));
444  }
445  unsigned int k = tv.size();
446  if (rowNum != k) {
447  try {
448  resize(k, 1);
449  } catch (...) {
450  throw;
451  }
452  }
453 
454  memcpy(data, tv.data, rowNum * sizeof(double));
455 
456  return *this;
457 }
472 {
473  unsigned int k = tv.rowNum;
474  if (rowNum != k) {
475  try {
476  resize(k, 1);
477  } catch (...) {
478  throw;
479  }
480  }
481 
482  memcpy(data, tv.data, rowNum * sizeof(double));
483 
484  return *this;
485 }
486 
499 {
500  double *d = data;
501 
502  for (int i = 0; i < 3; i++)
503  *(d++) = x;
504 
505  return *this;
506 }
507 
526 {
527  M.resize(3, 3);
528  M[0][0] = 0;
529  M[0][1] = -tv[2];
530  M[0][2] = tv[1];
531  M[1][0] = tv[2];
532  M[1][1] = 0;
533  M[1][2] = -tv[0];
534  M[2][0] = -tv[1];
535  M[2][1] = tv[0];
536  M[2][2] = 0;
537 }
538 
558 {
559  vpMatrix M(3, 3);
560  skew(tv, M);
561  return M;
562 }
563 
583 {
584  vpMatrix M(3, 3);
585  skew(*this, M);
586  return M;
587 }
588 
599 {
601  return (vpTranslationVector)(skew_a * b);
602 }
603 
608 {
609  vpRowVector v(rowNum);
610  memcpy(v.data, data, rowNum * sizeof(double));
611  return v;
612 }
613 
622 {
623  double norm = 0.0;
624  for (unsigned int i = 0; i < dsize; i++) {
625  double x = *(data + i);
626  norm += x * x;
627  }
628 
629  return sqrt(norm);
630 }
631 
639 {
640  double sum_square = 0.0;
641 
642  for (unsigned int i = 0; i < rowNum; i++) {
643  double x = rowPtrs[i][0];
644  sum_square += x * x;
645  }
646 
647  return sum_square;
648 }
649 
658 vpTranslationVector vpTranslationVector::mean(const std::vector<vpHomogeneousMatrix> &vec_M)
659 {
660  vpColVector meanT(3);
661  for (size_t i = 0; i < vec_M.size(); i++) {
662  meanT += (vpColVector) vec_M[i].getTranslationVector();
663  }
664  meanT /= static_cast<double>(vec_M.size());
665 
666  vpTranslationVector t(meanT);
667  return t;
668 }
669 
678 vpTranslationVector vpTranslationVector::mean(const std::vector<vpTranslationVector> &vec_t)
679 {
680  vpColVector meanT(3);
681  for (size_t i = 0; i < vec_t.size(); i++) {
682  meanT += (vpColVector) vec_t[i];
683  }
684  meanT /= static_cast<double>(vec_t.size());
685 
686  vpTranslationVector t(meanT);
687  return t;
688 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
vpTranslationVector & operator*=(double x)
static vpTranslationVector cross(const vpTranslationVector &a, const vpTranslationVector &b)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:72
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpTranslationVector operator-() const
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
unsigned int getCols() const
Definition: vpArray2D.h:146
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
vpTranslationVector operator/(const double x) const
double euclideanNorm() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
void extract(vpRotationMatrix &R) const
vpRowVector t() const
static vpTranslationVector mean(const std::vector< vpHomogeneousMatrix > &vec_M)
vpTranslationVector & operator=(const vpColVector &tv)
vpTranslationVector & operator/=(double x)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
vpMatrix operator*(const vpRowVector &v) const
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:80
void set(const double tx, const double ty, const double tz)
Class that consider the case of a translation vector.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
vpTranslationVector buildFrom(const double tx, const double ty, const double tz)
vpTranslationVector operator+(const vpTranslationVector &tv) const