Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTranslationVector.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 * Translation vector.
32 */
33
38
39#include <stdio.h>
40#include <string.h>
41
42#include <visp3/core/vpTranslationVector.h>
43
45const unsigned int vpTranslationVector::constr_val_3 = 3;
54vpTranslationVector::vpTranslationVector(double tx, double ty, double tz) : vpArray2D<double>(constr_val_3, 1), m_index(0)
55{
56 const unsigned int index_0 = 0;
57 const unsigned int index_1 = 1;
58 const unsigned int index_2 = 2;
59 (*this)[index_0] = tx;
60 (*this)[index_1] = ty;
61 (*this)[index_2] = tz;
62}
63
72{
73 M.extract(*this);
74}
75
84{
85 const unsigned int index_0 = 0;
86 const unsigned int index_1 = 1;
87 const unsigned int index_2 = 2;
88 (*this)[index_0] = p[index_0];
89 (*this)[index_1] = p[index_1];
90 (*this)[index_2] = p[index_2];
91}
92
104
120{
121 const unsigned int val_3 = 3;
122 if (v.size() != val_3) {
123 throw(vpException(vpException::dimensionError,
124 "Cannot construct a translation vector from a "
125 "%d-dimension column vector",
126 v.size()));
127 }
128}
129
142{
143 M.extract(*this);
144 return *this;
145}
146
157{
158 const unsigned int index_0 = 0;
159 const unsigned int index_1 = 1;
160 const unsigned int index_2 = 2;
161 (*this)[index_0] = p[index_0];
162 (*this)[index_1] = p[index_1];
163 (*this)[index_2] = p[index_2];
164 return *this;
165}
166
177{
178 const unsigned int index_0 = 0;
179 const unsigned int index_1 = 1;
180 const unsigned int index_2 = 2;
181 const unsigned int v_size = 3;
182 if (v.size() != v_size) {
184 "Cannot build a translation vector from a %d-dimension column vector", v.size()));
185 }
186
187 (*this)[index_0] = v[index_0];
188 (*this)[index_1] = v[index_1];
189 (*this)[index_2] = v[index_2];
190 return *this;
191}
192
203vpTranslationVector &vpTranslationVector::buildFrom(const double &tx, const double &ty, const double &tz)
204{
205 set(tx, ty, tz);
206 return *this;
207}
208
217void vpTranslationVector::set(double tx, double ty, double tz)
218{
219 const unsigned int index_0 = 0;
220 const unsigned int index_1 = 1;
221 const unsigned int index_2 = 2;
222 (*this)[index_0] = tx;
223 (*this)[index_1] = ty;
224 (*this)[index_2] = tz;
225}
226
246{
248
249 const unsigned int val_3 = 3;
250 for (unsigned int i = 0; i < val_3; ++i) {
251 s[i] = (*this)[i] + tv[i];
252 }
253
254 return s;
255}
256
278{
279 const unsigned int val_3 = 3;
280 if (v.size() != val_3) {
281 throw(vpException(vpException::dimensionError, "Cannot add translation vector to a %d-dimension column vector",
282 v.size()));
283 }
285
286 for (unsigned int i = 0; i < val_3; ++i) {
287 s[i] = (*this)[i] + v[i];
288 }
289
290 return s;
291}
292
312{
314
315 const unsigned int val_3 = 3;
316 for (unsigned int i = 0; i < val_3; ++i) {
317 sub[i] = (*this)[i] - tv[i];
318 }
319
320 return sub;
321}
322
338{
340 for (unsigned int i = 0; i < dsize; ++i) {
341 *(tv.data + i) = -*(data + i);
342 }
343
344 return tv;
345}
346
364{
366 for (unsigned int i = 0; i < dsize; ++i) {
367 *(tv.data + i) = (*(data + i)) * x;
368 }
369
370 return tv;
371}
372
383{
384 vpMatrix M(rowNum, v.getCols());
385 unsigned int v_col = v.getCols();
386 for (unsigned int i = 0; i < rowNum; ++i) {
387 for (unsigned int j = 0; j < v_col; ++j) {
388 M[i][j] = (*this)[i] * v[j];
389 }
390 }
391 return M;
392}
393
403{
404 for (unsigned int i = 0; i < rowNum; ++i) {
405 (*this)[i] *= x;
406 }
407 return *this;
408}
409
418{
419 for (unsigned int i = 0; i < rowNum; ++i) {
420 (*this)[i] /= x;
421 }
422 return *this;
423}
424
442{
444 for (unsigned int i = 0; i < dsize; ++i) {
445 *(tv.data + i) = (*(data + i)) / x;
446 }
447
448 return tv;
449}
450
468{
469 const unsigned int val_3 = 3;
470 if (tv.size() != val_3) {
472 "Cannot initialize a translation vector from a "
473 "%d-dimension col vector",
474 tv.size()));
475 }
476 unsigned int k = tv.size();
477 if (rowNum != k) {
478 try {
479 resize(k, 1);
480 }
481 catch (...) {
482 throw;
483 }
484 }
485
486 memcpy(data, tv.data, rowNum * sizeof(double));
487
488 return *this;
489}
490
504{
505 unsigned int k = tv.rowNum;
506 if (rowNum != k) {
507 try {
508 resize(k, 1);
509 }
510 catch (...) {
511 throw;
512 }
513 }
514
515 memcpy(data, tv.data, rowNum * sizeof(double));
516
517 return *this;
518}
519
532{
533 double *d = data;
534
535 const int val_3 = 3;
536 for (int i = 0; i < val_3; ++i) {
537 *(d++) = x;
538 }
539
540 return *this;
541}
542
543#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
565vpTranslationVector &vpTranslationVector::operator=(const std::initializer_list<double> &list)
566{
567 if (list.size() > size()) {
568 throw(vpException(
570 "Cannot set translation vector out of bounds. It has only %d values while you try to initialize with %d values",
571 size(), list.size()));
572 }
573 std::copy(list.begin(), list.end(), data);
574 return *this;
575}
576#endif
577
606{
607 m_index = 0;
608 data[m_index] = val;
609 return *this;
610}
611
640{
641 ++m_index;
642 if (m_index >= size()) {
643 throw(vpException(
645 "Cannot set translation vector out of bounds. It has only %d values while you try to initialize with %d values",
646 size(), m_index + 1));
647 }
648 data[m_index] = val;
649 return *this;
650}
651
670{
671 const unsigned int index_0 = 0;
672 const unsigned int index_1 = 1;
673 const unsigned int index_2 = 2;
674 const unsigned int val_3 = 3;
675 M.resize(val_3, val_3);
676 M[index_0][index_0] = 0;
677 M[index_0][index_1] = -tv[index_2];
678 M[index_0][index_2] = tv[index_1];
679 M[index_1][index_0] = tv[index_2];
680 M[index_1][index_1] = 0;
681 M[index_1][index_2] = -tv[index_0];
682 M[index_2][index_0] = -tv[index_1];
683 M[index_2][index_1] = tv[index_0];
684 M[index_2][index_2] = 0;
685}
686
706{
707 vpMatrix M(3, 3);
708 skew(tv, M);
709 return M;
710}
711
731{
732 vpMatrix M(3, 3);
733 skew(*this, M);
734 return M;
735}
736
752
757{
759 memcpy(v.data, data, rowNum * sizeof(double));
760 return v;
761}
762
763#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
764
776
777#endif
778
785{
786 double norm = sumSquare();
787
788 return sqrt(norm);
789}
790
798{
799 double sum_square = 0.0;
800
801 for (unsigned int i = 0; i < rowNum; ++i) {
802 double x = rowPtrs[i][0];
803 sum_square += x * x;
804 }
805
806 return sum_square;
807}
808
817vpTranslationVector vpTranslationVector::mean(const std::vector<vpHomogeneousMatrix> &vec_M)
818{
819 vpColVector meanT(3);
820 size_t vec_m_size = vec_M.size();
821 for (size_t i = 0; i < vec_m_size; ++i) {
822 meanT += static_cast<vpColVector>(vec_M[i].getTranslationVector());
823 }
824 meanT /= static_cast<double>(vec_M.size());
825
826 vpTranslationVector t(meanT);
827 return t;
828}
829
838vpTranslationVector vpTranslationVector::mean(const std::vector<vpTranslationVector> &vec_t)
839{
840 vpColVector meanT(3);
841 size_t l_vec_t_size = vec_t.size();
842 for (size_t i = 0; i < l_vec_t_size; ++i) {
843 meanT += static_cast<vpColVector>(vec_t[i]);
844 }
845 meanT /= static_cast<double>(vec_t.size());
846
847 vpTranslationVector t(meanT);
848 return t;
849}
850END_VISP_NAMESPACE
Type * data
Address of the first element of the data array.
Definition vpArray2D.h:149
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
unsigned int rowNum
Definition vpArray2D.h:1201
unsigned int dsize
Definition vpArray2D.h:1207
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
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.
void extract(vpRotationMatrix &R) const
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Implementation of a pose vector and operations on poses.
Implementation of row vector and the associated operations.
void set(double tx, double ty, double tz)
vpTranslationVector & operator=(const vpColVector &tv)
static vpTranslationVector mean(const std::vector< vpHomogeneousMatrix > &vec_M)
VP_DEPRECATED double euclideanNorm() const
vpTranslationVector & buildFrom(const double &tx, const double &ty, const double &tz)
vpTranslationVector operator/(double x) const
VP_NORETURN void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
vpTranslationVector & operator/=(double x)
vpTranslationVector & operator*=(double x)
vpMatrix operator*(const vpRowVector &v) const
vpTranslationVector operator-() const
vpTranslationVector operator+(const vpTranslationVector &tv) const
vpTranslationVector & operator<<(double val)
vpTranslationVector & operator,(double val)
static vpTranslationVector cross(const vpTranslationVector &a, const vpTranslationVector &b)