Visual Servoing Platform  version 3.0.0
vpVelocityTwistMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Velocity twist transformation matrix.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <sstream>
39 #include <assert.h>
40 
41 #include <visp3/core/vpVelocityTwistMatrix.h>
42 #include <visp3/core/vpException.h>
43 
44 
61 {
62  for (int i=0; i<6; i++)
63  {
64  for (int j=0; j<6; j++)
65  {
66  rowPtrs[i][j] = V.rowPtrs[i][j];
67  }
68  }
69 
70  return *this;
71 }
72 
73 
77 void
79 {
80  for (unsigned int i=0 ; i < 6 ; i++)
81  for (unsigned int j=0 ; j < 6; j++)
82  if (i==j)
83  (*this)[i][j] = 1.0 ;
84  else
85  (*this)[i][j] = 0.0;
86 }
87 
92  : vpArray2D<double>(6, 6)
93 {
94  eye() ;
95 }
96 
103  : vpArray2D<double>(6, 6)
104 {
105  *this = V;
106 }
107 
119  : vpArray2D<double>(6, 6)
120 {
121  buildFrom(M);
122 }
123 
135  const vpThetaUVector &thetau)
136  : vpArray2D<double>(6, 6)
137 {
138  buildFrom(t, thetau) ;
139 }
140 
152  const vpRotationMatrix &R)
153  : vpArray2D<double>(6, 6)
154 {
155  buildFrom(t,R) ;
156 }
157 
169  const double ty,
170  const double tz,
171  const double tux,
172  const double tuy,
173  const double tuz)
174  : vpArray2D<double>(6, 6)
175 {
176  vpTranslationVector T(tx,ty,tz) ;
177  vpThetaUVector tu(tux,tuy,tuz) ;
178  buildFrom(T,tu) ;
179 }
180 
186 void
188 {
189  eye() ;
190 }
191 
192 
201 {
203 
204  for (unsigned int i=0;i<6;i++)
205  for (unsigned int j=0;j<6;j++)
206  {
207  double s =0 ;
208  for (int k=0;k<6;k++)
209  s +=rowPtrs[i][k] * V.rowPtrs[k][j];
210  p[i][j] = s ;
211  }
212  return p;
213 }
214 
251 vpMatrix
253 {
254  if (6 != M.getRows()) {
256  "Cannot multiply a (6x6) velocity twist matrix by a (%dx%d) matrix",
257  M.getRows(), M.getCols()));
258  }
259 
260  vpMatrix p(6, M.getCols()) ;
261  for (unsigned int i=0;i<6;i++)
262  for (unsigned int j=0;j<M.getCols();j++)
263  {
264  double s =0 ;
265  for (unsigned int k=0;k<6;k++)
266  s += rowPtrs[i][k] * M[k][j];
267  p[i][j] = s ;
268  }
269  return p;
270 }
271 
284 {
285  vpColVector c(6);
286 
287  if (6 != v.getRows())
288  {
290  "Cannot multiply a (6x6) velocity twist matrix by a (%d) column vector",
291  v.getRows()));
292  }
293 
294  c = 0.0;
295 
296  for (unsigned int i=0;i<6;i++) {
297  for (unsigned int j=0;j<6;j++) {
298  {
299  c[i]+=rowPtrs[i][j] * v[j];
300  }
301  }
302  }
303 
304  return c ;
305 }
306 
307 
320  const vpRotationMatrix &R)
321 {
322  unsigned int i, j;
323  vpMatrix skewaR = t.skew(t)*R ;
324 
325  for (i=0 ; i < 3 ; i++)
326  for (j=0 ; j < 3 ; j++)
327  {
328  (*this)[i][j] = R[i][j] ;
329  (*this)[i+3][j+3] = R[i][j] ;
330  (*this)[i][j+3] = skewaR[i][j] ;
331 
332  }
333  return (*this) ;
334 }
335 
348  const vpThetaUVector &thetau)
349 {
350  vpRotationMatrix R ;
351  R.buildFrom(thetau) ;
352  buildFrom(t,R) ;
353  return (*this) ;
354 }
355 
356 
369 {
371  vpRotationMatrix R ;
372  M.extract(R) ;
373  M.extract(tv) ;
374 
375  buildFrom(tv, R) ;
376  return (*this) ;
377 }
378 
379 
383 {
387  vpTranslationVector RtT ; RtT = -(R.t()*T) ;
388 
389  Wi.buildFrom(RtT,R.t());
390 
391  return Wi ;
392 }
393 
394 
396 void
398 {
399  V = inverse();
400 }
401 
403 void
405 {
406  for (unsigned int i=0 ; i < 3 ; i++)
407  for (unsigned int j=0 ; j < 3; j++)
408  R[i][j] = (*this)[i][j];
409 }
410 
412 void
414 {
416  vpMatrix skTR(3,3);
417  for (unsigned int i=0 ; i < 3 ; i++)
418  for (unsigned int j=0 ; j < 3; j++)
419  skTR[i][j] = (*this)[i][j+3];
420 
421  vpMatrix skT = skTR*R.t();
422  tv[0] = skT[2][1];
423  tv[1] = skT[0][2];
424  tv[2] = skT[1][0];
425 }
426 
446 int
447 vpVelocityTwistMatrix::print(std::ostream& s, unsigned int length, char const* intro) const
448 {
449  typedef std::string::size_type size_type;
450 
451  unsigned int m = getRows();
452  unsigned int n = getCols();
453 
454  std::vector<std::string> values(m*n);
455  std::ostringstream oss;
456  std::ostringstream ossFixed;
457  std::ios_base::fmtflags original_flags = oss.flags();
458 
459  // ossFixed <<std::fixed;
460  ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
461 
462  size_type maxBefore=0; // the length of the integral part
463  size_type maxAfter=0; // number of decimals plus
464  // one place for the decimal point
465  for (unsigned int i=0;i<m;++i) {
466  for (unsigned int j=0;j<n;++j){
467  oss.str("");
468  oss << (*this)[i][j];
469  if (oss.str().find("e")!=std::string::npos){
470  ossFixed.str("");
471  ossFixed << (*this)[i][j];
472  oss.str(ossFixed.str());
473  }
474 
475  values[i*n+j]=oss.str();
476  size_type thislen=values[i*n+j].size();
477  size_type p=values[i*n+j].find('.');
478 
479  if (p==std::string::npos){
480  maxBefore=vpMath::maximum(maxBefore, thislen);
481  // maxAfter remains the same
482  } else{
483  maxBefore=vpMath::maximum(maxBefore, p);
484  maxAfter=vpMath::maximum(maxAfter, thislen-p-1);
485  }
486  }
487  }
488 
489  size_type totalLength=length;
490  // increase totalLength according to maxBefore
491  totalLength=vpMath::maximum(totalLength,maxBefore);
492  // decrease maxAfter according to totalLength
493  maxAfter=std::min(maxAfter, totalLength-maxBefore);
494  if (maxAfter==1) maxAfter=0;
495 
496  // the following line is useful for debugging
497  //std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
498 
499  if (intro) s <<intro;
500  s <<"["<<m<<","<<n<<"]=\n";
501 
502  for (unsigned int i=0;i<m;i++) {
503  s <<" ";
504  for (unsigned int j=0;j<n;j++){
505  size_type p=values[i*n+j].find('.');
506  s.setf(std::ios::right, std::ios::adjustfield);
507  s.width((std::streamsize)maxBefore);
508  s <<values[i*n+j].substr(0,p).c_str();
509 
510  if (maxAfter>0){
511  s.setf(std::ios::left, std::ios::adjustfield);
512  if (p!=std::string::npos){
513  s.width((std::streamsize)maxAfter);
514  s <<values[i*n+j].substr(p,maxAfter).c_str();
515  } else{
516  assert(maxAfter>1);
517  s.width((std::streamsize)maxAfter);
518  s <<".0";
519  }
520  }
521 
522  s <<' ';
523  }
524  s <<std::endl;
525  }
526 
527  s.flags(original_flags); // restore s to standard state
528 
529  return (int)(maxBefore+maxAfter);
530 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
Implementation of an homogeneous matrix and operations on such kind of matrices.
int print(std::ostream &s, unsigned int length, char const *intro=0) const
error that can be emited by ViSP classes.
Definition: vpException.h:73
vpRotationMatrix t() const
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:141
Implementation of a rotation matrix and operations on such kind of matrices.
vpVelocityTwistMatrix operator*(const vpVelocityTwistMatrix &V) const
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
void extract(vpRotationMatrix &R) const
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
vp_deprecated void setIdentity()
vpVelocityTwistMatrix inverse() const
Invert the velocity twist matrix.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpVelocityTwistMatrix & operator=(const vpVelocityTwistMatrix &V)
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
void extract(vpRotationMatrix &R) const
Extract the rotation matrix from the velocity twist matrix.