1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkOffsetPolygon_DEFINED
9cb93a386Sopenharmony_ci#define SkOffsetPolygon_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include <functional>
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci#include "include/core/SkPoint.h"
14cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h"
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cistruct SkRect;
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci/**
19cb93a386Sopenharmony_ci * Generates a polygon that is inset a constant from the boundary of a given convex polygon.
20cb93a386Sopenharmony_ci *
21cb93a386Sopenharmony_ci * @param inputPolygonVerts  Array of points representing the vertices of the original polygon.
22cb93a386Sopenharmony_ci *  It should be convex and have no coincident points.
23cb93a386Sopenharmony_ci * @param inputPolygonSize  Number of vertices in the original polygon.
24cb93a386Sopenharmony_ci * @param inset  How far we wish to inset the polygon. This should be a positive value.
25cb93a386Sopenharmony_ci * @param insetPolygon  The resulting inset polygon, if any.
26cb93a386Sopenharmony_ci * @return true if an inset polygon exists, false otherwise.
27cb93a386Sopenharmony_ci */
28cb93a386Sopenharmony_cibool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
29cb93a386Sopenharmony_ci                          SkScalar inset, SkTDArray<SkPoint>* insetPolygon);
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci/**
32cb93a386Sopenharmony_ci * Generates a simple polygon (if possible) that is offset a constant distance from the boundary
33cb93a386Sopenharmony_ci * of a given simple polygon.
34cb93a386Sopenharmony_ci * The input polygon must be simple and have no coincident vertices or collinear edges.
35cb93a386Sopenharmony_ci *
36cb93a386Sopenharmony_ci * @param inputPolygonVerts  Array of points representing the vertices of the original polygon.
37cb93a386Sopenharmony_ci * @param inputPolygonSize  Number of vertices in the original polygon.
38cb93a386Sopenharmony_ci * @param bounds Bounding rectangle for the original polygon.
39cb93a386Sopenharmony_ci * @param offset How far we wish to offset the polygon.
40cb93a386Sopenharmony_ci *   Positive values indicate insetting, negative values outsetting.
41cb93a386Sopenharmony_ci * @param offsetPolgon  The resulting offset polygon, if any.
42cb93a386Sopenharmony_ci * @param polygonIndices  The indices of the original polygon that map to the new one.
43cb93a386Sopenharmony_ci * @return true if an offset simple polygon exists, false otherwise.
44cb93a386Sopenharmony_ci */
45cb93a386Sopenharmony_cibool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
46cb93a386Sopenharmony_ci                           const SkRect& bounds, SkScalar offset, SkTDArray<SkPoint>* offsetPolygon,
47cb93a386Sopenharmony_ci                           SkTDArray<int>* polygonIndices = nullptr);
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci/**
50cb93a386Sopenharmony_ci * Compute the number of points needed for a circular join when offsetting a vertex.
51cb93a386Sopenharmony_ci * The lengths of offset0 and offset1 don't have to equal |offset| -- only the direction matters.
52cb93a386Sopenharmony_ci * The segment lengths will be approximately four pixels.
53cb93a386Sopenharmony_ci *
54cb93a386Sopenharmony_ci * @param offset0  Starting offset vector direction.
55cb93a386Sopenharmony_ci * @param offset1  Ending offset vector direction.
56cb93a386Sopenharmony_ci * @param offset  Offset value (can be negative).
57cb93a386Sopenharmony_ci * @param rotSin  Sine of rotation delta per step.
58cb93a386Sopenharmony_ci * @param rotCos  Cosine of rotation delta per step.
59cb93a386Sopenharmony_ci * @param n  Number of steps to fill out the arc.
60cb93a386Sopenharmony_ci * @return true for success, false otherwise
61cb93a386Sopenharmony_ci */
62cb93a386Sopenharmony_cibool SkComputeRadialSteps(const SkVector& offset0, const SkVector& offset1, SkScalar offset,
63cb93a386Sopenharmony_ci                          SkScalar* rotSin, SkScalar* rotCos, int* n);
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci/**
66cb93a386Sopenharmony_ci * Determine winding direction for a polygon.
67cb93a386Sopenharmony_ci * The input polygon must be simple or the result will be meaningless.
68cb93a386Sopenharmony_ci *
69cb93a386Sopenharmony_ci * @param polygonVerts  Array of points representing the vertices of the polygon.
70cb93a386Sopenharmony_ci * @param polygonSize  Number of vertices in the polygon.
71cb93a386Sopenharmony_ci * @return 1 for cw, -1 for ccw, and 0 if zero signed area (either degenerate or self-intersecting).
72cb93a386Sopenharmony_ci *         The y-axis is assumed to be pointing down.
73cb93a386Sopenharmony_ci */
74cb93a386Sopenharmony_ciint SkGetPolygonWinding(const SkPoint* polygonVerts, int polygonSize);
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ci/**
77cb93a386Sopenharmony_ci * Determine whether a polygon is convex or not.
78cb93a386Sopenharmony_ci *
79cb93a386Sopenharmony_ci * @param polygonVerts  Array of points representing the vertices of the polygon.
80cb93a386Sopenharmony_ci * @param polygonSize  Number of vertices in the polygon.
81cb93a386Sopenharmony_ci * @return true if the polygon is convex, false otherwise.
82cb93a386Sopenharmony_ci */
83cb93a386Sopenharmony_cibool SkIsConvexPolygon(const SkPoint* polygonVerts, int polygonSize);
84cb93a386Sopenharmony_ci
85cb93a386Sopenharmony_ci/**
86cb93a386Sopenharmony_ci * Determine whether a polygon is simple (i.e., not self-intersecting) or not.
87cb93a386Sopenharmony_ci * The input polygon must have no coincident vertices or the test will fail.
88cb93a386Sopenharmony_ci *
89cb93a386Sopenharmony_ci * @param polygonVerts  Array of points representing the vertices of the polygon.
90cb93a386Sopenharmony_ci * @param polygonSize  Number of vertices in the polygon.
91cb93a386Sopenharmony_ci * @return true if the polygon is simple, false otherwise.
92cb93a386Sopenharmony_ci */
93cb93a386Sopenharmony_ci bool SkIsSimplePolygon(const SkPoint* polygonVerts, int polygonSize);
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci /**
96cb93a386Sopenharmony_ci  * Compute indices to triangulate the given polygon.
97cb93a386Sopenharmony_ci  * The input polygon must be simple (i.e. it is not self-intersecting)
98cb93a386Sopenharmony_ci  * and have no coincident vertices or collinear edges.
99cb93a386Sopenharmony_ci  *
100cb93a386Sopenharmony_ci  * @param polygonVerts  Array of points representing the vertices of the polygon.
101cb93a386Sopenharmony_ci  * @param indexMap Mapping from index in the given array to the final index in the triangulation.
102cb93a386Sopenharmony_ci  * @param polygonSize  Number of vertices in the polygon.
103cb93a386Sopenharmony_ci  * @param triangleIndices  Indices of the resulting triangulation.
104cb93a386Sopenharmony_ci  * @return true if successful, false otherwise.
105cb93a386Sopenharmony_ci  */
106cb93a386Sopenharmony_ci bool SkTriangulateSimplePolygon(const SkPoint* polygonVerts, uint16_t* indexMap, int polygonSize,
107cb93a386Sopenharmony_ci                                 SkTDArray<uint16_t>* triangleIndices);
108cb93a386Sopenharmony_ci
109cb93a386Sopenharmony_ci// Experiment: doesn't handle really big floats (returns false), always returns true for count <= 3
110cb93a386Sopenharmony_cibool SkIsPolyConvex_experimental(const SkPoint[], int count);
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ci#endif
113