Flutter Impeller
superellipse_geometry.cc
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 #include <vector>
6 
8 
10 
11 namespace impeller {
12 
14  Scalar radius,
15  Scalar degree,
16  Scalar alpha,
17  Scalar beta)
18  : center_(center),
19  degree_(degree),
20  radius_(radius),
21  alpha_(alpha),
22  beta_(beta) {}
23 
25 
26 GeometryResult SuperellipseGeometry::GetPositionBuffer(
27  const ContentContext& renderer,
28  const Entity& entity,
29  RenderPass& pass) const {
30  // https://math.stackexchange.com/questions/2573746/superellipse-parametric-equation
31  Scalar a = alpha_;
32  Scalar b = beta_;
33  Scalar n = degree_;
34 
35  // TODO(jonahwilliams): determine parameter values based on scaling factor.
36  Scalar step = kPi / 80;
37 
38  // Generate the points for the top left quadrant, and then mirror to the other
39  // quadrants.
40  std::vector<Point> points;
41  points.reserve(41);
42  for (int i = 0; i <= 40; i++) {
43  Scalar t = i * step;
44  Scalar x = a * pow(abs(cos(t)), 2 / n);
45  Scalar y = b * pow(abs(sin(t)), 2 / n);
46  points.emplace_back(x * radius_, y * radius_);
47  }
48 
49  static constexpr Point reflection[4] = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
50 
51  // Reflect into the 4 quadrants and generate the tessellated mesh. The
52  // iteration order is reversed so that the trianges are continuous from
53  // quadrant to quadrant.
54  std::vector<Point> geometry;
55  geometry.reserve(1 + 4 * points.size());
56  geometry.push_back(center_);
57  for (auto i = 0u; i < points.size(); i++) {
58  geometry.push_back(center_ + (reflection[0] * points[i]));
59  }
60  for (auto i = 0u; i < points.size(); i++) {
61  geometry.push_back(center_ +
62  (reflection[1] * points[points.size() - i - 1]));
63  }
64  for (auto i = 0u; i < points.size(); i++) {
65  geometry.push_back(center_ + (reflection[2] * points[i]));
66  }
67  for (auto i = 0u; i < points.size(); i++) {
68  geometry.push_back(center_ +
69  (reflection[3] * points[points.size() - i - 1]));
70  }
71 
72  std::vector<uint16_t> indices;
73  indices.reserve(geometry.size() * 3);
74  for (auto i = 2u; i < geometry.size(); i++) {
75  indices.push_back(0);
76  indices.push_back(i - 1);
77  indices.push_back(i);
78  }
79 
80  auto& host_buffer = renderer.GetTransientsBuffer();
81  return GeometryResult{
83  .vertex_buffer =
84  {
85  .vertex_buffer = host_buffer.Emplace(
86  geometry.data(), geometry.size() * sizeof(Point),
87  alignof(Point)),
88  .index_buffer = host_buffer.Emplace(
89  indices.data(), indices.size() * sizeof(uint16_t),
90  alignof(uint16_t)),
91  .vertex_count = indices.size(),
92  .index_type = IndexType::k16bit,
93  },
94  .transform = entity.GetShaderTransform(pass),
95  };
96 }
97 
98 std::optional<Rect> SuperellipseGeometry::GetCoverage(
99  const Matrix& transform) const {
100  return Rect::MakeOriginSize(center_ - Point(radius_, radius_),
101  Size(radius_ * 2, radius_ * 2));
102 }
103 
105  const Rect& rect) const {
106  return false;
107 }
108 
110  return false;
111 }
112 
113 } // namespace impeller
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
SuperellipseGeometry(const Point &center, Scalar radius, Scalar degree, Scalar alpha, Scalar beta)
bool CoversArea(const Matrix &transform, const Rect &rect) const override
Determines if this geometry, transformed by the given transform, will completely cover all surface ar...
int32_t x
constexpr float kPi
Definition: constants.h:26
float Scalar
Definition: scalar.h:19
TPoint< Scalar > Point
Definition: point.h:327
TSize< Scalar > Size
Definition: size.h:159
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:144
std::vector< Point > points