Flutter Impeller
impeller::QuadraticPathComponent Struct Reference

#include <path_component.h>

Public Types

using PointProc = std::function< void(const Point &point)>
 

Public Member Functions

 QuadraticPathComponent ()
 
 QuadraticPathComponent (Point ap1, Point acp, Point ap2)
 
Point Solve (Scalar time) const
 
Point SolveDerivative (Scalar time) const
 
void AppendPolylinePoints (Scalar scale_factor, std::vector< Point > &points) const
 
void ToLinearPathComponents (Scalar scale_factor, const PointProc &proc) const
 
std::vector< PointExtrema () const
 
bool operator== (const QuadraticPathComponent &other) const
 
std::optional< Vector2GetStartDirection () const
 
std::optional< Vector2GetEndDirection () const
 

Public Attributes

Point p1
 
Point cp
 
Point p2
 

Detailed Description

Definition at line 53 of file path_component.h.

Member Typedef Documentation

◆ PointProc

using impeller::QuadraticPathComponent::PointProc = std::function<void(const Point& point)>

Definition at line 83 of file path_component.h.

Constructor & Destructor Documentation

◆ QuadraticPathComponent() [1/2]

impeller::QuadraticPathComponent::QuadraticPathComponent ( )
inline

Definition at line 61 of file path_component.h.

61 {}

◆ QuadraticPathComponent() [2/2]

impeller::QuadraticPathComponent::QuadraticPathComponent ( Point  ap1,
Point  acp,
Point  ap2 
)
inline

Definition at line 63 of file path_component.h.

64  : p1(ap1), cp(acp), p2(ap2) {}

Member Function Documentation

◆ AppendPolylinePoints()

void impeller::QuadraticPathComponent::AppendPolylinePoints ( Scalar  scale_factor,
std::vector< Point > &  points 
) const

Definition at line 106 of file path_component.cc.

108  {
109  ToLinearPathComponents(scale_factor, [&points](const Point& point) {
110  points.emplace_back(point);
111  });
112 }

References ToLinearPathComponents().

◆ Extrema()

std::vector< Point > impeller::QuadraticPathComponent::Extrema ( ) const

Definition at line 157 of file path_component.cc.

157  {
158  CubicPathComponent elevated(*this);
159  return elevated.Extrema();
160 }

References impeller::CubicPathComponent::Extrema().

◆ GetEndDirection()

std::optional< Vector2 > impeller::QuadraticPathComponent::GetEndDirection ( ) const

Definition at line 172 of file path_component.cc.

172  {
173  if (p2 != cp) {
174  return (p2 - cp).Normalize();
175  }
176  if (p2 != p1) {
177  return (p2 - p1).Normalize();
178  }
179  return std::nullopt;
180 }

References cp, p1, and p2.

Referenced by impeller::PathComponentEndDirectionVisitor::operator()().

◆ GetStartDirection()

std::optional< Vector2 > impeller::QuadraticPathComponent::GetStartDirection ( ) const

Definition at line 162 of file path_component.cc.

162  {
163  if (p1 != cp) {
164  return (p1 - cp).Normalize();
165  }
166  if (p1 != p2) {
167  return (p1 - p2).Normalize();
168  }
169  return std::nullopt;
170 }

References cp, p1, and p2.

Referenced by impeller::PathComponentStartDirectionVisitor::operator()().

◆ operator==()

bool impeller::QuadraticPathComponent::operator== ( const QuadraticPathComponent other) const
inline

Definition at line 89 of file path_component.h.

89  {
90  return p1 == other.p1 && cp == other.cp && p2 == other.p2;
91  }

References cp, p1, and p2.

◆ Solve()

Point impeller::QuadraticPathComponent::Solve ( Scalar  time) const

Definition at line 87 of file path_component.cc.

87  {
88  return {
89  QuadraticSolve(time, p1.x, cp.x, p2.x), // x
90  QuadraticSolve(time, p1.y, cp.y, p2.y), // y
91  };
92 }

References cp, p1, p2, impeller::QuadraticSolve(), impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by ToLinearPathComponents().

◆ SolveDerivative()

Point impeller::QuadraticPathComponent::SolveDerivative ( Scalar  time) const

