Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
44
BEGIN_VISP_NAMESPACE
45
const
unsigned
int
vpTranslationVector::constr_val_3 = 3;
54
vpTranslationVector::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
71
vpTranslationVector::vpTranslationVector
(
const
vpHomogeneousMatrix
&M) :
vpArray2D
<double>(constr_val_3, 1),
m_index
(0)
72
{
73
M.
extract
(*
this
);
74
}
75
83
vpTranslationVector::vpTranslationVector
(
const
vpPoseVector
&p) :
vpArray2D
<double>(constr_val_3, 1),
m_index
(0)
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
103
vpTranslationVector::vpTranslationVector
(
const
vpTranslationVector
&tv) :
vpArray2D
<double>(tv),
m_index
(0) { }
104
119
vpTranslationVector::vpTranslationVector
(
const
vpColVector
&v) :
vpArray2D
<double>(v),
m_index
(0)
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
141
vpTranslationVector
&
vpTranslationVector::buildFrom
(
const
vpHomogeneousMatrix
&M)
142
{
143
M.
extract
(*
this
);
144
return
*
this
;
145
}
146
156
vpTranslationVector
&
vpTranslationVector::buildFrom
(
const
vpPoseVector
&p)
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
176
vpTranslationVector
&
vpTranslationVector::buildFrom
(
const
vpColVector
&v)
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) {
183
throw
(
vpException
(
vpException::dimensionError
,
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
203
vpTranslationVector
&
vpTranslationVector::buildFrom
(
const
double
&tx,
const
double
&ty,
const
double
&tz)
204
{
205
set
(tx, ty, tz);
206
return
*
this
;
207
}
208
217
void
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
245
vpTranslationVector
vpTranslationVector::operator+
(
const
vpTranslationVector
&tv)
const
246
{
247
vpTranslationVector
s;
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
277
vpTranslationVector
vpTranslationVector::operator+
(
const
vpColVector
&v)
const
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
}
284
vpTranslationVector
s;
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
311
vpTranslationVector
vpTranslationVector::operator-
(
const
vpTranslationVector
&tv)
const
312
{
313
vpTranslationVector
sub;
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
337
vpTranslationVector
vpTranslationVector::operator-
() const
// negate
338
{
339
vpTranslationVector
tv;
340
for
(
unsigned
int
i = 0; i <
dsize
; ++i) {
341
*(tv.
data
+ i) = -*(
data
+ i);
342
}
343
344
return
tv;
345
}
346
363
vpTranslationVector
vpTranslationVector::operator*
(
double
x)
const
364
{
365
vpTranslationVector
tv;
366
for
(
unsigned
int
i = 0; i <
dsize
; ++i) {
367
*(tv.
data
+ i) = (*(
data
+ i)) * x;
368
}
369
370
return
tv;
371
}
372
382
vpMatrix
vpTranslationVector::operator*
(
const
vpRowVector
&v)
const
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
402
vpTranslationVector
&
vpTranslationVector::operator*=
(
double
x)
403
{
404
for
(
unsigned
int
i = 0; i <
rowNum
; ++i) {
405
(*this)[i] *= x;
406
}
407
return
*
this
;
408
}
409
417
vpTranslationVector
&
vpTranslationVector::operator/=
(
double
x)
418
{
419
for
(
unsigned
int
i = 0; i <
rowNum
; ++i) {
420
(*this)[i] /= x;
421
}
422
return
*
this
;
423
}
424
441
vpTranslationVector
vpTranslationVector::operator/
(
double
x)
const
442
{
443
vpTranslationVector
tv;
444
for
(
unsigned
int
i = 0; i <
dsize
; ++i) {
445
*(tv.
data
+ i) = (*(
data
+ i)) / x;
446
}
447
448
return
tv;
449
}
450
467
vpTranslationVector
&
vpTranslationVector::operator=
(
const
vpColVector
&tv)
468
{
469
const
unsigned
int
val_3 = 3;
470
if
(tv.
size
() != val_3) {
471
throw
(
vpException
(
vpException::dimensionError
,
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
503
vpTranslationVector
&
vpTranslationVector::operator=
(
const
vpTranslationVector
&tv)
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
531
vpTranslationVector
&
vpTranslationVector::operator=
(
double
x)
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)
565
vpTranslationVector
&
vpTranslationVector::operator=
(
const
std::initializer_list<double> &list)
566
{
567
if
(list.size() >
size
()) {
568
throw
(
vpException
(
569
vpException::dimensionError
,
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
605
vpTranslationVector
&
vpTranslationVector::operator<<
(
double
val)
606
{
607
m_index
= 0;
608
data
[
m_index
] = val;
609
return
*
this
;
610
}
611
639
vpTranslationVector
&
vpTranslationVector::operator,
(
double
val)
640
{
641
++
m_index
;
642
if
(
m_index
>=
size
()) {
643
throw
(
vpException
(
644
vpException::dimensionError
,
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
669
void
vpTranslationVector::skew
(
const
vpTranslationVector
&tv,
vpMatrix
&M)
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
705
vpMatrix
vpTranslationVector::skew
(
const
vpTranslationVector
&tv)
706
{
707
vpMatrix
M(3, 3);
708
skew
(tv, M);
709
return
M;
710
}
711
730
vpMatrix
vpTranslationVector::skew
()
const
731
{
732
vpMatrix
M(3, 3);
733
skew
(*
this
, M);
734
return
M;
735
}
736
747
vpTranslationVector
vpTranslationVector::cross
(
const
vpTranslationVector
&a,
const
vpTranslationVector
&b)
748
{
749
vpMatrix
skew_a =
vpTranslationVector::skew
(a);
750
return
static_cast<
vpTranslationVector
>
(skew_a * b);
751
}
752
756
vpRowVector
vpTranslationVector::t
()
const
757
{
758
vpRowVector
v(
rowNum
);
759
memcpy(v.data,
data
,
rowNum
*
sizeof
(
double
));
760
return
v;
761
}
762
763
#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
764
775
double
vpTranslationVector::euclideanNorm
()
const
{
return
frobeniusNorm
(); }
776
777
#endif
778
784
double
vpTranslationVector::frobeniusNorm
()
const
785
{
786
double
norm =
sumSquare
();
787
788
return
sqrt(norm);
789
}
790
797
double
vpTranslationVector::sumSquare
()
const
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
817
vpTranslationVector
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
838
vpTranslationVector
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
}
850
END_VISP_NAMESPACE
vpArray2D::data
Type * data
Address of the first element of the data array.
Definition
vpArray2D.h:149
vpArray2D< double >::rowPtrs
double ** rowPtrs
Definition
vpArray2D.h:1205
vpArray2D< double >::vpArray2D
vpArray2D()
Definition
vpArray2D.h:155
vpArray2D::resize
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition
vpArray2D.h:448
vpArray2D< double >::rowNum
unsigned int rowNum
Definition
vpArray2D.h:1201
vpArray2D< double >::dsize
unsigned int dsize
Definition
vpArray2D.h:1207
vpArray2D::size
unsigned int size() const
Return the number of elements of the 2D array.
Definition
vpArray2D.h:435
vpColVector
Implementation of column vector and the associated operations.
Definition
vpColVector.h:191
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::dimensionError
@ dimensionError
Bad dimension.
Definition
vpException.h:71
vpHomogeneousMatrix
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition
vpHomogeneousMatrix.h:221
vpHomogeneousMatrix::extract
void extract(vpRotationMatrix &R) const
Definition
vpHomogeneousMatrix.cpp:912
vpMatrix
Implementation of a matrix and operations on matrices.
Definition
vpMatrix.h:175
vpPoseVector
Implementation of a pose vector and operations on poses.
Definition
vpPoseVector.h:203
vpRowVector
Implementation of row vector and the associated operations.
Definition
vpRowVector.h:124
vpTranslationVector::m_index
unsigned int m_index
Definition
vpTranslationVector.h:208
vpTranslationVector::set
void set(double tx, double ty, double tz)
Definition
vpTranslationVector.cpp:217
vpTranslationVector::operator=
vpTranslationVector & operator=(const vpColVector &tv)
Definition
vpTranslationVector.cpp:467
vpTranslationVector::vpTranslationVector
vpTranslationVector()
Definition
vpTranslationVector.h:125
vpTranslationVector::mean
static vpTranslationVector mean(const std::vector< vpHomogeneousMatrix > &vec_M)
Definition
vpTranslationVector.cpp:817
vpTranslationVector::euclideanNorm
VP_DEPRECATED double euclideanNorm() const
Definition
vpTranslationVector.cpp:775
vpTranslationVector::buildFrom
vpTranslationVector & buildFrom(const double &tx, const double &ty, const double &tz)
Definition
vpTranslationVector.cpp:203
vpTranslationVector::operator/
vpTranslationVector operator/(double x) const
Definition
vpTranslationVector.cpp:441
vpTranslationVector::resize
VP_NORETURN void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
Definition
vpTranslationVector.h:175
vpTranslationVector::operator/=
vpTranslationVector & operator/=(double x)
Definition
vpTranslationVector.cpp:417
vpTranslationVector::operator*=
vpTranslationVector & operator*=(double x)
Definition
vpTranslationVector.cpp:402
vpTranslationVector::skew
vpMatrix skew() const
Definition
vpTranslationVector.cpp:730
vpTranslationVector::operator*
vpMatrix operator*(const vpRowVector &v) const
Definition
vpTranslationVector.cpp:382
vpTranslationVector::frobeniusNorm
double frobeniusNorm() const
Definition
vpTranslationVector.cpp:784
vpTranslationVector::operator-
vpTranslationVector operator-() const
Definition
vpTranslationVector.cpp:337
vpTranslationVector::operator+
vpTranslationVector operator+(const vpTranslationVector &tv) const
Definition
vpTranslationVector.cpp:245
vpTranslationVector::operator<<
vpTranslationVector & operator<<(double val)
Definition
vpTranslationVector.cpp:605
vpTranslationVector::operator,
vpTranslationVector & operator,(double val)
Definition
vpTranslationVector.cpp:639
vpTranslationVector::sumSquare
double sumSquare() const
Definition
vpTranslationVector.cpp:797
vpTranslationVector::cross
static vpTranslationVector cross(const vpTranslationVector &a, const vpTranslationVector &b)
Definition
vpTranslationVector.cpp:747
vpTranslationVector::t
vpRowVector t() const
Definition
vpTranslationVector.cpp:756
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
core
src
math
transformation
vpTranslationVector.cpp
Generated by
1.17.0