Flutter Impeller
impeller::CubicPathComponent Struct Reference

#include <path_component.h>

Public Member Functions

 CubicPathComponent ()
 
 CubicPathComponent (const QuadraticPathComponent &q)
 
 CubicPathComponent (Point ap1, Point acp1, Point acp2, Point ap2)
 
Point Solve (Scalar time) const
 
Point SolveDerivative (Scalar time) const
 
std::vector< PointCreatePolyline (Scalar scale) const
 
std::vector< PointExtrema () const
 
std::vector< QuadraticPathComponentToQuadraticPathComponents (Scalar accuracy) const
 
CubicPathComponent Subsegment (Scalar t0, Scalar t1) const
 
bool operator== (const CubicPathComponent &other) const
 
std::optional< Vector2GetStartDirection () const
 
std::optional< Vector2GetEndDirection () const
 

Public Attributes

Point p1
 
Point cp1
 
Point cp2
 
Point p2
 

Detailed Description

Definition at line 90 of file path_component.h.

Constructor & Destructor Documentation

◆ CubicPathComponent() [1/3]

impeller::CubicPathComponent::CubicPathComponent ( )
inline

Definition at line 96 of file path_component.h.

96 {}

Referenced by Subsegment().

◆ CubicPathComponent() [2/3]

impeller::CubicPathComponent::CubicPathComponent ( const QuadraticPathComponent q)
inline

Definition at line 98 of file path_component.h.

99  : p1(q.p1),
100  cp1(q.p1 + (q.cp - q.p1) * (2.0 / 3.0)),
101  cp2(q.p2 + (q.cp - q.p2) * (2.0 / 3.0)),
102  p2(q.p2) {}

◆ CubicPathComponent() [3/3]

impeller::CubicPathComponent::CubicPathComponent ( Point  ap1,
Point  acp1,
Point  acp2,
Point  ap2 
)
inline

Definition at line 104 of file path_component.h.

105  : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {}

Member Function Documentation

◆ CreatePolyline()

std::vector< Point > impeller::CubicPathComponent::CreatePolyline ( Scalar  scale) const

Definition at line 190 of file path_component.cc.

190  {
191  auto quads = ToQuadraticPathComponents(.1);
192  std::vector<Point> points;
193  for (const auto& quad : quads) {
194  quad.FillPointsForPolyline(points, scale);
195  }
196  return points;
197 }

References ToQuadraticPathComponents().

Referenced by impeller::testing::TEST().

◆ Extrema()

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

Definition at line 310 of file path_component.cc.

310  {
311  /*
312  * As described in: https://pomax.github.io/bezierinfo/#extremities
313  */
314  std::vector<Scalar> values;
315 
318 
319  std::vector<Point> points = {p1, p2};
320 
321  for (const auto& value : values) {
322  points.emplace_back(Solve(value));
323  }
324 
325  return points;
326 }

References cp1, cp2, impeller::CubicPathBoundingPopulateValues(), p1, p2, Solve(), impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by impeller::QuadraticPathComponent::Extrema(), and impeller::testing::TEST().

◆ GetEndDirection()

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

Definition at line 341 of file path_component.cc.

341  {
342  if (p2 != cp2) {
343  return (p2 - cp2).Normalize();
344  }
345  if (p2 != cp1) {
346  return (p2 - cp1).Normalize();
347  }
348  if (p2 != p1) {
349  return (p2 - p1).Normalize();
350  }
351  return std::nullopt;
352 }

References cp1, cp2, p1, and p2.

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

◆ GetStartDirection()

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

Definition at line 328 of file path_component.cc.

328  {
329  if (p1 != cp1) {
330  return (p1 - cp1).Normalize();
331  }
332  if (p1 != cp2) {
333  return (p1 - cp2).Normalize();
334  }
335  if (p1 != p2) {
336  return (p1 - p2).Normalize();
337  }
338  return std::nullopt;
339 }

References cp1, cp2, p1, and p2.

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

◆ operator==()

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

Definition at line 124 of file path_component.h.

124  {
125  return p1 == other.p1 && cp1 == other.cp1 && cp2 == other.cp2 &&
126  p2 == other.p2;
127  }

References cp1, cp2, p1, and p2.

◆ Solve()

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

Definition at line 176 of file path_component.cc.

176  {
177  return {
178  CubicSolve(time, p1.x, cp1.x, cp2.x, p2.x), // x
179  CubicSolve(time, p1.y, cp1.y, cp2.y, p2.y), // y
180  };
181 }

References cp1, cp2, impeller::CubicSolve(), p1, p2, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by Extrema(), and Subsegment().

◆ SolveDerivative()

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

