Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
vpVelocityTwistMatrix.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Velocity twist transformation matrix.
33  *
34 *****************************************************************************/
35 
36 #include <assert.h>
37 #include <sstream>
38 
39 #include <visp3/core/vpException.h>
40 #include <visp3/core/vpVelocityTwistMatrix.h>
41 
56 {
57  for (int i = 0; i < 6; ++i) {
58  for (int j = 0; j < 6; ++j) {
59  rowPtrs[i][j] = V.rowPtrs[i][j];
60  }
61  }
62 
63  return *this;
64 }
65 
70 {
71  for (unsigned int i = 0; i < 6; ++i) {
72  for (unsigned int j = 0; j < 6; ++j) {
73  if (i == j) {
74  (*this)[i][j] = 1.0;
75  }
76  else {
77  (*this)[i][j] = 0.0;
78  }
79  }
80  }
81 }
82 
87 
95 
115 {
116  if (full) {
117  buildFrom(M);
118  }
119  else {
121  }
122 }
123 
139  : vpArray2D<double>(6, 6)
140 {
141  buildFrom(t, thetau);
142 }
143 
157 {
158  buildFrom(thetau);
159 }
160 
175  : vpArray2D<double>(6, 6)
176 {
177  buildFrom(t, R);
178 }
179 
192 
208 vpVelocityTwistMatrix::vpVelocityTwistMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz)
209  : vpArray2D<double>(6, 6)
210 {
211  vpTranslationVector t(tx, ty, tz);
212  vpThetaUVector tu(tux, tuy, tuz);
213  buildFrom(t, tu);
214 }
215 
223 {
225 
226  for (unsigned int i = 0; i < 6; ++i) {
227  for (unsigned int j = 0; j < 6; ++j) {
228  double s = 0;
229  for (int k = 0; k < 6; ++k) {
230  s += rowPtrs[i][k] * V.rowPtrs[k][j];
231  }
232  p[i][j] = s;
233  }
234  }
235  return p;
236 }
237 
278 {
279  if (6 != M.getRows()) {
280  throw(vpException(vpException::dimensionError, "Cannot multiply a (6x6) velocity twist matrix by a (%dx%d) matrix",
281  M.getRows(), M.getCols()));
282  }
283 
284  vpMatrix p(6, M.getCols());
285  unsigned int m_col = M.getCols();
286  for (unsigned int i = 0; i < 6; ++i) {
287  for (unsigned int j = 0; j < m_col; ++j) {
288  double s = 0;
289  for (unsigned int k = 0; k < 6; ++k) {
290  s += rowPtrs[i][k] * M[k][j];
291  }
292  p[i][j] = s;
293  }
294  }
295  return p;
296 }
297 
310 {
311  vpColVector c(6);
312 
313  if (6 != v.getRows()) {
315  "Cannot multiply a (6x6) velocity twist matrix by a "
316  "(%d) column vector",
317  v.getRows()));
318  }
319 
320  c = 0.0;
321 
322  for (unsigned int i = 0; i < 6; ++i) {
323  for (unsigned int j = 0; j < 6; ++j) {
324  c[i] += rowPtrs[i][j] * v[j];
325  }
326  }
327 
328  return c;
329 }
330 
343 {
344  for (unsigned int i = 0; i < 3; ++i) {
345  for (unsigned int j = 0; j < 3; ++j) {
346  (*this)[i][j] = R[i][j];
347  (*this)[i + 3][j + 3] = R[i][j];
348  (*this)[i][j + 3] = 0;
349  }
350  }
351  return *this;
352 }
353 
369 {
370  vpMatrix skewaR = t.skew(t) * R;
371 
372  for (unsigned int i = 0; i < 3; ++i) {
373  for (unsigned int j = 0; j < 3; ++j) {
374  (*this)[i][j] = R[i][j];
375  (*this)[i + 3][j + 3] = R[i][j];
376  (*this)[i][j + 3] = skewaR[i][j];
377  }
378  }
379 
380  return *this;
381 }
382 
399 {
400  buildFrom(t, vpRotationMatrix(thetau));
401  return *this;
402 }
403 
417 {
418  buildFrom(vpRotationMatrix(thetau));
419  return *this;
420 }
421 
441 {
442  if (full) {
444  }
445  else {
447  }
448 
449  return *this;
450 }
451 
454 {
457  extract(R);
459  extract(T);
461  RtT = -(R.t() * T);
462 
463  Wi.buildFrom(RtT, R.t());
464 
465  return Wi;
466 }
467 
470 
473 {
474  for (unsigned int i = 0; i < 3; ++i) {
475  for (unsigned int j = 0; j < 3; ++j) {
476  R[i][j] = (*this)[i][j];
477  }
478  }
479 }
480 
483 {
485  extract(R);
486  vpMatrix skTR(3, 3);
487  for (unsigned int i = 0; i < 3; ++i) {
488  for (unsigned int j = 0; j < 3; ++j) {
489  skTR[i][j] = (*this)[i][j + 3];
490  }
491  }
492 
493  vpMatrix skT = skTR * R.t();
494  tv[0] = skT[2][1];
495  tv[1] = skT[0][2];
496  tv[2] = skT[1][0];
497 }
498 
518 int vpVelocityTwistMatrix::print(std::ostream &s, unsigned int length, char const *intro) const
519 {
520  typedef std::string::size_type size_type;
521 
522  unsigned int m = getRows();
523  unsigned int n = getCols();
524 
525  std::vector<std::string> values(m * n);
526  std::ostringstream oss;
527  std::ostringstream ossFixed;
528  std::ios_base::fmtflags original_flags = oss.flags();
529 
530  // --comment: ossFixed <<std::fixed
531  ossFixed.setf(std::ios::fixed, std::ios::floatfield);
532 
533  size_type maxBefore = 0; // the length of the integral part
534  size_type maxAfter = 0; // number of decimals plus
535  // one place for the decimal point
536  for (unsigned int i = 0; i < m; ++i) {
537  for (unsigned int j = 0; j < n; ++j) {
538  oss.str("");
539  oss << (*this)[i][j];
540  if (oss.str().find("e") != std::string::npos) {
541  ossFixed.str("");
542  ossFixed << (*this)[i][j];
543  oss.str(ossFixed.str());
544  }
545 
546  values[(i * n) + j] = oss.str();
547  size_type thislen = values[(i * n) + j].size();
548  size_type p = values[(i * n) + j].find('.');
549 
550  if (p == std::string::npos) {
551  maxBefore = vpMath::maximum(maxBefore, thislen);
552  // maxAfter remains the same
553  }
554  else {
555  maxBefore = vpMath::maximum(maxBefore, p);
556  maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
557  }
558  }
559  }
560 
561  size_type totalLength = length;
562  // increase totalLength according to maxBefore
563  totalLength = vpMath::maximum(totalLength, maxBefore);
564  // decrease maxAfter according to totalLength
565  maxAfter = std::min<size_type>(maxAfter, totalLength - maxBefore);
566  if (maxAfter == 1) {
567  maxAfter = 0;
568  }
569 
570  // the following line is useful for debugging
571  // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
572 
573  if (intro) {
574  s << intro;
575  }
576  s << "[" << m << "," << n << "]=\n";
577 
578  for (unsigned int i = 0; i < m; ++i) {
579  s << " ";
580  for (unsigned int j = 0; j < n; ++j) {
581  size_type p = values[(i * n) + j].find('.');
582  s.setf(std::ios::right, std::ios::adjustfield);
583  s.width(static_cast<std::streamsize>(maxBefore));
584  s << values[(i * n) + j].substr(0, p).c_str();
585 
586  if (maxAfter > 0) {
587  s.setf(std::ios::left, std::ios::adjustfield);
588  if (p != std::string::npos) {
589  s.width(static_cast<std::streamsize>(maxAfter));
590  s << values[(i * n) + j].substr(p, maxAfter).c_str();
591  }
592  else {
593  assert(maxAfter > 1);
594  s.width(static_cast<std::streamsize>(maxAfter));
595  s << ".0";
596  }
597  }
598 
599  s << ' ';
600  }
601  s << std::endl;
602  }
603 
604  s.flags(original_flags); // restore s to standard state
605 
606  return static_cast<int>(maxBefore + maxAfter);
607 }
608 
609 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
610 
618 
619 #endif // #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
unsigned int getCols() const
Definition: vpArray2D.h:327
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:133
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
vpArray2D< double > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1132
unsigned int getRows() const
Definition: vpArray2D.h:337
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
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:252
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Implementation of a rotation matrix and operations on such kind of matrices.
vpRotationMatrix t() const
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
vpVelocityTwistMatrix operator*(const vpVelocityTwistMatrix &V) const
void extract(vpRotationMatrix &R) const
Extract the rotation matrix from the velocity twist matrix.
vpVelocityTwistMatrix inverse() const
Invert the velocity twist matrix.
int print(std::ostream &s, unsigned int length, char const *intro=0) const
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpVelocityTwistMatrix & operator=(const vpVelocityTwistMatrix &V)
vp_deprecated void setIdentity()