Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTranslationVector.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Translation vector.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include <visp3/core/vpTranslationVector.h>
44 
56 vpTranslationVector::vpTranslationVector(const double tx, const double ty, const double tz)
57  : vpArray2D<double>(3, 1)
58 {
59  (*this)[0] = tx;
60  (*this)[1] = ty;
61  (*this)[2] = tz;
62 }
63 
72  : vpArray2D<double>(3, 1)
73 {
74  M.extract( *this );
75 }
76 
85  : vpArray2D<double>(3, 1)
86 {
87  (*this)[0] = p[0];
88  (*this)[1] = p[1];
89  (*this)[2] = p[2];
90 }
91 
103  : vpArray2D<double>(tv)
104 {
105 }
106 
121  : vpArray2D<double>(v)
122 {
123  if (v.size() != 3) {
125  "Cannot construct a translation vector from a %d-dimension column vector", v.size()));
126  }
127 }
128 
142 {
143  M.extract( *this );
144  return *this ;
145 }
146 
158 {
159  (*this)[0] = p[0];
160  (*this)[1] = p[1];
161  (*this)[2] = p[2];
162  return *this ;
163 }
164 
176 {
177  if (v.size() != 3) {
179  "Cannot build a translation vector from a %d-dimension column vector", v.size()));
180  }
181 
182  (*this)[0] = v[0];
183  (*this)[1] = v[1];
184  (*this)[2] = v[2];
185  return *this ;
186 }
187 
197 vpTranslationVector::buildFrom(const double tx, const double ty, const double tz)
198 {
199  set(tx, ty, tz);
200  return *this ;
201 }
202 
209 void
210 vpTranslationVector::set(const double tx, const double ty, const double tz)
211 {
212  (*this)[0] = tx ;
213  (*this)[1] = ty ;
214  (*this)[2] = tz ;
215 }
216 
236 {
238 
239  for (unsigned int i=0;i<3;i++) s[i] = (*this)[i]+tv[i] ;
240 
241  return s;
242 }
263 {
264  if (v.size() != 3) {
266  "Cannot add translation vector to a %d-dimension column vector", v.size()));
267  }
269 
270  for (unsigned int i=0;i<3;i++) s[i] = (*this)[i]+v[i] ;
271 
272  return s;
273 }
274 
294 {
295  vpTranslationVector sub ;
296 
297  for (unsigned int i=0;i<3;i++) sub[i] = (*this)[i]-tv[i] ;
298 
299  return sub;
300 }
301 
302 
318 {
320  for (unsigned int i=0;i<dsize;i++)
321  {
322  *(tv.data + i) = -*(data + i) ;
323  }
324 
325  return tv;
326 }
327 
344 {
346  for (unsigned int i=0;i<dsize;i++) {
347  *(tv.data + i) = (*(data + i)) * x ;
348  }
349 
350  return tv;
351 }
352 
363 {
364  vpMatrix M(rowNum, v.getCols());
365  for (unsigned int i=0; i<rowNum; i++) {
366  for (unsigned int j=0; j<v.getCols(); j++) {
367  M[i][j] = (*this)[i] * v[j];
368  }
369  }
370  return M;
371 }
372 
381 {
382  for (unsigned int i=0;i<rowNum;i++)
383  (*this)[i] *= x;
384  return (*this);
385 }
394 {
395  for (unsigned int i=0;i<rowNum;i++)
396  (*this)[i] /= x;
397  return (*this);
398 }
399 
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) {
444  throw(vpException(vpException::dimensionError, "Cannot initialize a translation vector from a %d-dimension col vector", tv.size()));
445  }
446  unsigned int k = tv.size() ;
447  if (rowNum != k){
448  try {
449  resize(k, 1);
450  }
451  catch(...)
452  {
453  throw ;
454  }
455  }
456 
457  memcpy(data, tv.data, rowNum*sizeof(double)) ;
458 
459  return *this;
460 }
475 {
476  unsigned int k = tv.rowNum ;
477  if (rowNum != k){
478  try {
479  resize(k, 1);
480  }
481  catch(...)
482  {
483  throw ;
484  }
485  }
486 
487  memcpy(data, tv.data, rowNum*sizeof(double)) ;
488 
489  return *this;
490 }
491 
504 {
505  double *d = data ;
506 
507  for (int i=0;i<3;i++)
508  *(d++)= x ;
509 
510  return *this;
511 }
512 
530 void
532 {
533  M.resize(3,3) ;
534  M[0][0] = 0 ; M[0][1] = -tv[2] ; M[0][2] = tv[1] ;
535  M[1][0] = tv[2] ; M[1][1] = 0 ; M[1][2] = -tv[0] ;
536  M[2][0] = -tv[1]; M[2][1] = tv[0] ; M[2][2] = 0 ;
537 }
538 
557 vpMatrix
559 {
560  vpMatrix M(3, 3);
561  skew(tv,M);
562  return M;
563 }
564 
583 vpMatrix
585 {
586  vpMatrix M(3, 3);
587  skew(*this,M);
588  return M;
589 }
590 
591 
603  const vpTranslationVector &b)
604 {
605  vpMatrix skew_a = vpTranslationVector::skew(a) ;
606  return (vpTranslationVector)(skew_a * b);
607 }
608 
613 {
614  vpRowVector v(rowNum);
615  memcpy(v.data, data, rowNum*sizeof(double)) ;
616  return v;
617 }
618 
627 {
628  double norm=0.0;
629  for (unsigned int i=0;i<dsize;i++) {
630  double x = *(data +i);
631  norm += x*x;
632  }
633 
634  return sqrt(norm);
635 }
636 
643 {
644  double sum_square=0.0;
645 
646  for (unsigned int i=0;i<rowNum;i++) {
647  double x=rowPtrs[i][0];
648  sum_square += x*x;
649  }
650 
651  return sum_square;
652 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
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:70
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
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
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:93
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