Add variable offset support
Bug: skia:7970
Change-Id: I9dadf75f21e19ebe26f82643bcc47dd5794d8970
Reviewed-on: https://skia-review.googlesource.com/134421
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/utils/SkOffsetPolygon.h b/src/utils/SkOffsetPolygon.h
index b6c3a22..4c98ac0 100755
--- a/src/utils/SkOffsetPolygon.h
+++ b/src/utils/SkOffsetPolygon.h
@@ -14,44 +14,76 @@
#include "SkPoint.h"
/**
- * Generates a polygon that is inset a given distance from the boundary of a given convex polygon.
+ * Generates a polygon that is inset a variable distance (controlled by offsetDistanceFunc)
+ * from the boundary of a given convex polygon.
*
* @param inputPolygonVerts Array of points representing the vertices of the original polygon.
* It should be convex and have no coincident points.
* @param inputPolygonSize Number of vertices in the original polygon.
- * @param insetDistanceFunc How far we wish to inset the polygon for a given index in the array.
+ * @param insetDistanceFunc How far we wish to inset the polygon for a given position.
* This should return a positive value.
* @param insetPolygon The resulting inset polygon, if any.
* @return true if an inset polygon exists, false otherwise.
*/
bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
- std::function<SkScalar(int index)> insetDistanceFunc,
+ std::function<SkScalar(const SkPoint&)> insetDistanceFunc,
SkTDArray<SkPoint>* insetPolygon);
+/**
+ * Generates a polygon that is inset a constant from the boundary of a given convex polygon.
+ *
+ * @param inputPolygonVerts Array of points representing the vertices of the original polygon.
+ * It should be convex and have no coincident points.
+ * @param inputPolygonSize Number of vertices in the original polygon.
+ * @param inset How far we wish to inset the polygon. This should be a positive value.
+ * @param insetPolygon The resulting inset polygon, if any.
+ * @return true if an inset polygon exists, false otherwise.
+ */
inline bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
SkScalar inset, SkTDArray<SkPoint>* insetPolygon) {
return SkInsetConvexPolygon(inputPolygonVerts, inputPolygonSize,
- [inset](int) { return inset; },
+ [inset](const SkPoint&) { return inset; },
insetPolygon);
}
/**
- * Generates a simple polygon (if possible) that is offset a given distance from the boundary of a
- * given simple polygon.
+ * Generates a simple polygon (if possible) that is offset a variable distance (controlled by
+ * offsetDistanceFunc) from the boundary of a given simple polygon.
*
* @param inputPolygonVerts Array of points representing the vertices of the original polygon.
* @param inputPolygonSize Number of vertices in the original polygon.
- * @param offset How far we wish to offset the polygon.
- * Positive value means inset, negative value means outset.
+ * @param offsetDistanceFunc How far we wish to offset the polygon for a given position.
+ * Positive values indicate insetting, negative values outsetting.
* @param offsetPolgon The resulting offset polygon, if any.
* @param polygonIndices The indices of the original polygon that map to the new one.
* @return true if an offset simple polygon exists, false otherwise.
*/
bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
- SkScalar offset, SkTDArray<SkPoint>* offsetPolygon,
+ std::function<SkScalar(const SkPoint&)> offsetDistanceFunc,
+ SkTDArray<SkPoint>* offsetPolygon,
SkTDArray<int>* polygonIndices = nullptr);
/**
+ * Generates a simple polygon (if possible) that is offset a constant distance from the boundary
+ * of a given simple polygon.
+ *
+ * @param inputPolygonVerts Array of points representing the vertices of the original polygon.
+ * @param inputPolygonSize Number of vertices in the original polygon.
+ * @param offset How far we wish to offset the polygon.
+ * Positive values indicate insetting, negative values outsetting.
+ * @param offsetPolgon The resulting offset polygon, if any.
+ * @param polygonIndices The indices of the original polygon that map to the new one.
+ * @return true if an offset simple polygon exists, false otherwise.
+ */
+inline bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
+ SkScalar offset, SkTDArray<SkPoint>* offsetPolygon,
+ SkTDArray<int>* polygonIndices = nullptr) {
+ return SkOffsetSimplePolygon(inputPolygonVerts, inputPolygonSize,
+ [offset](const SkPoint&) { return offset; },
+ offsetPolygon, polygonIndices);
+}
+
+/**
* Offset a segment by the given distance at each point.
* Uses the outer tangents of two circles centered on each endpoint.
* See: https://en.wikipedia.org/wiki/Tangent_lines_to_circles