Skip to content

Commit 3061a2c

Browse files
committed
add geometry utility test code
1 parent 987a847 commit 3061a2c

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

perception/image_projection_based_fusion/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ if(BUILD_TESTING)
165165
ament_auto_add_gtest(test_utils
166166
test/test_utils.cpp
167167
)
168+
ament_auto_add_gtest(test_geometry
169+
test/test_geometry.cpp
170+
)
168171
endif()
169172

170173
ament_auto_package(INSTALL_TO_SHARE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright 2024 TIER IV, inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "image_projection_based_fusion/utils/geometry.hpp"
16+
17+
#include <glog/logging.h>
18+
#include <gtest/gtest.h>
19+
20+
TEST(objectToVertices,
21+
test_objectToVertices)
22+
{
23+
// Test `boundingBoxToVertices()` and `cylinderToVertices()` simultaneously
24+
// Test at Shape::BOUNDING_BOX
25+
geometry_msgs::msg::Pose pose;
26+
pose.position.x = 1.0;
27+
pose.position.y = 2.0;
28+
pose.position.z = 3.0;
29+
const double q_scalar = std::sin(M_PI / 12);
30+
pose.orientation.x = 0.0;
31+
pose.orientation.y = 0.0;
32+
pose.orientation.z = q_scalar;
33+
pose.orientation.w = std::cos(M_PI / 12);
34+
autoware_auto_perception_msgs::msg::Shape shape;
35+
shape.type = 0;
36+
shape.dimensions.x = 4.0;
37+
shape.dimensions.y = 6.0;
38+
shape.dimensions.z = 8.0;
39+
std::vector<Eigen::Vector3d> vertices;
40+
41+
image_projection_based_fusion::objectToVertices(pose, shape, vertices);
42+
43+
EXPECT_FALSE(vertices.empty());
44+
EXPECT_NEAR(vertices.at(0).x(), 1.2320508075688772935274, 1e-6);
45+
EXPECT_NEAR(vertices.at(0).y(), 5.598076211353315940291, 1e-6);
46+
EXPECT_NEAR(vertices.at(0).z(), -1.0, 1e-6);
47+
EXPECT_NEAR(vertices.at(7).x(), -2.232050807568877293527, 1e-6);
48+
EXPECT_NEAR(vertices.at(7).y(), 3.598076211353315940291, 1e-6);
49+
EXPECT_NEAR(vertices.at(7).z(), 7.0, 1e-6);
50+
51+
// Test at Shape::CYLINDER
52+
shape.type = 1;
53+
vertices.clear();
54+
55+
image_projection_based_fusion::objectToVertices(pose, shape, vertices);
56+
57+
EXPECT_FALSE(vertices.empty());
58+
EXPECT_NEAR(vertices.at(0).x(), 2.732050807568877293528, 1e-6);
59+
EXPECT_NEAR(vertices.at(0).y(), 3.0, 1e-6);
60+
EXPECT_NEAR(vertices.at(0).z(), 7.0, 1e-6);
61+
EXPECT_NEAR(vertices.at(11).x(), 2.732050807568877293528, 1e-6);
62+
EXPECT_NEAR(vertices.at(11).y(), 1.0, 1e-6);
63+
EXPECT_NEAR(vertices.at(11).z(), -1.0, 1e-6);
64+
65+
// Test at Shape::POLYGON (Nothing to do)
66+
shape.type = 2;
67+
vertices.clear();
68+
69+
image_projection_based_fusion::objectToVertices(pose, shape, vertices);
70+
71+
EXPECT_TRUE(vertices.empty());
72+
}
73+
74+
TEST(transformPoints,
75+
test_transformPoints)
76+
{
77+
std::vector<Eigen::Vector3d> input_points;
78+
Eigen::Vector3d point(0.0, 0.0, 0.0);
79+
input_points.push_back(point);
80+
Eigen::Translation<double, 3> translation(1.0, 1.0, 1.0);
81+
Eigen::Matrix3d rotation = (Eigen::AngleAxisd(M_PI/4, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(M_PI/4, Eigen::Vector3d::UnitY()) * Eigen::AngleAxisd(M_PI/4, Eigen::Vector3d::UnitZ())).toRotationMatrix();
82+
Eigen::Affine3d affine_transform = rotation * translation;
83+
std::vector<Eigen::Vector3d> output_points;
84+
85+
image_projection_based_fusion::transformPoints(input_points, affine_transform, output_points);
86+
87+
EXPECT_FALSE(output_points.empty());
88+
EXPECT_NEAR(output_points.at(0).x(), 0.7071067811865475244008, 1e-6);
89+
EXPECT_NEAR(output_points.at(0).y(), 0.5, 1e-6);
90+
EXPECT_NEAR(output_points.at(0).z(), 1.5, 1e-6);
91+
}
92+
93+
TEST(is_inside,
94+
test_is_inside)
95+
{
96+
// Test default pattern
97+
sensor_msgs::msg::RegionOfInterest outer;
98+
outer.x_offset = 30;
99+
outer.y_offset = 40;
100+
outer.height = 400;
101+
outer.width = 300;
102+
sensor_msgs::msg::RegionOfInterest inner;
103+
inner.x_offset = 31;
104+
inner.y_offset = 41;
105+
inner.height = 399;
106+
inner.width = 299;
107+
const double outer_offset_scale = 1.0;
108+
109+
bool inside_flag = image_projection_based_fusion::is_inside(outer, inner, outer_offset_scale);
110+
111+
EXPECT_TRUE(inside_flag);
112+
113+
// Test left-top outside pattern
114+
inner.x_offset = 29;
115+
inner.y_offset = 39;
116+
117+
inside_flag = image_projection_based_fusion::is_inside(outer, inner, outer_offset_scale);
118+
119+
EXPECT_FALSE(inside_flag);
120+
121+
// Test right-bottom outside pattern
122+
inner.x_offset = 31;
123+
inner.y_offset = 41;
124+
inner.height = 401;
125+
inner.width = 301;
126+
127+
inside_flag = image_projection_based_fusion::is_inside(outer, inner, outer_offset_scale);
128+
129+
EXPECT_FALSE(inside_flag);
130+
}
131+
132+
TEST(sanitizeROI,
133+
test_sanitizeROI)
134+
{
135+
// Test default pattern
136+
sensor_msgs::msg::RegionOfInterest roi;
137+
roi.x_offset = 10;
138+
roi.y_offset = 20;
139+
roi.height = 200;
140+
roi.width = 100;
141+
int height_ = 400; // image height
142+
int width_ = 300; // image width
143+
144+
image_projection_based_fusion::sanitizeROI(roi, width_, height_);
145+
146+
EXPECT_EQ(roi.height, 200);
147+
EXPECT_EQ(roi.width, 100);
148+
149+
// Test pattern that x_offset or y_offset is not in image
150+
roi.x_offset = 100;
151+
roi.y_offset = 200;
152+
height_ = 100;
153+
width_ = 50;
154+
155+
image_projection_based_fusion::sanitizeROI(roi, width_, height_);
156+
157+
EXPECT_EQ(roi.height, 0);
158+
EXPECT_EQ(roi.width, 0);
159+
160+
// Test patten that roi does not fit within image
161+
roi.x_offset = 10;
162+
roi.y_offset = 20;
163+
roi.height = 500;
164+
roi.width = 400;
165+
166+
image_projection_based_fusion::sanitizeROI(roi, width_, height_);
167+
168+
EXPECT_EQ(roi.height, 80);
169+
EXPECT_EQ(roi.width, 40);
170+
}
171+
172+
int main(int argc, char * argv[])
173+
{
174+
testing::InitGoogleTest(&argc, argv);
175+
return RUN_ALL_TESTS();
176+
}

0 commit comments

Comments
 (0)