mandoline.cpp 6.2 KB

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