Path.js 15 KB

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