BayesVisualization.cpp
Go to the documentation of this file.
1 
19 
20 namespace Visualization {
21 
23 
24  ros::NodeHandle n("~");
25 
26  // Init publisher for visualization markers
27  gaussianPublisher = n.advertise<visualization_msgs::Marker>("gaussian", 0);
28  samplePublisher = n.advertise<visualization_msgs::Marker>("samples", 0);
29  eigenPublisher = n.advertise<visualization_msgs::Marker>("eigenvectors", 0);
30  }
31 
33 
34  void BayesVisualization::drawSamples(std::vector<Eigen::Vector3d> samples) {
35  for(unsigned int i = 0; i < samples.size(); i++) {
36  visualization_msgs::Marker m;
37 
38  m.header.frame_id = "/map";
39  m.header.stamp = ros::Time::now();
40  m.ns = "samples";
41  m.id = 100 + i;
42 
43  m.type = visualization_msgs::Marker::SPHERE;
44  m.action = visualization_msgs::Marker::ADD;
45 
46  m.pose.position.x = samples[i][0];
47  m.pose.position.y = samples[i][1];
48  m.pose.position.z = samples[i][2];
49 
50  m.pose.orientation.x = 1;
51  m.pose.orientation.y = 1;
52  m.pose.orientation.z = 1;
53  m.pose.orientation.w = 1;
54 
55  m.scale.x = 0.05;
56  m.scale.y = 0.05;
57  m.scale.z = 0.05;
58 
59  m.color.a = 1.0;
60  m.color.r = 0.0;
61  m.color.g = 0.0;
62  m.color.b = 0.0;
63 
65  }
66  }
67 
68  void BayesVisualization::drawDistribution(Eigen::Vector3d mean , Eigen::Matrix3d covariance) {
69 
70  // Calculates eigen vectors of covariance matrix.
71  // Use eigenvalues are required for the scale of the sphere,
72  // the eigenvectors form a rotation matrix, that will be converted into a quaternion;
73  // the latter one is required for rotating the sphere.
74  Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es(covariance);
75  Eigen::Quaterniond q(es.eigenvectors());
76 
77  // visualize eigen vectors
78  for(int i = 0; i < 3; i++) {
79  visualization_msgs::Marker e = createArrowMarker(mean, mean + es.eigenvectors().col(i) * es.eigenvalues()[i]);
80 
81  e.id = i;
82  e.ns = "eigenvectors";
83  e.color.r = (i == 0) ? 1.0 : 0.0;
84  e.color.g = (i - 1 == 0) ? 1.0 : 0.0;
85  e.color.b = (i - 2 == 0) ? 1.0 : 0.0;
86  e.color.a = 1.0;
87 
89  }
90 
91  // TODO remove
92  /* std::cout << "--------------------" << std::endl;
93  std::cout << "Mean: " << std::endl << mean << std::endl;
94  std::cout << "Covariance: " << std::endl << covariance << std::endl;
95  std::cout << "Eigen values: " << std::endl << es.eigenvalues() << std::endl;
96  std::cout << "Eigen vectors: " << std::endl << es.eigenvectors() << std::endl;
97  std::cout << "Quaternion: " << q.x() << " " << q.y() << " " << q.z() << " " << q.w() << std::endl;
98  std::cout << "X: " << es.eigenvectors().col(0) << std::endl;
99  std::cout << "Y: " << es.eigenvectors().col(1) << std::endl;
100  std::cout << "Z: " << es.eigenvectors().col(2) << std::endl;*/
101 
102  // Visualize distribution
103  visualization_msgs::Marker m;
104 
105  m.header.frame_id ="/map";
106  m.header.stamp = ros::Time::now();
107  m.ns = "gaussian_distribution";
108  m.id = ID_GAUSSIAN;
109 
110  m.type = visualization_msgs::Marker::SPHERE;
111  m.action = visualization_msgs::Marker::ADD;
112 
113  // add a small offset to prevent the disappearing
114  // of the sphere when an eigen value is zero
115  m.scale.x = (std::abs(es.eigenvalues()[0]) * 2) + 0.1;
116  m.scale.y = (std::abs(es.eigenvalues()[1]) * 2) + 0.1;
117  m.scale.z = (std::abs(es.eigenvalues()[2]) * 2) + 0.1;
118 
119  m.pose.position.x = mean[0];
120  m.pose.position.y = mean[1];
121  m.pose.position.z = mean[2];
122 
123  m.pose.orientation.w = q.w();
124  m.pose.orientation.x = q.x();
125  m.pose.orientation.y = q.y();
126  m.pose.orientation.z = q.z();
127 
128  m.color.r = 0.0;
129  m.color.g = 1.0;
130  m.color.b = 0.0;
131  m.color.a = 0.3;
132 
134  }
135 
136  void BayesVisualization::drawDistribution(std::vector<Eigen::Vector3d> samples) {
137  Eigen::Vector3d mean(0, 0, 0);
138  Eigen::Matrix3d covariance = Eigen::Matrix3d::Zero();
139 
140  // calculate mean
141  for(unsigned int i = 0; i < samples.size(); i++) {
142  mean += samples[i];
143  }
144  mean /= samples.size();
145 
146  // calculate covariance matrix
147  for(unsigned int i = 0; i < samples.size(); i++) {
148  covariance += (samples[i] - mean) * (samples[i].adjoint() - mean.adjoint());
149  }
150  covariance /= (samples.size());
151 
152  // draw samples with the related distribution
153  drawDistribution(mean, covariance);
154  drawSamples(samples);
155  }
156 
157  visualization_msgs::Marker BayesVisualization::createArrowMarker(const Eigen::Vector3d a, const Eigen::Vector3d b) {
158  visualization_msgs::Marker m;
159 
160  m.header.stamp = ros::Time::now();
161  m.header.frame_id = "/map";
162 
163  m.scale.x = 0.05;
164  m.scale.y = 0.05;
165  m.scale.z = 0.05;
166 
167  m.type = visualization_msgs::Marker::ARROW;
168  m.action = visualization_msgs::Marker::ADD;
169 
170  m.points.push_back(createPointMessage(a));
171  m.points.push_back(createPointMessage(b));
172 
173  return m;
174  }
175 
176  geometry_msgs::Point BayesVisualization::createPointMessage(Eigen::Vector3d point) {
177  geometry_msgs::Point p;
178 
179  p.x = point[0];
180  p.y = point[1];
181  p.z = point[2];
182 
183  return p;
184  }
185 
186 }
187 
void publish(const boost::shared_ptr< M > &message) const
geometry_msgs::Point createPointMessage(Eigen::Vector3d point)
void drawSamples(std::vector< Eigen::Vector3d > samples)
visualization_msgs::Marker createArrowMarker(const Eigen::Vector3d a, const Eigen::Vector3d b)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static Time now()
static const int ID_GAUSSIAN
void drawDistribution(Eigen::Vector3d mean, Eigen::Matrix3d covariance)


asr_psm_visualizations
Author(s): Gehrung Joachim, Meißner Pascal
autogenerated on Sat Nov 9 2019 03:49:12