Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PoseParametrization.cpp
1 #include "PoseParametrization.hh"
2 
3 #include "PoseParametrizationCovariance.hh"
4 
5 #include <Base/Debug/Exception.hh>
6 #include <Base/Common/CompareFloatingPoint.hh>
7 #include <Base/Math/Matrix4x4.hh>
8 #include <Base/Geometry/HomgPoint3D.hh>
9 
10 using namespace BIAS;
11 
14  : Pose_(7), Cov_(7, 7, MatrixZero)
15 {
16  Pose_.SetZero();
17  // init with valid parameterizaton (norml2(quat)==1)
18  Pose_[6]=1.0;
19 }
20 
21 
24  const Matrix<PP_TYPE>& covariance)
25  : Pose_(7), Cov_(7, 7, MatrixZero)
26 {
27  Set(CQvec, covariance);
28 }
29 
30 
33  const Quaternion<PP_TYPE>& orientation,
34  const Matrix<PP_TYPE>& covariance)
35  : Pose_(7), Cov_(7, 7, MatrixZero)
36 {
37  Set(position, orientation, covariance);
38 }
39 
40 
42 Set(const Vector3<PP_TYPE>& position, const Quaternion<PP_TYPE>& orientation,
43  const Matrix<PP_TYPE>& covariance)
44 {
45  Vector<PP_TYPE> CQvec(7);
46  CQ2Pose_(position, orientation, CQvec);
47  Set(CQvec, covariance);
48 }
49 
51 SetCQ(const Vector3<PP_TYPE>& position,
52  const Quaternion<PP_TYPE>& orientation)
53 {
54  Vector<PP_TYPE> CQvec(7);
55  CQ2Pose_(position, orientation, CQvec);
56  Pose_ = CQvec;
57  Cov_.SetZero();
58 }
59 
60 
62 SetCQ(const Vector<PP_TYPE>& CQvec)
63 {
64  if (CQvec.size() != 7){
65  BEXCEPTION("invalid vector size, must be 7");
66  }
67  Pose_ = CQvec;
68  Cov_.SetZero();
69 }
70 
71 
74 {
75  if(Cov.num_rows() != 7 || Cov.num_cols() != 7) {
76  BEXCEPTION("dimension of argument covariance matrix does not match!\n");
77  }
78  // todo: check symmetry and positiv definitness of cov
79  Cov_ = Cov;
80 }
81 
82 
84 GetPosition() const
85 {
88  Pose2CQ_(Pose_, c, q);
89  return c;
90 }
91 
92 
95 {
98  Pose2CQ_(Pose_, c, q);
99  return q;
100 }
101 
102 
105 {
106  Pose_[0] = C[0];
107  Pose_[1] = C[1];
108  Pose_[2] = C[2];
109  Cov_.SetZero();
110 }
111 
112 
115 {
116  Pose_[3] = q[0];
117  Pose_[4] = q[1];
118  Pose_[5] = q[2];
119  Pose_[6] = q[3];
120  Cov_.SetZero();
121 }
122 
123 
125 Load(const std::string& file)
126 {
127  std::ifstream ifs(file.c_str());
128  if(!ifs)
129  return false;
130  ifs>>*this;
131  if(!ifs) {
132  ifs.close();
133  return false;
134  }
135  ifs.close();
136  return true;
137 }
138 
139 
141 Save(const std::string& file) const
142 {
143  std::ofstream ofs(file.c_str());
144  if(!ofs)
145  return false;
146  ofs<<*this;
147  if(!ofs) {
148  ofs.close();
149  return false;
150  }
151  ofs.close();
152  return true;
153 }
154 
155 
158 {
159  BIASERR("untested");
161  RMatrixBase R;
162  R.SetFromQuaternion(q);
163  for (int r=0; r<3; r++){
164  Transf[r][3] = T[r];
165  for (int c=0; c<3; c++){
166  Transf[r][c] = R[r][c];
167  }
168  }
169 
170  Vector3<PP_TYPE> pos = GetPosition(), new_pos;
171  HomgPoint3D src(pos), dst;
172  dst = Transf * src;
173  if (dst.IsAtInfinity()){
174  BEXCEPTION("position is at infinity: "<<dst);
175  } else {
176  dst.GetEuclidean(new_pos);
177  }
178 
179  Quaternion<PP_TYPE> o = GetOrientation();
180  o = q * o;
181 
182  PoseParametrizationCovariance cov = GetCovarianceMatrix();
183  cov.Transform(Transf);
184 
185  Set(new_pos, o, cov);
186 }
187 
190  const double s)
191 {
192  BIASERR("untested");
194  RMatrixBase R;
195  R.SetFromQuaternion(q);
196  for (int r=0; r<3; r++){
197  Transf[r][3] = T[r];
198  for (int c=0; c<3; c++){
199  Transf[r][c] = s*R[r][c];
200  }
201  }
202 
203  Vector3<PP_TYPE> pos = GetPosition(), new_pos;
204  HomgPoint3D src(pos), dst;
205  dst = Transf * src;
206  if (dst.IsAtInfinity()){
207  BEXCEPTION("position is at infinity: "<<dst);
208  } else {
209  dst.GetEuclidean(new_pos);
210  }
211 
212  Quaternion<PP_TYPE> o = GetOrientation();
213  o = q * o;
214 
215  PoseParametrizationCovariance cov = GetCovarianceMatrix();
216  cov.Transform(Transf);
217 
218  Set(new_pos, o, cov);
219 }
220 
221 
222 std::ostream& BIAS::
223 operator<<(std::ostream &os, const PoseParametrization& p)
224 {
225  os<< "position= "<<p.GetPosition()<< " orientation= "<<p.GetOrientation()
226  << " cov= "<<p.GetCovarianceMatrix();
227  return os;
228 }
229 
230 
231 std::istream& BIAS::
232 operator>>(std::istream &is, PoseParametrization& p)
233 {
234  std::string temp;
235  Vector3<PP_TYPE> position;
236  Quaternion<PP_TYPE> orientation;
237  Matrix<PP_TYPE> cov;
238  is>>temp>>position>>temp>>orientation>>temp>>cov;
239  p.Set(position, orientation, cov);
240  return is;
241 }
bool Load(const std::string &file)
void GetEuclidean(Vector3< HOMGPOINT3D_TYPE > &dest) const
calculate affine coordinates of this and write them to dest affine coordinates are projective coordin...
Definition: HomgPoint3D.hh:336
void Pose2CQ_(const Vector< PP_TYPE > &pose, Vector3< PP_TYPE > &position, Quaternion< PP_TYPE > &orientation) const
Subscript num_cols() const
Definition: cmat.h:320
void Transform(const Vector3< double > &T, const Quaternion< double > &q)
Transforms the pose and associated covariance according to the point transformation parametrized as t...
void SetCovarianceMatrix(const Matrix< PP_TYPE > &Cov)
Sets covariance matrix from Cov.
bool Save(const std::string &file) const
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
Slim class bundeling pose parametrization and associated covariance matrix.
Vector< PP_TYPE > Pose_
first 3 entries relate to the position
void SetZero()
equivalent to matrix call
Definition: Vector.hh:156
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
const Matrix< PP_TYPE > & GetCovarianceMatrix() const
returns the covariance matrix associated with the CQ vector
void SetCQ(const Vector< PP_TYPE > &CQvec)
Set this from pose parametrization.
void SetPosition(const Vector3< PP_TYPE > &C)
set position part of vector and zero covariance matrix
bool IsAtInfinity() const
Definition: HomgPoint3D.hh:398
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
Vector3< PP_TYPE > GetPosition() const
returns the position (first 3 entries) part of the pose parametrization vector
void Set(const Vector3< PP_TYPE > &position, const Quaternion< PP_TYPE > &orientation, const Matrix< PP_TYPE > &covariance=Matrix< double >(7, 7, MatrixZero))
Set this from position, orientation and covariance.
void SetOrientation(const Quaternion< PP_TYPE > &q)
set orientation part of vector and zero covariance matrix
Matrix< PP_TYPE > Cov_
first columns relate to position
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
class encapsulating the transfromations between different pose parametrizations
Subscript num_rows() const
Definition: cmat.h:319
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
void Transform(const Matrix4x4< double > &transf)
Given a 6D pose (position and euler angles) and its associated covariance, the function transforms th...
class for rotation with axis and angle
Quaternion< PP_TYPE > GetOrientation() const
returns the quaternion (last 4 entries) part of the pose parametrization vector
void CQ2Pose_(const Vector3< PP_TYPE > &position, const Quaternion< PP_TYPE > &orientation, Vector< PP_TYPE > &pose) const
Subscript size() const
Definition: vec.h:262
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157