Path.js 15 KB

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