Path.js 15 KB

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