test_geometry_util.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <cmath>
00031 #include <limits>
00032 #include <vector>
00033 
00034 #include <gtest/gtest.h>
00035 
00036 #include <opencv2/core/core.hpp>
00037 
00038 #include <tf/transform_datatypes.h>
00039 
00040 #include <swri_math_util/constants.h>
00041 #include <swri_image_util/geometry_util.h>
00042 
00043 TEST(GeometryUtilTests, Intersects)
00044 {
00045   swri_image_util::BoundingBox b1(-2, -2, 4, 4);
00046   swri_image_util::BoundingBox b2(-1, -1, 2, 2);
00047   swri_image_util::BoundingBox b3(-4, -4, 2.5, 2.5);
00048   swri_image_util::BoundingBox b4(1.5, 1.5, 6, 6);
00049   swri_image_util::BoundingBox b5(4, 4, 6, 6);
00050 
00051   EXPECT_TRUE(swri_image_util::Intersects(b1, b2));
00052   EXPECT_TRUE(swri_image_util::Intersects(b1, b3));
00053   EXPECT_TRUE(swri_image_util::Intersects(b1, b4));
00054   EXPECT_FALSE(swri_image_util::Intersects(b1, b5));
00055 
00056   EXPECT_TRUE(swri_image_util::Intersects(b2, b1));
00057   EXPECT_FALSE(swri_image_util::Intersects(b2, b3));
00058   EXPECT_FALSE(swri_image_util::Intersects(b2, b4));
00059   EXPECT_FALSE(swri_image_util::Intersects(b2, b5));
00060 
00061   EXPECT_TRUE(swri_image_util::Intersects(b3, b1));
00062   EXPECT_FALSE(swri_image_util::Intersects(b3, b2));
00063   EXPECT_FALSE(swri_image_util::Intersects(b3, b4));
00064   EXPECT_FALSE(swri_image_util::Intersects(b3, b5));
00065 
00066   EXPECT_TRUE(swri_image_util::Intersects(b4, b1));
00067   EXPECT_FALSE(swri_image_util::Intersects(b4, b2));
00068   EXPECT_FALSE(swri_image_util::Intersects(b4, b3));
00069   EXPECT_TRUE(swri_image_util::Intersects(b4, b5));
00070 
00071   EXPECT_FALSE(swri_image_util::Intersects(b5, b1));
00072   EXPECT_FALSE(swri_image_util::Intersects(b5, b2));
00073   EXPECT_FALSE(swri_image_util::Intersects(b5, b3));
00074   EXPECT_TRUE(swri_image_util::Intersects(b5, b4));
00075 }
00076 
00077 TEST(GeometryUtilTests, GetOverlappingArea1)
00078 {
00079   cv::Rect rect(-5, -5, 10, 10);
00080   cv::Rect rect2(-10, -5, 20, 10);
00081 
00082   cv::Mat identity(2, 3, CV_32FC1);
00083   identity.at<float>(0,0) = 1;
00084   identity.at<float>(0,1) = 0;
00085   identity.at<float>(0,2) = 0;
00086   identity.at<float>(1,0) = 0;
00087   identity.at<float>(1,1) = 1;
00088   identity.at<float>(1,2) = 0;
00089 
00090   EXPECT_FLOAT_EQ(100, swri_image_util::GetOverlappingArea(rect, identity));
00091   EXPECT_FLOAT_EQ(200, swri_image_util::GetOverlappingArea(rect2, identity));
00092 
00093   cv::Mat shift_15_x(2, 3, CV_32FC1);
00094   shift_15_x.at<float>(0,0) = 1;
00095   shift_15_x.at<float>(0,1) = 0;
00096   shift_15_x.at<float>(0,2) = 15;
00097   shift_15_x.at<float>(1,0) = 0;
00098   shift_15_x.at<float>(1,1) = 1;
00099   shift_15_x.at<float>(1,2) = 0;
00100 
00101   EXPECT_FLOAT_EQ(0, swri_image_util::GetOverlappingArea(rect, shift_15_x));
00102   EXPECT_FLOAT_EQ(50, swri_image_util::GetOverlappingArea(rect2, shift_15_x));
00103 
00104   cv::Mat shift_5_x(2, 3, CV_32FC1);
00105   shift_5_x.at<float>(0,0) = 1;
00106   shift_5_x.at<float>(0,1) = 0;
00107   shift_5_x.at<float>(0,2) = 5;
00108   shift_5_x.at<float>(1,0) = 0;
00109   shift_5_x.at<float>(1,1) = 1;
00110   shift_5_x.at<float>(1,2) = 0;
00111 
00112   EXPECT_FLOAT_EQ(50, swri_image_util::GetOverlappingArea(rect, shift_5_x));
00113   EXPECT_FLOAT_EQ(150, swri_image_util::GetOverlappingArea(rect2, shift_5_x));
00114 
00115   cv::Mat shift_5_xy(2, 3, CV_32FC1);
00116   shift_5_xy.at<float>(0,0) = 1;
00117   shift_5_xy.at<float>(0,1) = 0;
00118   shift_5_xy.at<float>(0,2) = 5;
00119   shift_5_xy.at<float>(1,0) = 0;
00120   shift_5_xy.at<float>(1,1) = 1;
00121   shift_5_xy.at<float>(1,2) = 5;
00122 
00123   EXPECT_FLOAT_EQ(25, swri_image_util::GetOverlappingArea(rect, shift_5_xy));
00124   EXPECT_FLOAT_EQ(75, swri_image_util::GetOverlappingArea(rect2, shift_5_xy));
00125 }
00126 
00127 TEST(GeometryUtilTests, GetOverlappingArea2)
00128 {
00129   cv::Rect rect(-5, -5, 10, 10);
00130   cv::Rect rect2(-10, -5, 20, 10);
00131 
00132   cv::Mat rotate90(2, 3, CV_32FC1);
00133   rotate90.at<float>(0,0) = 0;
00134   rotate90.at<float>(0,1) = 1;
00135   rotate90.at<float>(0,2) = 0;
00136   rotate90.at<float>(1,0) = -1;
00137   rotate90.at<float>(1,1) = 0;
00138   rotate90.at<float>(1,2) = 0;
00139 
00140   EXPECT_FLOAT_EQ(100, swri_image_util::GetOverlappingArea(rect, rotate90));
00141   EXPECT_FLOAT_EQ(100, swri_image_util::GetOverlappingArea(rect2, rotate90));
00142 
00143   cv::Mat rotate180(2, 3, CV_32FC1);
00144   rotate180.at<float>(0,0) = -1;
00145   rotate180.at<float>(0,1) = 0;
00146   rotate180.at<float>(0,2) = 0;
00147   rotate180.at<float>(1,0) = 0;
00148   rotate180.at<float>(1,1) = -1;
00149   rotate180.at<float>(1,2) = 0;
00150 
00151   EXPECT_FLOAT_EQ(100, swri_image_util::GetOverlappingArea(rect, rotate180));
00152   EXPECT_FLOAT_EQ(200, swri_image_util::GetOverlappingArea(rect2, rotate180));
00153 
00154   cv::Mat rotate45(2, 3, CV_32FC1);
00155   rotate45.at<float>(0,0) = std::cos(swri_math_util::_half_pi * 0.5);
00156   rotate45.at<float>(0,1) = std::sin(swri_math_util::_half_pi * 0.5);
00157   rotate45.at<float>(0,2) = 0;
00158   rotate45.at<float>(1,0) = -std::sin(swri_math_util::_half_pi * 0.5);
00159   rotate45.at<float>(1,1) = std::cos(swri_math_util::_half_pi * 0.5);
00160   rotate45.at<float>(1,2) = 0;
00161 
00162   EXPECT_FLOAT_EQ(82.842712, swri_image_util::GetOverlappingArea(rect, rotate45));
00163   EXPECT_FLOAT_EQ(136.3961, swri_image_util::GetOverlappingArea(rect2, rotate45));
00164 }
00165 
00166 TEST(GeometryUtilTests, TestProjectEllipse1)
00167 {
00168   cv::Mat ellipsoid1(3, 3, CV_32FC1);
00169   ellipsoid1.at<float>(0,0) = 1;
00170   ellipsoid1.at<float>(0,1) = 0;
00171   ellipsoid1.at<float>(0,2) = 0;
00172   ellipsoid1.at<float>(1,0) = 0;
00173   ellipsoid1.at<float>(1,1) = 1;
00174   ellipsoid1.at<float>(1,2) = 0;
00175   ellipsoid1.at<float>(2,0) = 0;
00176   ellipsoid1.at<float>(2,1) = 0;
00177   ellipsoid1.at<float>(2,2) = 1;
00178 
00179   cv::Mat ellipse1(2, 2, CV_32FC1);
00180   ellipse1.at<float>(0,0) = 1;
00181   ellipse1.at<float>(0,1) = 0;
00182   ellipse1.at<float>(1,0) = 0;
00183   ellipse1.at<float>(1,1) = 1;
00184 
00185   cv::Mat projected1 = swri_image_util::ProjectEllipsoid(ellipsoid1);
00186 
00187   EXPECT_EQ(ellipse1.at<float>(0, 0), projected1.at<float>(0, 0));
00188   EXPECT_EQ(ellipse1.at<float>(0, 1), projected1.at<float>(0, 1));
00189   EXPECT_EQ(ellipse1.at<float>(1, 0), projected1.at<float>(1, 0));
00190   EXPECT_EQ(ellipse1.at<float>(1, 1), projected1.at<float>(1, 1));
00191 }
00192 
00193 TEST(GeometryUtilTests, TestProjectEllipse2)
00194 {
00195   cv::Mat ellipsoid1(3, 3, CV_32FC1);
00196   ellipsoid1.at<float>(0,0) = 10;
00197   ellipsoid1.at<float>(0,1) = 0;
00198   ellipsoid1.at<float>(0,2) = 0;
00199   ellipsoid1.at<float>(1,0) = 0;
00200   ellipsoid1.at<float>(1,1) = 15;
00201   ellipsoid1.at<float>(1,2) = 0;
00202   ellipsoid1.at<float>(2,0) = 0;
00203   ellipsoid1.at<float>(2,1) = 0;
00204   ellipsoid1.at<float>(2,2) = -35;
00205 
00206   cv::Mat ellipse1(2, 2, CV_32FC1);
00207   ellipse1.at<float>(0,0) = 10;
00208   ellipse1.at<float>(0,1) = 0;
00209   ellipse1.at<float>(1,0) = 0;
00210   ellipse1.at<float>(1,1) = 15;
00211 
00212   cv::Mat projected1 = swri_image_util::ProjectEllipsoid(ellipsoid1);
00213 
00214   EXPECT_EQ(ellipse1.at<float>(0, 0), projected1.at<float>(0, 0));
00215   EXPECT_EQ(ellipse1.at<float>(0, 1), projected1.at<float>(0, 1));
00216   EXPECT_EQ(ellipse1.at<float>(1, 0), projected1.at<float>(1, 0));
00217   EXPECT_EQ(ellipse1.at<float>(1, 1), projected1.at<float>(1, 1));
00218 }
00219 
00220 TEST(GeometryUtilTests, TestProjectEllipse3)
00221 {
00222   cv::Mat ellipsoid1(3, 3, CV_32FC1);
00223   ellipsoid1.at<float>(0,0) = 10;
00224   ellipsoid1.at<float>(0,1) = 0;
00225   ellipsoid1.at<float>(0,2) = 0;
00226   ellipsoid1.at<float>(1,0) = 0;
00227   ellipsoid1.at<float>(1,1) = 15;
00228   ellipsoid1.at<float>(1,2) = 0;
00229   ellipsoid1.at<float>(2,0) = 0;
00230   ellipsoid1.at<float>(2,1) = 0;
00231   ellipsoid1.at<float>(2,2) = std::numeric_limits<double>::max() * 0.5;
00232 
00233   cv::Mat ellipse1(2, 2, CV_32FC1);
00234   ellipse1.at<float>(0,0) = 10;
00235   ellipse1.at<float>(0,1) = 0;
00236   ellipse1.at<float>(1,0) = 0;
00237   ellipse1.at<float>(1,1) = 15;
00238 
00239   cv::Mat projected1 = swri_image_util::ProjectEllipsoid(ellipsoid1);
00240 
00241   EXPECT_EQ(ellipse1.at<float>(0, 0), projected1.at<float>(0, 0));
00242   EXPECT_EQ(ellipse1.at<float>(0, 1), projected1.at<float>(0, 1));
00243   EXPECT_EQ(ellipse1.at<float>(1, 0), projected1.at<float>(1, 0));
00244   EXPECT_EQ(ellipse1.at<float>(1, 1), projected1.at<float>(1, 1));
00245 }
00246 
00247 
00248 // TODO(malban): Test projecting an ellipsoid that is not axis aligned.
00249 
00250 TEST(GeometryUtilTests, TestProjectEllipseInvalid1)
00251 {
00252   cv::Mat ellipsoid2(3, 3, CV_32FC1);
00253   ellipsoid2.at<float>(0,0) = 1;
00254   ellipsoid2.at<float>(0,1) = 0;
00255   ellipsoid2.at<float>(0,2) = 0;
00256   ellipsoid2.at<float>(1,0) = 0;
00257   ellipsoid2.at<float>(1,1) = 1;
00258   ellipsoid2.at<float>(1,2) = 0;
00259   ellipsoid2.at<float>(2,0) = 0;
00260   ellipsoid2.at<float>(2,1) = 0;
00261   ellipsoid2.at<float>(2,2) = 0;
00262 
00263   cv::Mat projected2 = swri_image_util::ProjectEllipsoid(ellipsoid2);
00264 
00265   EXPECT_TRUE(projected2.empty());
00266 }
00267 
00268 TEST(GeometryUtilTests, TestProjectEllipseInvalid2)
00269 {
00270   cv::Mat ellipsoid2(2, 2, CV_32FC1);
00271   ellipsoid2.at<float>(0,0) = 1;
00272   ellipsoid2.at<float>(0,1) = 0;
00273   ellipsoid2.at<float>(1,0) = 0;
00274   ellipsoid2.at<float>(1,1) = 1;
00275 
00276   cv::Mat projected2 = swri_image_util::ProjectEllipsoid(ellipsoid2);
00277 
00278   EXPECT_TRUE(projected2.empty());
00279 }
00280 
00281 TEST(GeometryUtilTests, TestProjectEllipseInvalid3)
00282 {
00283   cv::Mat ellipsoid2(2, 2, CV_32SC1);
00284   ellipsoid2.at<int32_t>(0,0) = 1;
00285   ellipsoid2.at<int32_t>(0,1) = 0;
00286   ellipsoid2.at<int32_t>(1,0) = 0;
00287   ellipsoid2.at<int32_t>(1,1) = 1;
00288 
00289   cv::Mat projected2 = swri_image_util::ProjectEllipsoid(ellipsoid2);
00290 
00291   EXPECT_TRUE(projected2.empty());
00292 }
00293 
00294 TEST(GeometryUtilTests, TestGetEllipsePoints1)
00295 {
00296   cv::Mat ellipse(2, 2, CV_32FC1);
00297   ellipse.at<float>(0,0) = 1;
00298   ellipse.at<float>(0,1) = 0;
00299   ellipse.at<float>(1,0) = 0;
00300   ellipse.at<float>(1,1) = 1;
00301 
00302   std::vector<tf::Vector3> points = swri_image_util::GetEllipsePoints(
00303       ellipse, tf::Vector3(0, 0, 0), 1, 8);
00304 
00305   ASSERT_EQ(8, points.size());
00306 
00307   EXPECT_FLOAT_EQ(1, points[0].x());
00308   EXPECT_NEAR(0, points[0].y(), 0.000000001);
00309 
00310   EXPECT_NEAR(0.7071067811865475243818940365, points[1].x(),  0.000000001);
00311   EXPECT_NEAR(0.7071067811865475243818940365, points[1].y(),  0.000000001);
00312 
00313   EXPECT_NEAR(0, points[2].x(), 0.000000001);
00314   EXPECT_FLOAT_EQ(1, points[2].y());
00315 
00316   EXPECT_NEAR(-0.7071067811865475243818940365, points[3].x(),  0.000000001);
00317   EXPECT_NEAR(0.7071067811865475243818940365, points[3].y(),  0.000000001);
00318 
00319   EXPECT_FLOAT_EQ(-1, points[4].x());
00320   EXPECT_NEAR(0, points[4].y(), 0.000000001);
00321 
00322   EXPECT_NEAR(-0.7071067811865475243818940365, points[5].x(),  0.000000001);
00323   EXPECT_NEAR(-0.7071067811865475243818940365, points[5].y(),  0.000000001);
00324 
00325   EXPECT_NEAR(0, points[6].x(), 0.000000001);
00326   EXPECT_FLOAT_EQ(-1, points[6].y());
00327 
00328   EXPECT_NEAR(0.7071067811865475243818940365, points[7].x(),  0.000000001);
00329   EXPECT_NEAR(-0.7071067811865475243818940365, points[7].y(),  0.000000001);
00330 }
00331 
00332 // Run all the tests that were declared with TEST()
00333 int main(int argc, char **argv)
00334 {
00335   testing::InitGoogleTest(&argc, argv);
00336 
00337   return RUN_ALL_TESTS();
00338 }


swri_image_util
Author(s): Kris Kozak
autogenerated on Tue Oct 3 2017 03:19:34