-
Notifications
You must be signed in to change notification settings - Fork 20
/
example.cpp
183 lines (142 loc) · 4 KB
/
example.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "dbscan.hpp"
#include <iostream>
#include <string>
#include <system_error>
#include <vector>
#include <utility>
#include <fstream>
#include <charconv>
#include <cassert>
#include <tuple>
#include <cstring>
auto check_from_chars_error(std::errc err, const std::string_view& line, int line_counter)
{
if(err == std::errc())
return;
if(err == std::errc::invalid_argument)
{
std::cerr << "Error: Invalid value \"" << line
<< "\" at line " << line_counter << "\n";
std::exit(1);
}
if(err == std::errc::result_out_of_range)
{
std::cerr << "Error: Value \"" << line << "\"out of range at line "
<< line_counter << "\n";
std::exit(1);
}
}
auto push_values(std::vector<float>& store, const std::string_view& line, int line_counter)
{
auto ptr = line.data();
auto ec = std::errc();
auto n_pushed = 0;
do
{
float value;
auto [p, ec] = std::from_chars(ptr, line.data() + line.size(), value);
ptr = p + 1;
check_from_chars_error(ec, line, line_counter);
n_pushed++;
store.push_back(value);
}while(ptr < line.data() + line.size());
return n_pushed;
}
auto read_values(const std::string& filename)
{
std::ifstream file(filename);
if(not file.good())
{
std::perror(filename.c_str());
std::exit(2);
}
auto count = 0;
auto points = std::vector<float>();
auto dim = 0;
while(not file.eof())
{
count++;
auto line = std::string();
std::getline(file, line);
if(not line.empty())
{
auto n_pushed = push_values(points, line, count);
if(count != 1)
{
if(n_pushed != dim)
{
std::cerr << "Inconsistent number of dimensions at line '" << count << "'\n";
std::exit(1);
}
}
dim = n_pushed;
}
}
return std::tuple(points, dim);
}
template<typename T>
auto to_num(const std::string& str)
{
T value = 0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
if(ec != std::errc())
{
std::cerr << "Error converting value '" << str << "'\n";
std::exit(1);
}
return value;
}
// noise will be labelled as 0
auto label(const std::vector<std::vector<size_t>>& clusters, size_t n)
{
auto flat_clusters = std::vector<size_t>(n);
for(size_t i = 0; i < clusters.size(); i++)
{
for(auto p: clusters[i])
{
flat_clusters[p] = i + 1;
}
}
return flat_clusters;
}
auto dbscan2d(const std::span<const float>& data, float eps, int min_pts)
{
auto points = std::vector<point2>(data.size() / 2);
std::memcpy(points.data(), data.data(), sizeof(float) * data.size());
auto clusters = dbscan(points, eps, min_pts);
auto flat = label (clusters, points.size());
for(size_t i = 0; i < points.size(); i++)
{
std::cout << points[i].x << ',' << points[i].y << ',' << flat[i] << '\n';
}
}
auto dbscan3d(const std::span<const float>& data, float eps, int min_pts)
{
auto points = std::vector<point3>(data.size() / 3);
std::memcpy(points.data(), data.data(), sizeof(float) * data.size());
auto clusters = dbscan(points, eps, min_pts);
auto flat = label (clusters, points.size());
for(size_t i = 0; i < points.size(); i++)
{
std::cout << points[i].x << ',' << points[i].y << ',' << points[i].z << ',' << flat[i] << '\n';
}
}
int main(int argc, char** argv)
{
if(argc != 4)
{
std::cerr << "usage: example <tsv file> <epsilon> <min points>\n";
return 1;
}
auto epsilon = to_num<float>(argv[2]);
auto min_pts = to_num<int> (argv[3]);
auto [values, dim] = read_values(argv[1]);
if(dim == 2)
{
dbscan2d(values, epsilon, min_pts);
}
else if (dim == 3)
{
dbscan3d(values, epsilon, min_pts);
}
}