| 1 |
/* |
| 2 |
* Copyright 2018 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 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPath.h" |
| 13 |
#include "include/core/SkPathBuilder.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "src/base/SkRandom.h" |
| 20 |
#include "src/core/SkGeometry.h" |
| 21 |
|
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
namespace skiagm { |
| 25 |
|
| 26 |
// Slices paths into sliver-size contours shaped like ice cream cones. |
| 27 |
class MandolineSlicer { |
| 28 |
public: |
| 29 |
inline static constexpr int kDefaultSubdivisions = 10; |
| 30 |
|
| 31 |
MandolineSlicer(SkPoint anchorPt) { |
| 32 |
this->reset(anchorPt); |
| 33 |
} |
| 34 |
|
| 35 |
void reset(SkPoint anchorPt) { |
| 36 |
fBuilder.reset(); |
| 37 |
|
| 38 |
// see https://skia-review.googlesource.com/c/skia/+/1055736 |
| 39 |
fBuilder.setIsVolatile(true); |
| 40 |
|
| 41 |
fLastPt = fAnchorPt = anchorPt; |
| 42 |
} |
| 43 |
|
| 44 |
void sliceLine(SkPoint pt, int numSubdivisions = kDefaultSubdivisions) { |
| 45 |
if (numSubdivisions <= 0) { |
| 46 |
fBuilder.moveTo(fAnchorPt); |
| 47 |
fBuilder.lineTo(fLastPt); |
| 48 |
fBuilder.lineTo(pt); |
| 49 |
fBuilder.close(); |
| 50 |
fLastPt = pt; |
| 51 |
return; |
| 52 |
} |
| 53 |
float T = this->chooseChopT(numSubdivisions); |
| 54 |
if (0 == T) { |
| 55 |
return; |
| 56 |
} |
| 57 |
SkPoint midpt = fLastPt * (1 - T) + pt * T; |
| 58 |
this->sliceLine(midpt, numSubdivisions - 1); |
| 59 |
this->sliceLine(pt, numSubdivisions - 1); |
| 60 |
} |
| 61 |
|
| 62 |
void sliceQuadratic(SkPoint p1, SkPoint p2, int numSubdivisions = kDefaultSubdivisions) { |
| 63 |
if (numSubdivisions <= 0) { |
| 64 |
fBuilder.moveTo(fAnchorPt); |
| 65 |
fBuilder.lineTo(fLastPt); |
| 66 |
fBuilder.quadTo(p1, p2); |
| 67 |
fBuilder.close(); |
| 68 |
fLastPt = p2; |
| 69 |
return; |
| 70 |
} |
| 71 |
float T = this->chooseChopT(numSubdivisions); |
| 72 |
if (0 == T) { |
| 73 |
return; |
| 74 |
} |
| 75 |
SkPoint P[3] = {fLastPt, p1, p2}, PP[5]; |
| 76 |
SkChopQuadAt(P, PP, T); |
| 77 |
this->sliceQuadratic(PP[1], PP[2], numSubdivisions - 1); |
| 78 |
this->sliceQuadratic(PP[3], PP[4], numSubdivisions - 1); |
| 79 |
} |
| 80 |
|
| 81 |
void sliceCubic(SkPoint p1, SkPoint p2, SkPoint p3, |
| 82 |
int numSubdivisions = kDefaultSubdivisions) { |
| 83 |
if (numSubdivisions <= 0) { |
| 84 |
fBuilder.moveTo(fAnchorPt); |
| 85 |
fBuilder.lineTo(fLastPt); |
| 86 |
fBuilder.cubicTo(p1, p2, p3); |
| 87 |
fBuilder.close(); |
| 88 |
fLastPt = p3; |
| 89 |
return; |
| 90 |
} |
| 91 |
float T = this->chooseChopT(numSubdivisions); |
| 92 |
if (0 == T) { |
| 93 |
return; |
| 94 |
} |
| 95 |
SkPoint P[4] = {fLastPt, p1, p2, p3}, PP[7]; |
| 96 |
SkChopCubicAt(P, PP, T); |
| 97 |
this->sliceCubic(PP[1], PP[2], PP[3], numSubdivisions - 1); |
| 98 |
this->sliceCubic(PP[4], PP[5], PP[6], numSubdivisions - 1); |
| 99 |
} |
| 100 |
|
| 101 |
void sliceConic(SkPoint p1, SkPoint p2, float w, int numSubdivisions = kDefaultSubdivisions) { |
| 102 |
if (numSubdivisions <= 0) { |
| 103 |
fBuilder.moveTo(fAnchorPt); |
| 104 |
fBuilder.lineTo(fLastPt); |
| 105 |
fBuilder.conicTo(p1, p2, w); |
| 106 |
fBuilder.close(); |
| 107 |
fLastPt = p2; |
| 108 |
return; |
| 109 |
} |
| 110 |
float T = this->chooseChopT(numSubdivisions); |
| 111 |
if (0 == T) { |
| 112 |
return; |
| 113 |
} |
| 114 |
SkConic conic(fLastPt, p1, p2, w), halves[2]; |
| 115 |
if (!conic.chopAt(T, halves)) { |
| 116 |
SK_ABORT("SkConic::chopAt failed"); |
| 117 |
} |
| 118 |
this->sliceConic(halves[0].fPts[1], halves[0].fPts[2], halves[0].fW, numSubdivisions - 1); |
| 119 |
this->sliceConic(halves[1].fPts[1], halves[1].fPts[2], halves[1].fW, numSubdivisions - 1); |
| 120 |
} |
| 121 |
|
| 122 |
SkPath path() { return fBuilder.snapshot(); } |
| 123 |
|
| 124 |
private: |
| 125 |
float chooseChopT(int numSubdivisions) { |
| 126 |
SkASSERT(numSubdivisions > 0); |
| 127 |
if (numSubdivisions > 1) { |
| 128 |
return .5f; |
| 129 |
} |
| 130 |
float T = (0 == fRand.nextU() % 10) ? 0 : scalbnf(1, -(int)fRand.nextRangeU(10, 149)); |
| 131 |
SkASSERT(T >= 0 && T < 1); |
| 132 |
return T; |
| 133 |
} |
| 134 |
|
| 135 |
SkRandom fRand; |
| 136 |
SkPathBuilder fBuilder; |
| 137 |
SkPoint fAnchorPt; |
| 138 |
SkPoint fLastPt; |
| 139 |
}; |
| 140 |
|
| 141 |
class SliverPathsGM : public GM { |
| 142 |
public: |
| 143 |
SliverPathsGM() { |
| 144 |
this->setBGColor(SK_ColorBLACK); |
| 145 |
} |
| 146 |
|
| 147 |
protected: |
| 148 |
SkString getName() const override { return SkString("mandoline"); } |
| 149 |
|
| 150 |
SkISize getISize() override { return SkISize::Make(560, 475); } |
| 151 |
|
| 152 |
void onDraw(SkCanvas* canvas) override { |
| 153 |
SkPaint paint; |
| 154 |
paint.setColor(SK_ColorWHITE); |
| 155 |
paint.setAntiAlias(true); |
| 156 |
|
| 157 |
MandolineSlicer mandoline({41, 43}); |
| 158 |
mandoline.sliceCubic({5, 277}, {381, -74}, {243, 162}); |
| 159 |
mandoline.sliceLine({41, 43}); |
| 160 |
canvas->drawPath(mandoline.path(), paint); |
| 161 |
|
| 162 |
mandoline.reset({357.049988f, 446.049988f}); |
| 163 |
mandoline.sliceCubic({472.750000f, -71.950012f}, {639.750000f, 531.950012f}, |
| 164 |
{309.049988f, 347.950012f}); |
| 165 |
mandoline.sliceLine({309.049988f, 419}); |
| 166 |
mandoline.sliceLine({357.049988f, 446.049988f}); |
| 167 |
canvas->drawPath(mandoline.path(), paint); |
| 168 |
|
| 169 |
canvas->save(); |
| 170 |
canvas->translate(421, 105); |
| 171 |
canvas->scale(100, 81); |
| 172 |
mandoline.reset({-cosf(SkDegreesToRadians(-60)), sinf(SkDegreesToRadians(-60))}); |
| 173 |
mandoline.sliceConic({-2, 0}, |
| 174 |
{-cosf(SkDegreesToRadians(60)), sinf(SkDegreesToRadians(60))}, .5f); |
| 175 |
mandoline.sliceConic({-cosf(SkDegreesToRadians(120))*2, sinf(SkDegreesToRadians(120))*2}, |
| 176 |
{1, 0}, .5f); |
| 177 |
mandoline.sliceLine({0, 0}); |
| 178 |
mandoline.sliceLine({-cosf(SkDegreesToRadians(-60)), sinf(SkDegreesToRadians(-60))}); |
| 179 |
canvas->drawPath(mandoline.path(), paint); |
| 180 |
canvas->restore(); |
| 181 |
|
| 182 |
canvas->save(); |
| 183 |
canvas->translate(150, 300); |
| 184 |
canvas->scale(75, 75); |
| 185 |
mandoline.reset({1, 0}); |
| 186 |
constexpr int nquads = 5; |
| 187 |
for (int i = 0; i < nquads; ++i) { |
| 188 |
float theta1 = 2*SK_ScalarPI/nquads * (i + .5f); |
| 189 |
float theta2 = 2*SK_ScalarPI/nquads * (i + 1); |
| 190 |
mandoline.sliceQuadratic({cosf(theta1)*2, sinf(theta1)*2}, |
| 191 |
{cosf(theta2), sinf(theta2)}); |
| 192 |
} |
| 193 |
canvas->drawPath(mandoline.path(), paint); |
| 194 |
canvas->restore(); |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
DEF_GM(return new SliverPathsGM;) |
| 199 |
|
| 200 |
} // namespace skiagm |