1 /**
2     Holds utility functions for the OpenSimplex Implementation - original source and implementation: https://gist.github.com/KdotJPG/b1270127455a94ac5d19 and http://uniblock.tumblr.com/
3     Additional functions that might appear are also here
4 */
5 module dosimplex.util;
6 
7 
8 /// fastFloor floors a double into an int - like floor but with builtIn conversion
9 @nogc pure @safe int fastFloor(double x)
10 {
11     int xi = cast(int)x;
12     return x < xi ? xi - 1 : xi;
13 }
14 
15 /**
16     Gradients for the noise functions
17 */
18 enum GRADIENTS : byte[] {
19     /** Gradients for 2D. They approximate the directions to the
20     *   vertices of an octagon from the center.
21     */
22     GRADIENTS_2D = [
23         5,  2,    2,  5,
24         -5,  2,   -2,  5,
25         5, -2,    2, -5,
26         -5, -2,   -2, -5,
27     ],
28 
29     /**  Gradients for 3D. They approximate the directions to the
30     *    vertices of a rhombicuboctahedron from the center, skewed so
31     *    that the triangular and square facets can be inscribed inside
32     *    circles of the same radius.
33     */
34     GRADIENTS_3D = [
35         -11,  4,  4,     -4,  11,  4,    -4,  4,  11,
36         11,  4,  4,      4,  11,  4,     4,  4,  11,
37 		-11, -4,  4,     -4, -11,  4,    -4, -4,  11,
38         11, -4,  4,      4, -11,  4,     4, -4,  11,
39 		-11,  4, -4,     -4,  11, -4,    -4,  4, -11,
40         11,  4, -4,      4,  11, -4,     4,  4, -11,
41 		-11, -4, -4,     -4, -11, -4,    -4, -4, -11,
42         11, -4, -4,      4, -11, -4,     4, -4, -11,
43     ],
44 
45     /**     Gradients for 4D. They approximate the directions to the
46     *       vertices of a disprismatotesseractihexadecachoron from the center,
47     *       skewed so that the tetrahedral and cubic facets can be inscribed inside
48     *       spheres of the same radius.
49     */
50     GRADIENTS_4D = [
51         3,  1,  1,  1,      1,  3,  1,  1,      1,  1,  3,  1,      1,  1,  1,  3,
52 	    -3,  1,  1,  1,     -1,  3,  1,  1,     -1,  1,  3,  1,     -1,  1,  1,  3,
53         3, -1,  1,  1,      1, -3,  1,  1,      1, -1,  3,  1,      1, -1,  1,  3,
54 	    -3, -1,  1,  1,     -1, -3,  1,  1,     -1, -1,  3,  1,     -1, -1,  1,  3,
55         3,  1, -1,  1,      1,  3, -1,  1,      1,  1, -3,  1,      1,  1, -1,  3,
56 	    -3,  1, -1,  1,     -1,  3, -1,  1,     -1,  1, -3,  1,     -1,  1, -1,  3,
57         3, -1, -1,  1,      1, -3, -1,  1,      1, -1, -3,  1,      1, -1, -1,  3,
58 	    -3, -1, -1,  1,     -1, -3, -1,  1,     -1, -1, -3,  1,     -1, -1, -1,  3,
59         3,  1,  1, -1,      1,  3,  1, -1,      1,  1,  3, -1,      1,  1,  1, -3,
60 	    -3,  1,  1, -1,     -1,  3,  1, -1,     -1,  1,  3, -1,     -1,  1,  1, -3,
61         3, -1,  1, -1,      1, -3,  1, -1,      1, -1,  3, -1,      1, -1,  1, -3,
62 	    -3, -1,  1, -1,     -1, -3,  1, -1,     -1, -1,  3, -1,     -1, -1,  1, -3,
63         3,  1, -1, -1,      1,  3, -1, -1,      1,  1, -3, -1,      1,  1, -1, -3,
64 	    -3,  1, -1, -1,     -1,  3, -1, -1,     -1,  1, -3, -1,     -1,  1, -1, -3,
65         3, -1, -1, -1,      1, -3, -1, -1,      1, -1, -3, -1,      1, -1, -1, -3,
66 	    -3, -1, -1, -1,     -1, -3, -1, -1,     -1, -1, -3, -1,     -1, -1, -1, -3,
67     ]
68 }