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