Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpForceTwistMatrix.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 *****************************************************************************/
35 
44 #include <assert.h>
45 #include <sstream>
46 
47 #include <visp3/core/vpException.h>
48 #include <visp3/core/vpForceTwistMatrix.h>
49 
50 BEGIN_VISP_NAMESPACE
51 
58 {
59  for (int i = 0; i < 6; ++i) {
60  for (int j = 0; j < 6; ++j) {
61  rowPtrs[i][j] = M.rowPtrs[i][j];
62  }
63  }
64 
65  return *this;
66 }
67 
72 {
73  const unsigned int nparam = 6;
74  for (unsigned int i = 0; i < nparam; ++i) {
75  for (unsigned int j = 0; j < nparam; ++j) {
76  if (i == j) {
77  (*this)[i][j] = 1.0;
78  }
79  else {
80  (*this)[i][j] = 0.0;
81  }
82  }
83  }
84 }
85 
90 
99 
130 {
131  if (full) {
132  buildFrom(M);
133  }
134  else {
136  }
137 }
138 
159  : vpArray2D<double>(6, 6)
160 {
161  buildFrom(t, thetau);
162 }
163 
182 
203  : vpArray2D<double>(6, 6)
204 {
205  buildFrom(t, R);
206 }
207 
226 
247 vpForceTwistMatrix::vpForceTwistMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz)
248  : vpArray2D<double>(6, 6)
249 {
250  vpTranslationVector T(tx, ty, tz);
251  vpThetaUVector tu(tux, tuy, tuz);
252  buildFrom(T, tu);
253 }
254 
278 {
279  vpForceTwistMatrix Fout;
280  const unsigned int nparam = 6;
281  for (unsigned int i = 0; i < nparam; ++i) {
282  for (unsigned int j = 0; j < nparam; ++j) {
283  double s = 0;
284  for (unsigned int k = 0; k < nparam; ++k) {
285  s += rowPtrs[i][k] * F.rowPtrs[k][j];
286  }
287  Fout[i][j] = s;
288  }
289  }
290  return Fout;
291 }
292 
301 {
302  const unsigned int nparam = 6;
303  if (nparam != M.getRows()) {
305  "Cannot multiply (6x6) force/torque twist matrix by a (%dx%d) matrix", M.getRows(), M.getCols()));
306  }
307 
308  unsigned int m_col = M.getCols();
309  vpMatrix p(nparam, M.getCols());
310  for (unsigned int i = 0; i < nparam; ++i) {
311  for (unsigned int j = 0; j < m_col; ++j) {
312  double s = 0;
313  for (unsigned int k = 0; k < nparam; ++k) {
314  s += rowPtrs[i][k] * M[k][j];
315  }
316  p[i][j] = s;
317  }
318  }
319  return p;
320 }
321 
370 {
371  const unsigned int nparam = 6;
372  vpColVector Hout(nparam);
373 
374  if (6 != H.getRows()) {
376  "Cannot multiply a (6x6) force/torque twist matrix by "
377  "a %d dimension column vector",
378  H.getRows()));
379  }
380 
381  Hout = 0.0;
382 
383  for (unsigned int i = 0; i < nparam; ++i) {
384  for (unsigned int j = 0; j < nparam; ++j) {
385  Hout[i] += rowPtrs[i][j] * H[j];
386  }
387  }
388 
389  return Hout;
390 }
391 
411 {
412  vpMatrix skewaR = t.skew(t) * R;
413 
414  const unsigned int val_3 = 3;
415  for (unsigned int i = 0; i < val_3; ++i) {
416  for (unsigned int j = 0; j < val_3; ++j) {
417  (*this)[i][j] = R[i][j];
418  (*this)[i + 3][j + 3] = R[i][j];
419  (*this)[i + 3][j] = skewaR[i][j];
420  }
421  }
422  return *this;
423 }
424 
442 {
443  const unsigned int val_3 = 3;
444  for (unsigned int i = 0; i < val_3; ++i) {
445  for (unsigned int j = 0; j < val_3; ++j) {
446  (*this)[i][j] = R[i][j];
447  (*this)[i + 3][j + 3] = R[i][j];
448  (*this)[i + 3][j] = 0;
449  }
450  }
451  return *this;
452 }
453 
474 {
475  buildFrom(tv, vpRotationMatrix(thetau));
476  return *this;
477 }
478 
497 {
498  buildFrom(vpRotationMatrix(thetau));
499  return *this;
500 }
501 
530 {
531  if (full) {
533  }
534  else {
536  }
537 
538  return *this;
539 }
540 
560 int vpForceTwistMatrix::print(std::ostream &s, unsigned int length, char const *intro) const
561 {
562  typedef std::string::size_type size_type;
563 
564  unsigned int m = getRows();
565  unsigned int n = getCols();
566 
567  std::vector<std::string> values(m * n);
568  std::ostringstream oss;
569  std::ostringstream ossFixed;
570  std::ios_base::fmtflags original_flags = oss.flags();
571 
572  // --comment: ossFixed less less std fixed
573  ossFixed.setf(std::ios::fixed, std::ios::floatfield);
574 
575  size_type maxBefore = 0; // the length of the integral part
576  size_type maxAfter = 0; // number of decimals plus
577  // one place for the decimal point
578  for (unsigned int i = 0; i < m; ++i) {
579  for (unsigned int j = 0; j < n; ++j) {
580  oss.str("");
581  oss << (*this)[i][j];
582  if (oss.str().find("e") != std::string::npos) {
583  ossFixed.str("");
584  ossFixed << (*this)[i][j];
585  oss.str(ossFixed.str());
586  }
587 
588  values[(i * n) + j] = oss.str();
589  size_type thislen = values[(i * n) + j].size();
590  size_type p = values[(i * n) + j].find('.');
591 
592  if (p == std::string::npos) {
593  maxBefore = vpMath::maximum(maxBefore, thislen);
594  // maxAfter remains the same
595  }
596  else {
597  maxBefore = vpMath::maximum(maxBefore, p);
598  maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
599  }
600  }
601  }
602 
603  size_type totalLength = length;
604  // increase totalLength according to maxBefore
605  totalLength = vpMath::maximum(totalLength, maxBefore);
606  // decrease maxAfter according to totalLength
607  maxAfter = std::min<size_type>(maxAfter, totalLength - maxBefore);
608  if (maxAfter == 1) {
609  maxAfter = 0;
610  }
611 
612  // the following line is useful for debugging
613  // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
614 
615  if (intro) {
616  s << intro;
617  }
618  s << "[" << m << "," << n << "]=\n";
619 
620  for (unsigned int i = 0; i < m; ++i) {
621  s << " ";
622  for (unsigned int j = 0; j < n; ++j) {
623  size_type p = values[(i * n) + j].find('.');
624  s.setf(std::ios::right, std::ios::adjustfield);
625  s.width(static_cast<std::streamsize>(maxBefore));
626  s << values[(i * n) + j].substr(0, p).c_str();
627 
628  if (maxAfter > 0) {
629  s.setf(std::ios::left, std::ios::adjustfield);
630  if (p != std::string::npos) {
631  s.width(static_cast<std::streamsize>(maxAfter));
632  s << values[(i * n) + j].substr(p, maxAfter).c_str();
633  }
634  else {
635  assert(maxAfter > 1);
636  s.width(static_cast<std::streamsize>(maxAfter));
637  s << ".0";
638  }
639  }
640 
641  s << ' ';
642  }
643  s << std::endl;
644  }
645 
646  s.flags(original_flags); // restore s to standard state
647 
648  return static_cast<int>(maxBefore + maxAfter);
649 }
650 
651 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
652 
659 void vpForceTwistMatrix::setIdentity() { eye(); }
660 
661 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
662 #ifdef ENABLE_VISP_NAMESPACE
663  }
664 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
unsigned int getCols() const
Definition: vpArray2D.h:337
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:1105
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
vpArray2D< double > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1166
unsigned int getRows() const
Definition: vpArray2D.h:347
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
vpForceTwistMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpForceTwistMatrix operator*(const vpForceTwistMatrix &F) const
vpForceTwistMatrix & operator=(const vpForceTwistMatrix &H)
int print(std::ostream &s, unsigned int length, char const *intro=nullptr) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:254
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.