Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eigen.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR a PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <pcl/pcl_macros.h>
37 
38 void pcl::getTransFromUnitVectorsZY(const Eigen::Vector3f& z_axis, const Eigen::Vector3f& y_direction, Eigen::Affine3f& transformation)
39 {
40  Eigen::Vector3f tmp0 = (y_direction.cross(z_axis)).normalized();
41  Eigen::Vector3f tmp1 = (z_axis.cross(tmp0)).normalized();
42  Eigen::Vector3f tmp2 = z_axis.normalized();
43 
44  transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
45  transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
46  transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
47  transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
48 }
49 
50 Eigen::Affine3f pcl::getTransFromUnitVectorsZY(const Eigen::Vector3f& z_axis, const Eigen::Vector3f& y_direction)
51 {
52  Eigen::Affine3f transformation;
53  getTransFromUnitVectorsZY(z_axis, y_direction, transformation);
54  return transformation;
55 }
56 
57 void pcl::getTransFromUnitVectorsXY(const Eigen::Vector3f& x_axis, const Eigen::Vector3f& y_direction, Eigen::Affine3f& transformation)
58 {
59  Eigen::Vector3f tmp2 = (x_axis.cross(y_direction)).normalized();
60  Eigen::Vector3f tmp1 = (tmp2.cross(x_axis)).normalized();
61  Eigen::Vector3f tmp0 = x_axis.normalized();
62 
63  transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
64  transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
65  transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
66  transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
67 }
68 
69 Eigen::Affine3f pcl::getTransFromUnitVectorsXY(const Eigen::Vector3f& x_axis, const Eigen::Vector3f& y_direction)
70 {
71  Eigen::Affine3f transformation;
72  getTransFromUnitVectorsXY(x_axis, y_direction, transformation);
73  return transformation;
74 }
75 
76 void pcl::getTransformationFromTwoUnitVectors(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis, Eigen::Affine3f& transformation)
77 {
78  getTransFromUnitVectorsZY(z_axis, y_direction, transformation);
79 }
80 
81 Eigen::Affine3f pcl::getTransformationFromTwoUnitVectors(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis)
82 {
83  Eigen::Affine3f transformation;
84  getTransformationFromTwoUnitVectors(y_direction, z_axis, transformation);
85  return transformation;
86 }
87 
88 void pcl::getTransformationFromTwoUnitVectorsAndOrigin(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis,
89  const Eigen::Vector3f& origin, Eigen::Affine3f& transformation)
90 {
91  getTransformationFromTwoUnitVectors(y_direction, z_axis, transformation);
92  Eigen::Vector3f translation = transformation*origin;
93  transformation(0,3)=-translation[0]; transformation(1,3)=-translation[1]; transformation(2,3)=-translation[2];
94 }
95 
96 void pcl::getEulerAngles(const Eigen::Affine3f& t, float& roll, float& pitch, float& yaw)
97 {
98  roll = atan2f(t(2,1), t(2,2));
99  pitch = asinf(-t(2,0));
100  yaw = atan2f(t(1,0), t(0,0));
101 }
102 
103 void pcl::getTranslationAndEulerAngles(const Eigen::Affine3f& t, float& x, float& y, float& z, float& roll, float& pitch, float& yaw)
104 {
105  x = t(0,3);
106  y = t(1,3);
107  z = t(2,3);
108  roll = atan2f(t(2,1), t(2,2));
109  pitch = asinf(-t(2,0));
110  yaw = atan2f(t(1,0), t(0,0));
111 }
112 
113 void pcl::getTransformation(float x, float y, float z, float roll, float pitch, float yaw, Eigen::Affine3f& t)
114 {
115  float A=cosf(yaw), B=sinf(yaw), C=cosf(pitch), D=sinf(pitch),
116  E=cosf(roll), F=sinf(roll), DE=D*E, DF=D*F;
117  t(0,0) = A*C; t(0,1) = A*DF - B*E; t(0,2) = B*F + A*DE; t(0,3) = x;
118  t(1,0) = B*C; t(1,1) = A*E + B*DF; t(1,2) = B*DE - A*F; t(1,3) = y;
119  t(2,0) = -D; t(2,1) = C*F; t(2,2) = C*E; t(2,3) = z;
120  t(3,0) = 0; t(3,1) = 0; t(3,2) = 0; t(3,3) = 1;
121 }
122 
123 Eigen::Affine3f pcl::getTransformation(float x, float y, float z, float roll, float pitch, float yaw)
124 {
125  Eigen::Affine3f t;
126  getTransformation(x, y, z, roll, pitch, yaw, t);
127  return t;
128 }
129 
130 template <typename Derived> void
131 pcl::saveBinary (const Eigen::MatrixBase<Derived>& matrix, std::ostream& file)
132 {
133  uint32_t rows = static_cast<uint32_t> (matrix.rows ()), cols = static_cast<uint32_t> (matrix.cols ());
134  file.write (reinterpret_cast<char*> (&rows), sizeof (rows));
135  file.write (reinterpret_cast<char*> (&cols), sizeof (cols));
136  for (uint32_t i = 0; i < rows; ++i)
137  for (uint32_t j = 0; j < cols; ++j)
138  {
139  typename Derived::Scalar tmp = matrix(i,j);
140  file.write (reinterpret_cast<const char*> (&tmp), sizeof (tmp));
141  }
142 }
143 
144 template <typename Derived> void
145 pcl::loadBinary (Eigen::MatrixBase<Derived> const & matrix_, std::istream& file)
146 {
147  Eigen::MatrixBase<Derived> &matrix = const_cast<Eigen::MatrixBase<Derived> &> (matrix_);
148 
149  uint32_t rows, cols;
150  file.read (reinterpret_cast<char*> (&rows), sizeof (rows));
151  file.read (reinterpret_cast<char*> (&cols), sizeof (cols));
152  if (matrix.rows () != static_cast<int>(rows) || matrix.cols () != static_cast<int>(cols))
153  matrix.derived().resize(rows, cols);
154 
155  for (uint32_t i = 0; i < rows; ++i)
156  for (uint32_t j = 0; j < cols; ++j)
157  {
158  typename Derived::Scalar tmp;
159  file.read (reinterpret_cast<char*> (&tmp), sizeof (tmp));
160  matrix (i, j) = tmp;
161  }
162 }