xref: /aosp_15_r20/external/skia/modules/bentleyottmann/include/EventQueueInterface.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker // Copyright 2023 Google LLC
2*c8dee2aaSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3*c8dee2aaSAndroid Build Coastguard Worker 
4*c8dee2aaSAndroid Build Coastguard Worker #ifndef EventQueueInterface_DEFINED
5*c8dee2aaSAndroid Build Coastguard Worker #define EventQueueInterface_DEFINED
6*c8dee2aaSAndroid Build Coastguard Worker 
7*c8dee2aaSAndroid Build Coastguard Worker #include "modules/bentleyottmann/include/Point.h"
8*c8dee2aaSAndroid Build Coastguard Worker #include "modules/bentleyottmann/include/Segment.h"
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include <set>
11*c8dee2aaSAndroid Build Coastguard Worker 
12*c8dee2aaSAndroid Build Coastguard Worker // The EventQueueInterface and the SweepLineInterface allow the EventQueue and the SweepLine
13*c8dee2aaSAndroid Build Coastguard Worker // to be tested independently of each other. This allows very specific scenarios to be setup and
14*c8dee2aaSAndroid Build Coastguard Worker // tested in isolation.
15*c8dee2aaSAndroid Build Coastguard Worker 
16*c8dee2aaSAndroid Build Coastguard Worker namespace bentleyottmann {
17*c8dee2aaSAndroid Build Coastguard Worker // -- EventQueueInterface --------------------------------------------------------------------------
18*c8dee2aaSAndroid Build Coastguard Worker // An EventQueueInterface implementation must be able to add crossing events into the event queue.
19*c8dee2aaSAndroid Build Coastguard Worker class EventQueueInterface {
20*c8dee2aaSAndroid Build Coastguard Worker public:
21*c8dee2aaSAndroid Build Coastguard Worker     EventQueueInterface() = default;
22*c8dee2aaSAndroid Build Coastguard Worker     EventQueueInterface(const EventQueueInterface&) = default;
23*c8dee2aaSAndroid Build Coastguard Worker     EventQueueInterface(EventQueueInterface&&) = default;
24*c8dee2aaSAndroid Build Coastguard Worker     EventQueueInterface& operator=(const EventQueueInterface&) = default;
25*c8dee2aaSAndroid Build Coastguard Worker     EventQueueInterface& operator=(EventQueueInterface&&) = default;
26*c8dee2aaSAndroid Build Coastguard Worker     virtual ~EventQueueInterface() = default;
27*c8dee2aaSAndroid Build Coastguard Worker 
28*c8dee2aaSAndroid Build Coastguard Worker     virtual void addCrossing(Point crossingPoint, const Segment& s0, const Segment& s1) = 0;
29*c8dee2aaSAndroid Build Coastguard Worker };
30*c8dee2aaSAndroid Build Coastguard Worker 
31*c8dee2aaSAndroid Build Coastguard Worker using DeletionSegmentSet = std::set<Segment>;
32*c8dee2aaSAndroid Build Coastguard Worker struct OrderBySlope {
33*c8dee2aaSAndroid Build Coastguard Worker     bool operator()(const Segment& s0, const Segment& s1) const;
34*c8dee2aaSAndroid Build Coastguard Worker };
35*c8dee2aaSAndroid Build Coastguard Worker // The set of insertion segments is ordered by slope. Since all the lines pass through the same
36*c8dee2aaSAndroid Build Coastguard Worker // point, then the slope of each line must be ordered from smallest to largest to keep the
37*c8dee2aaSAndroid Build Coastguard Worker // segment order correct in the sweep line.
38*c8dee2aaSAndroid Build Coastguard Worker using InsertionSegmentSet = std::set<Segment, OrderBySlope>;
39*c8dee2aaSAndroid Build Coastguard Worker 
40*c8dee2aaSAndroid Build Coastguard Worker // The EventQueue uses an object of SweepLineInterface to find new crossings when manipulating
41*c8dee2aaSAndroid Build Coastguard Worker // the sweep line.
42*c8dee2aaSAndroid Build Coastguard Worker class SweepLineInterface {
43*c8dee2aaSAndroid Build Coastguard Worker public:
44*c8dee2aaSAndroid Build Coastguard Worker     virtual ~SweepLineInterface() = default;
45*c8dee2aaSAndroid Build Coastguard Worker 
46*c8dee2aaSAndroid Build Coastguard Worker     // These are the segments to remove from the sweep line.
47*c8dee2aaSAndroid Build Coastguard Worker     virtual void handleDeletions(Point eventPoint, const DeletionSegmentSet& removing) = 0;
48*c8dee2aaSAndroid Build Coastguard Worker 
49*c8dee2aaSAndroid Build Coastguard Worker     // Insert inserting into the sweep line. Check the inserting segments against the existing
50*c8dee2aaSAndroid Build Coastguard Worker     // sweep line segments and report any crossings using the addCrossing from the
51*c8dee2aaSAndroid Build Coastguard Worker     // EventQueueInterface.
52*c8dee2aaSAndroid Build Coastguard Worker     virtual void handleInsertionsAndCheckForNewCrossings(
53*c8dee2aaSAndroid Build Coastguard Worker             Point eventPoint, const InsertionSegmentSet& inserting, EventQueueInterface* queue) = 0;
54*c8dee2aaSAndroid Build Coastguard Worker };
55*c8dee2aaSAndroid Build Coastguard Worker }  // namespace bentleyottmann
56*c8dee2aaSAndroid Build Coastguard Worker #endif  // EventQueueInterface_DEFINED
57