Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Myers.h
Go to the documentation of this file.
1// Copyright 2023 Google LLC
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4#ifndef Myers_DEFINED
5#define Myers_DEFINED
6
9
10#include <algorithm>
11#include <cstddef>
12#include <cstdint>
13#include <tuple>
14#include <vector>
15
16namespace myers {
17
18// -- Point ----------------------------------------------------------------------------------------
19struct Point {
20 int32_t x = 0;
21 int32_t y = 0;
22};
23
24// Return p0 and p1 in the correct order for a Segment.
25constexpr bool operator<(const Point& p0, const Point& p1) {
26 return std::tie(p0.y, p0.x) < std::tie(p1.y, p1.x);
27}
28
29constexpr bool operator==(const Point& p0, const Point& p1) {
30 return std::tie(p0.x, p0.y) == std::tie(p1.x, p1.y);
31}
32
33constexpr bool operator!=(const Point& p0, const Point& p1) {
34 return std::tie(p0.x, p0.y) != std::tie(p1.x, p1.y);
35}
36
37// -- Segment --------------------------------------------------------------------------------------
38// A Segment is an undirected edge where the points have an order u.y < l.y else
39// if (u.y == l.y) u.x < x.y.
40class Segment {
41public:
42 constexpr Segment(Point p0, Point p1)
43 : Segment{std::minmax(p0, p1)} {}
44
45 const Point& upper() const;
46 const Point& lower() const;
47 std::tuple<int32_t, int32_t, int32_t, int32_t> bounds() const;
48 bool isHorizontal() const;
49 bool isVertical() const;
50 friend constexpr bool operator<(const Segment& s0, const Segment& s1);
51 friend constexpr bool operator==(const Segment& s0, const Segment& s1);
52 friend constexpr bool operator!=(const Segment& s0, const Segment& s1);
53
54private:
55 constexpr Segment(const std::tuple<Point, Point>& ps)
56 : fUpper{std::get<0>(ps)}
57 , fLower{std::get<1>(ps)} {
58 SkASSERT(fUpper != fLower);
59 SkASSERT(fUpper < fLower);
60 }
61
62 Point fUpper;
63 Point fLower;
64};
65
66constexpr bool operator<(const Segment& s0, const Segment& s1) {
67 return std::tie(s0.fUpper, s0.fLower) < std::tie(s1.fUpper, s1.fLower);
68}
69
70constexpr bool operator==(const Segment& s0, const Segment& s1) {
71 return std::tie(s0.fUpper, s0.fLower) == std::tie(s1.fUpper, s1.fLower);
72}
73
74constexpr bool operator!=(const Segment& s0, const Segment& s1) {
75 return std::tie(s0.fUpper, s0.fLower) != std::tie(s1.fUpper, s1.fLower);
76}
77
78// Support for Segment as a tuple.
79template<size_t> const myers::Point& get(const myers::Segment&);
80template<> inline const myers::Point& get<0>(const myers::Segment& s) { return s.upper(); }
81template<> inline const myers::Point& get<1>(const myers::Segment& s) { return s.lower(); }
82
83// -- Crossing -------------------------------------------------------------------------------------
84class Crossing {
85public:
86 Crossing(const Segment& s0, const Segment& s1) : Crossing{std::minmax(s0, s1)} {}
87 friend bool operator<(const Crossing& c0, const Crossing& c1);
88 friend bool operator==(const Crossing& c0, const Crossing& c1);
89
90private:
91 Crossing(std::tuple<Segment, Segment> highLow)
92 : fHigher{std::get<0>(highLow)}
93 , fLower{std::get<1>(highLow)} {}
94
95 Segment fHigher;
96 Segment fLower;
97};
98
99inline bool operator<(const Crossing& c0, const Crossing& c1) {
100 return std::tie(c0.fHigher, c0.fLower) < std::tie(c1.fHigher, c1.fLower);
101}
102
103inline bool operator==(const Crossing& c0, const Crossing& c1) {
104 return std::tie(c0.fHigher, c0.fLower) == std::tie(c1.fHigher, c1.fLower);
105}
106
107std::vector<Crossing> myers_find_crossings(const SkSpan<const Segment> segments);
108std::vector<Crossing> brute_force_crossings(SkSpan<Segment>);
109} // namespace myers
110
111// Support for Segment as a tuple. Must be in top-level namespace.
112template<> struct std::tuple_size<myers::Segment> {
113 static constexpr int value = 2;
114};
115
116template<size_t Index> struct std::tuple_element<Index, myers::Segment> {
117 using type = const myers::Point&;
118};
119#endif // Myers_DEFINED
#define SkASSERT(cond)
Definition SkAssert.h:116
friend bool operator==(const Crossing &c0, const Crossing &c1)
Definition Myers.h:103
friend bool operator<(const Crossing &c0, const Crossing &c1)
Definition Myers.h:99
Crossing(const Segment &s0, const Segment &s1)
Definition Myers.h:86
const Point & upper() const
Definition Myers.cpp:31
std::tuple< int32_t, int32_t, int32_t, int32_t > bounds() const
Definition Myers.cpp:39
friend constexpr bool operator<(const Segment &s0, const Segment &s1)
Definition Myers.h:66
friend constexpr bool operator!=(const Segment &s0, const Segment &s1)
Definition Myers.h:74
friend constexpr bool operator==(const Segment &s0, const Segment &s1)
Definition Myers.h:70
constexpr Segment(Point p0, Point p1)
Definition Myers.h:42
bool isVertical() const
Definition Myers.cpp:48
bool isHorizontal() const
Definition Myers.cpp:44
const Point & lower() const
Definition Myers.cpp:35
struct MyStruct s
uint8_t value
std::vector< Crossing > brute_force_crossings(SkSpan< Segment >)
Definition Myers.cpp:638
const myers::Point & get< 1 >(const myers::Segment &s)
Definition Myers.h:81
constexpr bool operator<(const Point &p0, const Point &p1)
Definition Myers.h:25
constexpr bool operator!=(const Point &p0, const Point &p1)
Definition Myers.h:33
const myers::Point & get< 0 >(const myers::Segment &s)
Definition Myers.h:80
const myers::Point & get(const myers::Segment &)
std::vector< Crossing > myers_find_crossings(const SkSpan< const Segment > segments)
Definition Myers.cpp:565
constexpr bool operator==(const Point &p0, const Point &p1)
Definition Myers.h:29
Definition ref_ptr.h:256
int32_t x
Definition Myers.h:20
int32_t y
Definition Myers.h:21