test_raycast.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <cmath>
31 #include <cstddef>
32 #include <vector>
33 
34 #include <gtest/gtest.h>
35 
36 #include <mcl_3dl/chunked_kdtree.h>
39 
40 TEST(Raycast, Collision)
41 {
42  pcl::PointCloud<pcl::PointXYZ> pc;
43  for (float y = -1.0; y < 1.0; y += 0.1)
44  {
45  for (float z = -1.0; z < 1.0; z += 0.1)
46  {
47  pc.push_back(pcl::PointXYZ(0.5, y, z));
48  }
49  }
51  kdtree->setInputCloud(pc.makeShared());
52  const float hit_range = 0.1 * std::sqrt(3.f);
53  mcl_3dl::RaycastUsingKDTree<pcl::PointXYZ> raycaster(0.1, 0.1, 0.1, hit_range);
54  for (float y = -0.8; y < 0.8; y += 0.11)
55  {
56  for (float z = -0.8; z < 0.8; z += 0.13)
57  {
58  bool collision = false;
59  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, y * 2.0, z * 2.0));
61  while (raycaster.getNextCastResult(point))
62  {
63  if (point.collision_)
64  {
65  collision = true;
66  EXPECT_NEAR((point.pos_ - mcl_3dl::Vec3(0.5, y, z)).norm(), 0.0, 0.2);
67  break;
68  }
69  }
70  ASSERT_TRUE(collision);
71  }
72  }
73  for (float y = -1.0; y < 1.0; y += 0.11)
74  {
75  for (float z = -1.0; z < 1.0; z += 0.13)
76  {
77  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5 - hit_range, y, z));
79  bool collision = false;
80  while (raycaster.getNextCastResult(point))
81  {
82  if (point.collision_)
83  collision = true;
84  }
85  ASSERT_FALSE(collision);
86  }
87  }
88  {
89  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 3.0, 0.0));
91  bool collision = false;
92  while (raycaster.getNextCastResult(point))
93  {
94  if (point.collision_)
95  collision = true;
96  }
97  ASSERT_FALSE(collision);
98  }
99 }
100 
101 TEST(Raycast, CollisionTolerance)
102 {
103  pcl::PointCloud<pcl::PointXYZ> pc;
104  for (float y = -1.0; y < 1.0; y += 0.05)
105  {
106  for (float z = -1.0; z < 1.0; z += 0.1)
107  {
108  pc.push_back(pcl::PointXYZ(0.5, y, z));
109  }
110  }
111 
113  kdtree->setInputCloud(pc.makeShared());
114 
115  {
116  mcl_3dl::RaycastUsingKDTree<pcl::PointXYZ> raycaster(0.05, 0.1, 0.1, 0.1 * std::sqrt(3.f));
117  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 0.0, 0.0));
119  bool collision = false;
120  while (raycaster.getNextCastResult(point))
121  {
122  if (point.collision_)
123  {
124  collision = true;
125  break;
126  }
127  }
128  ASSERT_TRUE(collision);
129  }
130  {
131  const float hit_range = 0.15 * std::sqrt(3.f);
132  const float epsilon = 0.01;
133  mcl_3dl::RaycastUsingKDTree<pcl::PointXYZ> raycaster(0.1, 0.15, 0.15, hit_range);
134  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5 - hit_range - epsilon, 0.0, 0.0));
136  bool collision = false;
137  while (raycaster.getNextCastResult(point))
138  {
139  if (point.collision_)
140  {
141  collision = true;
142  break;
143  }
144  }
145  ASSERT_FALSE(collision);
146  }
147 }
148 
149 TEST(Raycast, SinAng)
150 {
151  pcl::PointCloud<pcl::PointXYZ> pc;
152  for (float y = -1.0; y < 1.0; y += 0.1)
153  {
154  for (float z = -1.0; z < 1.0; z += 0.1)
155  {
156  pc.push_back(pcl::PointXYZ(0.5, y, z));
157  }
158  }
160  kdtree->setInputCloud(pc.makeShared());
161  mcl_3dl::RaycastUsingKDTree<pcl::PointXYZ> raycaster(0.1, 0.1, 0.1, 0.1 * std::sqrt(3.f));
162 
163  {
164  bool collision = false;
165  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, 0.0, 0.0));
167  while (raycaster.getNextCastResult(point))
168  {
169  if (point.collision_)
170  {
171  EXPECT_NEAR(point.sin_angle_, 1.0, 0.1);
172  collision = true;
173  break;
174  }
175  }
176  ASSERT_TRUE(collision);
177  }
178  {
179  bool collision = false;
180  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 5.0, 0.0), mcl_3dl::Vec3(1.0, -5.0, 0.0));
182  while (raycaster.getNextCastResult(point))
183  {
184  if (point.collision_)
185  {
186  EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 5.0), 0.05);
187  collision = true;
188  break;
189  }
190  }
191  ASSERT_TRUE(collision);
192  }
193  {
194  bool collision = false;
195  raycaster.setRay(kdtree, mcl_3dl::Vec3(0.0, 3.0, 0.0), mcl_3dl::Vec3(1.0, -3.0, 0.0));
197  while (raycaster.getNextCastResult(point))
198  {
199  if (point.collision_)
200  {
201  EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 3.0), 0.05);
202  collision = true;
203  break;
204  }
205  }
206  ASSERT_TRUE(collision);
207  }
208 }
209 
210 int main(int argc, char** argv)
211 {
212  testing::InitGoogleTest(&argc, argv);
213 
214  return RUN_ALL_TESTS();
215 }
f
std::shared_ptr< ChunkedKdtree > Ptr
void setInputCloud(const typename pcl::PointCloud< POINT_TYPE >::ConstPtr &cloud)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void setRay(typename ChunkedKdtree< POINT_TYPE >::Ptr kdtree, const Vec3 ray_begin, const Vec3 ray_end) final
bool getNextCastResult(CastResult &result) final
double epsilon
int main(int argc, char **argv)
TFSIMD_FORCE_INLINE const tfScalar & z() const
TEST(Raycast, Collision)


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29