| 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
7 #include "flutter/impeller/entity/geometry/superellipse_geometry.h"
8
9 #include "impeller/geometry/constants.h"
10
11 namespace impeller {
12
13 SuperellipseGeometry::SuperellipseGeometry(const Point& center,
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
24 SuperellipseGeometry::~SuperellipseGeometry() {}
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{
82 .type = PrimitiveType::kTriangle,
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
104 bool SuperellipseGeometry::CoversArea(const Matrix& transform,
105 const Rect& rect) const {
106 return false;
107 }
108
109 bool SuperellipseGeometry::IsAxisAlignedRect() const {
110 return false;
111 }
112
113 } // namespace impeller
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:296
impeller::Entity
Definition: entity.h:20
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::SuperellipseGeometry::IsAxisAlignedRect
bool IsAxisAlignedRect() const override
Definition: superellipse_geometry.cc:109
impeller::SuperellipseGeometry::SuperellipseGeometry
SuperellipseGeometry(const Point &center, Scalar radius, Scalar degree, Scalar alpha, Scalar beta)
Definition: superellipse_geometry.cc:13
impeller::SuperellipseGeometry::CoversArea
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...
Definition: superellipse_geometry.cc:104
impeller::SuperellipseGeometry::~SuperellipseGeometry
~SuperellipseGeometry() override
Definition: superellipse_geometry.cc:24
x
int32_t x
Definition: dl_golden_unittests.cc:458
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
constants.h
impeller::saturated::b
SI b
Definition: saturated_math.h:87
impeller
Definition: allocation.cc:12
impeller::IndexType::k16bit
@ k16bit
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::kPi
constexpr float kPi
Definition: constants.h:26
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::Size
TSize< Scalar > Size
Definition: size.h:159
impeller::GeometryResult
Definition: geometry.h:21
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TPoint< Scalar >
impeller::TRect< Scalar >
impeller::TRect< Scalar >::MakeOriginSize
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:144
superellipse_geometry.h