- added Mesh->derived and Mesh->decimated DerivedMesh pointers
[blender.git] / source / blender / blenlib / intern / noise.c
1 /*
2  *
3  * $Id$
4  *
5  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version. The Blender
11  * Foundation also sells licenses for use in proprietary software under
12  * the Blender License.  See http://www.blender.org/BL/ for information
13  * about this.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL/BL DUAL LICENSE BLOCK *****
32  *
33  */
34
35 #include <math.h>
36 #include "BLI_blenlib.h"
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 /* local */
43 float noise3_perlin(float vec[3]);
44 float turbulence_perlin(float *point, float lofreq, float hifreq);
45 float turbulencep(float noisesize, float x, float y, float z, int nr);
46
47 #define HASHVEC(x,y,z) hashvectf+3*hash[ (hash[ (hash[(z) & 255]+(y)) & 255]+(x)) & 255]
48
49 /* needed for voronoi */
50 #define HASHPNT(x,y,z) hashpntf+3*hash[ (hash[ (hash[(z) & 255]+(y)) & 255]+(x)) & 255]
51 static float hashpntf[768] = {0.536902, 0.020915, 0.501445, 0.216316, 0.517036, 0.822466, 0.965315,
52 0.377313, 0.678764, 0.744545, 0.097731, 0.396357, 0.247202, 0.520897,
53 0.613396, 0.542124, 0.146813, 0.255489, 0.810868, 0.638641, 0.980742,
54 0.292316, 0.357948, 0.114382, 0.861377, 0.629634, 0.722530, 0.714103,
55 0.048549, 0.075668, 0.564920, 0.162026, 0.054466, 0.411738, 0.156897,
56 0.887657, 0.599368, 0.074249, 0.170277, 0.225799, 0.393154, 0.301348,
57 0.057434, 0.293849, 0.442745, 0.150002, 0.398732, 0.184582, 0.915200,
58 0.630984, 0.974040, 0.117228, 0.795520, 0.763238, 0.158982, 0.616211,
59 0.250825, 0.906539, 0.316874, 0.676205, 0.234720, 0.667673, 0.792225,
60 0.273671, 0.119363, 0.199131, 0.856716, 0.828554, 0.900718, 0.705960,
61 0.635923, 0.989433, 0.027261, 0.283507, 0.113426, 0.388115, 0.900176,
62 0.637741, 0.438802, 0.715490, 0.043692, 0.202640, 0.378325, 0.450325,
63 0.471832, 0.147803, 0.906899, 0.524178, 0.784981, 0.051483, 0.893369,
64 0.596895, 0.275635, 0.391483, 0.844673, 0.103061, 0.257322, 0.708390,
65 0.504091, 0.199517, 0.660339, 0.376071, 0.038880, 0.531293, 0.216116,
66 0.138672, 0.907737, 0.807994, 0.659582, 0.915264, 0.449075, 0.627128,
67 0.480173, 0.380942, 0.018843, 0.211808, 0.569701, 0.082294, 0.689488, 
68 0.573060, 0.593859, 0.216080, 0.373159, 0.108117, 0.595539, 0.021768, 
69 0.380297, 0.948125, 0.377833, 0.319699, 0.315249, 0.972805, 0.792270, 
70 0.445396, 0.845323, 0.372186, 0.096147, 0.689405, 0.423958, 0.055675, 
71 0.117940, 0.328456, 0.605808, 0.631768, 0.372170, 0.213723, 0.032700, 
72 0.447257, 0.440661, 0.728488, 0.299853, 0.148599, 0.649212, 0.498381,
73 0.049921, 0.496112, 0.607142, 0.562595, 0.990246, 0.739659, 0.108633, 
74 0.978156, 0.209814, 0.258436, 0.876021, 0.309260, 0.600673, 0.713597, 
75 0.576967, 0.641402, 0.853930, 0.029173, 0.418111, 0.581593, 0.008394, 
76 0.589904, 0.661574, 0.979326, 0.275724, 0.111109, 0.440472, 0.120839, 
77 0.521602, 0.648308, 0.284575, 0.204501, 0.153286, 0.822444, 0.300786, 
78 0.303906, 0.364717, 0.209038, 0.916831, 0.900245, 0.600685, 0.890002, 
79 0.581660, 0.431154, 0.705569, 0.551250, 0.417075, 0.403749, 0.696652, 
80 0.292652, 0.911372, 0.690922, 0.323718, 0.036773, 0.258976, 0.274265, 
81 0.225076, 0.628965, 0.351644, 0.065158, 0.080340, 0.467271, 0.130643,
82 0.385914, 0.919315, 0.253821, 0.966163, 0.017439, 0.392610, 0.478792, 
83 0.978185, 0.072691, 0.982009, 0.097987, 0.731533, 0.401233, 0.107570, 
84 0.349587, 0.479122, 0.700598, 0.481751, 0.788429, 0.706864, 0.120086, 
85 0.562691, 0.981797, 0.001223, 0.192120, 0.451543, 0.173092, 0.108960,
86 0.549594, 0.587892, 0.657534, 0.396365, 0.125153, 0.666420, 0.385823, 
87 0.890916, 0.436729, 0.128114, 0.369598, 0.759096, 0.044677, 0.904752, 
88 0.088052, 0.621148, 0.005047, 0.452331, 0.162032, 0.494238, 0.523349, 
89 0.741829, 0.698450, 0.452316, 0.563487, 0.819776, 0.492160, 0.004210, 
90 0.647158, 0.551475, 0.362995, 0.177937, 0.814722, 0.727729, 0.867126, 
91 0.997157, 0.108149, 0.085726, 0.796024, 0.665075, 0.362462, 0.323124,
92 0.043718, 0.042357, 0.315030, 0.328954, 0.870845, 0.683186, 0.467922, 
93 0.514894, 0.809971, 0.631979, 0.176571, 0.366320, 0.850621, 0.505555, 
94 0.749551, 0.750830, 0.401714, 0.481216, 0.438393, 0.508832, 0.867971, 
95 0.654581, 0.058204, 0.566454, 0.084124, 0.548539, 0.902690, 0.779571, 
96 0.562058, 0.048082, 0.863109, 0.079290, 0.713559, 0.783496, 0.265266, 
97 0.672089, 0.786939, 0.143048, 0.086196, 0.876129, 0.408708, 0.229312, 
98 0.629995, 0.206665, 0.207308, 0.710079, 0.341704, 0.264921, 0.028748, 
99 0.629222, 0.470173, 0.726228, 0.125243, 0.328249, 0.794187, 0.741340, 
100 0.489895, 0.189396, 0.724654, 0.092841, 0.039809, 0.860126, 0.247701, 
101 0.655331, 0.964121, 0.672536, 0.044522, 0.690567, 0.837238, 0.631520, 
102 0.953734, 0.352484, 0.289026, 0.034152, 0.852575, 0.098454, 0.795529, 
103 0.452181, 0.826159, 0.186993, 0.820725, 0.440328, 0.922137, 0.704592,
104 0.915437, 0.738183, 0.733461, 0.193798, 0.929213, 0.161390, 0.318547,
105 0.888751, 0.430968, 0.740837, 0.193544, 0.872253, 0.563074, 0.274598, 
106 0.347805, 0.666176, 0.449831, 0.800991, 0.588727, 0.052296, 0.714761, 
107 0.420620, 0.570325, 0.057550, 0.210888, 0.407312, 0.662848, 0.924382, 
108 0.895958, 0.775198, 0.688605, 0.025721, 0.301913, 0.791408, 0.500602, 
109 0.831984, 0.828509, 0.642093, 0.494174, 0.525880, 0.446365, 0.440063, 
110 0.763114, 0.630358, 0.223943, 0.333806, 0.906033, 0.498306, 0.241278,
111 0.427640, 0.772683, 0.198082, 0.225379, 0.503894, 0.436599, 0.016503, 
112 0.803725, 0.189878, 0.291095, 0.499114, 0.151573, 0.079031, 0.904618, 
113 0.708535, 0.273900, 0.067419, 0.317124, 0.936499, 0.716511, 0.543845, 
114 0.939909, 0.826574, 0.715090, 0.154864, 0.750150, 0.845808, 0.648108, 
115 0.556564, 0.644757, 0.140873, 0.799167, 0.632989, 0.444245, 0.471978, 
116 0.435910, 0.359793, 0.216241, 0.007633, 0.337236, 0.857863, 0.380247, 
117 0.092517, 0.799973, 0.919000, 0.296798, 0.096989, 0.854831, 0.165369, 
118 0.568475, 0.216855, 0.020457, 0.835511, 0.538039, 0.999742, 0.620226, 
119 0.244053, 0.060399, 0.323007, 0.294874, 0.988899, 0.384919, 0.735655, 
120 0.773428, 0.549776, 0.292882, 0.660611, 0.593507, 0.621118, 0.175269, 
121 0.682119, 0.794493, 0.868197, 0.632150, 0.807823, 0.509656, 0.482035, 
122 0.001780, 0.259126, 0.358002, 0.280263, 0.192985, 0.290367, 0.208111, 
123 0.917633, 0.114422, 0.925491, 0.981110, 0.255570, 0.974862, 0.016629,
124 0.552599, 0.575741, 0.612978, 0.615965, 0.803615, 0.772334, 0.089745, 
125 0.838812, 0.634542, 0.113709, 0.755832, 0.577589, 0.667489, 0.529834,
126 0.325660, 0.817597, 0.316557, 0.335093, 0.737363, 0.260951, 0.737073, 
127 0.049540, 0.735541, 0.988891, 0.299116, 0.147695, 0.417271, 0.940811, 
128 0.524160, 0.857968, 0.176403, 0.244835, 0.485759, 0.033353, 0.280319, 
129 0.750688, 0.755809, 0.924208, 0.095956, 0.962504, 0.275584, 0.173715,
130 0.942716, 0.706721, 0.078464, 0.576716, 0.804667, 0.559249, 0.900611, 
131 0.646904, 0.432111, 0.927885, 0.383277, 0.269973, 0.114244, 0.574867, 
132 0.150703, 0.241855, 0.272871, 0.199950, 0.079719, 0.868566, 0.962833, 
133 0.789122, 0.320025, 0.905554, 0.234876, 0.991356, 0.061913, 0.732911, 
134 0.785960, 0.874074, 0.069035, 0.658632, 0.309901, 0.023676, 0.791603, 
135 0.764661, 0.661278, 0.319583, 0.829650, 0.117091, 0.903124, 0.982098, 
136 0.161631, 0.193576, 0.670428, 0.857390, 0.003760, 0.572578, 0.222162, 
137 0.114551, 0.420118, 0.530404, 0.470682, 0.525527, 0.764281, 0.040596, 
138 0.443275, 0.501124, 0.816161, 0.417467, 0.332172, 0.447565, 0.614591, 
139 0.559246, 0.805295, 0.226342, 0.155065, 0.714630, 0.160925, 0.760001, 
140 0.453456, 0.093869, 0.406092, 0.264801, 0.720370, 0.743388, 0.373269, 
141 0.403098, 0.911923, 0.897249, 0.147038, 0.753037, 0.516093, 0.739257, 
142 0.175018, 0.045768, 0.735857, 0.801330, 0.927708, 0.240977, 0.591870,
143 0.921831, 0.540733, 0.149100, 0.423152, 0.806876, 0.397081, 0.061100, 
144 0.811630, 0.044899, 0.460915, 0.961202, 0.822098, 0.971524, 0.867608, 
145 0.773604, 0.226616, 0.686286, 0.926972, 0.411613, 0.267873, 0.081937, 
146 0.226124, 0.295664, 0.374594, 0.533240, 0.237876, 0.669629, 0.599083, 
147 0.513081, 0.878719, 0.201577, 0.721296, 0.495038, 0.079760, 0.965959,
148 0.233090, 0.052496, 0.714748, 0.887844, 0.308724, 0.972885, 0.723337,
149 0.453089, 0.914474, 0.704063, 0.823198, 0.834769, 0.906561, 0.919600,
150 0.100601, 0.307564, 0.901977, 0.468879, 0.265376, 0.885188, 0.683875,
151 0.868623, 0.081032, 0.466835, 0.199087, 0.663437, 0.812241, 0.311337,
152 0.821361, 0.356628, 0.898054, 0.160781, 0.222539, 0.714889, 0.490287,
153 0.984915, 0.951755, 0.964097, 0.641795, 0.815472, 0.852732, 0.862074,
154 0.051108, 0.440139, 0.323207, 0.517171, 0.562984, 0.115295, 0.743103,
155 0.977914, 0.337596, 0.440694, 0.535879, 0.959427, 0.351427, 0.704361,
156 0.010826, 0.131162, 0.577080, 0.349572, 0.774892, 0.425796, 0.072697,
157 0.500001, 0.267322, 0.909654, 0.206176, 0.223987, 0.937698, 0.323423,
158 0.117501, 0.490308, 0.474372, 0.689943, 0.168671, 0.719417, 0.188928,
159 0.330464, 0.265273, 0.446271, 0.171933, 0.176133, 0.474616, 0.140182,
160 0.114246, 0.905043, 0.713870, 0.555261, 0.951333};
161
162 unsigned char hash[512]= {
163 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
164 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
165 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
166 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
167 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
168 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
169 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
170 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
171 };
172
173
174 float hashvectf[768]= {
175 0.33783,0.715698,-0.611206,-0.944031,-0.326599,-0.045624,-0.101074,-0.416443,-0.903503,0.799286,0.49411,-0.341949,-0.854645,0.518036,0.033936,0.42514,-0.437866,-0.792114,-0.358948,0.597046,0.717377,-0.985413,0.144714,0.089294,-0.601776,-0.33728,-0.723907,-0.449921,0.594513,0.666382,0.208313,-0.10791,
176 0.972076,0.575317,0.060425,0.815643,0.293365,-0.875702,-0.383453,0.293762,0.465759,0.834686,-0.846008,-0.233398,-0.47934,-0.115814,0.143036,-0.98291,0.204681,-0.949036,-0.239532,0.946716,-0.263947,0.184326,-0.235596,0.573822,0.784332,0.203705,-0.372253,-0.905487,0.756989,-0.651031,0.055298,0.497803,
177 0.814697,-0.297363,-0.16214,0.063995,-0.98468,-0.329254,0.834381,0.441925,0.703827,-0.527039,-0.476227,0.956421,0.266113,0.119781,0.480133,0.482849,0.7323,-0.18631,0.961212,-0.203125,-0.748474,-0.656921,-0.090393,-0.085052,-0.165253,0.982544,-0.76947,0.628174,-0.115234,0.383148,0.537659,0.751068,
178 0.616486,-0.668488,-0.415924,-0.259979,-0.630005,0.73175,0.570953,-0.087952,0.816223,-0.458008,0.023254,0.888611,-0.196167,0.976563,-0.088287,-0.263885,-0.69812,-0.665527,0.437134,-0.892273,-0.112793,-0.621674,-0.230438,0.748566,0.232422,0.900574,-0.367249,0.22229,-0.796143,0.562744,-0.665497,-0.73764,
179 0.11377,0.670135,0.704803,0.232605,0.895599,0.429749,-0.114655,-0.11557,-0.474243,0.872742,0.621826,0.604004,-0.498444,-0.832214,0.012756,0.55426,-0.702484,0.705994,-0.089661,-0.692017,0.649292,0.315399,-0.175995,-0.977997,0.111877,0.096954,-0.04953,0.994019,0.635284,-0.606689,-0.477783,-0.261261,
180 -0.607422,-0.750153,0.983276,0.165436,0.075958,-0.29837,0.404083,-0.864655,-0.638672,0.507721,0.578156,0.388214,0.412079,0.824249,0.556183,-0.208832,0.804352,0.778442,0.562012,0.27951,-0.616577,0.781921,-0.091522,0.196289,0.051056,0.979187,-0.121216,0.207153,-0.970734,-0.173401,-0.384735,0.906555,
181 0.161499,-0.723236,-0.671387,0.178497,-0.006226,-0.983887,-0.126038,0.15799,0.97934,0.830475,-0.024811,0.556458,-0.510132,-0.76944,0.384247,0.81424,0.200104,-0.544891,-0.112549,-0.393311,-0.912445,0.56189,0.152222,-0.813049,0.198914,-0.254517,-0.946381,-0.41217,0.690979,-0.593811,-0.407257,0.324524,
182 0.853668,-0.690186,0.366119,-0.624115,-0.428345,0.844147,-0.322296,-0.21228,-0.297546,-0.930756,-0.273071,0.516113,0.811798,0.928314,0.371643,0.007233,0.785828,-0.479218,-0.390778,-0.704895,0.058929,0.706818,0.173248,0.203583,0.963562,0.422211,-0.904297,-0.062469,-0.363312,-0.182465,0.913605,0.254028,
183 -0.552307,-0.793945,-0.28891,-0.765747,-0.574554,0.058319,0.291382,0.954803,0.946136,-0.303925,0.111267,-0.078156,0.443695,-0.892731,0.182098,0.89389,0.409515,-0.680298,-0.213318,0.701141,0.062469,0.848389,-0.525635,-0.72879,-0.641846,0.238342,-0.88089,0.427673,0.202637,-0.532501,-0.21405,0.818878,
184 0.948975,-0.305084,0.07962,0.925446,0.374664,0.055817,0.820923,0.565491,0.079102,0.25882,0.099792,-0.960724,-0.294617,0.910522,0.289978,0.137115,0.320038,-0.937408,-0.908386,0.345276,-0.235718,-0.936218,0.138763,0.322754,0.366577,0.925934,-0.090637,0.309296,-0.686829,-0.657684,0.66983,0.024445,
185 0.742065,-0.917999,-0.059113,-0.392059,0.365509,0.462158,-0.807922,0.083374,0.996399,-0.014801,0.593842,0.253143,-0.763672,0.974976,-0.165466,0.148285,0.918976,0.137299,0.369537,0.294952,0.694977,0.655731,0.943085,0.152618,-0.295319,0.58783,-0.598236,0.544495,0.203796,0.678223,0.705994,-0.478821,
186 -0.661011,0.577667,0.719055,-0.1698,-0.673828,-0.132172,-0.965332,0.225006,-0.981873,-0.14502,0.121979,0.763458,0.579742,0.284546,-0.893188,0.079681,0.442474,-0.795776,-0.523804,0.303802,0.734955,0.67804,-0.007446,0.15506,0.986267,-0.056183,0.258026,0.571503,-0.778931,-0.681549,-0.702087,-0.206116,
187 -0.96286,-0.177185,0.203613,-0.470978,-0.515106,0.716095,-0.740326,0.57135,0.354095,-0.56012,-0.824982,-0.074982,-0.507874,0.753204,0.417969,-0.503113,0.038147,0.863342,0.594025,0.673553,-0.439758,-0.119873,-0.005524,-0.992737,0.098267,-0.213776,0.971893,-0.615631,0.643951,0.454163,0.896851,-0.441071,
188 0.032166,-0.555023,0.750763,-0.358093,0.398773,0.304688,0.864929,-0.722961,0.303589,0.620544,-0.63559,-0.621948,-0.457306,-0.293243,0.072327,0.953278,-0.491638,0.661041,-0.566772,-0.304199,-0.572083,-0.761688,0.908081,-0.398956,0.127014,-0.523621,-0.549683,-0.650848,-0.932922,-0.19986,0.299408,0.099426,
189 0.140869,0.984985,-0.020325,-0.999756,-0.002319,0.952667,0.280853,-0.11615,-0.971893,0.082581,0.220337,0.65921,0.705292,-0.260651,0.733063,-0.175537,0.657043,-0.555206,0.429504,-0.712189,0.400421,-0.89859,0.179352,0.750885,-0.19696,0.630341,0.785675,-0.569336,0.241821,-0.058899,-0.464111,0.883789,
190 0.129608,-0.94519,0.299622,-0.357819,0.907654,0.219238,-0.842133,-0.439117,-0.312927,-0.313477,0.84433,0.434479,-0.241211,0.053253,0.968994,0.063873,0.823273,0.563965,0.476288,0.862152,-0.172516,0.620941,-0.298126,0.724915,0.25238,-0.749359,-0.612122,-0.577545,0.386566,0.718994,-0.406342,-0.737976,
191 0.538696,0.04718,0.556305,0.82959,-0.802856,0.587463,0.101166,-0.707733,-0.705963,0.026428,0.374908,0.68457,0.625092,0.472137,0.208405,-0.856506,-0.703064,-0.581085,-0.409821,-0.417206,-0.736328,0.532623,-0.447876,-0.20285,-0.870728,0.086945,-0.990417,0.107086,0.183685,0.018341,-0.982788,0.560638,
192 -0.428864,0.708282,0.296722,-0.952576,-0.0672,0.135773,0.990265,0.030243,-0.068787,0.654724,0.752686,0.762604,-0.551758,0.337585,-0.819611,-0.407684,0.402466,-0.727844,-0.55072,-0.408539,-0.855774,-0.480011,0.19281,0.693176,-0.079285,0.716339,0.226013,0.650116,-0.725433,0.246704,0.953369,-0.173553,
193 -0.970398,-0.239227,-0.03244,0.136383,-0.394318,0.908752,0.813232,0.558167,0.164368,0.40451,0.549042,-0.731323,-0.380249,-0.566711,0.730865,0.022156,0.932739,0.359741,0.00824,0.996552,-0.082306,0.956635,-0.065338,-0.283722,-0.743561,0.008209,0.668579,-0.859589,-0.509674,0.035767,-0.852234,0.363678,
194 -0.375977,-0.201965,-0.970795,-0.12915,0.313477,0.947327,0.06546,-0.254028,-0.528259,0.81015,0.628052,0.601105,0.49411,-0.494385,0.868378,0.037933,0.275635,-0.086426,0.957336,-0.197937,0.468903,-0.860748,0.895599,0.399384,0.195801,0.560791,0.825012,-0.069214,0.304199,-0.849487,0.43103,0.096375,
195 0.93576,0.339111,-0.051422,0.408966,-0.911072,0.330444,0.942841,-0.042389,-0.452362,-0.786407,0.420563,0.134308,-0.933472,-0.332489,0.80191,-0.566711,-0.188934,-0.987946,-0.105988,0.112518,-0.24408,0.892242,-0.379791,-0.920502,0.229095,-0.316376,0.7789,0.325958,0.535706,-0.912872,0.185211,-0.36377,
196 -0.184784,0.565369,-0.803833,-0.018463,0.119537,0.992615,-0.259247,-0.935608,0.239532,-0.82373,-0.449127,-0.345947,-0.433105,0.659515,0.614349,-0.822754,0.378845,-0.423676,0.687195,-0.674835,-0.26889,-0.246582,-0.800842,0.545715,-0.729187,-0.207794,0.651978,0.653534,-0.610443,-0.447388,0.492584,-0.023346,
197 0.869934,0.609039,0.009094,-0.79306,0.962494,-0.271088,-0.00885,0.2659,-0.004913,0.963959,0.651245,0.553619,-0.518951,0.280548,-0.84314,0.458618,-0.175293,-0.983215,0.049805,0.035339,-0.979919,0.196045,-0.982941,0.164307,-0.082245,0.233734,-0.97226,-0.005005,-0.747253,-0.611328,0.260437,0.645599,
198 0.592773,0.481384,0.117706,-0.949524,-0.29068,-0.535004,-0.791901,-0.294312,-0.627167,-0.214447,0.748718,-0.047974,-0.813477,-0.57959,-0.175537,0.477264,-0.860992,0.738556,-0.414246,-0.53183,0.562561,-0.704071,0.433289,-0.754944,0.64801,-0.100586,0.114716,0.044525,-0.992371,0.966003,0.244873,-0.082764,
199 };
200
201 /**************************/
202 /*  IMPROVED PERLIN NOISE */
203 /**************************/
204
205 #define lerp(t, a, b) ((a)+(t)*((b)-(a)))
206 #define npfade(t) ((t)*(t)*(t)*((t)*((t)*6-15)+10))
207
208 static float grad(int hash, float x, float y, float z)
209 {
210         int h = hash & 15;                     // CONVERT LO 4 BITS OF HASH CODE
211         float u = h<8 ? x : y,                 // INTO 12 GRADIENT DIRECTIONS.
212                                 v = h<4 ? y : h==12||h==14 ? x : z;
213         return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
214 }
215
216 /* instead of adding another permutation array, just use hash table defined above */
217 static float newPerlin(float x, float y, float z)
218 {
219         int A, AA, AB, B, BA, BB;
220         float u=floor(x), v=floor(y), w=floor(z);
221         int X=((int)u) & 255, Y=((int)v) & 255, Z=((int)w) & 255;       // FIND UNIT CUBE THAT CONTAINS POINT
222         x -= u;             // FIND RELATIVE X,Y,Z
223         y -= v;             // OF POINT IN CUBE.
224         z -= w;
225         u = npfade(x);      // COMPUTE FADE CURVES
226         v = npfade(y);      // FOR EACH OF X,Y,Z.
227         w = npfade(z);
228         A = hash[X  ]+Y;  AA = hash[A]+Z;  AB = hash[A+1]+Z;      // HASH COORDINATES OF
229         B = hash[X+1]+Y;  BA = hash[B]+Z;  BB = hash[B+1]+Z;      // THE 8 CUBE CORNERS,
230         return lerp(w, lerp(v, lerp(u, grad(hash[AA  ], x  , y  , z   ),  // AND ADD
231                                                                                                                                 grad(hash[BA  ], x-1, y  , z   )), // BLENDED
232                                                                                                 lerp(u, grad(hash[AB  ], x  , y-1, z   ),  // RESULTS
233                                                                                                                                 grad(hash[BB  ], x-1, y-1, z   ))),// FROM  8
234                                                                 lerp(v, lerp(u, grad(hash[AA+1], x  , y  , z-1 ),  // CORNERS
235                                                                                                                                 grad(hash[BA+1], x-1, y  , z-1 )), // OF CUBE
236                                                                                                 lerp(u, grad(hash[AB+1], x  , y-1, z-1 ),
237                                                                                                                                 grad(hash[BB+1], x-1, y-1, z-1 ))));
238 }
239
240 /* for use with BLI_gNoise()/BLI_gTurbulence(), returns unsigned improved perlin noise */
241 static float newPerlinU(float x, float y, float z)
242 {
243         return (0.5+0.5*newPerlin(x, y, z));
244 }
245
246
247 /**************************/
248 /* END OF IMPROVED PERLIN */
249 /**************************/
250
251 /* Was BLI_hnoise(), removed noisesize, so other functions can call it without scaling. */
252 static float orgBlenderNoise(float x, float y, float z)
253 {
254         register float cn1, cn2, cn3, cn4, cn5, cn6, i, *h;
255         float ox, oy, oz, jx, jy, jz;
256         float n= 0.5;
257         int ix, iy, iz, b00, b01, b10, b11, b20, b21;
258
259         ox= (x- (ix= (int)floor(x)) );
260         oy= (y- (iy= (int)floor(y)) );
261         oz= (z- (iz= (int)floor(z)) );
262
263         jx= ox-1;
264         jy= oy-1;
265         jz= oz-1;
266
267         cn1=ox*ox; cn2=oy*oy; cn3=oz*oz;
268         cn4=jx*jx; cn5=jy*jy; cn6=jz*jz;
269
270         cn1= 1.0-3.0*cn1+2.0*cn1*ox;
271         cn2= 1.0-3.0*cn2+2.0*cn2*oy;
272         cn3= 1.0-3.0*cn3+2.0*cn3*oz;
273         cn4= 1.0-3.0*cn4-2.0*cn4*jx;
274         cn5= 1.0-3.0*cn5-2.0*cn5*jy;
275         cn6= 1.0-3.0*cn6-2.0*cn6*jz;
276
277         b00= hash[ hash[ix & 255]+(iy & 255)];
278         b10= hash[ hash[(ix+1) & 255]+(iy & 255)];
279         b01= hash[ hash[ix & 255]+((iy+1) & 255)];
280         b11= hash[ hash[(ix+1) & 255]+((iy+1) & 255)];
281
282         b20=iz & 255; b21= (iz+1) & 255;
283
284                 /* 0 */
285         i= (cn1*cn2*cn3);
286                 h=hashvectf+ 3*hash[b20+b00];
287                 n+= i*(h[0]*ox+h[1]*oy+h[2]*oz);
288                 /* 1 */
289         i= (cn1*cn2*cn6);
290                 h=hashvectf+ 3*hash[b21+b00];
291                 n+= i*(h[0]*ox+h[1]*oy+h[2]*jz);
292                 /* 2 */
293         i= (cn1*cn5*cn3);
294                 h=hashvectf+ 3*hash[b20+b01];
295                 n+= i*(h[0]*ox+h[1]*jy+h[2]*oz);
296                 /* 3 */
297         i= (cn1*cn5*cn6);
298                 h=hashvectf+ 3*hash[b21+b01];
299                 n+= i*(h[0]*ox+h[1]*jy+h[2]*jz);
300                 /* 4 */
301         i= cn4*cn2*cn3;
302                 h=hashvectf+ 3*hash[b20+b10];
303                 n+= i*(h[0]*jx+h[1]*oy+h[2]*oz);
304                 /* 5 */
305         i= cn4*cn2*cn6;
306                 h=hashvectf+ 3*hash[b21+b10];
307                 n+= i*(h[0]*jx+h[1]*oy+h[2]*jz);
308                 /* 6 */
309         i= cn4*cn5*cn3;
310                 h=hashvectf+ 3*hash[b20+b11];
311                 n+=  i*(h[0]*jx+h[1]*jy+h[2]*oz);
312                 /* 7 */
313         i= (cn4*cn5*cn6);
314                 h=hashvectf+ 3*hash[b21+b11];
315                 n+= i*(h[0]*jx+h[1]*jy+h[2]*jz);
316
317         if(n<0.0) n=0.0; else if(n>1.0) n=1.0;
318         return n;
319 }
320
321 /* as orgBlenderNoise(), returning signed noise */
322 static float orgBlenderNoiseS(float x, float y, float z)
323 {
324         return (2.0*orgBlenderNoise(x, y, z)-1.0);
325 }
326
327 /* separated from orgBlenderNoise above, with scaling */
328 float BLI_hnoise(float noisesize, float x, float y, float z)
329 {
330         if(noisesize==0.0) return 0.0;
331         x= (1.0+x)/noisesize;
332         y= (1.0+y)/noisesize;
333         z= (1.0+z)/noisesize;
334         return orgBlenderNoise(x, y, z);
335 }
336
337
338 /* original turbulence functions */
339 float BLI_turbulence(float noisesize, float x, float y, float z, int nr)
340 {
341         float s, d= 0.5, div=1.0;
342
343         s= BLI_hnoise(noisesize, x, y, z);
344         
345         while(nr>0) {
346         
347                 s+= d*BLI_hnoise(noisesize*d, x, y, z);
348                 div+= d;
349                 d*= 0.5;
350
351                 nr--;
352         }
353         return s/div;
354 }
355
356 float BLI_turbulence1(float noisesize, float x, float y, float z, int nr)
357 {
358         float s, d= 0.5, div=1.0;
359
360         s= fabs( (-1.0+2.0*BLI_hnoise(noisesize, x, y, z)));
361         
362         while(nr>0) {
363         
364                 s+= fabs(d* (-1.0+2.0*BLI_hnoise(noisesize*d, x, y, z)));
365                 div+= d;
366                 d*= 0.5;
367                 
368                 nr--;
369         }
370         return s/div;
371 }
372
373 /* ********************* FROM PERLIN HIMSELF: ******************** */
374
375 static char p[512+2]= {
376 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
377 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
378 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
379 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
380 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
381 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
382 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
383 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
384 0xA2,0xA0};
385
386
387 float g[512+2][3]= {
388 0.33783,0.715698,-0.611206,-0.944031,-0.326599,-0.045624,-0.101074,-0.416443,-0.903503,0.799286,0.49411,-0.341949,-0.854645,0.518036,0.033936,0.42514,-0.437866,-0.792114,-0.358948,0.597046,0.717377,-0.985413,0.144714,0.089294,-0.601776,-0.33728,-0.723907,-0.449921,0.594513,0.666382,0.208313,-0.10791,
389 0.972076,0.575317,0.060425,0.815643,0.293365,-0.875702,-0.383453,0.293762,0.465759,0.834686,-0.846008,-0.233398,-0.47934,-0.115814,0.143036,-0.98291,0.204681,-0.949036,-0.239532,0.946716,-0.263947,0.184326,-0.235596,0.573822,0.784332,0.203705,-0.372253,-0.905487,0.756989,-0.651031,0.055298,0.497803,
390 0.814697,-0.297363,-0.16214,0.063995,-0.98468,-0.329254,0.834381,0.441925,0.703827,-0.527039,-0.476227,0.956421,0.266113,0.119781,0.480133,0.482849,0.7323,-0.18631,0.961212,-0.203125,-0.748474,-0.656921,-0.090393,-0.085052,-0.165253,0.982544,-0.76947,0.628174,-0.115234,0.383148,0.537659,0.751068,
391 0.616486,-0.668488,-0.415924,-0.259979,-0.630005,0.73175,0.570953,-0.087952,0.816223,-0.458008,0.023254,0.888611,-0.196167,0.976563,-0.088287,-0.263885,-0.69812,-0.665527,0.437134,-0.892273,-0.112793,-0.621674,-0.230438,0.748566,0.232422,0.900574,-0.367249,0.22229,-0.796143,0.562744,-0.665497,-0.73764,
392 0.11377,0.670135,0.704803,0.232605,0.895599,0.429749,-0.114655,-0.11557,-0.474243,0.872742,0.621826,0.604004,-0.498444,-0.832214,0.012756,0.55426,-0.702484,0.705994,-0.089661,-0.692017,0.649292,0.315399,-0.175995,-0.977997,0.111877,0.096954,-0.04953,0.994019,0.635284,-0.606689,-0.477783,-0.261261,
393 -0.607422,-0.750153,0.983276,0.165436,0.075958,-0.29837,0.404083,-0.864655,-0.638672,0.507721,0.578156,0.388214,0.412079,0.824249,0.556183,-0.208832,0.804352,0.778442,0.562012,0.27951,-0.616577,0.781921,-0.091522,0.196289,0.051056,0.979187,-0.121216,0.207153,-0.970734,-0.173401,-0.384735,0.906555,
394 0.161499,-0.723236,-0.671387,0.178497,-0.006226,-0.983887,-0.126038,0.15799,0.97934,0.830475,-0.024811,0.556458,-0.510132,-0.76944,0.384247,0.81424,0.200104,-0.544891,-0.112549,-0.393311,-0.912445,0.56189,0.152222,-0.813049,0.198914,-0.254517,-0.946381,-0.41217,0.690979,-0.593811,-0.407257,0.324524,
395 0.853668,-0.690186,0.366119,-0.624115,-0.428345,0.844147,-0.322296,-0.21228,-0.297546,-0.930756,-0.273071,0.516113,0.811798,0.928314,0.371643,0.007233,0.785828,-0.479218,-0.390778,-0.704895,0.058929,0.706818,0.173248,0.203583,0.963562,0.422211,-0.904297,-0.062469,-0.363312,-0.182465,0.913605,0.254028,
396 -0.552307,-0.793945,-0.28891,-0.765747,-0.574554,0.058319,0.291382,0.954803,0.946136,-0.303925,0.111267,-0.078156,0.443695,-0.892731,0.182098,0.89389,0.409515,-0.680298,-0.213318,0.701141,0.062469,0.848389,-0.525635,-0.72879,-0.641846,0.238342,-0.88089,0.427673,0.202637,-0.532501,-0.21405,0.818878,
397 0.948975,-0.305084,0.07962,0.925446,0.374664,0.055817,0.820923,0.565491,0.079102,0.25882,0.099792,-0.960724,-0.294617,0.910522,0.289978,0.137115,0.320038,-0.937408,-0.908386,0.345276,-0.235718,-0.936218,0.138763,0.322754,0.366577,0.925934,-0.090637,0.309296,-0.686829,-0.657684,0.66983,0.024445,
398 0.742065,-0.917999,-0.059113,-0.392059,0.365509,0.462158,-0.807922,0.083374,0.996399,-0.014801,0.593842,0.253143,-0.763672,0.974976,-0.165466,0.148285,0.918976,0.137299,0.369537,0.294952,0.694977,0.655731,0.943085,0.152618,-0.295319,0.58783,-0.598236,0.544495,0.203796,0.678223,0.705994,-0.478821,
399 -0.661011,0.577667,0.719055,-0.1698,-0.673828,-0.132172,-0.965332,0.225006,-0.981873,-0.14502,0.121979,0.763458,0.579742,0.284546,-0.893188,0.079681,0.442474,-0.795776,-0.523804,0.303802,0.734955,0.67804,-0.007446,0.15506,0.986267,-0.056183,0.258026,0.571503,-0.778931,-0.681549,-0.702087,-0.206116,
400 -0.96286,-0.177185,0.203613,-0.470978,-0.515106,0.716095,-0.740326,0.57135,0.354095,-0.56012,-0.824982,-0.074982,-0.507874,0.753204,0.417969,-0.503113,0.038147,0.863342,0.594025,0.673553,-0.439758,-0.119873,-0.005524,-0.992737,0.098267,-0.213776,0.971893,-0.615631,0.643951,0.454163,0.896851,-0.441071,
401 0.032166,-0.555023,0.750763,-0.358093,0.398773,0.304688,0.864929,-0.722961,0.303589,0.620544,-0.63559,-0.621948,-0.457306,-0.293243,0.072327,0.953278,-0.491638,0.661041,-0.566772,-0.304199,-0.572083,-0.761688,0.908081,-0.398956,0.127014,-0.523621,-0.549683,-0.650848,-0.932922,-0.19986,0.299408,0.099426,
402 0.140869,0.984985,-0.020325,-0.999756,-0.002319,0.952667,0.280853,-0.11615,-0.971893,0.082581,0.220337,0.65921,0.705292,-0.260651,0.733063,-0.175537,0.657043,-0.555206,0.429504,-0.712189,0.400421,-0.89859,0.179352,0.750885,-0.19696,0.630341,0.785675,-0.569336,0.241821,-0.058899,-0.464111,0.883789,
403 0.129608,-0.94519,0.299622,-0.357819,0.907654,0.219238,-0.842133,-0.439117,-0.312927,-0.313477,0.84433,0.434479,-0.241211,0.053253,0.968994,0.063873,0.823273,0.563965,0.476288,0.862152,-0.172516,0.620941,-0.298126,0.724915,0.25238,-0.749359,-0.612122,-0.577545,0.386566,0.718994,-0.406342,-0.737976,
404 0.538696,0.04718,0.556305,0.82959,-0.802856,0.587463,0.101166,-0.707733,-0.705963,0.026428,0.374908,0.68457,0.625092,0.472137,0.208405,-0.856506,-0.703064,-0.581085,-0.409821,-0.417206,-0.736328,0.532623,-0.447876,-0.20285,-0.870728,0.086945,-0.990417,0.107086,0.183685,0.018341,-0.982788,0.560638,
405 -0.428864,0.708282,0.296722,-0.952576,-0.0672,0.135773,0.990265,0.030243,-0.068787,0.654724,0.752686,0.762604,-0.551758,0.337585,-0.819611,-0.407684,0.402466,-0.727844,-0.55072,-0.408539,-0.855774,-0.480011,0.19281,0.693176,-0.079285,0.716339,0.226013,0.650116,-0.725433,0.246704,0.953369,-0.173553,
406 -0.970398,-0.239227,-0.03244,0.136383,-0.394318,0.908752,0.813232,0.558167,0.164368,0.40451,0.549042,-0.731323,-0.380249,-0.566711,0.730865,0.022156,0.932739,0.359741,0.00824,0.996552,-0.082306,0.956635,-0.065338,-0.283722,-0.743561,0.008209,0.668579,-0.859589,-0.509674,0.035767,-0.852234,0.363678,
407 -0.375977,-0.201965,-0.970795,-0.12915,0.313477,0.947327,0.06546,-0.254028,-0.528259,0.81015,0.628052,0.601105,0.49411,-0.494385,0.868378,0.037933,0.275635,-0.086426,0.957336,-0.197937,0.468903,-0.860748,0.895599,0.399384,0.195801,0.560791,0.825012,-0.069214,0.304199,-0.849487,0.43103,0.096375,
408 0.93576,0.339111,-0.051422,0.408966,-0.911072,0.330444,0.942841,-0.042389,-0.452362,-0.786407,0.420563,0.134308,-0.933472,-0.332489,0.80191,-0.566711,-0.188934,-0.987946,-0.105988,0.112518,-0.24408,0.892242,-0.379791,-0.920502,0.229095,-0.316376,0.7789,0.325958,0.535706,-0.912872,0.185211,-0.36377,
409 -0.184784,0.565369,-0.803833,-0.018463,0.119537,0.992615,-0.259247,-0.935608,0.239532,-0.82373,-0.449127,-0.345947,-0.433105,0.659515,0.614349,-0.822754,0.378845,-0.423676,0.687195,-0.674835,-0.26889,-0.246582,-0.800842,0.545715,-0.729187,-0.207794,0.651978,0.653534,-0.610443,-0.447388,0.492584,-0.023346,
410 0.869934,0.609039,0.009094,-0.79306,0.962494,-0.271088,-0.00885,0.2659,-0.004913,0.963959,0.651245,0.553619,-0.518951,0.280548,-0.84314,0.458618,-0.175293,-0.983215,0.049805,0.035339,-0.979919,0.196045,-0.982941,0.164307,-0.082245,0.233734,-0.97226,-0.005005,-0.747253,-0.611328,0.260437,0.645599,
411 0.592773,0.481384,0.117706,-0.949524,-0.29068,-0.535004,-0.791901,-0.294312,-0.627167,-0.214447,0.748718,-0.047974,-0.813477,-0.57959,-0.175537,0.477264,-0.860992,0.738556,-0.414246,-0.53183,0.562561,-0.704071,0.433289,-0.754944,0.64801,-0.100586,0.114716,0.044525,-0.992371,0.966003,0.244873,-0.082764,
412 0.33783,0.715698,-0.611206,-0.944031,-0.326599,-0.045624,-0.101074,-0.416443,-0.903503,0.799286,0.49411,-0.341949,-0.854645,0.518036,0.033936,0.42514,-0.437866,-0.792114,-0.358948,0.597046,0.717377,-0.985413,0.144714,0.089294,-0.601776,-0.33728,-0.723907,-0.449921,0.594513,0.666382,0.208313,-0.10791,
413 0.972076,0.575317,0.060425,0.815643,0.293365,-0.875702,-0.383453,0.293762,0.465759,0.834686,-0.846008,-0.233398,-0.47934,-0.115814,0.143036,-0.98291,0.204681,-0.949036,-0.239532,0.946716,-0.263947,0.184326,-0.235596,0.573822,0.784332,0.203705,-0.372253,-0.905487,0.756989,-0.651031,0.055298,0.497803,
414 0.814697,-0.297363,-0.16214,0.063995,-0.98468,-0.329254,0.834381,0.441925,0.703827,-0.527039,-0.476227,0.956421,0.266113,0.119781,0.480133,0.482849,0.7323,-0.18631,0.961212,-0.203125,-0.748474,-0.656921,-0.090393,-0.085052,-0.165253,0.982544,-0.76947,0.628174,-0.115234,0.383148,0.537659,0.751068,
415 0.616486,-0.668488,-0.415924,-0.259979,-0.630005,0.73175,0.570953,-0.087952,0.816223,-0.458008,0.023254,0.888611,-0.196167,0.976563,-0.088287,-0.263885,-0.69812,-0.665527,0.437134,-0.892273,-0.112793,-0.621674,-0.230438,0.748566,0.232422,0.900574,-0.367249,0.22229,-0.796143,0.562744,-0.665497,-0.73764,
416 0.11377,0.670135,0.704803,0.232605,0.895599,0.429749,-0.114655,-0.11557,-0.474243,0.872742,0.621826,0.604004,-0.498444,-0.832214,0.012756,0.55426,-0.702484,0.705994,-0.089661,-0.692017,0.649292,0.315399,-0.175995,-0.977997,0.111877,0.096954,-0.04953,0.994019,0.635284,-0.606689,-0.477783,-0.261261,
417 -0.607422,-0.750153,0.983276,0.165436,0.075958,-0.29837,0.404083,-0.864655,-0.638672,0.507721,0.578156,0.388214,0.412079,0.824249,0.556183,-0.208832,0.804352,0.778442,0.562012,0.27951,-0.616577,0.781921,-0.091522,0.196289,0.051056,0.979187,-0.121216,0.207153,-0.970734,-0.173401,-0.384735,0.906555,
418 0.161499,-0.723236,-0.671387,0.178497,-0.006226,-0.983887,-0.126038,0.15799,0.97934,0.830475,-0.024811,0.556458,-0.510132,-0.76944,0.384247,0.81424,0.200104,-0.544891,-0.112549,-0.393311,-0.912445,0.56189,0.152222,-0.813049,0.198914,-0.254517,-0.946381,-0.41217,0.690979,-0.593811,-0.407257,0.324524,
419 0.853668,-0.690186,0.366119,-0.624115,-0.428345,0.844147,-0.322296,-0.21228,-0.297546,-0.930756,-0.273071,0.516113,0.811798,0.928314,0.371643,0.007233,0.785828,-0.479218,-0.390778,-0.704895,0.058929,0.706818,0.173248,0.203583,0.963562,0.422211,-0.904297,-0.062469,-0.363312,-0.182465,0.913605,0.254028,
420 -0.552307,-0.793945,-0.28891,-0.765747,-0.574554,0.058319,0.291382,0.954803,0.946136,-0.303925,0.111267,-0.078156,0.443695,-0.892731,0.182098,0.89389,0.409515,-0.680298,-0.213318,0.701141,0.062469,0.848389,-0.525635,-0.72879,-0.641846,0.238342,-0.88089,0.427673,0.202637,-0.532501,-0.21405,0.818878,
421 0.948975,-0.305084,0.07962,0.925446,0.374664,0.055817,0.820923,0.565491,0.079102,0.25882,0.099792,-0.960724,-0.294617,0.910522,0.289978,0.137115,0.320038,-0.937408,-0.908386,0.345276,-0.235718,-0.936218,0.138763,0.322754,0.366577,0.925934,-0.090637,0.309296,-0.686829,-0.657684,0.66983,0.024445,
422 0.742065,-0.917999,-0.059113,-0.392059,0.365509,0.462158,-0.807922,0.083374,0.996399,-0.014801,0.593842,0.253143,-0.763672,0.974976,-0.165466,0.148285,0.918976,0.137299,0.369537,0.294952,0.694977,0.655731,0.943085,0.152618,-0.295319,0.58783,-0.598236,0.544495,0.203796,0.678223,0.705994,-0.478821,
423 -0.661011,0.577667,0.719055,-0.1698,-0.673828,-0.132172,-0.965332,0.225006,-0.981873,-0.14502,0.121979,0.763458,0.579742,0.284546,-0.893188,0.079681,0.442474,-0.795776,-0.523804,0.303802,0.734955,0.67804,-0.007446,0.15506,0.986267,-0.056183,0.258026,0.571503,-0.778931,-0.681549,-0.702087,-0.206116,
424 -0.96286,-0.177185,0.203613,-0.470978,-0.515106,0.716095,-0.740326,0.57135,0.354095,-0.56012,-0.824982,-0.074982,-0.507874,0.753204,0.417969,-0.503113,0.038147,0.863342,0.594025,0.673553,-0.439758,-0.119873,-0.005524,-0.992737,0.098267,-0.213776,0.971893,-0.615631,0.643951,0.454163,0.896851,-0.441071,
425 0.032166,-0.555023,0.750763,-0.358093,0.398773,0.304688,0.864929,-0.722961,0.303589,0.620544,-0.63559,-0.621948,-0.457306,-0.293243,0.072327,0.953278,-0.491638,0.661041,-0.566772,-0.304199,-0.572083,-0.761688,0.908081,-0.398956,0.127014,-0.523621,-0.549683,-0.650848,-0.932922,-0.19986,0.299408,0.099426,
426 0.140869,0.984985,-0.020325,-0.999756,-0.002319,0.952667,0.280853,-0.11615,-0.971893,0.082581,0.220337,0.65921,0.705292,-0.260651,0.733063,-0.175537,0.657043,-0.555206,0.429504,-0.712189,0.400421,-0.89859,0.179352,0.750885,-0.19696,0.630341,0.785675,-0.569336,0.241821,-0.058899,-0.464111,0.883789,
427 0.129608,-0.94519,0.299622,-0.357819,0.907654,0.219238,-0.842133,-0.439117,-0.312927,-0.313477,0.84433,0.434479,-0.241211,0.053253,0.968994,0.063873,0.823273,0.563965,0.476288,0.862152,-0.172516,0.620941,-0.298126,0.724915,0.25238,-0.749359,-0.612122,-0.577545,0.386566,0.718994,-0.406342,-0.737976,
428 0.538696,0.04718,0.556305,0.82959,-0.802856,0.587463,0.101166,-0.707733,-0.705963,0.026428,0.374908,0.68457,0.625092,0.472137,0.208405,-0.856506,-0.703064,-0.581085,-0.409821,-0.417206,-0.736328,0.532623,-0.447876,-0.20285,-0.870728,0.086945,-0.990417,0.107086,0.183685,0.018341,-0.982788,0.560638,
429 -0.428864,0.708282,0.296722,-0.952576,-0.0672,0.135773,0.990265,0.030243,-0.068787,0.654724,0.752686,0.762604,-0.551758,0.337585,-0.819611,-0.407684,0.402466,-0.727844,-0.55072,-0.408539,-0.855774,-0.480011,0.19281,0.693176,-0.079285,0.716339,0.226013,0.650116,-0.725433,0.246704,0.953369,-0.173553,
430 -0.970398,-0.239227,-0.03244,0.136383,-0.394318,0.908752,0.813232,0.558167,0.164368,0.40451,0.549042,-0.731323,-0.380249,-0.566711,0.730865,0.022156,0.932739,0.359741,0.00824,0.996552,-0.082306,0.956635,-0.065338,-0.283722,-0.743561,0.008209,0.668579,-0.859589,-0.509674,0.035767,-0.852234,0.363678,
431 -0.375977,-0.201965,-0.970795,-0.12915,0.313477,0.947327,0.06546,-0.254028,-0.528259,0.81015,0.628052,0.601105,0.49411,-0.494385,0.868378,0.037933,0.275635,-0.086426,0.957336,-0.197937,0.468903,-0.860748,0.895599,0.399384,0.195801,0.560791,0.825012,-0.069214,0.304199,-0.849487,0.43103,0.096375,
432 0.93576,0.339111,-0.051422,0.408966,-0.911072,0.330444,0.942841,-0.042389,-0.452362,-0.786407,0.420563,0.134308,-0.933472,-0.332489,0.80191,-0.566711,-0.188934,-0.987946,-0.105988,0.112518,-0.24408,0.892242,-0.379791,-0.920502,0.229095,-0.316376,0.7789,0.325958,0.535706,-0.912872,0.185211,-0.36377,
433 -0.184784,0.565369,-0.803833,-0.018463,0.119537,0.992615,-0.259247,-0.935608,0.239532,-0.82373,-0.449127,-0.345947,-0.433105,0.659515,0.614349,-0.822754,0.378845,-0.423676,0.687195,-0.674835,-0.26889,-0.246582,-0.800842,0.545715,-0.729187,-0.207794,0.651978,0.653534,-0.610443,-0.447388,0.492584,-0.023346,
434 0.869934,0.609039,0.009094,-0.79306,0.962494,-0.271088,-0.00885,0.2659,-0.004913,0.963959,0.651245,0.553619,-0.518951,0.280548,-0.84314,0.458618,-0.175293,-0.983215,0.049805,0.035339,-0.979919,0.196045,-0.982941,0.164307,-0.082245,0.233734,-0.97226,-0.005005,-0.747253,-0.611328,0.260437,0.645599,
435 0.592773,0.481384,0.117706,-0.949524,-0.29068,-0.535004,-0.791901,-0.294312,-0.627167,-0.214447,0.748718,-0.047974,-0.813477,-0.57959,-0.175537,0.477264,-0.860992,0.738556,-0.414246,-0.53183,0.562561,-0.704071,0.433289,-0.754944,0.64801,-0.100586,0.114716,0.044525,-0.992371,0.966003,0.244873,-0.082764,
436 0.33783,0.715698,-0.611206,-0.944031,-0.326599,-0.045624};
437
438
439
440 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
441
442 #define setup(i,b0,b1,r0,r1) \
443         t = vec[i] + 10000.; \
444         b0 = ((int)t) & 255; \
445         b1 = (b0+1) & 255; \
446         r0 = t - (int)t; \
447         r1 = r0 - 1.;
448
449
450 float noise3_perlin(float vec[3])
451 {
452         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
453         float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
454         register int i, j;
455
456
457         setup(0, bx0,bx1, rx0,rx1);
458         setup(1, by0,by1, ry0,ry1);
459         setup(2, bz0,bz1, rz0,rz1);
460
461         i = p[ bx0 ];
462         j = p[ bx1 ];
463
464         b00 = p[ i + by0 ];
465         b10 = p[ j + by0 ];
466         b01 = p[ i + by1 ];
467         b11 = p[ j + by1 ];
468
469 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
470
471 #define surve(t) ( t * t * (3. - 2. * t) )
472
473 /* lerp moved to improved perlin above */
474
475         sx = surve(rx0);
476         sy = surve(ry0);
477         sz = surve(rz0);
478
479
480         q = g[ b00 + bz0 ] ;
481         u = at(rx0,ry0,rz0);
482         q = g[ b10 + bz0 ] ;
483         v = at(rx1,ry0,rz0);
484         a = lerp(sx, u, v);
485
486         q = g[ b01 + bz0 ] ;
487         u = at(rx0,ry1,rz0);
488         q = g[ b11 + bz0 ] ;
489         v = at(rx1,ry1,rz0);
490         b = lerp(sx, u, v);
491
492         c = lerp(sy, a, b);          /* interpolate in y at lo x */
493
494         q = g[ b00 + bz1 ] ;
495         u = at(rx0,ry0,rz1);
496         q = g[ b10 + bz1 ] ;
497         v = at(rx1,ry0,rz1);
498         a = lerp(sx, u, v);
499
500         q = g[ b01 + bz1 ] ;
501         u = at(rx0,ry1,rz1);
502         q = g[ b11 + bz1 ] ;
503         v = at(rx1,ry1,rz1);
504         b = lerp(sx, u, v);
505
506         d = lerp(sy, a, b);          /* interpolate in y at hi x */
507
508         return 1.5 * lerp(sz, c, d); /* interpolate in z */
509 }
510
511 float turbulence_perlin(float *point, float lofreq, float hifreq)
512 {
513         float freq, t, p[3];
514
515         p[0] = point[0] + 123.456;
516         p[1] = point[1];
517         p[2] = point[2];
518
519         t = 0;
520         for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
521                 t += fabs(noise3_perlin(p)) / freq;
522                 p[0] *= 2.;
523                 p[1] *= 2.;
524                 p[2] *= 2.;
525         }
526         return t - 0.3; /* readjust to make mean value = 0.0 */
527 }
528
529 /* for use with BLI_gNoise/gTurbulence, returns signed noise */
530 static float orgPerlinNoise(float x, float y, float z)
531 {
532         float v[3];
533
534         v[0] = x;
535         v[1] = y;
536         v[2] = z;
537         return noise3_perlin(v);
538 }
539
540 /* for use with BLI_gNoise/gTurbulence, returns unsigned noise */
541 static float orgPerlinNoiseU(float x, float y, float z)
542 {
543         float v[3];
544
545         v[0] = x;
546         v[1] = y;
547         v[2] = z;
548         return (0.5+0.5*noise3_perlin(v));
549 }
550
551 /* *************** CALL AS: *************** */
552
553 float BLI_hnoisep(float noisesize, float x, float y, float z)
554 {
555         float vec[3];
556
557         vec[0]= x/noisesize;
558         vec[1]= y/noisesize;
559         vec[2]= z/noisesize;
560
561         return noise3_perlin(vec);
562 }
563
564 float turbulencep(float noisesize, float x, float y, float z, int nr)
565 {
566         float vec[3];
567
568         vec[0]= x/noisesize;
569         vec[1]= y/noisesize;
570         vec[2]= z/noisesize;
571         nr++;
572         return turbulence_perlin(vec, 1.0, (float)(1<<nr));
573 }
574
575 /******************/
576 /* VORONOI/WORLEY */
577 /******************/
578
579 /* distance metrics for voronoi, e parameter only used in Minkovsky */
580 /* Camberra omitted, didn't seem useful */
581
582 /* distance squared */
583 static float dist_Squared(float x, float y, float z, float e) { return (x*x + y*y + z*z); }
584 /* real distance */
585 static float dist_Real(float x, float y, float z, float e) { return sqrt(x*x + y*y + z*z); }
586 /* manhattan/taxicab/cityblock distance */
587 static float dist_Manhattan(float x, float y, float z, float e) { return (fabs(x) + fabs(y) + fabs(z)); }
588 /* Chebychev */
589 static float dist_Chebychev(float x, float y, float z, float e)
590 {
591         float t;
592         x = fabs(x);
593         y = fabs(y);
594         z = fabs(z);
595         t = (x>y)?x:y;
596         return ((z>t)?z:t);
597 }
598
599 /* minkovsky preset exponent 0.5 */
600 static float dist_MinkovskyH(float x, float y, float z, float e)
601 {
602         float d = sqrt(fabs(x)) + sqrt(fabs(y)) + sqrt(fabs(z));
603         return (d*d);
604 }
605
606 /* minkovsky preset exponent 4 */
607 static float dist_Minkovsky4(float x, float y, float z, float e)
608 {
609         x *= x;
610         y *= y;
611         z *= z;
612         return sqrt(sqrt(x*x + y*y + z*z));
613 }
614
615 /* Minkovsky, general case, slow, maybe too slow to be useful */
616 static float dist_Minkovsky(float x, float y, float z, float e)
617 {
618  return pow(pow(fabs(x), e) + pow(fabs(y), e) + pow(fabs(z), e), 1.0/e);
619 }
620
621
622 /* Not 'pure' Worley, but the results are virtually the same.
623          Returns distances in da and point coords in pa */
624 void voronoi(float x, float y, float z, float* da, float* pa, float me, int dtype)
625 {
626         int xx, yy, zz, xi, yi, zi;
627         float xd, yd, zd, d, *p;
628
629         float (*distfunc)(float, float, float, float);
630         switch (dtype) {
631                 case 1:
632                         distfunc = dist_Squared;
633                         break;
634                 case 2:
635                         distfunc = dist_Manhattan;
636                         break;
637                 case 3:
638                         distfunc = dist_Chebychev;
639                         break;
640                 case 4:
641                         distfunc = dist_MinkovskyH;
642                         break;
643                 case 5:
644                         distfunc = dist_Minkovsky4;
645                         break;
646                 case 6:
647                         distfunc = dist_Minkovsky;
648                         break;
649                 case 0:
650                 default:
651                         distfunc = dist_Real;
652         }
653
654         xi = (int)(floor(x));
655         yi = (int)(floor(y));
656         zi = (int)(floor(z));
657         da[0] = da[1] = da[2] = da[3] = 1e10f;
658         for (xx=xi-1;xx<=xi+1;xx++) {
659                 for (yy=yi-1;yy<=yi+1;yy++) {
660                         for (zz=zi-1;zz<=zi+1;zz++) {
661                                 p = HASHPNT(xx, yy, zz);
662                                 xd = x - (p[0] + xx);
663                                 yd = y - (p[1] + yy);
664                                 zd = z - (p[2] + zz);
665                                 d = distfunc(xd, yd, zd, me);
666                                 if (d<da[0]) {
667                                         da[3]=da[2];  da[2]=da[1];  da[1]=da[0];  da[0]=d;
668                                         pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
669                                         pa[6]=pa[3];  pa[7]=pa[4];  pa[8]=pa[5];
670                                         pa[3]=pa[0];  pa[4]=pa[1];  pa[5]=pa[2];
671                                         pa[0]=p[0]+xx;  pa[1]=p[1]+yy;  pa[2]=p[2]+zz;
672                                 }
673                                 else if (d<da[1]) {
674                                         da[3]=da[2];  da[2]=da[1];  da[1]=d;
675                                         pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
676                                         pa[6]=pa[3];  pa[7]=pa[4];  pa[8]=pa[5];
677                                         pa[3]=p[0]+xx;  pa[4]=p[1]+yy;  pa[5]=p[2]+zz;
678                                 }
679                                 else if (d<da[2]) {
680                                         da[3]=da[2];  da[2]=d;
681                                         pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
682                                         pa[6]=p[0]+xx;  pa[7]=p[1]+yy;  pa[8]=p[2]+zz;
683                                 }
684                                 else if (d<da[3]) {
685                                         da[3]=d;
686                                         pa[9]=p[0]+xx;  pa[10]=p[1]+yy;  pa[11]=p[2]+zz;
687                                 }
688                         }
689                 }
690         }
691 }
692
693 /* returns different feature points for use in BLI_gNoise() */
694 static float voronoi_F1(float x, float y, float z)
695 {
696         float da[4], pa[12];
697         voronoi(x, y, z, da, pa, 1, 0);
698         return da[0];
699 }
700
701 static float voronoi_F2(float x, float y, float z)
702 {
703         float da[4], pa[12];
704         voronoi(x, y, z, da, pa, 1, 0);
705         return da[1];
706 }
707
708 static float voronoi_F3(float x, float y, float z)
709 {
710         float da[4], pa[12];
711         voronoi(x, y, z, da, pa, 1, 0);
712         return da[2];
713 }
714
715 static float voronoi_F4(float x, float y, float z)
716 {
717         float da[4], pa[12];
718         voronoi(x, y, z, da, pa, 1, 0);
719         return da[3];
720 }
721
722 static float voronoi_F1F2(float x, float y, float z)
723 {
724         float da[4], pa[12];
725         voronoi(x, y, z, da, pa, 1, 0);
726         return (da[1]-da[0]);
727 }
728
729 /* Crackle type pattern, just a scale/clamp of F2-F1 */
730 static float voronoi_Cr(float x, float y, float z)
731 {
732         float t = 10*voronoi_F1F2(x, y, z);
733         if (t>1.f) return 1.f;
734         return t;
735 }
736
737
738 /* Signed version of all 6 of the above, just 2x-1, not really correct though (range is potentially (0, sqrt(6)).
739    Used in the musgrave functions */
740 static float voronoi_F1S(float x, float y, float z)
741 {
742         float da[4], pa[12];
743         voronoi(x, y, z, da, pa, 1, 0);
744         return (2.0*da[0]-1.0);
745 }
746
747 static float voronoi_F2S(float x, float y, float z)
748 {
749         float da[4], pa[12];
750         voronoi(x, y, z, da, pa, 1, 0);
751         return (2.0*da[1]-1.0);
752 }
753
754 static float voronoi_F3S(float x, float y, float z)
755 {
756         float da[4], pa[12];
757         voronoi(x, y, z, da, pa, 1, 0);
758         return (2.0*da[2]-1.0);
759 }
760
761 static float voronoi_F4S(float x, float y, float z)
762 {
763         float da[4], pa[12];
764         voronoi(x, y, z, da, pa, 1, 0);
765         return (2.0*da[3]-1.0);
766 }
767
768 static float voronoi_F1F2S(float x, float y, float z)
769 {
770         float da[4], pa[12];
771         voronoi(x, y, z, da, pa, 1, 0);
772         return (2.0*(da[1]-da[0])-1.0);
773 }
774
775 /* Crackle type pattern, just a scale/clamp of F2-F1 */
776 static float voronoi_CrS(float x, float y, float z)
777 {
778         float t = 10*voronoi_F1F2(x, y, z);
779         if (t>1.f) return 1.f;
780         return (2.0*t-1.0);
781 }
782
783
784 /***************/
785 /* voronoi end */
786 /***************/
787
788 /*************/
789 /* CELLNOISE */
790 /*************/
791
792 /* returns unsigned cellnoise */
793 static float cellNoiseU(float x, float y, float z)
794 {
795   int xi = (int)(floor(x));
796   int yi = (int)(floor(y));
797   int zi = (int)(floor(z));
798   unsigned int n = xi + yi*1301 + zi*314159;
799   n ^= (n<<13);
800   return ((float)(n*(n*n*15731 + 789221) + 1376312589) / 4294967296.0);
801 }
802
803 /* idem, signed */
804 float cellNoise(float x, float y, float z)
805 {
806   return (2.0*cellNoiseU(x, y, z)-1.0);
807 }
808
809 /* returns a vector/point/color in ca, using point hasharray directly */
810 void cellNoiseV(float x, float y, float z, float *ca)
811 {
812         int xi = (int)(floor(x));
813         int yi = (int)(floor(y));
814         int zi = (int)(floor(z));
815         float *p = HASHPNT(xi, yi, zi);
816         ca[0] = p[0];
817         ca[1] = p[1];
818         ca[2] = p[2];
819 }
820
821
822 /*****************/
823 /* end cellnoise */
824 /*****************/
825
826 /* newnoise: generic noise function for use with different noisebases */
827 float BLI_gNoise(float noisesize, float x, float y, float z, int hard, int noisebasis)
828 {
829         float (*noisefunc)(float, float, float);
830
831         switch (noisebasis) {
832                 case 1:
833                         noisefunc = orgPerlinNoiseU;
834                         break;
835                 case 2:
836                         noisefunc = newPerlinU;
837                         break;
838                 case 3:
839                         noisefunc = voronoi_F1;
840                         break;
841                 case 4:
842                         noisefunc = voronoi_F2;
843                         break;
844                 case 5:
845                         noisefunc = voronoi_F3;
846                         break;
847                 case 6:
848                         noisefunc = voronoi_F4;
849                         break;
850                 case 7:
851                         noisefunc = voronoi_F1F2;
852                         break;
853                 case 8:
854                         noisefunc = voronoi_Cr;
855                         break;
856                 case 14:
857                         noisefunc = cellNoiseU;
858                         break;
859                 case 0:
860                 default: {
861                         noisefunc = orgBlenderNoise;
862                         /* add one to make return value same as BLI_hnoise */
863                         x += 1;
864                         y += 1;
865                         z += 1;
866                 }
867         }
868
869         if (noisesize!=0.0) {
870                 noisesize = 1.0/noisesize;
871                 x *= noisesize;
872                 y *= noisesize;
873                 z *= noisesize;
874         }
875         
876         if (hard) return fabs(2.0*noisefunc(x, y, z)-1.0);
877         return noisefunc(x, y, z);
878 }
879
880 /* newnoise: generic turbulence function for use with different noisebasis */
881 float BLI_gTurbulence(float noisesize, float x, float y, float z, int oct, int hard, int noisebasis)
882 {
883         float (*noisefunc)(float, float, float);
884         float sum, t, amp=1, fscale=1;
885         int i;
886         
887         switch (noisebasis) {
888                 case 1:
889                         noisefunc = orgPerlinNoiseU;
890                         break;
891                 case 2:
892                         noisefunc = newPerlinU;
893                         break;
894                 case 3:
895                         noisefunc = voronoi_F1;
896                         break;
897                 case 4:
898                         noisefunc = voronoi_F2;
899                         break;
900                 case 5:
901                         noisefunc = voronoi_F3;
902                         break;
903                 case 6:
904                         noisefunc = voronoi_F4;
905                         break;
906                 case 7:
907                         noisefunc = voronoi_F1F2;
908                         break;
909                 case 8:
910                         noisefunc = voronoi_Cr;
911                         break;
912                 case 14:
913                         noisefunc = cellNoiseU;
914                         break;
915                 case 0:
916                 default:
917                         noisefunc = orgBlenderNoise;
918                         x += 1;
919                         y += 1;
920                         z += 1;
921         }
922
923         if (noisesize!=0.0) {
924                 noisesize = 1.0/noisesize;
925                 x *= noisesize;
926                 y *= noisesize;
927                 z *= noisesize;
928         }
929
930         sum = 0;
931         for (i=0;i<=oct;i++, amp*=0.5, fscale*=2) {
932                 t = noisefunc(fscale*x, fscale*y, fscale*z);
933                 if (hard) t = fabs(2.0*t-1.0);
934                 sum += t * amp;
935         }
936         
937         sum *= ((float)(1<<oct)/(float)((1<<(oct+1))-1));
938
939         return sum;
940
941 }
942
943
944 /*************************************/
945 /*  NOISE FUNCTIONS BY KEN MUSGRAVE  */
946 /* Copyright 1994 F. Kenton Musgrave */
947 /*************************************/
948
949 /* All of these are modified to be able to use them with different noisebasis.
950    In some cases the original code seemed to contain errors, so it is not exactly
951    the same now as the orginal code (from "Texturing and Modelling: A procedural approach") */
952
953 /*
954  * Procedural fBm evaluated at "point"; returns value stored in "value".
955  *
956  * Copyright 1994 F. Kenton Musgrave
957  *
958  * Parameters:
959  *    ``H''  is the fractal increment parameter
960  *    ``lacunarity''  is the gap between successive frequencies
961  *    ``octaves''  is the number of frequencies in the fBm
962  */
963 float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
964 {
965         float   rmd, value=0.0, pwr=1.0, pwHL=pow(lacunarity, -H);
966         int     i;
967
968         float (*noisefunc)(float, float, float);
969         switch (noisebasis) {
970                 case 1:
971                         noisefunc = orgPerlinNoise;
972                         break;
973                 case 2:
974                         noisefunc = newPerlin;
975                         break;
976                 case 3:
977                         noisefunc = voronoi_F1S;
978                         break;
979                 case 4:
980                         noisefunc = voronoi_F2S;
981                         break;
982                 case 5:
983                         noisefunc = voronoi_F3S;
984                         break;
985                 case 6:
986                         noisefunc = voronoi_F4S;
987                         break;
988                 case 7:
989                         noisefunc = voronoi_F1F2S;
990                         break;
991                 case 8:
992                         noisefunc = voronoi_CrS;
993                         break;
994                 case 14:
995                         noisefunc = cellNoise;
996                         break;
997                 case 0:
998                 default: {
999                         noisefunc = orgBlenderNoiseS;
1000                 }
1001         }
1002         
1003         for (i=0; i<(int)octaves; i++) {
1004                 value += noisefunc(x, y, z) * pwr;
1005                 pwr *= pwHL;
1006                 x *= lacunarity;
1007                 y *= lacunarity;
1008                 z *= lacunarity;
1009         }
1010
1011         rmd = octaves - floor(octaves);
1012         if (rmd!=0.f) value += rmd * noisefunc(x, y, z) * pwr;
1013
1014         return value;
1015
1016 } /* fBm() */
1017
1018
1019 /*
1020  * Procedural multifractal evaluated at "point";
1021  * returns value stored in "value".
1022  *
1023  * Copyright 1994 F. Kenton Musgrave
1024  *
1025  * Parameters:
1026  *    ``H''  determines the highest fractal dimension
1027  *    ``lacunarity''  is gap between successive frequencies
1028  *    ``octaves''  is the number of frequencies in the fBm
1029  *    ``offset''  is the zero offset, which determines multifractality (NOT USED??)
1030  */
1031  /* this one is in fact rather confusing,
1032         * there seem to be errors in the original source code (in all three versions of proc.text&mod),
1033         * I modified it to something that made sense to me, so it might be wrong... */
1034 float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
1035 {
1036         float   rmd, value=1.0, pwr=1.0, pwHL=pow(lacunarity, -H);
1037         int i;
1038         
1039         float (*noisefunc)(float, float, float);
1040         switch (noisebasis) {
1041                 case 1:
1042                         noisefunc = orgPerlinNoise;
1043                         break;
1044                 case 2:
1045                         noisefunc = newPerlin;
1046                         break;
1047                 case 3:
1048                         noisefunc = voronoi_F1S;
1049                         break;
1050                 case 4:
1051                         noisefunc = voronoi_F2S;
1052                         break;
1053                 case 5:
1054                         noisefunc = voronoi_F3S;
1055                         break;
1056                 case 6:
1057                         noisefunc = voronoi_F4S;
1058                         break;
1059                 case 7:
1060                         noisefunc = voronoi_F1F2S;
1061                         break;
1062                 case 8:
1063                         noisefunc = voronoi_CrS;
1064                         break;
1065                 case 14:
1066                         noisefunc = cellNoise;
1067                         break;
1068                 case 0:
1069                 default: {
1070                         noisefunc = orgBlenderNoiseS;
1071                 }
1072         }
1073
1074         for (i=0; i<(int)octaves; i++) {
1075                 value *= (pwr * noisefunc(x, y, z) + 1.0);
1076                 pwr *= pwHL;
1077                 x *= lacunarity;
1078                 y *= lacunarity;
1079                 z *= lacunarity;
1080         }
1081         rmd = octaves - floor(octaves);
1082         if (rmd!=0.0) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0);
1083
1084         return value;
1085
1086 } /* multifractal() */
1087
1088 /*
1089  * Heterogeneous procedural terrain function: stats by altitude method.
1090  * Evaluated at "point"; returns value stored in "value".
1091  *
1092  * Copyright 1994 F. Kenton Musgrave
1093  *
1094  * Parameters:
1095  *       ``H''  determines the fractal dimension of the roughest areas
1096  *       ``lacunarity''  is the gap between successive frequencies
1097  *       ``octaves''  is the number of frequencies in the fBm
1098  *       ``offset''  raises the terrain from `sea level'
1099  */
1100 float mg_HeteroTerrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis)
1101 {
1102         float   value, increment, rmd;
1103         int i;
1104         float pwHL = pow(lacunarity, -H);
1105         float pwr = pwHL;       /* starts with i=1 instead of 0 */
1106
1107         float (*noisefunc)(float, float, float);
1108         switch (noisebasis) {
1109                 case 1:
1110                         noisefunc = orgPerlinNoise;
1111                         break;
1112                 case 2:
1113                         noisefunc = newPerlin;
1114                         break;
1115                 case 3:
1116                         noisefunc = voronoi_F1S;
1117                         break;
1118                 case 4:
1119                         noisefunc = voronoi_F2S;
1120                         break;
1121                 case 5:
1122                         noisefunc = voronoi_F3S;
1123                         break;
1124                 case 6:
1125                         noisefunc = voronoi_F4S;
1126                         break;
1127                 case 7:
1128                         noisefunc = voronoi_F1F2S;
1129                         break;
1130                 case 8:
1131                         noisefunc = voronoi_CrS;
1132                         break;
1133                 case 14:
1134                         noisefunc = cellNoise;
1135                         break;
1136                 case 0:
1137                 default: {
1138                         noisefunc = orgBlenderNoiseS;
1139                 }
1140         }
1141
1142         /* first unscaled octave of function; later octaves are scaled */
1143         value = offset + noisefunc(x, y, z);
1144         x *= lacunarity;
1145         y *= lacunarity;
1146         z *= lacunarity;
1147
1148         for (i=1; i<(int)octaves; i++) {
1149                 increment = (noisefunc(x, y, z) + offset) * pwr * value;
1150                 value += increment;
1151                 pwr *= pwHL;
1152                 x *= lacunarity;
1153                 y *= lacunarity;
1154                 z *= lacunarity;
1155         }
1156
1157         rmd = octaves - floor(octaves);
1158         if (rmd!=0.0) {
1159                 increment = (noisefunc(x, y, z) + offset) * pwr * value;
1160                 value += rmd * increment;
1161         }
1162         return value;
1163 }
1164
1165
1166 /* Hybrid additive/multiplicative multifractal terrain model.
1167  *
1168  * Copyright 1994 F. Kenton Musgrave
1169  *
1170  * Some good parameter values to start with:
1171  *
1172  *      H:           0.25
1173  *      offset:      0.7
1174  */
1175 float mg_HybridMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
1176 {
1177         float result, signal, weight, rmd;
1178         int i;
1179         float pwHL = pow(lacunarity, -H);
1180         float pwr = pwHL;       /* starts with i=1 instead of 0 */
1181         float (*noisefunc)(float, float, float);
1182
1183         switch (noisebasis) {
1184                 case 1:
1185                         noisefunc = orgPerlinNoise;
1186                         break;
1187                 case 2:
1188                         noisefunc = newPerlin;
1189                         break;
1190                 case 3:
1191                         noisefunc = voronoi_F1S;
1192                         break;
1193                 case 4:
1194                         noisefunc = voronoi_F2S;
1195                         break;
1196                 case 5:
1197                         noisefunc = voronoi_F3S;
1198                         break;
1199                 case 6:
1200                         noisefunc = voronoi_F4S;
1201                         break;
1202                 case 7:
1203                         noisefunc = voronoi_F1F2S;
1204                         break;
1205                 case 8:
1206                         noisefunc = voronoi_CrS;
1207                         break;
1208                 case 14:
1209                         noisefunc = cellNoise;
1210                         break;
1211                 case 0:
1212                 default: {
1213                         noisefunc = orgBlenderNoiseS;
1214                 }
1215         }
1216
1217         result = noisefunc(x, y, z) + offset;
1218         weight = gain * result;
1219         x *= lacunarity;
1220         y *= lacunarity;
1221         z *= lacunarity;
1222
1223         for (i=1; (weight>0.001) && (i<(int)octaves); i++) {
1224                 if (weight>1.0)  weight=1.0;
1225                 signal = (noisefunc(x, y, z) + offset) * pwr;
1226                 pwr *= pwHL;
1227                 result += weight * signal;
1228                 weight *= gain * signal;
1229                 x *= lacunarity;
1230                 y *= lacunarity;
1231                 z *= lacunarity;
1232         }
1233
1234         rmd = octaves - floor(octaves);
1235         if (rmd!=0.f) result += rmd * ((noisefunc(x, y, z) + offset) * pwr);
1236
1237         return result;
1238
1239 } /* HybridMultifractal() */
1240
1241
1242 /* Ridged multifractal terrain model.
1243  *
1244  * Copyright 1994 F. Kenton Musgrave
1245  *
1246  * Some good parameter values to start with:
1247  *
1248  *      H:           1.0
1249  *      offset:      1.0
1250  *      gain:        2.0
1251  */
1252 float mg_RidgedMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
1253 {
1254         float result, signal, weight;
1255         int     i;
1256         float pwHL = pow(lacunarity, -H);
1257         float pwr = pwHL;       /* starts with i=1 instead of 0 */
1258         
1259         float (*noisefunc)(float, float, float);
1260         switch (noisebasis) {
1261                 case 1:
1262                         noisefunc = orgPerlinNoise;
1263                         break;
1264                 case 2:
1265                         noisefunc = newPerlin;
1266                         break;
1267                 case 3:
1268                         noisefunc = voronoi_F1S;
1269                         break;
1270                 case 4:
1271                         noisefunc = voronoi_F2S;
1272                         break;
1273                 case 5:
1274                         noisefunc = voronoi_F3S;
1275                         break;
1276                 case 6:
1277                         noisefunc = voronoi_F4S;
1278                         break;
1279                 case 7:
1280                         noisefunc = voronoi_F1F2S;
1281                         break;
1282                 case 8:
1283                         noisefunc = voronoi_CrS;
1284                         break;
1285                 case 14:
1286                         noisefunc = cellNoise;
1287                         break;
1288                 case 0:
1289                 default: {
1290                         noisefunc = orgBlenderNoiseS;
1291                 }
1292         }
1293
1294         signal = offset - fabs(noisefunc(x, y, z));
1295         signal *= signal;
1296         result = signal;
1297         weight = 1.f;
1298
1299         for( i=1; i<(int)octaves; i++ ) {
1300                 x *= lacunarity;
1301                 y *= lacunarity;
1302                 z *= lacunarity;
1303                 weight = signal * gain;
1304                 if (weight>1.0) weight=1.0; else if (weight<0.0) weight=0.0;
1305                 signal = offset - fabs(noisefunc(x, y, z));
1306                 signal *= signal;
1307                 signal *= weight;
1308                 result += signal * pwr;
1309                 pwr *= pwHL;
1310         }
1311
1312         return result;
1313 } /* RidgedMultifractal() */
1314
1315 /* "Variable Lacunarity Noise"
1316  * A distorted variety of Perlin noise.
1317  *
1318  * Copyright 1994 F. Kenton Musgrave
1319  */
1320 float mg_VLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2)
1321 {
1322         float rv[3];
1323         float (*noisefunc1)(float, float, float);
1324         float (*noisefunc2)(float, float, float);
1325
1326         switch (nbas1) {
1327                 case 1:
1328                         noisefunc1 = orgPerlinNoise;
1329                         break;
1330                 case 2:
1331                         noisefunc1 = newPerlin;
1332                         break;
1333                 case 3:
1334                         noisefunc1 = voronoi_F1S;
1335                         break;
1336                 case 4:
1337                         noisefunc1 = voronoi_F2S;
1338                         break;
1339                 case 5:
1340                         noisefunc1 = voronoi_F3S;
1341                         break;
1342                 case 6:
1343                         noisefunc1 = voronoi_F4S;
1344                         break;
1345                 case 7:
1346                         noisefunc1 = voronoi_F1F2S;
1347                         break;
1348                 case 8:
1349                         noisefunc1 = voronoi_CrS;
1350                         break;
1351                 case 14:
1352                         noisefunc1 = cellNoise;
1353                         break;
1354                 case 0:
1355                 default: {
1356                         noisefunc1 = orgBlenderNoiseS;
1357                 }
1358         }
1359
1360         switch (nbas2) {
1361                 case 1:
1362                         noisefunc2 = orgPerlinNoise;
1363                         break;
1364                 case 2:
1365                         noisefunc2 = newPerlin;
1366                         break;
1367                 case 3:
1368                         noisefunc2 = voronoi_F1S;
1369                         break;
1370                 case 4:
1371                         noisefunc2 = voronoi_F2S;
1372                         break;
1373                 case 5:
1374                         noisefunc2 = voronoi_F3S;
1375                         break;
1376                 case 6:
1377                         noisefunc2 = voronoi_F4S;
1378                         break;
1379                 case 7:
1380                         noisefunc2 = voronoi_F1F2S;
1381                         break;
1382                 case 8:
1383                         noisefunc2 = voronoi_CrS;
1384                         break;
1385                 case 14:
1386                         noisefunc2 = cellNoise;
1387                         break;
1388                 case 0:
1389                 default: {
1390                         noisefunc2 = orgBlenderNoiseS;
1391                 }
1392         }
1393
1394         /* get a random vector and scale the randomization */
1395         rv[0] = noisefunc1(x+13.5, y+13.5, z+13.5) * distortion;
1396         rv[1] = noisefunc1(x, y, z) * distortion;
1397         rv[2] = noisefunc1(x-13.5, y-13.5, z-13.5) * distortion;
1398         return noisefunc2(x+rv[0], y+rv[1], z+rv[2]);   /* distorted-domain noise */
1399 }
1400
1401 /****************/
1402 /* musgrave end */
1403 /****************/