Flutter Engine
 
Loading...
Searching...
No Matches
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
11namespace 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
26GeometryResult 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& data_host_buffer = renderer.GetTransientsDataBuffer();
81 auto& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
82 return GeometryResult{
84 .vertex_buffer =
85 {
86 .vertex_buffer = data_host_buffer.Emplace(
87 geometry.data(), geometry.size() * sizeof(Point),
88 alignof(Point)),
89 .index_buffer = indexes_host_buffer.Emplace(
90 indices.data(), indices.size() * sizeof(uint16_t),
91 alignof(uint16_t)),
92 .vertex_count = indices.size(),
93 .index_type = IndexType::k16bit,
94 },
95 .transform = entity.GetShaderTransform(pass),
96 };
97}
98
99std::optional<Rect> SuperellipseGeometry::GetCoverage(
100 const Matrix& transform) const {
101 return Rect::MakeOriginSize(center_ - Point(radius_, radius_),
102 Size(radius_ * 2, radius_ * 2));
103}
104
106 const Rect& rect) const {
107 return false;
108}
109
111 return false;
112}
113
114} // namespace impeller
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
Matrix GetShaderTransform(const RenderPass &pass) const
Definition entity.cc:48
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition host_buffer.h:92
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
double y
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
static constexpr TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition rect.h:144
std::vector< Point > points