Path.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 b2 = THREE.ShapeUtils.b2;
  122. var b3 = THREE.ShapeUtils.b3;
  123. var points = [];
  124. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  125. laste, tx, ty;
  126. for ( var i = 0, l = this.actions.length; i < l; i ++ ) {
  127. var item = this.actions[ i ];
  128. var action = item.action;
  129. var args = item.args;
  130. switch ( action ) {
  131. case 'moveTo':
  132. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  133. break;
  134. case 'lineTo':
  135. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  136. break;
  137. case 'quadraticCurveTo':
  138. cpx = args[ 2 ];
  139. cpy = args[ 3 ];
  140. cpx1 = args[ 0 ];
  141. cpy1 = args[ 1 ];
  142. if ( points.length > 0 ) {
  143. laste = points[ points.length - 1 ];
  144. cpx0 = laste.x;
  145. cpy0 = laste.y;
  146. } else {
  147. laste = this.actions[ i - 1 ].args;
  148. cpx0 = laste[ laste.length - 2 ];
  149. cpy0 = laste[ laste.length - 1 ];
  150. }
  151. for ( var j = 1; j <= divisions; j ++ ) {
  152. var t = j / divisions;
  153. tx = b2( t, cpx0, cpx1, cpx );
  154. ty = b2( t, cpy0, cpy1, cpy );
  155. points.push( new THREE.Vector2( tx, ty ) );
  156. }
  157. break;
  158. case 'bezierCurveTo':
  159. cpx = args[ 4 ];
  160. cpy = args[ 5 ];
  161. cpx1 = args[ 0 ];
  162. cpy1 = args[ 1 ];
  163. cpx2 = args[ 2 ];
  164. cpy2 = args[ 3 ];
  165. if ( points.length > 0 ) {
  166. laste = points[ points.length - 1 ];
  167. cpx0 = laste.x;
  168. cpy0 = laste.y;
  169. } else {
  170. laste = this.actions[ i - 1 ].args;
  171. cpx0 = laste[ laste.length - 2 ];
  172. cpy0 = laste[ laste.length - 1 ];
  173. }
  174. for ( var j = 1; j <= divisions; j ++ ) {
  175. var t = j / divisions;
  176. tx = b3( t, cpx0, cpx1, cpx2, cpx );
  177. ty = b3( t, cpy0, cpy1, cpy2, cpy );
  178. points.push( new THREE.Vector2( tx, ty ) );
  179. }
  180. break;
  181. case 'splineThru':
  182. laste = this.actions[ i - 1 ].args;
  183. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  184. var spts = [ last ];
  185. var n = divisions * args[ 0 ].length;
  186. spts = spts.concat( args[ 0 ] );
  187. var spline = new THREE.SplineCurve( spts );
  188. for ( var j = 1; j <= n; j ++ ) {
  189. points.push( spline.getPointAt( j / n ) );
  190. }
  191. break;
  192. case 'arc':
  193. var aX = args[ 0 ], aY = args[ 1 ],
  194. aRadius = args[ 2 ],
  195. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  196. aClockwise = !! args[ 5 ];
  197. var deltaAngle = aEndAngle - aStartAngle;
  198. var angle;
  199. var tdivisions = divisions * 2;
  200. for ( var j = 1; j <= tdivisions; j ++ ) {
  201. var t = j / tdivisions;
  202. if ( ! aClockwise ) {
  203. t = 1 - t;
  204. }
  205. angle = aStartAngle + t * deltaAngle;
  206. tx = aX + aRadius * Math.cos( angle );
  207. ty = aY + aRadius * Math.sin( angle );
  208. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  209. points.push( new THREE.Vector2( tx, ty ) );
  210. }
  211. //console.log(points);
  212. break;
  213. case 'ellipse':
  214. var aX = args[ 0 ], aY = args[ 1 ],
  215. xRadius = args[ 2 ],
  216. yRadius = args[ 3 ],
  217. aStartAngle = args[ 4 ], aEndAngle = args[ 5 ],
  218. aClockwise = !! args[ 6 ],
  219. aRotation = args[ 7 ];
  220. var deltaAngle = aEndAngle - aStartAngle;
  221. var angle;
  222. var tdivisions = divisions * 2;
  223. var cos, sin;
  224. if ( aRotation !== 0 ) {
  225. cos = Math.cos( aRotation );
  226. sin = Math.sin( aRotation );
  227. }
  228. for ( var j = 1; j <= tdivisions; j ++ ) {
  229. var t = j / tdivisions;
  230. if ( ! aClockwise ) {
  231. t = 1 - t;
  232. }
  233. angle = aStartAngle + t * deltaAngle;
  234. tx = aX + xRadius * Math.cos( angle );
  235. ty = aY + yRadius * Math.sin( angle );
  236. if ( aRotation !== 0 ) {
  237. var x = tx, y = ty;
  238. // Rotate the point about the center of the ellipse.
  239. tx = ( x - aX ) * cos - ( y - aY ) * sin + aX;
  240. ty = ( x - aX ) * sin + ( y - aY ) * cos + aY;
  241. }
  242. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  243. points.push( new THREE.Vector2( tx, ty ) );
  244. }
  245. //console.log(points);
  246. break;
  247. } // end switch
  248. }
  249. // Normalize to remove the closing point by default.
  250. var lastPoint = points[ points.length - 1 ];
  251. if ( Math.abs( lastPoint.x - points[ 0 ].x ) < Number.EPSILON &&
  252. Math.abs( lastPoint.y - points[ 0 ].y ) < Number.EPSILON )
  253. points.splice( points.length - 1, 1 );
  254. if ( closedPath ) {
  255. points.push( points[ 0 ] );
  256. }
  257. return points;
  258. };
  259. //
  260. // Breaks path into shapes
  261. //
  262. // Assumptions (if parameter isCCW==true the opposite holds):
  263. // - solid shapes are defined clockwise (CW)
  264. // - holes are defined counterclockwise (CCW)
  265. //
  266. // If parameter noHoles==true:
  267. // - all subPaths are regarded as solid shapes
  268. // - definition order CW/CCW has no relevance
  269. //
  270. THREE.Path.prototype.toShapes = function( isCCW, noHoles ) {
  271. function extractSubpaths( inActions ) {
  272. var subPaths = [], lastPath = new THREE.Path();
  273. for ( var i = 0, l = inActions.length; i < l; i ++ ) {
  274. var item = inActions[ i ];
  275. var args = item.args;
  276. var action = item.action;
  277. if ( action === 'moveTo' ) {
  278. if ( lastPath.actions.length !== 0 ) {
  279. subPaths.push( lastPath );
  280. lastPath = new THREE.Path();
  281. }
  282. }
  283. lastPath[ action ].apply( lastPath, args );
  284. }
  285. if ( lastPath.actions.length !== 0 ) {
  286. subPaths.push( lastPath );
  287. }
  288. // console.log(subPaths);
  289. return subPaths;
  290. }
  291. function toShapesNoHoles( inSubpaths ) {
  292. var shapes = [];
  293. for ( var i = 0, l = inSubpaths.length; i < l; i ++ ) {
  294. var tmpPath = inSubpaths[ i ];
  295. var tmpShape = new THREE.Shape();
  296. tmpShape.actions = tmpPath.actions;
  297. tmpShape.curves = tmpPath.curves;
  298. shapes.push( tmpShape );
  299. }
  300. //console.log("shape", shapes);
  301. return shapes;
  302. }
  303. function isPointInsidePolygon( inPt, inPolygon ) {
  304. var polyLen = inPolygon.length;
  305. // inPt on polygon contour => immediate success or
  306. // toggling of inside/outside at every single! intersection point of an edge
  307. // with the horizontal line through inPt, left of inPt
  308. // not counting lowerY endpoints of edges and whole edges on that line
  309. var inside = false;
  310. for ( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
  311. var edgeLowPt = inPolygon[ p ];
  312. var edgeHighPt = inPolygon[ q ];
  313. var edgeDx = edgeHighPt.x - edgeLowPt.x;
  314. var edgeDy = edgeHighPt.y - edgeLowPt.y;
  315. if ( Math.abs( edgeDy ) > Number.EPSILON ) {
  316. // not parallel
  317. if ( edgeDy < 0 ) {
  318. edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
  319. edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
  320. }
  321. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  322. if ( inPt.y === edgeLowPt.y ) {
  323. if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ?
  324. // continue; // no intersection or edgeLowPt => doesn't count !!!
  325. } else {
  326. var perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
  327. if ( perpEdge === 0 ) return true; // inPt is on contour ?
  328. if ( perpEdge < 0 ) continue;
  329. inside = ! inside; // true intersection left of inPt
  330. }
  331. } else {
  332. // parallel or collinear
  333. if ( inPt.y !== edgeLowPt.y ) continue; // parallel
  334. // edge lies on the same horizontal line as inPt
  335. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  336. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  337. // continue;
  338. }
  339. }
  340. return inside;
  341. }
  342. var isClockWise = THREE.ShapeUtils.isClockWise;
  343. var subPaths = extractSubpaths( this.actions );
  344. if ( subPaths.length === 0 ) return [];
  345. if ( noHoles === true ) return toShapesNoHoles( subPaths );
  346. var solid, tmpPath, tmpShape, shapes = [];
  347. if ( subPaths.length === 1 ) {
  348. tmpPath = subPaths[ 0 ];
  349. tmpShape = new THREE.Shape();
  350. tmpShape.actions = tmpPath.actions;
  351. tmpShape.curves = tmpPath.curves;
  352. shapes.push( tmpShape );
  353. return shapes;
  354. }
  355. var holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
  356. holesFirst = isCCW ? ! holesFirst : holesFirst;
  357. // console.log("Holes first", holesFirst);
  358. var betterShapeHoles = [];
  359. var newShapes = [];
  360. var newShapeHoles = [];
  361. var mainIdx = 0;
  362. var tmpPoints;
  363. newShapes[ mainIdx ] = undefined;
  364. newShapeHoles[ mainIdx ] = [];
  365. for ( var i = 0, l = subPaths.length; i < l; i ++ ) {
  366. tmpPath = subPaths[ i ];
  367. tmpPoints = tmpPath.getPoints();
  368. solid = isClockWise( tmpPoints );
  369. solid = isCCW ? ! solid : solid;
  370. if ( solid ) {
  371. if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++;
  372. newShapes[ mainIdx ] = { s: new THREE.Shape(), p: tmpPoints };
  373. newShapes[ mainIdx ].s.actions = tmpPath.actions;
  374. newShapes[ mainIdx ].s.curves = tmpPath.curves;
  375. if ( holesFirst ) mainIdx ++;
  376. newShapeHoles[ mainIdx ] = [];
  377. //console.log('cw', i);
  378. } else {
  379. newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
  380. //console.log('ccw', i);
  381. }
  382. }
  383. // only Holes? -> probably all Shapes with wrong orientation
  384. if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths );
  385. if ( newShapes.length > 1 ) {
  386. var ambiguous = false;
  387. var toChange = [];
  388. for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  389. betterShapeHoles[ sIdx ] = [];
  390. }
  391. for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  392. var sho = newShapeHoles[ sIdx ];
  393. for ( var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
  394. var ho = sho[ hIdx ];
  395. var hole_unassigned = true;
  396. for ( var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
  397. if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
  398. if ( sIdx !== s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } );
  399. if ( hole_unassigned ) {
  400. hole_unassigned = false;
  401. betterShapeHoles[ s2Idx ].push( ho );
  402. } else {
  403. ambiguous = true;
  404. }
  405. }
  406. }
  407. if ( hole_unassigned ) {
  408. betterShapeHoles[ sIdx ].push( ho );
  409. }
  410. }
  411. }
  412. // console.log("ambiguous: ", ambiguous);
  413. if ( toChange.length > 0 ) {
  414. // console.log("to change: ", toChange);
  415. if ( ! ambiguous ) newShapeHoles = betterShapeHoles;
  416. }
  417. }
  418. var tmpHoles;
  419. for ( var i = 0, il = newShapes.length; i < il; i ++ ) {
  420. tmpShape = newShapes[ i ].s;
  421. shapes.push( tmpShape );
  422. tmpHoles = newShapeHoles[ i ];
  423. for ( var j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  424. tmpShape.holes.push( tmpHoles[ j ].h );
  425. }
  426. }
  427. //console.log("shape", shapes);
  428. return shapes;
  429. };
粤ICP备19079148号