|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Neka-Nat |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | + * IN THE SOFTWARE. |
| 20 | + **/ |
| 21 | +#include "cupoch/registration/correspondence_checker.h" |
| 22 | + |
| 23 | +#include <Eigen/Dense> |
| 24 | +#include <thrust/logical.h> |
| 25 | + |
| 26 | +#include "cupoch/geometry/pointcloud.h" |
| 27 | +#include "cupoch/utility/console.h" |
| 28 | + |
| 29 | +namespace cupoch { |
| 30 | +namespace registration { |
| 31 | + |
| 32 | +namespace { |
| 33 | + struct edge_length_checker_functor { |
| 34 | + float similarity_threshold_; |
| 35 | + size_t corres_size_; |
| 36 | + const Eigen::Vector3f* source_points_; |
| 37 | + const Eigen::Vector3f* target_points_; |
| 38 | + const Eigen::Vector2i* corres_; |
| 39 | + edge_length_checker_functor( |
| 40 | + float similarity_threshold, |
| 41 | + size_t corres_size, |
| 42 | + const Eigen::Vector3f* source_points, |
| 43 | + const Eigen::Vector3f* target_points, |
| 44 | + const Eigen::Vector2i* corres) |
| 45 | + : similarity_threshold_(similarity_threshold), |
| 46 | + corres_size_(corres_size), |
| 47 | + source_points_(source_points), |
| 48 | + target_points_(target_points), |
| 49 | + corres_(corres) {} |
| 50 | + __device__ bool operator()(int idx) { |
| 51 | + int i = idx / corres_size_; |
| 52 | + int j = idx % corres_size_; |
| 53 | + if (i == j) return true; |
| 54 | + float dis_source = (source_points_[corres_[i](0)] - source_points_[corres_[j](0)]).norm(); |
| 55 | + float dis_target = (target_points_[corres_[i](1)] - target_points_[corres_[j](1)]).norm(); |
| 56 | + return dis_source >= dis_target * similarity_threshold_ && |
| 57 | + dis_target >= dis_source * similarity_threshold_; |
| 58 | + } |
| 59 | + |
| 60 | + }; |
| 61 | + |
| 62 | + struct distance_checker_functor { |
| 63 | + float distance_threshold_; |
| 64 | + const Eigen::Vector3f* source_points_; |
| 65 | + const Eigen::Vector3f* target_points_; |
| 66 | + const Eigen::Matrix4f transformation_; |
| 67 | + distance_checker_functor( |
| 68 | + float distance_threshold, |
| 69 | + const Eigen::Vector3f* source_points, |
| 70 | + const Eigen::Vector3f* target_points, |
| 71 | + const Eigen::Matrix4f transformation) |
| 72 | + : distance_threshold_(distance_threshold), |
| 73 | + source_points_(source_points), |
| 74 | + target_points_(target_points), |
| 75 | + transformation_(transformation) {} |
| 76 | + __device__ bool operator()(const Eigen::Vector2i& corr) { |
| 77 | + const auto &pt = source_points_[corr(0)]; |
| 78 | + Eigen::Vector3f pt_trans = |
| 79 | + (transformation_ * Eigen::Vector4f(pt(0), pt(1), pt(2), 1.0)) |
| 80 | + .block<3, 1>(0, 0); |
| 81 | + return (target_points_[ |
| 82 | + corr(1)] - pt_trans).norm() <= distance_threshold_; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + struct normal_checker_functor { |
| 87 | + float cos_normal_angle_threshold_; |
| 88 | + const Eigen::Vector3f* source_points_; |
| 89 | + const Eigen::Vector3f* target_points_; |
| 90 | + const Eigen::Vector3f* source_normals_; |
| 91 | + const Eigen::Vector3f* target_normals_; |
| 92 | + const Eigen::Matrix4f transformation_; |
| 93 | + normal_checker_functor( |
| 94 | + float cos_normal_angle_threshold, |
| 95 | + const Eigen::Vector3f* source_points, |
| 96 | + const Eigen::Vector3f* target_points, |
| 97 | + const Eigen::Vector3f* source_normals, |
| 98 | + const Eigen::Vector3f* target_normals, |
| 99 | + const Eigen::Matrix4f transformation) |
| 100 | + : cos_normal_angle_threshold_(cos_normal_angle_threshold), |
| 101 | + source_points_(source_points), |
| 102 | + target_points_(target_points), |
| 103 | + source_normals_(source_normals), |
| 104 | + target_normals_(target_normals), |
| 105 | + transformation_(transformation) {} |
| 106 | + __device__ bool operator()(const Eigen::Vector2i& corr) { |
| 107 | + const auto &normal = source_normals_[corr(0)]; |
| 108 | + Eigen::Vector3f normal_trans = |
| 109 | + (transformation_ * |
| 110 | + Eigen::Vector4f(normal(0), normal(1), normal(2), 0.0)) |
| 111 | + .block<3, 1>(0, 0); |
| 112 | + return target_normals_[corr(1)].dot(normal) >= cos_normal_angle_threshold_; |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +bool CorrespondenceCheckerBasedOnEdgeLength::Check( |
| 118 | + const geometry::PointCloud &source, |
| 119 | + const geometry::PointCloud &target, |
| 120 | + const CorrespondenceSet &corres, |
| 121 | + const Eigen::Matrix4f & /*transformation*/) const { |
| 122 | + return thrust::all_of( |
| 123 | + thrust::make_counting_iterator<size_t>(0), |
| 124 | + thrust::make_counting_iterator(corres.size() * corres.size()), |
| 125 | + edge_length_checker_functor( |
| 126 | + similarity_threshold_, |
| 127 | + corres.size(), |
| 128 | + thrust::raw_pointer_cast(source.points_.data()), |
| 129 | + thrust::raw_pointer_cast(target.points_.data()), |
| 130 | + thrust::raw_pointer_cast(corres.data()))); |
| 131 | +} |
| 132 | + |
| 133 | +bool CorrespondenceCheckerBasedOnDistance::Check( |
| 134 | + const geometry::PointCloud &source, |
| 135 | + const geometry::PointCloud &target, |
| 136 | + const CorrespondenceSet &corres, |
| 137 | + const Eigen::Matrix4f &transformation) const { |
| 138 | + return thrust::all_of( |
| 139 | + corres.begin(), corres.end(), |
| 140 | + distance_checker_functor( |
| 141 | + distance_threshold_, |
| 142 | + thrust::raw_pointer_cast(source.points_.data()), |
| 143 | + thrust::raw_pointer_cast(target.points_.data()), |
| 144 | + transformation)); |
| 145 | +} |
| 146 | + |
| 147 | +bool CorrespondenceCheckerBasedOnNormal::Check( |
| 148 | + const geometry::PointCloud &source, |
| 149 | + const geometry::PointCloud &target, |
| 150 | + const CorrespondenceSet &corres, |
| 151 | + const Eigen::Matrix4f &transformation) const { |
| 152 | + if (!source.HasNormals() || !target.HasNormals()) { |
| 153 | + utility::LogWarning( |
| 154 | + "[CorrespondenceCheckerBasedOnNormal::Check] Pointcloud has no " |
| 155 | + "normals."); |
| 156 | + return true; |
| 157 | + } |
| 158 | + float cos_normal_angle_threshold = std::cos(normal_angle_threshold_); |
| 159 | + return thrust::all_of( |
| 160 | + corres.begin(), corres.end(), |
| 161 | + normal_checker_functor( |
| 162 | + cos_normal_angle_threshold, |
| 163 | + thrust::raw_pointer_cast(source.points_.data()), |
| 164 | + thrust::raw_pointer_cast(target.points_.data()), |
| 165 | + thrust::raw_pointer_cast(source.normals_.data()), |
| 166 | + thrust::raw_pointer_cast(target.normals_.data()), |
| 167 | + transformation)); |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace registration |
| 171 | +} // namespace cupoch |
0 commit comments