Visual Servoing Platform  version 3.0.0
vpForceTwistMatrix.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  * Twist transformation matrix that allows to transform forces from one
32  * frame to an other.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <sstream>
40 #include <assert.h>
41 
42 #include <visp3/core/vpForceTwistMatrix.h>
43 #include <visp3/core/vpException.h>
44 #include <visp3/core/vpDebug.h>
45 
46 
63 {
64  for (int i=0; i<6; i++) {
65  for (int j=0; j<6; j++) {
66  rowPtrs[i][j] = M.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  }
88 }
89 
94  : vpArray2D<double>(6,6)
95 {
96  eye() ;
97 }
98 
107  : vpArray2D<double>(6,6)
108 {
109  *this = F ;
110 }
111 
137  : vpArray2D<double>(6,6)
138 {
139  buildFrom(M);
140 }
141 
153  const vpThetaUVector &thetau)
154  : vpArray2D<double>(6,6)
155 {
156  buildFrom(t, thetau) ;
157 }
158 
170  const vpRotationMatrix &R)
171  : vpArray2D<double>(6,6)
172 {
173  buildFrom(t, R) ;
174 }
175 
185 vpForceTwistMatrix::vpForceTwistMatrix(const double tx, const double ty, const double tz,
186  const double tux, const double tuy, const double tuz)
187  : vpArray2D<double>(6,6)
188 {
189  vpTranslationVector T(tx,ty,tz) ;
190  vpThetaUVector tu(tux,tuy,tuz) ;
191  buildFrom(T,tu) ;
192 }
193 
198 void
200 {
201  eye() ;
202 }
203 
204 
225 {
226  vpForceTwistMatrix Fout ;
227 
228  for (unsigned int i=0;i<6;i++) {
229  for (unsigned int j=0;j<6;j++) {
230  double s =0 ;
231  for (unsigned int k=0;k<6;k++)
232  s +=rowPtrs[i][k] * F.rowPtrs[k][j];
233  Fout[i][j] = s ;
234  }
235  }
236  return Fout;
237 }
238 
245 vpMatrix
247 {
248 
249  if (6 != M.getRows()) {
251  "Cannot multiply (6x6) force/torque twist matrix by a (%dx%d) matrix",
252  M.getRows(), M.getCols()));
253 
254  }
255 
256  vpMatrix p(6, M.getCols()) ;
257  for (unsigned int i=0;i<6;i++) {
258  for (unsigned int j=0;j<M.getCols();j++) {
259  double s =0 ;
260  for (unsigned int k=0;k<6;k++)
261  s += rowPtrs[i][k] * M[k][j];
262  p[i][j] = s ;
263  }
264  }
265  return p;
266 }
267 
319 {
320  vpColVector Hout(6);
321 
322  if (6 != H.getRows()) {
324  "Cannot multiply a (6x6) force/torque twist matrix by a %d dimension column vector",
325  H.getRows()));
326  }
327 
328  Hout = 0.0;
329 
330  for (unsigned int i=0;i<6;i++) {
331  for (unsigned int j=0;j<6;j++) {
332  Hout[i]+=rowPtrs[i][j] * H[j];
333  }
334  }
335 
336  return Hout ;
337 }
338 
339 
352  const vpRotationMatrix &R)
353 {
354  unsigned int i, j;
355  vpMatrix skewaR = t.skew(t)*R ;
356 
357  for (i=0 ; i < 3 ; i++) {
358  for (j=0 ; j < 3 ; j++) {
359  (*this)[i][j] = R[i][j] ;
360  (*this)[i+3][j+3] = R[i][j] ;
361  (*this)[i+3][j] = skewaR[i][j] ;
362  }
363  }
364  return (*this) ;
365 }
366 
379  const vpThetaUVector &thetau)
380 {
381  vpRotationMatrix R ;
382  R.buildFrom(thetau) ;
383  buildFrom(tv,R) ;
384  return (*this) ;
385 }
386 
387 
400 {
402  vpRotationMatrix R ;
403  M.extract(R) ;
404  M.extract(tv) ;
405 
406  buildFrom(tv, R) ;
407  return (*this) ;
408 }
409 
429 int
430 vpForceTwistMatrix::print(std::ostream& s, unsigned int length, char const* intro) const
431 {
432  typedef std::string::size_type size_type;
433 
434  unsigned int m = getRows();
435  unsigned int n = getCols();
436 
437  std::vector<std::string> values(m*n);
438  std::ostringstream oss;
439  std::ostringstream ossFixed;
440  std::ios_base::fmtflags original_flags = oss.flags();
441 
442  // ossFixed <<std::fixed;
443  ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
444 
445  size_type maxBefore=0; // the length of the integral part
446  size_type maxAfter=0; // number of decimals plus
447  // one place for the decimal point
448  for (unsigned int i=0;i<m;++i) {
449  for (unsigned int j=0;j<n;++j){
450  oss.str("");
451  oss << (*this)[i][j];
452  if (oss.str().find("e")!=std::string::npos){
453  ossFixed.str("");
454  ossFixed << (*this)[i][j];
455  oss.str(ossFixed.str());
456  }
457 
458  values[i*n+j]=oss.str();
459  size_type thislen=values[i*n+j].size();
460  size_type p=values[i*n+j].find('.');
461 
462  if (p==std::string::npos){
463  maxBefore=vpMath::maximum(maxBefore, thislen);
464  // maxAfter remains the same
465  } else{
466  maxBefore=vpMath::maximum(maxBefore, p);
467  maxAfter=vpMath::maximum(maxAfter, thislen-p-1);
468  }
469  }
470  }
471 
472  size_type totalLength=length;
473  // increase totalLength according to maxBefore
474  totalLength=vpMath::maximum(totalLength,maxBefore);
475  // decrease maxAfter according to totalLength
476  maxAfter=std::min(maxAfter, totalLength-maxBefore);
477  if (maxAfter==1) maxAfter=0;
478 
479  // the following line is useful for debugging
480  //std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
481 
482  if (intro) s <<intro;
483  s <<"["<<m<<","<<n<<"]=\n";
484 
485  for (unsigned int i=0;i<m;i++) {
486  s <<" ";
487  for (unsigned int j=0;j<n;j++){
488  size_type p=values[i*n+j].find('.');
489  s.setf(std::ios::right, std::ios::adjustfield);
490  s.width((std::streamsize)maxBefore);
491  s <<values[i*n+j].substr(0,p).c_str();
492 
493  if (maxAfter>0){
494  s.setf(std::ios::left, std::ios::adjustfield);
495  if (p!=std::string::npos){
496  s.width((std::streamsize)maxAfter);
497  s <<values[i*n+j].substr(p,maxAfter).c_str();
498  } else{
499  assert(maxAfter>1);
500  s.width((std::streamsize)maxAfter);
501  s <<".0";
502  }
503  }
504 
505  s <<' ';
506  }
507  s <<std::endl;
508  }
509 
510  s.flags(original_flags); // restore s to standard state
511 
512  return (int)(maxBefore+maxAfter);
513 }
vpForceTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpForceTwistMatrix & operator=(const vpForceTwistMatrix &H)
error that can be emited by ViSP classes.
Definition: vpException.h:73
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
vp_deprecated void setIdentity()
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.
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
vpForceTwistMatrix operator*(const vpForceTwistMatrix &F) const
void extract(vpRotationMatrix &R) const
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
Implementation of a force/torque twist matrix and operations on such kind of matrices.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
int print(std::ostream &s, unsigned int length, char const *intro=0) const
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