poly.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. // Poly path support NaN point
  42. import Path from 'zrender/lib/graphic/Path.js';
  43. import PathProxy from 'zrender/lib/core/PathProxy.js';
  44. import { cubicRootAt, cubicAt } from 'zrender/lib/core/curve.js';
  45. import tokens from '../../visual/tokens.js';
  46. import { isPointIllegal } from './helper.js';
  47. var mathMin = Math.min;
  48. var mathMax = Math.max;
  49. /**
  50. * Draw smoothed line in non-monotone, in may cause undesired curve in extreme
  51. * situations. This should be used when points are non-monotone neither in x or
  52. * y dimension.
  53. */
  54. function drawSegment(ctx, points, start, segLen, allLen, dir, smooth, smoothMonotone, connectNulls) {
  55. var prevX;
  56. var prevY;
  57. var cpx0;
  58. var cpy0;
  59. var cpx1;
  60. var cpy1;
  61. var idx = start;
  62. var k = 0;
  63. for (; k < segLen; k++) {
  64. var x = points[idx * 2];
  65. var y = points[idx * 2 + 1];
  66. if (idx >= allLen || idx < 0) {
  67. break;
  68. }
  69. if (isPointIllegal(x, y)) {
  70. if (connectNulls) {
  71. idx += dir;
  72. continue;
  73. }
  74. break;
  75. }
  76. if (idx === start) {
  77. ctx[dir > 0 ? 'moveTo' : 'lineTo'](x, y);
  78. cpx0 = x;
  79. cpy0 = y;
  80. } else {
  81. var dx = x - prevX;
  82. var dy = y - prevY;
  83. // Ignore tiny segment.
  84. if (dx * dx + dy * dy < 0.5) {
  85. idx += dir;
  86. continue;
  87. }
  88. if (smooth > 0) {
  89. var nextIdx = idx + dir;
  90. var nextX = points[nextIdx * 2];
  91. var nextY = points[nextIdx * 2 + 1];
  92. // Ignore duplicate point
  93. while (nextX === x && nextY === y && k < segLen) {
  94. k++;
  95. nextIdx += dir;
  96. idx += dir;
  97. nextX = points[nextIdx * 2];
  98. nextY = points[nextIdx * 2 + 1];
  99. x = points[idx * 2];
  100. y = points[idx * 2 + 1];
  101. dx = x - prevX;
  102. dy = y - prevY;
  103. }
  104. var tmpK = k + 1;
  105. if (connectNulls) {
  106. // Find next point not null
  107. while (isPointIllegal(nextX, nextY) && tmpK < segLen) {
  108. tmpK++;
  109. nextIdx += dir;
  110. nextX = points[nextIdx * 2];
  111. nextY = points[nextIdx * 2 + 1];
  112. }
  113. }
  114. var ratioNextSeg = 0.5;
  115. var vx = 0;
  116. var vy = 0;
  117. var nextCpx0 = void 0;
  118. var nextCpy0 = void 0;
  119. // Is last point
  120. if (tmpK >= segLen || isPointIllegal(nextX, nextY)) {
  121. cpx1 = x;
  122. cpy1 = y;
  123. } else {
  124. vx = nextX - prevX;
  125. vy = nextY - prevY;
  126. var dx0 = x - prevX;
  127. var dx1 = nextX - x;
  128. var dy0 = y - prevY;
  129. var dy1 = nextY - y;
  130. var lenPrevSeg = void 0;
  131. var lenNextSeg = void 0;
  132. if (smoothMonotone === 'x') {
  133. lenPrevSeg = Math.abs(dx0);
  134. lenNextSeg = Math.abs(dx1);
  135. var dir_1 = vx > 0 ? 1 : -1;
  136. cpx1 = x - dir_1 * lenPrevSeg * smooth;
  137. cpy1 = y;
  138. nextCpx0 = x + dir_1 * lenNextSeg * smooth;
  139. nextCpy0 = y;
  140. } else if (smoothMonotone === 'y') {
  141. lenPrevSeg = Math.abs(dy0);
  142. lenNextSeg = Math.abs(dy1);
  143. var dir_2 = vy > 0 ? 1 : -1;
  144. cpx1 = x;
  145. cpy1 = y - dir_2 * lenPrevSeg * smooth;
  146. nextCpx0 = x;
  147. nextCpy0 = y + dir_2 * lenNextSeg * smooth;
  148. } else {
  149. lenPrevSeg = Math.sqrt(dx0 * dx0 + dy0 * dy0);
  150. lenNextSeg = Math.sqrt(dx1 * dx1 + dy1 * dy1);
  151. // Use ratio of seg length
  152. ratioNextSeg = lenNextSeg / (lenNextSeg + lenPrevSeg);
  153. cpx1 = x - vx * smooth * (1 - ratioNextSeg);
  154. cpy1 = y - vy * smooth * (1 - ratioNextSeg);
  155. // cp0 of next segment
  156. nextCpx0 = x + vx * smooth * ratioNextSeg;
  157. nextCpy0 = y + vy * smooth * ratioNextSeg;
  158. // Smooth constraint between point and next point.
  159. // Avoid exceeding extreme after smoothing.
  160. nextCpx0 = mathMin(nextCpx0, mathMax(nextX, x));
  161. nextCpy0 = mathMin(nextCpy0, mathMax(nextY, y));
  162. nextCpx0 = mathMax(nextCpx0, mathMin(nextX, x));
  163. nextCpy0 = mathMax(nextCpy0, mathMin(nextY, y));
  164. // Reclaculate cp1 based on the adjusted cp0 of next seg.
  165. vx = nextCpx0 - x;
  166. vy = nextCpy0 - y;
  167. cpx1 = x - vx * lenPrevSeg / lenNextSeg;
  168. cpy1 = y - vy * lenPrevSeg / lenNextSeg;
  169. // Smooth constraint between point and prev point.
  170. // Avoid exceeding extreme after smoothing.
  171. cpx1 = mathMin(cpx1, mathMax(prevX, x));
  172. cpy1 = mathMin(cpy1, mathMax(prevY, y));
  173. cpx1 = mathMax(cpx1, mathMin(prevX, x));
  174. cpy1 = mathMax(cpy1, mathMin(prevY, y));
  175. // Adjust next cp0 again.
  176. vx = x - cpx1;
  177. vy = y - cpy1;
  178. nextCpx0 = x + vx * lenNextSeg / lenPrevSeg;
  179. nextCpy0 = y + vy * lenNextSeg / lenPrevSeg;
  180. }
  181. }
  182. ctx.bezierCurveTo(cpx0, cpy0, cpx1, cpy1, x, y);
  183. cpx0 = nextCpx0;
  184. cpy0 = nextCpy0;
  185. } else {
  186. ctx.lineTo(x, y);
  187. }
  188. }
  189. prevX = x;
  190. prevY = y;
  191. idx += dir;
  192. }
  193. return k;
  194. }
  195. var ECPolylineShape = /** @class */function () {
  196. function ECPolylineShape() {
  197. this.smooth = 0;
  198. this.smoothConstraint = true;
  199. }
  200. return ECPolylineShape;
  201. }();
  202. var ECPolyline = /** @class */function (_super) {
  203. __extends(ECPolyline, _super);
  204. function ECPolyline(opts) {
  205. var _this = _super.call(this, opts) || this;
  206. _this.type = 'ec-polyline';
  207. return _this;
  208. }
  209. ECPolyline.prototype.getDefaultStyle = function () {
  210. return {
  211. stroke: tokens.color.neutral99,
  212. fill: null
  213. };
  214. };
  215. ECPolyline.prototype.getDefaultShape = function () {
  216. return new ECPolylineShape();
  217. };
  218. ECPolyline.prototype.buildPath = function (ctx, shape) {
  219. var points = shape.points;
  220. var i = 0;
  221. var len = points.length / 2;
  222. // const result = getBoundingBox(points, shape.smoothConstraint);
  223. if (shape.connectNulls) {
  224. // Must remove first and last null values avoid draw error in polygon
  225. for (; len > 0; len--) {
  226. if (!isPointIllegal(points[len * 2 - 2], points[len * 2 - 1])) {
  227. break;
  228. }
  229. }
  230. for (; i < len; i++) {
  231. if (!isPointIllegal(points[i * 2], points[i * 2 + 1])) {
  232. break;
  233. }
  234. }
  235. }
  236. while (i < len) {
  237. i += drawSegment(ctx, points, i, len, len, 1, shape.smooth, shape.smoothMonotone, shape.connectNulls) + 1;
  238. }
  239. };
  240. ECPolyline.prototype.getPointOn = function (xOrY, dim) {
  241. if (!this.path) {
  242. this.createPathProxy();
  243. this.buildPath(this.path, this.shape);
  244. }
  245. var path = this.path;
  246. var data = path.data;
  247. var CMD = PathProxy.CMD;
  248. var x0;
  249. var y0;
  250. var isDimX = dim === 'x';
  251. var roots = [];
  252. for (var i = 0; i < data.length;) {
  253. var cmd = data[i++];
  254. var x = void 0;
  255. var y = void 0;
  256. var x2 = void 0;
  257. var y2 = void 0;
  258. var x3 = void 0;
  259. var y3 = void 0;
  260. var t = void 0;
  261. switch (cmd) {
  262. case CMD.M:
  263. x0 = data[i++];
  264. y0 = data[i++];
  265. break;
  266. case CMD.L:
  267. x = data[i++];
  268. y = data[i++];
  269. t = isDimX ? (xOrY - x0) / (x - x0) : (xOrY - y0) / (y - y0);
  270. if (t <= 1 && t >= 0) {
  271. var val = isDimX ? (y - y0) * t + y0 : (x - x0) * t + x0;
  272. return isDimX ? [xOrY, val] : [val, xOrY];
  273. }
  274. x0 = x;
  275. y0 = y;
  276. break;
  277. case CMD.C:
  278. x = data[i++];
  279. y = data[i++];
  280. x2 = data[i++];
  281. y2 = data[i++];
  282. x3 = data[i++];
  283. y3 = data[i++];
  284. var nRoot = isDimX ? cubicRootAt(x0, x, x2, x3, xOrY, roots) : cubicRootAt(y0, y, y2, y3, xOrY, roots);
  285. if (nRoot > 0) {
  286. for (var i_1 = 0; i_1 < nRoot; i_1++) {
  287. var t_1 = roots[i_1];
  288. if (t_1 <= 1 && t_1 >= 0) {
  289. var val = isDimX ? cubicAt(y0, y, y2, y3, t_1) : cubicAt(x0, x, x2, x3, t_1);
  290. return isDimX ? [xOrY, val] : [val, xOrY];
  291. }
  292. }
  293. }
  294. x0 = x3;
  295. y0 = y3;
  296. break;
  297. }
  298. }
  299. };
  300. return ECPolyline;
  301. }(Path);
  302. export { ECPolyline };
  303. var ECPolygonShape = /** @class */function (_super) {
  304. __extends(ECPolygonShape, _super);
  305. function ECPolygonShape() {
  306. return _super !== null && _super.apply(this, arguments) || this;
  307. }
  308. return ECPolygonShape;
  309. }(ECPolylineShape);
  310. var ECPolygon = /** @class */function (_super) {
  311. __extends(ECPolygon, _super);
  312. function ECPolygon(opts) {
  313. var _this = _super.call(this, opts) || this;
  314. _this.type = 'ec-polygon';
  315. return _this;
  316. }
  317. ECPolygon.prototype.getDefaultShape = function () {
  318. return new ECPolygonShape();
  319. };
  320. ECPolygon.prototype.buildPath = function (ctx, shape) {
  321. var points = shape.points;
  322. var stackedOnPoints = shape.stackedOnPoints;
  323. var i = 0;
  324. var len = points.length / 2;
  325. var smoothMonotone = shape.smoothMonotone;
  326. if (shape.connectNulls) {
  327. // Must remove first and last null values avoid draw error in polygon
  328. for (; len > 0; len--) {
  329. if (!isPointIllegal(points[len * 2 - 2], points[len * 2 - 1])) {
  330. break;
  331. }
  332. }
  333. for (; i < len; i++) {
  334. if (!isPointIllegal(points[i * 2], points[i * 2 + 1])) {
  335. break;
  336. }
  337. }
  338. }
  339. while (i < len) {
  340. var k = drawSegment(ctx, points, i, len, len, 1, shape.smooth, smoothMonotone, shape.connectNulls);
  341. drawSegment(ctx, stackedOnPoints, i + k - 1, k, len, -1, shape.stackedOnSmooth, smoothMonotone, shape.connectNulls);
  342. i += k + 1;
  343. ctx.closePath();
  344. }
  345. };
  346. return ECPolygon;
  347. }(Path);
  348. export { ECPolygon };