Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpForceTwistMatrix.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  * 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 
214 {
215  vpForceTwistMatrix Fout ;
216 
217  for (unsigned int i=0;i<6;i++) {
218  for (unsigned int j=0;j<6;j++) {
219  double s =0 ;
220  for (unsigned int k=0;k<6;k++)
221  s +=rowPtrs[i][k] * F.rowPtrs[k][j];
222  Fout[i][j] = s ;
223  }
224  }
225  return Fout;
226 }
227 
234 vpMatrix
236 {
237 
238  if (6 != M.getRows()) {
240  "Cannot multiply (6x6) force/torque twist matrix by a (%dx%d) matrix",
241  M.getRows(), M.getCols()));
242 
243  }
244 
245  vpMatrix p(6, M.getCols()) ;
246  for (unsigned int i=0;i<6;i++) {
247  for (unsigned int j=0;j<M.getCols();j++) {
248  double s =0 ;
249  for (unsigned int k=0;k<6;k++)
250  s += rowPtrs[i][k] * M[k][j];
251  p[i][j] = s ;
252  }
253  }
254  return p;
255 }
256 
305 {
306  vpColVector Hout(6);
307 
308  if (6 != H.getRows()) {
310  "Cannot multiply a (6x6) force/torque twist matrix by a %d dimension column vector",
311  H.getRows()));
312  }
313 
314  Hout = 0.0;
315 
316  for (unsigned int i=0;i<6;i++) {
317  for (unsigned int j=0;j<6;j++) {
318  Hout[i]+=rowPtrs[i][j] * H[j];
319  }
320  }
321 
322  return Hout ;
323 }
324 
325 
338  const vpRotationMatrix &R)
339 {
340  unsigned int i, j;
341  vpMatrix skewaR = t.skew(t)*R ;
342 
343  for (i=0 ; i < 3 ; i++) {
344  for (j=0 ; j < 3 ; j++) {
345  (*this)[i][j] = R[i][j] ;
346  (*this)[i+3][j+3] = R[i][j] ;
347  (*this)[i+3][j] = skewaR[i][j] ;
348  }
349  }
350  return (*this) ;
351 }
352 
365  const vpThetaUVector &thetau)
366 {
367  vpRotationMatrix R ;
368  R.buildFrom(thetau) ;
369  buildFrom(tv,R) ;
370  return (*this) ;
371 }
372 
373 
386 {
388  vpRotationMatrix R ;
389  M.extract(R) ;
390  M.extract(tv) ;
391 
392  buildFrom(tv, R) ;
393  return (*this) ;
394 }
395 
415 int
416 vpForceTwistMatrix::print(std::ostream& s, unsigned int length, char const* intro) const
417 {
418  typedef std::string::size_type size_type;
419 
420  unsigned int m = getRows();
421  unsigned int n = getCols();
422 
423  std::vector<std::string> values(m*n);
424  std::ostringstream oss;
425  std::ostringstream ossFixed;
426  std::ios_base::fmtflags original_flags = oss.flags();
427 
428  // ossFixed <<std::fixed;
429  ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
430 
431  size_type maxBefore=0; // the length of the integral part
432  size_type maxAfter=0; // number of decimals plus
433  // one place for the decimal point
434  for (unsigned int i=0;i<m;++i) {
435  for (unsigned int j=0;j<n;++j){
436  oss.str("");
437  oss << (*this)[i][j];
438  if (oss.str().find("e")!=std::string::npos){
439  ossFixed.str("");
440  ossFixed << (*this)[i][j];
441  oss.str(ossFixed.str());
442  }
443 
444  values[i*n+j]=oss.str();
445  size_type thislen=values[i*n+j].size();
446  size_type p=values[i*n+j].find('.');
447 
448  if (p==std::string::npos){
449  maxBefore=vpMath::maximum(maxBefore, thislen);
450  // maxAfter remains the same
451  } else{
452  maxBefore=vpMath::maximum(maxBefore, p);
453  maxAfter=vpMath::maximum(maxAfter, thislen-p-1);
454  }
455  }
456  }
457 
458  size_type totalLength=length;
459  // increase totalLength according to maxBefore
460  totalLength=vpMath::maximum(totalLength,maxBefore);
461  // decrease maxAfter according to totalLength
462  maxAfter=std::min(maxAfter, totalLength-maxBefore);
463  if (maxAfter==1) maxAfter=0;
464 
465  // the following line is useful for debugging
466  //std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
467 
468  if (intro) s <<intro;
469  s <<"["<<m<<","<<n<<"]=\n";
470 
471  for (unsigned int i=0;i<m;i++) {
472  s <<" ";
473  for (unsigned int j=0;j<n;j++){
474  size_type p=values[i*n+j].find('.');
475  s.setf(std::ios::right, std::ios::adjustfield);
476  s.width((std::streamsize)maxBefore);
477  s <<values[i*n+j].substr(0,p).c_str();
478 
479  if (maxAfter>0){
480  s.setf(std::ios::left, std::ios::adjustfield);
481  if (p!=std::string::npos){
482  s.width((std::streamsize)maxAfter);
483  s <<values[i*n+j].substr(p,maxAfter).c_str();
484  } else{
485  assert(maxAfter>1);
486  s.width((std::streamsize)maxAfter);
487  s <<".0";
488  }
489  }
490 
491  s <<' ';
492  }
493  s <<std::endl;
494  }
495 
496  s.flags(original_flags); // restore s to standard state
497 
498  return (int)(maxBefore+maxAfter);
499 }
500 
501 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
502 
509 void
511 {
512  eye() ;
513 }
514 
515 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
vpForceTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
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:140
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