ShapeUtils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. */
  4. THREE.ShapeUtils = {
  5. // calculate area of the contour polygon
  6. area: function ( contour ) {
  7. var n = contour.length;
  8. var a = 0.0;
  9. for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
  10. a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
  11. }
  12. return a * 0.5;
  13. },
  14. triangulate: ( function () {
  15. /**
  16. * This code is a quick port of code written in C++ which was submitted to
  17. * flipcode.com by John W. Ratcliff // July 22, 2000
  18. * See original code and more information here:
  19. * http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
  20. *
  21. * ported to actionscript by Zevan Rosser
  22. * www.actionsnippet.com
  23. *
  24. * ported to javascript by Joshua Koo
  25. * http://www.lab4games.net/zz85/blog
  26. *
  27. */
  28. function snip( contour, u, v, w, n, verts ) {
  29. var p;
  30. var ax, ay, bx, by;
  31. var cx, cy, px, py;
  32. ax = contour[ verts[ u ] ].x;
  33. ay = contour[ verts[ u ] ].y;
  34. bx = contour[ verts[ v ] ].x;
  35. by = contour[ verts[ v ] ].y;
  36. cx = contour[ verts[ w ] ].x;
  37. cy = contour[ verts[ w ] ].y;
  38. if ( Number.EPSILON > ( ( ( bx - ax ) * ( cy - ay ) ) - ( ( by - ay ) * ( cx - ax ) ) ) ) return false;
  39. var aX, aY, bX, bY, cX, cY;
  40. var apx, apy, bpx, bpy, cpx, cpy;
  41. var cCROSSap, bCROSScp, aCROSSbp;
  42. aX = cx - bx; aY = cy - by;
  43. bX = ax - cx; bY = ay - cy;
  44. cX = bx - ax; cY = by - ay;
  45. for ( p = 0; p < n; p ++ ) {
  46. px = contour[ verts[ p ] ].x;
  47. py = contour[ verts[ p ] ].y;
  48. if ( ( ( px === ax ) && ( py === ay ) ) ||
  49. ( ( px === bx ) && ( py === by ) ) ||
  50. ( ( px === cx ) && ( py === cy ) ) ) continue;
  51. apx = px - ax; apy = py - ay;
  52. bpx = px - bx; bpy = py - by;
  53. cpx = px - cx; cpy = py - cy;
  54. // see if p is inside triangle abc
  55. aCROSSbp = aX * bpy - aY * bpx;
  56. cCROSSap = cX * apy - cY * apx;
  57. bCROSScp = bX * cpy - bY * cpx;
  58. if ( ( aCROSSbp >= - Number.EPSILON ) && ( bCROSScp >= - Number.EPSILON ) && ( cCROSSap >= - Number.EPSILON ) ) return false;
  59. }
  60. return true;
  61. }
  62. // takes in an contour array and returns
  63. return function triangulate( contour, indices ) {
  64. var n = contour.length;
  65. if ( n < 3 ) return null;
  66. var result = [],
  67. verts = [],
  68. vertIndices = [];
  69. /* we want a counter-clockwise polygon in verts */
  70. var u, v, w;
  71. if ( THREE.ShapeUtils.area( contour ) > 0.0 ) {
  72. for ( v = 0; v < n; v ++ ) verts[ v ] = v;
  73. } else {
  74. for ( v = 0; v < n; v ++ ) verts[ v ] = ( n - 1 ) - v;
  75. }
  76. var nv = n;
  77. /* remove nv - 2 vertices, creating 1 triangle every time */
  78. var count = 2 * nv; /* error detection */
  79. for ( v = nv - 1; nv > 2; ) {
  80. /* if we loop, it is probably a non-simple polygon */
  81. if ( ( count -- ) <= 0 ) {
  82. //** Triangulate: ERROR - probable bad polygon!
  83. //throw ( "Warning, unable to triangulate polygon!" );
  84. //return null;
  85. // Sometimes warning is fine, especially polygons are triangulated in reverse.
  86. console.warn( 'THREE.ShapeUtils: Unable to triangulate polygon! in triangulate()' );
  87. if ( indices ) return vertIndices;
  88. return result;
  89. }
  90. /* three consecutive vertices in current polygon, <u,v,w> */
  91. u = v; if ( nv <= u ) u = 0; /* previous */
  92. v = u + 1; if ( nv <= v ) v = 0; /* new v */
  93. w = v + 1; if ( nv <= w ) w = 0; /* next */
  94. if ( snip( contour, u, v, w, nv, verts ) ) {
  95. var a, b, c, s, t;
  96. /* true names of the vertices */
  97. a = verts[ u ];
  98. b = verts[ v ];
  99. c = verts[ w ];
  100. /* output Triangle */
  101. result.push( [ contour[ a ],
  102. contour[ b ],
  103. contour[ c ] ] );
  104. vertIndices.push( [ verts[ u ], verts[ v ], verts[ w ] ] );
  105. /* remove v from the remaining polygon */
  106. for ( s = v, t = v + 1; t < nv; s ++, t ++ ) {
  107. verts[ s ] = verts[ t ];
  108. }
  109. nv --;
  110. /* reset error detection counter */
  111. count = 2 * nv;
  112. }
  113. }
  114. if ( indices ) return vertIndices;
  115. return result;
  116. }
  117. } )(),
  118. triangulateShape: function ( contour, holes ) {
  119. function point_in_segment_2D_colin( inSegPt1, inSegPt2, inOtherPt ) {
  120. // inOtherPt needs to be collinear to the inSegment
  121. if ( inSegPt1.x !== inSegPt2.x ) {
  122. if ( inSegPt1.x < inSegPt2.x ) {
  123. return ( ( inSegPt1.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt2.x ) );
  124. } else {
  125. return ( ( inSegPt2.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt1.x ) );
  126. }
  127. } else {
  128. if ( inSegPt1.y < inSegPt2.y ) {
  129. return ( ( inSegPt1.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt2.y ) );
  130. } else {
  131. return ( ( inSegPt2.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt1.y ) );
  132. }
  133. }
  134. }
  135. function intersect_segments_2D( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1, inSeg2Pt2, inExcludeAdjacentSegs ) {
  136. var seg1dx = inSeg1Pt2.x - inSeg1Pt1.x, seg1dy = inSeg1Pt2.y - inSeg1Pt1.y;
  137. var seg2dx = inSeg2Pt2.x - inSeg2Pt1.x, seg2dy = inSeg2Pt2.y - inSeg2Pt1.y;
  138. var seg1seg2dx = inSeg1Pt1.x - inSeg2Pt1.x;
  139. var seg1seg2dy = inSeg1Pt1.y - inSeg2Pt1.y;
  140. var limit = seg1dy * seg2dx - seg1dx * seg2dy;
  141. var perpSeg1 = seg1dy * seg1seg2dx - seg1dx * seg1seg2dy;
  142. if ( Math.abs( limit ) > Number.EPSILON ) {
  143. // not parallel
  144. var perpSeg2;
  145. if ( limit > 0 ) {
  146. if ( ( perpSeg1 < 0 ) || ( perpSeg1 > limit ) ) return [];
  147. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  148. if ( ( perpSeg2 < 0 ) || ( perpSeg2 > limit ) ) return [];
  149. } else {
  150. if ( ( perpSeg1 > 0 ) || ( perpSeg1 < limit ) ) return [];
  151. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  152. if ( ( perpSeg2 > 0 ) || ( perpSeg2 < limit ) ) return [];
  153. }
  154. // i.e. to reduce rounding errors
  155. // intersection at endpoint of segment#1?
  156. if ( perpSeg2 === 0 ) {
  157. if ( ( inExcludeAdjacentSegs ) &&
  158. ( ( perpSeg1 === 0 ) || ( perpSeg1 === limit ) ) ) return [];
  159. return [ inSeg1Pt1 ];
  160. }
  161. if ( perpSeg2 === limit ) {
  162. if ( ( inExcludeAdjacentSegs ) &&
  163. ( ( perpSeg1 === 0 ) || ( perpSeg1 === limit ) ) ) return [];
  164. return [ inSeg1Pt2 ];
  165. }
  166. // intersection at endpoint of segment#2?
  167. if ( perpSeg1 === 0 ) return [ inSeg2Pt1 ];
  168. if ( perpSeg1 === limit ) return [ inSeg2Pt2 ];
  169. // return real intersection point
  170. var factorSeg1 = perpSeg2 / limit;
  171. return [ { x: inSeg1Pt1.x + factorSeg1 * seg1dx,
  172. y: inSeg1Pt1.y + factorSeg1 * seg1dy } ];
  173. } else {
  174. // parallel or collinear
  175. if ( ( perpSeg1 !== 0 ) ||
  176. ( seg2dy * seg1seg2dx !== seg2dx * seg1seg2dy ) ) return [];
  177. // they are collinear or degenerate
  178. var seg1Pt = ( ( seg1dx === 0 ) && ( seg1dy === 0 ) ); // segment1 is just a point?
  179. var seg2Pt = ( ( seg2dx === 0 ) && ( seg2dy === 0 ) ); // segment2 is just a point?
  180. // both segments are points
  181. if ( seg1Pt && seg2Pt ) {
  182. if ( ( inSeg1Pt1.x !== inSeg2Pt1.x ) ||
  183. ( inSeg1Pt1.y !== inSeg2Pt1.y ) ) return []; // they are distinct points
  184. return [ inSeg1Pt1 ]; // they are the same point
  185. }
  186. // segment#1 is a single point
  187. if ( seg1Pt ) {
  188. if ( ! point_in_segment_2D_colin( inSeg2Pt1, inSeg2Pt2, inSeg1Pt1 ) ) return []; // but not in segment#2
  189. return [ inSeg1Pt1 ];
  190. }
  191. // segment#2 is a single point
  192. if ( seg2Pt ) {
  193. if ( ! point_in_segment_2D_colin( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1 ) ) return []; // but not in segment#1
  194. return [ inSeg2Pt1 ];
  195. }
  196. // they are collinear segments, which might overlap
  197. var seg1min, seg1max, seg1minVal, seg1maxVal;
  198. var seg2min, seg2max, seg2minVal, seg2maxVal;
  199. if ( seg1dx !== 0 ) {
  200. // the segments are NOT on a vertical line
  201. if ( inSeg1Pt1.x < inSeg1Pt2.x ) {
  202. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.x;
  203. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.x;
  204. } else {
  205. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.x;
  206. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.x;
  207. }
  208. if ( inSeg2Pt1.x < inSeg2Pt2.x ) {
  209. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.x;
  210. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.x;
  211. } else {
  212. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.x;
  213. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.x;
  214. }
  215. } else {
  216. // the segments are on a vertical line
  217. if ( inSeg1Pt1.y < inSeg1Pt2.y ) {
  218. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.y;
  219. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.y;
  220. } else {
  221. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.y;
  222. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.y;
  223. }
  224. if ( inSeg2Pt1.y < inSeg2Pt2.y ) {
  225. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.y;
  226. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.y;
  227. } else {
  228. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.y;
  229. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.y;
  230. }
  231. }
  232. if ( seg1minVal <= seg2minVal ) {
  233. if ( seg1maxVal < seg2minVal ) return [];
  234. if ( seg1maxVal === seg2minVal ) {
  235. if ( inExcludeAdjacentSegs ) return [];
  236. return [ seg2min ];
  237. }
  238. if ( seg1maxVal <= seg2maxVal ) return [ seg2min, seg1max ];
  239. return [ seg2min, seg2max ];
  240. } else {
  241. if ( seg1minVal > seg2maxVal ) return [];
  242. if ( seg1minVal === seg2maxVal ) {
  243. if ( inExcludeAdjacentSegs ) return [];
  244. return [ seg1min ];
  245. }
  246. if ( seg1maxVal <= seg2maxVal ) return [ seg1min, seg1max ];
  247. return [ seg1min, seg2max ];
  248. }
  249. }
  250. }
  251. function isPointInsideAngle( inVertex, inLegFromPt, inLegToPt, inOtherPt ) {
  252. // The order of legs is important
  253. // translation of all points, so that Vertex is at (0,0)
  254. var legFromPtX = inLegFromPt.x - inVertex.x, legFromPtY = inLegFromPt.y - inVertex.y;
  255. var legToPtX = inLegToPt.x - inVertex.x, legToPtY = inLegToPt.y - inVertex.y;
  256. var otherPtX = inOtherPt.x - inVertex.x, otherPtY = inOtherPt.y - inVertex.y;
  257. // main angle >0: < 180 deg.; 0: 180 deg.; <0: > 180 deg.
  258. var from2toAngle = legFromPtX * legToPtY - legFromPtY * legToPtX;
  259. var from2otherAngle = legFromPtX * otherPtY - legFromPtY * otherPtX;
  260. if ( Math.abs( from2toAngle ) > Number.EPSILON ) {
  261. // angle != 180 deg.
  262. var other2toAngle = otherPtX * legToPtY - otherPtY * legToPtX;
  263. // console.log( "from2to: " + from2toAngle + ", from2other: " + from2otherAngle + ", other2to: " + other2toAngle );
  264. if ( from2toAngle > 0 ) {
  265. // main angle < 180 deg.
  266. return ( ( from2otherAngle >= 0 ) && ( other2toAngle >= 0 ) );
  267. } else {
  268. // main angle > 180 deg.
  269. return ( ( from2otherAngle >= 0 ) || ( other2toAngle >= 0 ) );
  270. }
  271. } else {
  272. // angle == 180 deg.
  273. // console.log( "from2to: 180 deg., from2other: " + from2otherAngle );
  274. return ( from2otherAngle > 0 );
  275. }
  276. }
  277. function removeHoles( contour, holes ) {
  278. var shape = contour.concat(); // work on this shape
  279. var hole;
  280. function isCutLineInsideAngles( inShapeIdx, inHoleIdx ) {
  281. // Check if hole point lies within angle around shape point
  282. var lastShapeIdx = shape.length - 1;
  283. var prevShapeIdx = inShapeIdx - 1;
  284. if ( prevShapeIdx < 0 ) prevShapeIdx = lastShapeIdx;
  285. var nextShapeIdx = inShapeIdx + 1;
  286. if ( nextShapeIdx > lastShapeIdx ) nextShapeIdx = 0;
  287. var insideAngle = isPointInsideAngle( shape[ inShapeIdx ], shape[ prevShapeIdx ], shape[ nextShapeIdx ], hole[ inHoleIdx ] );
  288. if ( ! insideAngle ) {
  289. // console.log( "Vertex (Shape): " + inShapeIdx + ", Point: " + hole[inHoleIdx].x + "/" + hole[inHoleIdx].y );
  290. return false;
  291. }
  292. // Check if shape point lies within angle around hole point
  293. var lastHoleIdx = hole.length - 1;
  294. var prevHoleIdx = inHoleIdx - 1;
  295. if ( prevHoleIdx < 0 ) prevHoleIdx = lastHoleIdx;
  296. var nextHoleIdx = inHoleIdx + 1;
  297. if ( nextHoleIdx > lastHoleIdx ) nextHoleIdx = 0;
  298. insideAngle = isPointInsideAngle( hole[ inHoleIdx ], hole[ prevHoleIdx ], hole[ nextHoleIdx ], shape[ inShapeIdx ] );
  299. if ( ! insideAngle ) {
  300. // console.log( "Vertex (Hole): " + inHoleIdx + ", Point: " + shape[inShapeIdx].x + "/" + shape[inShapeIdx].y );
  301. return false;
  302. }
  303. return true;
  304. }
  305. function intersectsShapeEdge( inShapePt, inHolePt ) {
  306. // checks for intersections with shape edges
  307. var sIdx, nextIdx, intersection;
  308. for ( sIdx = 0; sIdx < shape.length; sIdx ++ ) {
  309. nextIdx = sIdx + 1; nextIdx %= shape.length;
  310. intersection = intersect_segments_2D( inShapePt, inHolePt, shape[ sIdx ], shape[ nextIdx ], true );
  311. if ( intersection.length > 0 ) return true;
  312. }
  313. return false;
  314. }
  315. var indepHoles = [];
  316. function intersectsHoleEdge( inShapePt, inHolePt ) {
  317. // checks for intersections with hole edges
  318. var ihIdx, chkHole,
  319. hIdx, nextIdx, intersection;
  320. for ( ihIdx = 0; ihIdx < indepHoles.length; ihIdx ++ ) {
  321. chkHole = holes[ indepHoles[ ihIdx ]];
  322. for ( hIdx = 0; hIdx < chkHole.length; hIdx ++ ) {
  323. nextIdx = hIdx + 1; nextIdx %= chkHole.length;
  324. intersection = intersect_segments_2D( inShapePt, inHolePt, chkHole[ hIdx ], chkHole[ nextIdx ], true );
  325. if ( intersection.length > 0 ) return true;
  326. }
  327. }
  328. return false;
  329. }
  330. var holeIndex, shapeIndex,
  331. shapePt, holePt,
  332. holeIdx, cutKey, failedCuts = [],
  333. tmpShape1, tmpShape2,
  334. tmpHole1, tmpHole2;
  335. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  336. indepHoles.push( h );
  337. }
  338. var minShapeIndex = 0;
  339. var counter = indepHoles.length * 2;
  340. while ( indepHoles.length > 0 ) {
  341. counter --;
  342. if ( counter < 0 ) {
  343. console.log( "Infinite Loop! Holes left:" + indepHoles.length + ", Probably Hole outside Shape!" );
  344. break;
  345. }
  346. // search for shape-vertex and hole-vertex,
  347. // which can be connected without intersections
  348. for ( shapeIndex = minShapeIndex; shapeIndex < shape.length; shapeIndex ++ ) {
  349. shapePt = shape[ shapeIndex ];
  350. holeIndex = - 1;
  351. // search for hole which can be reached without intersections
  352. for ( var h = 0; h < indepHoles.length; h ++ ) {
  353. holeIdx = indepHoles[ h ];
  354. // prevent multiple checks
  355. cutKey = shapePt.x + ":" + shapePt.y + ":" + holeIdx;
  356. if ( failedCuts[ cutKey ] !== undefined ) continue;
  357. hole = holes[ holeIdx ];
  358. for ( var h2 = 0; h2 < hole.length; h2 ++ ) {
  359. holePt = hole[ h2 ];
  360. if ( ! isCutLineInsideAngles( shapeIndex, h2 ) ) continue;
  361. if ( intersectsShapeEdge( shapePt, holePt ) ) continue;
  362. if ( intersectsHoleEdge( shapePt, holePt ) ) continue;
  363. holeIndex = h2;
  364. indepHoles.splice( h, 1 );
  365. tmpShape1 = shape.slice( 0, shapeIndex + 1 );
  366. tmpShape2 = shape.slice( shapeIndex );
  367. tmpHole1 = hole.slice( holeIndex );
  368. tmpHole2 = hole.slice( 0, holeIndex + 1 );
  369. shape = tmpShape1.concat( tmpHole1 ).concat( tmpHole2 ).concat( tmpShape2 );
  370. minShapeIndex = shapeIndex;
  371. // Debug only, to show the selected cuts
  372. // glob_CutLines.push( [ shapePt, holePt ] );
  373. break;
  374. }
  375. if ( holeIndex >= 0 ) break; // hole-vertex found
  376. failedCuts[ cutKey ] = true; // remember failure
  377. }
  378. if ( holeIndex >= 0 ) break; // hole-vertex found
  379. }
  380. }
  381. return shape; /* shape with no holes */
  382. }
  383. var i, il, f, face,
  384. key, index,
  385. allPointsMap = {};
  386. // 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.
  387. var allpoints = contour.concat();
  388. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  389. Array.prototype.push.apply( allpoints, holes[ h ] );
  390. }
  391. //console.log( "allpoints",allpoints, allpoints.length );
  392. // prepare all points map
  393. for ( i = 0, il = allpoints.length; i < il; i ++ ) {
  394. key = allpoints[ i ].x + ":" + allpoints[ i ].y;
  395. if ( allPointsMap[ key ] !== undefined ) {
  396. console.warn( "THREE.Shape: Duplicate point", key );
  397. }
  398. allPointsMap[ key ] = i;
  399. }
  400. // remove holes by cutting paths to holes and adding them to the shape
  401. var shapeWithoutHoles = removeHoles( contour, holes );
  402. var triangles = THREE.ShapeUtils.triangulate( shapeWithoutHoles, false ); // True returns indices for points of spooled shape
  403. //console.log( "triangles",triangles, triangles.length );
  404. // check all face vertices against all points map
  405. for ( i = 0, il = triangles.length; i < il; i ++ ) {
  406. face = triangles[ i ];
  407. for ( f = 0; f < 3; f ++ ) {
  408. key = face[ f ].x + ":" + face[ f ].y;
  409. index = allPointsMap[ key ];
  410. if ( index !== undefined ) {
  411. face[ f ] = index;
  412. }
  413. }
  414. }
  415. return triangles.concat();
  416. },
  417. isClockWise: function ( pts ) {
  418. return THREE.ShapeUtils.area( pts ) < 0;
  419. },
  420. // Bezier Curves formulas obtained from
  421. // http://en.wikipedia.org/wiki/B%C3%A9zier_curve
  422. // Quad Bezier Functions
  423. b2: ( function () {
  424. function b2p0( t, p ) {
  425. var k = 1 - t;
  426. return k * k * p;
  427. }
  428. function b2p1( t, p ) {
  429. return 2 * ( 1 - t ) * t * p;
  430. }
  431. function b2p2( t, p ) {
  432. return t * t * p;
  433. }
  434. return function b2( t, p0, p1, p2 ) {
  435. return b2p0( t, p0 ) + b2p1( t, p1 ) + b2p2( t, p2 );
  436. };
  437. } )(),
  438. // Cubic Bezier Functions
  439. b3: ( function () {
  440. function b3p0( t, p ) {
  441. var k = 1 - t;
  442. return k * k * k * p;
  443. }
  444. function b3p1( t, p ) {
  445. var k = 1 - t;
  446. return 3 * k * k * t * p;
  447. }
  448. function b3p2( t, p ) {
  449. var k = 1 - t;
  450. return 3 * k * t * t * p;
  451. }
  452. function b3p3( t, p ) {
  453. return t * t * t * p;
  454. }
  455. return function b3( t, p0, p1, p2, p3 ) {
  456. return b3p0( t, p0 ) + b3p1( t, p1 ) + b3p2( t, p2 ) + b3p3( t, p3 );
  457. };
  458. } )()
  459. };