performance_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 <boost/chrono.hpp>
35 
36 #include <mcl_3dl/chunked_kdtree.h>
39 
40 void performanceTestRaycast(const float chunk_size)
41 {
42  std::cerr << "## Chunk size: " << chunk_size << std::endl;
43  const auto ts = boost::chrono::high_resolution_clock::now();
44  pcl::PointCloud<pcl::PointXYZ> pc;
45  for (float y = -50.0; y < 50.0; y += 0.1)
46  {
47  for (float z = -50.0; z < 50.0; z += 0.1)
48  {
49  pc.push_back(pcl::PointXYZ(5.0, y, z));
50  pc.push_back(pcl::PointXYZ(y + 10.0, 1.5, z));
51  }
52  }
54  kdtree->setInputCloud(pc.makeShared());
55  const auto tnow = boost::chrono::high_resolution_clock::now();
56  std::cerr << "- Generate kdtree: " << boost::chrono::duration<float>(tnow - ts).count() << " sec" << std::endl;
57 
58  const auto ts2 = boost::chrono::high_resolution_clock::now();
59  size_t collision_cnt = 0;
60  size_t cnt = 0;
61  mcl_3dl::RaycastUsingKDTree<pcl::PointXYZ> ray(0.1, 0.1, 0.1, 0.1 * std::sqrt(3.f));
62  const auto ts3 = boost::chrono::high_resolution_clock::now();
63  for (float y = -50.0; y < 50.0; y += 1.2)
64  {
65  for (float z = -50.0; z < 50.0; z += 1.1)
66  {
67  cnt++;
68  ray.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, y * 2.0, z * 2.0));
70  while (ray.getNextCastResult(point))
71  {
72  if (point.collision_)
73  {
74  collision_cnt++;
75  break;
76  }
77  }
78  }
79  }
80  std::cerr << "- Collisions: " << collision_cnt << "/" << cnt << std::endl;
81  const auto tnow2 = boost::chrono::high_resolution_clock::now();
82  std::cerr << "- mcl_3dl::RaycastUsingKDTree Init: " << boost::chrono::duration<float>(ts3 - ts2).count() << " sec"
83  << std::endl;
84  std::cerr << "- mcl_3dl::RaycastUsingKDTree: " << boost::chrono::duration<float>(tnow2 - ts3).count() << " sec"
85  << std::endl;
86  std::cerr << std::endl;
87 }
88 
89 void performanceTestRaycastUsingDDA(const float dda_grid_size)
90 {
91  std::cerr << "## DDA grid size: " << dda_grid_size << std::endl;
92  const auto ts = boost::chrono::high_resolution_clock::now();
93  pcl::PointCloud<pcl::PointXYZ> pc;
94  for (float y = -50.0; y < 50.0; y += 0.1)
95  {
96  for (float z = -50.0; z < 50.0; z += 0.1)
97  {
98  pc.push_back(pcl::PointXYZ(5.0, y, z));
99  pc.push_back(pcl::PointXYZ(y + 10.0, 1.5, z));
100  }
101  }
102  // Add dummy points to get edges of map
103  pc.push_back(pcl::PointXYZ(-1.05, -50.05, -50.05));
104  pc.push_back(pcl::PointXYZ(2.05, 50.05, 50.05));
105 
107  kdtree->setInputCloud(pc.makeShared());
108  const auto tnow = boost::chrono::high_resolution_clock::now();
109  std::cerr << "- Generate kdtree: " << boost::chrono::duration<float>(tnow - ts).count() << " sec" << std::endl;
110 
111  const auto ts2 = boost::chrono::high_resolution_clock::now();
112  mcl_3dl::RaycastUsingDDA<pcl::PointXYZ> ray(0.1, 0.1, 0.1, dda_grid_size, 0.5, dda_grid_size * std::sqrt(3));
113  // Set dummy ray to initialize DDA.
114  ray.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, 0.0, 0.0));
115  size_t collision_cnt = 0;
116  size_t cnt = 0;
117  const auto ts3 = boost::chrono::high_resolution_clock::now();
118  for (float y = -50.0; y < 50.0; y += 1.2)
119  {
120  for (float z = -50.0; z < 50.0; z += 1.1)
121  {
122  cnt++;
123  ray.setRay(kdtree, mcl_3dl::Vec3(0.0, 0.0, 0.0), mcl_3dl::Vec3(1.0, y * 2.0, z * 2.0));
125  while (ray.getNextCastResult(point))
126  {
127  if (point.collision_)
128  {
129  collision_cnt++;
130  break;
131  }
132  }
133  }
134  }
135  std::cerr << "- Collisions: " << collision_cnt << "/" << cnt << std::endl;
136  const auto tnow2 = boost::chrono::high_resolution_clock::now();
137  std::cerr << "- mcl_3dl::RaycastUsingDDA Init: " << boost::chrono::duration<float>(ts3 - ts2).count() << " sec"
138  << std::endl;
139  std::cerr << "- mcl_3dl::RaycastUsingDDA: " << boost::chrono::duration<float>(tnow2 - ts3).count() << " sec"
140  << std::endl;
141  std::cerr << std::endl;
142 }
143 
144 int main(int argc, char** argv)
145 {
152  performanceTestRaycast(100.0);
153 
159 
160  return 0;
161 }
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
int main(int argc, char **argv)
bool getNextCastResult(CastResult &result) final
void performanceTestRaycastUsingDDA(const float dda_grid_size)
void setRay(typename ChunkedKdtree< POINT_TYPE >::Ptr kdtree, const Vec3 ray_begin, const Vec3 ray_end_org) final
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool getNextCastResult(CastResult &result) final
void performanceTestRaycast(const float chunk_size)


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