代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
#include<vector>
#include <memory>
#include <cmath>
#include <random>
#include <algorithm>
#include <chrono>

class Cluster
{
public:
struct Options
{
struct Threshold
{
double distance = 2.0;
double theta = 10.0;
};
Threshold threshold = {};
};
struct Pose
{
double x = 0.0;
double y = 0.0;
double theta = 0.0;
};
public:
Options m_options;

public :
Cluster(const Options &options, std::vector<Pose> &pose_list);
~Cluster();
void Run();
void print_cluster();
protected:
std::vector<Pose> m_pose_list;
// 数据矩阵保存聚类结果(pose, cluster_id)
std::vector<std::pair<Pose, uint>> m_cluster_result;
};

Cluster::Cluster(const Options &options, std::vector<Pose> &pose_list)
: m_options(options)
, m_pose_list(pose_list)
{
Run();
}

Cluster::~Cluster()
{
}

void Cluster::Run()
{
// 初始化聚类结果
m_cluster_result.resize(m_pose_list.size());
for (uint i = 0; i < m_pose_list.size(); ++i)
{
m_cluster_result[i].first = m_pose_list[i];
m_cluster_result[i].second = i;
}

// 初始化聚类结果
std::vector<uint> cluster_id_list(m_pose_list.size(), 0);
uint cluster_id = 0;
for (uint i = 0; i < m_pose_list.size(); ++i)
{
if (cluster_id_list[i] == 0)
{
cluster_id_list[i] = cluster_id;
cluster_id++;
for (uint j = i + 1; j < m_pose_list.size(); ++j)
{
if (cluster_id_list[j] == 0)
{
double distance = std::hypot(m_pose_list[i].x - m_pose_list[j].x, m_pose_list[i].y - m_pose_list[j].y);
double theta = std::abs(m_pose_list[i].theta - m_pose_list[j].theta);
if (distance < m_options.threshold.distance && theta < m_options.threshold.theta)
{
cluster_id_list[j] = cluster_id_list[i];
}
}
}
}
}
// 检查cluster_id_list是否存在阈值交集,并合并交集
for (uint i = 0; i < m_pose_list.size(); ++i)
{
for(uint j = i + 1; j < m_pose_list.size(); ++j)
{
double distance = std::hypot(m_pose_list[i].x - m_pose_list[j].x, m_pose_list[i].y - m_pose_list[j].y);
double theta = std::abs(m_pose_list[i].theta - m_pose_list[j].theta);
if (distance < m_options.threshold.distance && theta < m_options.threshold.theta)
{
if (cluster_id_list[i] != cluster_id_list[j])
{
for (uint k = 0; k < m_pose_list.size(); ++k)
{
if (cluster_id_list[k] == cluster_id_list[j])
{
cluster_id_list[k] = cluster_id_list[i];
}
}
}
}
}
}

// 更新聚类结果
for (uint i = 0; i < m_pose_list.size(); ++i)
{
m_cluster_result[i].second = cluster_id_list[i];
}
// 按照聚类结果排序
// std::sort(m_cluster_result.begin(), m_cluster_result.end(), [](const std::pair<Pose, uint> &a, const std::pair<Pose, uint> &b) {
// return a.second < b.second;
// });

// 打印
print_cluster();
}

void Cluster::print_cluster()
{
std::cout << "cluster result: " << std::endl;
for (uint i = 0; i < m_cluster_result.size(); ++i)
{
std::cout << "pose: " << m_cluster_result[i].first.x << " " << m_cluster_result[i].first.y << " " << m_cluster_result[i].first.theta << " " << "cluster_id: " << m_cluster_result[i].second << std::endl;
}
// 判断是否聚类
bool is_cluster = true;
for (auto cluster_id : m_cluster_result)
{
if (cluster_id.second != m_cluster_result.front().second)
{
is_cluster = false;
break;
}
}
std::cout << "is_cluster: " << is_cluster << std::endl;
}

int main()
{
std::vector<Cluster::Pose> pose_list;
// 随机生成一组pose_list
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 10);
for (int i = 0; i < 10; i++)
{
Cluster::Pose pose;
pose = {dis(gen), dis(gen), dis(gen)};
pose_list.push_back(pose);
}
Cluster::Options options;
options.threshold.distance = 5;
options.threshold.theta = 50;

Cluster cluster(options, pose_list);


return 0;
}