Debian Codegraph @

modules/bentleyottmann/src/Contour.cpp

raw
1 // Copyright 2023 Google LLC
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4 #include "modules/bentleyottmann/include/Contour.h"
5
6 #include "include/core/SkPath.h"
7 #include "include/core/SkPoint.h"
8 #include "include/core/SkScalar.h"
9 #include "include/private/base/SkTo.h"
10 #include "modules/bentleyottmann/include/Myers.h"
11
12 #include <algorithm>
13 #include <vector>
14
15 namespace contour {
16 Contours Contours::Make(SkPath path) {
17 SkPath::Iter iter(path, false);
18 Contours contours;
19 while (auto rec = iter.next()) {
20 SkSpan<const SkPoint> pts = rec->fPoints;
21 switch (rec->fVerb) {
22 case SkPathVerb::kConic: {
23 SK_ABORT("Not implemented");
24 break;
25 }
26 case SkPathVerb::kMove:
27 contours.closeContourIfNeeded();
28 contours.moveToStartOfContour(pts[0]);
29 break;
30 case SkPathVerb::kLine: {
31 contours.addPointToCurrentContour(pts[1]);
32 break;
33 }
34 case SkPathVerb::kQuad: {
35 SK_ABORT("Not implemented");
36 break;
37 }
38 case SkPathVerb::kCubic: {
39 SK_ABORT("Not implemented");
40 break;
41 }
42 case SkPathVerb::kClose: {
43 contours.closeContourIfNeeded();
44 break;
45 }
46 }
47 }
48
49 // Close the remaining contour.
50 contours.closeContourIfNeeded();
51
52 return contours;
53 }
54
55 std::vector<myers::Segment> Contours::segments() const {
56 SK_ABORT("Not implemented");
57 }
58
59 static SkIRect extend_rect(SkIRect r, Point p) {
60 int32_t left = std::min(p.x, r.fLeft),
61 top = std::min(p.y, r.fTop),
62 right = std::max(p.x, r.fRight),
63 bottom = std::max(p.y, r.fBottom);
64 return {left, top, right, bottom};
65 }
66
67 Point Contours::RoundSkPoint(SkPoint p) {
68 return {SkScalarRoundToInt(p.x() * kScaleFactor), SkScalarRoundToInt(p.y() * kScaleFactor)};
69 }
70
71 bool Contours::currentContourIsEmpty() const {
72 int32_t lastEnd = fContours.empty() ? 0 : fContours.back().end;
73 return lastEnd == SkToS32(fPoints.size());
74 }
75
76 void Contours::addPointToCurrentContour(SkPoint p) {
77 if (this->currentContourIsEmpty()) {
78 fPoints.push_back(fContourStart);
79 fContourBounds = extend_rect(fContourBounds, fContourStart);
80 }
81 Point point = RoundSkPoint(p);
82 fPoints.push_back(point);
83 fContourBounds = extend_rect(fContourBounds, point);
84 }
85
86 void Contours::moveToStartOfContour(SkPoint p) {
87 fContourStart = RoundSkPoint(p);
88 }
89
90 void Contours::closeContourIfNeeded() {
91 if (this->currentContourIsEmpty()) {
92 // The current contour is empty. Don't record it.
93 return;
94 }
95 fContours.push_back({fContourBounds, SkToS32(fPoints.size())});
96 fContourBounds = kEmptyRect;
97 }
98 } // namespace contour