Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
SweepLine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4#include <queue>
5#include <memory>
6
10
11namespace fishnet::geometry {
12
13enum class EventType{
15};
26template<typename Input, typename Output,util::BiPredicate<Input> SLSLess = std::less<Input>,bool InsertEventsFirst = false, util::BiPredicate<fishnet::math::DEFAULT_NUMERIC> EventQueueGreater = std::greater<fishnet::math::DEFAULT_NUMERIC>>
28public:
29
36 class IEvent{
37 protected:
38 const Input * obj; //non-owning ptr
39 public:
40
41 IEvent(const Input & obj):obj(&obj){}
42
43 virtual void process(SweepLine<Input,Output,SLSLess,InsertEventsFirst,EventQueueGreater> & sweepLine, Output & output)const=0;
44
45 virtual EventType type() const noexcept = 0;
46
47 virtual fishnet::math::DEFAULT_NUMERIC eventPoint() const noexcept = 0;
48
49 const Input & getObject() const noexcept {
50 return *obj;
51 }
52
53 virtual ~IEvent()=default;
54 };
55
56 struct InsertEvent: public IEvent {
57 InsertEvent(const Input & obj):IEvent(obj){}
58 virtual EventType type() const noexcept {
59 return EventType::INSERT;
60 }
61 };
62
63 struct RemoveEvent: public IEvent {
64 RemoveEvent(const Input & obj):IEvent(obj){}
65 virtual EventType type() const noexcept {
66 return EventType::REMOVE;
67 }
68 };
69
72 virtual void process(SweepLine<Input,Output,SLSLess,InsertEventsFirst,EventQueueGreater> & sweepLine, Output & output) const {
73 sweepLine.addSLS(this->obj);
74 }
75 };
76
79 virtual void process(SweepLine<Input,Output,SLSLess,InsertEventsFirst,EventQueueGreater> & sweepLine, Output & output) const {
80 sweepLine.removeSLS(this->obj);
81 }
82 };
83
84 using eventPointer = std::unique_ptr<IEvent>;
85
86private:
87 const static inline EventQueueGreater eventQueueGreater = EventQueueGreater {};
88 const static inline SLSLess slsLess = SLSLess {};
89
94 struct EventQueueGreaterPtr {
95 bool operator()(const eventPointer & lhs, const eventPointer & rhs) const noexcept {
96 if(fishnet::math::areEqual(lhs->eventPoint(),rhs->eventPoint())){
97 if constexpr(InsertEventsFirst)
98 return lhs->type() == EventType::INSERT; // insert events are processed after remove events
99 else {
100 return lhs->type() != EventType::INSERT; // insert events are processed before remove events
101 }
102 }
103 return eventQueueGreater(rhs->eventPoint(), lhs->eventPoint());
104 }
105 };
106
111 struct SLSOrdering {
112 bool operator()(const Input * lhs, const Input * rhs) const noexcept {
113 return slsLess(*lhs,*rhs);
114 }
115 };
116
117public:
118 using SLS = std::set<const Input *,SLSOrdering>;
119 using EventQ = std::priority_queue<eventPointer,std::vector<eventPointer>,EventQueueGreaterPtr>;
120private:
121 SLS sls;
122 EventQ queue;
123public:
124
125 void addEvent(eventPointer event) noexcept {
126 queue.push(std::move(event));
127 }
128
129 void removeEvent(eventPointer const & event) noexcept {
130 queue.erase(event);
131 }
132
134 return queue.top();
135 }
136
137 void addSLS(const Input * input) noexcept {
138 sls.insert(input);
139 }
140
141 void removeSLS(const Input * input) noexcept {
142 sls.erase(input);
143 }
144
145 bool containsSLS(const Input * input) noexcept {
146 return sls.contains(input);
147 }
148
149 const SLS & getSLS() const noexcept {
150 return this->sls;
151 }
152
153 SLS & getSLS() noexcept {
154 return this->sls;
155 }
156
157 auto lowerBound(const Input & input) const noexcept {
158 return this->sls.lower_bound(&input);
159 }
160
161 auto upperBound(const Input & input) const noexcept {
162 return this->sls.upper_bound(&input);
163 }
164
165 const EventQ & getEventQueue() const noexcept {
166 return this->queue;
167 }
168
169 EventQ & getEventQueue() noexcept {
170 return this->queue;
171 }
172
173 void addEvents(util::forward_range_of<eventPointer> auto && events) noexcept {
174 for(auto && event : events) {
175 addEvent(std::move(event));
176 }
177 }
178
179 void addEvents(util::forward_range_of<Input> auto const & objects, util::UnaryFunction<Input,std::vector<eventPointer>> auto eventMapper) noexcept {
180 for(const auto & obj : objects) {
181 for(auto && event : eventMapper(obj)){
182 addEvent(std::move(event));
183 }
184 }
185 }
186
187 Output sweep(Output & output) noexcept {
188 while(not queue.empty()){
189 queue.top()->process(*this,output);
190 queue.pop();
191 }
192 this->sls.clear();
193 return output;
194 }
195};
196}
197
Interface for EventQueue events. process(...) is executed whenever an element is polled from the Even...
Definition SweepLine.hpp:36
const Input & getObject() const noexcept
Definition SweepLine.hpp:49
IEvent(const Input &obj)
Definition SweepLine.hpp:41
virtual fishnet::math::DEFAULT_NUMERIC eventPoint() const noexcept=0
const Input * obj
Definition SweepLine.hpp:38
virtual void process(SweepLine< Input, Output, SLSLess, InsertEventsFirst, EventQueueGreater > &sweepLine, Output &output) const =0
virtual EventType type() const noexcept=0
Generic Sweep Line template Stores a Sweep Line Status (SLS), keeping track of the current state of t...
Definition SweepLine.hpp:27
const EventQ & getEventQueue() const noexcept
Definition SweepLine.hpp:165
void addEvents(util::forward_range_of< Input > auto const &objects, util::UnaryFunction< Input, std::vector< eventPointer > > auto eventMapper) noexcept
Definition SweepLine.hpp:179
auto upperBound(const Input &input) const noexcept
Definition SweepLine.hpp:161
SLS & getSLS() noexcept
Definition SweepLine.hpp:153
eventPointer nextEvent() noexcept
Definition SweepLine.hpp:133
void addEvents(util::forward_range_of< eventPointer > auto &&events) noexcept
Definition SweepLine.hpp:173
EventQ & getEventQueue() noexcept
Definition SweepLine.hpp:169
auto lowerBound(const Input &input) const noexcept
Definition SweepLine.hpp:157
void addSLS(const Input *input) noexcept
Definition SweepLine.hpp:137
bool containsSLS(const Input *input) noexcept
Definition SweepLine.hpp:145
std::unique_ptr< IEvent > eventPointer
Definition SweepLine.hpp:84
void removeSLS(const Input *input) noexcept
Definition SweepLine.hpp:141
Output sweep(Output &output) noexcept
Definition SweepLine.hpp:187
std::priority_queue< eventPointer, std::vector< eventPointer >, EventQueueGreaterPtr > EventQ
Definition SweepLine.hpp:119
std::set< const Input *, SLSOrdering > SLS
Definition SweepLine.hpp:118
const SLS & getSLS() const noexcept
Definition SweepLine.hpp:149
void removeEvent(eventPointer const &event) noexcept
Definition SweepLine.hpp:129
void addEvent(eventPointer event) noexcept
Definition SweepLine.hpp:125
Definition FunctionalConcepts.hpp:9
Definition CollectionConcepts.hpp:27
Definition BoundingBoxWrapper.hpp:5
EventType
Definition SweepLine.hpp:13
constexpr bool areEqual(T lhs, U rhs) noexcept
Definition Constants.hpp:31
Definition BFSClustering.hpp:5
virtual void process(SweepLine< Input, Output, SLSLess, InsertEventsFirst, EventQueueGreater > &sweepLine, Output &output) const
Definition SweepLine.hpp:72
DefaultInsertEvent(const Input &obj)
Definition SweepLine.hpp:71
virtual void process(SweepLine< Input, Output, SLSLess, InsertEventsFirst, EventQueueGreater > &sweepLine, Output &output) const
Definition SweepLine.hpp:79
DefaultRemoveEvent(const Input &obj)
Definition SweepLine.hpp:78
InsertEvent(const Input &obj)
Definition SweepLine.hpp:57
virtual EventType type() const noexcept
Definition SweepLine.hpp:58
RemoveEvent(const Input &obj)
Definition SweepLine.hpp:64
virtual EventType type() const noexcept
Definition SweepLine.hpp:65