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