Path.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. * Creates free form 2d path using series of points, lines or curves.
  4. *
  5. **/
  6. THREE.Path = function ( points ) {
  7. THREE.CurvePath.call(this);
  8. this.actions = [];
  9. if ( points ) {
  10. this.fromPoints( points );
  11. }
  12. };
  13. THREE.Path.prototype = Object.create( THREE.CurvePath.prototype );
  14. THREE.Path.prototype.constructor = THREE.Path;
  15. THREE.PathActions = {
  16. MOVE_TO: 'moveTo',
  17. LINE_TO: 'lineTo',
  18. QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
  19. BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve
  20. CSPLINE_THRU: 'splineThru', // Catmull-rom spline
  21. ARC: 'arc', // Circle
  22. ELLIPSE: 'ellipse'
  23. };
  24. // TODO Clean up PATH API
  25. // Create path using straight lines to connect all points
  26. // - vectors: array of Vector2
  27. THREE.Path.prototype.fromPoints = function ( vectors ) {
  28. this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y );
  29. for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) {
  30. this.lineTo( vectors[ v ].x, vectors[ v ].y );
  31. };
  32. };
  33. // startPath() endPath()?
  34. THREE.Path.prototype.moveTo = function ( x, y ) {
  35. var args = Array.prototype.slice.call( arguments );
  36. this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );
  37. };
  38. THREE.Path.prototype.lineTo = function ( x, y ) {
  39. var args = Array.prototype.slice.call( arguments );
  40. var lastargs = this.actions[ this.actions.length - 1 ].args;
  41. var x0 = lastargs[ lastargs.length - 2 ];
  42. var y0 = lastargs[ lastargs.length - 1 ];
  43. var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
  44. this.curves.push( curve );
  45. this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
  46. };
  47. THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {
  48. var args = Array.prototype.slice.call( arguments );
  49. var lastargs = this.actions[ this.actions.length - 1 ].args;
  50. var x0 = lastargs[ lastargs.length - 2 ];
  51. var y0 = lastargs[ lastargs.length - 1 ];
  52. var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
  53. new THREE.Vector2( aCPx, aCPy ),
  54. new THREE.Vector2( aX, aY ) );
  55. this.curves.push( curve );
  56. this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
  57. };
  58. THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
  59. aCP2x, aCP2y,
  60. aX, aY ) {
  61. var args = Array.prototype.slice.call( arguments );
  62. var lastargs = this.actions[ this.actions.length - 1 ].args;
  63. var x0 = lastargs[ lastargs.length - 2 ];
  64. var y0 = lastargs[ lastargs.length - 1 ];
  65. var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
  66. new THREE.Vector2( aCP1x, aCP1y ),
  67. new THREE.Vector2( aCP2x, aCP2y ),
  68. new THREE.Vector2( aX, aY ) );
  69. this.curves.push( curve );
  70. this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
  71. };
  72. THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
  73. var args = Array.prototype.slice.call( arguments );
  74. var lastargs = this.actions[ this.actions.length - 1 ].args;
  75. var x0 = lastargs[ lastargs.length - 2 ];
  76. var y0 = lastargs[ lastargs.length - 1 ];
  77. //---
  78. var npts = [ new THREE.Vector2( x0, y0 ) ];
  79. Array.prototype.push.apply( npts, pts );
  80. var curve = new THREE.SplineCurve( npts );
  81. this.curves.push( curve );
  82. this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } );
  83. };
  84. // FUTURE: Change the API or follow canvas API?
  85. THREE.Path.prototype.arc = function ( aX, aY, aRadius,
  86. aStartAngle, aEndAngle, aClockwise ) {
  87. var lastargs = this.actions[ this.actions.length - 1].args;
  88. var x0 = lastargs[ lastargs.length - 2 ];
  89. var y0 = lastargs[ lastargs.length - 1 ];
  90. this.absarc(aX + x0, aY + y0, aRadius,
  91. aStartAngle, aEndAngle, aClockwise );
  92. };
  93. THREE.Path.prototype.absarc = function ( aX, aY, aRadius,
  94. aStartAngle, aEndAngle, aClockwise ) {
  95. this.absellipse(aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise);
  96. };
  97. THREE.Path.prototype.ellipse = function ( aX, aY, xRadius, yRadius,
  98. aStartAngle, aEndAngle, aClockwise ) {
  99. var lastargs = this.actions[ this.actions.length - 1].args;
  100. var x0 = lastargs[ lastargs.length - 2 ];
  101. var y0 = lastargs[ lastargs.length - 1 ];
  102. this.absellipse(aX + x0, aY + y0, xRadius, yRadius,
  103. aStartAngle, aEndAngle, aClockwise );
  104. };
  105. THREE.Path.prototype.absellipse = function ( aX, aY, xRadius, yRadius,
  106. aStartAngle, aEndAngle, aClockwise ) {
  107. var args = Array.prototype.slice.call( arguments );
  108. var curve = new THREE.EllipseCurve( aX, aY, xRadius, yRadius,
  109. aStartAngle, aEndAngle, aClockwise );
  110. this.curves.push( curve );
  111. var lastPoint = curve.getPoint(1);
  112. args.push(lastPoint.x);
  113. args.push(lastPoint.y);
  114. this.actions.push( { action: THREE.PathActions.ELLIPSE, args: args } );
  115. };
  116. THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
  117. if ( ! divisions ) divisions = 40;
  118. var points = [];
  119. for ( var i = 0; i < divisions; i ++ ) {
  120. points.push( this.getPoint( i / divisions ) );
  121. //if( !this.getPoint( i / divisions ) ) throw "DIE";
  122. }
  123. // if ( closedPath ) {
  124. //
  125. // points.push( points[ 0 ] );
  126. //
  127. // }
  128. return points;
  129. };
  130. /* Return an array of vectors based on contour of the path */
  131. THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
  132. if (this.useSpacedPoints) {
  133. console.log('tata');
  134. return this.getSpacedPoints( divisions, closedPath );
  135. }
  136. divisions = divisions || 12;
  137. var points = [];
  138. var i, il, item, action, args;
  139. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  140. laste, j,
  141. t, tx, ty;
  142. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  143. item = this.actions[ i ];
  144. action = item.action;
  145. args = item.args;
  146. switch ( action ) {
  147. case THREE.PathActions.MOVE_TO:
  148. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  149. break;
  150. case THREE.PathActions.LINE_TO:
  151. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  152. break;
  153. case THREE.PathActions.QUADRATIC_CURVE_TO:
  154. cpx = args[ 2 ];
  155. cpy = args[ 3 ];
  156. cpx1 = args[ 0 ];
  157. cpy1 = args[ 1 ];
  158. if ( points.length > 0 ) {
  159. laste = points[ points.length - 1 ];
  160. cpx0 = laste.x;
  161. cpy0 = laste.y;
  162. } else {
  163. laste = this.actions[ i - 1 ].args;
  164. cpx0 = laste[ laste.length - 2 ];
  165. cpy0 = laste[ laste.length - 1 ];
  166. }
  167. for ( j = 1; j <= divisions; j ++ ) {
  168. t = j / divisions;
  169. tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  170. ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  171. points.push( new THREE.Vector2( tx, ty ) );
  172. }
  173. break;
  174. case THREE.PathActions.BEZIER_CURVE_TO:
  175. cpx = args[ 4 ];
  176. cpy = args[ 5 ];
  177. cpx1 = args[ 0 ];
  178. cpy1 = args[ 1 ];
  179. cpx2 = args[ 2 ];
  180. cpy2 = args[ 3 ];
  181. if ( points.length > 0 ) {
  182. laste = points[ points.length - 1 ];
  183. cpx0 = laste.x;
  184. cpy0 = laste.y;
  185. } else {
  186. laste = this.actions[ i - 1 ].args;
  187. cpx0 = laste[ laste.length - 2 ];
  188. cpy0 = laste[ laste.length - 1 ];
  189. }
  190. for ( j = 1; j <= divisions; j ++ ) {
  191. t = j / divisions;
  192. tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  193. ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  194. points.push( new THREE.Vector2( tx, ty ) );
  195. }
  196. break;
  197. case THREE.PathActions.CSPLINE_THRU:
  198. laste = this.actions[ i - 1 ].args;
  199. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  200. var spts = [ last ];
  201. var n = divisions * args[ 0 ].length;
  202. spts = spts.concat( args[ 0 ] );
  203. var spline = new THREE.SplineCurve( spts );
  204. for ( j = 1; j <= n; j ++ ) {
  205. points.push( spline.getPointAt( j / n ) ) ;
  206. }
  207. break;
  208. case THREE.PathActions.ARC:
  209. var aX = args[ 0 ], aY = args[ 1 ],
  210. aRadius = args[ 2 ],
  211. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  212. aClockwise = !! args[ 5 ];
  213. var deltaAngle = aEndAngle - aStartAngle;
  214. var angle;
  215. var tdivisions = divisions * 2;
  216. for ( j = 1; j <= tdivisions; j ++ ) {
  217. t = j / tdivisions;
  218. if ( ! aClockwise ) {
  219. t = 1 - t;
  220. }
  221. angle = aStartAngle + t * deltaAngle;
  222. tx = aX + aRadius * Math.cos( angle );
  223. ty = aY + aRadius * Math.sin( angle );
  224. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  225. points.push( new THREE.Vector2( tx, ty ) );
  226. }
  227. //console.log(points);
  228. break;
  229. case THREE.PathActions.ELLIPSE:
  230. var aX = args[ 0 ], aY = args[ 1 ],
  231. xRadius = args[ 2 ],
  232. yRadius = args[ 3 ],
  233. aStartAngle = args[ 4 ], aEndAngle = args[ 5 ],
  234. aClockwise = !! args[ 6 ];
  235. var deltaAngle = aEndAngle - aStartAngle;
  236. var angle;
  237. var tdivisions = divisions * 2;
  238. for ( j = 1; j <= tdivisions; j ++ ) {
  239. t = j / tdivisions;
  240. if ( ! aClockwise ) {
  241. t = 1 - t;
  242. }
  243. angle = aStartAngle + t * deltaAngle;
  244. tx = aX + xRadius * Math.cos( angle );
  245. ty = aY + yRadius * Math.sin( angle );
  246. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  247. points.push( new THREE.Vector2( tx, ty ) );
  248. }
  249. //console.log(points);
  250. break;
  251. } // end switch
  252. }
  253. // Normalize to remove the closing point by default.
  254. var lastPoint = points[ points.length - 1];
  255. var EPSILON = 0.0000000001;
  256. if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
  257. Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
  258. points.splice( points.length - 1, 1);
  259. if ( closedPath ) {
  260. points.push( points[ 0 ] );
  261. }
  262. return points;
  263. };
  264. //
  265. // Breaks path into shapes
  266. //
  267. // Assumptions (if parameter isCCW==true the opposite holds):
  268. // - solid shapes are defined clockwise (CW)
  269. // - holes are defined counterclockwise (CCW)
  270. //
  271. // If parameter noHoles==true:
  272. // - all subPaths are regarded as solid shapes
  273. // - definition order CW/CCW has no relevance
  274. //
  275. THREE.Path.prototype.toShapes = function( isCCW, noHoles ) {
  276. function extractSubpaths( inActions ) {
  277. var i, il, item, action, args;
  278. var subPaths = [], lastPath = new THREE.Path();
  279. for ( i = 0, il = inActions.length; i < il; i ++ ) {
  280. item = inActions[ i ];
  281. args = item.args;
  282. action = item.action;
  283. if ( action == THREE.PathActions.MOVE_TO ) {
  284. if ( lastPath.actions.length != 0 ) {
  285. subPaths.push( lastPath );
  286. lastPath = new THREE.Path();
  287. }
  288. }
  289. lastPath[ action ].apply( lastPath, args );
  290. }
  291. if ( lastPath.actions.length != 0 ) {
  292. subPaths.push( lastPath );
  293. }
  294. // console.log(subPaths);
  295. return subPaths;
  296. }
  297. function toShapesNoHoles( inSubpaths ) {
  298. var shapes = [];
  299. for ( var i = 0, il = inSubpaths.length; i < il; i ++ ) {
  300. var tmpPath = inSubpaths[ i ];
  301. var tmpShape = new THREE.Shape();
  302. tmpShape.actions = tmpPath.actions;
  303. tmpShape.curves = tmpPath.curves;
  304. shapes.push( tmpShape );
  305. }
  306. //console.log("shape", shapes);
  307. return shapes;
  308. };
  309. function isPointInsidePolygon( inPt, inPolygon ) {
  310. var EPSILON = 0.0000000001;
  311. var polyLen = inPolygon.length;
  312. // inPt on polygon contour => immediate success or
  313. // toggling of inside/outside at every single! intersection point of an edge
  314. // with the horizontal line through inPt, left of inPt
  315. // not counting lowerY endpoints of edges and whole edges on that line
  316. var inside = false;
  317. for ( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
  318. var edgeLowPt = inPolygon[ p ];
  319. var edgeHighPt = inPolygon[ q ];
  320. var edgeDx = edgeHighPt.x - edgeLowPt.x;
  321. var edgeDy = edgeHighPt.y - edgeLowPt.y;
  322. if ( Math.abs(edgeDy) > EPSILON ) { // not parallel
  323. if ( edgeDy < 0 ) {
  324. edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
  325. edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
  326. }
  327. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  328. if ( inPt.y == edgeLowPt.y ) {
  329. if ( inPt.x == edgeLowPt.x ) return true; // inPt is on contour ?
  330. // continue; // no intersection or edgeLowPt => doesn't count !!!
  331. } else {
  332. var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y);
  333. if ( perpEdge == 0 ) return true; // inPt is on contour ?
  334. if ( perpEdge < 0 ) continue;
  335. inside = ! inside; // true intersection left of inPt
  336. }
  337. } else { // parallel or colinear
  338. if ( inPt.y != edgeLowPt.y ) continue; // parallel
  339. // egde lies on the same horizontal line as inPt
  340. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  341. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  342. // continue;
  343. }
  344. }
  345. return inside;
  346. }
  347. var subPaths = extractSubpaths( this.actions );
  348. if ( subPaths.length == 0 ) return [];
  349. if ( noHoles === true ) return toShapesNoHoles( subPaths );
  350. var solid, tmpPath, tmpShape, shapes = [];
  351. if ( subPaths.length == 1) {
  352. tmpPath = subPaths[0];
  353. tmpShape = new THREE.Shape();
  354. tmpShape.actions = tmpPath.actions;
  355. tmpShape.curves = tmpPath.curves;
  356. shapes.push( tmpShape );
  357. return shapes;
  358. }
  359. var holesFirst = ! THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  360. holesFirst = isCCW ? ! holesFirst : holesFirst;
  361. // console.log("Holes first", holesFirst);
  362. var betterShapeHoles = [];
  363. var newShapes = [];
  364. var newShapeHoles = [];
  365. var mainIdx = 0;
  366. var tmpPoints;
  367. newShapes[mainIdx] = undefined;
  368. newShapeHoles[mainIdx] = [];
  369. var i, il;
  370. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  371. tmpPath = subPaths[ i ];
  372. tmpPoints = tmpPath.getPoints();
  373. solid = THREE.Shape.Utils.isClockWise( tmpPoints );
  374. solid = isCCW ? ! solid : solid;
  375. if ( solid ) {
  376. if ( (! holesFirst ) && ( newShapes[mainIdx] ) ) mainIdx ++;
  377. newShapes[mainIdx] = { s: new THREE.Shape(), p: tmpPoints };
  378. newShapes[mainIdx].s.actions = tmpPath.actions;
  379. newShapes[mainIdx].s.curves = tmpPath.curves;
  380. if ( holesFirst ) mainIdx ++;
  381. newShapeHoles[mainIdx] = [];
  382. //console.log('cw', i);
  383. } else {
  384. newShapeHoles[mainIdx].push( { h: tmpPath, p: tmpPoints[0] } );
  385. //console.log('ccw', i);
  386. }
  387. }
  388. // only Holes? -> probably all Shapes with wrong orientation
  389. if ( ! newShapes[0] ) return toShapesNoHoles( subPaths );
  390. if ( newShapes.length > 1 ) {
  391. var ambigious = false;
  392. var toChange = [];
  393. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  394. betterShapeHoles[sIdx] = [];
  395. }
  396. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  397. var sh = newShapes[sIdx];
  398. var sho = newShapeHoles[sIdx];
  399. for (var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
  400. var ho = sho[hIdx];
  401. var hole_unassigned = true;
  402. for (var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
  403. if ( isPointInsidePolygon( ho.p, newShapes[s2Idx].p ) ) {
  404. if ( sIdx != s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } );
  405. if ( hole_unassigned ) {
  406. hole_unassigned = false;
  407. betterShapeHoles[s2Idx].push( ho );
  408. } else {
  409. ambigious = true;
  410. }
  411. }
  412. }
  413. if ( hole_unassigned ) { betterShapeHoles[sIdx].push( ho ); }
  414. }
  415. }
  416. // console.log("ambigious: ", ambigious);
  417. if ( toChange.length > 0 ) {
  418. // console.log("to change: ", toChange);
  419. if (! ambigious) newShapeHoles = betterShapeHoles;
  420. }
  421. }
  422. var tmpHoles, j, jl;
  423. for ( i = 0, il = newShapes.length; i < il; i ++ ) {
  424. tmpShape = newShapes[i].s;
  425. shapes.push( tmpShape );
  426. tmpHoles = newShapeHoles[i];
  427. for ( j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  428. tmpShape.holes.push( tmpHoles[j].h );
  429. }
  430. }
  431. //console.log("shape", shapes);
  432. return shapes;
  433. };