Curve.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. * Extensible curve object
  4. *
  5. * This file contains following classes:
  6. *
  7. * THREE.Curve
  8. * THREE.LineCurve
  9. * THREE.QuadraticBezierCurve
  10. * THREE.CubicBezierCurve
  11. * THREE.SplineCurve
  12. * THREE.ArcCurve
  13. *
  14. **/
  15. /**************************************************************
  16. * Abstract Curve base class
  17. **************************************************************/
  18. THREE.Curve = function () {
  19. };
  20. // Virtual base class method to overwrite and implement in subclasses
  21. // - t [0 .. 1]
  22. THREE.Curve.prototype.getPoint = function ( t ) {
  23. console.log( "Warning, getPoint() not implemented!" );
  24. return null;
  25. };
  26. // Get point at relative position in curve according to arc length
  27. // - u [0 .. 1]
  28. THREE.Curve.prototype.getPointAt = function ( u ) {
  29. var t = this.getUtoTmapping( u );
  30. return this.getPoint( t );
  31. };
  32. // Get sequence of points using getPoint( t )
  33. THREE.Curve.prototype.getPoints = function ( divisions ) {
  34. if ( !divisions ) divisions = 5;
  35. var d, pts = [];
  36. for ( d = 0; d <= divisions; d ++ ) {
  37. pts.push( this.getPoint( d / divisions ) );
  38. };
  39. return pts;
  40. };
  41. // Get sequence of points using getPointAt( u )
  42. THREE.Curve.prototype.getSpacedPoints = function ( divisions ) {
  43. if ( !divisions ) divisions = 5;
  44. var d, pts = [];
  45. for ( d = 0; d <= divisions; d ++ ) {
  46. pts.push( this.getPointAt( d / divisions ) );
  47. };
  48. return pts;
  49. };
  50. // Get total curve length
  51. THREE.Curve.prototype.getLength = function () {
  52. var lengths = this.getLengths();
  53. return lengths[ lengths.length - 1 ];
  54. };
  55. // Get list of cumulative segment lengths
  56. THREE.Curve.prototype.getLengths = function ( divisions ) {
  57. if ( !divisions ) divisions = 200;
  58. if ( this.cacheLengths && ( this.cacheLengths.length == divisions + 1 ) ) {
  59. //console.log( "cached", this.cacheLengths );
  60. return this.cacheLengths;
  61. }
  62. var cache = [];
  63. var current, last = this.getPoint( 0 );
  64. var p, sum = 0;
  65. cache.push(0);
  66. for ( p = 1; p <= divisions; p++ ) {
  67. current = this.getPoint ( p / divisions );
  68. sum += current.distanceTo( last );
  69. cache.push( sum );
  70. last = current;
  71. }
  72. this.cacheLengths = cache;
  73. return cache; // { sums: cache, sum:sum }; Sum is in the last element.
  74. };
  75. // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equi distance
  76. THREE.Curve.prototype.getUtoTmapping = function ( u, distance ) {
  77. var arcLengths = this.getLengths();
  78. var i = 0, il = arcLengths.length;
  79. var targetArcLength; // The targetted u distance value to get
  80. if ( distance ) {
  81. targetArcLength = distance;
  82. } else {
  83. targetArcLength = u * arcLengths[ il - 1 ];
  84. }
  85. // // TODO Should do binary search + sub division + interpolation when needed
  86. // time = Date.now();
  87. // while ( i < il ) {
  88. //
  89. // i++;
  90. //
  91. // if ( targetArcLength < arcLengths[ i ] ) break;
  92. //
  93. // }
  94. //
  95. // i--;
  96. // console.log('o' , i, Date.now()- time);
  97. time = Date.now();
  98. // binary search for the index with largest value smaller than target u distance
  99. var low=0, high = il-1, comparison;
  100. while (low <= high) {
  101. i = Math.floor((low + high) / 2);
  102. comparison = arcLengths[ i ] - targetArcLength;
  103. if (comparison < 0) {
  104. low = i + 1; continue;
  105. } else if (comparison > 0) {
  106. high = i - 1; continue;
  107. } else {
  108. high = i;
  109. break;
  110. // DONE
  111. }
  112. }
  113. i = high;
  114. //console.log('b' , i, low, high, Date.now()- time);
  115. if (arcLengths[i] == targetArcLength) {
  116. var t = i / (il - 1);
  117. return t;
  118. }
  119. // We could get finer grain ar lengths, or use simple interpolatation between two points
  120. var lengthBefore = arcLengths[i];
  121. var lengthAfter = arcLengths[i+1];
  122. var segmentLength = lengthAfter - lengthBefore;
  123. // determine where we are between the 'before' and 'after' points.
  124. var segmentFraction = (targetArcLength - lengthBefore) / segmentLength;
  125. // add that fractional amount to t
  126. t = (i + segmentFraction) / (il -1);
  127. return t;
  128. };
  129. /**************************************************************
  130. * Line
  131. **************************************************************/
  132. THREE.LineCurve = function ( x1, y1, x2, y2 ) {
  133. this.x1 = x1;
  134. this.y1 = y1;
  135. this.x2 = x2;
  136. this.y2 = y2;
  137. };
  138. THREE.LineCurve.prototype = new THREE.Curve();
  139. THREE.LineCurve.prototype.constructor = THREE.LineCurve;
  140. THREE.LineCurve.prototype.getPoint = function ( t ) {
  141. var dx = this.x2 - this.x1;
  142. var dy = this.y2 - this.y1;
  143. var tx = this.x1 + dx * t;
  144. var ty = this.y1 + dy * t;
  145. return new THREE.Vector2( tx, ty );
  146. };
  147. THREE.LineCurve.prototype.getNormalVector = function( t ) {
  148. tx = this.x2 - this.x1;
  149. ty = this.y2 - this.y1;
  150. // return normal unit vector
  151. return new THREE.Vector2( -ty , tx ).unit();
  152. }
  153. /**************************************************************
  154. * Quadratic Bezier curve
  155. **************************************************************/
  156. THREE.QuadraticBezierCurve = function ( x0, y0, x1, y1, x2, y2 ) {
  157. this.x0 = x0;
  158. this.y0 = y0;
  159. this.x1 = x1;
  160. this.y1 = y1;
  161. this.x2 = x2;
  162. this.y2 = y2;
  163. };
  164. THREE.QuadraticBezierCurve.prototype = new THREE.Curve();
  165. THREE.QuadraticBezierCurve.prototype.constructor = THREE.QuadraticBezierCurve;
  166. THREE.QuadraticBezierCurve.prototype.getPoint = function ( t ) {
  167. var tx, ty;
  168. tx = THREE.Shape.Utils.b2( t, this.x0, this.x1, this.x2 );
  169. ty = THREE.Shape.Utils.b2( t, this.y0, this.y1, this.y2 );
  170. return new THREE.Vector2( tx, ty );
  171. };
  172. THREE.QuadraticBezierCurve.prototype.getNormalVector = function( t ) {
  173. // iterate sub segments
  174. // get lengths for sub segments
  175. // if segment is bezier
  176. // perform subdivisions or perform integrals
  177. var x0, y0, x1, y1, x2, y2;
  178. // x0 = this.actions[ 0 ].args[ 0 ];
  179. // y0 = this.actions[ 0 ].args[ 1 ];
  180. //
  181. // x1 = this.actions[ 1 ].args[ 0 ];
  182. // y1 = this.actions[ 1 ].args[ 1 ];
  183. //
  184. // x2 = this.actions[ 1 ].args[ 2 ];
  185. // y2 = this.actions[ 1 ].args[ 3 ];
  186. var tx, ty;
  187. tx = THREE.Curve.Utils.tangentQuadraticBezier( t, this.x0, this.x1, this.x2 );
  188. ty = THREE.Curve.Utils.tangentQuadraticBezier( t, this.y0, this.y1, this.y2 );
  189. // return normal unit vector
  190. return new THREE.Vector2( -ty , tx ).unit();
  191. };
  192. /**************************************************************
  193. * Cubic Bezier curve
  194. **************************************************************/
  195. THREE.CubicBezierCurve = function ( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  196. this.x0 = x0;
  197. this.y0 = y0;
  198. this.x1 = x1;
  199. this.y1 = y1;
  200. this.x2 = x2;
  201. this.y2 = y2;
  202. this.x3 = x3;
  203. this.y3 = y3;
  204. };
  205. THREE.CubicBezierCurve.prototype = new THREE.Curve();
  206. THREE.CubicBezierCurve.prototype.constructor = THREE.CubicBezierCurve;
  207. THREE.CubicBezierCurve.prototype.getPoint = function ( t ) {
  208. var tx, ty;
  209. tx = THREE.Shape.Utils.b3( t, this.x0, this.x1, this.x2, this.x3 );
  210. ty = THREE.Shape.Utils.b3( t, this.y0, this.y1, this.y2, this.y3 );
  211. return new THREE.Vector2( tx, ty );
  212. };
  213. /**************************************************************
  214. * Spline curve
  215. **************************************************************/
  216. THREE.SplineCurve = function ( points /* array of Vector2*/ ) {
  217. this.points = points;
  218. };
  219. THREE.SplineCurve.prototype = new THREE.Curve();
  220. THREE.SplineCurve.prototype.constructor = THREE.SplineCurve;
  221. THREE.SplineCurve.prototype.getPoint = function ( t ) {
  222. var v = new THREE.Vector2();
  223. var c = [];
  224. var points = this.points, point, intPoint, weight;
  225. point = ( points.length - 1 ) * t;
  226. intPoint = Math.floor( point );
  227. weight = point - intPoint;
  228. c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1;
  229. c[ 1 ] = intPoint;
  230. c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
  231. c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;
  232. v.x = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].x, points[ c[ 1 ] ].x, points[ c[ 2 ] ].x, points[ c[ 3 ] ].x, weight );
  233. v.y = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].y, points[ c[ 1 ] ].y, points[ c[ 2 ] ].y, points[ c[ 3 ] ].y, weight );
  234. return v;
  235. };
  236. /**************************************************************
  237. * Arc curve
  238. **************************************************************/
  239. THREE.ArcCurve = function ( aX, aY, aRadius,
  240. aStartAngle, aEndAngle,
  241. aClockwise ) {
  242. this.aX = aX;
  243. this.aY = aY;
  244. this.aRadius = aRadius;
  245. this.aStartAngle = aStartAngle;
  246. this.aEndAngle = aEndAngle;
  247. this.aClockwise = aClockwise;
  248. };
  249. THREE.ArcCurve.prototype = new THREE.Curve();
  250. THREE.ArcCurve.prototype.constructor = THREE.ArcCurve;
  251. THREE.ArcCurve.prototype.getPoint = function ( t ) {
  252. var deltaAngle = this.aEndAngle - this.aStartAngle;
  253. if ( !this.aClockwise ) {
  254. t = 1 - t;
  255. }
  256. var angle = this.aStartAngle + t * deltaAngle;
  257. var tx = this.aX + this.aRadius * Math.cos( angle );
  258. var ty = this.aY + this.aRadius * Math.sin( angle );
  259. return new THREE.Vector2( tx, ty );
  260. };
  261. /**************************************************************
  262. * Utils
  263. **************************************************************/
  264. THREE.Curve.Utils = {
  265. tangentQuadraticBezier: function ( t, p0, p1, p2 ) {
  266. return 2 * ( 1 - t ) * ( p1 - p0 ) + 2 * t * ( p2 - p1 );
  267. },
  268. tangentSpline: function ( t, p0, p1, p2, p3 ) {
  269. // To check if my formulas are correct
  270. var h00 = 6 * t * t - 6 * t; // derived from 2t^3 − 3t^2 + 1
  271. var h10 = 3 * t * t - 4 * t + 1; // t^3 − 2t^2 + t
  272. var h01 = -6 * t * t + 6 * t; // − 2t3 + 3t2
  273. var h11 = 3 * t * t - 2 * t; // t3 − t2
  274. },
  275. // Catmull-Rom
  276. interpolate: function( p0, p1, p2, p3, t ) {
  277. var v0 = ( p2 - p0 ) * 0.5;
  278. var v1 = ( p3 - p1 ) * 0.5;
  279. var t2 = t * t;
  280. var t3 = t * t2;
  281. return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  282. }
  283. };
  284. /*
  285. getPoint DONE
  286. getLength DONE
  287. getLengths DONE
  288. curve.getPoints(); DONE
  289. curve.getPointAtArcLength(t); DONE
  290. curve.transform(params);
  291. curve.getTangentAt(t)
  292. */
  293. /**************************************************************
  294. * 3D Curves
  295. **************************************************************/
  296. // A Factory method for creating new curve subclasses
  297. THREE.Curve.create = function( constructor, getPointFunc ) {
  298. var subClass = constructor;
  299. subClass.prototype = new THREE.Curve();
  300. subClass.prototype.constructor = constructor;
  301. subClass.prototype.getPoint = getPointFunc;
  302. return subClass;
  303. };
  304. /**************************************************************
  305. * Line3D
  306. **************************************************************/
  307. THREE.LineCurve3 = THREE.Curve.create(
  308. function ( v1, v2 ) {
  309. this.v1 = v1;
  310. this.v2 = v2;
  311. },
  312. function ( t ) {
  313. var r = new THREE.Vector3();
  314. r.sub( v2, v1 ); // diff
  315. r.multiplyScalar( t );
  316. r.addSelf( this.v1 );
  317. return r;
  318. }
  319. );
  320. /**************************************************************
  321. * Quadratic Bezier 3D curve
  322. **************************************************************/
  323. THREE.QuadraticBezierCurve3 = THREE.Curve.create(
  324. function ( v0, v1, v2 ) { // Qn should we use 2 Vector3 instead?
  325. this.v0 = v0;
  326. this.v1 = v1;
  327. this.v2 = v2;
  328. },
  329. function ( t ) {
  330. var tx, ty, tz;
  331. tx = THREE.Shape.Utils.b2( t, this.v0.x, this.v1.x, this.v2.x );
  332. ty = THREE.Shape.Utils.b2( t, this.v0.y, this.v1.y, this.v2.y );
  333. tz = THREE.Shape.Utils.b2( t, this.v0.z, this.v1.z, this.v2.z );
  334. return new THREE.Vector3( tx, ty, tz );
  335. }
  336. );