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