Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpVelocityTwistMatrix.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 * Velocity twist transformation matrix.
32 */
33
41
42#include <assert.h>
43#include <sstream>
44
45#include <visp3/core/vpException.h>
46#include <visp3/core/vpVelocityTwistMatrix.h>
47
49const unsigned int vpVelocityTwistMatrix::constr_val_6 = 6;
56{
57 // why not unsigned int
58 const int val_6 = 6;
59 for (int i = 0; i < val_6; ++i) {
60 for (int j = 0; j < val_6; ++j) {
61 rowPtrs[i][j] = V.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
89vpVelocityTwistMatrix::vpVelocityTwistMatrix() : vpArray2D<double>(constr_val_6, constr_val_6) { eye(); }
90
97vpVelocityTwistMatrix::vpVelocityTwistMatrix(const vpVelocityTwistMatrix &V) : vpArray2D<double>(constr_val_6, constr_val_6) { *this = V; }
98
117vpVelocityTwistMatrix::vpVelocityTwistMatrix(const vpHomogeneousMatrix &M, bool full) : vpArray2D<double>(constr_val_6, constr_val_6)
118{
119 if (full) {
120 buildFrom(M);
121 }
122 else {
124 }
125}
126
142 : vpArray2D<double>(constr_val_6, constr_val_6)
143{
144 buildFrom(t, thetau);
145}
146
159vpVelocityTwistMatrix::vpVelocityTwistMatrix(const vpThetaUVector &thetau) : vpArray2D<double>(constr_val_6, constr_val_6)
160{
161 buildFrom(thetau);
162}
163
178 : vpArray2D<double>(constr_val_6, constr_val_6)
179{
180 buildFrom(t, R);
181}
182
194vpVelocityTwistMatrix::vpVelocityTwistMatrix(const vpRotationMatrix &R) : vpArray2D<double>(constr_val_6, constr_val_6) { buildFrom(R); }
195
217vpVelocityTwistMatrix::vpVelocityTwistMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz)
218 : vpArray2D<double>(constr_val_6, constr_val_6)
219{
220 vpTranslationVector t(tx, ty, tz);
221 vpThetaUVector tu(tux, tuy, tuz);
222 buildFrom(t, tu);
223}
224
232{
234 const unsigned int nparam = 6;
235 for (unsigned int i = 0; i < nparam; ++i) {
236 for (unsigned int j = 0; j < nparam; ++j) {
237 double s = 0;
238 for (unsigned int k = 0; k < nparam; ++k) {
239 s += rowPtrs[i][k] * V.rowPtrs[k][j];
240 }
241 p[i][j] = s;
242 }
243 }
244 return p;
245}
246
291{
292 const unsigned int nparam = 6;
293 if (nparam != M.getRows()) {
294 throw(vpException(vpException::dimensionError, "Cannot multiply a (6x6) velocity twist matrix by a (%dx%d) matrix",
295 M.getRows(), M.getCols()));
296 }
297
298 vpMatrix p(nparam, M.getCols());
299 unsigned int m_col = M.getCols();
300 for (unsigned int i = 0; i < nparam; ++i) {
301 for (unsigned int j = 0; j < m_col; ++j) {
302 double s = 0;
303 for (unsigned int k = 0; k < nparam; ++k) {
304 s += rowPtrs[i][k] * M[k][j];
305 }
306 p[i][j] = s;
307 }
308 }
309 return p;
310}
311
324{
325 const unsigned int nparam = 6;
326 vpColVector c(nparam);
327
328 if (nparam != v.getRows()) {
330 "Cannot multiply a (6x6) velocity twist matrix by a "
331 "(%d) column vector",
332 v.getRows()));
333 }
334
335 c = 0.0;
336
337 for (unsigned int i = 0; i < nparam; ++i) {
338 for (unsigned int j = 0; j < nparam; ++j) {
339 c[i] += rowPtrs[i][j] * v[j];
340 }
341 }
342
343 return c;
344}
345
358{
359 const unsigned int index_3 = 3;
360 const unsigned int val_3 = 3;
361 for (unsigned int i = 0; i < val_3; ++i) {
362 for (unsigned int j = 0; j < val_3; ++j) {
363 (*this)[i][j] = R[i][j];
364 (*this)[i + index_3][j + index_3] = R[i][j];
365 (*this)[i][j + index_3] = 0;
366 }
367 }
368 return *this;
369}
370
386{
387 vpMatrix skewaR = t.skew(t) * R;
388
389 const unsigned int index_3 = 3;
390 const unsigned int val_3 = 3;
391 for (unsigned int i = 0; i < val_3; ++i) {
392 for (unsigned int j = 0; j < val_3; ++j) {
393 (*this)[i][j] = R[i][j];
394 (*this)[i + index_3][j + index_3] = R[i][j];
395 (*this)[i][j + index_3] = skewaR[i][j];
396 }
397 }
398
399 return *this;
400}
401
422
440
460{
461 if (full) {
463 }
464 else {
466 }
467
468 return *this;
469}
470
473{
476 extract(R);
478 extract(T);
480 RtT = -(R.t() * T);
481
482 Wi.buildFrom(RtT, R.t());
483
484 return Wi;
485}
486
489
492{
493 const unsigned int val_3 = 3;
494 for (unsigned int i = 0; i < val_3; ++i) {
495 for (unsigned int j = 0; j < val_3; ++j) {
496 R[i][j] = (*this)[i][j];
497 }
498 }
499}
500
503{
505 extract(R);
506 vpMatrix skTR(3, 3);
507 const unsigned int val_3 = 3;
508 for (unsigned int i = 0; i < val_3; ++i) {
509 for (unsigned int j = 0; j < val_3; ++j) {
510 skTR[i][j] = (*this)[i][j + val_3];
511 }
512 }
513
514 vpMatrix skT = skTR * R.t();
515 const unsigned int index_0 = 0;
516 const unsigned int index_1 = 1;
517 const unsigned int index_2 = 2;
518 tv[index_0] = skT[index_2][index_1];
519 tv[index_1] = skT[index_0][index_2];
520 tv[index_2] = skT[index_1][index_0];
521}
522
542int vpVelocityTwistMatrix::print(std::ostream &s, unsigned int length, char const *intro) const
543{
544 typedef std::string::size_type size_type;
545
546 unsigned int m = getRows();
547 unsigned int n = getCols();
548
549 std::vector<std::string> values(m * n);
550 std::ostringstream oss;
551 std::ostringstream ossFixed;
552 std::ios_base::fmtflags original_flags = oss.flags();
553
554 // --comment: could be used ossFixed << std::fixed
555 ossFixed.setf(std::ios::fixed, std::ios::floatfield);
556
557 size_type maxBefore = 0; // the length of the integral part
558 size_type maxAfter = 0; // number of decimals plus
559 // one place for the decimal point
560 for (unsigned int i = 0; i < m; ++i) {
561 for (unsigned int j = 0; j < n; ++j) {
562 oss.str("");
563 oss << (*this)[i][j];
564 if (oss.str().find("e") != std::string::npos) {
565 ossFixed.str("");
566 ossFixed << (*this)[i][j];
567 oss.str(ossFixed.str());
568 }
569
570 values[(i * n) + j] = oss.str();
571 size_type thislen = values[(i * n) + j].size();
572 size_type p = values[(i * n) + j].find('.');
573
574 if (p == std::string::npos) {
575 maxBefore = vpMath::maximum(maxBefore, thislen);
576 // maxAfter remains the same
577 }
578 else {
579 maxBefore = vpMath::maximum(maxBefore, p);
580 maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
581 }
582 }
583 }
584
585 size_type totalLength = length;
586 // increase totalLength according to maxBefore
587 totalLength = vpMath::maximum(totalLength, maxBefore);
588 // decrease maxAfter according to totalLength
589 maxAfter = std::min<size_type>(maxAfter, totalLength - maxBefore);
590 if (maxAfter == 1) {
591 maxAfter = 0;
592 }
593
594 // the following line is useful for debugging
595 // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
596
597 if (intro) {
598 s << intro;
599 }
600 s << "[" << m << "," << n << "]=\n";
601
602 for (unsigned int i = 0; i < m; ++i) {
603 s << " ";
604 for (unsigned int j = 0; j < n; ++j) {
605 size_type p = values[(i * n) + j].find('.');
606 s.setf(std::ios::right, std::ios::adjustfield);
607 s.width(static_cast<std::streamsize>(maxBefore));
608 s << values[(i * n) + j].substr(0, p).c_str();
609
610 if (maxAfter > 0) {
611 s.setf(std::ios::left, std::ios::adjustfield);
612 if (p != std::string::npos) {
613 s.width(static_cast<std::streamsize>(maxAfter));
614 s << values[(i * n) + j].substr(p, maxAfter).c_str();
615 }
616 else {
617 assert(maxAfter > 1);
618 s.width(static_cast<std::streamsize>(maxAfter));
619 s << ".0";
620 }
621 }
622
623 s << ' ';
624 }
625 s << std::endl;
626 }
627
628 s.flags(original_flags); // restore s to standard state
629
630 return static_cast<int>(maxBefore + maxAfter);
631}
632
633#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
634
642
643#endif // #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
644END_VISP_NAMESPACE
unsigned int getCols() const
Definition vpArray2D.h:423
unsigned int size() const
Definition vpArray2D.h:435
vpArray2D< double > t() const
Definition vpArray2D.h:1273
unsigned int getRows() const
Definition vpArray2D.h:433
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
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:257
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.
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.
vpVelocityTwistMatrix & operator=(const vpVelocityTwistMatrix &V)
vpVelocityTwistMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
int print(std::ostream &s, unsigned int length, char const *intro=nullptr) const
VP_DEPRECATED void setIdentity()