rvt_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, PickNik Consulting
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 PickNik Consulting 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 /* Author: Dave Coleman
36  Desc: Test for Rviz Visual tools
37 */
38 
39 // C++
40 #include <string>
41 #include <vector>
42 
43 #include <gtest/gtest.h>
44 #include <ros/ros.h>
45 
46 // For visualizing things in rviz
48 
49 class RVTTest
50 {
51 public:
52  bool initialize()
53  {
54  visual_tools_.reset(new rviz_visual_tools::RvizVisualTools("base", "/rviz_visual_tools"));
55 
56  // Allow time to publish messages
57  ROS_INFO_STREAM_NAMED("test", "Waiting 4 seconds to start test...");
58  return true;
59  }
60 
61  bool testIsometry3d(const std::string& id, const Eigen::Isometry3d& expect, const Eigen::Isometry3d& actual)
62  {
63  static const double EPSILON = 0.000001;
64  EXPECT_GT(EPSILON, fabs(expect.translation().x() - actual.translation().x()))
65  << id << " Translation x - expect: " << expect.translation().x() << " actual: " << actual.translation().x();
66  EXPECT_GT(EPSILON, fabs(expect.translation().y() - actual.translation().y()))
67  << id << " Translation y - expect: " << expect.translation().y() << " actual: " << actual.translation().y();
68  EXPECT_GT(EPSILON, fabs(expect.translation().z() - actual.translation().z()))
69  << id << " Translation z - expect: " << expect.translation().z() << " actual: " << actual.translation().z();
70 
71  Eigen::Quaterniond q1(expect.rotation());
72  Eigen::Quaterniond q2(actual.rotation());
73  EXPECT_GT(EPSILON, fabs(q1.x() - q2.x())) << id << " Quaternion x - expect: " << q1.x() << " actual: " << q2.x();
74  EXPECT_GT(EPSILON, fabs(q1.y() - q2.y())) << id << " Quaternion y - expect: " << q1.y() << " actual: " << q2.y();
75  EXPECT_GT(EPSILON, fabs(q1.z() - q2.z())) << id << " Quaternion z - expect: " << q1.z() << " actual: " << q2.z();
76  EXPECT_GT(EPSILON, fabs(q1.w() - q2.w())) << id << " Quaternion w - expect: " << q1.w() << " actual: " << q2.w();
77 
78  return true;
79  }
80 
81  bool testVector(const std::string& id, const std::vector<double>& expect, const std::vector<double>& actual)
82  {
83  EXPECT_EQ(expect.size(), actual.size()) << id << " Unequal vector sizes";
84 
85  static const double EPSILON = 0.000001;
86  for (std::size_t i = 0; i < expect.size(); ++i)
87  {
88  EXPECT_GT(EPSILON, fabs(expect[i] - actual[i]))
89  << "Section " << id << ", Element " << i << ", Expect: " << expect[i] << ", Actual: " << actual[i];
90  }
91 
92  return true;
93  }
94 
95  // A shared node handle
96  // ros::NodeHandle nh_;
97 
98  // For visualizing things in rviz
100 
101 }; // class
102 
103 /* Create instance of test class ---------------------------------------------------------- */
105 
106 /* Run tests ------------------------------------------------------------------------------ */
107 TEST(RVTTest, initialize)
108 {
109  ASSERT_TRUE(BASE.initialize());
110 }
111 
112 // Test rpy conversion
113 TEST(RVTTest, test_rpy_conversions)
114 {
115  // Identity conversions with RPY
116  Eigen::Isometry3d expected_affine = Eigen::Isometry3d::Identity();
117  std::vector<double> xyzrpy;
118  BASE.visual_tools_->convertToXYZRPY(expected_affine, xyzrpy);
119  std::vector<double> expected_vector;
120  expected_vector.push_back(0);
121  expected_vector.push_back(0);
122  expected_vector.push_back(0);
123  expected_vector.push_back(0);
124  expected_vector.push_back(0);
125  expected_vector.push_back(0);
126  EXPECT_TRUE(BASE.testVector("Identity: ", expected_vector, xyzrpy));
127 
128  // Identity conversion back to Eigen
129  Eigen::Isometry3d expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy, rviz_visual_tools::XYZ);
130  EXPECT_TRUE(BASE.testIsometry3d("Identity convert back", expected_affine, expected_affine2));
131 
132  // -------------------------------------------------------------------
133  // Translation conversions to RPY
134  expected_affine.translation().x() = 1;
135  expected_affine.translation().y() = 2;
136  expected_affine.translation().z() = 3;
137  BASE.visual_tools_->convertToXYZRPY(expected_affine, xyzrpy);
138  expected_vector[0] = 1;
139  expected_vector[1] = 2;
140  expected_vector[2] = 3;
141  EXPECT_TRUE(BASE.testVector("123: ", expected_vector, xyzrpy));
142 
143  // Translation convertion back to Eigen
144  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy, rviz_visual_tools::XYZ);
145  EXPECT_TRUE(BASE.testIsometry3d("123 convert back", expected_affine, expected_affine2));
146 
147  // Translation convertion back to Eigen via long function
148  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy[0], xyzrpy[1], xyzrpy[2], xyzrpy[3], xyzrpy[4],
149  xyzrpy[5], rviz_visual_tools::XYZ);
150  EXPECT_TRUE(BASE.testIsometry3d("123 convert back long", expected_affine, expected_affine2));
151 
152  // Translation convertion back to Eigen via NEW long function
153  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy[0], xyzrpy[1], xyzrpy[2], xyzrpy[3], xyzrpy[4],
154  xyzrpy[5], rviz_visual_tools::XYZ);
155  EXPECT_TRUE(BASE.testIsometry3d("123 convert back new long", expected_affine, expected_affine2));
156 
157  // -------------------------------------------------------------------
158  // Rotation conversions to RPY
159  expected_affine = expected_affine * Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX()) *
160  Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitZ()) *
161  Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX());
162  BASE.visual_tools_->convertToXYZRPY(expected_affine, xyzrpy);
163 
164  // Rotation convertion back to Eigen
165  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy, rviz_visual_tools::XYZ);
166  EXPECT_TRUE(BASE.testIsometry3d("123 convert back", expected_affine, expected_affine2));
167 
168  // Rotation convertion back to Eigen via long function
169  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy[0], xyzrpy[1], xyzrpy[2], xyzrpy[3], xyzrpy[4],
170  xyzrpy[5], rviz_visual_tools::XYZ);
171  EXPECT_TRUE(BASE.testIsometry3d("123 convert back long", expected_affine, expected_affine2));
172 
173  // Rotation convertion back to Eigen via NEW long function
174  expected_affine2 = BASE.visual_tools_->convertFromXYZRPY(xyzrpy[0], xyzrpy[1], xyzrpy[2], xyzrpy[3], xyzrpy[4],
175  xyzrpy[5], rviz_visual_tools::XYZ);
176  EXPECT_TRUE(BASE.testIsometry3d("123 convert back new long", expected_affine, expected_affine2));
177 }
178 
179 TEST(RVTTest, default_arguments)
180 {
181  // Check for correct number of parameters and correct size of path
182  std::vector<geometry_msgs::Point> path1;
183  EXPECT_FALSE(BASE.visual_tools_->publishPath(path1, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
184  path1.resize(1);
185  EXPECT_FALSE(BASE.visual_tools_->publishPath(path1, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
186  path1.resize(2);
187  EXPECT_TRUE(BASE.visual_tools_->publishPath(path1, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
188 
190  EXPECT_FALSE(BASE.visual_tools_->publishPath(path2, rviz_visual_tools::GREEN, rviz_visual_tools::MEDIUM));
191  path2.resize(1);
192  EXPECT_FALSE(BASE.visual_tools_->publishPath(path2, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
193  path2.resize(2);
194  EXPECT_TRUE(BASE.visual_tools_->publishPath(path2, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
195 
197  EXPECT_FALSE(BASE.visual_tools_->publishPath(path3, rviz_visual_tools::BLUE, rviz_visual_tools::MEDIUM));
198  path3.resize(1);
199  EXPECT_FALSE(BASE.visual_tools_->publishPath(path3, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
200  path3.resize(2);
201  EXPECT_TRUE(BASE.visual_tools_->publishPath(path3, rviz_visual_tools::RED, rviz_visual_tools::MEDIUM));
202 }
203 
204 TEST(RVTTest, get_vector_between_points)
205 {
206  using namespace Eigen;
207  const auto x_actual = BASE.visual_tools_->getVectorBetweenPoints(Vector3d::UnitX(), Vector3d::Zero());
208  const auto x_expected =
209  Isometry3d(Isometry3d::Identity()).translate(Vector3d::UnitX()).rotate(AngleAxisd(M_PI, Vector3d::UnitY()));
210  EXPECT_TRUE(BASE.testIsometry3d("get_vector_between_points X", x_expected, x_actual));
211 
212  const auto x2_actual = BASE.visual_tools_->getVectorBetweenPoints(2 * Vector3d::UnitX(), Vector3d::Zero());
213  const auto x2_expected =
214  Isometry3d(Isometry3d::Identity()).translate(2 * Vector3d::UnitX()).rotate(AngleAxisd(M_PI, Vector3d::UnitY()));
215  EXPECT_TRUE(BASE.testIsometry3d("get_vector_between_points 2X", x2_expected, x2_actual));
216 
217  const auto random_actual = BASE.visual_tools_->getVectorBetweenPoints({ 2.0, 3.0, 4.0 }, { 5.0, 6.0, 7.0 });
218  const auto random_expected = []() {
219  Eigen::Matrix4d d;
220  d << 0.57735, -0.211325, -0.788675, 2, 0.57735, 0.788675, 0.211325, 3, 0.57735, -0.57735, 0.57735, 4, 0, 0, 0, 1;
221  return Eigen::Isometry3d(d);
222  }();
223  EXPECT_TRUE(BASE.testIsometry3d("get_vector_between_points random", random_expected, random_actual));
224 }
225 
226 /* Main ------------------------------------------------------------------------------------- */
227 int main(int argc, char** argv)
228 {
229  testing::InitGoogleTest(&argc, argv);
230  ros::init(argc, argv, "rviz_visual_tools_tests");
231  return RUN_ALL_TESTS();
232 }
233 
234 /*
235 reminders:
236 EXPECT_FALSE(robot_state.hasFixedLinks());
237 EXPECT_EQ(robot_state.getFixedLinksCount(), 0);
238 EXPECT_TRUE(robot_state.getPrimaryFixedLink() == NULL);
239 EXPECT_GT(robot_state.getFixedLinksMode(), 0);
240 EXPECT_LT( fabs(vars[0] - 0), EPSILON) << "Virtual joint in wrong position " << vars[0];
241 */
Eigen
RVTTest::initialize
bool initialize()
Definition: rvt_test.cpp:84
EigenSTL::vector_Isometry3d
std::vector< Eigen::Isometry3d, Eigen::aligned_allocator< Eigen::Isometry3d > > vector_Isometry3d
RVTTest
Definition: rvt_test.cpp:49
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
main
int main(int argc, char **argv)
Definition: rvt_test.cpp:227
rviz_visual_tools::RvizVisualTools
Definition: rviz_visual_tools.h:184
rviz_visual_tools::RvizVisualToolsPtr
std::shared_ptr< RvizVisualTools > RvizVisualToolsPtr
Definition: rviz_visual_tools.h:1151
RVTTest::testIsometry3d
bool testIsometry3d(const std::string &id, const Eigen::Isometry3d &expect, const Eigen::Isometry3d &actual)
Definition: rvt_test.cpp:93
EigenSTL::vector_Vector3d
std::vector< Eigen::Vector3d, Eigen::aligned_allocator< Eigen::Vector3d > > vector_Vector3d
d
d
rviz_visual_tools.h
rviz_visual_tools::RED
@ RED
Definition: rviz_visual_tools.h:104
rviz_visual_tools::GREEN
@ GREEN
Definition: rviz_visual_tools.h:99
TEST
TEST(RVTTest, initialize)
Definition: rvt_test.cpp:107
rviz_visual_tools::MEDIUM
@ MEDIUM
Definition: rviz_visual_tools.h:123
RVTTest::visual_tools_
rviz_visual_tools::RvizVisualToolsPtr visual_tools_
Definition: rvt_test.cpp:131
ROS_INFO_STREAM_NAMED
#define ROS_INFO_STREAM_NAMED(name, args)
rviz_visual_tools::BLUE
@ BLUE
Definition: rviz_visual_tools.h:95
BASE
RVTTest BASE
Definition: rvt_test.cpp:104
RVTTest::testVector
bool testVector(const std::string &id, const std::vector< double > &expect, const std::vector< double > &actual)
Definition: rvt_test.cpp:113
rviz_visual_tools::XYZ
@ XYZ
Definition: rviz_visual_tools.h:133


rviz_visual_tools
Author(s): Dave Coleman
autogenerated on Wed Mar 2 2022 01:03:26