|
| 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 "flutter/impeller/entity/geometry/circle_tessellator.h" |
| 6 | + |
| 7 | +#include "flutter/fml/logging.h" |
| 8 | + |
| 9 | +namespace impeller { |
| 10 | + |
| 11 | +std::vector<Trig> CircleTessellator::trigs_[MAX_DIVISIONS_ + 1]; |
| 12 | + |
| 13 | +size_t CircleTessellator::ComputeQuadrantDivisions(Scalar pixel_radius) { |
| 14 | + // Note: these values are approximated based on the values returned from |
| 15 | + // the decomposition of 4 cubics performed by Path::CreatePolyline. |
| 16 | + if (pixel_radius < 1.0) { |
| 17 | + return 1; |
| 18 | + } |
| 19 | + if (pixel_radius < 2.0) { |
| 20 | + return 2; |
| 21 | + } |
| 22 | + if (pixel_radius < 12.0) { |
| 23 | + return 6; |
| 24 | + } |
| 25 | + if (pixel_radius <= 36.0) { |
| 26 | + return 9; |
| 27 | + } |
| 28 | + pixel_radius /= 4; |
| 29 | + if (pixel_radius > (MAX_DIVISIONS_ - 1)) { |
| 30 | + return MAX_DIVISIONS_; |
| 31 | + } |
| 32 | + return static_cast<int>(ceil(pixel_radius)); |
| 33 | +} |
| 34 | + |
| 35 | +const std::vector<Trig>& CircleTessellator::GetTrigForDivisions( |
| 36 | + size_t divisions) { |
| 37 | + FML_DCHECK(divisions > 0 && divisions <= MAX_DIVISIONS_); |
| 38 | + std::vector<Trig>& trigs = trigs_[divisions]; |
| 39 | + |
| 40 | + if (trigs.empty()) { |
| 41 | + double angle_scale = kPiOver2 / divisions; |
| 42 | + |
| 43 | + trigs.emplace_back(1.0, 0.0); |
| 44 | + for (size_t i = 1; i < divisions; i++) { |
| 45 | + trigs.emplace_back(Radians(i * angle_scale)); |
| 46 | + } |
| 47 | + trigs.emplace_back(0.0, 1.0); |
| 48 | + |
| 49 | + FML_DCHECK(trigs.size() == divisions + 1); |
| 50 | + } |
| 51 | + |
| 52 | + return trigs; |
| 53 | +} |
| 54 | + |
| 55 | +void CircleTessellator::ExtendRelativeQuadrantToAbsoluteCircle( |
| 56 | + std::vector<Point>& points, |
| 57 | + const Point& center) { |
| 58 | + auto quadrant_points = points.size(); |
| 59 | + |
| 60 | + // The 1st quadrant points are reversed in order, reflected around |
| 61 | + // the Y axis, and translated to become absolute 2nd quadrant points. |
| 62 | + for (size_t i = 1; i <= quadrant_points; i++) { |
| 63 | + auto point = points[quadrant_points - i]; |
| 64 | + points.emplace_back(center.x + point.x, center.y - point.y); |
| 65 | + } |
| 66 | + |
| 67 | + // The 1st quadrant points are reflected around the X & Y axes |
| 68 | + // and translated to become absolute 3rd quadrant points. |
| 69 | + for (size_t i = 0; i < quadrant_points; i++) { |
| 70 | + auto point = points[i]; |
| 71 | + points.emplace_back(center.x - point.x, center.y - point.y); |
| 72 | + } |
| 73 | + |
| 74 | + // The 1st quadrant points are reversed in order, reflected around |
| 75 | + // the X axis and translated to become absolute 4th quadrant points. |
| 76 | + // The 1st quadrant points are also translated to the center point as |
| 77 | + // well since this is the last time we will use them. |
| 78 | + for (size_t i = 1; i <= quadrant_points; i++) { |
| 79 | + auto point = points[quadrant_points - i]; |
| 80 | + points.emplace_back(center.x - point.x, center.y + point.y); |
| 81 | + |
| 82 | + // This is the last loop where we need the first quadrant to be |
| 83 | + // relative so we convert them to absolute as we go. |
| 84 | + points[quadrant_points - i] = center + point; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void CircleTessellator::FillQuadrantTriangles(std::vector<Point>& points, |
| 89 | + const Point& center, |
| 90 | + const Point& start_vector, |
| 91 | + const Point& end_vector) const { |
| 92 | + // We only deal with circles for now |
| 93 | + FML_DCHECK(start_vector.GetLength() - end_vector.GetLength() < |
| 94 | + kEhCloseEnough); |
| 95 | + // And only for perpendicular vectors |
| 96 | + FML_DCHECK(start_vector.Dot(end_vector) < kEhCloseEnough); |
| 97 | + |
| 98 | + auto trigs = GetTrigForDivisions(quadrant_divisions_); |
| 99 | + |
| 100 | + auto prev = center + (trigs[0].cos * start_vector + // |
| 101 | + trigs[0].sin * end_vector); |
| 102 | + for (size_t i = 1; i < trigs.size(); i++) { |
| 103 | + points.emplace_back(center); |
| 104 | + points.emplace_back(prev); |
| 105 | + prev = center + (trigs[i].cos * start_vector + // |
| 106 | + trigs[i].sin * end_vector); |
| 107 | + points.emplace_back(prev); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +std::vector<Point> CircleTessellator::GetCircleTriangles(const Point& center, |
| 112 | + Scalar radius) const { |
| 113 | + std::vector<Point> points = std::vector<Point>(); |
| 114 | + const size_t quadrant_points = quadrant_divisions_ * 3; |
| 115 | + points.reserve(quadrant_points * 4); |
| 116 | + |
| 117 | + // Start with the quadrant top-center to right-center using coordinates |
| 118 | + // relative to the (0, 0). The coordinates will be made absolute relative |
| 119 | + // to the center during the extend method below. |
| 120 | + FillQuadrantTriangles(points, {}, {0, -radius}, {radius, 0}); |
| 121 | + FML_DCHECK(points.size() == quadrant_points); |
| 122 | + |
| 123 | + ExtendRelativeQuadrantToAbsoluteCircle(points, center); |
| 124 | + |
| 125 | + FML_DCHECK(points.size() == quadrant_points * 4); |
| 126 | + |
| 127 | + return points; |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace impeller |
0 commit comments