|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// - Open3D: www.open3d.org - |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// Copyright (c) 2018-2024 www.open3d.org |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | +// ---------------------------------------------------------------------------- |
| 7 | + |
| 8 | +#include "open3d/pipelines/registration/SymmetricICP.h" |
| 9 | + |
| 10 | +#include <tbb/blocked_range.h> |
| 11 | +#include <tbb/parallel_reduce.h> |
| 12 | + |
| 13 | +#include <cmath> |
| 14 | +#include <cstddef> |
| 15 | + |
| 16 | +#include "open3d/geometry/PointCloud.h" |
| 17 | +#include "open3d/pipelines/registration/SymmetricICPImpl.h" |
| 18 | +#include "open3d/utility/Eigen.h" |
| 19 | +#include "open3d/utility/Logging.h" |
| 20 | + |
| 21 | +namespace open3d { |
| 22 | +namespace pipelines { |
| 23 | +namespace registration { |
| 24 | +namespace { |
| 25 | + |
| 26 | +void ValidateSymmetricICPNormals(const geometry::PointCloud &source, |
| 27 | + const geometry::PointCloud &target) { |
| 28 | + if (!source.HasNormals() || !target.HasNormals()) { |
| 29 | + utility::LogError( |
| 30 | + "SymmetricICP requires both source and target to have " |
| 31 | + "normals."); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void ValidateSymmetricICPCorrespondences(const geometry::PointCloud &source, |
| 36 | + const geometry::PointCloud &target, |
| 37 | + const CorrespondenceSet &corres) { |
| 38 | + for (const Eigen::Vector2i &correspondence : corres) { |
| 39 | + if (correspondence[0] < 0 || correspondence[1] < 0 || |
| 40 | + static_cast<std::size_t>(correspondence[0]) >= |
| 41 | + source.points_.size() || |
| 42 | + static_cast<std::size_t>(correspondence[1]) >= |
| 43 | + target.points_.size()) { |
| 44 | + utility::LogError( |
| 45 | + "SymmetricICP correspondence ({}, {}) is out of range for " |
| 46 | + "source size {} and target size {}.", |
| 47 | + correspondence[0], correspondence[1], source.points_.size(), |
| 48 | + target.points_.size()); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +Eigen::Vector3d GetSymmetricNormal(const Eigen::Vector3d &source_normal, |
| 54 | + const Eigen::Vector3d &target_normal) { |
| 55 | + if (source_normal.dot(target_normal) < 0.0) { |
| 56 | + return target_normal - source_normal; |
| 57 | + } |
| 58 | + return target_normal + source_normal; |
| 59 | +} |
| 60 | + |
| 61 | +struct CorrespondenceSums { |
| 62 | + Eigen::Vector3d source = Eigen::Vector3d::Zero(); |
| 63 | + Eigen::Vector3d target = Eigen::Vector3d::Zero(); |
| 64 | +}; |
| 65 | + |
| 66 | +struct NormalEquations { |
| 67 | + Eigen::Matrix6d JTJ = Eigen::Matrix6d::Zero(); |
| 68 | + Eigen::Vector6d JTr = Eigen::Vector6d::Zero(); |
| 69 | +}; |
| 70 | + |
| 71 | +} // namespace |
| 72 | + |
| 73 | +double TransformationEstimationSymmetric::ComputeRMSE( |
| 74 | + const geometry::PointCloud &source, |
| 75 | + const geometry::PointCloud &target, |
| 76 | + const CorrespondenceSet &corres) const { |
| 77 | + ValidateSymmetricICPNormals(source, target); |
| 78 | + ValidateSymmetricICPCorrespondences(source, target, corres); |
| 79 | + if (corres.empty()) { |
| 80 | + return 0.0; |
| 81 | + } |
| 82 | + |
| 83 | + double err = 0.0; |
| 84 | + for (const auto &c : corres) { |
| 85 | + const Eigen::Vector3d &source_point = source.points_[c[0]]; |
| 86 | + const Eigen::Vector3d &target_point = target.points_[c[1]]; |
| 87 | + const Eigen::Vector3d normal = GetSymmetricNormal( |
| 88 | + source.normals_[c[0]], target.normals_[c[1]]); |
| 89 | + const double residual = (source_point - target_point).dot(normal); |
| 90 | + err += residual * residual; |
| 91 | + } |
| 92 | + return std::sqrt(err / static_cast<double>(corres.size())); |
| 93 | +} |
| 94 | + |
| 95 | +Eigen::Matrix4d TransformationEstimationSymmetric::ComputeTransformation( |
| 96 | + const geometry::PointCloud &source, |
| 97 | + const geometry::PointCloud &target, |
| 98 | + const CorrespondenceSet &corres) const { |
| 99 | + ValidateSymmetricICPNormals(source, target); |
| 100 | + ValidateSymmetricICPCorrespondences(source, target, corres); |
| 101 | + if (corres.empty()) { |
| 102 | + return Eigen::Matrix4d::Identity(); |
| 103 | + } |
| 104 | + |
| 105 | + const CorrespondenceSums sums = tbb::parallel_reduce( |
| 106 | + tbb::blocked_range<std::size_t>(0, corres.size()), |
| 107 | + CorrespondenceSums(), |
| 108 | + [&](const tbb::blocked_range<std::size_t> &range, |
| 109 | + CorrespondenceSums local) { |
| 110 | + for (std::size_t i = range.begin(); i != range.end(); ++i) { |
| 111 | + local.source += source.points_[corres[i][0]]; |
| 112 | + local.target += target.points_[corres[i][1]]; |
| 113 | + } |
| 114 | + return local; |
| 115 | + }, |
| 116 | + [](CorrespondenceSums lhs, const CorrespondenceSums &rhs) { |
| 117 | + lhs.source += rhs.source; |
| 118 | + lhs.target += rhs.target; |
| 119 | + return lhs; |
| 120 | + }); |
| 121 | + const double inverse_count = 1.0 / static_cast<double>(corres.size()); |
| 122 | + const Eigen::Vector3d source_mean = sums.source * inverse_count; |
| 123 | + const Eigen::Vector3d target_mean = sums.target * inverse_count; |
| 124 | + |
| 125 | + // Centering the correspondences decouples the symmetric rotation and |
| 126 | + // translation system while the robust weight uses the raw residual. |
| 127 | + const NormalEquations equations = tbb::parallel_reduce( |
| 128 | + tbb::blocked_range<std::size_t>(0, corres.size()), |
| 129 | + NormalEquations(), |
| 130 | + [&](const tbb::blocked_range<std::size_t> &range, |
| 131 | + NormalEquations local) { |
| 132 | + for (std::size_t i = range.begin(); i != range.end(); ++i) { |
| 133 | + const Eigen::Vector3d &source_point = |
| 134 | + source.points_[corres[i][0]]; |
| 135 | + const Eigen::Vector3d &target_point = |
| 136 | + target.points_[corres[i][1]]; |
| 137 | + const Eigen::Vector3d normal = |
| 138 | + GetSymmetricNormal(source.normals_[corres[i][0]], |
| 139 | + target.normals_[corres[i][1]]); |
| 140 | + const Eigen::Vector3d source_centered = |
| 141 | + source_point - source_mean; |
| 142 | + const Eigen::Vector3d target_centered = |
| 143 | + target_point - target_mean; |
| 144 | + const double raw_residual = |
| 145 | + (source_point - target_point).dot(normal); |
| 146 | + const double residual = |
| 147 | + (source_centered - target_centered).dot(normal); |
| 148 | + |
| 149 | + Eigen::Vector6d jacobian; |
| 150 | + jacobian.head<3>() = |
| 151 | + (source_centered + target_centered).cross(normal); |
| 152 | + jacobian.tail<3>() = normal; |
| 153 | + |
| 154 | + const double weight = kernel_->Weight(raw_residual); |
| 155 | + local.JTJ.noalias() += |
| 156 | + weight * jacobian * jacobian.transpose(); |
| 157 | + local.JTr.noalias() += weight * jacobian * residual; |
| 158 | + } |
| 159 | + return local; |
| 160 | + }, |
| 161 | + [](NormalEquations lhs, const NormalEquations &rhs) { |
| 162 | + lhs.JTJ += rhs.JTJ; |
| 163 | + lhs.JTr += rhs.JTr; |
| 164 | + return lhs; |
| 165 | + }); |
| 166 | + |
| 167 | + bool is_success = false; |
| 168 | + Eigen::Vector6d pose; |
| 169 | + std::tie(is_success, pose) = |
| 170 | + utility::SolveLinearSystemPSD(equations.JTJ, -equations.JTr); |
| 171 | + return is_success ? TransformSymmetricPoseToMatrix4d(pose, source_mean, |
| 172 | + target_mean) |
| 173 | + : Eigen::Matrix4d::Identity(); |
| 174 | +} |
| 175 | + |
| 176 | +std::tuple<std::shared_ptr<const geometry::PointCloud>, |
| 177 | + std::shared_ptr<const geometry::PointCloud>> |
| 178 | +TransformationEstimationSymmetric::InitializePointCloudsForTransformation( |
| 179 | + const geometry::PointCloud &source, |
| 180 | + const geometry::PointCloud &target, |
| 181 | + double max_correspondence_distance) const { |
| 182 | + ValidateSymmetricICPNormals(source, target); |
| 183 | + std::shared_ptr<const geometry::PointCloud> source_initialized_c( |
| 184 | + &source, [](const geometry::PointCloud *) {}); |
| 185 | + std::shared_ptr<const geometry::PointCloud> target_initialized_c( |
| 186 | + &target, [](const geometry::PointCloud *) {}); |
| 187 | + if (!source_initialized_c || !target_initialized_c) { |
| 188 | + utility::LogError( |
| 189 | + "Internal error: InitializePointCloudsFor" |
| 190 | + "Transformation returns nullptr."); |
| 191 | + } |
| 192 | + return std::make_tuple(source_initialized_c, target_initialized_c); |
| 193 | +} |
| 194 | + |
| 195 | +RegistrationResult RegistrationSymmetricICP( |
| 196 | + const geometry::PointCloud &source, |
| 197 | + const geometry::PointCloud &target, |
| 198 | + double max_correspondence_distance, |
| 199 | + const Eigen::Matrix4d &init, |
| 200 | + const TransformationEstimationSymmetric &estimation, |
| 201 | + const ICPConvergenceCriteria &criteria) { |
| 202 | + return RegistrationICP(source, target, max_correspondence_distance, init, |
| 203 | + estimation, criteria); |
| 204 | +} |
| 205 | + |
| 206 | +} // namespace registration |
| 207 | +} // namespace pipelines |
| 208 | +} // namespace open3d |
0 commit comments