blob: 7263eca58224dded069da63f7b36958692d43fcd [file] [log] [blame]
Brian Salomonab664fa2017-03-24 16:07:20 +00001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Jim Van Verth4db18ed2018-04-03 10:00:37 -04008#ifndef SkOffsetPolygon_DEFINED
9#define SkOffsetPolygon_DEFINED
Brian Salomonab664fa2017-03-24 16:07:20 +000010
Jim Van Verthda965502017-04-11 15:29:14 -040011#include <functional>
12
Brian Salomonab664fa2017-03-24 16:07:20 +000013#include "SkTDArray.h"
14#include "SkPoint.h"
15
Jim Van Verthda965502017-04-11 15:29:14 -040016/**
Jim Van Verthbdde4282018-06-14 09:09:18 -040017 * Generates a polygon that is inset a constant from the boundary of a given convex polygon.
18 *
19 * @param inputPolygonVerts Array of points representing the vertices of the original polygon.
20 * It should be convex and have no coincident points.
21 * @param inputPolygonSize Number of vertices in the original polygon.
22 * @param inset How far we wish to inset the polygon. This should be a positive value.
23 * @param insetPolygon The resulting inset polygon, if any.
24 * @return true if an inset polygon exists, false otherwise.
25 */
Jim Van Verthda58cac2018-09-05 12:41:56 -040026bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
27 SkScalar inset, SkTDArray<SkPoint>* insetPolygon);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040028
29/**
Jim Van Verthbdde4282018-06-14 09:09:18 -040030 * Generates a simple polygon (if possible) that is offset a constant distance from the boundary
31 * of a given simple polygon.
Jim Van Verth8664a1d2018-06-28 16:26:50 -040032 * The input polygon must be simple and have no coincident vertices or collinear edges.
Jim Van Verthbdde4282018-06-14 09:09:18 -040033 *
34 * @param inputPolygonVerts Array of points representing the vertices of the original polygon.
35 * @param inputPolygonSize Number of vertices in the original polygon.
36 * @param offset How far we wish to offset the polygon.
37 * Positive values indicate insetting, negative values outsetting.
38 * @param offsetPolgon The resulting offset polygon, if any.
39 * @param polygonIndices The indices of the original polygon that map to the new one.
40 * @return true if an offset simple polygon exists, false otherwise.
41 */
Jim Van Verthda58cac2018-09-05 12:41:56 -040042bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
43 SkScalar offset, SkTDArray<SkPoint>* offsetPolygon,
44 SkTDArray<int>* polygonIndices = nullptr);
Brian Salomonab664fa2017-03-24 16:07:20 +000045
Jim Van Verth8664a1d2018-06-28 16:26:50 -040046/**
47 * Compute the number of points needed for a circular join when offsetting a vertex.
Jim Van Verth66c5dc52018-08-06 14:38:31 -040048 * The lengths of offset0 and offset1 don't have to equal |offset| -- only the direction matters.
Jim Van Verth8664a1d2018-06-28 16:26:50 -040049 * The segment lengths will be approximately four pixels.
50 *
51 * @param offset0 Starting offset vector direction.
52 * @param offset1 Ending offset vector direction.
Jim Van Verth66c5dc52018-08-06 14:38:31 -040053 * @param offset Offset value (can be negative).
Jim Van Verth8664a1d2018-06-28 16:26:50 -040054 * @param rotSin Sine of rotation delta per step.
55 * @param rotCos Cosine of rotation delta per step.
56 * @param n Number of steps to fill out the arc.
Jim Van Verth061cc212018-07-11 14:09:09 -040057 * @return true for success, false otherwise
Jim Van Verth8664a1d2018-06-28 16:26:50 -040058 */
Jim Van Verth66c5dc52018-08-06 14:38:31 -040059bool SkComputeRadialSteps(const SkVector& offset0, const SkVector& offset1, SkScalar offset,
Jim Van Verth8664a1d2018-06-28 16:26:50 -040060 SkScalar* rotSin, SkScalar* rotCos, int* n);
61
62/**
Jim Van Verth6784ffa2018-07-03 16:12:39 -040063 * Determine winding direction for a polygon.
64 * The input polygon must be simple or the result will be meaningless.
65 *
66 * @param polygonVerts Array of points representing the vertices of the polygon.
67 * @param polygonSize Number of vertices in the polygon.
68 * @return 1 for cw, -1 for ccw, and 0 if zero signed area (either degenerate or self-intersecting).
69 * The y-axis is assumed to be pointing down.
70 */
71int SkGetPolygonWinding(const SkPoint* polygonVerts, int polygonSize);
72
73/**
74 * Determine whether a polygon is convex or not.
75 *
76 * @param polygonVerts Array of points representing the vertices of the polygon.
77 * @param polygonSize Number of vertices in the polygon.
78 * @return true if the polygon is convex, false otherwise.
79 */
80bool SkIsConvexPolygon(const SkPoint* polygonVerts, int polygonSize);
81
82/**
Jim Van Verth8664a1d2018-06-28 16:26:50 -040083 * Determine whether a polygon is simple (i.e., not self-intersecting) or not.
Jim Van Verth6784ffa2018-07-03 16:12:39 -040084 * The input polygon must have no coincident vertices or the test will fail.
Jim Van Verth8664a1d2018-06-28 16:26:50 -040085 *
86 * @param polygonVerts Array of points representing the vertices of the polygon.
87 * @param polygonSize Number of vertices in the polygon.
88 * @return true if the polygon is simple, false otherwise.
89 */
90 bool SkIsSimplePolygon(const SkPoint* polygonVerts, int polygonSize);
91
92 /**
93 * Compute indices to triangulate the given polygon.
94 * The input polygon must be simple (i.e. it is not self-intersecting)
95 * and have no coincident vertices or collinear edges.
96 *
97 * @param polygonVerts Array of points representing the vertices of the polygon.
98 * @param indexMap Mapping from index in the given array to the final index in the triangulation.
99 * @param polygonSize Number of vertices in the polygon.
100 * @param triangleIndices Indices of the resulting triangulation.
101 * @return true if successful, false otherwise.
102 */
103 bool SkTriangulateSimplePolygon(const SkPoint* polygonVerts, uint16_t* indexMap, int polygonSize,
104 SkTDArray<uint16_t>* triangleIndices);
105
Brian Salomonab664fa2017-03-24 16:07:20 +0000106#endif