ShapeUtils.js 14 KB

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