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