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 <cstddef>
31 #include <cmath>
32 #include <vector>
33 
34 #include <gtest/gtest.h>
35 
36 #include <mcl_3dl/chunked_kdtree.h>
37 #include <mcl_3dl/raycast.h>
38 
39 TEST(Raycast, Collision)
40 {
41  pcl::PointCloud<pcl::PointXYZ> pc;
42  for (float y = -1.0; y < 1.0; y += 0.1)
43  {
44  for (float z = -1.0; z < 1.0; z += 0.1)
45  {
46  pc.push_back(pcl::PointXYZ(0.5, y, z));
47  }
48  }
50  kdtree->setInputCloud(pc.makeShared());
51 
52  for (float y = -0.8; y < 0.8; y += 0.11)
53  {
54  for (float z = -0.8; z < 0.8; z += 0.13)
55  {
56  bool collision = false;
58  kdtree,
59  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, y * 2.0, z * 2.0), 0.1, 0.1);
60  for (auto point : ray)
61  {
62  if (point.collision_)
63  {
64  collision = true;
65  EXPECT_NEAR((point.pos_ - mcl_3dl::Vec3(0.5, y, z)).norm(), 0.0, 0.2);
66  break;
67  }
68  }
69  ASSERT_TRUE(collision);
70  }
71  }
72  for (float y = -1.0; y < 1.0; y += 0.11)
73  {
74  for (float z = -1.0; z < 1.0; z += 0.13)
75  {
76  bool collision = false;
78  kdtree,
79  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, y, z), 0.1, 0.1);
80  for (auto point : ray)
81  {
82  if (point.collision_)
83  collision = true;
84  }
85  ASSERT_FALSE(collision);
86  }
87  }
88  {
89  bool collision = false;
91  kdtree,
92  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 3.0, 0.0), 0.1, 0.1);
93  for (auto point : ray)
94  {
95  if (point.collision_)
96  collision = true;
97  }
98  ASSERT_FALSE(collision);
99  }
100 }
101 
102 TEST(Raycast, CollisionTolerance)
103 {
104  pcl::PointCloud<pcl::PointXYZ> pc;
105  for (float y = -1.0; y < 1.0; y += 0.05)
106  {
107  for (float z = -1.0; z < 1.0; z += 0.1)
108  {
109  pc.push_back(pcl::PointXYZ(0.5, y, z));
110  }
111  }
113  kdtree->setInputCloud(pc.makeShared());
114 
115  {
116  bool collision = false;
118  kdtree,
119  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.7, 0.0, 0.0), 0.05, 0.1);
120  for (auto point : ray)
121  {
122  if (point.collision_)
123  {
124  collision = true;
125  break;
126  }
127  }
128  ASSERT_TRUE(collision);
129  }
130  {
131  bool collision = false;
133  kdtree,
134  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.5, 0.0, 0.0), 0.1, 0.15);
135  for (auto point : ray)
136  {
137  if (point.collision_)
138  {
139  collision = true;
140  break;
141  }
142  }
143  ASSERT_FALSE(collision);
144  }
145  {
146  bool collision = false;
148  kdtree,
149  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(0.3, 0.0, 0.0), 0.1, 0.15);
150  for (auto point : ray)
151  {
152  if (point.collision_)
153  {
154  collision = true;
155  break;
156  }
157  }
158  ASSERT_FALSE(collision);
159  }
160 }
161 TEST(Raycast, SinAng)
162 {
163  pcl::PointCloud<pcl::PointXYZ> pc;
164  for (float y = -1.0; y < 1.0; y += 0.1)
165  {
166  for (float z = -1.0; z < 1.0; z += 0.1)
167  {
168  pc.push_back(pcl::PointXYZ(0.5, y, z));
169  }
170  }
172  kdtree->setInputCloud(pc.makeShared());
173 
174  {
175  bool collision = false;
177  kdtree,
178  mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, 0.0, 0.0), 0.1, 0.1);
179  for (auto point : ray)
180  {
181  if (point.collision_)
182  {
183  EXPECT_NEAR(point.sin_angle_, 1.0, 0.1);
184  collision = true;
185  break;
186  }
187  }
188  ASSERT_TRUE(collision);
189  }
190  {
191  bool collision = false;
193  kdtree,
194  mcl_3dl::Vec3(0.0, 5.0, 0.0), mcl_3dl::Vec3(1.0, -5.0, 0.0), 0.1, 0.1);
195  for (auto point : ray)
196  {
197  if (point.collision_)
198  {
199  EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 5.0), 0.05);
200  collision = true;
201  break;
202  }
203  }
204  ASSERT_TRUE(collision);
205  }
206  {
207  bool collision = false;
209  kdtree,
210  mcl_3dl::Vec3(0.0, 3.0, 0.0), mcl_3dl::Vec3(1.0, -3.0, 0.0), 0.1, 0.1);
211  for (auto point : ray)
212  {
213  if (point.collision_)
214  {
215  EXPECT_NEAR(point.sin_angle_, sinf(0.5 / 3.0), 0.05);
216  collision = true;
217  break;
218  }
219  }
220  ASSERT_TRUE(collision);
221  }
222 }
223 
224 int main(int argc, char** argv)
225 {
226  testing::InitGoogleTest(&argc, argv);
227 
228  return RUN_ALL_TESTS();
229 }
std::shared_ptr< ChunkedKdtree > Ptr
TFSIMD_FORCE_INLINE const tfScalar & y() const
int main(int argc, char **argv)
void setInputCloud(const typename pcl::PointCloud< POINT_TYPE >::ConstPtr cloud)
TFSIMD_FORCE_INLINE const tfScalar & z() const
TEST(Raycast, Collision)


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36