cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
descriptor_distance_sorter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <vector>
5
6#include <cpp-toolbox/cpp-toolbox_export.hpp>
8
9namespace toolbox::pcl
10{
11
32template<typename DataType>
33class CPP_TOOLBOX_EXPORT descriptor_distance_sorter_t
35 descriptor_distance_sorter_t<DataType>, DataType>
36{
37public:
38 using base_type =
40 DataType>;
41 using typename base_type::quality_scores_t;
42
45
52 void set_invert_score(bool invert)
53 {
54 m_invert = invert;
55 this->m_cached = false; // 参数改变,清除缓存 / Parameter changed, clear
56 // cache
57 }
58
63 [[nodiscard]] bool get_invert_score() const { return m_invert; }
64
70 void set_normalize(bool normalize)
71 {
72 m_normalize = normalize;
73 this->m_cached = false;
74 }
75
80 [[nodiscard]] bool get_normalize() const { return m_normalize; }
81
82protected:
83 // Friend declaration to allow base class access
85 descriptor_distance_sorter_t<DataType>, DataType>;
86
92 {
93 const auto& corrs = *this->m_correspondences;
94
95 if (m_normalize) {
96 // 找到最大和最小距离用于归一化 / Find max and min distance for
97 // normalization
98 DataType max_distance = 0;
99 DataType min_distance = std::numeric_limits<DataType>::max();
100
101 for (const auto& corr : corrs) {
102 max_distance =
103 std::max(max_distance, static_cast<DataType>(corr.distance));
104 min_distance =
105 std::min(min_distance, static_cast<DataType>(corr.distance));
106 }
107
108 // 避免除零 / Avoid division by zero
109 DataType range = max_distance - min_distance;
110 if (range < std::numeric_limits<DataType>::epsilon()) {
111 range = static_cast<DataType>(1.0);
112 }
113
114 // 计算归一化的质量分数 / Compute normalized quality scores
115 for (std::size_t i = 0; i < corrs.size(); ++i) {
116 DataType normalized =
117 (corrs[i].distance - min_distance) / range; // [0, 1]
118
119 if (m_invert) {
120 // 距离越小,质量越高 / Smaller distance, higher quality
121 scores[i] = static_cast<DataType>(1.0) - normalized;
122 } else {
123 // 距离越大,质量越高(不常用) / Larger distance, higher quality
124 // (uncommon)
125 scores[i] = normalized;
126 }
127 }
128 } else {
129 // 不归一化,直接使用距离 / No normalization, use distance directly
130 for (std::size_t i = 0; i < corrs.size(); ++i) {
131 if (m_invert) {
132 // 使用负距离,这样排序时距离小的会排在前面 / Use negative distance,
133 // so smaller distances rank first
134 scores[i] = -static_cast<DataType>(corrs[i].distance);
135 } else {
136 scores[i] = static_cast<DataType>(corrs[i].distance);
137 }
138 }
139 }
140 }
141
146 [[nodiscard]] std::string get_sorter_name_impl() const
147 {
148 return "DescriptorDistance";
149 }
150
151private:
152 bool m_invert = true;
154 bool m_normalize = true;
155};
156
157} // namespace toolbox::pcl
对应关系排序器的基类(CRTP模式) / Base class for correspondence sorters (CRTP pattern)
Definition base_correspondence_sorter.hpp:51
std::vector< DataType > quality_scores_t
Definition base_correspondence_sorter.hpp:56
基于描述子距离的对应关系排序器 / Descriptor distance-based correspondence sorter
Definition descriptor_distance_sorter.hpp:36
void set_invert_score(bool invert)
设置是否反转分数(距离越小质量越高) / Set whether to invert score (smaller distance means higher quality)
Definition descriptor_distance_sorter.hpp:52
bool get_invert_score() const
获取是否反转分数 / Get whether to invert score
Definition descriptor_distance_sorter.hpp:63
void compute_quality_scores_impl(quality_scores_t &scores)
计算质量分数的实现 / Implementation of quality score computation
Definition descriptor_distance_sorter.hpp:91
void set_normalize(bool normalize)
设置归一化方式 / Set normalization method
Definition descriptor_distance_sorter.hpp:70
std::string get_sorter_name_impl() const
获取排序器名称实现 / Get sorter name implementation
Definition descriptor_distance_sorter.hpp:146
Definition base_correspondence_generator.hpp:18