Definition at line 183 of file path_component.cc.

183  {
184  return {
185  CubicSolveDerivative(time, p1.x, cp1.x, cp2.x, p2.x), // x
186  CubicSolveDerivative(time, p1.y, cp1.y, cp2.y, p2.y), // y
187  };
188 }

References cp1, cp2, impeller::CubicSolveDerivative(), p1, p2, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

◆ Subsegment()

CubicPathComponent impeller::CubicPathComponent::Subsegment ( Scalar  t0,
Scalar  t1 
) const

Definition at line 204 of file path_component.cc.

204  {
205  auto p0 = Solve(t0);
206  auto p3 = Solve(t1);
207  auto d = Lower();
208  auto scale = (t1 - t0) * (1.0 / 3.0);
209  auto p1 = p0 + scale * d.Solve(t0);
210  auto p2 = p3 - scale * d.Solve(t1);
211  return CubicPathComponent(p0, p1, p2, p3);
212 }

References CubicPathComponent(), p1, p2, and Solve().

Referenced by ToQuadraticPathComponents().

◆ ToQuadraticPathComponents()

std::vector< QuadraticPathComponent > impeller::CubicPathComponent::ToQuadraticPathComponents ( Scalar  accuracy) const

Definition at line 215 of file path_component.cc.

215  {
216  std::vector<QuadraticPathComponent> quads;
217  // The maximum error, as a vector from the cubic to the best approximating
218  // quadratic, is proportional to the third derivative, which is constant
219  // across the segment. Thus, the error scales down as the third power of
220  // the number of subdivisions. Our strategy then is to subdivide `t` evenly.
221  //
222  // This is an overestimate of the error because only the component
223  // perpendicular to the first derivative is important. But the simplicity is
224  // appealing.
225 
226  // This magic number is the square of 36 / sqrt(3).
227  // See: http://caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
228  auto max_hypot2 = 432.0 * accuracy * accuracy;
229  auto p1x2 = 3.0 * cp1 - p1;
230  auto p2x2 = 3.0 * cp2 - p2;
231  auto p = p2x2 - p1x2;
232  auto err = p.Dot(p);
233  auto quad_count = std::max(1., ceil(pow(err / max_hypot2, 1. / 6.0)));
234 
235  for (size_t i = 0; i < quad_count; i++) {
236  auto t0 = i / quad_count;
237  auto t1 = (i + 1) / quad_count;
238  auto seg = Subsegment(t0, t1);
239  auto p1x2 = 3.0 * seg.cp1 - seg.p1;
240  auto p2x2 = 3.0 * seg.cp2 - seg.p2;
241  quads.emplace_back(
242  QuadraticPathComponent(seg.p1, ((p1x2 + p2x2) / 4.0), seg.p2));
243  }
244  return quads;
245 }

References cp1, cp2, impeller::TPoint< T >::Dot(), p1, p2, and Subsegment().

Referenced by CreatePolyline().

Member Data Documentation

◆ cp1

◆ cp2

◆ p1

◆ p2


The documentation for this struct was generated from the following files:
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::CubicPathComponent::Subsegment
CubicPathComponent Subsegment(Scalar t0, Scalar t1) const
Definition: path_component.cc:204
impeller::CubicPathComponent::p1
Point p1
Definition: path_component.h:91
impeller::CubicPathComponent::cp2
Point cp2
Definition: path_component.h:93
impeller::CubicPathComponent::Solve
Point Solve(Scalar time) const
Definition: path_component.cc:176
impeller::CubicPathBoundingPopulateValues
static void CubicPathBoundingPopulateValues(std::vector< Scalar > &values, Scalar p1, Scalar p2, Scalar p3, Scalar p4)
Definition: path_component.cc:255
impeller::TPoint::Dot
constexpr Type Dot(const TPoint &p) const
Definition: point.h:213
impeller::CubicSolveDerivative
static Scalar CubicSolveDerivative(Scalar t, Scalar p0, Scalar p1, Scalar p2, Scalar p3)
Definition: path_component.cc:44
impeller::CubicPathComponent::cp1
Point cp1
Definition: path_component.h:92
impeller::CubicSolve
static Scalar CubicSolve(Scalar t, Scalar p0, Scalar p1, Scalar p2, Scalar p3)
Definition: path_component.cc:33
impeller::CubicPathComponent::ToQuadraticPathComponents
std::vector< QuadraticPathComponent > ToQuadraticPathComponents(Scalar accuracy) const
Definition: path_component.cc:215
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::CubicPathComponent::CubicPathComponent
CubicPathComponent()
Definition: path_component.h:96
impeller::CubicPathComponent::p2
Point p2
Definition: path_component.h:94