1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Translation vector.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #ifndef vpTRANSLATIONVECTOR_H
41 #define vpTRANSLATIONVECTOR_H
42 
43 /*!
44   \file vpTranslationVector.h
45   \brief Class that consider the case of a translation vector.
46 */
47 
48 #include <visp3/core/vpArray2D.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpMatrix.h>
51 #include <visp3/core/vpPoseVector.h>
52 
53 /*!
54   \class vpTranslationVector
55 
56   \ingroup group_core_transformations
57 
58   \brief Class that consider the case of a translation vector.
59 
60   Let us denote \f$^{a}{\bf t}_{b} = [t_x,t_y,t_z]^\top\f$ the translation
61   from frame \f$ a \f$ to frame \f$ b \f$.  The representation of a
62   translation is a column vector of dimension 3.
63 
64   Translations along x,y,z axis are expressed in meters.
65 
66   From the implementation point of view, it is nothing more than an
67   array of three doubles with values in [meters].
68 
69   You can set values [meters] accessing each element:
70   \code
71   vpTranslationVector t;
72   t[0] = 0;
73   t[1] = 0.1;
74   t[2] = 0.5;
75   \endcode
76   You can also initialize the vector using operator<<(double):
77   \code
78   t << 0, 0.1, 05;
79   \endcode
80   Or you can also initialize the vector from a list of doubles if ViSP is build with c++11 enabled:
81   \code
82 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
83   t = {0, 0.1, 0.5};
84 #endif
85   \endcode
86 
87   To get the values [meters] use:
88   \code
89   double tx = t[0];
90   double ty = t[1];
91   double tz = t[2];
92   \endcode
93 
94   The code below shows how to use a translation vector to build an
95   homogeneous matrix.
96 
97   \code
98 #include <visp3/core/vpHomogeneousMatrix.h>
99 #include <visp3/core/vpRotationMatrix.h>
100 #include <visp3/core/vpTranslationVector.h>
101 
102 int main()
103 {
104   vpTranslationVector t; // Translation vector
105 
106   // Initialization of the translation vector
107   t[0] =  0.2; // tx = 0.2 meters
108   t[1] = -0.1; // ty = -0.1 meters
109   t[2] =  1.0; // tz = 1 meters
110 
111   // Construction of a rotation matrix
112   vpRotationMatrix R; // Set to identity by default
113 
114   // Construction of an homogeneous matrix
115   vpHomogeneousMatrix M(t, R);
116 }
117   \endcode
118 */
119 class VISP_EXPORT vpTranslationVector : public vpArray2D<double>
120 {
121 public:
122   /*!
123       Default constructor.
124       The translation vector is initialized to zero.
125     */
vpTranslationVector()126   vpTranslationVector() : vpArray2D<double>(3, 1), m_index(0) {};
127   vpTranslationVector(double tx, double ty, double tz);
128   vpTranslationVector(const vpTranslationVector &tv);
129   explicit vpTranslationVector(const vpHomogeneousMatrix &M);
130   explicit vpTranslationVector(const vpPoseVector &p);
131   explicit vpTranslationVector(const vpColVector &v);
132 
133   vpTranslationVector buildFrom(double tx, double ty, double tz);
134   vpTranslationVector buildFrom(const vpHomogeneousMatrix &M);
135   vpTranslationVector buildFrom(const vpPoseVector &p);
136   vpTranslationVector buildFrom(const vpColVector &v);
137 
138   vp_deprecated double euclideanNorm() const;
139   double frobeniusNorm() const;
140 
141   // operators
142 
143   // translation vectors additions  c = a + b (a, b  unchanged)
144   vpTranslationVector operator+(const vpTranslationVector &tv) const;
145   vpTranslationVector operator+(const vpColVector &v) const;
146   // translation vectors substraction  c = a - b (a, b  unchanged)
147   vpTranslationVector operator-(const vpTranslationVector &tv) const;
148   // negate t = -a  (t is unchanged)
149   vpTranslationVector operator-() const;
150   vpMatrix operator*(const vpRowVector &v) const;
151   // b = x * a (x=scalar)
152   vpTranslationVector operator*(double x) const;
153   vpTranslationVector &operator*=(double x);
154   vpTranslationVector operator/(double x) const;
155   vpTranslationVector &operator/=(double x);
156   // Copy operator.   Allow operation such as A = v
157   vpTranslationVector &operator=(const vpColVector &tv);
158   vpTranslationVector &operator=(const vpTranslationVector &tv);
159   vpTranslationVector &operator=(double x);
160 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
161   vpTranslationVector &operator=(const std::initializer_list<double> &list);
162 #endif
163 
164   //! Operator that allows to set a value of an element \f$t_i\f$: t[i] = x
165   inline double &operator[](unsigned int n) { return *(data + n); }
166   //! Operator that allows to get the value of an element \f$t_i\f$: x = t[i]
167   inline const double &operator[](unsigned int n) const { return *(data + n); }
168 
169   vpTranslationVector &operator<<(double val);
170   vpTranslationVector &operator,(double val);
171 
172 
173   /*!
174     This function is not applicable to a translation vector that is always a
175     3-by-1 column vector.
176     \exception vpException::fatalError When this function is called.
177     */
178   void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
179   {
180     (void)nrows;
181     (void)ncols;
182     (void)flagNullify;
183     throw(vpException(vpException::fatalError, "Cannot resize a translation vector"));
184   };
185 
186   void set(double tx, double ty, double tz);
187 
188   // Skew Symmetric matrix
189   vpMatrix skew() const;
190 
191   double sumSquare() const;
192 
193   vpRowVector t() const;
194 
195   static vpTranslationVector cross(const vpTranslationVector &a, const vpTranslationVector &b);
196   static vpTranslationVector mean(const std::vector<vpHomogeneousMatrix> &vec_M);
197   static vpTranslationVector mean(const std::vector<vpTranslationVector> &vec_t);
198   static vpMatrix skew(const vpTranslationVector &tv);
199   static void skew(const vpTranslationVector &tv, vpMatrix &M);
200 
201 protected:
202   unsigned int m_index; // index used for operator<< and operator, to fill a vector
203 };
204 
205 #endif
206