CovarianceVisual.cpp
Go to the documentation of this file.
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013-2015, Timm Linder, Social Robotics Lab, University of Freiburg
5 * Copyright (c) 2006, Kai O. Arras, Autonomous Intelligent Systems Lab, University of Freiburg
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * * Neither the name of the copyright holder nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
33 #include <ros/console.h>
34 
35 namespace tuw_object_rviz
36 {
37 
38  CovarianceVisual::CovarianceVisual(Ogre::SceneManager* sceneManager, Ogre::SceneNode* parentNode) : m_sceneManager(sceneManager)
39  {
40  m_sceneNode = parentNode->createChildSceneNode();
41  }
42 
44  {
45  m_sceneManager->destroySceneNode(m_sceneNode->getName());
46  };
47 
48  void CovarianceVisual::setPosition(const Ogre::Vector3& position)
49  {
50  m_sceneNode->setPosition(position);
51  }
52 
53  void CovarianceVisual::setOrientation(const Ogre::Quaternion& orientation)
54  {
55  m_sceneNode->setOrientation(orientation);
56  }
57 
58  void CovarianceVisual::setVisible(bool visible)
59  {
60  m_sceneNode->setVisible(visible, true);
61  }
62 
63  ProbabilityEllipseCovarianceVisual::ProbabilityEllipseCovarianceVisual(Ogre::SceneManager* sceneManager, Ogre::SceneNode* parentNode)
64  : CovarianceVisual(sceneManager, parentNode)
65  {
67  }
68 
70  {
71  delete m_line;
72  }
73 
75  {
76  m_line->setLineWidth(lineWidth);
77  }
78 
79  void ProbabilityEllipseCovarianceVisual::setColor(const Ogre::ColourValue& c)
80  {
81  m_line->setColor(c.r, c.g, c.b, c.a);
82  }
83 
84  void ProbabilityEllipseCovarianceVisual::setMeanCovariance(const Ogre::Vector3& mean, const Ogre::Matrix3& cov)
85  {
86  int numberOfPoints;
87  double* xs, *ys;
88  double determinant = cov[0][0] * cov[1][1] - cov[1][0] * cov[0][1];
89 
90  m_line->clear();
91 
92  if (!std::isfinite(determinant))
93  {
95  5.0, "Covariance matrix has non-finite values in ProbabilityEllipseCovarianceVisual::setMeanCovariance(): "
96  << cov);
97  return;
98  }
99 
100  if (std::abs(cov[0][1] - cov[1][0]) > 0.00001)
101  {
103  5.0,
104  "Covariance matrix is not symmetric in ProbabilityEllipseCovarianceVisual::setMeanCovariance(): " << cov);
105  return;
106  }
107 
108  if ((determinant > 0 && cov[0][0] > 0) /* positive definite? */ || (std::abs(determinant - 0.00001) == 0.0 && (cov[0][0] > 0 || cov[1][1] > 0)) /* positive semidefinite? */)
109  {
110  calc_prob_elli_99(mean.x, mean.y, cov[0][0], cov[1][1], cov[0][1], numberOfPoints, xs, ys);
111 
112  m_line->setMaxPointsPerLine(numberOfPoints);
113 
114  for (int i = 0; i < numberOfPoints; i++)
115  {
116  Ogre::Vector3 vertex(xs[i], ys[i], mean.z);
117  m_line->addPoint(vertex);
118  }
119  }
120  else
121  {
122  ROS_WARN_STREAM_THROTTLE(5.0, "Covariance matrix is not positive (semi-)definite in "
123  "ProbabilityEllipseCovarianceVisual::setMeanCovariance(): "
124  << cov);
125  }
126  }
127 
128  // Puts angle alpha into the interval [min..min+2*pi[
130  {
131  while (alpha >= min + 2.0 * M_PI)
132  {
133  alpha -= 2.0 * M_PI;
134  }
135  while (alpha < min)
136  {
137  alpha += 2.0 * M_PI;
138  }
139  return alpha;
140  }
141 
142  // Calculates the points on a rotated ellipse given by center xc, yc, half axes a, b and angle phi.
143  // Returns number of points np and points in Cart. coordinates
144  void ProbabilityEllipseCovarianceVisual::calc_ellipse(double xc, double yc, double a, double b, double phi, int& np, double*& xvec, double*& yvec)
145  {
146  const int N_ELLI_POINTS = 40;
147  int i, offset;
148  double t, cr, sr, ca, sa, xi, yi, reso;
149  static double x[N_ELLI_POINTS + 1];
150  static double y[N_ELLI_POINTS + 1];
151  reso = 2 * M_PI / N_ELLI_POINTS;
152  offset = N_ELLI_POINTS / 2;
153  ca = cos(phi);
154  sa = sin(phi);
155  i = 0;
156  t = 0;
157  while (t < M_PI)
158  {
159  cr = cos(t);
160  sr = sin(t);
161  xi = a * cr * ca - b * sr * sa;
162  yi = a * cr * sa + b * sr * ca;
163  x[i] = xi + xc;
164  y[i] = yi + yc;
165  x[offset + i] = -xi + xc;
166  y[offset + i] = -yi + yc;
167  t = t + reso;
168  i++;
169  }
170  x[N_ELLI_POINTS] = x[0]; // Close contour
171  y[N_ELLI_POINTS] = y[0]; // Close contour
172  np = N_ELLI_POINTS + 1;
173  xvec = x;
174  yvec = y;
175  }
176 
177  // Calculates the points on a 95%-iso-probability ellipse given by the bivarate RV with mean xc, yc
178  // and covariance matrix sxx, syy, sxy. Returns number of points np and points in Cart. coordinates
179  void ProbabilityEllipseCovarianceVisual::calc_prob_elli_95(double xc, double yc, double sxx, double syy, double sxy, int& np, double*& x, double*& y)
180  {
181  double la, lb, a, b, phi;
182  la = (sxx + syy + sqrt((sxx - syy) * (sxx - syy) + 4 * sxy * sxy)) / 2;
183  lb = (sxx + syy - sqrt((sxx - syy) * (sxx - syy) + 4 * sxy * sxy)) / 2;
184  a = sqrt(5.991464 * la);
185  b = sqrt(5.991464 * lb);
186  phi = set_angle_to_range(atan2(2 * sxy, sxx - syy) / 2, 0);
187  calc_ellipse(xc, yc, a, b, phi, np, x, y);
188  }
189 
190  // Calculates the points on a 99%-iso-probability ellipse given by the bivarate RV with mean xc, yc
191  // and covariance matrix sxx, syy, sxy. Returns number of points np and points in Cart. coordinates
192  void ProbabilityEllipseCovarianceVisual::calc_prob_elli_99(double xc, double yc, double sxx, double syy, double sxy, int& np, double*& x, double*& y)
193  {
194  double la, lb, a, b, phi;
195  la = (sxx + syy + sqrt((sxx - syy) * (sxx - syy) + 4 * sxy * sxy)) / 2;
196  lb = (sxx + syy - sqrt((sxx - syy) * (sxx - syy) + 4 * sxy * sxy)) / 2;
197  a = sqrt(9.210340 * la);
198  b = sqrt(9.210340 * lb);
199  phi = set_angle_to_range(atan2(2 * sxy, sxx - syy) / 2, 0);
200  calc_ellipse(xc, yc, a, b, phi, np, x, y);
201  }
202 
203 };
virtual void setColor(const Ogre::ColourValue &c)
void addPoint(const Ogre::Vector3 &point)
void calc_prob_elli_95(double xc, double yc, double sxx, double syy, double sxy, int &np, double *&x, double *&y)
virtual void setMeanCovariance(const Ogre::Vector3 &mean, const Ogre::Matrix3 &cov)
NOTE: It is assumed that the covariance matrix is already rotated into the target frame of the sceneN...
TFSIMD_FORCE_INLINE const tfScalar & y() const
void setPosition(const Ogre::Vector3 &position)
ProbabilityEllipseCovarianceVisual(Ogre::SceneManager *sceneManager, Ogre::SceneNode *parentNode)
virtual void setColor(float r, float g, float b, float a)
CovarianceVisual(Ogre::SceneManager *sceneManager, Ogre::SceneNode *parentNode)
void calc_ellipse(double xc, double yc, double a, double b, double phi, int &np, double *&xvec, double *&yvec)
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setOrientation(const Ogre::Quaternion &orientation)
void setMaxPointsPerLine(uint32_t max)
#define ROS_WARN_STREAM_THROTTLE(rate, args)
Ogre::SceneManager * m_sceneManager
void calc_prob_elli_99(double xc, double yc, double sxx, double syy, double sxy, int &np, double *&x, double *&y)
void setLineWidth(float width)


tuw_object_rviz
Author(s): Florian Beck
autogenerated on Mon Jun 10 2019 15:40:17