Shape.js 16 KB

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