Mango request: added an input node to use track's position in compositor
[blender.git] / source / blender / blenlib / intern / noise.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/blenlib/intern/noise.c
30  *  \ingroup bli
31  */
32
33
34 #ifdef _WIN32    
35 #pragma warning (disable : 4244) // "conversion from double to float"
36 #pragma warning (disable : 4305) // "truncation from const double to float" 
37 #endif
38
39 #include <math.h>
40
41 #include "BLI_noise.h"
42
43 /* local */
44 static float noise3_perlin(float vec[3]);
45 //static float turbulence_perlin(float *point, float lofreq, float hifreq);
46 //static float turbulencep(float noisesize, float x, float y, float z, int nr);
47
48 #define HASHVEC(x, y, z) hashvectf + 3 * hash[(hash[(hash[(z) & 255] + (y)) & 255] + (x)) & 255]
49
50 /* needed for voronoi */
51 #define HASHPNT(x, y, z) hashpntf + 3 * hash[(hash[(hash[(z) & 255] + (y)) & 255] + (x)) & 255]
52 static float hashpntf[768] = {
53         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
165 unsigned char hash[512] = {
166         0xA2, 0xA0, 0x19, 0x3B, 0xF8, 0xEB, 0xAA, 0xEE, 0xF3, 0x1C, 0x67, 0x28, 0x1D, 0xED, 0x0,  0xDE, 0x95, 0x2E, 0xDC,
167         0x3F, 0x3A, 0x82, 0x35, 0x4D, 0x6C, 0xBA, 0x36, 0xD0, 0xF6, 0xC,  0x79, 0x32, 0xD1, 0x59, 0xF4, 0x8,  0x8B, 0x63,
168         0x89, 0x2F, 0xB8, 0xB4, 0x97, 0x83, 0xF2, 0x8F, 0x18, 0xC7, 0x51, 0x14, 0x65, 0x87, 0x48, 0x20, 0x42, 0xA8, 0x80,
169         0xB5, 0x40, 0x13, 0xB2, 0x22, 0x7E, 0x57, 0xBC, 0x7F, 0x6B, 0x9D, 0x86, 0x4C, 0xC8, 0xDB, 0x7C, 0xD5, 0x25, 0x4E,
170         0x5A, 0x55, 0x74, 0x50, 0xCD, 0xB3, 0x7A, 0xBB, 0xC3, 0xCB, 0xB6, 0xE2, 0xE4, 0xEC, 0xFD, 0x98, 0xB,  0x96, 0xD3,
171         0x9E, 0x5C, 0xA1, 0x64, 0xF1, 0x81, 0x61, 0xE1, 0xC4, 0x24, 0x72, 0x49, 0x8C, 0x90, 0x4B, 0x84, 0x34, 0x38, 0xAB,
172         0x78, 0xCA, 0x1F, 0x1,  0xD7, 0x93, 0x11, 0xC1, 0x58, 0xA9, 0x31, 0xF9, 0x44, 0x6D, 0xBF, 0x33, 0x9C, 0x5F, 0x9,
173         0x94, 0xA3, 0x85, 0x6,  0xC6, 0x9A, 0x1E, 0x7B, 0x46, 0x15, 0x30, 0x27, 0x2B, 0x1B, 0x71, 0x3C, 0x5B, 0xD6, 0x6F,
174         0x62, 0xAC, 0x4F, 0xC2, 0xC0, 0xE,  0xB1, 0x23, 0xA7, 0xDF, 0x47, 0xB0, 0x77, 0x69, 0x5,  0xE9, 0xE6, 0xE7, 0x76,
175         0x73, 0xF,  0xFE, 0x6E, 0x9B, 0x56, 0xEF, 0x12, 0xA5, 0x37, 0xFC, 0xAE, 0xD9, 0x3,  0x8E, 0xDD, 0x10, 0xB9, 0xCE,
176         0xC9, 0x8D, 0xDA, 0x2A, 0xBD, 0x68, 0x17, 0x9F, 0xBE, 0xD4, 0xA,  0xCC, 0xD2, 0xE8, 0x43, 0x3D, 0x70, 0xB7, 0x2,
177         0x7D, 0x99, 0xD8, 0xD,  0x60, 0x8A, 0x4,  0x2C, 0x3E, 0x92, 0xE5, 0xAF, 0x53, 0x7,  0xE0, 0x29, 0xA6, 0xC5, 0xE3,
178         0xF5, 0xF7, 0x4A, 0x41, 0x26, 0x6A, 0x16, 0x5E, 0x52, 0x2D, 0x21, 0xAD, 0xF0, 0x91, 0xFF, 0xEA, 0x54, 0xFA, 0x66,
179         0x1A, 0x45, 0x39, 0xCF, 0x75, 0xA4, 0x88, 0xFB, 0x5D, 0xA2, 0xA0, 0x19, 0x3B, 0xF8, 0xEB, 0xAA, 0xEE, 0xF3, 0x1C,
180         0x67, 0x28, 0x1D, 0xED, 0x0,  0xDE, 0x95, 0x2E, 0xDC, 0x3F, 0x3A, 0x82, 0x35, 0x4D, 0x6C, 0xBA, 0x36, 0xD0, 0xF6,
181         0xC,  0x79, 0x32, 0xD1, 0x59, 0xF4, 0x8,  0x8B, 0x63, 0x89, 0x2F, 0xB8, 0xB4, 0x97, 0x83, 0xF2, 0x8F, 0x18, 0xC7,
182         0x51, 0x14, 0x65, 0x87, 0x48, 0x20, 0x42, 0xA8, 0x80, 0xB5, 0x40, 0x13, 0xB2, 0x22, 0x7E, 0x57, 0xBC, 0x7F, 0x6B,
183         0x9D, 0x86, 0x4C, 0xC8, 0xDB, 0x7C, 0xD5, 0x25, 0x4E, 0x5A, 0x55, 0x74, 0x50, 0xCD, 0xB3, 0x7A, 0xBB, 0xC3, 0xCB,
184         0xB6, 0xE2, 0xE4, 0xEC, 0xFD, 0x98, 0xB,  0x96, 0xD3, 0x9E, 0x5C, 0xA1, 0x64, 0xF1, 0x81, 0x61, 0xE1, 0xC4, 0x24,
185         0x72, 0x49, 0x8C, 0x90, 0x4B, 0x84, 0x34, 0x38, 0xAB, 0x78, 0xCA, 0x1F, 0x1,  0xD7, 0x93, 0x11, 0xC1, 0x58, 0xA9,
186         0x31, 0xF9, 0x44, 0x6D, 0xBF, 0x33, 0x9C, 0x5F, 0x9,  0x94, 0xA3, 0x85, 0x6,  0xC6, 0x9A, 0x1E, 0x7B, 0x46, 0x15,
187         0x30, 0x27, 0x2B, 0x1B, 0x71, 0x3C, 0x5B, 0xD6, 0x6F, 0x62, 0xAC, 0x4F, 0xC2, 0xC0, 0xE,  0xB1, 0x23, 0xA7, 0xDF,
188         0x47, 0xB0, 0x77, 0x69, 0x5,  0xE9, 0xE6, 0xE7, 0x76, 0x73, 0xF,  0xFE, 0x6E, 0x9B, 0x56, 0xEF, 0x12, 0xA5, 0x37,
189         0xFC, 0xAE, 0xD9, 0x3,  0x8E, 0xDD, 0x10, 0xB9, 0xCE, 0xC9, 0x8D, 0xDA, 0x2A, 0xBD, 0x68, 0x17, 0x9F, 0xBE, 0xD4,
190         0xA,  0xCC, 0xD2, 0xE8, 0x43, 0x3D, 0x70, 0xB7, 0x2,  0x7D, 0x99, 0xD8, 0xD,  0x60, 0x8A, 0x4,  0x2C, 0x3E, 0x92,
191         0xE5, 0xAF, 0x53, 0x7,  0xE0, 0x29, 0xA6, 0xC5, 0xE3, 0xF5, 0xF7, 0x4A, 0x41, 0x26, 0x6A, 0x16, 0x5E, 0x52, 0x2D,
192         0x21, 0xAD, 0xF0, 0x91, 0xFF, 0xEA, 0x54, 0xFA, 0x66, 0x1A, 0x45, 0x39, 0xCF, 0x75, 0xA4, 0x88, 0xFB, 0x5D,
193 };
194
195
196 float hashvectf[768] = {
197         0.33783, 0.715698, -0.611206, -0.944031, -0.326599, -0.045624, -0.101074, -0.416443, -0.903503, 0.799286, 0.49411,
198         -0.341949, -0.854645, 0.518036, 0.033936, 0.42514, -0.437866, -0.792114, -0.358948, 0.597046, 0.717377, -0.985413,
199         0.144714, 0.089294, -0.601776, -0.33728, -0.723907, -0.449921, 0.594513, 0.666382, 0.208313, -0.10791, 0.972076,
200         0.575317, 0.060425, 0.815643, 0.293365, -0.875702, -0.383453, 0.293762, 0.465759, 0.834686, -0.846008, -0.233398,
201         -0.47934, -0.115814, 0.143036, -0.98291, 0.204681, -0.949036, -0.239532, 0.946716, -0.263947, 0.184326, -0.235596,
202         0.573822, 0.784332, 0.203705, -0.372253, -0.905487, 0.756989, -0.651031, 0.055298, 0.497803, 0.814697, -0.297363,
203         -0.16214, 0.063995, -0.98468, -0.329254, 0.834381, 0.441925, 0.703827, -0.527039, -0.476227, 0.956421, 0.266113,
204         0.119781, 0.480133, 0.482849, 0.7323, -0.18631, 0.961212, -0.203125, -0.748474, -0.656921, -0.090393, -0.085052,
205         -0.165253, 0.982544, -0.76947, 0.628174, -0.115234, 0.383148, 0.537659, 0.751068, 0.616486, -0.668488, -0.415924,
206         -0.259979, -0.630005, 0.73175, 0.570953, -0.087952, 0.816223, -0.458008, 0.023254, 0.888611, -0.196167, 0.976563,
207         -0.088287, -0.263885, -0.69812, -0.665527, 0.437134, -0.892273, -0.112793, -0.621674, -0.230438, 0.748566, 0.232422,
208         0.900574, -0.367249, 0.22229, -0.796143, 0.562744, -0.665497, -0.73764, 0.11377, 0.670135, 0.704803, 0.232605,
209         0.895599, 0.429749, -0.114655, -0.11557, -0.474243, 0.872742, 0.621826, 0.604004, -0.498444, -0.832214, 0.012756,
210         0.55426, -0.702484, 0.705994, -0.089661, -0.692017, 0.649292, 0.315399, -0.175995, -0.977997, 0.111877, 0.096954,
211         -0.04953, 0.994019, 0.635284, -0.606689, -0.477783, -0.261261, -0.607422, -0.750153, 0.983276, 0.165436, 0.075958,
212         -0.29837, 0.404083, -0.864655, -0.638672, 0.507721, 0.578156, 0.388214, 0.412079, 0.824249, 0.556183, -0.208832,
213         0.804352, 0.778442, 0.562012, 0.27951, -0.616577, 0.781921, -0.091522, 0.196289, 0.051056, 0.979187, -0.121216,
214         0.207153, -0.970734, -0.173401, -0.384735, 0.906555, 0.161499, -0.723236, -0.671387, 0.178497, -0.006226, -0.983887,
215         -0.126038, 0.15799, 0.97934, 0.830475, -0.024811, 0.556458, -0.510132, -0.76944, 0.384247, 0.81424, 0.200104,
216         -0.544891, -0.112549, -0.393311, -0.912445, 0.56189, 0.152222, -0.813049, 0.198914, -0.254517, -0.946381, -0.41217,
217         0.690979, -0.593811, -0.407257, 0.324524, 0.853668, -0.690186, 0.366119, -0.624115, -0.428345, 0.844147, -0.322296,
218         -0.21228, -0.297546, -0.930756, -0.273071, 0.516113, 0.811798, 0.928314, 0.371643, 0.007233, 0.785828, -0.479218,
219         -0.390778, -0.704895, 0.058929, 0.706818, 0.173248, 0.203583, 0.963562, 0.422211, -0.904297, -0.062469, -0.363312,
220         -0.182465, 0.913605, 0.254028, -0.552307, -0.793945, -0.28891, -0.765747, -0.574554, 0.058319, 0.291382, 0.954803,
221         0.946136, -0.303925, 0.111267, -0.078156, 0.443695, -0.892731, 0.182098, 0.89389, 0.409515, -0.680298, -0.213318,
222         0.701141, 0.062469, 0.848389, -0.525635, -0.72879, -0.641846, 0.238342, -0.88089, 0.427673, 0.202637, -0.532501,
223         -0.21405, 0.818878, 0.948975, -0.305084, 0.07962, 0.925446, 0.374664, 0.055817, 0.820923, 0.565491, 0.079102,
224         0.25882, 0.099792, -0.960724, -0.294617, 0.910522, 0.289978, 0.137115, 0.320038, -0.937408, -0.908386, 0.345276,
225         -0.235718, -0.936218, 0.138763, 0.322754, 0.366577, 0.925934, -0.090637, 0.309296, -0.686829, -0.657684, 0.66983,
226         0.024445, 0.742065, -0.917999, -0.059113, -0.392059, 0.365509, 0.462158, -0.807922, 0.083374, 0.996399, -0.014801,
227         0.593842, 0.253143, -0.763672, 0.974976, -0.165466, 0.148285, 0.918976, 0.137299, 0.369537, 0.294952, 0.694977,
228         0.655731, 0.943085, 0.152618, -0.295319, 0.58783, -0.598236, 0.544495, 0.203796, 0.678223, 0.705994, -0.478821,
229         -0.661011, 0.577667, 0.719055, -0.1698, -0.673828, -0.132172, -0.965332, 0.225006, -0.981873, -0.14502, 0.121979,
230         0.763458, 0.579742, 0.284546, -0.893188, 0.079681, 0.442474, -0.795776, -0.523804, 0.303802, 0.734955, 0.67804,
231         -0.007446, 0.15506, 0.986267, -0.056183, 0.258026, 0.571503, -0.778931, -0.681549, -0.702087, -0.206116, -0.96286,
232         -0.177185, 0.203613, -0.470978, -0.515106, 0.716095, -0.740326, 0.57135, 0.354095, -0.56012, -0.824982, -0.074982,
233         -0.507874, 0.753204, 0.417969, -0.503113, 0.038147, 0.863342, 0.594025, 0.673553, -0.439758, -0.119873, -0.005524,
234         -0.992737, 0.098267, -0.213776, 0.971893, -0.615631, 0.643951, 0.454163, 0.896851, -0.441071, 0.032166, -0.555023,
235         0.750763, -0.358093, 0.398773, 0.304688, 0.864929, -0.722961, 0.303589, 0.620544, -0.63559, -0.621948, -0.457306,
236         -0.293243, 0.072327, 0.953278, -0.491638, 0.661041, -0.566772, -0.304199, -0.572083, -0.761688, 0.908081, -0.398956,
237         0.127014, -0.523621, -0.549683, -0.650848, -0.932922, -0.19986, 0.299408, 0.099426, 0.140869, 0.984985, -0.020325,
238         -0.999756, -0.002319, 0.952667, 0.280853, -0.11615, -0.971893, 0.082581, 0.220337, 0.65921, 0.705292, -0.260651,
239         0.733063, -0.175537, 0.657043, -0.555206, 0.429504, -0.712189, 0.400421, -0.89859, 0.179352, 0.750885, -0.19696,
240         0.630341, 0.785675, -0.569336, 0.241821, -0.058899, -0.464111, 0.883789, 0.129608, -0.94519, 0.299622, -0.357819,
241         0.907654, 0.219238, -0.842133, -0.439117, -0.312927, -0.313477, 0.84433, 0.434479, -0.241211, 0.053253, 0.968994,
242         0.063873, 0.823273, 0.563965, 0.476288, 0.862152, -0.172516, 0.620941, -0.298126, 0.724915, 0.25238, -0.749359,
243         -0.612122, -0.577545, 0.386566, 0.718994, -0.406342, -0.737976, 0.538696, 0.04718, 0.556305, 0.82959, -0.802856,
244         0.587463, 0.101166, -0.707733, -0.705963, 0.026428, 0.374908, 0.68457, 0.625092, 0.472137, 0.208405, -0.856506,
245         -0.703064, -0.581085, -0.409821, -0.417206, -0.736328, 0.532623, -0.447876, -0.20285, -0.870728, 0.086945,
246         -0.990417, 0.107086, 0.183685, 0.018341, -0.982788, 0.560638, -0.428864, 0.708282, 0.296722, -0.952576, -0.0672,
247         0.135773, 0.990265, 0.030243, -0.068787, 0.654724, 0.752686, 0.762604, -0.551758, 0.337585, -0.819611, -0.407684,
248         0.402466, -0.727844, -0.55072, -0.408539, -0.855774, -0.480011, 0.19281, 0.693176, -0.079285, 0.716339, 0.226013,
249         0.650116, -0.725433, 0.246704, 0.953369, -0.173553, -0.970398, -0.239227, -0.03244, 0.136383, -0.394318, 0.908752,
250         0.813232, 0.558167, 0.164368, 0.40451, 0.549042, -0.731323, -0.380249, -0.566711, 0.730865, 0.022156, 0.932739,
251         0.359741, 0.00824, 0.996552, -0.082306, 0.956635, -0.065338, -0.283722, -0.743561, 0.008209, 0.668579, -0.859589,
252         -0.509674, 0.035767, -0.852234, 0.363678, -0.375977, -0.201965, -0.970795, -0.12915, 0.313477, 0.947327, 0.06546,
253         -0.254028, -0.528259, 0.81015, 0.628052, 0.601105, 0.49411, -0.494385, 0.868378, 0.037933, 0.275635, -0.086426,
254         0.957336, -0.197937, 0.468903, -0.860748, 0.895599, 0.399384, 0.195801, 0.560791, 0.825012, -0.069214, 0.304199,
255         -0.849487, 0.43103, 0.096375, 0.93576, 0.339111, -0.051422, 0.408966, -0.911072, 0.330444, 0.942841, -0.042389,
256         -0.452362, -0.786407, 0.420563, 0.134308, -0.933472, -0.332489, 0.80191, -0.566711, -0.188934, -0.987946, -0.105988,
257         0.112518, -0.24408, 0.892242, -0.379791, -0.920502, 0.229095, -0.316376, 0.7789, 0.325958, 0.535706, -0.912872,
258         0.185211, -0.36377, -0.184784, 0.565369, -0.803833, -0.018463, 0.119537, 0.992615, -0.259247, -0.935608, 0.239532,
259         -0.82373, -0.449127, -0.345947, -0.433105, 0.659515, 0.614349, -0.822754, 0.378845, -0.423676, 0.687195, -0.674835,
260         -0.26889, -0.246582, -0.800842, 0.545715, -0.729187, -0.207794, 0.651978, 0.653534, -0.610443, -0.447388, 0.492584,
261         -0.023346, 0.869934, 0.609039, 0.009094, -0.79306, 0.962494, -0.271088, -0.00885, 0.2659, -0.004913, 0.963959,
262         0.651245, 0.553619, -0.518951, 0.280548, -0.84314, 0.458618, -0.175293, -0.983215, 0.049805, 0.035339, -0.979919,
263         0.196045, -0.982941, 0.164307, -0.082245, 0.233734, -0.97226, -0.005005, -0.747253, -0.611328, 0.260437, 0.645599,
264         0.592773, 0.481384, 0.117706, -0.949524, -0.29068, -0.535004, -0.791901, -0.294312, -0.627167, -0.214447, 0.748718,
265         -0.047974, -0.813477, -0.57959, -0.175537, 0.477264, -0.860992, 0.738556, -0.414246, -0.53183, 0.562561, -0.704071,
266         0.433289, -0.754944, 0.64801, -0.100586, 0.114716, 0.044525, -0.992371, 0.966003, 0.244873, -0.082764,
267 };
268
269 /**************************/
270 /*  IMPROVED PERLIN NOISE */
271 /**************************/
272
273 static float lerp(float t, float a, float b)
274 {
275         return (a + t * (b - a));
276 }
277
278 static float npfade(float t)
279 {
280         return (t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f));
281 }
282
283 static float grad(int hash, float x, float y, float z)
284 {
285         int h = hash & 15;                     /* CONVERT LO 4 BITS OF HASH CODE */
286         float u = h < 8 ? x : y,               /* INTO 12 GRADIENT DIRECTIONS. */
287               v = h < 4 ? y : h == 12 || h == 14 ? x : z;
288         return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
289 }
290
291 /* instead of adding another permutation array, just use hash table defined above */
292 static float newPerlin(float x, float y, float z)
293 {
294         int A, AA, AB, B, BA, BB;
295         float u = floor(x), v = floor(y), w = floor(z);
296         int X = ((int)u) & 255, Y = ((int)v) & 255, Z = ((int)w) & 255;   /* FIND UNIT CUBE THAT CONTAINS POINT */
297         x -= u;             /* FIND RELATIVE X,Y,Z */
298         y -= v;             /* OF POINT IN CUBE. */
299         z -= w;
300         u = npfade(x);      /* COMPUTE FADE CURVES */
301         v = npfade(y);      /* FOR EACH OF X,Y,Z. */
302         w = npfade(z);
303         A = hash[X  ]+Y;  AA = hash[A]+Z;  AB = hash[A+1]+Z;      /* HASH COORDINATES OF */
304         B = hash[X+1]+Y;  BA = hash[B]+Z;  BB = hash[B+1]+Z;      /* THE 8 CUBE CORNERS, */
305         return lerp(w, lerp(v, lerp(u, grad(hash[AA   ],  x,     y,     z    ),   /* AND ADD */
306                                        grad(hash[BA   ],  x - 1, y,     z    )),  /* BLENDED */
307                                lerp(u, grad(hash[AB   ],  x,     y - 1, z    ),   /* RESULTS */
308                                        grad(hash[BB   ],  x - 1, y - 1, z    ))), /* FROM  8 */
309                        lerp(v, lerp(u, grad(hash[AA + 1], x,     y,     z - 1),   /* CORNERS */
310                                        grad(hash[BA + 1], x - 1, y,     z - 1)),  /* OF CUBE */
311                                lerp(u, grad(hash[AB + 1], x,     y - 1, z - 1),
312                                        grad(hash[BB + 1], x - 1, y - 1, z - 1))));
313 }
314
315 /* for use with BLI_gNoise()/BLI_gTurbulence(), returns unsigned improved perlin noise */
316 static float newPerlinU(float x, float y, float z)
317 {
318         return (0.5f + 0.5f * newPerlin(x, y, z));
319 }
320
321
322 /**************************/
323 /* END OF IMPROVED PERLIN */
324 /**************************/
325
326 /* Was BLI_hnoise(), removed noisesize, so other functions can call it without scaling. */
327 static float orgBlenderNoise(float x, float y, float z)
328 {
329         register float cn1, cn2, cn3, cn4, cn5, cn6, i, *h;
330         float fx, fy, fz, ox, oy, oz, jx, jy, jz;
331         float n = 0.5;
332         int ix, iy, iz, b00, b01, b10, b11, b20, b21;
333
334         fx = floor(x);
335         fy = floor(y);
336         fz = floor(z);
337
338         ox = x - fx;
339         oy = y - fy;
340         oz = z - fz;
341
342         ix = (int)fx;
343         iy = (int)fy;
344         iz = (int)fz;
345
346         jx = ox - 1;
347         jy = oy - 1;
348         jz = oz - 1;
349
350         cn1 = ox * ox; cn2 = oy * oy; cn3 = oz * oz;
351         cn4 = jx * jx; cn5 = jy * jy; cn6 = jz * jz;
352
353         cn1 = 1.0f - 3.0f * cn1 + 2.0f * cn1 * ox;
354         cn2 = 1.0f - 3.0f * cn2 + 2.0f * cn2 * oy;
355         cn3 = 1.0f - 3.0f * cn3 + 2.0f * cn3 * oz;
356         cn4 = 1.0f - 3.0f * cn4 - 2.0f * cn4 * jx;
357         cn5 = 1.0f - 3.0f * cn5 - 2.0f * cn5 * jy;
358         cn6 = 1.0f - 3.0f * cn6 - 2.0f * cn6 * jz;
359
360         b00 = hash[hash[ix & 255] + (iy & 255)];
361         b10 = hash[hash[(ix + 1) & 255] + (iy & 255)];
362         b01 = hash[hash[ix & 255] + ((iy + 1) & 255)];
363         b11 = hash[hash[(ix + 1) & 255] + ((iy + 1) & 255)];
364
365         b20 = iz & 255; b21 = (iz + 1) & 255;
366
367         /* 0 */
368         i = (cn1 * cn2 * cn3);
369         h = hashvectf + 3 * hash[b20 + b00];
370         n += i * (h[0] * ox + h[1] * oy + h[2] * oz);
371         /* 1 */
372         i = (cn1 * cn2 * cn6);
373         h = hashvectf + 3 * hash[b21 + b00];
374         n += i * (h[0] * ox + h[1] * oy + h[2] * jz);
375         /* 2 */
376         i = (cn1 * cn5 * cn3);
377         h = hashvectf + 3 * hash[b20 + b01];
378         n += i * (h[0] * ox + h[1] * jy + h[2] * oz);
379         /* 3 */
380         i = (cn1 * cn5 * cn6);
381         h = hashvectf + 3 * hash[b21 + b01];
382         n += i * (h[0] * ox + h[1] * jy + h[2] * jz);
383         /* 4 */
384         i = cn4 * cn2 * cn3;
385         h = hashvectf + 3 * hash[b20 + b10];
386         n += i * (h[0] * jx + h[1] * oy + h[2] * oz);
387         /* 5 */
388         i = cn4 * cn2 * cn6;
389         h = hashvectf + 3 * hash[b21 + b10];
390         n += i * (h[0] * jx + h[1] * oy + h[2] * jz);
391         /* 6 */
392         i = cn4 * cn5 * cn3;
393         h = hashvectf + 3 * hash[b20 + b11];
394         n +=  i * (h[0] * jx + h[1] * jy + h[2] * oz);
395         /* 7 */
396         i = (cn4 * cn5 * cn6);
397         h = hashvectf + 3 * hash[b21 + b11];
398         n += i * (h[0] * jx + h[1] * jy + h[2] * jz);
399
400         if (n < 0.0f) n = 0.0f; else if (n > 1.0f) n = 1.0f;
401         return n;
402 }
403
404 /* as orgBlenderNoise(), returning signed noise */
405 static float orgBlenderNoiseS(float x, float y, float z)
406 {
407         return (2.0f * orgBlenderNoise(x, y, z) - 1.0f);
408 }
409
410 /* separated from orgBlenderNoise above, with scaling */
411 float BLI_hnoise(float noisesize, float x, float y, float z)
412 {
413         if (noisesize == 0.0f) return 0.0f;
414         x = (1.0f + x) / noisesize;
415         y = (1.0f + y) / noisesize;
416         z = (1.0f + z) / noisesize;
417         return orgBlenderNoise(x, y, z);
418 }
419
420
421 /* original turbulence functions */
422 float BLI_turbulence(float noisesize, float x, float y, float z, int nr)
423 {
424         float s, d = 0.5, div = 1.0;
425
426         s = BLI_hnoise(noisesize, x, y, z);
427         
428         while (nr > 0) {
429         
430                 s += d * BLI_hnoise(noisesize * d, x, y, z);
431                 div += d;
432                 d *= 0.5f;
433
434                 nr--;
435         }
436         return s / div;
437 }
438
439 float BLI_turbulence1(float noisesize, float x, float y, float z, int nr)
440 {
441         float s, d = 0.5, div = 1.0;
442
443         s = fabsf((-1.0f + 2.0f * BLI_hnoise(noisesize, x, y, z)));
444         
445         while (nr > 0) {
446         
447                 s += fabsf(d * (-1.0f + 2.0f * BLI_hnoise(noisesize * d, x, y, z)));
448                 div += d;
449                 d *= 0.5f;
450                 
451                 nr--;
452         }
453         return s / div;
454 }
455
456 /* ********************* FROM PERLIN HIMSELF: ******************** */
457
458 static char p[512 + 2] = {
459         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,
460         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,
461         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,
462         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,
463         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,
464         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,
465         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,
466         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,
467         0xA2, 0xA0
468 };
469
470
471 static float g[512 + 2][3] = {
472         {0.33783, 0.715698, -0.611206},
473         {-0.944031, -0.326599, -0.045624},
474         {-0.101074, -0.416443, -0.903503},
475         {0.799286, 0.49411, -0.341949},
476         {-0.854645, 0.518036, 0.033936},
477         {0.42514, -0.437866, -0.792114},
478         {-0.358948, 0.597046, 0.717377},
479         {-0.985413, 0.144714, 0.089294},
480         {-0.601776, -0.33728, -0.723907},
481         {-0.449921, 0.594513, 0.666382},
482         {0.208313, -0.10791, 0.972076},
483         {0.575317, 0.060425, 0.815643},
484         {0.293365, -0.875702, -0.383453},
485         {0.293762, 0.465759, 0.834686},
486         {-0.846008, -0.233398, -0.47934},
487         {-0.115814, 0.143036, -0.98291},
488         {0.204681, -0.949036, -0.239532},
489         {0.946716, -0.263947, 0.184326},
490         {-0.235596, 0.573822, 0.784332},
491         {0.203705, -0.372253, -0.905487},
492         {0.756989, -0.651031, 0.055298},
493         {0.497803, 0.814697, -0.297363},
494         {-0.16214, 0.063995, -0.98468},
495         {-0.329254, 0.834381, 0.441925},
496         {0.703827, -0.527039, -0.476227},
497         {0.956421, 0.266113, 0.119781},
498         {0.480133, 0.482849, 0.7323},
499         {-0.18631, 0.961212, -0.203125},
500         {-0.748474, -0.656921, -0.090393},
501         {-0.085052, -0.165253, 0.982544},
502         {-0.76947, 0.628174, -0.115234},
503         {0.383148, 0.537659, 0.751068},
504         {0.616486, -0.668488, -0.415924},
505         {-0.259979, -0.630005, 0.73175},
506         {0.570953, -0.087952, 0.816223},
507         {-0.458008, 0.023254, 0.888611},
508         {-0.196167, 0.976563, -0.088287},
509         {-0.263885, -0.69812, -0.665527},
510         {0.437134, -0.892273, -0.112793},
511         {-0.621674, -0.230438, 0.748566},
512         {0.232422, 0.900574, -0.367249},
513         {0.22229, -0.796143, 0.562744},
514         {-0.665497, -0.73764, 0.11377},
515         {0.670135, 0.704803, 0.232605},
516         {0.895599, 0.429749, -0.114655},
517         {-0.11557, -0.474243, 0.872742},
518         {0.621826, 0.604004, -0.498444},
519         {-0.832214, 0.012756, 0.55426},
520         {-0.702484, 0.705994, -0.089661},
521         {-0.692017, 0.649292, 0.315399},
522         {-0.175995, -0.977997, 0.111877},
523         {0.096954, -0.04953, 0.994019},
524         {0.635284, -0.606689, -0.477783},
525         {-0.261261, -0.607422, -0.750153},
526         {0.983276, 0.165436, 0.075958},
527         {-0.29837, 0.404083, -0.864655},
528         {-0.638672, 0.507721, 0.578156},
529         {0.388214, 0.412079, 0.824249},
530         {0.556183, -0.208832, 0.804352},
531         {0.778442, 0.562012, 0.27951},
532         {-0.616577, 0.781921, -0.091522},
533         {0.196289, 0.051056, 0.979187},
534         {-0.121216, 0.207153, -0.970734},
535         {-0.173401, -0.384735, 0.906555},
536         {0.161499, -0.723236, -0.671387},
537         {0.178497, -0.006226, -0.983887},
538         {-0.126038, 0.15799, 0.97934},
539         {0.830475, -0.024811, 0.556458},
540         {-0.510132, -0.76944, 0.384247},
541         {0.81424, 0.200104, -0.544891},
542         {-0.112549, -0.393311, -0.912445},
543         {0.56189, 0.152222, -0.813049},
544         {0.198914, -0.254517, -0.946381},
545         {-0.41217, 0.690979, -0.593811},
546         {-0.407257, 0.324524, 0.853668},
547         {-0.690186, 0.366119, -0.624115},
548         {-0.428345, 0.844147, -0.322296},
549         {-0.21228, -0.297546, -0.930756},
550         {-0.273071, 0.516113, 0.811798},
551         {0.928314, 0.371643, 0.007233},
552         {0.785828, -0.479218, -0.390778},
553         {-0.704895, 0.058929, 0.706818},
554         {0.173248, 0.203583, 0.963562},
555         {0.422211, -0.904297, -0.062469},
556         {-0.363312, -0.182465, 0.913605},
557         {0.254028, -0.552307, -0.793945},
558         {-0.28891, -0.765747, -0.574554},
559         {0.058319, 0.291382, 0.954803},
560         {0.946136, -0.303925, 0.111267},
561         {-0.078156, 0.443695, -0.892731},
562         {0.182098, 0.89389, 0.409515},
563         {-0.680298, -0.213318, 0.701141},
564         {0.062469, 0.848389, -0.525635},
565         {-0.72879, -0.641846, 0.238342},
566         {-0.88089, 0.427673, 0.202637},
567         {-0.532501, -0.21405, 0.818878},
568         {0.948975, -0.305084, 0.07962},
569         {0.925446, 0.374664, 0.055817},
570         {0.820923, 0.565491, 0.079102},
571         {0.25882, 0.099792, -0.960724},
572         {-0.294617, 0.910522, 0.289978},
573         {0.137115, 0.320038, -0.937408},
574         {-0.908386, 0.345276, -0.235718},
575         {-0.936218, 0.138763, 0.322754},
576         {0.366577, 0.925934, -0.090637},
577         {0.309296, -0.686829, -0.657684},
578         {0.66983, 0.024445, 0.742065},
579         {-0.917999, -0.059113, -0.392059},
580         {0.365509, 0.462158, -0.807922},
581         {0.083374, 0.996399, -0.014801},
582         {0.593842, 0.253143, -0.763672},
583         {0.974976, -0.165466, 0.148285},
584         {0.918976, 0.137299, 0.369537},
585         {0.294952, 0.694977, 0.655731},
586         {0.943085, 0.152618, -0.295319},
587         {0.58783, -0.598236, 0.544495},
588         {0.203796, 0.678223, 0.705994},
589         {-0.478821, -0.661011, 0.577667},
590         {0.719055, -0.1698, -0.673828},
591         {-0.132172, -0.965332, 0.225006},
592         {-0.981873, -0.14502, 0.121979},
593         {0.763458, 0.579742, 0.284546},
594         {-0.893188, 0.079681, 0.442474},
595         {-0.795776, -0.523804, 0.303802},
596         {0.734955, 0.67804, -0.007446},
597         {0.15506, 0.986267, -0.056183},
598         {0.258026, 0.571503, -0.778931},
599         {-0.681549, -0.702087, -0.206116},
600         {-0.96286, -0.177185, 0.203613},
601         {-0.470978, -0.515106, 0.716095},
602         {-0.740326, 0.57135, 0.354095},
603         {-0.56012, -0.824982, -0.074982},
604         {-0.507874, 0.753204, 0.417969},
605         {-0.503113, 0.038147, 0.863342},
606         {0.594025, 0.673553, -0.439758},
607         {-0.119873, -0.005524, -0.992737},
608         {0.098267, -0.213776, 0.971893},
609         {-0.615631, 0.643951, 0.454163},
610         {0.896851, -0.441071, 0.032166},
611         {-0.555023, 0.750763, -0.358093},
612         {0.398773, 0.304688, 0.864929},
613         {-0.722961, 0.303589, 0.620544},
614         {-0.63559, -0.621948, -0.457306},
615         {-0.293243, 0.072327, 0.953278},
616         {-0.491638, 0.661041, -0.566772},
617         {-0.304199, -0.572083, -0.761688},
618         {0.908081, -0.398956, 0.127014},
619         {-0.523621, -0.549683, -0.650848},
620         {-0.932922, -0.19986, 0.299408},
621         {0.099426, 0.140869, 0.984985},
622         {-0.020325, -0.999756, -0.002319},
623         {0.952667, 0.280853, -0.11615},
624         {-0.971893, 0.082581, 0.220337},
625         {0.65921, 0.705292, -0.260651},
626         {0.733063, -0.175537, 0.657043},
627         {-0.555206, 0.429504, -0.712189},
628         {0.400421, -0.89859, 0.179352},
629         {0.750885, -0.19696, 0.630341},
630         {0.785675, -0.569336, 0.241821},
631         {-0.058899, -0.464111, 0.883789},
632         {0.129608, -0.94519, 0.299622},
633         {-0.357819, 0.907654, 0.219238},
634         {-0.842133, -0.439117, -0.312927},
635         {-0.313477, 0.84433, 0.434479},
636         {-0.241211, 0.053253, 0.968994},
637         {0.063873, 0.823273, 0.563965},
638         {0.476288, 0.862152, -0.172516},
639         {0.620941, -0.298126, 0.724915},
640         {0.25238, -0.749359, -0.612122},
641         {-0.577545, 0.386566, 0.718994},
642         {-0.406342, -0.737976, 0.538696},
643         {0.04718, 0.556305, 0.82959},
644         {-0.802856, 0.587463, 0.101166},
645         {-0.707733, -0.705963, 0.026428},
646         {0.374908, 0.68457, 0.625092},
647         {0.472137, 0.208405, -0.856506},
648         {-0.703064, -0.581085, -0.409821},
649         {-0.417206, -0.736328, 0.532623},
650         {-0.447876, -0.20285, -0.870728},
651         {0.086945, -0.990417, 0.107086},
652         {0.183685, 0.018341, -0.982788},
653         {0.560638, -0.428864, 0.708282},
654         {0.296722, -0.952576, -0.0672},
655         {0.135773, 0.990265, 0.030243},
656         {-0.068787, 0.654724, 0.752686},
657         {0.762604, -0.551758, 0.337585},
658         {-0.819611, -0.407684, 0.402466},
659         {-0.727844, -0.55072, -0.408539},
660         {-0.855774, -0.480011, 0.19281},
661         {0.693176, -0.079285, 0.716339},
662         {0.226013, 0.650116, -0.725433},
663         {0.246704, 0.953369, -0.173553},
664         {-0.970398, -0.239227, -0.03244},
665         {0.136383, -0.394318, 0.908752},
666         {0.813232, 0.558167, 0.164368},
667         {0.40451, 0.549042, -0.731323},
668         {-0.380249, -0.566711, 0.730865},
669         {0.022156, 0.932739, 0.359741},
670         {0.00824, 0.996552, -0.082306},
671         {0.956635, -0.065338, -0.283722},
672         {-0.743561, 0.008209, 0.668579},
673         {-0.859589, -0.509674, 0.035767},
674         {-0.852234, 0.363678, -0.375977},
675         {-0.201965, -0.970795, -0.12915},
676         {0.313477, 0.947327, 0.06546},
677         {-0.254028, -0.528259, 0.81015},
678         {0.628052, 0.601105, 0.49411},
679         {-0.494385, 0.868378, 0.037933},
680         {0.275635, -0.086426, 0.957336},
681         {-0.197937, 0.468903, -0.860748},
682         {0.895599, 0.399384, 0.195801},
683         {0.560791, 0.825012, -0.069214},
684         {0.304199, -0.849487, 0.43103},
685         {0.096375, 0.93576, 0.339111},
686         {-0.051422, 0.408966, -0.911072},
687         {0.330444, 0.942841, -0.042389},
688         {-0.452362, -0.786407, 0.420563},
689         {0.134308, -0.933472, -0.332489},
690         {0.80191, -0.566711, -0.188934},
691         {-0.987946, -0.105988, 0.112518},
692         {-0.24408, 0.892242, -0.379791},
693         {-0.920502, 0.229095, -0.316376},
694         {0.7789, 0.325958, 0.535706},
695         {-0.912872, 0.185211, -0.36377},
696         {-0.184784, 0.565369, -0.803833},
697         {-0.018463, 0.119537, 0.992615},
698         {-0.259247, -0.935608, 0.239532},
699         {-0.82373, -0.449127, -0.345947},
700         {-0.433105, 0.659515, 0.614349},
701         {-0.822754, 0.378845, -0.423676},
702         {0.687195, -0.674835, -0.26889},
703         {-0.246582, -0.800842, 0.545715},
704         {-0.729187, -0.207794, 0.651978},
705         {0.653534, -0.610443, -0.447388},
706         {0.492584, -0.023346, 0.869934},
707         {0.609039, 0.009094, -0.79306},
708         {0.962494, -0.271088, -0.00885},
709         {0.2659, -0.004913, 0.963959},
710         {0.651245, 0.553619, -0.518951},
711         {0.280548, -0.84314, 0.458618},
712         {-0.175293, -0.983215, 0.049805},
713         {0.035339, -0.979919, 0.196045},
714         {-0.982941, 0.164307, -0.082245},
715         {0.233734, -0.97226, -0.005005},
716         {-0.747253, -0.611328, 0.260437},
717         {0.645599, 0.592773, 0.481384},
718         {0.117706, -0.949524, -0.29068},
719         {-0.535004, -0.791901, -0.294312},
720         {-0.627167, -0.214447, 0.748718},
721         {-0.047974, -0.813477, -0.57959},
722         {-0.175537, 0.477264, -0.860992},
723         {0.738556, -0.414246, -0.53183},
724         {0.562561, -0.704071, 0.433289},
725         {-0.754944, 0.64801, -0.100586},
726         {0.114716, 0.044525, -0.992371},
727         {0.966003, 0.244873, -0.082764},
728         {0.33783, 0.715698, -0.611206},
729         {-0.944031, -0.326599, -0.045624},
730         {-0.101074, -0.416443, -0.903503},
731         {0.799286, 0.49411, -0.341949},
732         {-0.854645, 0.518036, 0.033936},
733         {0.42514, -0.437866, -0.792114},
734         {-0.358948, 0.597046, 0.717377},
735         {-0.985413, 0.144714, 0.089294},
736         {-0.601776, -0.33728, -0.723907},
737         {-0.449921, 0.594513, 0.666382},
738         {0.208313, -0.10791, 0.972076},
739         {0.575317, 0.060425, 0.815643},
740         {0.293365, -0.875702, -0.383453},
741         {0.293762, 0.465759, 0.834686},
742         {-0.846008, -0.233398, -0.47934},
743         {-0.115814, 0.143036, -0.98291},
744         {0.204681, -0.949036, -0.239532},
745         {0.946716, -0.263947, 0.184326},
746         {-0.235596, 0.573822, 0.784332},
747         {0.203705, -0.372253, -0.905487},
748         {0.756989, -0.651031, 0.055298},
749         {0.497803, 0.814697, -0.297363},
750         {-0.16214, 0.063995, -0.98468},
751         {-0.329254, 0.834381, 0.441925},
752         {0.703827, -0.527039, -0.476227},
753         {0.956421, 0.266113, 0.119781},
754         {0.480133, 0.482849, 0.7323},
755         {-0.18631, 0.961212, -0.203125},
756         {-0.748474, -0.656921, -0.090393},
757         {-0.085052, -0.165253, 0.982544},
758         {-0.76947, 0.628174, -0.115234},
759         {0.383148, 0.537659, 0.751068},
760         {0.616486, -0.668488, -0.415924},
761         {-0.259979, -0.630005, 0.73175},
762         {0.570953, -0.087952, 0.816223},
763         {-0.458008, 0.023254, 0.888611},
764         {-0.196167, 0.976563, -0.088287},
765         {-0.263885, -0.69812, -0.665527},
766         {0.437134, -0.892273, -0.112793},
767         {-0.621674, -0.230438, 0.748566},
768         {0.232422, 0.900574, -0.367249},
769         {0.22229, -0.796143, 0.562744},
770         {-0.665497, -0.73764, 0.11377},
771         {0.670135, 0.704803, 0.232605},
772         {0.895599, 0.429749, -0.114655},
773         {-0.11557, -0.474243, 0.872742},
774         {0.621826, 0.604004, -0.498444},
775         {-0.832214, 0.012756, 0.55426},
776         {-0.702484, 0.705994, -0.089661},
777         {-0.692017, 0.649292, 0.315399},
778         {-0.175995, -0.977997, 0.111877},
779         {0.096954, -0.04953, 0.994019},
780         {0.635284, -0.606689, -0.477783},
781         {-0.261261, -0.607422, -0.750153},
782         {0.983276, 0.165436, 0.075958},
783         {-0.29837, 0.404083, -0.864655},
784         {-0.638672, 0.507721, 0.578156},
785         {0.388214, 0.412079, 0.824249},
786         {0.556183, -0.208832, 0.804352},
787         {0.778442, 0.562012, 0.27951},
788         {-0.616577, 0.781921, -0.091522},
789         {0.196289, 0.051056, 0.979187},
790         {-0.121216, 0.207153, -0.970734},
791         {-0.173401, -0.384735, 0.906555},
792         {0.161499, -0.723236, -0.671387},
793         {0.178497, -0.006226, -0.983887},
794         {-0.126038, 0.15799, 0.97934},
795         {0.830475, -0.024811, 0.556458},
796         {-0.510132, -0.76944, 0.384247},
797         {0.81424, 0.200104, -0.544891},
798         {-0.112549, -0.393311, -0.912445},
799         {0.56189, 0.152222, -0.813049},
800         {0.198914, -0.254517, -0.946381},
801         {-0.41217, 0.690979, -0.593811},
802         {-0.407257, 0.324524, 0.853668},
803         {-0.690186, 0.366119, -0.624115},
804         {-0.428345, 0.844147, -0.322296},
805         {-0.21228, -0.297546, -0.930756},
806         {-0.273071, 0.516113, 0.811798},
807         {0.928314, 0.371643, 0.007233},
808         {0.785828, -0.479218, -0.390778},
809         {-0.704895, 0.058929, 0.706818},
810         {0.173248, 0.203583, 0.963562},
811         {0.422211, -0.904297, -0.062469},
812         {-0.363312, -0.182465, 0.913605},
813         {0.254028, -0.552307, -0.793945},
814         {-0.28891, -0.765747, -0.574554},
815         {0.058319, 0.291382, 0.954803},
816         {0.946136, -0.303925, 0.111267},
817         {-0.078156, 0.443695, -0.892731},
818         {0.182098, 0.89389, 0.409515},
819         {-0.680298, -0.213318, 0.701141},
820         {0.062469, 0.848389, -0.525635},
821         {-0.72879, -0.641846, 0.238342},
822         {-0.88089, 0.427673, 0.202637},
823         {-0.532501, -0.21405, 0.818878},
824         {0.948975, -0.305084, 0.07962},
825         {0.925446, 0.374664, 0.055817},
826         {0.820923, 0.565491, 0.079102},
827         {0.25882, 0.099792, -0.960724},
828         {-0.294617, 0.910522, 0.289978},
829         {0.137115, 0.320038, -0.937408},
830         {-0.908386, 0.345276, -0.235718},
831         {-0.936218, 0.138763, 0.322754},
832         {0.366577, 0.925934, -0.090637},
833         {0.309296, -0.686829, -0.657684},
834         {0.66983, 0.024445, 0.742065},
835         {-0.917999, -0.059113, -0.392059},
836         {0.365509, 0.462158, -0.807922},
837         {0.083374, 0.996399, -0.014801},
838         {0.593842, 0.253143, -0.763672},
839         {0.974976, -0.165466, 0.148285},
840         {0.918976, 0.137299, 0.369537},
841         {0.294952, 0.694977, 0.655731},
842         {0.943085, 0.152618, -0.295319},
843         {0.58783, -0.598236, 0.544495},
844         {0.203796, 0.678223, 0.705994},
845         {-0.478821, -0.661011, 0.577667},
846         {0.719055, -0.1698, -0.673828},
847         {-0.132172, -0.965332, 0.225006},
848         {-0.981873, -0.14502, 0.121979},
849         {0.763458, 0.579742, 0.284546},
850         {-0.893188, 0.079681, 0.442474},
851         {-0.795776, -0.523804, 0.303802},
852         {0.734955, 0.67804, -0.007446},
853         {0.15506, 0.986267, -0.056183},
854         {0.258026, 0.571503, -0.778931},
855         {-0.681549, -0.702087, -0.206116},
856         {-0.96286, -0.177185, 0.203613},
857         {-0.470978, -0.515106, 0.716095},
858         {-0.740326, 0.57135, 0.354095},
859         {-0.56012, -0.824982, -0.074982},
860         {-0.507874, 0.753204, 0.417969},
861         {-0.503113, 0.038147, 0.863342},
862         {0.594025, 0.673553, -0.439758},
863         {-0.119873, -0.005524, -0.992737},
864         {0.098267, -0.213776, 0.971893},
865         {-0.615631, 0.643951, 0.454163},
866         {0.896851, -0.441071, 0.032166},
867         {-0.555023, 0.750763, -0.358093},
868         {0.398773, 0.304688, 0.864929},
869         {-0.722961, 0.303589, 0.620544},
870         {-0.63559, -0.621948, -0.457306},
871         {-0.293243, 0.072327, 0.953278},
872         {-0.491638, 0.661041, -0.566772},
873         {-0.304199, -0.572083, -0.761688},
874         {0.908081, -0.398956, 0.127014},
875         {-0.523621, -0.549683, -0.650848},
876         {-0.932922, -0.19986, 0.299408},
877         {0.099426, 0.140869, 0.984985},
878         {-0.020325, -0.999756, -0.002319},
879         {0.952667, 0.280853, -0.11615},
880         {-0.971893, 0.082581, 0.220337},
881         {0.65921, 0.705292, -0.260651},
882         {0.733063, -0.175537, 0.657043},
883         {-0.555206, 0.429504, -0.712189},
884         {0.400421, -0.89859, 0.179352},
885         {0.750885, -0.19696, 0.630341},
886         {0.785675, -0.569336, 0.241821},
887         {-0.058899, -0.464111, 0.883789},
888         {0.129608, -0.94519, 0.299622},
889         {-0.357819, 0.907654, 0.219238},
890         {-0.842133, -0.439117, -0.312927},
891         {-0.313477, 0.84433, 0.434479},
892         {-0.241211, 0.053253, 0.968994},
893         {0.063873, 0.823273, 0.563965},
894         {0.476288, 0.862152, -0.172516},
895         {0.620941, -0.298126, 0.724915},
896         {0.25238, -0.749359, -0.612122},
897         {-0.577545, 0.386566, 0.718994},
898         {-0.406342, -0.737976, 0.538696},
899         {0.04718, 0.556305, 0.82959},
900         {-0.802856, 0.587463, 0.101166},
901         {-0.707733, -0.705963, 0.026428},
902         {0.374908, 0.68457, 0.625092},
903         {0.472137, 0.208405, -0.856506},
904         {-0.703064, -0.581085, -0.409821},
905         {-0.417206, -0.736328, 0.532623},
906         {-0.447876, -0.20285, -0.870728},
907         {0.086945, -0.990417, 0.107086},
908         {0.183685, 0.018341, -0.982788},
909         {0.560638, -0.428864, 0.708282},
910         {0.296722, -0.952576, -0.0672},
911         {0.135773, 0.990265, 0.030243},
912         {-0.068787, 0.654724, 0.752686},
913         {0.762604, -0.551758, 0.337585},
914         {-0.819611, -0.407684, 0.402466},
915         {-0.727844, -0.55072, -0.408539},
916         {-0.855774, -0.480011, 0.19281},
917         {0.693176, -0.079285, 0.716339},
918         {0.226013, 0.650116, -0.725433},
919         {0.246704, 0.953369, -0.173553},
920         {-0.970398, -0.239227, -0.03244},
921         {0.136383, -0.394318, 0.908752},
922         {0.813232, 0.558167, 0.164368},
923         {0.40451, 0.549042, -0.731323},
924         {-0.380249, -0.566711, 0.730865},
925         {0.022156, 0.932739, 0.359741},
926         {0.00824, 0.996552, -0.082306},
927         {0.956635, -0.065338, -0.283722},
928         {-0.743561, 0.008209, 0.668579},
929         {-0.859589, -0.509674, 0.035767},
930         {-0.852234, 0.363678, -0.375977},
931         {-0.201965, -0.970795, -0.12915},
932         {0.313477, 0.947327, 0.06546},
933         {-0.254028, -0.528259, 0.81015},
934         {0.628052, 0.601105, 0.49411},
935         {-0.494385, 0.868378, 0.037933},
936         {0.275635, -0.086426, 0.957336},
937         {-0.197937, 0.468903, -0.860748},
938         {0.895599, 0.399384, 0.195801},
939         {0.560791, 0.825012, -0.069214},
940         {0.304199, -0.849487, 0.43103},
941         {0.096375, 0.93576, 0.339111},
942         {-0.051422, 0.408966, -0.911072},
943         {0.330444, 0.942841, -0.042389},
944         {-0.452362, -0.786407, 0.420563},
945         {0.134308, -0.933472, -0.332489},
946         {0.80191, -0.566711, -0.188934},
947         {-0.987946, -0.105988, 0.112518},
948         {-0.24408, 0.892242, -0.379791},
949         {-0.920502, 0.229095, -0.316376},
950         {0.7789, 0.325958, 0.535706},
951         {-0.912872, 0.185211, -0.36377},
952         {-0.184784, 0.565369, -0.803833},
953         {-0.018463, 0.119537, 0.992615},
954         {-0.259247, -0.935608, 0.239532},
955         {-0.82373, -0.449127, -0.345947},
956         {-0.433105, 0.659515, 0.614349},
957         {-0.822754, 0.378845, -0.423676},
958         {0.687195, -0.674835, -0.26889},
959         {-0.246582, -0.800842, 0.545715},
960         {-0.729187, -0.207794, 0.651978},
961         {0.653534, -0.610443, -0.447388},
962         {0.492584, -0.023346, 0.869934},
963         {0.609039, 0.009094, -0.79306},
964         {0.962494, -0.271088, -0.00885},
965         {0.2659, -0.004913, 0.963959},
966         {0.651245, 0.553619, -0.518951},
967         {0.280548, -0.84314, 0.458618},
968         {-0.175293, -0.983215, 0.049805},
969         {0.035339, -0.979919, 0.196045},
970         {-0.982941, 0.164307, -0.082245},
971         {0.233734, -0.97226, -0.005005},
972         {-0.747253, -0.611328, 0.260437},
973         {0.645599, 0.592773, 0.481384},
974         {0.117706, -0.949524, -0.29068},
975         {-0.535004, -0.791901, -0.294312},
976         {-0.627167, -0.214447, 0.748718},
977         {-0.047974, -0.813477, -0.57959},
978         {-0.175537, 0.477264, -0.860992},
979         {0.738556, -0.414246, -0.53183},
980         {0.562561, -0.704071, 0.433289},
981         {-0.754944, 0.64801, -0.100586},
982         {0.114716, 0.044525, -0.992371},
983         {0.966003, 0.244873, -0.082764},
984         {0.33783, 0.715698, -0.611206},
985         {-0.944031, -0.326599, -0.045624},
986 };
987
988 #define SETUP(val, b0, b1, r0, r1)                                            \
989         {                                                                         \
990                 t = val + 10000.0f;                                                   \
991                 b0 = ((int)t) & 255;                                                  \
992                 b1 = (b0 + 1) & 255;                                                  \
993                 r0 = t - floorf(t);                                                   \
994                 r1 = r0 - 1.0f;                                                       \
995         } (void)0
996
997
998 static float noise3_perlin(float vec[3])
999 {
1000         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
1001         float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
1002         register int i, j;
1003
1004
1005         SETUP(vec[0],  bx0, bx1,  rx0, rx1);
1006         SETUP(vec[1],  by0, by1,  ry0, ry1);
1007         SETUP(vec[2],  bz0, bz1,  rz0, rz1);
1008
1009         i = p[bx0];
1010         j = p[bx1];
1011
1012         b00 = p[i + by0];
1013         b10 = p[j + by0];
1014         b01 = p[i + by1];
1015         b11 = p[j + by1];
1016
1017 #define VALUE_AT(rx, ry, rz) (rx * q[0] + ry * q[1] + rz * q[2])
1018 #define SURVE(t) (t * t * (3.0f - 2.0f * t))
1019
1020 /* lerp moved to improved perlin above */
1021
1022         sx = SURVE(rx0);
1023         sy = SURVE(ry0);
1024         sz = SURVE(rz0);
1025
1026
1027         q = g[b00 + bz0];
1028         u = VALUE_AT(rx0, ry0, rz0);
1029         q = g[b10 + bz0];
1030         v = VALUE_AT(rx1, ry0, rz0);
1031         a = lerp(sx, u, v);
1032
1033         q = g[b01 + bz0];
1034         u = VALUE_AT(rx0, ry1, rz0);
1035         q = g[b11 + bz0];
1036         v = VALUE_AT(rx1, ry1, rz0);
1037         b = lerp(sx, u, v);
1038
1039         c = lerp(sy, a, b);          /* interpolate in y at lo x */
1040
1041         q = g[b00 + bz1];
1042         u = VALUE_AT(rx0, ry0, rz1);
1043         q = g[b10 + bz1];
1044         v = VALUE_AT(rx1, ry0, rz1);
1045         a = lerp(sx, u, v);
1046
1047         q = g[b01 + bz1];
1048         u = VALUE_AT(rx0, ry1, rz1);
1049         q = g[b11 + bz1];
1050         v = VALUE_AT(rx1, ry1, rz1);
1051         b = lerp(sx, u, v);
1052
1053         d = lerp(sy, a, b);          /* interpolate in y at hi x */
1054
1055         return 1.5f * lerp(sz, c, d); /* interpolate in z */
1056
1057 #undef VALUE_AT
1058 #undef SURVE
1059 }
1060
1061 #if 0
1062 static float turbulence_perlin(float *point, float lofreq, float hifreq)
1063 {
1064         float freq, t, p[3];
1065
1066         p[0] = point[0] + 123.456;
1067         p[1] = point[1];
1068         p[2] = point[2];
1069
1070         t = 0;
1071         for (freq = lofreq; freq < hifreq; freq *= 2.0) {
1072                 t += fabsf(noise3_perlin(p)) / freq;
1073                 p[0] *= 2.0f;
1074                 p[1] *= 2.0f;
1075                 p[2] *= 2.0f;
1076         }
1077         return t - 0.3; /* readjust to make mean value = 0.0 */
1078 }
1079 #endif
1080
1081 /* for use with BLI_gNoise/gTurbulence, returns signed noise */
1082 static float orgPerlinNoise(float x, float y, float z)
1083 {
1084         float v[3];
1085
1086         v[0] = x;
1087         v[1] = y;
1088         v[2] = z;
1089         return noise3_perlin(v);
1090 }
1091
1092 /* for use with BLI_gNoise/gTurbulence, returns unsigned noise */
1093 static float orgPerlinNoiseU(float x, float y, float z)
1094 {
1095         float v[3];
1096
1097         v[0] = x;
1098         v[1] = y;
1099         v[2] = z;
1100         return (0.5f + 0.5f * noise3_perlin(v));
1101 }
1102
1103 /* *************** CALL AS: *************** */
1104
1105 float BLI_hnoisep(float noisesize, float x, float y, float z)
1106 {
1107         float vec[3];
1108
1109         vec[0] = x / noisesize;
1110         vec[1] = y / noisesize;
1111         vec[2] = z / noisesize;
1112
1113         return noise3_perlin(vec);
1114 }
1115
1116 #if 0
1117 static float turbulencep(float noisesize, float x, float y, float z, int nr)
1118 {
1119         float vec[3];
1120
1121         vec[0] = x / noisesize;
1122         vec[1] = y / noisesize;
1123         vec[2] = z / noisesize;
1124         nr++;
1125         return turbulence_perlin(vec, 1.0, (float)(1 << nr));
1126 }
1127 #endif
1128
1129 /******************/
1130 /* VORONOI/WORLEY */
1131 /******************/
1132
1133 /* distance metrics for voronoi, e parameter only used in Minkovsky */
1134 /* Camberra omitted, didn't seem useful */
1135
1136 /* distance squared */
1137 static float dist_Squared(float x, float y, float z, float e)
1138 {
1139         (void)e; return (x * x + y * y + z * z);
1140 }
1141 /* real distance */
1142 static float dist_Real(float x, float y, float z, float e)
1143 {
1144         (void)e; return sqrtf(x * x + y * y + z * z);
1145 }
1146 /* manhattan/taxicab/cityblock distance */
1147 static float dist_Manhattan(float x, float y, float z, float e)
1148 {
1149         (void)e; return (fabsf(x) + fabsf(y) + fabsf(z));
1150 }
1151 /* Chebychev */
1152 static float dist_Chebychev(float x, float y, float z, float e)
1153 {
1154         float t;
1155         (void)e;
1156
1157         x = fabsf(x);
1158         y = fabsf(y);
1159         z = fabsf(z);
1160         t = (x > y) ? x : y;
1161         return ((z > t) ? z : t);
1162 }
1163
1164 /* minkovsky preset exponent 0.5 */
1165 static float dist_MinkovskyH(float x, float y, float z, float e)
1166 {
1167         float d = sqrtf(fabsf(x)) + sqrtf(fabsf(y)) + sqrtf(fabsf(z));
1168         (void)e;
1169         return (d * d);
1170 }
1171
1172 /* minkovsky preset exponent 4 */
1173 static float dist_Minkovsky4(float x, float y, float z, float e)
1174 {
1175         (void)e;
1176         x *= x;
1177         y *= y;
1178         z *= z;
1179         return sqrtf(sqrtf(x * x + y * y + z * z));
1180 }
1181
1182 /* Minkovsky, general case, slow, maybe too slow to be useful */
1183 static float dist_Minkovsky(float x, float y, float z, float e)
1184 {
1185         return powf(powf(fabsf(x), e) + powf(fabsf(y), e) + powf(fabsf(z), e), 1.0f / e);
1186 }
1187
1188
1189 /* Not 'pure' Worley, but the results are virtually the same.
1190  * Returns distances in da and point coords in pa */
1191 void voronoi(float x, float y, float z, float *da, float *pa, float me, int dtype)
1192 {
1193         int xx, yy, zz, xi, yi, zi;
1194         float xd, yd, zd, d, *p;
1195
1196         float (*distfunc)(float, float, float, float);
1197         switch (dtype) {
1198                 case 1:
1199                         distfunc = dist_Squared;
1200                         break;
1201                 case 2:
1202                         distfunc = dist_Manhattan;
1203                         break;
1204                 case 3:
1205                         distfunc = dist_Chebychev;
1206                         break;
1207                 case 4:
1208                         distfunc = dist_MinkovskyH;
1209                         break;
1210                 case 5:
1211                         distfunc = dist_Minkovsky4;
1212                         break;
1213                 case 6:
1214                         distfunc = dist_Minkovsky;
1215                         break;
1216                 case 0:
1217                 default:
1218                         distfunc = dist_Real;
1219         }
1220
1221         xi = (int)(floor(x));
1222         yi = (int)(floor(y));
1223         zi = (int)(floor(z));
1224         da[0] = da[1] = da[2] = da[3] = 1e10f;
1225         for (xx = xi - 1; xx <= xi + 1; xx++) {
1226                 for (yy = yi - 1; yy <= yi + 1; yy++) {
1227                         for (zz = zi - 1; zz <= zi + 1; zz++) {
1228                                 p = HASHPNT(xx, yy, zz);
1229                                 xd = x - (p[0] + xx);
1230                                 yd = y - (p[1] + yy);
1231                                 zd = z - (p[2] + zz);
1232                                 d = distfunc(xd, yd, zd, me);
1233                                 if (d < da[0]) {
1234                                         da[3] = da[2];  da[2] = da[1];  da[1] = da[0];  da[0] = d;
1235                                         pa[9] = pa[6];  pa[10] = pa[7];  pa[11] = pa[8];
1236                                         pa[6] = pa[3];  pa[7] = pa[4];  pa[8] = pa[5];
1237                                         pa[3] = pa[0];  pa[4] = pa[1];  pa[5] = pa[2];
1238                                         pa[0] = p[0] + xx;  pa[1] = p[1] + yy;  pa[2] = p[2] + zz;
1239                                 }
1240                                 else if (d < da[1]) {
1241                                         da[3] = da[2];  da[2] = da[1];  da[1] = d;
1242                                         pa[9] = pa[6];  pa[10] = pa[7];  pa[11] = pa[8];
1243                                         pa[6] = pa[3];  pa[7] = pa[4];  pa[8] = pa[5];
1244                                         pa[3] = p[0] + xx;  pa[4] = p[1] + yy;  pa[5] = p[2] + zz;
1245                                 }
1246                                 else if (d < da[2]) {
1247                                         da[3] = da[2];  da[2] = d;
1248                                         pa[9] = pa[6];  pa[10] = pa[7];  pa[11] = pa[8];
1249                                         pa[6] = p[0] + xx;  pa[7] = p[1] + yy;  pa[8] = p[2] + zz;
1250                                 }
1251                                 else if (d < da[3]) {
1252                                         da[3] = d;
1253                                         pa[9] = p[0] + xx;  pa[10] = p[1] + yy;  pa[11] = p[2] + zz;
1254                                 }
1255                         }
1256                 }
1257         }
1258 }
1259
1260 /* returns different feature points for use in BLI_gNoise() */
1261 static float voronoi_F1(float x, float y, float z)
1262 {
1263         float da[4], pa[12];
1264         voronoi(x, y, z, da, pa, 1, 0);
1265         return da[0];
1266 }
1267
1268 static float voronoi_F2(float x, float y, float z)
1269 {
1270         float da[4], pa[12];
1271         voronoi(x, y, z, da, pa, 1, 0);
1272         return da[1];
1273 }
1274
1275 static float voronoi_F3(float x, float y, float z)
1276 {
1277         float da[4], pa[12];
1278         voronoi(x, y, z, da, pa, 1, 0);
1279         return da[2];
1280 }
1281
1282 static float voronoi_F4(float x, float y, float z)
1283 {
1284         float da[4], pa[12];
1285         voronoi(x, y, z, da, pa, 1, 0);
1286         return da[3];
1287 }
1288
1289 static float voronoi_F1F2(float x, float y, float z)
1290 {
1291         float da[4], pa[12];
1292         voronoi(x, y, z, da, pa, 1, 0);
1293         return (da[1] - da[0]);
1294 }
1295
1296 /* Crackle type pattern, just a scale/clamp of F2-F1 */
1297 static float voronoi_Cr(float x, float y, float z)
1298 {
1299         float t = 10 * voronoi_F1F2(x, y, z);
1300         if (t > 1.f) return 1.f;
1301         return t;
1302 }
1303
1304
1305 /* Signed version of all 6 of the above, just 2x-1, not really correct though (range is potentially (0, sqrt(6)).
1306  * Used in the musgrave functions */
1307 static float voronoi_F1S(float x, float y, float z)
1308 {
1309         float da[4], pa[12];
1310         voronoi(x, y, z, da, pa, 1, 0);
1311         return (2.0f * da[0] - 1.0f);
1312 }
1313
1314 static float voronoi_F2S(float x, float y, float z)
1315 {
1316         float da[4], pa[12];
1317         voronoi(x, y, z, da, pa, 1, 0);
1318         return (2.0f * da[1] - 1.0f);
1319 }
1320
1321 static float voronoi_F3S(float x, float y, float z)
1322 {
1323         float da[4], pa[12];
1324         voronoi(x, y, z, da, pa, 1, 0);
1325         return (2.0f * da[2] - 1.0f);
1326 }
1327
1328 static float voronoi_F4S(float x, float y, float z)
1329 {
1330         float da[4], pa[12];
1331         voronoi(x, y, z, da, pa, 1, 0);
1332         return (2.0f * da[3] - 1.0f);
1333 }
1334
1335 static float voronoi_F1F2S(float x, float y, float z)
1336 {
1337         float da[4], pa[12];
1338         voronoi(x, y, z, da, pa, 1, 0);
1339         return (2.0f * (da[1] - da[0]) - 1.0f);
1340 }
1341
1342 /* Crackle type pattern, just a scale/clamp of F2-F1 */
1343 static float voronoi_CrS(float x, float y, float z)
1344 {
1345         float t = 10 * voronoi_F1F2(x, y, z);
1346         if (t > 1.f) return 1.f;
1347         return (2.0f * t - 1.0f);
1348 }
1349
1350
1351 /***************/
1352 /* voronoi end */
1353 /***************/
1354
1355 /*************/
1356 /* CELLNOISE */
1357 /*************/
1358
1359 /* returns unsigned cellnoise */
1360 static float cellNoiseU(float x, float y, float z)
1361 {
1362         int xi = (int)(floor(x));
1363         int yi = (int)(floor(y));
1364         int zi = (int)(floor(z));
1365         unsigned int n = xi + yi * 1301 + zi * 314159;
1366         n ^= (n << 13);
1367         return ((float)(n * (n * n * 15731 + 789221) + 1376312589) / 4294967296.0f);
1368 }
1369
1370 /* idem, signed */
1371 float cellNoise(float x, float y, float z)
1372 {
1373         return (2.0f * cellNoiseU(x, y, z) - 1.0f);
1374 }
1375
1376 /* returns a vector/point/color in ca, using point hasharray directly */
1377 void cellNoiseV(float x, float y, float z, float *ca)
1378 {
1379         int xi = (int)(floor(x));
1380         int yi = (int)(floor(y));
1381         int zi = (int)(floor(z));
1382         float *p = HASHPNT(xi, yi, zi);
1383         ca[0] = p[0];
1384         ca[1] = p[1];
1385         ca[2] = p[2];
1386 }
1387
1388
1389 /*****************/
1390 /* end cellnoise */
1391 /*****************/
1392
1393 /* newnoise: generic noise function for use with different noisebases */
1394 float BLI_gNoise(float noisesize, float x, float y, float z, int hard, int noisebasis)
1395 {
1396         float (*noisefunc)(float, float, float);
1397
1398         switch (noisebasis) {
1399                 case 1:
1400                         noisefunc = orgPerlinNoiseU;
1401                         break;
1402                 case 2:
1403                         noisefunc = newPerlinU;
1404                         break;
1405                 case 3:
1406                         noisefunc = voronoi_F1;
1407                         break;
1408                 case 4:
1409                         noisefunc = voronoi_F2;
1410                         break;
1411                 case 5:
1412                         noisefunc = voronoi_F3;
1413                         break;
1414                 case 6:
1415                         noisefunc = voronoi_F4;
1416                         break;
1417                 case 7:
1418                         noisefunc = voronoi_F1F2;
1419                         break;
1420                 case 8:
1421                         noisefunc = voronoi_Cr;
1422                         break;
1423                 case 14:
1424                         noisefunc = cellNoiseU;
1425                         break;
1426                 case 0:
1427                 default: {
1428                         noisefunc = orgBlenderNoise;
1429                         /* add one to make return value same as BLI_hnoise */
1430                         x += 1;
1431                         y += 1;
1432                         z += 1;
1433                 }
1434         }
1435
1436         if (noisesize != 0.0f) {
1437                 noisesize = 1.0f / noisesize;
1438                 x *= noisesize;
1439                 y *= noisesize;
1440                 z *= noisesize;
1441         }
1442         
1443         if (hard) return fabsf(2.0f * noisefunc(x, y, z) - 1.0f);
1444         return noisefunc(x, y, z);
1445 }
1446
1447 /* newnoise: generic turbulence function for use with different noisebasis */
1448 float BLI_gTurbulence(float noisesize, float x, float y, float z, int oct, int hard, int noisebasis)
1449 {
1450         float (*noisefunc)(float, float, float);
1451         float sum, t, amp = 1, fscale = 1;
1452         int i;
1453         
1454         switch (noisebasis) {
1455                 case 1:
1456                         noisefunc = orgPerlinNoiseU;
1457                         break;
1458                 case 2:
1459                         noisefunc = newPerlinU;
1460                         break;
1461                 case 3:
1462                         noisefunc = voronoi_F1;
1463                         break;
1464                 case 4:
1465                         noisefunc = voronoi_F2;
1466                         break;
1467                 case 5:
1468                         noisefunc = voronoi_F3;
1469                         break;
1470                 case 6:
1471                         noisefunc = voronoi_F4;
1472                         break;
1473                 case 7:
1474                         noisefunc = voronoi_F1F2;
1475                         break;
1476                 case 8:
1477                         noisefunc = voronoi_Cr;
1478                         break;
1479                 case 14:
1480                         noisefunc = cellNoiseU;
1481                         break;
1482                 case 0:
1483                 default:
1484                         noisefunc = orgBlenderNoise;
1485                         x += 1;
1486                         y += 1;
1487                         z += 1;
1488         }
1489
1490         if (noisesize != 0.0f) {
1491                 noisesize = 1.0f / noisesize;
1492                 x *= noisesize;
1493                 y *= noisesize;
1494                 z *= noisesize;
1495         }
1496
1497         sum = 0;
1498         for (i = 0; i <= oct; i++, amp *= 0.5f, fscale *= 2.0f) {
1499                 t = noisefunc(fscale * x, fscale * y, fscale * z);
1500                 if (hard) t = fabsf(2.0f * t - 1.0f);
1501                 sum += t * amp;
1502         }
1503         
1504         sum *= ((float)(1 << oct) / (float)((1 << (oct + 1)) - 1));
1505
1506         return sum;
1507
1508 }
1509
1510
1511 /*
1512  * The following code is based on Ken Musgrave's explanations and sample
1513  * source code in the book "Texturing and Modelling: A procedural approach"
1514  */
1515
1516 /*
1517  * Procedural fBm evaluated at "point"; returns value stored in "value".
1518  *
1519  * Parameters:
1520  *    ``H''  is the fractal increment parameter
1521  *    ``lacunarity''  is the gap between successive frequencies
1522  *    ``octaves''  is the number of frequencies in the fBm
1523  */
1524 float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
1525 {
1526         float rmd, value = 0.0, pwr = 1.0, pwHL = powf(lacunarity, -H);
1527         int i;
1528
1529         float (*noisefunc)(float, float, float);
1530         switch (noisebasis) {
1531                 case 1:
1532                         noisefunc = orgPerlinNoise;
1533                         break;
1534                 case 2:
1535                         noisefunc = newPerlin;
1536                         break;
1537                 case 3:
1538                         noisefunc = voronoi_F1S;
1539                         break;
1540                 case 4:
1541                         noisefunc = voronoi_F2S;
1542                         break;
1543                 case 5:
1544                         noisefunc = voronoi_F3S;
1545                         break;
1546                 case 6:
1547                         noisefunc = voronoi_F4S;
1548                         break;
1549                 case 7:
1550                         noisefunc = voronoi_F1F2S;
1551                         break;
1552                 case 8:
1553                         noisefunc = voronoi_CrS;
1554                         break;
1555                 case 14:
1556                         noisefunc = cellNoise;
1557                         break;
1558                 case 0:
1559                 default: {
1560                         noisefunc = orgBlenderNoiseS;
1561                 }
1562         }
1563         
1564         for (i = 0; i < (int)octaves; i++) {
1565                 value += noisefunc(x, y, z) * pwr;
1566                 pwr *= pwHL;
1567                 x *= lacunarity;
1568                 y *= lacunarity;
1569                 z *= lacunarity;
1570         }
1571
1572         rmd = octaves - floorf(octaves);
1573         if (rmd != 0.f) value += rmd * noisefunc(x, y, z) * pwr;
1574
1575         return value;
1576
1577 } /* fBm() */
1578
1579
1580 /*
1581  * Procedural multifractal evaluated at "point";
1582  * returns value stored in "value".
1583  *
1584  * Parameters:
1585  *    ``H''  determines the highest fractal dimension
1586  *    ``lacunarity''  is gap between successive frequencies
1587  *    ``octaves''  is the number of frequencies in the fBm
1588  *    ``offset''  is the zero offset, which determines multifractality (NOT USED??)
1589  */
1590
1591 /* this one is in fact rather confusing,
1592  * there seem to be errors in the original source code (in all three versions of proc.text&mod),
1593  * I modified it to something that made sense to me, so it might be wrong... */
1594 float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
1595 {
1596         float rmd, value = 1.0, pwr = 1.0, pwHL = powf(lacunarity, -H);
1597         int i;
1598
1599         float (*noisefunc)(float, float, float);
1600         switch (noisebasis) {
1601                 case 1:
1602                         noisefunc = orgPerlinNoise;
1603                         break;
1604                 case 2:
1605                         noisefunc = newPerlin;
1606                         break;
1607                 case 3:
1608                         noisefunc = voronoi_F1S;
1609                         break;
1610                 case 4:
1611                         noisefunc = voronoi_F2S;
1612                         break;
1613                 case 5:
1614                         noisefunc = voronoi_F3S;
1615                         break;
1616                 case 6:
1617                         noisefunc = voronoi_F4S;
1618                         break;
1619                 case 7:
1620                         noisefunc = voronoi_F1F2S;
1621                         break;
1622                 case 8:
1623                         noisefunc = voronoi_CrS;
1624                         break;
1625                 case 14:
1626                         noisefunc = cellNoise;
1627                         break;
1628                 case 0:
1629                 default: {
1630                         noisefunc = orgBlenderNoiseS;
1631                 }
1632         }
1633
1634         for (i = 0; i < (int)octaves; i++) {
1635                 value *= (pwr * noisefunc(x, y, z) + 1.0f);
1636                 pwr *= pwHL;
1637                 x *= lacunarity;
1638                 y *= lacunarity;
1639                 z *= lacunarity;
1640         }
1641         rmd = octaves - floorf(octaves);
1642         if (rmd != 0.0f) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0f);
1643
1644         return value;
1645
1646 } /* multifractal() */
1647
1648 /*
1649  * Heterogeneous procedural terrain function: stats by altitude method.
1650  * Evaluated at "point"; returns value stored in "value".
1651  *
1652  * Parameters:
1653  *       ``H''  determines the fractal dimension of the roughest areas
1654  *       ``lacunarity''  is the gap between successive frequencies
1655  *       ``octaves''  is the number of frequencies in the fBm
1656  *       ``offset''  raises the terrain from `sea level'
1657  */
1658 float mg_HeteroTerrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis)
1659 {
1660         float value, increment, rmd;
1661         int i;
1662         float pwHL = powf(lacunarity, -H);
1663         float pwr = pwHL;   /* starts with i=1 instead of 0 */
1664
1665         float (*noisefunc)(float, float, float);
1666         switch (noisebasis) {
1667                 case 1:
1668                         noisefunc = orgPerlinNoise;
1669                         break;
1670                 case 2:
1671                         noisefunc = newPerlin;
1672                         break;
1673                 case 3:
1674                         noisefunc = voronoi_F1S;
1675                         break;
1676                 case 4:
1677                         noisefunc = voronoi_F2S;
1678                         break;
1679                 case 5:
1680                         noisefunc = voronoi_F3S;
1681                         break;
1682                 case 6:
1683                         noisefunc = voronoi_F4S;
1684                         break;
1685                 case 7:
1686                         noisefunc = voronoi_F1F2S;
1687                         break;
1688                 case 8:
1689                         noisefunc = voronoi_CrS;
1690                         break;
1691                 case 14:
1692                         noisefunc = cellNoise;
1693                         break;
1694                 case 0:
1695                 default: {
1696                         noisefunc = orgBlenderNoiseS;
1697                 }
1698         }
1699
1700         /* first unscaled octave of function; later octaves are scaled */
1701         value = offset + noisefunc(x, y, z);
1702         x *= lacunarity;
1703         y *= lacunarity;
1704         z *= lacunarity;
1705
1706         for (i = 1; i < (int)octaves; i++) {
1707                 increment = (noisefunc(x, y, z) + offset) * pwr * value;
1708                 value += increment;
1709                 pwr *= pwHL;
1710                 x *= lacunarity;
1711                 y *= lacunarity;
1712                 z *= lacunarity;
1713         }
1714
1715         rmd = octaves - floorf(octaves);
1716         if (rmd != 0.0f) {
1717                 increment = (noisefunc(x, y, z) + offset) * pwr * value;
1718                 value += rmd * increment;
1719         }
1720         return value;
1721 }
1722
1723
1724 /* Hybrid additive/multiplicative multifractal terrain model.
1725  *
1726  * Some good parameter values to start with:
1727  *
1728  *      H:           0.25
1729  *      offset:      0.7
1730  */
1731 float mg_HybridMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
1732 {
1733         float result, signal, weight, rmd;
1734         int i;
1735         float pwHL = powf(lacunarity, -H);
1736         float pwr = pwHL;   /* starts with i=1 instead of 0 */
1737         float (*noisefunc)(float, float, float);
1738
1739         switch (noisebasis) {
1740                 case 1:
1741                         noisefunc = orgPerlinNoise;
1742                         break;
1743                 case 2:
1744                         noisefunc = newPerlin;
1745                         break;
1746                 case 3:
1747                         noisefunc = voronoi_F1S;
1748                         break;
1749                 case 4:
1750                         noisefunc = voronoi_F2S;
1751                         break;
1752                 case 5:
1753                         noisefunc = voronoi_F3S;
1754                         break;
1755                 case 6:
1756                         noisefunc = voronoi_F4S;
1757                         break;
1758                 case 7:
1759                         noisefunc = voronoi_F1F2S;
1760                         break;
1761                 case 8:
1762                         noisefunc = voronoi_CrS;
1763                         break;
1764                 case 14:
1765                         noisefunc = cellNoise;
1766                         break;
1767                 case 0:
1768                 default: {
1769                         noisefunc = orgBlenderNoiseS;
1770                 }
1771         }
1772
1773         result = noisefunc(x, y, z) + offset;
1774         weight = gain * result;
1775         x *= lacunarity;
1776         y *= lacunarity;
1777         z *= lacunarity;
1778
1779         for (i = 1; (weight > 0.001f) && (i < (int)octaves); i++) {
1780                 if (weight > 1.0f) weight = 1.0f;
1781                 signal = (noisefunc(x, y, z) + offset) * pwr;
1782                 pwr *= pwHL;
1783                 result += weight * signal;
1784                 weight *= gain * signal;
1785                 x *= lacunarity;
1786                 y *= lacunarity;
1787                 z *= lacunarity;
1788         }
1789
1790         rmd = octaves - floorf(octaves);
1791         if (rmd != 0.f) result += rmd * ((noisefunc(x, y, z) + offset) * pwr);
1792
1793         return result;
1794
1795 } /* HybridMultifractal() */
1796
1797
1798 /* Ridged multifractal terrain model.
1799  *
1800  * Some good parameter values to start with:
1801  *
1802  *      H:           1.0
1803  *      offset:      1.0
1804  *      gain:        2.0
1805  */
1806 float mg_RidgedMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
1807 {
1808         float result, signal, weight;
1809         int i;
1810         float pwHL = powf(lacunarity, -H);
1811         float pwr = pwHL;   /* starts with i=1 instead of 0 */
1812         
1813         float (*noisefunc)(float, float, float);
1814         switch (noisebasis) {
1815                 case 1:
1816                         noisefunc = orgPerlinNoise;
1817                         break;
1818                 case 2:
1819                         noisefunc = newPerlin;
1820                         break;
1821                 case 3:
1822                         noisefunc = voronoi_F1S;
1823                         break;
1824                 case 4:
1825                         noisefunc = voronoi_F2S;
1826                         break;
1827                 case 5:
1828                         noisefunc = voronoi_F3S;
1829                         break;
1830                 case 6:
1831                         noisefunc = voronoi_F4S;
1832                         break;
1833                 case 7:
1834                         noisefunc = voronoi_F1F2S;
1835                         break;
1836                 case 8:
1837                         noisefunc = voronoi_CrS;
1838                         break;
1839                 case 14:
1840                         noisefunc = cellNoise;
1841                         break;
1842                 case 0:
1843                 default: {
1844                         noisefunc = orgBlenderNoiseS;
1845                 }
1846         }
1847
1848         signal = offset - fabsf(noisefunc(x, y, z));
1849         signal *= signal;
1850         result = signal;
1851
1852
1853         for (i = 1; i < (int)octaves; i++) {
1854                 x *= lacunarity;
1855                 y *= lacunarity;
1856                 z *= lacunarity;
1857                 weight = signal * gain;
1858                 if (weight > 1.0f) weight = 1.0f; else if (weight < 0.0f) weight = 0.0f;
1859                 signal = offset - fabsf(noisefunc(x, y, z));
1860                 signal *= signal;
1861                 signal *= weight;
1862                 result += signal * pwr;
1863                 pwr *= pwHL;
1864         }
1865
1866         return result;
1867 } /* RidgedMultifractal() */
1868
1869 /* "Variable Lacunarity Noise"
1870  * A distorted variety of Perlin noise.
1871  */
1872 float mg_VLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2)
1873 {
1874         float rv[3];
1875         float (*noisefunc1)(float, float, float);
1876         float (*noisefunc2)(float, float, float);
1877
1878         switch (nbas1) {
1879                 case 1:
1880                         noisefunc1 = orgPerlinNoise;
1881                         break;
1882                 case 2:
1883                         noisefunc1 = newPerlin;
1884                         break;
1885                 case 3:
1886                         noisefunc1 = voronoi_F1S;
1887                         break;
1888                 case 4:
1889                         noisefunc1 = voronoi_F2S;
1890                         break;
1891                 case 5:
1892                         noisefunc1 = voronoi_F3S;
1893                         break;
1894                 case 6:
1895                         noisefunc1 = voronoi_F4S;
1896                         break;
1897                 case 7:
1898                         noisefunc1 = voronoi_F1F2S;
1899                         break;
1900                 case 8:
1901                         noisefunc1 = voronoi_CrS;
1902                         break;
1903                 case 14:
1904                         noisefunc1 = cellNoise;
1905                         break;
1906                 case 0:
1907                 default: {
1908                         noisefunc1 = orgBlenderNoiseS;
1909                 }
1910         }
1911
1912         switch (nbas2) {
1913                 case 1:
1914                         noisefunc2 = orgPerlinNoise;
1915                         break;
1916                 case 2:
1917                         noisefunc2 = newPerlin;
1918                         break;
1919                 case 3:
1920                         noisefunc2 = voronoi_F1S;
1921                         break;
1922                 case 4:
1923                         noisefunc2 = voronoi_F2S;
1924                         break;
1925                 case 5:
1926                         noisefunc2 = voronoi_F3S;
1927                         break;
1928                 case 6:
1929                         noisefunc2 = voronoi_F4S;
1930                         break;
1931                 case 7:
1932                         noisefunc2 = voronoi_F1F2S;
1933                         break;
1934                 case 8:
1935                         noisefunc2 = voronoi_CrS;
1936                         break;
1937                 case 14:
1938                         noisefunc2 = cellNoise;
1939                         break;
1940                 case 0:
1941                 default: {
1942                         noisefunc2 = orgBlenderNoiseS;
1943                 }
1944         }
1945
1946         /* get a random vector and scale the randomization */
1947         rv[0] = noisefunc1(x + 13.5f, y + 13.5f, z + 13.5f) * distortion;
1948         rv[1] = noisefunc1(x, y, z) * distortion;
1949         rv[2] = noisefunc1(x - 13.5f, y - 13.5f, z - 13.5f) * distortion;
1950         return noisefunc2(x + rv[0], y + rv[1], z + rv[2]);   /* distorted-domain noise */
1951 }
1952
1953 /****************/
1954 /* musgrave end */
1955 /****************/