-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistrator.h
215 lines (174 loc) · 6.08 KB
/
registrator.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef REGISTRATOR_FOR_SEGMENT_INTERSECTION
#define REGISTRATOR_FOR_SEGMENT_INTERSECTION
/*
*
* Copyright (c) 2011-2020 Ivan Balaban
*
This file is part of Seg_int library.
Seg_int is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Seg_int is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Seg_int. If not, see <http://www.gnu.org/licenses/>.
*/
// NEW IMPLEMENTATION
#include <algorithm>
#include <tuple>
#include <vector>
#include "utils.h"
enum _RegistrationType
{
count = 1,
segments = 2,
point = 4,
count_and_segments = 3,
full = 7
};
template <class ipoint>
class JustCountingRegistrator
{
double counter = 0;
public:
static constexpr uint4 reg_type = _RegistrationType::count;
void register_pair(uint4 s1, uint4 s2) noexcept {
#ifdef PRINT_SEG_AND_INT
if (s1 < s2)
printf("alt int %i %i\n", s1, s2);
else
printf("alt int %i %i\n", s2, s1);
#endif
++counter;
};
void register_pair_and_point(uint4 s1, uint4 s2, const ipoint& p) { register_pair(s1, s2); };
void combine_reg_data(uint4 n_threads, JustCountingRegistrator* additional_reg_obj[])
{
for (int i = 0; i < n_threads - 1; i++)
counter += additional_reg_obj[i]->counter;
};
void Alloc(uint4 _N) {};
// to return some statistics about registered intersections;
double get_stat(uint4 stat_type = 0) { return counter; };
void write_SVG(uint4 alg, chostream* SVG_text) {};
};
template <class ipoint>
class PerSegmCountingRegistrator
{
uint4 N;
uint4* segm_counters = nullptr;
double counter = 0;
public:
static constexpr uint4 reg_type = _RegistrationType::count + _RegistrationType::segments;
~PerSegmCountingRegistrator() { if (segm_counters != nullptr) { delete[] segm_counters; segm_counters = nullptr; } };
void register_pair(uint4 s1, uint4 s2) noexcept {
++counter;
++segm_counters[s1];
++segm_counters[s2];
};
void register_pair_and_point(uint4 s1, uint4 s2, const ipoint& p) { register_pair(s1, s2); };
void Alloc(uint4 _N)
{
N = _N;
segm_counters = new uint4[N];
std::memset(segm_counters, 0, N * sizeof(*segm_counters));
};
void combine_reg_data(uint4 n_threads, PerSegmCountingRegistrator* to_add[])
{
for (uint4 r = 0; r < n_threads - 1; ++r)
{
counter += to_add[r]->counter;
uint4* added = to_add[r]->segm_counters;
for (uint4 i = 0; i < N; ++i)
segm_counters[i] += added[i];
}
};
// to return some statistics about registered intersections;
double get_stat(uint4 stat_type = 0)
{
// if stat_type nonzero return maximal number of intersections per segment;
if (stat_type == _Registrator::per_segm_reg_max_per_segm_stat)
return *std::max_element(segm_counters, segm_counters + N);
return counter;
};
void write_SVG(uint4 alg, chostream* SVG_text) {};
};
template <class ipoint>
class PairAndPointRegistrator
{
using int_rec = std::tuple<uint4, uint4, ipoint>;
std::vector<int_rec> intersections;
double counter = 0;
public:
static constexpr uint4 reg_type = _RegistrationType::count + _RegistrationType::segments + _RegistrationType::point;
PairAndPointRegistrator() {
};
~PairAndPointRegistrator() {
};
void register_pair(uint4 s1, uint4 s2) noexcept {
if (counter == 0)
intersections.reserve(1024 * 1024);
++counter;
};
void register_pair_and_point(uint4 s1, uint4 s2, const ipoint& p) {
if (counter == 0)
intersections.reserve(1024 * 1024);
++counter;
if (s1 < s2)
intersections.emplace_back(s1, s2, p);
else
intersections.emplace_back(s2, s1, p);
};
void Alloc(uint4) {};
void combine_reg_data(uint4 n_threads, PairAndPointRegistrator* to_add[])
{
auto tot = intersections.size();
for (uint4 r = 0; r < n_threads - 1; ++r) {
counter += to_add[r]->counter;
tot += to_add[r]->intersections.size();
}
intersections.reserve(tot);
for (uint4 r = 0; r < n_threads - 1; ++r)
{
auto &added = to_add[r]->intersections;
std::move(added.begin(), added.end(), std::back_inserter(intersections));
// intersections.insert(intersections.end(), added.begin(), added.end());
}
};
// to return some statistics about registered intersections;
double get_stat(uint4 stat_type = 0)
{
return counter;
};
void write_SVG(uint4 alg, chostream* SVG_text) {
if (SVG_text) {
for (uint4 i = 0; i < MIN(intersections.size(), max_SVG_points); ++i) {
uint4 s1 = std::get<0>(intersections[i]);
uint4 s2 = std::get<1>(intersections[i]);
auto& p = std::get<2>(intersections[i]);
*SVG_text << "<circle cx='" << p.getX();
*SVG_text << "' cy='" << p.getY();
switch (alg)
{
case _Algorithm::triv:*SVG_text << "' class='triv' id='t" << s1<<"_"<<s2<<"' />\n"; break;
case _Algorithm::simple_sweep:*SVG_text << "' class='ssw' id='s" << s1<<"_"<<s2<<"' />\n"; break;
case _Algorithm::fast:*SVG_text << "' class='fast' id='f" << s1<<"_"<<s2<<"' />\n"; break;
case _Algorithm::optimal:*SVG_text << "' class='optimal' id='o" << s1<<"_"<<s2<<"' />\n"; break;
case _Algorithm::fast_parallel:*SVG_text << "' class='parallel' id='p" << s1<<"_"<<s2<<"' />\n"; break;
case _Algorithm::mem_save:*SVG_text << "' class='mem_save' id='m" << s1<<"_"<<s2<<"' />\n"; break;
default:
*SVG_text << "' class='algorithm' id='a" << s1<<"_"<<s2<<"' />\n";
break;
}
}
};
}
};
using SimpleCounter=JustCountingRegistrator<TPlaneVect> ;
using PerSegmCounter=PerSegmCountingRegistrator<TPlaneVect> ;
using TrueRegistrator = PairAndPointRegistrator<TPlaneVect> ;
#endif // !REGISTRATOR_FOR_SEGMENT_INTERSECTION