SkPolyUtils.h 4.9 KB

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