Shape.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. * Defines a 2d shape plane using paths.
  4. **/
  5. // STEP 1 Create a path.
  6. // STEP 2 Turn path into shape.
  7. // STEP 3 ExtrudeGeometry takes in Shape/Shapes
  8. // STEP 3a - Extract points from each shape, turn to vertices
  9. // STEP 3b - Triangulate each shape, add faces.
  10. THREE.Shape = function () {
  11. THREE.Path.apply( this, arguments );
  12. this.holes = [];
  13. };
  14. THREE.Shape.prototype = Object.create( THREE.Path.prototype );
  15. // Convenience method to return ExtrudeGeometry
  16. THREE.Shape.prototype.extrude = function ( options ) {
  17. var extruded = new THREE.ExtrudeGeometry( this, options );
  18. return extruded;
  19. };
  20. // Convenience method to return ShapeGeometry
  21. THREE.Shape.prototype.makeGeometry = function ( options ) {
  22. var geometry = new THREE.ShapeGeometry( this, options );
  23. return geometry;
  24. };
  25. // Get points of holes
  26. THREE.Shape.prototype.getPointsHoles = function ( divisions ) {
  27. var i, il = this.holes.length, holesPts = [];
  28. for ( i = 0; i < il; i ++ ) {
  29. holesPts[ i ] = this.holes[ i ].getTransformedPoints( divisions, this.bends );
  30. }
  31. return holesPts;
  32. };
  33. // Get points of holes (spaced by regular distance)
  34. THREE.Shape.prototype.getSpacedPointsHoles = function ( divisions ) {
  35. var i, il = this.holes.length, holesPts = [];
  36. for ( i = 0; i < il; i ++ ) {
  37. holesPts[ i ] = this.holes[ i ].getTransformedSpacedPoints( divisions, this.bends );
  38. }
  39. return holesPts;
  40. };
  41. // Get points of shape and holes (keypoints based on segments parameter)
  42. THREE.Shape.prototype.extractAllPoints = function ( divisions ) {
  43. return {
  44. shape: this.getTransformedPoints( divisions ),
  45. holes: this.getPointsHoles( divisions )
  46. };
  47. };
  48. THREE.Shape.prototype.extractPoints = function ( divisions ) {
  49. if (this.useSpacedPoints) {
  50. return this.extractAllSpacedPoints(divisions);
  51. }
  52. return this.extractAllPoints(divisions);
  53. };
  54. //
  55. // THREE.Shape.prototype.extractAllPointsWithBend = function ( divisions, bend ) {
  56. //
  57. // return {
  58. //
  59. // shape: this.transform( bend, divisions ),
  60. // holes: this.getPointsHoles( divisions, bend )
  61. //
  62. // };
  63. //
  64. // };
  65. // Get points of shape and holes (spaced by regular distance)
  66. THREE.Shape.prototype.extractAllSpacedPoints = function ( divisions ) {
  67. return {
  68. shape: this.getTransformedSpacedPoints( divisions ),
  69. holes: this.getSpacedPointsHoles( divisions )
  70. };
  71. };
  72. /**************************************************************
  73. * Utils
  74. **************************************************************/
  75. THREE.Shape.Utils = {
  76. triangulateShape: function ( contour, holes ) {
  77. function point_in_segment_2D_colin( inSegPt1, inSegPt2, inOtherPt ) {
  78. // inOtherPt needs to be colinear to the inSegment
  79. if ( inSegPt1.x != inSegPt2.x ) {
  80. if ( inSegPt1.x < inSegPt2.x ) {
  81. return ( ( inSegPt1.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt2.x ) );
  82. } else {
  83. return ( ( inSegPt2.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt1.x ) );
  84. }
  85. } else {
  86. if ( inSegPt1.y < inSegPt2.y ) {
  87. return ( ( inSegPt1.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt2.y ) );
  88. } else {
  89. return ( ( inSegPt2.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt1.y ) );
  90. }
  91. }
  92. }
  93. function intersect_segments_2D( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1, inSeg2Pt2, inExcludeAdjacentSegs ) {
  94. var EPSILON = 0.0000000001;
  95. var seg1dx = inSeg1Pt2.x - inSeg1Pt1.x, seg1dy = inSeg1Pt2.y - inSeg1Pt1.y;
  96. var seg2dx = inSeg2Pt2.x - inSeg2Pt1.x, seg2dy = inSeg2Pt2.y - inSeg2Pt1.y;
  97. var seg1seg2dx = inSeg1Pt1.x - inSeg2Pt1.x;
  98. var seg1seg2dy = inSeg1Pt1.y - inSeg2Pt1.y;
  99. var limit = seg1dy * seg2dx - seg1dx * seg2dy;
  100. var perpSeg1 = seg1dy * seg1seg2dx - seg1dx * seg1seg2dy;
  101. if ( Math.abs(limit) > EPSILON ) { // not parallel
  102. var perpSeg2;
  103. if ( limit > 0 ) {
  104. if ( ( perpSeg1 < 0 ) || ( perpSeg1 > limit ) ) return [];
  105. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  106. if ( ( perpSeg2 < 0 ) || ( perpSeg2 > limit ) ) return [];
  107. } else {
  108. if ( ( perpSeg1 > 0 ) || ( perpSeg1 < limit ) ) return [];
  109. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  110. if ( ( perpSeg2 > 0 ) || ( perpSeg2 < limit ) ) return [];
  111. }
  112. // i.e. to reduce rounding errors
  113. // intersection at endpoint of segment#1?
  114. if ( perpSeg2 == 0 ) {
  115. if ( ( inExcludeAdjacentSegs ) &&
  116. ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return [];
  117. return [ inSeg1Pt1 ];
  118. }
  119. if ( perpSeg2 == limit ) {
  120. if ( ( inExcludeAdjacentSegs ) &&
  121. ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return [];
  122. return [ inSeg1Pt2 ];
  123. }
  124. // intersection at endpoint of segment#2?
  125. if ( perpSeg1 == 0 ) return [ inSeg2Pt1 ];
  126. if ( perpSeg1 == limit ) return [ inSeg2Pt2 ];
  127. // return real intersection point
  128. var factorSeg1 = perpSeg2 / limit;
  129. return [ { x: inSeg1Pt1.x + factorSeg1 * seg1dx,
  130. y: inSeg1Pt1.y + factorSeg1 * seg1dy } ];
  131. } else { // parallel or colinear
  132. if ( ( perpSeg1 != 0 ) ||
  133. ( seg2dy * seg1seg2dx != seg2dx * seg1seg2dy ) ) return [];
  134. // they are collinear or degenerate
  135. var seg1Pt = ( (seg1dx == 0) && (seg1dy == 0) ); // segment1 ist just a point?
  136. var seg2Pt = ( (seg2dx == 0) && (seg2dy == 0) ); // segment2 ist just a point?
  137. // both segments are points
  138. if ( seg1Pt && seg2Pt ) {
  139. if ( (inSeg1Pt1.x != inSeg2Pt1.x) ||
  140. (inSeg1Pt1.y != inSeg2Pt1.y) ) return []; // they are distinct points
  141. return [ inSeg1Pt1 ]; // they are the same point
  142. }
  143. // segment#1 is a single point
  144. if ( seg1Pt ) {
  145. if (! point_in_segment_2D_colin( inSeg2Pt1, inSeg2Pt2, inSeg1Pt1 ) ) return []; // but not in segment#2
  146. return [ inSeg1Pt1 ];
  147. }
  148. // segment#2 is a single point
  149. if ( seg2Pt ) {
  150. if (! point_in_segment_2D_colin( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1 ) ) return []; // but not in segment#1
  151. return [ inSeg2Pt1 ];
  152. }
  153. // they are collinear segments, which might overlap
  154. var seg1min, seg1max, seg1minVal, seg1maxVal;
  155. var seg2min, seg2max, seg2minVal, seg2maxVal;
  156. if (seg1dx != 0) { // the segments are NOT on a vertical line
  157. if ( inSeg1Pt1.x < inSeg1Pt2.x ) {
  158. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.x;
  159. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.x;
  160. } else {
  161. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.x;
  162. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.x;
  163. }
  164. if ( inSeg2Pt1.x < inSeg2Pt2.x ) {
  165. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.x;
  166. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.x;
  167. } else {
  168. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.x;
  169. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.x;
  170. }
  171. } else { // the segments are on a vertical line
  172. if ( inSeg1Pt1.y < inSeg1Pt2.y ) {
  173. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.y;
  174. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.y;
  175. } else {
  176. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.y;
  177. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.y;
  178. }
  179. if ( inSeg2Pt1.y < inSeg2Pt2.y ) {
  180. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.y;
  181. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.y;
  182. } else {
  183. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.y;
  184. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.y;
  185. }
  186. }
  187. if ( seg1minVal <= seg2minVal ) {
  188. if ( seg1maxVal < seg2minVal ) return [];
  189. if ( seg1maxVal == seg2minVal ) {
  190. if ( inExcludeAdjacentSegs ) return [];
  191. return [ seg2min ];
  192. }
  193. if ( seg1maxVal <= seg2maxVal ) return [ seg2min, seg1max ];
  194. return [ seg2min, seg2max ];
  195. } else {
  196. if ( seg1minVal > seg2maxVal ) return [];
  197. if ( seg1minVal == seg2maxVal ) {
  198. if ( inExcludeAdjacentSegs ) return [];
  199. return [ seg1min ];
  200. }
  201. if ( seg1maxVal <= seg2maxVal ) return [ seg1min, seg1max ];
  202. return [ seg1min, seg2max ];
  203. }
  204. }
  205. }
  206. function isPointInsideAngle( inVertex, inLegFromPt, inLegToPt, inOtherPt ) {
  207. // The order of legs is important
  208. var EPSILON = 0.0000000001;
  209. // translation of all points, so that Vertex is at (0,0)
  210. var legFromPtX = inLegFromPt.x - inVertex.x, legFromPtY = inLegFromPt.y - inVertex.y;
  211. var legToPtX = inLegToPt.x - inVertex.x, legToPtY = inLegToPt.y - inVertex.y;
  212. var otherPtX = inOtherPt.x - inVertex.x, otherPtY = inOtherPt.y - inVertex.y;
  213. // main angle >0: < 180 deg.; 0: 180 deg.; <0: > 180 deg.
  214. var from2toAngle = legFromPtX * legToPtY - legFromPtY * legToPtX;
  215. var from2otherAngle = legFromPtX * otherPtY - legFromPtY * otherPtX;
  216. if ( Math.abs(from2toAngle) > EPSILON ) { // angle != 180 deg.
  217. var other2toAngle = otherPtX * legToPtY - otherPtY * legToPtX;
  218. // console.log( "from2to: " + from2toAngle + ", from2other: " + from2otherAngle + ", other2to: " + other2toAngle );
  219. if ( from2toAngle > 0 ) { // main angle < 180 deg.
  220. return ( ( from2otherAngle >= 0 ) && ( other2toAngle >= 0 ) );
  221. } else { // main angle > 180 deg.
  222. return ( ( from2otherAngle >= 0 ) || ( other2toAngle >= 0 ) );
  223. }
  224. } else { // angle == 180 deg.
  225. // console.log( "from2to: 180 deg., from2other: " + from2otherAngle );
  226. return ( from2otherAngle > 0 );
  227. }
  228. }
  229. function removeHoles( contour, holes ) {
  230. var shape = contour.concat(); // work on this shape
  231. var hole;
  232. function isCutLineInsideAngles( inShapeIdx, inHoleIdx ) {
  233. // Check if hole point lies within angle around shape point
  234. var lastShapeIdx = shape.length - 1;
  235. var prevShapeIdx = inShapeIdx - 1;
  236. if ( prevShapeIdx < 0 ) prevShapeIdx = lastShapeIdx;
  237. var nextShapeIdx = inShapeIdx + 1;
  238. if ( nextShapeIdx > lastShapeIdx ) nextShapeIdx = 0;
  239. var insideAngle = isPointInsideAngle( shape[inShapeIdx], shape[ prevShapeIdx ], shape[ nextShapeIdx ], hole[inHoleIdx] );
  240. if (! insideAngle ) {
  241. // console.log( "Vertex (Shape): " + inShapeIdx + ", Point: " + hole[inHoleIdx].x + "/" + hole[inHoleIdx].y );
  242. return false;
  243. }
  244. // Check if shape point lies within angle around hole point
  245. var lastHoleIdx = hole.length - 1;
  246. var prevHoleIdx = inHoleIdx - 1;
  247. if ( prevHoleIdx < 0 ) prevHoleIdx = lastHoleIdx;
  248. var nextHoleIdx = inHoleIdx + 1;
  249. if ( nextHoleIdx > lastHoleIdx ) nextHoleIdx = 0;
  250. insideAngle = isPointInsideAngle( hole[inHoleIdx], hole[ prevHoleIdx ], hole[ nextHoleIdx ], shape[inShapeIdx] );
  251. if (! insideAngle ) {
  252. // console.log( "Vertex (Hole): " + inHoleIdx + ", Point: " + shape[inShapeIdx].x + "/" + shape[inShapeIdx].y );
  253. return false;
  254. }
  255. return true;
  256. }
  257. function intersectsShapeEdge( inShapePt, inHolePt ) {
  258. // checks for intersections with shape edges
  259. var sIdx, nextIdx, intersection;
  260. for ( sIdx = 0; sIdx < shape.length; sIdx++ ) {
  261. nextIdx = sIdx+1; nextIdx %= shape.length;
  262. intersection = intersect_segments_2D( inShapePt, inHolePt, shape[sIdx], shape[nextIdx], true );
  263. if ( intersection.length > 0 ) return true;
  264. }
  265. return false;
  266. }
  267. var indepHoles = [];
  268. function intersectsHoleEdge( inShapePt, inHolePt ) {
  269. // checks for intersections with hole edges
  270. var ihIdx, chkHole,
  271. hIdx, nextIdx, intersection;
  272. for ( ihIdx = 0; ihIdx < indepHoles.length; ihIdx++ ) {
  273. chkHole = holes[indepHoles[ihIdx]];
  274. for ( hIdx = 0; hIdx < chkHole.length; hIdx++ ) {
  275. nextIdx = hIdx+1; nextIdx %= chkHole.length;
  276. intersection = intersect_segments_2D( inShapePt, inHolePt, chkHole[hIdx], chkHole[nextIdx], true );
  277. if ( intersection.length > 0 ) return true;
  278. }
  279. }
  280. return false;
  281. }
  282. var holeIndex, shapeIndex,
  283. shapePt, holePt,
  284. holeIdx, cutKey, failedCuts = [],
  285. tmpShape1, tmpShape2,
  286. tmpHole1, tmpHole2;
  287. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  288. indepHoles.push( h );
  289. }
  290. var counter = indepHoles.length * 2;
  291. while ( indepHoles.length > 0 ) {
  292. counter --;
  293. if ( counter < 0 ) {
  294. console.log( "Infinite Loop! Holes left:" + indepHoles.length + ", Probably Hole outside Shape!" );
  295. break;
  296. }
  297. // search for shape-vertex and hole-vertex,
  298. // which can be connected without intersections
  299. for ( shapeIndex = 0; shapeIndex < shape.length; shapeIndex++ ) {
  300. shapePt = shape[ shapeIndex ];
  301. holeIndex = -1;
  302. // search for hole which can be reached without intersections
  303. for ( var h = 0; h < indepHoles.length; h ++ ) {
  304. holeIdx = indepHoles[h];
  305. // prevent multiple checks
  306. cutKey = shapePt.x + ":" + shapePt.y + ":" + holeIdx;
  307. if ( failedCuts[cutKey] !== undefined ) continue;
  308. hole = holes[holeIdx];
  309. for ( var h2 = 0; h2 < hole.length; h2 ++ ) {
  310. holePt = hole[ h2 ];
  311. if (! isCutLineInsideAngles( shapeIndex, h2 ) ) continue;
  312. if ( intersectsShapeEdge( shapePt, holePt ) ) continue;
  313. if ( intersectsHoleEdge( shapePt, holePt ) ) continue;
  314. holeIndex = h2;
  315. indepHoles.splice(h,1);
  316. tmpShape1 = shape.slice( 0, shapeIndex+1 );
  317. tmpShape2 = shape.slice( shapeIndex );
  318. tmpHole1 = hole.slice( holeIndex );
  319. tmpHole2 = hole.slice( 0, holeIndex+1 );
  320. shape = tmpShape1.concat( tmpHole1 ).concat( tmpHole2 ).concat( tmpShape2 );
  321. // Debug only, to show the selected cuts
  322. // glob_CutLines.push( [ shapePt, holePt ] );
  323. break;
  324. }
  325. if ( holeIndex >= 0 ) break; // hole-vertex found
  326. failedCuts[cutKey] = true; // remember failure
  327. }
  328. if ( holeIndex >= 0 ) break; // hole-vertex found
  329. }
  330. }
  331. return shape; /* shape with no holes */
  332. }
  333. var i, il, f, face,
  334. key, index,
  335. allPointsMap = {};
  336. // To maintain reference to old shape, one must match coordinates, or offset the indices from original arrays. It's probably easier to do the first.
  337. var allpoints = contour.concat();
  338. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  339. Array.prototype.push.apply( allpoints, holes[h] );
  340. }
  341. //console.log( "allpoints",allpoints, allpoints.length );
  342. // prepare all points map
  343. for ( i = 0, il = allpoints.length; i < il; i ++ ) {
  344. key = allpoints[ i ].x + ":" + allpoints[ i ].y;
  345. if ( allPointsMap[ key ] !== undefined ) {
  346. console.log( "Duplicate point", key );
  347. }
  348. allPointsMap[ key ] = i;
  349. }
  350. // remove holes by cutting paths to holes and adding them to the shape
  351. var shapeWithoutHoles = removeHoles( contour, holes );
  352. var triangles = THREE.FontUtils.Triangulate( shapeWithoutHoles, false ); // True returns indices for points of spooled shape
  353. //console.log( "triangles",triangles, triangles.length );
  354. // check all face vertices against all points map
  355. for ( i = 0, il = triangles.length; i < il; i ++ ) {
  356. face = triangles[ i ];
  357. for ( f = 0; f < 3; f ++ ) {
  358. key = face[ f ].x + ":" + face[ f ].y;
  359. index = allPointsMap[ key ];
  360. if ( index !== undefined ) {
  361. face[ f ] = index;
  362. }
  363. }
  364. }
  365. return triangles.concat();
  366. },
  367. isClockWise: function ( pts ) {
  368. return THREE.FontUtils.Triangulate.area( pts ) < 0;
  369. },
  370. // Bezier Curves formulas obtained from
  371. // http://en.wikipedia.org/wiki/B%C3%A9zier_curve
  372. // Quad Bezier Functions
  373. b2p0: function ( t, p ) {
  374. var k = 1 - t;
  375. return k * k * p;
  376. },
  377. b2p1: function ( t, p ) {
  378. return 2 * ( 1 - t ) * t * p;
  379. },
  380. b2p2: function ( t, p ) {
  381. return t * t * p;
  382. },
  383. b2: function ( t, p0, p1, p2 ) {
  384. return this.b2p0( t, p0 ) + this.b2p1( t, p1 ) + this.b2p2( t, p2 );
  385. },
  386. // Cubic Bezier Functions
  387. b3p0: function ( t, p ) {
  388. var k = 1 - t;
  389. return k * k * k * p;
  390. },
  391. b3p1: function ( t, p ) {
  392. var k = 1 - t;
  393. return 3 * k * k * t * p;
  394. },
  395. b3p2: function ( t, p ) {
  396. var k = 1 - t;
  397. return 3 * k * t * t * p;
  398. },
  399. b3p3: function ( t, p ) {
  400. return t * t * t * p;
  401. },
  402. b3: function ( t, p0, p1, p2, p3 ) {
  403. return this.b3p0( t, p0 ) + this.b3p1( t, p1 ) + this.b3p2( t, p2 ) + this.b3p3( t, p3 );
  404. }
  405. };
粤ICP备19079148号