| 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 <vector>
12
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/scalar.h"
15
16 namespace impeller {
17
18 /// @brief An interface for generating a multi contour polyline as a triangle
19 /// strip.
20 class VertexWriter {
21 public:
22 explicit VertexWriter(std::vector<Point>& points,
23 std::vector<uint16_t>& indices);
24
25 ~VertexWriter() = default;
26
27 void EndContour();
28
29 void Write(Point point);
30
31 private:
32 bool previous_contour_odd_points_ = false;
33 size_t contour_start_ = 0u;
34 std::vector<Point>& points_;
35 std::vector<uint16_t>& indices_;
36 };
37
38 struct LinearPathComponent {
39 Point p1;
40 Point p2;
41
42 LinearPathComponent() {}
43
44 LinearPathComponent(Point ap1, Point ap2) : p1(ap1), p2(ap2) {}
45
46 Point Solve(Scalar time) const;
47
48 void AppendPolylinePoints(std::vector<Point>& points) const;
49
50 std::vector<Point> Extrema() const;
51
52 bool operator==(const LinearPathComponent& other) const {
53 return p1 == other.p1 && p2 == other.p2;
54 }
55
56 std::optional<Vector2> GetStartDirection() const;
57
58 std::optional<Vector2> GetEndDirection() const;
59 };
60
61 // A component that represets a Quadratic Bézier curve.
62 struct QuadraticPathComponent {
63 // Start point.
64 Point p1;
65 // Control point.
66 Point cp;
67 // End point.
68 Point p2;
69
70 QuadraticPathComponent() {}
71
72 QuadraticPathComponent(Point ap1, Point acp, Point ap2)
73 : p1(ap1), cp(acp), p2(ap2) {}
74
75 Point Solve(Scalar time) const;
76
77 Point SolveDerivative(Scalar time) const;
78
79 void AppendPolylinePoints(Scalar scale_factor,
80 std::vector<Point>& points) const;
81
82 using PointProc = std::function<void(const Point& point)>;
83
84 void ToLinearPathComponents(Scalar scale_factor, const PointProc& proc) const;
85
86 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
87
88 std::vector<Point> Extrema() const;
89
90 bool operator==(const QuadraticPathComponent& other) const {
91 return p1 == other.p1 && cp == other.cp && p2 == other.p2;
92 }
93
94 std::optional<Vector2> GetStartDirection() const;
95
96 std::optional<Vector2> GetEndDirection() const;
97 };
98
99 // A component that represets a Cubic Bézier curve.
100 struct CubicPathComponent {
101 // Start point.
102 Point p1;
103 // The first control point.
104 Point cp1;
105 // The second control point.
106 Point cp2;
107 // End point.
108 Point p2;
109
110 CubicPathComponent() {}
111
112 explicit CubicPathComponent(const QuadraticPathComponent& q)
113 : p1(q.p1),
114 cp1(q.p1 + (q.cp - q.p1) * (2.0 / 3.0)),
115 cp2(q.p2 + (q.cp - q.p2) * (2.0 / 3.0)),
116 p2(q.p2) {}
117
118 CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
119 : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {}
120
121 Point Solve(Scalar time) const;
122
123 Point SolveDerivative(Scalar time) const;
124
125 void AppendPolylinePoints(Scalar scale, std::vector<Point>& points) const;
126
127 std::vector<Point> Extrema() const;
128
129 using PointProc = std::function<void(const Point& point)>;
130
131 void ToLinearPathComponents(Scalar scale, const PointProc& proc) const;
132
133 void ToLinearPathComponents(Scalar scale, VertexWriter& writer) const;
134
135 CubicPathComponent Subsegment(Scalar t0, Scalar t1) const;
136
137 bool operator==(const CubicPathComponent& other) const {
138 return p1 == other.p1 && cp1 == other.cp1 && cp2 == other.cp2 &&
139 p2 == other.p2;
140 }
141
142 std::optional<Vector2> GetStartDirection() const;
143
144 std::optional<Vector2> GetEndDirection() const;
145
146 private:
147 QuadraticPathComponent Lower() const;
148 };
149
150 struct ContourComponent {
151 Point destination;
152
153 // 0, 0 for closed, anything else for open.
154 Point closed = Point(1, 1);
155
156 ContourComponent() {}
157
158 constexpr bool IsClosed() const { return closed == Point{0, 0}; }
159
160 explicit ContourComponent(Point p, Point closed)
161 : destination(p), closed(closed) {}
162
163 bool operator==(const ContourComponent& other) const {
164 return destination == other.destination && IsClosed() == other.IsClosed();
165 }
166 };
167
168 static_assert(!std::is_polymorphic<LinearPathComponent>::value);
169 static_assert(!std::is_polymorphic<QuadraticPathComponent>::value);
170 static_assert(!std::is_polymorphic<CubicPathComponent>::value);
171
172 } // namespace impeller
173
174 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
point.h
impeller::LinearPathComponent
Definition: path_component.h:38
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::CubicPathComponent::Subsegment
CubicPathComponent Subsegment(Scalar t0, Scalar t1) const
Definition: path_component.cc:250
impeller::CubicPathComponent::p1
Point p1
Definition: path_component.h:102
impeller::QuadraticPathComponent::operator==
bool operator==(const QuadraticPathComponent &other) const
Definition: path_component.h:90
impeller::LinearPathComponent::p2
Point p2
Definition: path_component.h:40
impeller::QuadraticPathComponent::p1
Point p1
Definition: path_component.h:64
impeller::CubicPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:350
impeller::CubicPathComponent::cp2
Point cp2
Definition: path_component.h:106
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:66
impeller::QuadraticPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:195
impeller::CubicPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:215
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:163
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:42
impeller::QuadraticPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:82
impeller::QuadraticPathComponent::SolveDerivative
Point SolveDerivative(Scalar time) const
Definition: path_component.cc:154
impeller::CubicPathComponent::cp1
Point cp1
Definition: path_component.h:104
impeller::VertexWriter::VertexWriter
VertexWriter(std::vector< Point > &points, std::vector< uint16_t > &indices)
Definition: path_component.cc:13
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::LinearPathComponent::p1
Point p1
Definition: path_component.h:39
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent(Point ap1, Point acp, Point ap2)
Definition: path_component.h:72
impeller::ContourComponent::ContourComponent
ContourComponent(Point p, Point closed)
Definition: path_component.h:160
impeller::LinearPathComponent::GetStartDirection
std::optional< Vector2 > GetStartDirection() const
Definition: path_component.cc:133
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:52
impeller::CubicPathComponent::operator==
bool operator==(const CubicPathComponent &other) const
Definition: path_component.h:137
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:44
impeller::QuadraticPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:190
impeller::ContourComponent::destination
Point destination
Definition: path_component.h:151
impeller::CubicPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:363
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent(const QuadraticPathComponent &q)
Definition: path_component.h:112
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent()
Definition: path_component.h:110
impeller::CubicPathComponent
Definition: path_component.h:100
scalar.h
impeller::ContourComponent::ContourComponent
ContourComponent()
Definition: path_component.h:156
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:20
impeller::VertexWriter::~VertexWriter
~VertexWriter()=default
impeller::ContourComponent::IsClosed
constexpr bool IsClosed() const
Definition: path_component.h:158
impeller::QuadraticPathComponent::p2
Point p2
Definition: path_component.h:68
impeller::TPoint
Definition: point.h:27
impeller::VertexWriter::Write
void Write(Point point)
Definition: path_component.cc:67
impeller::QuadraticPathComponent::QuadraticPathComponent
QuadraticPathComponent()
Definition: path_component.h:70
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::CubicPathComponent::PointProc
std::function< void(const Point &point)> PointProc
Definition: path_component.h:129
impeller::CubicPathComponent::p2
Point p2
Definition: path_component.h:108
impeller::CubicPathComponent::Extrema
std::vector< Point > Extrema() const
Definition: path_component.cc:332
impeller::ContourComponent
Definition: path_component.h:150
impeller::LinearPathComponent::GetEndDirection
std::optional< Vector2 > GetEndDirection() const
Definition: path_component.cc:140
impeller
Definition: allocation.cc:12
impeller::QuadraticPathComponent
Definition: path_component.h:62
impeller::ContourComponent::closed
Point closed
Definition: path_component.h:154
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:118