CurveExtras.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // GrannyKnot
  17. class GrannyKnot extends Curve {
  18. getPoint( t, optionalTarget = new Vector3() ) {
  19. const point = optionalTarget;
  20. t = 2 * Math.PI * t;
  21. const x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t );
  22. const 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 );
  23. const z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t );
  24. return point.set( x, y, z ).multiplyScalar( 20 );
  25. }
  26. }
  27. // HeartCurve
  28. class HeartCurve extends Curve {
  29. constructor( scale = 5 ) {
  30. super();
  31. this.scale = scale;
  32. }
  33. getPoint( t, optionalTarget = new Vector3() ) {
  34. const point = optionalTarget;
  35. t *= 2 * Math.PI;
  36. const x = 16 * Math.pow( Math.sin( t ), 3 );
  37. const y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t );
  38. const z = 0;
  39. return point.set( x, y, z ).multiplyScalar( this.scale );
  40. }
  41. }
  42. // Viviani's Curve
  43. class VivianiCurve extends Curve {
  44. constructor( scale = 70 ) {
  45. super();
  46. this.scale = scale;
  47. }
  48. getPoint( t, optionalTarget = new Vector3() ) {
  49. const point = optionalTarget;
  50. t = t * 4 * Math.PI; // normalized to 0..1
  51. const a = this.scale / 2;
  52. const x = a * ( 1 + Math.cos( t ) );
  53. const y = a * Math.sin( t );
  54. const z = 2 * a * Math.sin( t / 2 );
  55. return point.set( x, y, z );
  56. }
  57. }
  58. // KnotCurve
  59. class KnotCurve extends Curve {
  60. getPoint( t, optionalTarget = new Vector3() ) {
  61. const point = optionalTarget;
  62. t *= 2 * Math.PI;
  63. const R = 10;
  64. const s = 50;
  65. const x = s * Math.sin( t );
  66. const y = Math.cos( t ) * ( R + s * Math.cos( t ) );
  67. const z = Math.sin( t ) * ( R + s * Math.cos( t ) );
  68. return point.set( x, y, z );
  69. }
  70. }
  71. // HelixCurve
  72. class HelixCurve extends Curve {
  73. getPoint( t, optionalTarget = new Vector3() ) {
  74. const point = optionalTarget;
  75. const a = 30; // radius
  76. const b = 150; // height
  77. const t2 = 2 * Math.PI * t * b / 30;
  78. const x = Math.cos( t2 ) * a;
  79. const y = Math.sin( t2 ) * a;
  80. const z = b * t;
  81. return point.set( x, y, z );
  82. }
  83. }
  84. // TrefoilKnot
  85. class TrefoilKnot extends Curve {
  86. constructor( scale = 10 ) {
  87. super();
  88. this.scale = scale;
  89. }
  90. getPoint( t, optionalTarget = new Vector3() ) {
  91. const point = optionalTarget;
  92. t *= Math.PI * 2;
  93. const x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t );
  94. const y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t );
  95. const z = Math.sin( 3 * t );
  96. return point.set( x, y, z ).multiplyScalar( this.scale );
  97. }
  98. }
  99. // TorusKnot
  100. class TorusKnot extends Curve {
  101. constructor( scale = 10 ) {
  102. super();
  103. this.scale = scale;
  104. }
  105. getPoint( t, optionalTarget = new Vector3() ) {
  106. const point = optionalTarget;
  107. const p = 3;
  108. const q = 4;
  109. t *= Math.PI * 2;
  110. const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  111. const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  112. const z = Math.sin( q * t );
  113. return point.set( x, y, z ).multiplyScalar( this.scale );
  114. }
  115. }
  116. // CinquefoilKnot
  117. class CinquefoilKnot extends Curve {
  118. constructor( scale = 10 ) {
  119. super();
  120. this.scale = scale;
  121. }
  122. getPoint( t, optionalTarget = new Vector3() ) {
  123. const point = optionalTarget;
  124. const p = 2;
  125. const q = 5;
  126. t *= Math.PI * 2;
  127. const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  128. const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  129. const z = Math.sin( q * t );
  130. return point.set( x, y, z ).multiplyScalar( this.scale );
  131. }
  132. }
  133. // TrefoilPolynomialKnot
  134. class TrefoilPolynomialKnot extends Curve {
  135. constructor( scale = 10 ) {
  136. super();
  137. this.scale = scale;
  138. }
  139. getPoint( t, optionalTarget = new Vector3() ) {
  140. const point = optionalTarget;
  141. t = t * 4 - 2;
  142. const x = Math.pow( t, 3 ) - 3 * t;
  143. const y = Math.pow( t, 4 ) - 4 * t * t;
  144. const z = 1 / 5 * Math.pow( t, 5 ) - 2 * t;
  145. return point.set( x, y, z ).multiplyScalar( this.scale );
  146. }
  147. }
  148. function scaleTo( x, y, t ) {
  149. const r = y - x;
  150. return t * r + x;
  151. }
  152. // FigureEightPolynomialKnot
  153. class FigureEightPolynomialKnot extends Curve {
  154. constructor( scale = 1 ) {
  155. super();
  156. this.scale = scale;
  157. }
  158. getPoint( t, optionalTarget = new Vector3() ) {
  159. const point = optionalTarget;
  160. t = scaleTo( - 4, 4, t );
  161. const x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 );
  162. const y = Math.pow( t, 4 ) - 13 * t * t;
  163. const z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 );
  164. return point.set( x, y, z ).multiplyScalar( this.scale );
  165. }
  166. }
  167. // DecoratedTorusKnot4a
  168. class DecoratedTorusKnot4a extends Curve {
  169. constructor( scale = 40 ) {
  170. super();
  171. this.scale = scale;
  172. }
  173. getPoint( t, optionalTarget = new Vector3() ) {
  174. const point = optionalTarget;
  175. t *= Math.PI * 2;
  176. const x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  177. const y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  178. const z = 0.35 * Math.sin( 5 * t );
  179. return point.set( x, y, z ).multiplyScalar( this.scale );
  180. }
  181. }
  182. // DecoratedTorusKnot4b
  183. class DecoratedTorusKnot4b extends Curve {
  184. constructor( scale = 40 ) {
  185. super();
  186. this.scale = scale;
  187. }
  188. getPoint( t, optionalTarget = new Vector3() ) {
  189. const point = optionalTarget;
  190. const fi = t * Math.PI * 2;
  191. const x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  192. const y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  193. const z = 0.2 * Math.sin( 9 * fi );
  194. return point.set( x, y, z ).multiplyScalar( this.scale );
  195. }
  196. }
  197. // DecoratedTorusKnot5a
  198. class DecoratedTorusKnot5a extends Curve {
  199. constructor( scale = 40 ) {
  200. super();
  201. this.scale = scale;
  202. }
  203. getPoint( t, optionalTarget = new Vector3() ) {
  204. const point = optionalTarget;
  205. const fi = t * Math.PI * 2;
  206. const x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  207. const y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  208. const z = 0.2 * Math.sin( 20 * fi );
  209. return point.set( x, y, z ).multiplyScalar( this.scale );
  210. }
  211. }
  212. // DecoratedTorusKnot5c
  213. class DecoratedTorusKnot5c extends Curve {
  214. constructor( scale = 40 ) {
  215. super();
  216. this.scale = scale;
  217. }
  218. getPoint( t, optionalTarget = new Vector3() ) {
  219. const point = optionalTarget;
  220. const fi = t * Math.PI * 2;
  221. const x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  222. const y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  223. const z = 0.35 * Math.sin( 15 * fi );
  224. return point.set( x, y, z ).multiplyScalar( this.scale );
  225. }
  226. }
  227. const Curves = {
  228. GrannyKnot: GrannyKnot,
  229. HeartCurve: HeartCurve,
  230. VivianiCurve: VivianiCurve,
  231. KnotCurve: KnotCurve,
  232. HelixCurve: HelixCurve,
  233. TrefoilKnot: TrefoilKnot,
  234. TorusKnot: TorusKnot,
  235. CinquefoilKnot: CinquefoilKnot,
  236. TrefoilPolynomialKnot: TrefoilPolynomialKnot,
  237. FigureEightPolynomialKnot: FigureEightPolynomialKnot,
  238. DecoratedTorusKnot4a: DecoratedTorusKnot4a,
  239. DecoratedTorusKnot4b: DecoratedTorusKnot4b,
  240. DecoratedTorusKnot5a: DecoratedTorusKnot5a,
  241. DecoratedTorusKnot5c: DecoratedTorusKnot5c
  242. };
  243. export { Curves };