test_raycast.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2018, the mcl_3dl authors
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
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 the copyright holder nor the names of its 
00014  *       contributors may be used to endorse or promote products derived from 
00015  *       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 THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <cstddef>
00031 #include <cmath>
00032 #include <vector>
00033 
00034 #include <gtest/gtest.h>
00035 
00036 #include <mcl_3dl/chunked_kdtree.h>
00037 #include <mcl_3dl/raycast.h>
00038 
00039 TEST(Raycast, Collision)
00040 {
00041   pcl::PointCloud<pcl::PointXYZ> pc;
00042   for (float y = -1.0; y < 1.0; y += 0.1)
00043   {
00044     for (float z = -1.0; z < 1.0; z += 0.1)
00045     {
00046       pc.push_back(pcl::PointXYZ(0.5, y, z));
00047     }
00048   }
00049   mcl_3dl::ChunkedKdtree<pcl::PointXYZ>::Ptr kdtree(new mcl_3dl::ChunkedKdtree<pcl::PointXYZ>(10.0, 1.0));
00050   kdtree->setInputCloud(pc.makeShared());
00051 
00052   for (float y = -0.8; y < 0.8; y += 0.11)
00053   {
00054     for (float z = -0.8; z < 0.8; z += 0.13)
00055     {
00056       bool collision = false;
00057       mcl_3dl::Raycast<pcl::PointXYZ> ray(
00058           kdtree,
00059           mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, y * 2.0, z * 2.0), 0.1, 0.1);
00060       for (auto point : ray)
00061       {
00062         if (point.collision_)
00063         {
00064           collision = true;
00065           EXPECT_NEAR((point.pos_ - mcl_3dl::Vec3(0.5, y, z)).norm(), 0.0, 0.2);
00066           break;
00067         }
00068       }
00069       ASSERT_TRUE(collision);
00070     }
00071   }
00072   for (float y = -1.0; y < 1.0; y += 0.11)
00073   {
00074     for (float z = -1.0; z < 1.0; z += 0.13)
00075     {
00076       bool collision = false;
00077       mcl_3dl::Raycast<pcl::PointXYZ> ray(
00078           kdtree,
00079           mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, y, z), 0.1, 0.1);
00080       for (auto point : ray)
00081       {
00082         if (point.collision_)
00083           collision = true;
00084       }
00085       ASSERT_FALSE(collision);
00086     }
00087   }
00088   {
00089     bool collision = false;
00090     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00091         kdtree,
00092         mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 3.0, 0.0), 0.1, 0.1);
00093     for (auto point : ray)
00094     {
00095       if (point.collision_)
00096         collision = true;
00097     }
00098     ASSERT_FALSE(collision);
00099   }
00100 }
00101 
00102 TEST(Raycast, CollisionTolerance)
00103 {
00104   pcl::PointCloud<pcl::PointXYZ> pc;
00105   for (float y = -1.0; y < 1.0; y += 0.05)
00106   {
00107     for (float z = -1.0; z < 1.0; z += 0.1)
00108     {
00109       pc.push_back(pcl::PointXYZ(0.5, y, z));
00110     }
00111   }
00112   mcl_3dl::ChunkedKdtree<pcl::PointXYZ>::Ptr kdtree(new mcl_3dl::ChunkedKdtree<pcl::PointXYZ>(10.0, 1.0));
00113   kdtree->setInputCloud(pc.makeShared());
00114 
00115   {
00116     bool collision = false;
00117     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00118         kdtree,
00119         mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.7, 0.0, 0.0), 0.05, 0.1);
00120     for (auto point : ray)
00121     {
00122       if (point.collision_)
00123       {
00124         collision = true;
00125         break;
00126       }
00127     }
00128     ASSERT_TRUE(collision);
00129   }
00130   {
00131     bool collision = false;
00132     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00133         kdtree,
00134         mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 0.0, 0.0), 0.1, 0.15);
00135     for (auto point : ray)
00136     {
00137       if (point.collision_)
00138       {
00139         collision = true;
00140         break;
00141       }
00142     }
00143     ASSERT_FALSE(collision);
00144   }
00145   {
00146     bool collision = false;
00147     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00148         kdtree,
00149         mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.3, 0.0, 0.0), 0.1, 0.15);
00150     for (auto point : ray)
00151     {
00152       if (point.collision_)
00153       {
00154         collision = true;
00155         break;
00156       }
00157     }
00158     ASSERT_FALSE(collision);
00159   }
00160 }
00161 TEST(Raycast, SinAng)
00162 {
00163   pcl::PointCloud<pcl::PointXYZ> pc;
00164   for (float y = -1.0; y < 1.0; y += 0.1)
00165   {
00166     for (float z = -1.0; z < 1.0; z += 0.1)
00167     {
00168       pc.push_back(pcl::PointXYZ(0.5, y, z));
00169     }
00170   }
00171   mcl_3dl::ChunkedKdtree<pcl::PointXYZ>::Ptr kdtree(new mcl_3dl::ChunkedKdtree<pcl::PointXYZ>(10.0, 1.0));
00172   kdtree->setInputCloud(pc.makeShared());
00173 
00174   {
00175     bool collision = false;
00176     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00177         kdtree,
00178         mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, 0.0, 0.0), 0.1, 0.1);
00179     for (auto point : ray)
00180     {
00181       if (point.collision_)
00182       {
00183         EXPECT_NEAR(point.sin_angle_, 1.0, 0.1);
00184         collision = true;
00185         break;
00186       }
00187     }
00188     ASSERT_TRUE(collision);
00189   }
00190   {
00191     bool collision = false;
00192     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00193         kdtree,
00194         mcl_3dl::Vec3(0.0, 5.0, 0.0), mcl_3dl::Vec3(1.0, -5.0, 0.0), 0.1, 0.1);
00195     for (auto point : ray)
00196     {
00197       if (point.collision_)
00198       {
00199         EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 5.0), 0.05);
00200         collision = true;
00201         break;
00202       }
00203     }
00204     ASSERT_TRUE(collision);
00205   }
00206   {
00207     bool collision = false;
00208     mcl_3dl::Raycast<pcl::PointXYZ> ray(
00209         kdtree,
00210         mcl_3dl::Vec3(0.0, 3.0, 0.0), mcl_3dl::Vec3(1.0, -3.0, 0.0), 0.1, 0.1);
00211     for (auto point : ray)
00212     {
00213       if (point.collision_)
00214       {
00215         EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 3.0), 0.05);
00216         collision = true;
00217         break;
00218       }
00219     }
00220     ASSERT_TRUE(collision);
00221   }
00222 }
00223 
00224 int main(int argc, char** argv)
00225 {
00226   testing::InitGoogleTest(&argc, argv);
00227 
00228   return RUN_ALL_TESTS();
00229 }


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 20 2019 20:04:43