| Flutter Impeller |

path_component.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
7
8 #include <functional>
9 #include <optional>
10 #include <type_traits>
11 #include <variant>
12 #include <vector>
13
14 #include "impeller/geometry/point.h"
15 #include "impeller/geometry/scalar.h"
16
17 namespace impeller {
18
19 /// @brief An interface for generating a multi contour polyline as a triangle
20 /// strip.
21 class VertexWriter {
22 public:
23 explicit VertexWriter(std::vector<Point>& points,
24 std::vector<uint16_t>& indices);
25
26 ~VertexWriter() = default;
27
28 void EndContour();
29
30 void Write(Point point);
31
32 private:
33 bool previous_contour_odd_points_ = false;
34 size_t contour_start_ = 0u;
35 std::vector<Point>& points_;
36 std::vector<uint16_t>& indices_;
37 };
38
39 struct LinearPathComponent {
40 Point p1;
41 Point p2;
42
43 LinearPathComponent() {}
44
45 LinearPathComponent(Point ap1, Point ap2) : p1(ap1), p2(ap2) {}
46
47 Point Solve(Scalar time) const;
48
49 void AppendPolylinePoints(std::vector<Point>& points) const;
50
51 std::vector<Point> Extrema() const;
52
53 bool operator==(const LinearPathComponent& other) const {
54 return p1 == other.p1 && p2 == other.p2;
55 }
56
57 std::optional<Vector2> GetStartDirection() const;
58
59 std::optional<Vector2> GetEndDirection() const;
60 };
61
62 // A component that represets a Quadratic Bézier curve.
63 struct QuadraticPathComponent {
64 // Start point.
65 Point p1;
66 // Control point.
67 Point cp;
68 // End point.
69 Point p2;
70
71 QuadraticPathComponent() {}
72
73 QuadraticPathComponent(Point ap1, Point acp, Point ap2)
74 : p1(ap1), cp(acp), p2(ap2) {}
75
76 Point Solve(Scalar time) const;
77
78 Point SolveDerivative(Scalar time) const;
79
80 void AppendPolylinePoints(Scalar scale_factor,
81 std::vector<Point>& points) const;
82
83 using PointProc = std::function<void(const Point& point)>;
84
85 void ToLinearPathComponents(Scalar scale_factor, const PointProc& proc) const;
86
87 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
88
89 std::vector<Point> Extrema() const;
90
91 bool operator==(const QuadraticPathComponent& other) const {
92 return p1 == other.p1 && cp == other.cp && p2 == other.p2;
93 }
94
95 std::optional<Vector2> GetStartDirection() const;
96
97 std::optional<Vector2> GetEndDirection() const;
98 };
99
100 // A component that represets a Cubic Bézier curve.
101 struct CubicPathComponent {
102 // Start point.
103 Point p1;
104 // The first control point.
105 Point cp1;
106 // The second control point.
107 Point cp2;
108 // End point.
109 Point p2;
110
111 CubicPathComponent() {}
112
113 explicit CubicPathComponent(const QuadraticPathComponent& q)
114 : p1(q.p1),
115 cp1(q.p1 + (q.cp - q.p1) * (2.0 / 3.0)),
116 cp2(q.p2 + (q.cp - q.p2) * (2.0 / 3.0)),
117 p2(q.p2) {}
118
119 CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
120 : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {}
121
122 Point Solve(Scalar time) const;
123
124 Point SolveDerivative(Scalar time) const;
125
126 void AppendPolylinePoints(Scalar scale, std::vector<Point>& points) const;
127
128 std::vector<Point> Extrema() const;
129
130 using PointProc = std::function<void(const Point& point)>;
131
132 void ToLinearPathComponents(Scalar scale, const PointProc& proc) const;
133
134 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
135
136 CubicPathComponent Subsegment(Scalar t0, Scalar t1) const;
137
138 bool operator==(const CubicPathComponent& other) const {
139 return p1 == other.p1 && cp1 == other.cp1 && cp2 == other.cp2 &&
140 p2 == other.p2;
141 }
142
143 std::optional<Vector2> GetStartDirection() const;
144
145 std::optional<Vector2> GetEndDirection() const;
146
147 private:
148 QuadraticPathComponent Lower() const;
149 };
150
151 struct ContourComponent {
152 Point destination;
153 bool is_closed = false;
154
155 ContourComponent() {}
156
157 explicit ContourComponent(Point p, bool is_closed = false)
158 : destination(p), is_closed(is_closed) {}
159
160 bool operator==(const ContourComponent& other) const {
161 return destination == other.destination && is_closed == other.is_closed;
162 }
163 };
164
165 using PathComponentVariant = std::variant<std::monostate,
166 const LinearPathComponent*,
167 const QuadraticPathComponent*,
168 const CubicPathComponent*>;
169
170 struct PathComponentStartDirectionVisitor {
171 std::optional<Vector2> operator()(const LinearPathComponent* component);
172 std::optional<Vector2> operator()(const QuadraticPathComponent* component);
173 std::optional<Vector2> operator()(const CubicPathComponent* component);
174 std::optional<Vector2> operator()(std::monostate monostate) {
175 return std::nullopt;
176 }
177 };
178
179 struct PathComponentEndDirectionVisitor {
180 std::optional<Vector2> operator()(const LinearPathComponent* component);
181 std::optional<Vector2> operator()(const QuadraticPathComponent* component);
182 std::optional<Vector2> operator()(const CubicPathComponent* component);
183 std::optional<Vector2> operator()(std::monostate monostate) {
184 return std::nullopt;
185 }
186 };
187
188 static_assert(!std::is_polymorphic<LinearPathComponent>::value);
189 static_assert(!std::is_polymorphic<QuadraticPathComponent>::value);
190 static_assert(!std::is_polymorphic<CubicPathComponent>::value);
191
192 } // namespace impeller
193
194 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
point.h
impeller::LinearPathComponent
Definition: path_component.h:39
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::PathComponentStartDirectionVisitor::operator()
std::optional< Vector2 > operator()(std::monostate monostate)
Definition: path_component.h:174
impeller::CubicPathComponent::Subsegment
CubicPathComponent Subsegment(Scalar t0, Scalar t1) const
Definition: path_component.cc:250
impeller::CubicPathComponent::p1
Point p1
Definition: path_component.h:103
impeller::QuadraticPathComponent::operator==
bool operator==(const QuadraticPathComponent &other) const
Definition: path_component.h:91
impeller::LinearPathComponent::p2
Point p2
Definition: path_component.h:41
impeller::QuadraticPathComponent::p1
Point p1
Definition: path_component.h:65
impeller::CubicPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:350
impeller::CubicPathComponent::cp2
Point cp2
Definition: path_component.h:107
impeller::CubicPathComponent::AppendPolylinePoints
void AppendPolylinePoints(Scalar scale, std::vector< Point > &points) const
Definition: path_component.cc:229
impeller::LinearPathComponent::AppendPolylinePoints
void AppendPolylinePoints(std::vector< Point > &points) const
Definition: path_component.cc:122
impeller::VertexWriter::EndContour
void EndContour()
Definition: path_component.cc:17
impeller::QuadraticPathComponent::cp
Point cp
Definition: path_component.h:67
impeller::QuadraticPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:195
impeller::PathComponentEndDirectionVisitor
Definition: path_component.h:179
impeller::ContourComponent::ContourComponent
ContourComponent(Point p, bool is_closed=false)
Definition: path_component.h:157
impeller::CubicPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:215
impeller::PathComponentStartDirectionVisitor::operator()
std::optional< Vector2 > operator()(const LinearPathComponent *component)
Definition: path_component.cc:376
impeller::QuadraticPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:205
impeller::ContourComponent::operator==
bool operator==(const ContourComponent &other) const
Definition: path_component.h:160
impeller::QuadraticPathComponent::ToLinearPathComponents
void ToLinearPathComponents(Scalar scale_factor, const PointProc &proc) const
Definition: path_component.cc:179
impeller::LinearPathComponent::LinearPathComponent
LinearPathComponent()
Definition: path_component.h:43
impeller::QuadraticPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:83
impeller::QuadraticPathComponent::SolveDerivative
Point SolveDerivative(Scalar time) const
Definition: path_component.cc:154
impeller::CubicPathComponent::cp1
Point cp1
Definition: path_component.h:105
impeller::VertexWriter::VertexWriter
VertexWriter(std::vector< Point > &points, std::vector< uint16_t > &indices)
Definition: path_component.cc:13
impeller::LinearPathComponent::p1
Point p1
Definition: path_component.h:40
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent(Point ap1, Point acp, Point ap2)
Definition: path_component.h:73
impeller::LinearPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:133
impeller::ContourComponent::is_closed
bool is_closed
Definition: path_component.h:153
impeller::QuadraticPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:147
impeller::LinearPathComponent::operator==
bool operator==(const LinearPathComponent &other) const
Definition: path_component.h:53
impeller::CubicPathComponent::operator==
bool operator==(const CubicPathComponent &other) const
Definition: path_component.h:138
impeller::CubicPathComponent::SolveDerivative
Point SolveDerivative(Scalar time) const
Definition: path_component.cc:222
impeller::LinearPathComponent::LinearPathComponent
LinearPathComponent(Point ap1, Point ap2)
Definition: path_component.h:45
impeller::PathComponentStartDirectionVisitor
Definition: path_component.h:170
impeller::QuadraticPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:190
impeller::ContourComponent::destination
Point destination
Definition: path_component.h:152
impeller::CubicPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:363
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent(const QuadraticPathComponent &q)
Definition: path_component.h:113
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent()
Definition: path_component.h:111
impeller::CubicPathComponent
Definition: path_component.h:101
scalar.h
impeller::ContourComponent::ContourComponent
ContourComponent()
Definition: path_component.h:155
impeller::CubicPathComponent::ToLinearPathComponents
void ToLinearPathComponents(Scalar scale, const PointProc &proc) const
Definition: path_component.cc:260
impeller::QuadraticPathComponent::AppendPolylinePoints
void AppendPolylinePoints(Scalar scale_factor, std::vector< Point > &points) const
Definition: path_component.cc:171
impeller::VertexWriter
An interface for generating a multi contour polyline as a triangle strip.
Definition: path_component.h:21
impeller::VertexWriter::~VertexWriter
~VertexWriter()=default
impeller::QuadraticPathComponent::p2
Point p2
Definition: path_component.h:69
impeller::TPoint
Definition: point.h:27
impeller::VertexWriter::Write
void Write(Point point)
Definition: path_component.cc:67
impeller::PathComponentEndDirectionVisitor::operator()
std::optional< Vector2 > operator()(const LinearPathComponent *component)
Definition: path_component.cc:400
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent()
Definition: path_component.h:71
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
impeller::CubicPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:130
impeller::CubicPathComponent::p2
Point p2
Definition: path_component.h:109
impeller::CubicPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:332
impeller::PathComponentVariant
std::variant< std::monostate, const LinearPathComponent *, const QuadraticPathComponent *, const CubicPathComponent * > PathComponentVariant
Definition: path_component.h:168
impeller::PathComponentEndDirectionVisitor::operator()
std::optional< Vector2 > operator()(std::monostate monostate)
Definition: path_component.h:183
impeller::ContourComponent
Definition: path_component.h:151
impeller::LinearPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:140
impeller
Definition: aiks_blend_unittests.cc:18
impeller::QuadraticPathComponent
Definition: path_component.h:63
impeller::LinearPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:115
impeller::LinearPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:129
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
Definition: path_component.h:119