Definition at line 94 of file path_component.cc.

94  {
95  return {
96  QuadraticSolveDerivative(time, p1.x, cp.x, p2.x), // x
97  QuadraticSolveDerivative(time, p1.y, cp.y, p2.y), // y
98  };
99 }

References cp, p1, p2, impeller::QuadraticSolveDerivative(), impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

◆ ToLinearPathComponents()

void impeller::QuadraticPathComponent::ToLinearPathComponents ( Scalar  scale_factor,
const PointProc proc 
) const

Definition at line 114 of file path_component.cc.

116  {
117  auto tolerance = kDefaultCurveTolerance / scale_factor;
118  auto sqrt_tolerance = sqrt(tolerance);
119 
120  auto d01 = cp - p1;
121  auto d12 = p2 - cp;
122  auto dd = d01 - d12;
123  auto cross = (p2 - p1).Cross(dd);
124  auto x0 = d01.Dot(dd) * 1 / cross;
125  auto x2 = d12.Dot(dd) * 1 / cross;
126  auto scale = std::abs(cross / (hypot(dd.x, dd.y) * (x2 - x0)));
127 
128  auto a0 = ApproximateParabolaIntegral(x0);
129  auto a2 = ApproximateParabolaIntegral(x2);
130  Scalar val = 0.f;
131  if (std::isfinite(scale)) {
132  auto da = std::abs(a2 - a0);
133  auto sqrt_scale = sqrt(scale);
134  if ((x0 < 0 && x2 < 0) || (x0 >= 0 && x2 >= 0)) {
135  val = da * sqrt_scale;
136  } else {
137  // cusp case
138  auto xmin = sqrt_tolerance / sqrt_scale;
139  val = sqrt_tolerance * da / ApproximateParabolaIntegral(xmin);
140  }
141  }
142  auto u0 = ApproximateParabolaIntegral(a0);
143  auto u2 = ApproximateParabolaIntegral(a2);
144  auto uscale = 1 / (u2 - u0);
145 
146  auto line_count = std::max(1., ceil(0.5 * val / sqrt_tolerance));
147  auto step = 1 / line_count;
148  for (size_t i = 1; i < line_count; i += 1) {
149  auto u = i * step;
150  auto a = a0 + (a2 - a0) * u;
151  auto t = (ApproximateParabolaIntegral(a) - u0) * uscale;
152  proc(Solve(t));
153  }
154  proc(p2);
155 }

References impeller::ApproximateParabolaIntegral(), cp, impeller::TPoint< T >::Dot(), impeller::kDefaultCurveTolerance, p1, p2, scale, and Solve().

Referenced by AppendPolylinePoints(), and impeller::CubicPathComponent::ToLinearPathComponents().

Member Data Documentation

◆ cp

Point impeller::QuadraticPathComponent::cp

◆ p1

Point impeller::QuadraticPathComponent::p1

◆ p2

Point impeller::QuadraticPathComponent::p2

The documentation for this struct was generated from the following files:
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::kDefaultCurveTolerance
static constexpr Scalar kDefaultCurveTolerance
Definition: path_component.h:27
impeller::QuadraticPathComponent::p1
Point p1
Definition: path_component.h:55
impeller::QuadraticPathComponent::cp
Point cp
Definition: path_component.h:57
impeller::QuadraticSolve
static Scalar QuadraticSolve(Scalar t, Scalar p0, Scalar p1, Scalar p2)
Definition: path_component.cc:19
impeller::TPoint::Dot
constexpr Type Dot(const TPoint &p) const
Definition: point.h:220
impeller::QuadraticPathComponent::ToLinearPathComponents
void ToLinearPathComponents(Scalar scale_factor, const PointProc &proc) const
Definition: path_component.cc:114
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
impeller::QuadraticPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:87
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::QuadraticSolveDerivative
static Scalar QuadraticSolveDerivative(Scalar t, Scalar p0, Scalar p1, Scalar p2)
Definition: path_component.cc:25
impeller::QuadraticPathComponent::p2
Point p2
Definition: path_component.h:59
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::ApproximateParabolaIntegral
static Scalar ApproximateParabolaIntegral(Scalar x)
Definition: path_component.cc:101