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 "SkTDArray.h" 14 #include "SkPoint.h" 15 16 /** 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 */ 26 bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, 27 SkScalar inset, SkTDArray<SkPoint>* insetPolygon); 28 29 /** 30 * Generates a simple polygon (if possible) that is offset a constant distance from the boundary 31 * of a given simple polygon. 32 * The input polygon must be simple and have no coincident vertices or collinear edges. 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 */ 42 bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, 43 SkScalar offset, SkTDArray<SkPoint>* offsetPolygon, 44 SkTDArray<int>* polygonIndices = nullptr); 45 46 /** 47 * Compute the number of points needed for a circular join when offsetting a vertex. 48 * The lengths of offset0 and offset1 don't have to equal |offset| -- only the direction matters. 49 * The segment lengths will be approximately four pixels. 50 * 51 * @param offset0 Starting offset vector direction. 52 * @param offset1 Ending offset vector direction. 53 * @param offset Offset value (can be negative). 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. 57 * @return true for success, false otherwise 58 */ 59 bool SkComputeRadialSteps(const SkVector& offset0, const SkVector& offset1, SkScalar offset, 60 SkScalar* rotSin, SkScalar* rotCos, int* n); 61 62 /** 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 /** 83 * Determine whether a polygon is simple (i.e., not self-intersecting) or not. 84 * The input polygon must have no coincident vertices or the test will fail. 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 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 109 #endif 110