Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Pose.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 #include <Base/Common/W32Compat.hh>
25 #include <Geometry/Pose.hh>
26 #include <Base/Common/CompareFloatingPoint.hh>
27 #include <Base/Geometry/PoseParametrizationCovariance.hh>
28 #include <fstream>
29 #include <iostream>
30 
31 #define POSE_STREAM_PRECISION 12
32 
33 using namespace BIAS;
34 using namespace std;
35 
37 {
38  Cov_.newsize(7, 7);
39  Cov_.Fill(0);
40 }
41 
43 {
44  Cov_.clear();
45 }
46 
49 {
51  for (unsigned int row = 0; row < 3; row++)
52  {
53  for (unsigned int col = 0; col < 3; col++)
54  {
55  ret[row][col] = Cov_[row][col];
56  }
57  }
58  return ret;
59 }
60 
63 {
65  for (unsigned int row = 0; row < 4; row++)
66  {
67  for (unsigned int col = 0; col < 4; col++)
68  {
69  ret[row][col] = Cov_[row + 3][col + 3];
70  }
71  }
72  return ret;
73 }
74 
75 void
77 {
79  Cov_.SetZero();
80 }
81 
82 void
84 {
86  Cov_.SetZero();
87 }
88 
89 void
91 {
93  for (unsigned int i = 0; i < 3; i++)
94  {
95  for (unsigned int j = 0; j < 3; j++)
96  {
97  Cov_[i][j] = cov[i][j];
98  }
99  }
100 }
101 
102 void
104 {
106  for (unsigned int row = 0; row < 4; row++)
107  {
108  for (unsigned int col = 0; col < 4; col++)
109  {
110  Cov_[row + 3][col + 3] = cov[row][col];
111  }
112  }
113 }
114 
115 void
117 {
119  Cov_.SetZero();
120 }
121 
122 int
124 {
125  if (Cov.GetRows() != 6 || Cov.GetCols() != 6)
126  {
127  BIASERR("Euclidean Pose Covariance has to be 7x7");
128  return -1;
129  }
130  else
131  {
132  BIASERR("Not implemented!!");
133  BIASABORT;
134  return -2;
135  }
136  //return 0;
137 }
138 
139 int
141 {
142  int ret = 0;
143  Set(pose.GetOrientation(), pose.GetPosition());
144  ret -= SetCovariance(pose.GetCovarianceMatrix());
145  return ret;
146 }
147 
148 void
150  const Matrix<POSE_TYPE>& Cov)
151 {
152  Set(Q, C);
153  SetCovariance(Cov);
154 }
155 
156 int
158 {
159  if (c.GetRows() != 7 || c.GetCols() != 7)
160  {
161  BIASERR("Pose Covariance has to be 7x7");
162  Cov_.SetZero();
163  return -1;
164  }
165  else
166  {
167  Cov_ = c;
168  bool defaultcov = false;
169 //#ifdef BIAS_DEBUG
170  for (unsigned int x = 0; x < 7; x++)
171  {
172  for (unsigned int y = 0; y < 7; y++)
173  {
174  if (BIAS_ISNAN(Cov_[y][x]) || BIAS_ISINF(Cov_[y][x]))
175  {
176  defaultcov = true;
177  BIASERR("Invalid covariance matrix set in pose: " << Cov_);
178  //BIASABORT;
179  }
180  }
181  }
182 //#endif
183  if (defaultcov)
184  {
185  Cov_.SetIdentity();
186  Cov_ *= 1e-10;
187  }
188  }
189  return 0;
190 }
191 
194 {
196  res.SetOrientation(Q_);
197  res.SetPosition(C_);
198  res.SetCovarianceMatrix(Cov_);
199  return res;
200 }
201 
202 bool
203 BIAS::Pose::Load(const std::string& inputFile)
204 {
205  ifstream ifs(inputFile.c_str());
206  if (!ifs)
207  {
208  return false;
209  }
210  ifs >> *this;
211  if (!ifs)
212  {
213  ifs.close();
214  return false;
215  }
216  ifs.close();
217  UpdateMatrix_();
218  return true;
219 }
220 
221 bool
222 BIAS::Pose::Save(const std::string& outputFile)
223 {
224  ofstream ofs(outputFile.c_str());
225  if (!ofs)
226  {
227  return false;
228  }
229  ofs << *this;
230  if (!ofs)
231  {
232  ofs.close();
233  return false;
234  }
235  ofs.close();
236  return true;
237 }
238 
239 #ifdef BIAS_HAVE_XML2
240 
241 int
242 BIAS::Pose::XMLGetClassName(std::string& TopLevelTag, double& Version) const
243 {
244  TopLevelTag = "Pose";
245  Version = 0.2;
246  return 0;
247 }
248 
249 int
250 BIAS::Pose::XMLOut(const xmlNodePtr Node, BIAS::XMLIO& XMLObject) const
251 {
252  // Write rotation quaternion
253  {
254  std::ostringstream values;
255  xmlNodePtr childNode = XMLObject.addChildNode(Node, "Rotation");
256  values << std::setprecision(POSE_STREAM_PRECISION);
257  for (unsigned int i = 0; i < 4; i++)
258  values << (double) Q_.GetData()[i] << " ";
259  XMLObject.addContent(childNode, values.str());
260  }
261 
262  // Write position vector
263  {
264  std::ostringstream values;
265  xmlNodePtr childNode = XMLObject.addChildNode(Node, "Position");
266  values << std::setprecision(POSE_STREAM_PRECISION);
267  for (unsigned int i = 0; i < 3; i++)
268  values << (double) C_.GetData()[i] << " ";
269  XMLObject.addContent(childNode, values.str());
270  }
271 
272  // Write covariance matrix
273  if (!Cov_.IsZero())
274  {
275  std::ostringstream values;
276  xmlNodePtr childNode = XMLObject.addChildNode(Node, "Covariance");
277  values << std::setprecision(POSE_STREAM_PRECISION);
278  for (unsigned int i = 0; i < 49; i++)
279  values << (double) Cov_.GetData()[i] << " ";
280  XMLObject.addContent(childNode, values.str());
281  }
282 
283  return 0;
284 }
285 
286 int
287 BIAS::Pose::XMLIn(const xmlNodePtr Node, BIAS::XMLIO& XMLObject)
288 {
289  xmlAttrPtr attr;
290  attr = XMLObject.getAttributeByName(Node, "Position");
291  if (attr) {
292  std::istringstream values(XMLObject.getAttributeValueString(attr));
293  for (unsigned int i = 0; i < 3; i++) values >> C_.GetData()[i];
294  } else {
295  C_.SetZero();
296  }
297  attr = XMLObject.getAttributeByName(Node, "Rotation");
298  if (attr) {
299  std::istringstream values(XMLObject.getAttributeValueString(attr));
300  for (unsigned int i = 0; i < 4; i++) values >> Q_.GetData()[i];
301  } else {
302  Q_.SetIdentity();
303  }
304  attr = XMLObject.getAttributeByName(Node, "Covariance");
305  if (attr) {
306  std::istringstream values(XMLObject.getAttributeValueString(attr));
307  for (unsigned int i = 0; i < 49; i++) values >> Cov_.GetData()[i];
308  } else {
309  Cov_.SetZero();
310  }
311 
312  UpdateMatrix_();
313 
314  return 0;
315 }
316 
317 #endif
318 
319 void
321 {
324  CoordinateTransform3D intermediate;
325  trsf.ConcatenateLocalTransform((*this), intermediate);
326  this->Set(intermediate.GetQ(), intermediate.GetC());
327  Cov_ = ppc;
328 }
329 
332 {
333  RMatrixBase R = GetR();
334  return Vector3<double> (R[0][2], R[1][2], R[2][2]);
335 }
336 
339 {
340  RMatrixBase R = GetR();
341  return Vector3<double> (R[0][0], R[1][0], R[2][0]);
342 }
343 
346 {
347  RMatrixBase R = GetR();
348  return Vector3<double> (R[0][1], R[1][1], R[2][1]);
349 }
virtual int XMLOut(const xmlNodePtr Node, BIAS::XMLIO &XMLObject) const
Specialization of XML write function.
Definition: Pose.cpp:250
int Set(const PoseParametrization &pose)
Set pose from pose parametrization.
Definition: Pose.cpp:140
virtual int XMLGetClassName(std::string &TopLevelTag, double &Version) const
Specialization of XML block name function.
Definition: Pose.cpp:242
virtual void Set(const Quaternion< double > &Q, const Vector3< double > &C, const double &scale=1.0)
Define local coordinate frame from within the world frame.
void SetCovarianceMatrix(const Matrix< PP_TYPE > &Cov)
Sets covariance matrix from Cov.
bool Save(const std::string &outputFile)
Write pose transformation to a text file using operator &lt;&lt;.
Definition: Pose.cpp:222
bool Load(const std::string &inputFile)
Load pose transformation from a text file using operator &gt;&gt;.
Definition: Pose.cpp:203
void SetC(const Vector3< POSE_TYPE > &C, const Matrix3x3< POSE_TYPE > &cov)
Set translation (resp.
Definition: Pose.cpp:90
Matrix4x4< double > GetLocalToGlobalTransform() const
Get 4x4 matrix which transforms points in local coordinate system to homogeneous points in global coo...
const Vector3< double > & GetC() const
Set origin of local coordinate system in global coordinates.
Vector3< double > GetZAxis() const
Definition: Pose.cpp:331
const Quaternion< double > & GetQ() const
Get orientation of local coordinate system as unit quaternion mapping from local coordinates to globa...
Vector3< double > GetXAxis() const
Definition: Pose.cpp:338
void SetQ(const Quaternion< double > &Q)
Set orientation of local coordinate system from unit quaternion.
Slim class bundeling pose parametrization and associated covariance matrix.
void SetQ(const Quaternion< POSE_TYPE > &Q, const Matrix4x4< POSE_TYPE > &cov)
Set rotation (resp.
Definition: Pose.cpp:103
int SetCovEuclidean(Matrix< POSE_TYPE > Cov)
Set covariance matrix explicitely from an Euclidean covariance matrix, i.e.
Definition: Pose.cpp:123
std::string getAttributeValueString(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:716
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
const Matrix< PP_TYPE > & GetCovarianceMatrix() const
returns the covariance matrix associated with the CQ vector
unsigned int GetRows() const
Definition: Matrix.hh:202
Pose()
Default constructor creates identity pose transformation.
Definition: Pose.cpp:36
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131
Matrix3x3< POSE_TYPE > GetCovC() const
Return the 3x3 covariance matrix Cov(C) of the translation part C of this pose transformation.
Definition: Pose.cpp:48
void SetPosition(const Vector3< PP_TYPE > &C)
set position part of vector and zero covariance matrix
int SetCovariance(const Matrix< POSE_TYPE > &c)
Set covariance matrix explicitely from 7x7 matrix Cov([C Q]).
Definition: Pose.cpp:157
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
void TransformToNewGlobalBy(const CoordinateTransform3D &trsf)
Transforms the position and orientation stored in this pose into another global coordinate system...
Definition: Pose.cpp:320
PoseParametrization GetPoseParameters() const
Get pose parametrization representing this pose.
Definition: Pose.cpp:193
virtual int XMLIn(const xmlNodePtr Node, BIAS::XMLIO &XMLObject)
Specialization of XML read function.
Definition: Pose.cpp:287
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
void SetC(const Vector3< double > &C)
Set origin of local coordinate system in global coordinates.
Vector3< PP_TYPE > GetPosition() const
returns the position (first 3 entries) part of the pose parametrization vector
xmlAttrPtr getAttributeByName(const xmlNodePtr Node, const std::string &attribute_name)
search for a specific attribute
Definition: XMLIO.cpp:650
unsigned int GetCols() const
Definition: Matrix.hh:204
void SetOrientation(const Quaternion< PP_TYPE > &q)
set orientation part of vector and zero covariance matrix
~Pose()
Definition: Pose.cpp:42
Vector3< double > GetYAxis() const
Definition: Pose.cpp:345
Transforms 3d points between two different coodinate systems, e.g.
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
class encapsulating the transfromations between different pose parametrizations
void ConcatenateLocalTransform(const CoordinateTransform3D &localT, CoordinateTransform3D &res) const
Calculates transformation from global system of this instance to the local coordinate system of the g...
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
Matrix4x4< POSE_TYPE > GetCovQ() const
Return the 4x4 covariance matrix Cov(Q) of the rotational part of this pose transformation described ...
Definition: Pose.cpp:62
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