|
| 1 | +/* |
| 2 | + * Software License Agreement (BSD License) |
| 3 | + * |
| 4 | + * Point Cloud Library (PCL) - www.pointclouds.org |
| 5 | + * Copyright (c) 2009-2011, Willow Garage, Inc. |
| 6 | + * Copyright (c) 2012-, Open Perception, Inc. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions |
| 12 | + * are met: |
| 13 | + * |
| 14 | + * * Redistributions of source code must retain the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer. |
| 16 | + * * Redistributions in binary form must reproduce the above |
| 17 | + * copyright notice, this list of conditions and the following |
| 18 | + * disclaimer in the documentation and/or other materials provided |
| 19 | + * with the distribution. |
| 20 | + * * Neither the name of the copyright holder(s) nor the names of its |
| 21 | + * contributors may be used to endorse or promote products derived |
| 22 | + * from this software without specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | + * POSSIBILITY OF SUCH DAMAGE. |
| 36 | + * |
| 37 | + * aabb_tree_cgal.h |
| 38 | + * Created on: Jun 02, 2022 |
| 39 | + * Author: Ramzi Sabra |
| 40 | + */ |
| 41 | + |
| 42 | +#pragma once |
| 43 | + |
| 44 | +#include <pcl/aabb_tree/aabb_tree.h> |
| 45 | + |
| 46 | +#include <CGAL/AABB_traits.h> |
| 47 | +#include <CGAL/AABB_tree.h> |
| 48 | +#include <CGAL/AABB_triangle_primitive.h> |
| 49 | +#include <CGAL/Simple_cartesian.h> |
| 50 | + |
| 51 | +namespace pcl { |
| 52 | +/** \brief AABBTreeCGAL is a ray intersection testing class using AABB tree |
| 53 | + * structures. The class is making use of the CGAL (The Computational Geometry Algorithms |
| 54 | + * Library) project. \author Ramzi Sabra \ingroup aabb_tree |
| 55 | + */ |
| 56 | +template <typename PointT> |
| 57 | +class AABBTreeCGAL : public AABBTree<PointT> { |
| 58 | +protected: |
| 59 | + using K = CGAL::Simple_cartesian<float>; |
| 60 | + using CGALPoint = K::Point_3; |
| 61 | + using CGALVector = K::Vector_3; |
| 62 | + using Ray = K::Ray_3; |
| 63 | + using Triangle = K::Triangle_3; |
| 64 | + |
| 65 | + using Iterator = std::vector<Triangle>::const_iterator; |
| 66 | + using Primitive = CGAL::AABB_triangle_primitive<K, Iterator>; |
| 67 | + using AABB_triangle_traits = CGAL::AABB_traits<K, Primitive>; |
| 68 | + |
| 69 | +public: |
| 70 | + using Tree = CGAL::AABB_tree<AABB_triangle_traits>; |
| 71 | + using TreePtr = std::shared_ptr<Tree>; |
| 72 | + using TreeConstPtr = std::shared_ptr<const Tree>; |
| 73 | + |
| 74 | + using PointCloud = typename AABBTree<PointT>::PointCloud; |
| 75 | + using PointCloudPtr = typename AABBTree<PointT>::PointCloudPtr; |
| 76 | + using PointCloudConstPtr = typename AABBTree<PointT>::PointCloudConstPtr; |
| 77 | + |
| 78 | + /** \brief Default constructor for AABBTreeCGAL. |
| 79 | + * \param[in] tree already-built CGAL AABB tree. Set to empty by default. |
| 80 | + */ |
| 81 | + AABBTreeCGAL(TreeConstPtr tree = TreeConstPtr()); |
| 82 | + |
| 83 | + /** \brief Set an already-build CGAL AABB tree as the tree. |
| 84 | + * \param[in] tree already-built CGAL AABB tree |
| 85 | + */ |
| 86 | + void |
| 87 | + setTree(TreeConstPtr tree); |
| 88 | + |
| 89 | + /** \brief Provide an input mesh to construct the tree. |
| 90 | + * \param[in] mesh the polygon mesh |
| 91 | + */ |
| 92 | + void |
| 93 | + setInputMesh(const pcl::PolygonMesh& mesh) override; |
| 94 | + |
| 95 | + /** \brief Provide an input mesh to construct the tree. |
| 96 | + * \param[in] mesh_cloud the mesh cloud |
| 97 | + * \param[in] mesh_polygons the mesh polygons |
| 98 | + */ |
| 99 | + void |
| 100 | + setInputMesh(PointCloudConstPtr mesh_cloud, |
| 101 | + const std::vector<Vertices>& mesh_polygons) override; |
| 102 | + |
| 103 | + /** \brief Check for the presence of intersection(s) for the given ray |
| 104 | + * \param[in] source the ray source point |
| 105 | + * \param[in] direction the ray direction vector |
| 106 | + */ |
| 107 | + bool |
| 108 | + checkForIntersection(const PointT& source, |
| 109 | + const Eigen::Vector3f& direction) const override; |
| 110 | + |
| 111 | + /** \brief Check for the number of intersections for the given ray |
| 112 | + * \param[in] source the ray source point |
| 113 | + * \param[in] direction the ray direction vector |
| 114 | + */ |
| 115 | + size_t |
| 116 | + numberOfIntersections(const PointT& source, |
| 117 | + const Eigen::Vector3f& direction) const override; |
| 118 | + |
| 119 | + /** \brief Get all intersections for the given ray |
| 120 | + * \param[in] source the ray source point |
| 121 | + * \param[in] direction the ray direction vector |
| 122 | + */ |
| 123 | + std::vector<index_t> |
| 124 | + getAllIntersections(const PointT& source, |
| 125 | + const Eigen::Vector3f& direction) const override; |
| 126 | + |
| 127 | + /** \brief Get any intersection for the given ray |
| 128 | + * \param[in] source the ray source point |
| 129 | + * \param[in] direction the ray direction vector |
| 130 | + */ |
| 131 | + index_t |
| 132 | + getAnyIntersection(const PointT& source, |
| 133 | + const Eigen::Vector3f& direction) const override; |
| 134 | + |
| 135 | + /** \brief Get the nearest intersection for the given ray |
| 136 | + * \param[in] source the ray source point |
| 137 | + * \param[in] direction the ray direction vector |
| 138 | + */ |
| 139 | + index_t |
| 140 | + getNearestIntersection(const PointT& source, |
| 141 | + const Eigen::Vector3f& direction) const override; |
| 142 | + |
| 143 | +protected: |
| 144 | + /** \brief Class getName method. */ |
| 145 | + std::string |
| 146 | + getName() const override |
| 147 | + { |
| 148 | + return ("AABBTreeCGAL"); |
| 149 | + } |
| 150 | + |
| 151 | + /** \brief A CGAL AABB tree object. */ |
| 152 | + TreeConstPtr tree_; |
| 153 | + |
| 154 | + /** \brief CGAL triangles used for building the tree. */ |
| 155 | + std::vector<Triangle> triangles_; |
| 156 | + |
| 157 | +private: |
| 158 | + /** \brief Generate a CGAL ray from a source point and a direction vector |
| 159 | + * \param[in] source the ray source point |
| 160 | + * \param[in] direction the ray direction vector |
| 161 | + */ |
| 162 | + static Ray |
| 163 | + generateRay(const PointT& source, const Eigen::Vector3f& direction); |
| 164 | +}; |
| 165 | +} // namespace pcl |
| 166 | + |
| 167 | +#ifdef PCL_NO_PRECOMPILE |
| 168 | +#include <pcl/aabb_tree/impl/aabb_tree_cgal.hpp> |
| 169 | +#endif |
0 commit comments