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 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
| 14 | #include "SkPoint.h" |
| 15 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 16 | /** |
Jim Van Verth | bdde428 | 2018-06-14 09:09:18 -0400 | [diff] [blame] | 17 | * 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 Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 26 | bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, |
| 27 | SkScalar inset, SkTDArray<SkPoint>* insetPolygon); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 28 | |
| 29 | /** |
Jim Van Verth | bdde428 | 2018-06-14 09:09:18 -0400 | [diff] [blame] | 30 | * Generates a simple polygon (if possible) that is offset a constant distance from the boundary |
| 31 | * of a given simple polygon. |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 32 | * 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] | 33 | * |
| 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 Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 42 | bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, |
| 43 | SkScalar offset, SkTDArray<SkPoint>* offsetPolygon, |
| 44 | SkTDArray<int>* polygonIndices = nullptr); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 45 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 46 | /** |
| 47 | * 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] | 48 | * 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] | 49 | * 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 Verth | 66c5dc5 | 2018-08-06 14:38:31 -0400 | [diff] [blame] | 53 | * @param offset Offset value (can be negative). |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 54 | * @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 Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 57 | * @return true for success, false otherwise |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 58 | */ |
Jim Van Verth | 66c5dc5 | 2018-08-06 14:38:31 -0400 | [diff] [blame] | 59 | bool SkComputeRadialSteps(const SkVector& offset0, const SkVector& offset1, SkScalar offset, |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 60 | SkScalar* rotSin, SkScalar* rotCos, int* n); |
| 61 | |
| 62 | /** |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 63 | * 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 | */ |
| 71 | int 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 | */ |
| 80 | bool SkIsConvexPolygon(const SkPoint* polygonVerts, int polygonSize); |
| 81 | |
| 82 | /** |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 83 | * 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] | 84 | * 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] | 85 | * |
| 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 | |
Mike Reed | 64284e1 | 2018-11-30 15:55:15 -0500 | [diff] [blame^] | 106 | // Experiment: doesn't handle really big floats (returns false), always returns true for count <= 3 |
| 107 | bool SkIsPolyConvex_experimental(const SkPoint[], int count); |
| 108 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 109 | #endif |