| 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& data_host_buffer = renderer.GetTransientsDataBuffer();
81 auto& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
82 return GeometryResult{
83 .type = PrimitiveType::kTriangle,
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
99 std::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
105 bool SuperellipseGeometry::CoversArea(const Matrix& transform,
106 const Rect& rect) const {
107 return false;
108 }
109
110 bool SuperellipseGeometry::IsAxisAlignedRect() const {
111 return false;
112 }
113
114 } // namespace impeller
impeller::ContentContext
Definition: content_context.h:122
impeller::ContentContext::GetTransientsIndexesBuffer
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
Definition: content_context.h:290
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:299
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50
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:110
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:105
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:312
constants.h
impeller::saturated::b
SI b
Definition: saturated_math.h:87
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::kPi
constexpr float kPi
Definition: constants.h:26
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::IndexType::k16bit
@ k16bit
impeller::Point
TPoint< Scalar > Point
Definition: point.h:425
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
points
std::vector< Point > points
Definition: tessellator_libtess.cc:76