001/*
002 * Derived from Joise, which is derived from the Accidental Noise Library.
003 * Licenses for these projects are as follows
004 * 
005 * ============================================================================
006 * | Joise
007 * ============================================================================
008 * 
009 * Copyright (C) 2016 Jason Taylor
010 * 
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 *      http://www.apache.org/licenses/LICENSE-2.0
016 * 
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 * 
023 * ============================================================================
024 * | Accidental Noise Library
025 * | --------------------------------------------------------------------------
026 * | Joise is a derivative work based on Josua Tippetts' C++ library:
027 * | http://accidentalnoise.sourceforge.net/index.html
028 * ============================================================================
029 * 
030 * Copyright (C) 2011 Joshua Tippetts
031 * 
032 *   This software is provided 'as-is', without any express or implied
033 *   warranty.  In no event will the authors be held liable for any damages
034 *   arising from the use of this software.
035 * 
036 *   Permission is granted to anyone to use this software for any purpose,
037 *   including commercial applications, and to alter it and redistribute it
038 *   freely, subject to the following restrictions:
039 * 
040 *   1. The origin of this software must not be misrepresented; you must not
041 *      claim that you wrote the original software. If you use this software
042 *      in a product, an acknowledgment in the product documentation would be
043 *      appreciated but is not required.
044 *   2. Altered source versions must be plainly marked as such, and must not be
045 *      misrepresented as being the original software.
046 *   3. This notice may not be removed or altered from any source distribution.
047 */
048package squidpony.squidmath;
049
050import static squidpony.squidmath.HastyPointHash.hash256;
051import static squidpony.squidmath.Noise.fastFloor;
052
053/**
054 * More advanced noise functions, in 2D, 3D, 4D, and 6D, with the last two as options for generating seamlessly-tiling
055 * noise using {@link Noise#seamless2D(double[][], long, int, Noise.Noise4D)} and/or
056 * {@link Noise#seamless3D(double[][][], long, int, Noise.Noise6D)}. All functions can take a long seed that should
057 * significantly change the pattern of noise produced. Incorporates code from Joise; the full library is available at
058 * https://github.com/SudoPlayGames/Joise , and this class adds rather significant optimization in a few methods,
059 * especially 6D noise. Joise is derived from the Accidental Noise Library, available in C++ at
060 * http://accidentalnoise.sourceforge.net/index.html . Both Joise and ANL have many features that SquidLib has not (yet)
061 * incorporated, but now that SquidLib has seamless noise, that's a nice feature that would have needed Joise before.
062 */
063public class SeededNoise implements Noise.Noise2D, Noise.Noise3D, Noise.Noise4D, Noise.Noise6D {
064
065    /**
066     * 256 2-element gradient vectors formed from the cos and sin of increasing multiples of the inverse of phi, the
067     * golden ratio, while also adding increasing multiples of 2/3 of the reciprocal of {@link Math#E}. This produces
068     * a sequence with remarkably low overlap possible from nearby angles, distributed nicely around the unit circle.
069     * For i from 1 to 256 inclusive, this gets the cosine and sine of an angle in radians of
070     * {@code 0.61803398874989484820458683436563811772 * i + (i / (1.5 * 2.7182818284590452354))}. This is expected to
071     * be accessed using an 8-bit int (depending on how you got your int, the normal way to get 8 bits would be
072     * {@code index & 255}), but smaller numbers should also work down to about 4 bits (typically using
073     * {@code index & 15}).
074     */
075    public static final double[][] phiGrad2 = {
076            {0.6499429579167653, 0.759982994187637},
077            {-0.1551483029088119, 0.9878911904175052},
078            {-0.8516180517334043, 0.5241628506120981},
079            {-0.9518580082090311, -0.30653928330368374},
080            {-0.38568876701087174, -0.9226289476282616},
081            {0.4505066120763985, -0.8927730912586049},
082            {0.9712959670388622, -0.23787421973396244},
083            {0.8120673355833279, 0.5835637432865366},
084            {0.08429892519436613, 0.9964405106232257},
085            {-0.702488350003267, 0.7116952424385647},
086            {-0.9974536374007479, -0.07131788861160528},
087            {-0.5940875849508908, -0.804400361391775},
088            {0.2252075529515288, -0.9743108118529653},
089            {0.8868317111719171, -0.4620925405802277},
090            {0.9275724981153959, 0.373643226540993},
091            {0.3189067150428103, 0.9477861083074618},
092            {-0.5130301507665112, 0.8583705868705491},
093            {-0.9857873824221494, 0.1679977281313266},
094            {-0.7683809836504446, -0.6399927061806058},
095            {-0.013020236219374872, -0.9999152331316848},
096            {0.7514561619680513, -0.6597830223946701},
097            {0.9898275175279653, 0.14227257481477412},
098            {0.5352066871710182, 0.8447211386057674},
099            {-0.29411988281443646, 0.9557685360657266},
100            {-0.9175289804081126, 0.39766892022290273},
101            {-0.8985631161871687, -0.43884430750324743},
102            {-0.2505005588110731, -0.968116454790094},
103            {0.5729409678802212, -0.8195966369650838},
104            {0.9952584535626074, -0.09726567026534665},
105            {0.7207814785200723, 0.6931623620930514},
106            {-0.05832476124070039, 0.998297662136006},
107            {-0.7965970142012075, 0.6045107087270838},
108            {-0.977160478114496, -0.21250270589112422},
109            {-0.4736001288089817, -0.8807399831914728},
110            {0.36153434093875386, -0.9323587937709286},
111            {0.9435535266854258, -0.3312200813348966},
112            {0.8649775992346886, 0.5018104750024599},
113            {0.1808186720712497, 0.9835164502083277},
114            {-0.6299339540895539, 0.7766487066139361},
115            {-0.9996609468975833, 0.02603826506945166},
116            {-0.6695112313914258, -0.7428019325774111},
117            {0.12937272671950842, -0.9915960354807594},
118            {0.8376810167470904, -0.5461597881403947},
119            {0.959517028911149, 0.28165061908243916},
120            {0.4095816551369482, 0.9122734610714476},
121            {-0.42710760401484793, 0.9042008043530463},
122            {-0.9647728141412515, 0.2630844295924223},
123            {-0.8269869890664444, -0.562221059650754},
124            {-0.11021592552380209, -0.9939076666174438},
125            {0.6837188597775012, -0.72974551782423},
126            {0.998972441738333, 0.04532174585508431},
127            {0.6148313475439905, 0.7886586169422362},
128            {-0.1997618324529528, 0.9798444827088829},
129            {-0.8744989400706802, 0.48502742583822706},
130            {-0.9369870231562731, -0.3493641630687752},
131            {-0.3434772946489506, -0.9391609809082988},
132            {0.4905057254335028, -0.8714379687143274},
133            {0.9810787787756657, -0.1936089611460388},
134            {0.7847847614201463, 0.6197684069414349},
135            {0.03905187955516296, 0.9992371844077906},
136            {-0.7340217731995672, 0.6791259356474049},
137            {-0.9931964444524306, -0.1164509455824639},
138            {-0.5570202966000876, -0.830498879695542},
139            {0.2691336060685578, -0.9631028512493016},
140            {0.9068632806061, -0.4214249521425399},
141            {0.9096851999779008, 0.4152984913783901},
142            {0.27562369868737335, 0.9612656119522284},
143            {-0.5514058359842319, 0.8342371389734039},
144            {-0.9923883787916933, 0.12314749546456379},
145            {-0.7385858406439617, -0.6741594440488484},
146            {0.032311046904542805, -0.9994778618098213},
147            {0.7805865154410089, -0.6250477517051506},
148            {0.9823623706068018, 0.18698709264487903},
149            {0.49637249435561115, 0.8681096398768929},
150            {-0.3371347561867868, 0.9414564016304079},
151            {-0.9346092156607797, 0.35567627697379833},
152            {-0.877750600058892, -0.47911781859606817},
153            {-0.20636642697019966, -0.9784747813917093},
154            {0.6094977881394418, -0.7927877687333024},
155            {0.998644017504346, -0.052058873429796634},
156            {0.6886255051458764, 0.7251171723677399},
157            {-0.10350942208147358, 0.9946284731196666},
158            {-0.8231759450656516, 0.567786371327519},
159            {-0.9665253951623188, -0.2565709658288005},
160            {-0.43319680340129196, -0.9012993562201753},
161            {0.4034189716368784, -0.9150153732716426},
162            {0.9575954428121146, -0.28811624026678895},
163            {0.8413458575409575, 0.5404971304259356},
164            {0.13605818775026976, 0.9907008476558967},
165            {-0.664485735550556, 0.7473009482463117},
166            {-0.999813836664718, -0.01929487014147803},
167            {-0.6351581891853917, -0.7723820781910558},
168            {0.17418065221630152, -0.984713714941304},
169            {0.8615731658120597, -0.5076334109892543},
170            {0.945766171482902, 0.32484819358982736},
171            {0.3678149601703667, 0.9298990026206456},
172            {-0.4676486851245607, 0.883914423064399},
173            {-0.9757048995218635, 0.2190889067228882},
174            {-0.8006563717736747, -0.5991238388999518},
175            {-0.06505704156910719, -0.9978815467490495},
176            {0.716089639712196, -0.6980083293893113},
177            {0.9958918787052943, 0.09055035024139549},
178            {0.5784561871098056, 0.8157134543418942},
179            {-0.24396482815448167, 0.9697840804135497},
180            {-0.8955826311865743, 0.4448952131872543},
181            {-0.9201904205900768, -0.39147105876968413},
182            {-0.3005599364234082, -0.9537629289384008},
183            {0.5294967923694863, -0.84831193960148},
184            {0.9888453593035162, -0.1489458135829932},
185            {0.7558893631265085, 0.6546993743025888},
186            {-0.006275422246980369, 0.9999803093439501},
187            {-0.764046696121276, 0.6451609459244744},
188            {-0.9868981170802014, -0.16134468229090512},
189            {-0.5188082666339063, -0.8548906260290385},
190            {0.31250655826478446, -0.9499156020623616},
191            {0.9250311403279032, -0.3798912863223621},
192            {0.889928392754896, 0.45610026942404636},
193            {0.2317742435145519, 0.9727696027545563},
194            {-0.5886483179573486, 0.8083892365475831},
195            {-0.996949901406418, 0.0780441803450664},
196            {-0.707272817672466, -0.7069407057042696},
197            {0.07757592706207364, -0.9969864470194466},
198            {0.8081126726681943, -0.5890279350532263},
199            {0.9728783545459001, 0.23131733021125322},
200            {0.4565181982253288, 0.8897140746830408},
201            {-0.3794567783511009, 0.9252094645881026},
202            {-0.9497687200714887, 0.31295267753091066},
203            {-0.8551342041690687, -0.5184066867432686},
204            {-0.16180818807538452, -0.9868222283024238},
205            {0.6448020194233159, -0.7643496292585048},
206            {0.9999772516247822, -0.006745089543285545},
207            {0.6550543261176665, 0.7555817823601425},
208            {-0.14848135899860646, 0.9889152066936411},
209            {-0.848063153443784, 0.5298951667745091},
210            {-0.9539039899003245, -0.300111942535184},
211            {-0.3919032080850608, -0.9200064540494471},
212            {0.44447452934057863, -0.8957914895596358},
213            {0.9696693887216105, -0.24442028675267172},
214            {0.8159850520735595, 0.5780730012658526},
215            {0.0910180879994953, 0.9958492394217692},
216            {-0.6976719213969089, 0.7164173993520435},
217            {-0.9979119924958648, -0.06458835214597858},
218            {-0.5994998228898376, -0.8003748886334786},
219            {0.2186306161766729, -0.9758076929755208},
220            {0.8836946816279001, -0.46806378802740584},
221            {0.9300716543684309, 0.36737816720699407},
222            {0.32529236260160294, 0.9456134933645286},
223            {-0.5072286936943775, 0.8618114946396893},
224            {-0.9846317976415725, 0.17464313062106204},
225            {-0.7726803123417516, -0.6347953488483143},
226            {-0.019764457813331488, -0.9998046640256011},
227            {0.7469887719961158, -0.6648366525032559},
228            {0.9907646418168752, 0.13559286310672486},
229            {0.5408922318074902, 0.8410919055432124},
230            {-0.2876664477065717, 0.9577306588304888},
231            {-0.9148257956391065, 0.40384868903250853},
232            {-0.9015027194859215, -0.4327734358292892},
233            {-0.2570248925062563, -0.9664047830139022},
234            {0.5673996816983953, -0.8234425306046317},
235            {0.9945797473944409, -0.10397656501736473},
236            {0.7254405241129018, 0.6882848581617921},
237            {-0.05158982732517303, 0.9986683582233687},
238            {-0.7925014140531963, 0.609870075281354},
239            {-0.9785715990807187, -0.20590683687679034},
240            {-0.47953002522651733, -0.8775254725113429},
241            {0.35523727306945746, -0.9347761656258549},
242            {0.9412979532686209, -0.33757689964259285},
243            {0.868342678987353, 0.4959647082697184},
244            {0.18744846526420056, 0.9822744386728669},
245            {-0.6246810590458048, 0.7808800000444446},
246            {-0.9994625758058275, 0.03278047534097766},
247            {-0.674506266646887, -0.738269121834361},
248            {0.12268137965007223, -0.9924461089082646},
249            {0.8339780641890598, -0.5517975973592748},
250            {0.9613949601033843, 0.2751721837101493},
251            {0.41572570400265835, 0.9094900433932711},
252            {-0.42099897262033487, 0.907061114287578},
253            {-0.9629763390922247, 0.2695859238694348},
254            {-0.8307604078465821, -0.5566301687427484},
255            {-0.11691741449967302, -0.9931416405461567},
256            {0.6787811074228051, -0.7343406622310046},
257            {0.999255415972447, 0.03858255628819732},
258            {0.6201369341201711, 0.7844935837468874},
259            {-0.19314814942146824, 0.9811696042861612},
260            {-0.8712074932224428, 0.4909149659086258},
261            {-0.9393222007870077, -0.34303615422962713},
262            {-0.3498042060103595, -0.9368228314134226},
263            {0.4846166400948296, -0.8747266499559725},
264            {0.9797505510481769, -0.20022202106859724},
265            {0.7889473022428521, 0.6144608647291752},
266            {0.045790935472179155, 0.9989510449609544},
267            {-0.7294243101497431, 0.684061529222753},
268            {-0.9939593229024027, -0.10974909756074072},
269            {-0.562609414602539, -0.8267228354174018},
270            {0.26263126874523307, -0.9648962724963078},
271            {0.9040001019019392, -0.4275322394408211},
272            {0.9124657316291773, 0.4091531358824348},
273            {0.28210125132356934, 0.9593846381935018},
274            {-0.5457662881946498, 0.8379374431723614},
275            {-0.9915351626845509, 0.12983844253579577},
276            {-0.7431163048326799, -0.6691622803863227},
277            {0.02556874420628532, -0.9996730662170076},
278            {0.7763527553119807, -0.6302986588273021},
279            {0.9836012681423212, 0.1803567168386515},
280            {0.5022166799422209, 0.8647418148718223},
281            {-0.330776879188771, 0.9437089891455613},
282            {-0.9321888864830543, 0.3619722087639923},
283            {-0.8809623252471085, -0.47318641305008735},
284            {-0.21296163248563432, -0.9770605626515961},
285            {0.604136498566135, -0.7968808512571063},
286            {0.9982701582127194, -0.05879363249495786},
287            {0.6935008202914851, 0.7204558364362367},
288            {-0.09679820929680796, 0.9953040272584711},
289            {-0.8193274492343137, 0.5733258505694586},
290            {-0.9682340024187017, -0.25004582891994304},
291            {-0.4392662937408502, -0.8983569018954422},
292            {0.39723793388455464, -0.9177156552457467},
293            {0.9556302892322005, -0.2945687530984589},
294            {0.8449724198323217, 0.5348098818484104},
295            {0.14273745857559722, 0.9897605861618151},
296            {-0.6594300077680133, 0.7517659641504648},
297            {-0.9999212381512442, -0.01255059735959867},
298            {-0.6403535266476091, -0.768080308893523},
299            {0.16753470770767478, -0.9858661784001437},
300            {0.8581295336101056, -0.5134332513054668},
301            {0.9479357869928937, 0.31846152630759517},
302            {0.37407884501651706, 0.9273969040875156},
303            {-0.461675964944643, 0.8870486477034012},
304            {-0.9742049295269273, 0.22566513972130173},
305            {-0.8046793020829978, -0.5937097108850584},
306            {-0.07178636201352963, -0.9974200309943962},
307            {0.7113652211526822, -0.7028225395748172},
308            {0.9964799940037152, 0.08383091047075403},
309            {0.5839450884626246, 0.8117931594072332},
310            {-0.23741799789097484, 0.9714075840127259},
311            {-0.8925614000865144, 0.45092587758477687},
312            {-0.9228099950981292, -0.38525538665538556},
313            {-0.30698631553196837, -0.95171392869712},
314            {0.5237628071845146, -0.8518641451605984},
315            {0.9878182118285335, -0.15561227580071732},
316            {0.7602881737752754, 0.6495859395164404},
317            {4.6967723669845613E-4, 0.9999998897016406},
318            {-0.7596776469502666, 0.6502998329417794},
319            {-0.9879639510809196, -0.15468429579171308},
320            {-0.5245627784110601, -0.8513717704420726},
321            {0.3060921834538644, -0.9520018777441807},
322            {0.9224476966294768, -0.3861220622846781},
323            {0.8929845854878761, 0.45008724718774934},
324            {0.23833038910266038, 0.9711841358002995},
325            {-0.5831822693781987, 0.8123413326200348},
326            {-0.9964008074312266, 0.0847669213219385},
327            {-0.712025106726807, -0.7021540054650968},
328            {0.07084939947717452, -0.9974870237721009},
329            {0.8041212432524677, -0.5944653279629567},
330            {0.9744164792492415, 0.22474991650168097},
331            {0.462509014279733, 0.8866145790082576},
332    };
333    // these are the exact vertex positions, before scaling has been applied
334//            {
335//            {-0.324919696232904f,  0.850650808352036f,  0.000000000000001f },
336//            { 0.000000000000001f,  0.850650808352035f,  0.525731112119131f },
337//            { 0.324919696232906f,  0.850650808352036f,  0.000000000000001f },
338//            { 0.000000000000001f,  0.850650808352036f, -0.525731112119131f },
339//            {-0.525731112119131f,  0.525731112119132f, -0.525731112119130f },
340//            {-0.850650808352035f,  0.525731112119132f,  0.000000000000001f },
341//            {-0.525731112119130f,  0.525731112119131f,  0.525731112119132f },
342//            { 0.525731112119132f,  0.525731112119131f,  0.525731112119131f },
343//            { 0.850650808352036f,  0.525731112119132f,  0.000000000000000f },
344//            { 0.525731112119132f,  0.525731112119132f, -0.525731112119131f },
345//            {-0.525731112119132f,  0.000000000000002f, -0.850650808352036f },
346//            {-0.850650808352036f,  0.000000000000002f, -0.324919696232905f },
347//            { 0.000000000000000f,  0.324919696232906f, -0.850650808352037f },
348//            {-0.525731112119131f,  0.000000000000001f,  0.850650808352037f },
349//            { 0.000000000000001f,  0.324919696232905f,  0.850650808352037f },
350//            {-0.850650808352037f,  0.000000000000001f,  0.324919696232905f },
351//            { 0.525731112119133f,  0.000000000000001f,  0.850650808352036f },
352//            { 0.850650808352037f,  0.000000000000001f,  0.324919696232905f },
353//            { 0.525731112119132f,  0.000000000000001f, -0.850650808352038f },
354//            { 0.850650808352038f,  0.000000000000001f, -0.324919696232906f },
355//            {-0.525731112119134f, -0.525731112119130f, -0.525731112119133f },
356//            {-0.850650808352038f, -0.525731112119130f, -0.000000000000001f },
357//            {-0.000000000000001f, -0.324919696232905f, -0.850650808352038f },
358//            {-0.000000000000001f, -0.324919696232905f,  0.850650808352038f },
359//            {-0.525731112119132f, -0.525731112119131f,  0.525731112119133f },
360//            { 0.525731112119133f, -0.525731112119131f,  0.525731112119134f },
361//            { 0.850650808352039f, -0.525731112119130f,  0.000000000000001f },
362//            { 0.525731112119132f, -0.525731112119134f, -0.525731112119133f },
363//            {-0.000000000000003f, -0.850650808352038f, -0.525731112119134f },
364//            {-0.324919696232908f, -0.850650808352038f, -0.000000000000002f },
365//            {-0.000000000000002f, -0.850650808352042f,  0.525731112119130f },
366//            { 0.324919696232902f, -0.850650808352041f,  0.000000000000002f }
367//    };
368    protected static final float[] grad3d =
369            {
370                    -0.448549002408981f,  1.174316525459290f,  0.000000000000001f,
371                     0.000000000000001f,  1.069324374198914f,  0.660878777503967f,
372                     0.448549002408981f,  1.174316525459290f,  0.000000000000001f,
373                     0.000000000000001f,  1.069324374198914f, -0.660878777503967f,
374                    -0.725767493247986f,  0.725767493247986f, -0.725767493247986f,
375                    -1.069324374198914f,  0.660878777503967f,  0.000000000000001f,
376                    -0.725767493247986f,  0.725767493247986f,  0.725767493247986f,
377                     0.725767493247986f,  0.725767493247986f,  0.725767493247986f,
378                     1.069324374198914f,  0.660878777503967f,  0.000000000000000f,
379                     0.725767493247986f,  0.725767493247986f, -0.725767493247986f,
380                    -0.660878777503967f,  0.000000000000003f, -1.069324374198914f,
381                    -1.174316525459290f,  0.000000000000003f, -0.448549002408981f,
382                     0.000000000000000f,  0.448549002408981f, -1.174316525459290f,
383                    -0.660878777503967f,  0.000000000000001f,  1.069324374198914f,
384                     0.000000000000001f,  0.448549002408981f,  1.174316525459290f,
385                    -1.174316525459290f,  0.000000000000001f,  0.448549002408981f,
386                     0.660878777503967f,  0.000000000000001f,  1.069324374198914f,
387                     1.174316525459290f,  0.000000000000001f,  0.448549002408981f,
388                     0.660878777503967f,  0.000000000000001f, -1.069324374198914f,
389                     1.174316525459290f,  0.000000000000001f, -0.448549002408981f,
390                    -0.725767493247986f, -0.725767493247986f, -0.725767493247986f,
391                    -1.069324374198914f, -0.660878777503967f, -0.000000000000001f,
392                    -0.000000000000001f, -0.448549002408981f, -1.174316525459290f,
393                    -0.000000000000001f, -0.448549002408981f,  1.174316525459290f,
394                    -0.725767493247986f, -0.725767493247986f,  0.725767493247986f,
395                     0.725767493247986f, -0.725767493247986f,  0.725767493247986f,
396                     1.069324374198914f, -0.660878777503967f,  0.000000000000001f,
397                     0.725767493247986f, -0.725767493247986f, -0.725767493247986f,
398                    -0.000000000000004f, -1.069324374198914f, -0.660878777503967f,
399                    -0.448549002408981f, -1.174316525459290f, -0.000000000000003f,
400                    -0.000000000000003f, -1.069324374198914f,  0.660878777503967f,
401                     0.448549002408981f, -1.174316525459290f,  0.000000000000003f,
402            };
403    protected static final double[] grad4d =
404            {
405                    -0.5875167, 1.4183908, 1.4183908, 1.4183908,
406                    -0.5875167, 1.4183908, 1.4183908, -1.4183908,
407                    -0.5875167, 1.4183908, -1.4183908, 1.4183908,
408                    -0.5875167, 1.4183908, -1.4183908, -1.4183908,
409                    -0.5875167, -1.4183908, 1.4183908, 1.4183908,
410                    -0.5875167, -1.4183908, 1.4183908, -1.4183908,
411                    -0.5875167, -1.4183908, -1.4183908, 1.4183908,
412                    -0.5875167, -1.4183908, -1.4183908, -1.4183908,
413                    1.4183908, -0.5875167, 1.4183908, 1.4183908,
414                    1.4183908, -0.5875167, 1.4183908, -1.4183908,
415                    1.4183908, -0.5875167, -1.4183908, 1.4183908,
416                    1.4183908, -0.5875167, -1.4183908, -1.4183908,
417                    -1.4183908, -0.5875167, 1.4183908, 1.4183908,
418                    -1.4183908, -0.5875167, 1.4183908, -1.4183908,
419                    -1.4183908, -0.5875167, -1.4183908, 1.4183908,
420                    -1.4183908, -0.5875167, -1.4183908, -1.4183908,
421                    1.4183908, 1.4183908, -0.5875167, 1.4183908,
422                    1.4183908, 1.4183908, -0.5875167, -1.4183908,
423                    1.4183908, -1.4183908, -0.5875167, 1.4183908,
424                    1.4183908, -1.4183908, -0.5875167, -1.4183908,
425                    -1.4183908, 1.4183908, -0.5875167, 1.4183908,
426                    -1.4183908, 1.4183908, -0.5875167, -1.4183908,
427                    -1.4183908, -1.4183908, -0.5875167, 1.4183908,
428                    -1.4183908, -1.4183908, -0.5875167, -1.4183908,
429                    1.4183908, 1.4183908, 1.4183908, -0.5875167,
430                    1.4183908, 1.4183908, -1.4183908, -0.5875167,
431                    1.4183908, -1.4183908, 1.4183908, -0.5875167,
432                    1.4183908, -1.4183908, -1.4183908, -0.5875167,
433                    -1.4183908, 1.4183908, 1.4183908, -0.5875167,
434                    -1.4183908, 1.4183908, -1.4183908, -0.5875167,
435                    -1.4183908, -1.4183908, 1.4183908, -0.5875167,
436                    -1.4183908, -1.4183908, -1.4183908, -0.5875167,
437                    0.5875167, 1.4183908, 1.4183908, 1.4183908,
438                    0.5875167, 1.4183908, 1.4183908, -1.4183908,
439                    0.5875167, 1.4183908, -1.4183908, 1.4183908,
440                    0.5875167, 1.4183908, -1.4183908, -1.4183908,
441                    0.5875167, -1.4183908, 1.4183908, 1.4183908,
442                    0.5875167, -1.4183908, 1.4183908, -1.4183908,
443                    0.5875167, -1.4183908, -1.4183908, 1.4183908,
444                    0.5875167, -1.4183908, -1.4183908, -1.4183908,
445                    1.4183908, 0.5875167, 1.4183908, 1.4183908,
446                    1.4183908, 0.5875167, 1.4183908, -1.4183908,
447                    1.4183908, 0.5875167, -1.4183908, 1.4183908,
448                    1.4183908, 0.5875167, -1.4183908, -1.4183908,
449                    -1.4183908, 0.5875167, 1.4183908, 1.4183908,
450                    -1.4183908, 0.5875167, 1.4183908, -1.4183908,
451                    -1.4183908, 0.5875167, -1.4183908, 1.4183908,
452                    -1.4183908, 0.5875167, -1.4183908, -1.4183908,
453                    1.4183908, 1.4183908, 0.5875167, 1.4183908,
454                    1.4183908, 1.4183908, 0.5875167, -1.4183908,
455                    1.4183908, -1.4183908, 0.5875167, 1.4183908,
456                    1.4183908, -1.4183908, 0.5875167, -1.4183908,
457                    -1.4183908, 1.4183908, 0.5875167, 1.4183908,
458                    -1.4183908, 1.4183908, 0.5875167, -1.4183908,
459                    -1.4183908, -1.4183908, 0.5875167, 1.4183908,
460                    -1.4183908, -1.4183908, 0.5875167, -1.4183908,
461                    1.4183908, 1.4183908, 1.4183908, 0.5875167,
462                    1.4183908, 1.4183908, -1.4183908, 0.5875167,
463                    1.4183908, -1.4183908, 1.4183908, 0.5875167,
464                    1.4183908, -1.4183908, -1.4183908, 0.5875167,
465                    -1.4183908, 1.4183908, 1.4183908, 0.5875167,
466                    -1.4183908, 1.4183908, -1.4183908, 0.5875167,
467                    -1.4183908, -1.4183908, 1.4183908, 0.5875167,
468                    -1.4183908, -1.4183908, -1.4183908, 0.5875167,
469            };
470    protected final long defaultSeed;
471    public static final SeededNoise instance = new SeededNoise();
472
473    public SeededNoise() {
474        defaultSeed = 0x1337BEEF2A22L;
475    }
476    public SeededNoise(long seed)
477    {
478        defaultSeed = seed;
479    }
480
481    /**
482     * Computes the hash for a 3D int point and its dot product with a 3D double point as one step.
483     * @param seed
484     * @param x
485     * @param y
486     * @param z
487     * @param xd
488     * @param yd
489     * @param zd
490     * @return a double between -1.2571 and 1.2571, exclusive
491     */
492    protected static double gradCoord3D(long seed, int x, int y, int z, double xd, double yd, double zd) {
493        final int hash = HastyPointHash.hash32(x, y, z, seed) * 3;
494        return xd * grad3d[hash] + yd * grad3d[hash + 1] + zd * grad3d[hash + 2];
495    }
496
497    public double getNoise(final double x, final double y) {
498        return noise(x, y, defaultSeed);
499    }
500    public double getNoise(final double x, final double y, final double z) {
501        return noise(x, y, z, defaultSeed);
502    }
503    public double getNoise(final double x, final double y, final double z, final double w) {
504        return noise(x, y, z, w, defaultSeed);
505    }
506    public double getNoise(final double x, final double y, final double z, final double w, final double u, final double v) {
507        return noise(x, y, z, w, u, v, defaultSeed);
508    }
509
510    public double getNoiseWithSeed(final double x, final double y, final long seed) {
511        return noise(x, y, seed);
512    }
513    public double getNoiseWithSeed(final double x, final double y, final double z, final long seed) {
514        return noise(x, y, z, seed);
515    }
516    public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final long seed) {
517        return noise(x, y, z, w, seed);
518    }
519    public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final double u, final double v, final long seed) {
520        return noise(x, y, z, w, u, v, seed);
521    }
522
523    protected static final double[] gradient6DLUT = {
524            0.31733186658157, 0.043599150809166, -0.63578104939541,
525            0.60224147484783, -0.061995657882187, 0.35587048501823,
526            -0.54645425808647, -0.75981513883963, -0.035144342454363,
527            0.13137365402959, 0.29650029456531, 0.13289887942467,
528            0.72720729277573, -0.0170513084554, 0.10403853926717, 0.57016794579524,
529            0.10006650294475, -0.35348266879289,
530            0.0524867271859, 0.16599786784909, -0.49406271077513, 0.51847470894887,
531            0.63927166664011, -0.21933445140234,
532            -0.57224122530978, -0.089985946187774, 0.44829955643248,
533            0.53836681748476, -0.051299333576026, -0.41352093713992,
534            -0.35034584363296, -0.37367516013323, -0.52676009109159,
535            0.12379417201967, 0.42566489477591, 0.51345191723381,
536            0.40936909283115, 0.33036021753157, 0.46771483894695, 0.15073372728805,
537            0.51541333179083, -0.46491971651678,
538            -0.64339751231027, -0.29341468636474, -0.50841617762291,
539            -0.080659811936781, -0.46873502824317, -0.12345817650503,
540            0.46950904113222, 0.41685007896275, -0.33378791988356,
541            -0.39617029121348, 0.54659770033168, 0.19662896748851,
542            -0.49213884108338, 0.50450587466563, -0.0073247243900323,
543            0.57958418990163, 0.39591449230465, 0.10272980841415,
544            0.34572956497624, 0.62770109739866, 0.12165109216674, 0.35267248385686,
545            0.34842369637704, -0.47527514024373,
546            0.076282233884284, 0.56461194794873, -0.392426730607,
547            -0.20639693057567, 0.33197602170266, 0.60711436994661,
548            0.46792592791359, -0.38434666353171, -0.46719345820863,
549            -0.40169520060432, -0.061343490026986, 0.49993117813162,
550            -0.25398819915038, -0.82255018555745, 0.40372967512401,
551            0.21051604195389, 0.020384827146984, 0.22621006002887,
552            0.23269489013955, -0.42234243708413, -0.18886779174866,
553            0.44290933725703, -0.40895242871151, 0.60695810498111,
554            -0.13615585122038, 0.26142849716038, 0.68738606675966,
555            0.42914965171764, 0.26332301994884, 0.43256061294487,
556            0.06145597366231, -0.25432792035414, 0.65050463165568,
557            0.35622065678761, -0.52670947710524, -0.32259598080167,
558            -0.28027055313228, 0.30275296247348, 0.39083872911587,
559            0.17564171472763, 0.25278203996272, 0.76307625890429,
560            -0.62937098181034, -0.24958587788613, 0.11855057687171,
561            0.52714220921895, 0.47759151204224, -0.14687496867489,
562            0.68607574135496, 0.28465344118508, 0.57132493696771, 0.11365238375433,
563            -0.32111327299854, -0.076352560636185,
564            0.42669573845021, -0.1643996530281, -0.54881376863042,
565            -0.56551221465284, 0.4027156095588, -0.087880721039792,
566            -0.30211042220321, -0.47278547361731, 0.050137867251391,
567            0.46804387457884, -0.39450159355792, 0.55497099667426,
568            0.31255895138908, 0.034478918459459, -0.079232996020732,
569            0.39803160685016, 0.82281399721198, 0.24369695191021,
570            -0.5524321671417, 0.49350231710234, 0.52530668244467, 0.253625789825,
571            0.26218499242504, -0.20557247282514,
572            0.060763010271891, -0.023938406391206, 0.36557410300471,
573            0.55368747615095, 0.25557899769702, -0.70014279913759,
574            0.36398574324757, 0.049110464042478, -0.2428951164628,
575            -0.18733973495522, 0.020130805835303, 0.87784000694654,
576            -0.62385490124849, 0.020947599003133, -0.44548631925386,
577            -0.21069894502123, -0.60559127508405, 0.027809382425643,
578            0.51562840479369, -0.27416131751628, -0.14365580420426,
579            -0.46525735490594, 0.16338488557607, 0.62862302132303,
580            0.52085189275139, 0.51359303425374, 0.021844789421786,
581            0.53521775458267, -0.23767218281397, -0.34858599348565,
582            0.12263603513069, 0.53912951801629, 0.57550729534804,
583            -0.10335514143554, 0.57524709075397, 0.14662748040551,
584            0.40942178494947, 0.17197663954561, -0.025238012475873,
585            -0.20104824969996, -0.60303014654018, 0.63094779803243,
586            0.051685704973311, 0.23577798459204, -0.19154992327678,
587            -0.67743578708385, -0.51070301615526, 0.43047548181493,
588            0.21373839204543, -0.44348268823586, 0.34347986958921,
589            -0.49945694096162, 0.45888698118478, -0.42382317871053,
590            -0.60376535923059, -0.065300874745824, 0.49448067868339,
591            0.12358559784007, 0.58623743735263, -0.16656623971303,
592            0.44140930948322, -0.41692548571374, -0.23774988226818,
593            -0.27542786466885, 0.39264397083621, 0.58717642823542,
594            -0.67860697457746, 0.2070991391515, -0.12832398784247,
595            -0.58381216132288, 0.24050209342748, 0.2854077401022,
596            -0.021324501342617, 0.0098658783730532, 0.2694901128571,
597            0.42580554353158, -0.82903198308789, -0.24128534823695,
598            -0.20344882384938, 0.51719618805529, 0.24379623299129,
599            0.11303683173372, -0.46058654895958, -0.63777957124993,
600            0.15686479897897, -0.67777169905813, -0.04974608057712,
601            0.51313211803344, 0.49928667286231, -0.030863149692696,
602            0.53527130791104, -0.50102597915466, -0.60754472649714,
603            -0.25235098830686, 0.13490559284448, 0.10708155847142,
604            -0.20613512232544, 0.39533044356843, -0.34422306275706,
605            0.4792145528465, -0.19178040223502, -0.64521804411898,
606            0.3304779611047, 0.49148538926455, -0.30004348427342, 0.33473309391851,
607            0.31079743137844, 0.59208027276116,
608            -0.52688857216953, 0.40250311061529, 0.38833191043333,
609            0.50432308135853, -0.33327489215794, -0.21015252001231,
610            -0.30306420816123, -0.34460825415019, -0.26894228639121,
611            -0.58579646837355, -0.51178483212848, 0.33464319317466,
612            -0.20258582390514, -0.29195675136034, 0.11887973573086,
613            0.91211540292822, 0.034118810787236, -0.16269371903027,
614            0.61207678339522, -0.21883722070929, -0.23415725333464,
615            0.0041447691596985, -0.34019274152454, 0.6378827339521,
616            0.11272999861808, -0.54780877011146, -0.62497664375172,
617            -0.41373740141301, 0.33306010353229, 0.12039112788093,
618            0.24918468395037, -0.068734287809286, -0.42234580029763,
619            0.12235329631887, -0.26545138767734, 0.81815148205875,
620            0.32048708659406, -0.40233908147851, 0.24633289057781,
621            -0.37087758270512, -0.55466799718133, -0.47908728788262,
622            -0.33748729653627, -0.45507986822699, -0.50597645316527,
623            -0.2863701644881, -0.5404199724601, -0.22120318557996,
624            -0.23520314824941, 0.82195093398991, -0.22661283339659,
625            0.16382454786402, -0.41400232366734, -0.13959354720703,
626            -0.30495751902889, -0.47964557116121, -0.68490238495876,
627            -0.4324077675155, -0.13521732523742, -0.050887702629247,
628            -0.56629250538137, 0.19768903044, -0.080075220953828,
629            -0.29952637623112, 0.095974426142512, -0.73136356489112,
630            -0.21316607993139, 0.47585902758173, -0.49429850443227,
631            -0.24146904800157, 0.45631329089651, 0.46610972545109,
632            0.12647584748018, -0.10203700758813, 0.20801341293098,
633            0.66418891258418, -0.65219775460192, -0.2526141453282,
634            -0.69345279552921, 0.30149980453822, -0.46870940095961,
635            0.20092958919922, -0.21817920622376, 0.34721422759447,
636            -0.69001417476102, 0.09722776919634, -0.37852252163632,
637            -0.24995374433763, 0.24829304775112, 0.4970126640943,
638            -0.82278510972964, 0.050748830242865, -0.3934733016285,
639            0.00029980431140623, -0.34677214869339, -0.21301870187776,
640            -0.51821811089111, -0.22147302694699, 0.53524316281446,
641            0.12892242816244, -0.5543955478928, -0.26821451961648,
642            -0.21006612796354, 0.26079212570498, -0.021870637510645,
643            0.72402587064608, -0.27651658712238, 0.53544979218311,
644            -0.099744280251479, -0.4534212871731, 0.71954978543864,
645            -0.31082396323078, -0.26933824624449, 0.31233586755618,
646            -0.48121951222937, -0.43051247772929, -0.5038415181805,
647            0.12342710418307, 0.037467829082858, -0.55909965468017,
648            -0.51180831908824, -0.079955485578946, -0.53046702060975,
649            0.48748209854708, 0.16148937559829, -0.43191028009105,
650            -0.38131649706702, 0.46242477534251, 0.46416075424014,
651            -0.20634110277567, -0.53778490132009, 0.30582118902172,
652            0.6245043069106, 0.14316692963071, -0.1436103838143, 0.27519251589203,
653            -0.60467865310212, -0.35708047307373,
654            0.52425890739441, -0.20390682829262, -0.33609142609195,
655            0.51803372559413, 0.28921536255925, 0.46756035964091,
656            -0.4455164148456, 0.31831805515328, 0.24217750314789, 0.49821219078654,
657            -0.47209418708575, 0.41285649844363,
658            -0.015857310429397, -0.45214512052441, -0.14591363373753,
659            0.74070676188619, 0.0098874230592725, -0.47463489014478,
660            0.24260837156464, 0.44639366601915, 0.31528570191456, 0.45334773303464,
661            -0.47964168123625, -0.45484996397296,
662            0.47123463487178, 0.64525048646519, -0.064257637508608,
663            -0.18737730572971, -0.11735335340515, -0.55549853319118,
664            -0.025197229767488, -0.257963271803, 0.26277107860996,
665            -0.58236203161499, -0.41893538667715, 0.59086294196016,
666            -0.48940330017687, 0.33728563842186, -0.057634928591543,
667            0.44862021996899, -0.40048256377746, 0.53080564921806,
668            0.73350664260388, -0.021482988114587, 0.016568147533453,
669            0.0021905972927896, 0.49384961731337, 0.46619710394628,
670            -0.25151229880228, -0.62009962583403, -0.26948657433033,
671            0.31711936293198, -0.35081923073755, 0.50592112116981,
672            0.0094298597779172, -0.35925999444899, 0.47529205807388,
673            -0.26709475088579, -0.53352146543694, 0.53754630836074,
674            -0.5948549517534, -0.53195924881292, -0.094383768924555,
675            -0.41704491211939, -0.41397531920841, -0.09463944474724,
676            -0.74917126125127, -0.24166385705367, 0.22864554725283,
677            0.31721357549513, 0.06066292638611, -0.47303041351952,
678            -0.3300396030254, -0.08758658200966, -0.096726092930468,
679            -0.39607089556472, 0.55566932028997, 0.63906648027271,
680            -0.58933068378397, -0.38176870540341, 0.46748019640554,
681            -0.061358837959321, 0.36268480315292, -0.39127879224432,
682            -0.066556695042975, -0.73863083674701, -0.32153946998935,
683            0.57454599361106, -0.090856896694743, -0.09082394033963,
684            -0.36335404704287, -0.41643677881158, -0.57839830999334,
685            -0.030959887755637, 0.5989792522053, -0.016582566905843,
686            0.23126668855143, 0.2107790785413, -0.14272193312959,
687            -0.29232225134991, -0.48451339172564, -0.74934159314943,
688            0.48188197979627, -0.040214759215399, -0.15667971883369,
689            0.16054853668069, -0.6083975436752, -0.58796308779952,
690            0.31319356064062, -0.19280657835646, 0.76136690598738,
691            -0.084506239097717, 0.4768786755523, -0.22472488900872,
692            0.67504537519138, 0.36920158913876, 0.40321048682396,
693            0.034436041975613, -0.29332731631919, 0.39774172001359,
694            -0.1459159803857, -0.59726183207777, -0.036384224081948,
695            -0.65093487874945, 0.39515711468056, -0.20198429937477,
696            0.60092128630869, 0.18110182176699, 0.2579491954112, -0.39594768022975,
697            0.15112959843347, 0.59995268930018,
698            -0.42310244265976, -0.26937197256148, 0.074700012546319,
699            0.53119510349465, 0.41614374632783, 0.53618944036115,
700            0.0071605427687482, -0.69599782505338, -0.053138604739257,
701            -0.00054500262230378, 0.69533871546989, 0.1709263483943,
702            0.12447149375466, 0.33265313001972, 0.35070015349473, 0.53879932284829,
703            0.37648083373421, 0.56463759722353,
704            0.29540077719054, 0.04954124873475, -0.48345087234985,
705            0.72758494948264, 0.070069102610626, 0.377186640377,
706            0.4882414260383, 0.45135801463006, 0.48450857902353, -0.26042407965644,
707            -0.4251358047458, 0.2731053563007,
708            -0.49806371818291, -0.4719759672029, 0.029647087810764,
709            -0.13788472163255, -0.45346141932978, -0.5510470160674,
710            -0.5359511936033, -0.53585470245895, 0.1771036246335, -0.4537763243703,
711            0.41838964069644, 0.11527149720722,
712            -0.36846431808379, -0.46533180802325, 0.65800816763703,
713            -0.28691297783558, 0.31521457275327, 0.18178647457201,
714            -0.29243126901345, -0.4352956525447, -0.58895978125929,
715            -0.49649471729812, 0.29271342931272, 0.21433587621517,
716            0.056256690265475, -0.50387710054371, 0.48145041862725,
717            0.44723671964597, -0.55771174894027, -0.0092449146014199,
718            -0.40973125164006, -0.73147173623276, -0.094076302480945,
719            0.43033451471976, 0.014334271843521, -0.32066459724334,
720            0.26752725373294, 0.50477344684769, 0.065069516529324,
721            0.36001097578267, 0.59393393889869, -0.43247366096278,
722            0.48945720845334, 0.6043315650632, 0.12458128550608, -0.48327805813458,
723            -0.25681943056744, 0.28316179557217,
724            -0.45182760404001, 0.21574002665039, -0.31462623994251,
725            0.25279349500371, 0.44865729380505, -0.62058075048081,
726            0.44017304540101, 0.43789555905674, 0.58423563606269, 0.41842994331139,
727            -0.26836655962348, 0.16143005677844,
728            -0.67897032028819, -0.32730885869255, -0.0243997359109,
729            0.40649244381227, 0.47711065295824, -0.19596475712206,
730            0.57441588138131, 0.09386994843744, 0.28400793066375, 0.59394229842661,
731            0.45349906020748, 0.14881354725974,
732            -0.3393739967757, -0.54929055652002, 0.26209493900588, 0.0733800373509,
733            0.56557076402003, 0.43492125584075,
734            0.050007991188197, 0.74652764513134, -0.36432144611385,
735            -0.20993543754239, -0.1352041047841, 0.49508839805322,
736            -0.041332158875019, -0.20655741061568, 0.52511282214888,
737            0.047248635933477, -0.6276121766011, -0.5326844609727,
738            -0.1889491176448, 0.05188976739355, -0.45677123586268,
739            0.42884456750344, 0.61612085530435, -0.43526216197988,
740            -0.65873541163911, -0.094770059351695, 0.40844030815782,
741            0.35536013391048, -0.16940065827957, 0.48506226422661,
742            -0.45779281442862, -0.46052673126242, 0.34138050378631,
743            -0.54943270263121, 0.37140594702643, -0.14826175595089,
744            -0.069378715405383, -0.14845488608058, -0.73991837897813,
745            0.41519184526768, -0.11098464009855, -0.49088356499611,
746            0.46422563805447, 0.46130716873201, -0.44207791495441,
747            0.12050605352899, 0.34969556083561, -0.4893349322843,
748            -0.35482925073362, 0.28146983672487, -0.35356606227648,
749            -0.38774754218768, 0.35979702647173, -0.62454776976122,
750            -0.48343191508515, 0.41492185792886, -0.50175316406656,
751            0.21953122931153, -0.54083165333237, 0.041040952107647,
752            -0.51280508048852, -0.54131124436697, -0.0099287129207481,
753            0.23788701199175, 0.4350333223576, 0.44505087885649,
754            0.2253837335044, -0.30117119745248, 0.46587685049056,
755            -0.46672901001472, -0.59182069765377, 0.27086737661249,
756            0.43015756480475, -0.067851118947538, -0.26917802105288,
757            -0.57731860676632, -0.53950120703807, -0.33696522367557,
758            0.20858352742161, 0.63695057987625, 0.49453142202915,
759            -0.046235371593379, -0.54436247241885, -0.088075720520231,
760            -0.35626464703623, 0.067539543974725, -0.18142793486226,
761            -0.49044207117167, 0.5542388249925, 0.53654796190017,
762            0.52238539932434, 0.55175875223621, 0.29070268774296,
763            -0.14119026819648, -0.55841587206055, -0.080029639759127,
764            -0.025988002903175, 0.46612949273683, -0.56880970348453,
765            -0.44824563336003, -0.030000490931808, 0.50663523727173,
766            0.047284583258099, -0.26595723160738, 0.21032033434131,
767            0.52986834914146, -0.52245334572957, -0.5736534757312,
768            -0.31924244568277, -0.13888420092891, 0.30725800370737,
769            0.49792332552544, 0.61035592292817, -0.40487771982263,
770            0.038758575627018, -0.53813545398707, -0.56167256912901,
771            0.46815373895572, -0.14142713486975, 0.39276248966752,
772            -0.19936871608885, 0.12488860648831, -0.62990029833727,
773            -0.29296146144627, 0.49734531468753, 0.46335923993672,
774            -0.078826705546604, -0.15548800857414, 0.57456768467721,
775            0.5558854465212, -0.56893054194692, -0.082408823513622,
776            0.11678856295109, 0.53358760166951, 0.49302489382249,
777            -0.53981846952046, -0.237913367643, -0.33251226509871,
778            0.39126928439834, -0.39416116630681, -0.35778844984527,
779            -0.39395609960567, 0.50270356681194, -0.39448759513757,
780            -0.17961290695406, 0.34239532682819, -0.21870225043453,
781            -0.23322835296688, 0.75997835134209, 0.41317237364121,
782            0.29699501400111, 0.17195435585404, -0.34903627841034,
783            -0.31751884057854, -0.59661546358767, 0.55102732418683,
784            -0.2237291316445, -0.51254305965518, -0.31277318571798,
785            0.54270199705442, -0.34885011313806, 0.41616819064585,
786            0.53534023676892, 0.45905986582643, -0.20308675275303,
787            0.019523641323632, 0.3378580580099, 0.58898336258938,
788            -0.045038463119119, -0.52553334288797, -0.6098545897634,
789            0.46226027841702, -0.36069029000651, 0.077984430434637,
790            -0.40129033029845, 0.39526722066586, -0.20379584931963,
791            0.45466492237669, 0.46504795737483, -0.46712669863522,
792            -0.43845831945339, -0.59284534057943, 0.050241908216277,
793            -0.36494839821973, 0.32363879325018, 0.46458051299488,
794            -0.46057360356064, -0.34584626825548, -0.12264748451482,
795            0.48835437094478, 0.21102526990984, 0.60843919401837,
796            -0.086047549693024, -0.16981605114589, -0.37222833669973,
797            0.45158609930017, -0.55710254634126, 0.55759406480139,
798            0.54697451263099, -0.45070837355303, 0.032962522247893,
799            -0.48584332140086, -0.28055687213837, 0.42642516953676,
800            0.34061925303691, 0.38443007758012, 0.61614808332652,
801            -0.55774172327958, -0.075660378162998, 0.19938218730551,
802            0.30626924920956, -0.057939049897675, -0.10461119704504,
803            -0.4395638756485, -0.57307193269415, 0.60849886616281,
804            -0.52519951444608, -0.42567534157254, -0.19896500097138,
805            0.48819483593271, 0.12539008064447, 0.49932157157064,
806            -0.10173361116951, -0.07873850987854, 0.3713554090283,
807            0.65889542748449, 0.63411890875068, 0.096414235519521,
808            0.60342393773609, 0.057617370697663, 0.35558841250938,
809            0.20766418929404, 0.030670189501999, -0.67974377143949,
810            -0.071971052874019, -0.44567383014704, 0.65917594080871,
811            0.44113802003588, -0.29627117199757, 0.28160739274962,
812            0.38284479693596, 0.43552320173998, -0.4282368470258,
813            -0.54809258921772, -0.27202273485667, 0.32551612927831,
814            -0.74755699288716, -0.20979308948438, 0.19268299390085,
815            0.27864013929953, -0.39085278833717, 0.36001727246301,
816            -0.64575536737195, 0.59253747557756, 0.040885512266333,
817            -0.20167391777406, -0.43481684011627, -0.02212841779644,
818            0.45874103754271, -0.0066587566394561, -0.30494054091993,
819            0.52731059172348, -0.64443887148677, 0.056264275617853,
820            0.61573773369959, -0.00074622703454316, 0.25455659350429,
821            0.30670278147618, -0.18573195942296, 0.65383825999316,
822            -0.089919562456316, -0.28968403215216, -0.60618287937171,
823            0.53370861364121, 0.37921556323246, -0.33450055738044,
824            -0.47481167613763, 0.3899274103573, -0.1047963185367, 0.45545456567005,
825            0.12142073778317, 0.62397625076847,
826            0.59154225785278, -0.10812441303593, -0.4685834521013,
827            -0.36007270807588, -0.1012374701199, 0.52812407295968,
828            -0.01292122984647, -0.23607532114711, -0.57680411110671,
829            -0.44955815301222, -0.31913443306122, -0.55448100298376,
830            0.54231398466289, -0.31845386154668, -0.38636423612049,
831            0.22187979539931, -0.6346425853783, -0.056599490898788,
832            -0.41950690366157, -0.4578028963184, 0.31139813874057,
833            0.39787962066193, -0.20885901240181, 0.56172180435883,
834            -0.031404881097728, 0.56267475273157, -0.5556815383811,
835            0.33075363850824, 0.39071115867626, 0.3340294973255,
836            -0.51485161085589, -0.34037011091125, -0.46826090820473,
837            -0.60086679836276, -0.075069409610657, 0.18202033570633,
838            -0.49669644859095, 0.13236483793072, 0.53440735955877, 0.4720120049858,
839            -0.05992551666341, -0.47306929861073,
840            -0.32796852486185, 0.65593302097807, 0.20800030327303,
841            -0.38965914824176, -0.51564565153044, -0.034636725857177,
842            -0.30473794783797, 0.12584230588041, 0.63911213518179,
843            0.11269477188219, 0.62944339013855, 0.27191006392352,
844            -0.53642197294029, 0.50742224701512, -0.22907820767928,
845            0.47022559371179, -0.1914125650624, 0.38019261684316,
846            -0.28865425091309, 0.76169672032907, -0.36166127667225,
847            -0.30555403321368, -0.12541657537884, -0.31081403770203,
848            0.0025978417989835, 0.3737146483793, -0.3151511957077,
849            0.62032810853005, 0.60524642517936, -0.09939888944988,
850            -0.40019833530022, 0.15931480693456, -0.61653030345628,
851            -0.49479441153976, -0.021517911098538, -0.43481713333933,
852            -0.26445143166732, -0.48401155081335, 0.27737058096082,
853            -0.12537486208624, -0.46956235249512, 0.61859207953377,
854            -0.49776294425122, 0.6509513246149, -0.20147785800704,
855            0.26022926925791, 0.39526195830317, -0.25288299425858,
856            0.20792543895216, 0.6725599557329, 0.013296712014115,
857            0.069082404776847, -0.37233547685047, 0.60070560947898,
858            -0.60329265885108, 0.40708027238668, -0.17229997007444,
859            -0.52997954496878, 0.22211745651394, -0.33229784433365,
860            0.61826884506104, -0.62582169643111, 0.33820439950773,
861            0.23870919720066, -0.20670655096227, -0.10953969425599,
862            -0.63678168786213, -0.51101649337563, -0.19131817442969,
863            -0.49493417544846, -0.22614515287593, 0.025828539221376,
864            0.7068462559507, 0.072932806612059, -0.30827034359477,
865            -0.52659704221432, -0.33954839093364, 0.086145323573817,
866            -0.52429050496975, 0.39091424683727, 0.52819210715237,
867            -0.16569162349745, 0.447191673089, 0.25667977984796,
868            0.85033978527922, -0.37311666188152, -0.031585518143925,
869            -0.063546921071094, -0.35026506762952, 0.099923633151172,
870            -0.43149574251927, 0.16017753208259, -0.36624037246965,
871            0.49372029676385, -0.60067103922455, 0.2223896202103,
872            -0.43599537393092, -0.360658355506, -0.42475053011196,
873            -0.52301759011739, 0.039454536357949, 0.47362064109658,
874            -0.35793170214797, -0.43917817788312, -0.49072242572643,
875            -0.32880277826743, -0.38509560837703, -0.42636724894184,
876            -0.043679644403255, 0.74697226557232, -0.40732954428872,
877            -0.48088968590275, 0.18029290312902, -0.10220931735307,
878            -0.058902573502295, 0.0082595236590186, 0.7136596141971,
879            -0.53043791172483, 0.22906331492979, 0.39155822265168,
880            0.43459649233879, 0.18964470832196, 0.15217427204218, 0.59694624534505,
881            0.053786588105393, 0.62671041756872,
882            -0.48833575031057, 0.068909881680922, 0.60168404074737,
883            -0.055455043023162, -0.62426261497771, -0.044461939113733,
884            -0.71822145541427, 0.054494951105527, 0.25733756171599,
885            -0.42706881935297, -0.44024663347316, 0.19687748949208,
886            0.4723221071836, 0.63009683957253, 0.2166256995021, 0.31063720960745,
887            0.079455887335627, 0.47974409023622,
888            -0.39506538843406, 0.42517729990346, 0.29375773990216,
889            0.044503633424429, -0.46173213926286, 0.60139575234582,
890            -0.40354126620316, 0.41304136826673, -0.29533980868045,
891            -0.45300699221804, 0.23702354154238, -0.56385297528377,
892            -0.62315380378984, -0.42397903326965, 0.53044082394843,
893            0.37874432092957, 0.054922713129263, 0.063952196248596,
894            0.41959045692314, -0.83420441875842, -0.25505372502578,
895            0.25012310515014, 0.010974237503127, 0.017675743681809,
896            -0.25231575134089, -0.17034034508503, -0.0022254428444259,
897            -0.4967771056787, 0.43184899693064, -0.68850194407078,
898            -0.1852812882862, -0.48330898597592, 0.13528868642679,
899            0.15202104844417, 0.57661281495368, -0.59848767913131,
900            0.64287473226568, -0.30923674494923, 0.22234318117192,
901            0.099248962994541, 0.64370450011427, 0.13206961744112,
902            -0.49018899717866, 0.68654120859156, -0.27238863334662,
903            -0.085832423495263, 0.44161945604453, 0.10856057983467,
904            0.48795432482822, 0.42184193883513, -0.43797315744756,
905            0.35186997012044, -0.46483432791096, 0.22857392808385,
906            0.52970834834669, -0.50684486922008, -0.39782161731912,
907            -0.3932709335414, -0.34863027587322, 0.16748196501934,
908            -0.46048505533, -0.3887126918161, -0.68287320410729, -0.18448530888361,
909            -0.25358256326157, 0.26870280714361,
910            0.6889557358588, -0.3101022706485, -0.35882194962822, 0.30088738418801,
911            -0.039139540883101, -0.45646277242166,
912            -0.21954767479275, 0.40838837410593, 0.23284186868997,
913            0.30349649888064, 0.57233263099925, 0.55778817953937,
914            0.57731035290905, 0.091218309942656, 0.70670016667131,
915            0.016358033634041, 0.3939245235472, -0.059352634867484,
916            0.50055570130024, -0.021749790970703, 0.56767851040093,
917            0.50580176326624, 0.34691320957643, 0.22478399991032,
918            -0.37901911159632, 0.53804099887537, -0.46780195460858,
919            0.51497346779204, -0.27981005467588, 0.067278440906787,
920            0.67241900483514, 0.074099582737, 0.43138117954806, 0.054567519697911,
921            -0.37927768894619, 0.45764946429346,
922            0.14529189179172, -0.23854982910384, 0.45401647091062,
923            0.25466539906731, 0.46182069803887, -0.66160446396375,
924            -0.15570980059397, -0.38476787034627, 0.37322840954917,
925            -0.43977613626294, -0.61243005550684, -0.34631643815896,
926            -0.19590302894013, 0.42065974653653, 0.43447548638809,
927            -0.10575548452794, 0.70439951675651, -0.29754920754254,
928            -0.13558865796725, 0.1427073453776, 0.49647494823192,
929            -0.65533234019218, -0.11714854214663, 0.5211321311867,
930            -0.6228374766114, 0.20812698103217, -0.16205154548883,
931            0.20384566967497, -0.59321895467652, 0.38604941246779,
932            0.44487837128099, -0.37224943035393, -0.22188447638327,
933            0.48921538939858, 0.41432418029434, -0.45087099253189,
934            0.66422841315008, 0.21517761068003, 0.094012579794123,
935            -0.4358159040875, 0.22245680154647, -0.51404116085847,
936            -0.11369362736032, 0.32284689991698, -0.38818285117689,
937            0.49680024166881, 0.047684866166158, -0.69503480904222,
938            -0.5137200731924, -0.50673230867252, 0.32715252974108,
939            -0.26799714004956, -0.47616510509846, 0.27153195326233,
940            -0.47315177716491, -0.45711495983609, -0.31178280842352,
941            -0.51697763052226, -0.14302372043059, -0.42689944315384,
942            -0.050442035795027, 0.23609184251469, 0.38634880236106,
943            0.56012774305243, 0.38963669840218, -0.57174382424149,
944            -0.15472134925391, -0.15333579424307, -0.14189768300467,
945            0.032279269476252, -0.66054298438621, -0.70360180527557,
946            -0.10345191679557, -0.30503725808375, 0.31038263802383,
947            0.36878846502877, -0.76824774853417, 0.2714830658427,
948            -0.060212868606223, -0.4172755444983, 0.39199300681258,
949            -0.44040104260082, 0.24955102139032, -0.64215903203727,
950            0.25443195353315, -0.013789583113498, 0.44365000614699,
951            0.53296203342425, -0.55057750350733, -0.38867053403178,
952            -0.36068564301268, -0.65616661625162, -0.48495997865466,
953            0.24088316031012, -0.18080297655217, -0.33682435258394,
954            -0.53824550487673, -0.096728907851005, -0.5208619866167,
955            0.33195321221408, -0.032263947064791, 0.56427315050798,
956            0.40151657866643, -0.44825725748635, -0.54910020122855,
957            -0.095936272447708, 0.5719563905078, 0.00097783623607218,
958            0.21961099467771, 0.62823723408945, -0.010045934028323,
959            -0.6610564872634, -0.17161595423903, -0.30089924032373,
960            0.27961471530636, 0.054523395513076, 0.61485903249347,
961            0.11958885677663, -0.61032561244673, -0.39241856813031,
962            -0.30223065341134, -0.23605925177166, -0.09697276975263,
963            -0.46458104180761, -0.37853464945647, 0.69599203908657,
964            0.0023635513043496, 0.62702100484886, 0.49658954056984,
965            -0.20369645124455, -0.56457560315907, 0.00021299797811461,
966            -0.64198493892962, 0.59676262320476, 0.46274573284143,
967            0.088421912306785, 0.098029994490406, -0.012953072012707,
968            -0.053965435026011, 0.13439533803278, -0.33103493780685,
969            0.55991756423782, -0.58127599631056, -0.46696041830103,
970            -0.43965993689353, 0.07544961763381, 0.1509639518808,
971            -0.38868406689028, -0.0033436054452783, -0.79191533434483,
972            -0.21743914630025, -0.32019630124298, -0.56067107727615,
973            0.027284914419519, -0.49444926389798, -0.53908992599417,
974            -0.36492599248168, 0.52529904803377, 0.18002253442693,
975            0.14829474115897, 0.17212619314998, -0.71194315827942,
976            0.0051876209353066, 0.50490293404098, 0.24361032552454,
977            0.13688117617809, -0.61381291176911, -0.5386997104485,
978            0.66421180843392, 0.21833854629637, -0.087909936660014,
979            0.15624552502148, -0.68780724971724, 0.077015056461268,
980            0.52710630558705, -0.42143671471468, -0.069964559463205,
981            -0.24196341534187, -0.68814841622245, 0.08695091377684,
982            0.62392249806692, -0.23663281560035, -0.59058622185178,
983            0.22685863859977, -0.36683948058558, -0.14105848121323,
984            0.18069852004855, -0.083828559172887, 0.66240167877879,
985            0.16722813432165, -0.25503640214793, -0.65462662498637,
986            -0.37112528006203, 0.43100319401562, -0.11342774633614,
987            0.14418808646988, 0.5753326931164, 0.55842502411684,
988            0.55378724068611, 0.21098160548047, -0.3224976646632, 0.31268307369255,
989            -0.37624695517597, -0.55269271266764,
990            0.2601465870231, 0.56373458886982, -0.21638357910201, 0.41216916619413,
991            -0.25078072187299, -0.57873208070982,
992            0.11217864148346, 0.54196554704815, -0.31989128683717,
993            0.54691221598945, 0.24062434044524, 0.48409277788476,
994            0.087564423746579, -0.12083081671284, 0.69931172084498,
995            0.35220575672909, 0.28770484569954, -0.53091668762919,
996            0.3395702120398, 0.042520943289575, -0.30935928261896,
997            0.61022210846475, 0.54650816974112, 0.34079124619266,
998            0.32746112891934, 0.32095220193351, -0.61142534799442,
999            0.32197324480666, -0.38236071343678, 0.40749411210419,
1000            0.58741915356593, -0.30916030490652, -0.57642977381104,
1001            -0.038846190358607, 0.047926713761208, -0.4725265742377,
1002            0.026224389898652, 0.031768907187292, -0.12510902263321,
1003            0.36102734397001, -0.72217212865059, 0.57513252722531,
1004            -0.27510374152496, -0.5153402145828, 0.025774022629799,
1005            0.59201067073603, 0.40728366085253, -0.37645913420642,
1006            -0.29983338495183, -0.61017291361195, -0.18551919513643,
1007            0.50515945610161, 0.18206593801497, -0.46372136367049,
1008            -0.64290893575119, -0.34887011406157, -0.55318606770362,
1009            -0.21230198963112, -0.19828983785672, 0.2730419816548,
1010            -0.32778879906348, -0.094317293167129, 0.57811170538439,
1011            0.54346692190204, 0.17699503497579, -0.47197676839855,
1012            -0.075738705663962, 0.53381750682665, -0.13406342524856,
1013            0.71765386263773, 0.34271060834977, 0.24259408122628,
1014            -0.30574273227855, 0.17419449782542, -0.78861555508124,
1015            0.43305678368813, 0.064853328282818, 0.25003806266734,
1016            0.4397035983709, -0.51651518914239, -0.3972346186176,
1017            -0.34513492086703, 0.32129829777342, -0.39965829527563,
1018            -0.25184899643619, -0.35937572373004, 0.15273239148905,
1019            -0.51640931868766, 0.4218715745627, -0.58261460582976,
1020            -0.57396000790758, 0.1912786199605, 0.45995634753032,
1021            -0.43664716984512, 0.4601630113166, 0.14146310231856,
1022            0.11500068018889, 0.05112652754666, -0.25672855859366,
1023            -0.54715738035577, 0.67669928552409, 0.40118355777989,
1024            -0.45252668004418, -0.40809988524453, -0.064931545867856,
1025            0.19116562077283, 0.76523014995576, 0.048337406798767,
1026            -0.080075651760374, 0.75305314115418, 0.34797424409913,
1027            0.29104493928016, 0.0040185919664457, -0.46977598520425,
1028            -0.3890257668276, 0.49100041230416, -0.17812126809985,
1029            -0.43787557151231, -0.46923187878333, 0.40489108352503,
1030            0.37433236324043, -0.29441766760791, -0.066285137006724,
1031            0.33217472508825, 0.73917165688328, 0.33479099915638,
1032            -0.02973230696179, -0.51371026289118, 0.34133522703692,
1033            -0.41361792362786, -0.51561746819514, -0.4263412462482,
1034            0.51057171220039, -0.23740201245544, 0.26673587003088, 0.5521767379032,
1035            0.16849318602455, 0.52774964064755,
1036    };
1037
1038
1039    /**
1040     * Used by {@link #noise(double, double, double, double, long)} to look up the vertices of the 4D triangle analogue.
1041     */
1042    protected static final int[] SIMPLEX_4D = {0, 1, 3, 7, 0, 1, 7, 3,
1043            0, 0, 0, 0, 0, 3, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1044            0, 0, 0, 0, 1, 3, 7, 0, 0, 3, 1, 7, 0, 0, 0, 0,
1045            0, 7, 1, 3, 0, 7, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1046            0, 0, 0, 0, 1, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1047            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1048            0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 7, 0, 0, 0, 0,
1049            1, 7, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1050            3, 7, 0, 1, 3, 7, 1, 0, 1, 0, 3, 7, 1, 0, 7, 3,
1051            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 7, 1,
1052            0, 0, 0, 0, 3, 1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1053            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1054            0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 7, 0, 0, 0, 0,
1055            0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 1, 3, 7, 0, 3, 1,
1056            0, 0, 0, 0, 7, 1, 3, 0, 3, 1, 0, 7, 0, 0, 0, 0,
1057            0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 3, 0, 0, 0, 0,
1058            7, 3, 0, 1, 7, 3, 1, 0};
1059    
1060    protected static final double F2 = 0.36602540378443864676372317075294,
1061            G2 = 0.21132486540518711774542560974902,
1062            F3 = 1.0 / 3.0,
1063            G3 = 1.0 / 6.0,
1064            F4 = (Math.sqrt(5.0) - 1.0) * 0.25,
1065            G4 = (5.0 - Math.sqrt(5.0)) * 0.05,
1066            LIMIT4 = 0.62,
1067            F6 = (Math.sqrt(7.0) - 1.0) / 6.0,
1068            G6 = F6 / (1.0 + 6.0 * F6),
1069            LIMIT6 = 0.8375
1070            //LIMIT6 = 0.777
1071            //LIMIT6 = 0.86
1072            /*
1073            sideLength = (float)Math.sqrt(6.0) / (6f * F6 + 1f),
1074            a6 = (float)(Math.sqrt((sideLength * sideLength)
1075                    - ((sideLength * 0.5) * (sideLength * 0.5f)))),
1076            cornerFace = (float)Math.sqrt(a6 * a6 + (a6 * 0.5) * (a6 * 0.5)),
1077            cornerFaceSq = cornerFace * cornerFace,
1078            valueScaler = 9.5f
1079             */
1080            ;
1081    //Math.pow(5.0, -0.5) * (Math.pow(5.0, -3.5) * 100 + 13),
1082    
1083    public static double noise(final double x, final double y, final long seed) {
1084        final double s = (x + y) * F2;
1085        final int i = fastFloor(x + s),
1086                j = fastFloor(y + s);
1087        final double t = (i + j) * G2,
1088                X0 = i - t,
1089                Y0 = j - t,
1090                x0 = x - X0,
1091                y0 = y - Y0;
1092        int i1, j1;
1093        if (x0 > y0) {
1094            i1 = 1;
1095            j1 = 0;
1096        } else {
1097            i1 = 0;
1098            j1 = 1;
1099        }
1100        final double
1101                x1 = x0 - i1 + G2,
1102                y1 = y0 - j1 + G2,
1103                x2 = x0 - 1 + 2 * G2,
1104                y2 = y0 - 1 + 2 * G2;
1105        double n = 0.0;
1106        final int
1107                gi0 = hash256(i, j, seed),
1108                gi1 = hash256(i + i1, j + j1, seed),
1109                gi2 = hash256(i + 1, j + 1, seed);
1110        // Calculate the contribution from the three corners for 2D gradient
1111        double t0 = 0.75 - x0 * x0 - y0 * y0;
1112        if (t0 > 0) {
1113            t0 *= t0;
1114            n += t0 * t0 * (phiGrad2[gi0][0] * x0 + phiGrad2[gi0][1] * y0);
1115        }
1116        double t1 = 0.75 - x1 * x1 - y1 * y1;
1117        if (t1 > 0) {
1118            t1 *= t1;
1119            n += t1 * t1 * (phiGrad2[gi1][0] * x1 + phiGrad2[gi1][1] * y1);
1120        }
1121        double t2 = 0.75 - x2 * x2 - y2 * y2;
1122        if (t2 > 0)  {
1123            t2 *= t2;
1124            n += t2 * t2 * (phiGrad2[gi2][0] * x2 + phiGrad2[gi2][1] * y2);
1125        }
1126        // Add contributions from each corner to get the final noise value.
1127        // The result is clamped to return values in the interval [-1,1].
1128        return Math.max(-1.0, Math.min(1.0, 9.125f * n));
1129
1130//        double n0, n1, n2;
1131//        double t0 = 0.5 - x0 * x0 - y0 * y0;
1132//        if (t0 < 0)
1133//            n0 = 0;
1134//        else {
1135//            t0 *= t0;
1136//            n0 = t0 * t0 * (x0 * gradient2DLUT[h0] + y0 * gradient2DLUT[h0 | 1]);
1137//        }
1138//        double t1 = 0.5 - x1 * x1 - y1 * y1;
1139//        if (t1 < 0)
1140//            n1 = 0;
1141//        else {
1142//            t1 *= t1;
1143//            n1 = t1 * t1 * (x1 * gradient2DLUT[h1] + y1 * gradient2DLUT[h1 | 1]);
1144//        }
1145//        double t2 = 0.5 - x2 * x2 - y2 * y2;
1146//        if (t2 < 0)
1147//            n2 = 0;
1148//        else {
1149//            t2 *= t2;
1150//            n2 = t2 * t2 * (x2 * gradient2DLUT[h2] + y2 * gradient2DLUT[h2 | 1]);
1151//        }
1152//        return (70 * (n0 + n1 + n2)) * 1.42188695 + 0.001054489;
1153    }
1154
1155    public static double noise(final double x, final double y, final double z, final long seed) {
1156        double n = 0.0;
1157        final double s = (x + y + z) * F3;
1158        final int i = fastFloor(x + s),
1159                j = fastFloor(y + s),
1160                k = fastFloor(z + s);
1161
1162        final double t = (i + j + k) * G3;
1163        final double X0 = i - t, Y0 = j - t, Z0 = k - t,
1164                x0 = x - X0, y0 = y - Y0, z0 = z - Z0;
1165
1166        int i1, j1, k1;
1167        int i2, j2, k2;
1168
1169        if (x0 >= y0) {
1170            if (y0 >= z0) {
1171                i1 = 1;
1172                j1 = 0;
1173                k1 = 0;
1174                i2 = 1;
1175                j2 = 1;
1176                k2 = 0;
1177            } else if (x0 >= z0) {
1178                i1 = 1;
1179                j1 = 0;
1180                k1 = 0;
1181                i2 = 1;
1182                j2 = 0;
1183                k2 = 1;
1184            } else {
1185                i1 = 0;
1186                j1 = 0;
1187                k1 = 1;
1188                i2 = 1;
1189                j2 = 0;
1190                k2 = 1;
1191            }
1192        } else {
1193            if (y0 < z0) {
1194                i1 = 0;
1195                j1 = 0;
1196                k1 = 1;
1197                i2 = 0;
1198                j2 = 1;
1199                k2 = 1;
1200            } else if (x0 < z0) {
1201                i1 = 0;
1202                j1 = 1;
1203                k1 = 0;
1204                i2 = 0;
1205                j2 = 1;
1206                k2 = 1;
1207            } else {
1208                i1 = 0;
1209                j1 = 1;
1210                k1 = 0;
1211                i2 = 1;
1212                j2 = 1;
1213                k2 = 0;
1214            }
1215        }
1216
1217        double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
1218        double y1 = y0 - j1 + G3;
1219        double z1 = z0 - k1 + G3;
1220        double x2 = x0 - i2 + F3; // Offsets for third corner in (x,y,z) coords
1221        double y2 = y0 - j2 + F3;
1222        double z2 = z0 - k2 + F3;
1223        double x3 = x0 - 0.5; // Offsets for last corner in (x,y,z) coords
1224        double y3 = y0 - 0.5;
1225        double z3 = z0 - 0.5;
1226
1227        // Calculate the contribution from the four corners
1228        double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
1229        if (t0 > 0) {
1230            t0 *= t0;
1231            n += t0 * t0 * gradCoord3D(seed, i, j, k, x0, y0, z0);
1232        }
1233        double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
1234        if (t1 > 0) {
1235            t1 *= t1;
1236            n += t1 * t1 * gradCoord3D(seed, i + i1, j + j1, k + k1, x1, y1, z1);
1237        }
1238        double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
1239        if (t2 > 0) {
1240            t2 *= t2;
1241            n += t2 * t2 * gradCoord3D(seed, i + i2, j + j2, k + k2, x2, y2, z2);
1242        }
1243        double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
1244        if (t3 > 0) {
1245            t3 *= t3;
1246            n += t3 * t3 * gradCoord3D(seed, i + 1, j + 1, k + 1, x3, y3, z3);
1247        }
1248        // Add contributions from each corner to get the final noise value.
1249        // The result is clamped to stay just inside [-1,1]
1250        return Math.max(-1.0, Math.min(1.0, 31.5 * n));
1251        //return (32.0 * n) * 1.25086885 + 0.0003194984;
1252    }
1253
1254    public static double noise(final double x, final double y, final double z, final double w, final long seed) {
1255        double n = 0.0;
1256        final double s = (x + y + z + w) * F4;
1257        final int i = fastFloor(x + s), j = fastFloor(y + s), k = fastFloor(z + s), l = fastFloor(w + s);
1258        final double[] gradient4DLUT = grad4d;
1259        final double t = (i + j + k + l) * G4,
1260                X0 = i - t,
1261                Y0 = j - t,
1262                Z0 = k - t,
1263                W0 = l - t,
1264                x0 = x - X0,
1265                y0 = y - Y0,
1266                z0 = z - Z0,
1267                w0 = w - W0;
1268        final int c = (x0 > y0 ? 128 : 0) | (x0 > z0 ? 64 : 0) | (y0 > z0 ? 32 : 0) | (x0 > w0 ? 16 : 0) | (y0 > w0 ? 8 : 0) | (z0 > w0 ? 4 : 0);
1269        final int i1 = SIMPLEX_4D[c] >>> 2,
1270                j1 = SIMPLEX_4D[c | 1] >>> 2,
1271                k1 = SIMPLEX_4D[c | 2] >>> 2,
1272                l1 = SIMPLEX_4D[c | 3] >>> 2,
1273                i2 = SIMPLEX_4D[c] >>> 1 & 1,
1274                j2 = SIMPLEX_4D[c | 1] >>> 1 & 1,
1275                k2 = SIMPLEX_4D[c | 2] >>> 1 & 1,
1276                l2 = SIMPLEX_4D[c | 3] >>> 1 & 1,
1277                i3 = SIMPLEX_4D[c] & 1,
1278                j3 = SIMPLEX_4D[c | 1] & 1,
1279                k3 = SIMPLEX_4D[c | 2] & 1,
1280                l3 = SIMPLEX_4D[c | 3] & 1;
1281        final double x1 = x0 - i1 + G4,
1282                y1 = y0 - j1 + G4,
1283                z1 = z0 - k1 + G4,
1284                w1 = w0 - l1 + G4,
1285                x2 = x0 - i2 + 2 * G4,
1286                y2 = y0 - j2 + 2 * G4,
1287                z2 = z0 - k2 + 2 * G4,
1288                w2 = w0 - l2 + 2 * G4,
1289                x3 = x0 - i3 + 3 * G4,
1290                y3 = y0 - j3 + 3 * G4,
1291                z3 = z0 - k3 + 3 * G4,
1292                w3 = w0 - l3 + 3 * G4,
1293                x4 = x0 - 1 + 4 * G4,
1294                y4 = y0 - 1 + 4 * G4,
1295                z4 = z0 - 1 + 4 * G4,
1296                w4 = w0 - 1 + 4 * G4;
1297        final int h0 = (hash256(i, j, k, l, seed) & 0xFC),
1298                h1 = (hash256(i + i1, j + j1, k + k1, l + l1, seed) & 0xFC),
1299                h2 = (hash256(i + i2, j + j2, k + k2, l + l2, seed) & 0xFC),
1300                h3 = (hash256(i + i3, j + j3, k + k3, l + l3, seed) & 0xFC),
1301                h4 = (hash256(i + 1, j + 1, k + 1, l + 1, seed) & 0xFC);
1302        double t0 = LIMIT4 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
1303        if(t0 > 0) {
1304            t0 *= t0;
1305            n += t0 * t0 * (x0 * gradient4DLUT[h0] + y0 * gradient4DLUT[h0 | 1] + z0 * gradient4DLUT[h0 | 2] + w0 * gradient4DLUT[h0 | 3]);
1306        }
1307        double t1 = LIMIT4 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
1308        if (t1 > 0) {
1309            t1 *= t1;
1310            n += t1 * t1 * (x1 * gradient4DLUT[h1] + y1 * gradient4DLUT[h1 | 1] + z1 * gradient4DLUT[h1 | 2] + w1 * gradient4DLUT[h1 | 3]);
1311        }
1312        double t2 = LIMIT4 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
1313        if (t2 > 0) {
1314            t2 *= t2;
1315            n += t2 * t2 * (x2 * gradient4DLUT[h2] + y2 * gradient4DLUT[h2 | 1] + z2 * gradient4DLUT[h2 | 2] + w2 * gradient4DLUT[h2 | 3]);
1316        }
1317        double t3 = LIMIT4 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
1318        if (t3 > 0) {
1319            t3 *= t3;
1320            n += t3 * t3 * (x3 * gradient4DLUT[h3] + y3 * gradient4DLUT[h3 | 1] + z3 * gradient4DLUT[h3 | 2] + w3 * gradient4DLUT[h3 | 3]);
1321        }
1322        double t4 = LIMIT4 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
1323        if (t4 > 0) {
1324            t4 *= t4;
1325            n += t4 * t4 * (x4 * gradient4DLUT[h4] + y4 * gradient4DLUT[h4 | 1] + z4 * gradient4DLUT[h4 | 2] + w4 * gradient4DLUT[h4 | 3]);
1326        }
1327        //return NumberTools.bounce(5.0 + 41.0 * n);
1328        return Math.max(-1.0, Math.min(1.0, 14.75 * n));
1329    }
1330
1331    
1332    private static final double[] mShared = {0, 0, 0, 0, 0, 0}, cellDistShared = {0, 0, 0, 0, 0, 0};
1333    private static final int[] distOrderShared = {0, 0, 0, 0, 0, 0}, intLocShared = {0, 0, 0, 0, 0, 0};
1334
1335    public static double noise(final double x, final double y, final double z,
1336                               final double w, final double u, final double v, final long seed) {
1337        final double s = (x + y + z + w + u + v) * F6;
1338
1339        final int skewX = fastFloor(x + s), skewY = fastFloor(y + s), skewZ = fastFloor(z + s),
1340                skewW = fastFloor(w + s), skewU = fastFloor(u + s), skewV = fastFloor(v + s);
1341        final double[] m = mShared, cellDist = cellDistShared, gradient6DLUT = SeededNoise.gradient6DLUT;
1342        final int[] distOrder = distOrderShared,
1343                intLoc = intLocShared;
1344        intLoc[0] = skewX;
1345        intLoc[1] = skewY;
1346        intLoc[2] = skewZ;
1347        intLoc[3] = skewW;
1348        intLoc[4] = skewU;
1349        intLoc[5] = skewV;
1350
1351        final double unskew = (skewX + skewY + skewZ + skewW + skewU + skewV) * G6;
1352        cellDist[0] = x - skewX + unskew;
1353        cellDist[1] = y - skewY + unskew;
1354        cellDist[2] = z - skewZ + unskew;
1355        cellDist[3] = w - skewW + unskew;
1356        cellDist[4] = u - skewU + unskew;
1357        cellDist[5] = v - skewV + unskew;
1358
1359        int o0 = (cellDist[0]<cellDist[1]?1:0)+(cellDist[0]<cellDist[2]?1:0)+(cellDist[0]<cellDist[3]?1:0)+(cellDist[0]<cellDist[4]?1:0)+(cellDist[0]<cellDist[5]?1:0);
1360        int o1 = (cellDist[1]<=cellDist[0]?1:0)+(cellDist[1]<cellDist[2]?1:0)+(cellDist[1]<cellDist[3]?1:0)+(cellDist[1]<cellDist[4]?1:0)+(cellDist[1]<cellDist[5]?1:0);
1361        int o2 = (cellDist[2]<=cellDist[0]?1:0)+(cellDist[2]<=cellDist[1]?1:0)+(cellDist[2]<cellDist[3]?1:0)+(cellDist[2]<cellDist[4]?1:0)+(cellDist[2]<cellDist[5]?1:0);
1362        int o3 = (cellDist[3]<=cellDist[0]?1:0)+(cellDist[3]<=cellDist[1]?1:0)+(cellDist[3]<=cellDist[2]?1:0)+(cellDist[3]<cellDist[4]?1:0)+(cellDist[3]<cellDist[5]?1:0);
1363        int o4 = (cellDist[4]<=cellDist[0]?1:0)+(cellDist[4]<=cellDist[1]?1:0)+(cellDist[4]<=cellDist[2]?1:0)+(cellDist[4]<=cellDist[3]?1:0)+(cellDist[4]<cellDist[5]?1:0);
1364        int o5 = 15-(o0+o1+o2+o3+o4);
1365
1366        distOrder[o0]=0;
1367        distOrder[o1]=1;
1368        distOrder[o2]=2;
1369        distOrder[o3]=3;
1370        distOrder[o4]=4;
1371        distOrder[o5]=5;
1372
1373        double n = 0;
1374        double skewOffset = 0;
1375
1376        for (int c = -1; c < 6; c++) {
1377            if (c != -1) intLoc[distOrder[c]]++;
1378
1379            m[0] = cellDist[0] - (intLoc[0] - skewX) + skewOffset;
1380            m[1] = cellDist[1] - (intLoc[1] - skewY) + skewOffset;
1381            m[2] = cellDist[2] - (intLoc[2] - skewZ) + skewOffset;
1382            m[3] = cellDist[3] - (intLoc[3] - skewW) + skewOffset;
1383            m[4] = cellDist[4] - (intLoc[4] - skewU) + skewOffset;
1384            m[5] = cellDist[5] - (intLoc[5] - skewV) + skewOffset;
1385
1386            double tc = LIMIT6;
1387
1388            for (int d = 0; d < 6; d++) {
1389                tc -= m[d] * m[d];
1390            }
1391
1392            if (tc > 0) {
1393                final int h = hash256(intLoc[0], intLoc[1], intLoc[2], intLoc[3],
1394                        intLoc[4], intLoc[5], seed) * 6;
1395                final double gr = gradient6DLUT[h] * m[0] + gradient6DLUT[h + 1] * m[1]
1396                        + gradient6DLUT[h + 2] * m[2] + gradient6DLUT[h + 3] * m[3]
1397                        + gradient6DLUT[h + 4] * m[4] + gradient6DLUT[h + 5] * m[5];
1398                tc *= tc;
1399                n += gr * tc * tc;
1400            }
1401            skewOffset += G6;
1402        }
1403        return Math.max(-1.0, Math.min(1.0, 7.5f * n));
1404    }
1405
1406}