Flutter Engine
The Flutter Engine
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
16
17namespace impeller {
18
19/// @brief An interface for generating a multi contour polyline as a triangle
20/// strip.
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
42
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.
64 // Start point.
66 // Control point.
68 // End point.
70
72
74 : p1(ap1), cp(acp), p2(ap2) {}
75
76 Point Solve(Scalar time) const;
77
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
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.
102 // Start point.
104 // The first control point.
106 // The second control point.
108 // End point.
110
112
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
120 : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {}
121
122 Point Solve(Scalar time) const;
123
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
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
153 bool is_closed = false;
154
156
157 explicit ContourComponent(Point p, bool is_closed = false)
159
160 bool operator==(const ContourComponent& other) const {
161 return destination == other.destination && is_closed == other.is_closed;
162 }
163};
164
165using PathComponentVariant = std::variant<std::monostate,
166 const LinearPathComponent*,
168 const CubicPathComponent*>;
169
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
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
191
192} // namespace impeller
193
194#endif // FLUTTER_IMPELLER_GEOMETRY_PATH_COMPONENT_H_
static const int points[]
An interface for generating a multi contour polyline as a triangle strip.
VertexWriter(std::vector< Point > &points, std::vector< uint16_t > &indices)
void Write(Point point)
uint8_t value
Dart_NativeFunction function
Definition: fuchsia.cc:51
float Scalar
Definition: scalar.h:18
std::variant< std::monostate, const LinearPathComponent *, const QuadraticPathComponent *, const CubicPathComponent * > PathComponentVariant
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394
const Scalar scale
bool operator==(const ContourComponent &other) const
ContourComponent(Point p, bool is_closed=false)
void ToLinearPathComponents(Scalar scale, const PointProc &proc) const
void AppendPolylinePoints(Scalar scale, std::vector< Point > &points) const
CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2)
bool operator==(const CubicPathComponent &other) const
std::function< void(const Point &point)> PointProc
CubicPathComponent Subsegment(Scalar t0, Scalar t1) const
Point Solve(Scalar time) const
std::optional< Vector2 > GetStartDirection() const
std::vector< Point > Extrema() const
std::optional< Vector2 > GetEndDirection() const
Point SolveDerivative(Scalar time) const
CubicPathComponent(const QuadraticPathComponent &q)
std::optional< Vector2 > GetEndDirection() const
LinearPathComponent(Point ap1, Point ap2)
std::optional< Vector2 > GetStartDirection() const
std::vector< Point > Extrema() const
bool operator==(const LinearPathComponent &other) const
Point Solve(Scalar time) const
void AppendPolylinePoints(std::vector< Point > &points) const
std::optional< Vector2 > operator()(const LinearPathComponent *component)
std::optional< Vector2 > operator()(std::monostate monostate)
std::optional< Vector2 > operator()(std::monostate monostate)
std::optional< Vector2 > operator()(const LinearPathComponent *component)
std::optional< Vector2 > GetEndDirection() const
QuadraticPathComponent(Point ap1, Point acp, Point ap2)
void AppendPolylinePoints(Scalar scale_factor, std::vector< Point > &points) const
bool operator==(const QuadraticPathComponent &other) const
std::function< void(const Point &point)> PointProc
std::vector< Point > Extrema() const
Point SolveDerivative(Scalar time) const
std::optional< Vector2 > GetStartDirection() const
void ToLinearPathComponents(Scalar scale_factor, const PointProc &proc) const
Point Solve(Scalar time) const