Flutter Impeller
tessellator.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 "tessellator.h"
6 
7 #include <vector>
8 
9 namespace impeller {
11  return new PathBuilder();
12 }
13 
15  delete builder;
16 }
17 
18 void MoveTo(PathBuilder* builder, Scalar x, Scalar y) {
19  builder->MoveTo(Point(x, y));
20 }
21 
22 void LineTo(PathBuilder* builder, Scalar x, Scalar y) {
23  builder->LineTo(Point(x, y));
24 }
25 
26 void CubicTo(PathBuilder* builder,
27  Scalar x1,
28  Scalar y1,
29  Scalar x2,
30  Scalar y2,
31  Scalar x3,
32  Scalar y3) {
33  builder->CubicCurveTo(Point(x1, y1), Point(x2, y2), Point(x3, y3));
34 }
35 
36 void Close(PathBuilder* builder) {
37  builder->Close();
38 }
39 
40 struct Vertices* Tessellate(PathBuilder* builder,
41  int fill_type,
42  Scalar tolerance) {
43  auto path = builder->CopyPath(static_cast<FillType>(fill_type));
44  std::vector<float> points;
45  if (Tessellator{}.Tessellate(
46  path, tolerance,
47  [&points](const float* vertices, size_t vertices_count,
48  const uint16_t* indices, size_t indices_count) {
49  // Results are expected to be re-duplicated.
50  std::vector<Point> raw_points;
51  for (auto i = 0u; i < vertices_count * 2; i += 2) {
52  raw_points.emplace_back(Point{vertices[i], vertices[i + 1]});
53  }
54  for (auto i = 0u; i < indices_count; i++) {
55  auto point = raw_points[indices[i]];
56  points.push_back(point.x);
57  points.push_back(point.y);
58  }
59  return true;
61  return nullptr;
62  }
63 
64  Vertices* vertices = new Vertices();
65  vertices->points = new float[points.size()];
66  if (!vertices->points) {
67  return nullptr;
68  }
69  vertices->length = points.size();
70  std::copy(points.begin(), points.end(), vertices->points);
71  return vertices;
72 }
73 
74 void DestroyVertices(Vertices* vertices) {
75  delete vertices->points;
76  delete vertices;
77 }
78 
79 } // namespace impeller
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DestroyVertices
void DestroyVertices(Vertices *vertices)
Definition: tessellator.cc:74
impeller::Tessellator
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:33
impeller::PathBuilder::CubicCurveTo
PathBuilder & CubicCurveTo(Point controlPoint1, Point controlPoint2, Point point, bool relative=false)
Insert a cubic curve from the curren position to point using the control points controlPoint1 and con...
Definition: path_builder.cc:85
impeller::PathBuilder
Definition: path_builder.h:14
impeller::MoveTo
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:18
impeller::Vertices
Definition: tessellator.h:23
impeller::Vertices::points
float * points
Definition: tessellator.h:24
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
impeller::Vertices::length
uint32_t length
Definition: tessellator.h:25
impeller::PathBuilder::CopyPath
Path CopyPath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:17
impeller::CreatePathBuilder
PathBuilder * CreatePathBuilder()
Definition: tessellator.cc:10
impeller::CubicTo
void CubicTo(PathBuilder *builder, Scalar x1, Scalar y1, Scalar x2, Scalar y2, Scalar x3, Scalar y3)
Definition: tessellator.cc:26
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:47
impeller::Tessellator::Result::kSuccess
@ kSuccess
impeller::FillType
FillType
Definition: path.h:29
impeller::Close
void Close(PathBuilder *builder)
Definition: tessellator.cc:36
impeller::PathBuilder::Close
PathBuilder & Close()
Definition: path_builder.cc:40
impeller::LineTo
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:22
impeller::TPoint< Scalar >
impeller::Tessellator::Tessellate
Tessellator::Result Tessellate(const Path &path, Scalar tolerance, const BuilderCallback &callback)
Generates filled triangles from the path. A callback is invoked once for the entire tessellation.
Definition: tessellator.cc:58
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:33
impeller::DestroyPathBuilder
void DestroyPathBuilder(PathBuilder *builder)
Definition: tessellator.cc:14
tessellator.h
impeller::Tessellate
struct Vertices * Tessellate(PathBuilder *builder, int fill_type, Scalar tolerance)
Definition: tessellator.cc:40
impeller
Definition: aiks_blur_unittests.cc:20