CurveExtras.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import {
  2. Curve,
  3. Vector3
  4. } from '../../../build/three.module.js';
  5. /**
  6. * A bunch of parametric curves
  7. *
  8. * Formulas collected from various sources
  9. * http://mathworld.wolfram.com/HeartCurve.html
  10. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
  11. * http://en.wikipedia.org/wiki/Viviani%27s_curve
  12. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
  13. * http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
  14. * https://prideout.net/blog/old/blog/index.html@p=44.html
  15. */
  16. var Curves = ( function () {
  17. // GrannyKnot
  18. function GrannyKnot() {
  19. Curve.call( this );
  20. }
  21. GrannyKnot.prototype = Object.create( Curve.prototype );
  22. GrannyKnot.prototype.constructor = GrannyKnot;
  23. GrannyKnot.prototype.getPoint = function ( t, optionalTarget ) {
  24. var point = optionalTarget || new Vector3();
  25. t = 2 * Math.PI * t;
  26. var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t );
  27. var y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t );
  28. var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t );
  29. return point.set( x, y, z ).multiplyScalar( 20 );
  30. };
  31. // HeartCurve
  32. function HeartCurve( scale ) {
  33. Curve.call( this );
  34. this.scale = ( scale === undefined ) ? 5 : scale;
  35. }
  36. HeartCurve.prototype = Object.create( Curve.prototype );
  37. HeartCurve.prototype.constructor = HeartCurve;
  38. HeartCurve.prototype.getPoint = function ( t, optionalTarget ) {
  39. var point = optionalTarget || new Vector3();
  40. t *= 2 * Math.PI;
  41. var x = 16 * Math.pow( Math.sin( t ), 3 );
  42. var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t );
  43. var z = 0;
  44. return point.set( x, y, z ).multiplyScalar( this.scale );
  45. };
  46. // Viviani's Curve
  47. function VivianiCurve( scale ) {
  48. Curve.call( this );
  49. this.scale = ( scale === undefined ) ? 70 : scale;
  50. }
  51. VivianiCurve.prototype = Object.create( Curve.prototype );
  52. VivianiCurve.prototype.constructor = VivianiCurve;
  53. VivianiCurve.prototype.getPoint = function ( t, optionalTarget ) {
  54. var point = optionalTarget || new Vector3();
  55. t = t * 4 * Math.PI; // normalized to 0..1
  56. var a = this.scale / 2;
  57. var x = a * ( 1 + Math.cos( t ) );
  58. var y = a * Math.sin( t );
  59. var z = 2 * a * Math.sin( t / 2 );
  60. return point.set( x, y, z );
  61. };
  62. // KnotCurve
  63. function KnotCurve() {
  64. Curve.call( this );
  65. }
  66. KnotCurve.prototype = Object.create( Curve.prototype );
  67. KnotCurve.prototype.constructor = KnotCurve;
  68. KnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
  69. var point = optionalTarget || new Vector3();
  70. t *= 2 * Math.PI;
  71. var R = 10;
  72. var s = 50;
  73. var x = s * Math.sin( t );
  74. var y = Math.cos( t ) * ( R + s * Math.cos( t ) );
  75. var z = Math.sin( t ) * ( R + s * Math.cos( t ) );
  76. return point.set( x, y, z );
  77. };
  78. // HelixCurve
  79. function HelixCurve() {
  80. Curve.call( this );
  81. }
  82. HelixCurve.prototype = Object.create( Curve.prototype );
  83. HelixCurve.prototype.constructor = HelixCurve;
  84. HelixCurve.prototype.getPoint = function ( t, optionalTarget ) {
  85. var point = optionalTarget || new Vector3();
  86. var a = 30; // radius
  87. var b = 150; // height
  88. var t2 = 2 * Math.PI * t * b / 30;
  89. var x = Math.cos( t2 ) * a;
  90. var y = Math.sin( t2 ) * a;
  91. var z = b * t;
  92. return point.set( x, y, z );
  93. };
  94. // TrefoilKnot
  95. function TrefoilKnot( scale ) {
  96. Curve.call( this );
  97. this.scale = ( scale === undefined ) ? 10 : scale;
  98. }
  99. TrefoilKnot.prototype = Object.create( Curve.prototype );
  100. TrefoilKnot.prototype.constructor = TrefoilKnot;
  101. TrefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  102. var point = optionalTarget || new Vector3();
  103. t *= Math.PI * 2;
  104. var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t );
  105. var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t );
  106. var z = Math.sin( 3 * t );
  107. return point.set( x, y, z ).multiplyScalar( this.scale );
  108. };
  109. // TorusKnot
  110. function TorusKnot( scale ) {
  111. Curve.call( this );
  112. this.scale = ( scale === undefined ) ? 10 : scale;
  113. }
  114. TorusKnot.prototype = Object.create( Curve.prototype );
  115. TorusKnot.prototype.constructor = TorusKnot;
  116. TorusKnot.prototype.getPoint = function ( t, optionalTarget ) {
  117. var point = optionalTarget || new Vector3();
  118. var p = 3;
  119. var q = 4;
  120. t *= Math.PI * 2;
  121. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  122. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  123. var z = Math.sin( q * t );
  124. return point.set( x, y, z ).multiplyScalar( this.scale );
  125. };
  126. // CinquefoilKnot
  127. function CinquefoilKnot( scale ) {
  128. Curve.call( this );
  129. this.scale = ( scale === undefined ) ? 10 : scale;
  130. }
  131. CinquefoilKnot.prototype = Object.create( Curve.prototype );
  132. CinquefoilKnot.prototype.constructor = CinquefoilKnot;
  133. CinquefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  134. var point = optionalTarget || new Vector3();
  135. var p = 2;
  136. var q = 5;
  137. t *= Math.PI * 2;
  138. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  139. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  140. var z = Math.sin( q * t );
  141. return point.set( x, y, z ).multiplyScalar( this.scale );
  142. };
  143. // TrefoilPolynomialKnot
  144. function TrefoilPolynomialKnot( scale ) {
  145. Curve.call( this );
  146. this.scale = ( scale === undefined ) ? 10 : scale;
  147. }
  148. TrefoilPolynomialKnot.prototype = Object.create( Curve.prototype );
  149. TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot;
  150. TrefoilPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  151. var point = optionalTarget || new Vector3();
  152. t = t * 4 - 2;
  153. var x = Math.pow( t, 3 ) - 3 * t;
  154. var y = Math.pow( t, 4 ) - 4 * t * t;
  155. var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t;
  156. return point.set( x, y, z ).multiplyScalar( this.scale );
  157. };
  158. var scaleTo = function ( x, y, t ) {
  159. var r = y - x;
  160. return t * r + x;
  161. };
  162. // FigureEightPolynomialKnot
  163. function FigureEightPolynomialKnot( scale ) {
  164. Curve.call( this );
  165. this.scale = ( scale === undefined ) ? 1 : scale;
  166. }
  167. FigureEightPolynomialKnot.prototype = Object.create( Curve.prototype );
  168. FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot;
  169. FigureEightPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  170. var point = optionalTarget || new Vector3();
  171. t = scaleTo( - 4, 4, t );
  172. var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 );
  173. var y = Math.pow( t, 4 ) - 13 * t * t;
  174. var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 );
  175. return point.set( x, y, z ).multiplyScalar( this.scale );
  176. };
  177. // DecoratedTorusKnot4a
  178. function DecoratedTorusKnot4a( scale ) {
  179. Curve.call( this );
  180. this.scale = ( scale === undefined ) ? 40 : scale;
  181. }
  182. DecoratedTorusKnot4a.prototype = Object.create( Curve.prototype );
  183. DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a;
  184. DecoratedTorusKnot4a.prototype.getPoint = function ( t, optionalTarget ) {
  185. var point = optionalTarget || new Vector3();
  186. t *= Math.PI * 2;
  187. var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  188. var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  189. var z = 0.35 * Math.sin( 5 * t );
  190. return point.set( x, y, z ).multiplyScalar( this.scale );
  191. };
  192. // DecoratedTorusKnot4b
  193. function DecoratedTorusKnot4b( scale ) {
  194. Curve.call( this );
  195. this.scale = ( scale === undefined ) ? 40 : scale;
  196. }
  197. DecoratedTorusKnot4b.prototype = Object.create( Curve.prototype );
  198. DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b;
  199. DecoratedTorusKnot4b.prototype.getPoint = function ( t, optionalTarget ) {
  200. var point = optionalTarget || new Vector3();
  201. var fi = t * Math.PI * 2;
  202. var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  203. var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  204. var z = 0.2 * Math.sin( 9 * fi );
  205. return point.set( x, y, z ).multiplyScalar( this.scale );
  206. };
  207. // DecoratedTorusKnot5a
  208. function DecoratedTorusKnot5a( scale ) {
  209. Curve.call( this );
  210. this.scale = ( scale === undefined ) ? 40 : scale;
  211. }
  212. DecoratedTorusKnot5a.prototype = Object.create( Curve.prototype );
  213. DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a;
  214. DecoratedTorusKnot5a.prototype.getPoint = function ( t, optionalTarget ) {
  215. var point = optionalTarget || new Vector3();
  216. var fi = t * Math.PI * 2;
  217. var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  218. var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  219. var z = 0.2 * Math.sin( 20 * fi );
  220. return point.set( x, y, z ).multiplyScalar( this.scale );
  221. };
  222. // DecoratedTorusKnot5c
  223. function DecoratedTorusKnot5c( scale ) {
  224. Curve.call( this );
  225. this.scale = ( scale === undefined ) ? 40 : scale;
  226. }
  227. DecoratedTorusKnot5c.prototype = Object.create( Curve.prototype );
  228. DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c;
  229. DecoratedTorusKnot5c.prototype.getPoint = function ( t, optionalTarget ) {
  230. var point = optionalTarget || new Vector3();
  231. var fi = t * Math.PI * 2;
  232. var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  233. var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  234. var z = 0.35 * Math.sin( 15 * fi );
  235. return point.set( x, y, z ).multiplyScalar( this.scale );
  236. };
  237. return {
  238. GrannyKnot: GrannyKnot,
  239. HeartCurve: HeartCurve,
  240. VivianiCurve: VivianiCurve,
  241. KnotCurve: KnotCurve,
  242. HelixCurve: HelixCurve,
  243. TrefoilKnot: TrefoilKnot,
  244. TorusKnot: TorusKnot,
  245. CinquefoilKnot: CinquefoilKnot,
  246. TrefoilPolynomialKnot: TrefoilPolynomialKnot,
  247. FigureEightPolynomialKnot: FigureEightPolynomialKnot,
  248. DecoratedTorusKnot4a: DecoratedTorusKnot4a,
  249. DecoratedTorusKnot4b: DecoratedTorusKnot4b,
  250. DecoratedTorusKnot5a: DecoratedTorusKnot5a,
  251. DecoratedTorusKnot5c: DecoratedTorusKnot5c
  252. };
  253. } )();
  254. export { Curves };