1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PATH_PATH_ITERATOR_H
18 #define PATH_PATH_ITERATOR_H
19 
20 #include "Path.h"
21 #include "Conic.h"
22 
23 class PathIterator {
24 public:
25     enum class VerbDirection : uint8_t  {
26         Forward, // API >=30
27         Backward // API < 30
28     };
29 
30     enum class ConicEvaluation : uint8_t {
31         AsConic,
32         AsQuadratics
33     };
34 
35     PathIterator(
36             Point* points,
37             Verb* verbs,
38             float* conicWeights,
39             int count,
40             VerbDirection direction,
41             ConicEvaluation conicEvaluation,
42             float tolerance = 0.25f
43     ) noexcept
mPoints(points)44             : mPoints(points),
45               mVerbs(verbs),
46               mConicWeights(conicWeights),
47               mIndex(count),
48               mCount(count),
49               mDirection(direction),
50               mConicEvaluation(conicEvaluation),
51               mTolerance(tolerance) {
52     }
53 
rawCount()54     int rawCount() const noexcept { return mCount; }
55 
56     int count() noexcept;
57 
hasNext()58     bool hasNext() const noexcept { return mIndex > 0; }
59 
peek()60     Verb peek() const noexcept {
61         auto verbs = mDirection == VerbDirection::Forward ? mVerbs : mVerbs - 1;
62         return mIndex > 0 ? *verbs : Verb::Done;
63     }
64 
65     Verb next(Point points[4]) noexcept;
66 
67 private:
68     const Point* mPoints;
69     const Verb* mVerbs;
70     const float* mConicWeights;
71     int mIndex;
72     const int mCount;
73     const VerbDirection mDirection;
74     const ConicEvaluation mConicEvaluation;
75     const float mTolerance;
76     ConicConverter mConverter;
77     int mConicCurrentQuadratic = 0;
78 };
79 
80 #endif //PATH_PATH_ITERATOR_H
81