Repository navigation
Expand file tree
/
Copy pathOpenSimplex2F.c
More file actions
1530 lines (1360 loc) · 58.4 KB
/
Copy pathOpenSimplex2F.c
File metadata and controls
1530 lines (1360 loc) · 58.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019-2020 KdotJPG (original work, previous license:
* CC0 1.0 Universal, public domain dedication).
*
* Copyright (C) 2026 QuasarApp Group DOO
* Licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0),
* see the accompanying LICENSE file.
*/
/**
* K.jpg's OpenSimplex 2, faster variant
*
* - 2D is standard simplex implemented using a lookup table.
* - 3D is "Re-oriented 4-point BCC noise" which constructs a
* congruent BCC lattice in a much different way than usual.
* - 4D constructs the lattice as a union of five copies of its
* reciprocal. It successively finds the closest point on each.
*
* Multiple versions of each function are provided. See the
* documentation above each, for more info.
*/
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include "OpenSimplex2F.h"
#define PSIZE (2048)
#define PMASK (2047)
static const double N1 = 0.009202377986303158;
static const double N2 = 0.01001634121365712;
static const double N3 = 0.030485933181293584;
static const double N4 = 0.009202377986303158;
#define B0000 0x00
#define B0001 0x01
#define B0010 0x02
#define B0011 0x03
#define B0100 0x04
#define B0101 0x05
#define B0110 0x06
#define B0111 0x07
#define B1000 0x08
#define B1001 0x09
#define B1010 0x0a
#define B1011 0x0b
#define B1100 0x0c
#define B1101 0x0d
#define B1110 0x0e
#define B1111 0x0f
/* 5D bit constants for the A5 lattice */
#define B00000 0x00
#define B00001 0x01
#define B00010 0x02
#define B00011 0x03
#define B00100 0x04
#define B00101 0x05
#define B00110 0x06
#define B00111 0x07
#define B01000 0x08
#define B01001 0x09
#define B01010 0x0a
#define B01011 0x0b
#define B01100 0x0c
#define B01101 0x0d
#define B01110 0x0e
#define B01111 0x0f
#define B10000 0x10
#define B10001 0x11
#define B10010 0x12
#define B10011 0x13
#define B10100 0x14
#define B10101 0x15
#define B10110 0x16
#define B10111 0x17
#define B11000 0x18
#define B11001 0x19
#define B11010 0x1a
#define B11011 0x1b
#define B11100 0x1c
#define B11101 0x1d
#define B11110 0x1e
#define B11111 0x1f
struct Grad1 {
double dx;
};
struct Grad2 {
double dx, dy;
};
struct Grad3 {
double dx, dy, dz;
};
struct Grad4 {
double dx, dy, dz, dw;
};
struct Grad5 {
double dx, dy, dz, dw, dv;
};
struct LatticePoint2D {
int xsv, ysv;
double dx, dy;
};
static struct LatticePoint2D *new_LatticePoint2D(int xsv, int ysv)
{
struct LatticePoint2D *this = calloc(1, sizeof(*this));
this->xsv = xsv; this->ysv = ysv;
double ssv = (xsv + ysv) * -0.211324865405187;
this->dx = -xsv - ssv;
this->dy = -ysv - ssv;
return this;
}
struct LatticePoint3D {
double dxr, dyr, dzr;
int xrv, yrv, zrv;
struct LatticePoint3D *nextOnFailure, *nextOnSuccess;
};
static struct LatticePoint3D *new_LatticePoint3D(int xrv, int yrv, int zrv, int lattice)
{
struct LatticePoint3D *this = calloc(1, sizeof(*this));
this->dxr = -xrv + lattice * 0.5; this->dyr = -yrv + lattice * 0.5; this->dzr = -zrv + lattice * 0.5;
this->xrv = xrv + lattice * 1024; this->yrv = yrv + lattice * 1024; this->zrv = zrv + lattice * 1024;
return this;
}
static void find_unique_pointers(struct LatticePoint3D *tree, struct LatticePoint3D *list[], int *n)
{
if (!tree)
return;
int i, found;
found = 0;
for (i = 0; i < *n; i++) {
if (list[i] == tree) {
found = 1;
break;
}
}
if (!found) {
list[*n] = tree;
(*n)++;
}
find_unique_pointers(tree->nextOnFailure, list, n);
find_unique_pointers(tree->nextOnSuccess, list, n);
}
static void free_LatticePoint3D(struct LatticePoint3D *list[], int n)
{
int i;
for (i = 0; i < n; i++)
free(list[i]);
}
struct LatticePoint4D {
int xsv, ysv, zsv, wsv;
double dx, dy, dz, dw;
double xsi, ysi, zsi, wsi;
double ssiDelta;
};
struct LatticePoint5D {
int xsv, ysv, zsv, wsv, vsv;
double dx, dy, dz, dw, dv;
double xsi, ysi, zsi, wsi, vsvi;
double ssiDelta;
};
static struct LatticePoint4D *new_LatticePoint4D(int xsv, int ysv, int zsv, int wsv)
{
struct LatticePoint4D *this = calloc(1, sizeof(*this));
this->xsv = xsv + 409; this->ysv = ysv + 409; this->zsv = zsv + 409; this->wsv = wsv + 409;
double ssv = (xsv + ysv + zsv + wsv) * 0.309016994374947;
this->dx = -xsv - ssv;
this->dy = -ysv - ssv;
this->dz = -zsv - ssv;
this->dw = -wsv - ssv;
this->xsi = 0.2 - xsv;
this->ysi = 0.2 - ysv;
this->zsi = 0.2 - zsv;
this->wsi = 0.2 - wsv;
this->ssiDelta = (0.8 - xsv - ysv - zsv - wsv) * 0.309016994374947;
return this;
}
static struct LatticePoint5D *new_LatticePoint5D(int xsv, int ysv, int zsv, int wsv, int vsv)
{
struct LatticePoint5D *this = calloc(1, sizeof(*this));
this->xsv = xsv + 817; this->ysv = ysv + 817; this->zsv = zsv + 817; this->wsv = wsv + 817; this->vsv = vsv + 817;
double ssv = (xsv + ysv + zsv + wsv + vsv) * 0.309016994374947;
this->dx = -xsv - ssv;
this->dy = -ysv - ssv;
this->dz = -zsv - ssv;
this->dw = -wsv - ssv;
this->dv = -vsv - ssv;
this->xsi = 0.2 - xsv;
this->ysi = 0.2 - ysv;
this->zsi = 0.2 - zsv;
this->wsi = 0.2 - wsv;
this->vsvi = 0.2 - vsv;
this->ssiDelta = (1.6 - xsv - ysv - zsv - wsv - vsv) * 0.309016994374947;
return this;
}
struct OpenSimplex2F_context {
struct Grad1 *permGrad1;
int16_t *perm;
struct Grad2 *permGrad2;
struct Grad3 *permGrad3;
struct Grad4 *permGrad4;
struct Grad5 *permGrad5;
};
#define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
static struct Grad2 GRADIENTS_2D[PSIZE];
static struct Grad3 GRADIENTS_3D[PSIZE];
static struct Grad4 GRADIENTS_4D[PSIZE];
static struct Grad5 GRADIENTS_5D[PSIZE];
static struct Grad1 GRADIENTS_1D[PSIZE];
static struct LatticePoint2D *LOOKUP_2D[4];
static struct LatticePoint3D *LOOKUP_3D[8];
static struct LatticePoint4D *VERTICES_4D[16];
static struct LatticePoint5D *VERTICES_5D[32];
static struct Grad2 grad2[] = {
{ 0.130526192220052, 0.99144486137381},
{ 0.38268343236509, 0.923879532511287},
{ 0.608761429008721, 0.793353340291235},
{ 0.793353340291235, 0.608761429008721},
{ 0.923879532511287, 0.38268343236509},
{ 0.99144486137381, 0.130526192220051},
{ 0.99144486137381, -0.130526192220051},
{ 0.923879532511287, -0.38268343236509},
{ 0.793353340291235, -0.60876142900872},
{ 0.608761429008721, -0.793353340291235},
{ 0.38268343236509, -0.923879532511287},
{ 0.130526192220052, -0.99144486137381},
{-0.130526192220052, -0.99144486137381},
{-0.38268343236509, -0.923879532511287},
{-0.608761429008721, -0.793353340291235},
{-0.793353340291235, -0.608761429008721},
{-0.923879532511287, -0.38268343236509},
{-0.99144486137381, -0.130526192220052},
{-0.99144486137381, 0.130526192220051},
{-0.923879532511287, 0.38268343236509},
{-0.793353340291235, 0.608761429008721},
{-0.608761429008721, 0.793353340291235},
{-0.38268343236509, 0.923879532511287},
{-0.130526192220052, 0.99144486137381}
};
static struct Grad3 grad3[] = {
{-2.22474487139, -2.22474487139, -1.0},
{-2.22474487139, -2.22474487139, 1.0},
{-3.0862664687972017, -1.1721513422464978, 0.0},
{-1.1721513422464978, -3.0862664687972017, 0.0},
{-2.22474487139, -1.0, -2.22474487139},
{-2.22474487139, 1.0, -2.22474487139},
{-1.1721513422464978, 0.0, -3.0862664687972017},
{-3.0862664687972017, 0.0, -1.1721513422464978},
{-2.22474487139, -1.0, 2.22474487139},
{-2.22474487139, 1.0, 2.22474487139},
{-3.0862664687972017, 0.0, 1.1721513422464978},
{-1.1721513422464978, 0.0, 3.0862664687972017},
{-2.22474487139, 2.22474487139, -1.0},
{-2.22474487139, 2.22474487139, 1.0},
{-1.1721513422464978, 3.0862664687972017, 0.0},
{-3.0862664687972017, 1.1721513422464978, 0.0},
{-1.0, -2.22474487139, -2.22474487139},
{ 1.0, -2.22474487139, -2.22474487139},
{ 0.0, -3.0862664687972017, -1.1721513422464978},
{ 0.0, -1.1721513422464978, -3.0862664687972017},
{-1.0, -2.22474487139, 2.22474487139},
{ 1.0, -2.22474487139, 2.22474487139},
{ 0.0, -1.1721513422464978, 3.0862664687972017},
{ 0.0, -3.0862664687972017, 1.1721513422464978},
{-1.0, 2.22474487139, -2.22474487139},
{ 1.0, 2.22474487139, -2.22474487139},
{ 0.0, 1.1721513422464978, -3.0862664687972017},
{ 0.0, 3.0862664687972017, -1.1721513422464978},
{-1.0, 2.22474487139, 2.22474487139},
{ 1.0, 2.22474487139, 2.22474487139},
{ 0.0, 3.0862664687972017, 1.1721513422464978},
{ 0.0, 1.1721513422464978, 3.0862664687972017},
{ 2.22474487139, -2.22474487139, -1.0},
{ 2.22474487139, -2.22474487139, 1.0},
{ 1.1721513422464978, -3.0862664687972017, 0.0},
{ 3.0862664687972017, -1.1721513422464978, 0.0},
{ 2.22474487139, -1.0, -2.22474487139},
{ 2.22474487139, 1.0, -2.22474487139},
{ 3.0862664687972017, 0.0, -1.1721513422464978},
{ 1.1721513422464978, 0.0, -3.0862664687972017},
{ 2.22474487139, -1.0, 2.22474487139},
{ 2.22474487139, 1.0, 2.22474487139},
{ 1.1721513422464978, 0.0, 3.0862664687972017},
{ 3.0862664687972017, 0.0, 1.1721513422464978},
{ 2.22474487139, 2.22474487139, -1.0},
{ 2.22474487139, 2.22474487139, 1.0},
{ 3.0862664687972017, 1.1721513422464978, 0.0},
{ 1.1721513422464978, 3.0862664687972017, 0.0}
};
static struct Grad4 grad4[] = {
{-0.753341017856078, -0.37968289875261624, -0.37968289875261624, -0.37968289875261624},
{-0.7821684431180708, -0.4321472685365301, -0.4321472685365301, 0.12128480194602098},
{-0.7821684431180708, -0.4321472685365301, 0.12128480194602098, -0.4321472685365301},
{-0.7821684431180708, 0.12128480194602098, -0.4321472685365301, -0.4321472685365301},
{-0.8586508742123365, -0.508629699630796, 0.044802370851755174, 0.044802370851755174},
{-0.8586508742123365, 0.044802370851755174, -0.508629699630796, 0.044802370851755174},
{-0.8586508742123365, 0.044802370851755174, 0.044802370851755174, -0.508629699630796},
{-0.9982828964265062, -0.03381941603233842, -0.03381941603233842, -0.03381941603233842},
{-0.37968289875261624, -0.753341017856078, -0.37968289875261624, -0.37968289875261624},
{-0.4321472685365301, -0.7821684431180708, -0.4321472685365301, 0.12128480194602098},
{-0.4321472685365301, -0.7821684431180708, 0.12128480194602098, -0.4321472685365301},
{ 0.12128480194602098, -0.7821684431180708, -0.4321472685365301, -0.4321472685365301},
{-0.508629699630796, -0.8586508742123365, 0.044802370851755174, 0.044802370851755174},
{ 0.044802370851755174, -0.8586508742123365, -0.508629699630796, 0.044802370851755174},
{ 0.044802370851755174, -0.8586508742123365, 0.044802370851755174, -0.508629699630796},
{-0.03381941603233842, -0.9982828964265062, -0.03381941603233842, -0.03381941603233842},
{-0.37968289875261624, -0.37968289875261624, -0.753341017856078, -0.37968289875261624},
{-0.4321472685365301, -0.4321472685365301, -0.7821684431180708, 0.12128480194602098},
{-0.4321472685365301, 0.12128480194602098, -0.7821684431180708, -0.4321472685365301},
{ 0.12128480194602098, -0.4321472685365301, -0.7821684431180708, -0.4321472685365301},
{-0.508629699630796, 0.044802370851755174, -0.8586508742123365, 0.044802370851755174},
{ 0.044802370851755174, -0.508629699630796, -0.8586508742123365, 0.044802370851755174},
{ 0.044802370851755174, 0.044802370851755174, -0.8586508742123365, -0.508629699630796},
{-0.03381941603233842, -0.03381941603233842, -0.9982828964265062, -0.03381941603233842},
{-0.37968289875261624, -0.37968289875261624, -0.37968289875261624, -0.753341017856078},
{-0.4321472685365301, -0.4321472685365301, 0.12128480194602098, -0.7821684431180708},
{-0.4321472685365301, 0.12128480194602098, -0.4321472685365301, -0.7821684431180708},
{ 0.12128480194602098, -0.4321472685365301, -0.4321472685365301, -0.7821684431180708},
{-0.508629699630796, 0.044802370851755174, 0.044802370851755174, -0.8586508742123365},
{ 0.044802370851755174, -0.508629699630796, 0.044802370851755174, -0.8586508742123365},
{ 0.044802370851755174, 0.044802370851755174, -0.508629699630796, -0.8586508742123365},
{-0.03381941603233842, -0.03381941603233842, -0.03381941603233842, -0.9982828964265062},
{-0.6740059517812944, -0.3239847771997537, -0.3239847771997537, 0.5794684678643381},
{-0.7504883828755602, -0.4004672082940195, 0.15296486218853164, 0.5029860367700724},
{-0.7504883828755602, 0.15296486218853164, -0.4004672082940195, 0.5029860367700724},
{-0.8828161875373585, 0.08164729285680945, 0.08164729285680945, 0.4553054119602712},
{-0.4553054119602712, -0.08164729285680945, -0.08164729285680945, 0.8828161875373585},
{-0.5029860367700724, -0.15296486218853164, 0.4004672082940195, 0.7504883828755602},
{-0.5029860367700724, 0.4004672082940195, -0.15296486218853164, 0.7504883828755602},
{-0.5794684678643381, 0.3239847771997537, 0.3239847771997537, 0.6740059517812944},
{-0.3239847771997537, -0.6740059517812944, -0.3239847771997537, 0.5794684678643381},
{-0.4004672082940195, -0.7504883828755602, 0.15296486218853164, 0.5029860367700724},
{ 0.15296486218853164, -0.7504883828755602, -0.4004672082940195, 0.5029860367700724},
{ 0.08164729285680945, -0.8828161875373585, 0.08164729285680945, 0.4553054119602712},
{-0.08164729285680945, -0.4553054119602712, -0.08164729285680945, 0.8828161875373585},
{-0.15296486218853164, -0.5029860367700724, 0.4004672082940195, 0.7504883828755602},
{ 0.4004672082940195, -0.5029860367700724, -0.15296486218853164, 0.7504883828755602},
{ 0.3239847771997537, -0.5794684678643381, 0.3239847771997537, 0.6740059517812944},
{-0.3239847771997537, -0.3239847771997537, -0.6740059517812944, 0.5794684678643381},
{-0.4004672082940195, 0.15296486218853164, -0.7504883828755602, 0.5029860367700724},
{ 0.15296486218853164, -0.4004672082940195, -0.7504883828755602, 0.5029860367700724},
{ 0.08164729285680945, 0.08164729285680945, -0.8828161875373585, 0.4553054119602712},
{-0.08164729285680945, -0.08164729285680945, -0.4553054119602712, 0.8828161875373585},
{-0.15296486218853164, 0.4004672082940195, -0.5029860367700724, 0.7504883828755602},
{ 0.4004672082940195, -0.15296486218853164, -0.5029860367700724, 0.7504883828755602},
{ 0.3239847771997537, 0.3239847771997537, -0.5794684678643381, 0.6740059517812944},
{-0.6740059517812944, -0.3239847771997537, 0.5794684678643381, -0.3239847771997537},
{-0.7504883828755602, -0.4004672082940195, 0.5029860367700724, 0.15296486218853164},
{-0.7504883828755602, 0.15296486218853164, 0.5029860367700724, -0.4004672082940195},
{-0.8828161875373585, 0.08164729285680945, 0.4553054119602712, 0.08164729285680945},
{-0.4553054119602712, -0.08164729285680945, 0.8828161875373585, -0.08164729285680945},
{-0.5029860367700724, -0.15296486218853164, 0.7504883828755602, 0.4004672082940195},
{-0.5029860367700724, 0.4004672082940195, 0.7504883828755602, -0.15296486218853164},
{-0.5794684678643381, 0.3239847771997537, 0.6740059517812944, 0.3239847771997537},
{-0.3239847771997537, -0.6740059517812944, 0.5794684678643381, -0.3239847771997537},
{-0.4004672082940195, -0.7504883828755602, 0.5029860367700724, 0.15296486218853164},
{ 0.15296486218853164, -0.7504883828755602, 0.5029860367700724, -0.4004672082940195},
{ 0.08164729285680945, -0.8828161875373585, 0.4553054119602712, 0.08164729285680945},
{-0.08164729285680945, -0.4553054119602712, 0.8828161875373585, -0.08164729285680945},
{-0.15296486218853164, -0.5029860367700724, 0.7504883828755602, 0.4004672082940195},
{ 0.4004672082940195, -0.5029860367700724, 0.7504883828755602, -0.15296486218853164},
{ 0.3239847771997537, -0.5794684678643381, 0.6740059517812944, 0.3239847771997537},
{-0.3239847771997537, -0.3239847771997537, 0.5794684678643381, -0.6740059517812944},
{-0.4004672082940195, 0.15296486218853164, 0.5029860367700724, -0.7504883828755602},
{ 0.15296486218853164, -0.4004672082940195, 0.5029860367700724, -0.7504883828755602},
{ 0.08164729285680945, 0.08164729285680945, 0.4553054119602712, -0.8828161875373585},
{-0.08164729285680945, -0.08164729285680945, 0.8828161875373585, -0.4553054119602712},
{-0.15296486218853164, 0.4004672082940195, 0.7504883828755602, -0.5029860367700724},
{ 0.4004672082940195, -0.15296486218853164, 0.7504883828755602, -0.5029860367700724},
{ 0.3239847771997537, 0.3239847771997537, 0.6740059517812944, -0.5794684678643381},
{-0.6740059517812944, 0.5794684678643381, -0.3239847771997537, -0.3239847771997537},
{-0.7504883828755602, 0.5029860367700724, -0.4004672082940195, 0.15296486218853164},
{-0.7504883828755602, 0.5029860367700724, 0.15296486218853164, -0.4004672082940195},
{-0.8828161875373585, 0.4553054119602712, 0.08164729285680945, 0.08164729285680945},
{-0.4553054119602712, 0.8828161875373585, -0.08164729285680945, -0.08164729285680945},
{-0.5029860367700724, 0.7504883828755602, -0.15296486218853164, 0.4004672082940195},
{-0.5029860367700724, 0.7504883828755602, 0.4004672082940195, -0.15296486218853164},
{-0.5794684678643381, 0.6740059517812944, 0.3239847771997537, 0.3239847771997537},
{-0.3239847771997537, 0.5794684678643381, -0.6740059517812944, -0.3239847771997537},
{-0.4004672082940195, 0.5029860367700724, -0.7504883828755602, 0.15296486218853164},
{ 0.15296486218853164, 0.5029860367700724, -0.7504883828755602, -0.4004672082940195},
{ 0.08164729285680945, 0.4553054119602712, -0.8828161875373585, 0.08164729285680945},
{-0.08164729285680945, 0.8828161875373585, -0.4553054119602712, -0.08164729285680945},
{-0.15296486218853164, 0.7504883828755602, -0.5029860367700724, 0.4004672082940195},
{ 0.4004672082940195, 0.7504883828755602, -0.5029860367700724, -0.15296486218853164},
{ 0.3239847771997537, 0.6740059517812944, -0.5794684678643381, 0.3239847771997537},
{-0.3239847771997537, 0.5794684678643381, -0.3239847771997537, -0.6740059517812944},
{-0.4004672082940195, 0.5029860367700724, 0.15296486218853164, -0.7504883828755602},
{ 0.15296486218853164, 0.5029860367700724, -0.4004672082940195, -0.7504883828755602},
{ 0.08164729285680945, 0.4553054119602712, 0.08164729285680945, -0.8828161875373585},
{-0.08164729285680945, 0.8828161875373585, -0.08164729285680945, -0.4553054119602712},
{-0.15296486218853164, 0.7504883828755602, 0.4004672082940195, -0.5029860367700724},
{ 0.4004672082940195, 0.7504883828755602, -0.15296486218853164, -0.5029860367700724},
{ 0.3239847771997537, 0.6740059517812944, 0.3239847771997537, -0.5794684678643381},
{ 0.5794684678643381, -0.6740059517812944, -0.3239847771997537, -0.3239847771997537},
{ 0.5029860367700724, -0.7504883828755602, -0.4004672082940195, 0.15296486218853164},
{ 0.5029860367700724, -0.7504883828755602, 0.15296486218853164, -0.4004672082940195},
{ 0.4553054119602712, -0.8828161875373585, 0.08164729285680945, 0.08164729285680945},
{ 0.8828161875373585, -0.4553054119602712, -0.08164729285680945, -0.08164729285680945},
{ 0.7504883828755602, -0.5029860367700724, -0.15296486218853164, 0.4004672082940195},
{ 0.7504883828755602, -0.5029860367700724, 0.4004672082940195, -0.15296486218853164},
{ 0.6740059517812944, -0.5794684678643381, 0.3239847771997537, 0.3239847771997537},
{ 0.5794684678643381, -0.3239847771997537, -0.6740059517812944, -0.3239847771997537},
{ 0.5029860367700724, -0.4004672082940195, -0.7504883828755602, 0.15296486218853164},
{ 0.5029860367700724, 0.15296486218853164, -0.7504883828755602, -0.4004672082940195},
{ 0.4553054119602712, 0.08164729285680945, -0.8828161875373585, 0.08164729285680945},
{ 0.8828161875373585, -0.08164729285680945, -0.4553054119602712, -0.08164729285680945},
{ 0.7504883828755602, -0.15296486218853164, -0.5029860367700724, 0.4004672082940195},
{ 0.7504883828755602, 0.4004672082940195, -0.5029860367700724, -0.15296486218853164},
{ 0.6740059517812944, 0.3239847771997537, -0.5794684678643381, 0.3239847771997537},
{ 0.5794684678643381, -0.3239847771997537, -0.3239847771997537, -0.6740059517812944},
{ 0.5029860367700724, -0.4004672082940195, 0.15296486218853164, -0.7504883828755602},
{ 0.5029860367700724, 0.15296486218853164, -0.4004672082940195, -0.7504883828755602},
{ 0.4553054119602712, 0.08164729285680945, 0.08164729285680945, -0.8828161875373585},
{ 0.8828161875373585, -0.08164729285680945, -0.08164729285680945, -0.4553054119602712},
{ 0.7504883828755602, -0.15296486218853164, 0.4004672082940195, -0.5029860367700724},
{ 0.7504883828755602, 0.4004672082940195, -0.15296486218853164, -0.5029860367700724},
{ 0.6740059517812944, 0.3239847771997537, 0.3239847771997537, -0.5794684678643381},
{ 0.03381941603233842, 0.03381941603233842, 0.03381941603233842, 0.9982828964265062},
{-0.044802370851755174, -0.044802370851755174, 0.508629699630796, 0.8586508742123365},
{-0.044802370851755174, 0.508629699630796, -0.044802370851755174, 0.8586508742123365},
{-0.12128480194602098, 0.4321472685365301, 0.4321472685365301, 0.7821684431180708},
{ 0.508629699630796, -0.044802370851755174, -0.044802370851755174, 0.8586508742123365},
{ 0.4321472685365301, -0.12128480194602098, 0.4321472685365301, 0.7821684431180708},
{ 0.4321472685365301, 0.4321472685365301, -0.12128480194602098, 0.7821684431180708},
{ 0.37968289875261624, 0.37968289875261624, 0.37968289875261624, 0.753341017856078},
{ 0.03381941603233842, 0.03381941603233842, 0.9982828964265062, 0.03381941603233842},
{-0.044802370851755174, 0.044802370851755174, 0.8586508742123365, 0.508629699630796},
{-0.044802370851755174, 0.508629699630796, 0.8586508742123365, -0.044802370851755174},
{-0.12128480194602098, 0.4321472685365301, 0.7821684431180708, 0.4321472685365301},
{ 0.508629699630796, -0.044802370851755174, 0.8586508742123365, -0.044802370851755174},
{ 0.4321472685365301, -0.12128480194602098, 0.7821684431180708, 0.4321472685365301},
{ 0.4321472685365301, 0.4321472685365301, 0.7821684431180708, -0.12128480194602098},
{ 0.37968289875261624, 0.37968289875261624, 0.753341017856078, 0.37968289875261624},
{ 0.03381941603233842, 0.9982828964265062, 0.03381941603233842, 0.03381941603233842},
{-0.044802370851755174, 0.8586508742123365, -0.044802370851755174, 0.508629699630796},
{-0.044802370851755174, 0.8586508742123365, 0.508629699630796, -0.044802370851755174},
{-0.12128480194602098, 0.7821684431180708, 0.4321472685365301, 0.4321472685365301},
{ 0.508629699630796, 0.8586508742123365, -0.044802370851755174, -0.044802370851755174},
{ 0.4321472685365301, 0.7821684431180708, -0.12128480194602098, 0.4321472685365301},
{ 0.4321472685365301, 0.7821684431180708, 0.4321472685365301, -0.12128480194602098},
{ 0.37968289875261624, 0.753341017856078, 0.37968289875261624, 0.37968289875261624},
{ 0.9982828964265062, 0.03381941603233842, 0.03381941603233842, 0.03381941603233842},
{ 0.8586508742123365, -0.044802370851755174, -0.044802370851755174, 0.508629699630796},
{ 0.8586508742123365, -0.044802370851755174, 0.508629699630796, -0.044802370851755174},
{ 0.7821684431180708, -0.12128480194602098, 0.4321472685365301, 0.4321472685365301},
{ 0.8586508742123365, 0.508629699630796, -0.044802370851755174, -0.044802370851755174},
{ 0.7821684431180708, 0.4321472685365301, -0.12128480194602098, 0.4321472685365301},
{ 0.7821684431180708, 0.4321472685365301, 0.4321472685365301, -0.12128480194602098},
{ 0.753341017856078, 0.37968289875261624, 0.37968289875261624, 0.37968289875261624}
};
static struct Grad5 grad5[] = {
{ 1.0, 1.0, 1.0, 1.0, 1.0 },
{ 1.0, 1.0, 1.0, 1.0, -1.0 },
{ 1.0, 1.0, 1.0, -1.0, 1.0 },
{ 1.0, 1.0, 1.0, -1.0, -1.0 },
{ 1.0, 1.0, -1.0, 1.0, 1.0 },
{ 1.0, 1.0, -1.0, 1.0, -1.0 },
{ 1.0, 1.0, -1.0, -1.0, 1.0 },
{ 1.0, 1.0, -1.0, -1.0, -1.0 },
{ 1.0, -1.0, 1.0, 1.0, 1.0 },
{ 1.0, -1.0, 1.0, 1.0, -1.0 },
{ 1.0, -1.0, 1.0, -1.0, 1.0 },
{ 1.0, -1.0, 1.0, -1.0, -1.0 },
{ 1.0, -1.0, -1.0, 1.0, 1.0 },
{ 1.0, -1.0, -1.0, 1.0, -1.0 },
{ 1.0, -1.0, -1.0, -1.0, 1.0 },
{ 1.0, -1.0, -1.0, -1.0, -1.0 },
{-1.0, 1.0, 1.0, 1.0, 1.0 },
{-1.0, 1.0, 1.0, 1.0, -1.0 },
{-1.0, 1.0, 1.0, -1.0, 1.0 },
{-1.0, 1.0, 1.0, -1.0, -1.0 },
{-1.0, 1.0, -1.0, 1.0, 1.0 },
{-1.0, 1.0, -1.0, 1.0, -1.0 },
{-1.0, 1.0, -1.0, -1.0, 1.0 },
{-1.0, 1.0, -1.0, -1.0, -1.0 },
{-1.0, -1.0, 1.0, 1.0, 1.0 },
{-1.0, -1.0, 1.0, 1.0, -1.0 },
{-1.0, -1.0, 1.0, -1.0, 1.0 },
{-1.0, -1.0, 1.0, -1.0, -1.0 },
{-1.0, -1.0, -1.0, 1.0, 1.0 },
{-1.0, -1.0, -1.0, 1.0, -1.0 },
{-1.0, -1.0, -1.0, -1.0, 1.0 },
{-1.0, -1.0, -1.0, -1.0, -1.0 }
};
static void setup_gradients(void)
{
static int already_did = 0;
int i;
if (already_did)
return;
already_did = 1;
for (i = 0; (size_t) i < ARRAYSIZE(grad2); i++) {
grad2[i].dx /= N2; grad2[i].dy /= N2;
}
for (int i = 0; i < PSIZE; i++) {
GRADIENTS_2D[i] = grad2[i % ARRAYSIZE(grad2)];
}
for (i = 0; (size_t) i < ARRAYSIZE(grad3); i++) {
grad3[i].dx /= N3; grad3[i].dy /= N3; grad3[i].dz /= N3;
}
for (i = 0; i < PSIZE; i++) {
GRADIENTS_3D[i] = grad3[i % ARRAYSIZE(grad3)];
}
for (i = 0; (size_t) i < ARRAYSIZE(grad4); i++) {
grad4[i].dx /= N4; grad4[i].dy /= N4; grad4[i].dz /= N4; grad4[i].dw /= N4;
}
for (i = 0; i < PSIZE; i++) {
GRADIENTS_4D[i] = grad4[i % ARRAYSIZE(grad4)];
}
for (int i = 0; i < PSIZE; i++) {
GRADIENTS_5D[i] = grad5[i % ARRAYSIZE(grad5)];
}
// 1D gradient table - just two directions
struct Grad1 grad1[] = {
{ 1.0 },
{ -1.0 },
};
for (i = 0; (size_t) i < ARRAYSIZE(grad1); i++) {
grad1[i].dx /= N1;
}
for (size_t i = 0; i < PSIZE; i++) {
GRADIENTS_1D[i] = grad1[i % ARRAYSIZE(grad1)];
}
}
static void setup_lattice_points(void)
{
static int already_did = 0;
int i;
if (already_did)
return;
already_did = 1;
LOOKUP_2D[0] = new_LatticePoint2D(1, 0);
LOOKUP_2D[1] = new_LatticePoint2D(0, 0);
LOOKUP_2D[2] = new_LatticePoint2D(1, 1);
LOOKUP_2D[3] = new_LatticePoint2D(0, 1);
for (i = 0; i < 8; i++) {
int i1, j1, k1, i2, j2, k2;
i1 = (i >> 0) & 1; j1 = (i >> 1) & 1; k1 = (i >> 2) & 1;
i2 = i1 ^ 1; j2 = j1 ^ 1; k2 = k1 ^ 1;
// The two points within this octant, one from each of the two cubic half-lattices.
struct LatticePoint3D *c0 = new_LatticePoint3D(i1, j1, k1, 0);
struct LatticePoint3D *c1 = new_LatticePoint3D(i1 + i2, j1 + j2, k1 + k2, 1);
// Each single step away on the first half-lattice.
struct LatticePoint3D *c2 = new_LatticePoint3D(i1 ^ 1, j1, k1, 0);
struct LatticePoint3D *c3 = new_LatticePoint3D(i1, j1 ^ 1, k1, 0);
struct LatticePoint3D *c4 = new_LatticePoint3D(i1, j1, k1 ^ 1, 0);
// Each single step away on the second half-lattice.
struct LatticePoint3D *c5 = new_LatticePoint3D(i1 + (i2 ^ 1), j1 + j2, k1 + k2, 1);
struct LatticePoint3D *c6 = new_LatticePoint3D(i1 + i2, j1 + (j2 ^ 1), k1 + k2, 1);
struct LatticePoint3D *c7 = new_LatticePoint3D(i1 + i2, j1 + j2, k1 + (k2 ^ 1), 1);
// First two are guaranteed.
c0->nextOnFailure = c0->nextOnSuccess = c1;
c1->nextOnFailure = c1->nextOnSuccess = c2;
// Once we find one on the first half-lattice, the rest are out.
// In addition, knowing c2 rules out c5.
c2->nextOnFailure = c3; c2->nextOnSuccess = c6;
c3->nextOnFailure = c4; c3->nextOnSuccess = c5;
c4->nextOnFailure = c4->nextOnSuccess = c5;
// Once we find one on the second half-lattice, the rest are out.
c5->nextOnFailure = c6; c5->nextOnSuccess = NULL;
c6->nextOnFailure = c7; c6->nextOnSuccess = NULL;
c7->nextOnFailure = c7->nextOnSuccess = NULL;
LOOKUP_3D[i] = c0;
}
for (i = 0; i < 16; i++) {
VERTICES_4D[i] = new_LatticePoint4D((i >> 0) & 1, (i >> 1) & 1, (i >> 2) & 1, (i >> 3) & 1);
}
}
static void setup_lattice_points_5D(void)
{
static int already_did = 0;
int i;
if (already_did)
return;
already_did = 1;
// Generate all 32 vertices of the 5D hypercube (2^5 = 32)
for (i = 0; i < 32; i++) {
VERTICES_5D[i] = new_LatticePoint5D(
(i >> 0) & 1,
(i >> 1) & 1,
(i >> 2) & 1,
(i >> 3) & 1,
(i >> 4) & 1
);
}
}
/* Free up all the LatticePoint stuff allocated in setup_lattice_points() */
void OpenSimplex2F_shutdown(void)
{
int i;
struct LatticePoint3D *list[8];
int count = 0;
for (i = 0; (size_t) i < ARRAYSIZE(LOOKUP_2D); i++) {
if (LOOKUP_2D[i]) {
free(LOOKUP_2D[i]);
LOOKUP_2D[i] = NULL;
}
}
for (i = 0; (size_t) i < ARRAYSIZE(LOOKUP_3D); i++) {
if (LOOKUP_3D[i]) {
/* There are cycles in the tree LOOKUP_3D[i], so we have to
* make a list of unique pointers within the tree to free them.
* We know there are only 8 unique pointers in the tree.
*/
count = 0;
memset(list, 0, sizeof(list));
find_unique_pointers(LOOKUP_3D[i], list, &count);
free_LatticePoint3D(list, count);
LOOKUP_3D[i] = NULL;
}
}
for (i = 0; (size_t) i < ARRAYSIZE(VERTICES_4D); i++) {
if (VERTICES_4D[i]) {
free(VERTICES_4D[i]);
VERTICES_4D[i] = NULL;
}
}
for (i = 0; (size_t) i < ARRAYSIZE(VERTICES_5D); i++) {
if (VERTICES_5D[i]) {
free(VERTICES_5D[i]);
VERTICES_5D[i] = NULL;
}
}
}
void OpenSimplex2F_free(struct OpenSimplex2F_context *ctx)
{
if (ctx->permGrad1)
free(ctx->permGrad1);
if (ctx->perm)
free(ctx->perm);
if (ctx->permGrad2)
free(ctx->permGrad2);
if (ctx->permGrad3)
free(ctx->permGrad3);
if (ctx->permGrad4)
free(ctx->permGrad4);
if (ctx->permGrad5)
free(ctx->permGrad5);
free(ctx);
}
int OpenSimplex2F(int64_t seed, struct OpenSimplex2F_context **ctx)
{
struct OpenSimplex2F_context *c;
int i, *source;
setup_gradients();
setup_lattice_points();
setup_lattice_points_5D();
source = calloc(1, sizeof(*source) * PSIZE);
if (!source)
return -1;
c = calloc(1, sizeof(**ctx));
if (!c) {
free(source);
return -1;
}
*ctx = c;
c->permGrad1 = calloc(1, sizeof(*c->permGrad1) * PSIZE);
c->perm = calloc(1, sizeof(*c->perm) * PSIZE);
c->permGrad2 = calloc(1, sizeof(*c->permGrad2) * PSIZE);
c->permGrad3 = calloc(1, sizeof(*c->permGrad3) * PSIZE);
c->permGrad4 = calloc(1, sizeof(*c->permGrad4) * PSIZE);
c->permGrad5 = calloc(1, sizeof(*c->permGrad5) * PSIZE);
if (!c->permGrad1 || !c->perm || !c->permGrad2 || !c->permGrad3 || !c->permGrad4 || !c->permGrad5) {
OpenSimplex2F_free(*ctx);
*ctx = NULL;
free(source);
return -1;
}
for (i = 0; i < PSIZE; i++)
source[i] = (int16_t) i;
for (int i = PSIZE - 1; i >= 0; i--) {
seed = (uint64_t) seed * (uint64_t) 6364136223846793005LL + (uint64_t) 1442695040888963407LL;
int r = (int)((seed + 31) % (i + 1));
if (r < 0)
r += (i + 1);
c->perm[i] = source[r];
c->permGrad1[i] = GRADIENTS_1D[c->perm[i]];
c->permGrad2[i] = GRADIENTS_2D[c->perm[i]];
c->permGrad3[i] = GRADIENTS_3D[c->perm[i]];
c->permGrad4[i] = GRADIENTS_4D[c->perm[i]];
c->permGrad5[i] = GRADIENTS_5D[c->perm[i]];
source[r] = source[i];
}
free(source);
return 0;
}
static int fastFloor(double x)
{
int xi = (int)x;
return x < xi ? xi - 1 : xi;
}
/*
* 1D Simplex noise - super fast implementation.
* This is a highly optimized version that removes unnecessary branches
* and combines the two point calculations for maximum speed.
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1(struct OpenSimplex2F_context *ctx, double x)
{
// Skew the input space to determine the simplex cell
int xsv = (int) fastFloor(x);
double xsi = x - xsv;
// For 1D, we only have two points: origin and one point at distance 1
// The simplex is just a line segment from (0,0) to (1,1) in 1D
//
// Note: xsi is in [0, 1), but distances to endpoints can exceed 0.5
// when xsi is close to 0 or 1, making a0/a1 potentially negative.
// We must clamp/protect these to avoid incorrect contributions.
{
double value = 0.0;
int base_idx = xsv & PMASK;
// Use pre-computed gradient values from the lookup table
// The gradients have been pre-divided by N1 in setup_gradients()
// so we don't need to multiply by N1 here (matching 2D/3D/4D convention)
int idx0 = ctx->perm[base_idx];
int idx1 = ctx->perm[(base_idx + 1) & PMASK];
// Get gradient values directly from the static table using the permuted index
double g0 = GRADIENTS_1D[idx0].dx;
double g1 = GRADIENTS_1D[idx1].dx;
// Compute contributions from both endpoints
{
double dx0 = xsi;
double dx1 = xsi - 1.0;
double a0 = 0.5 - dx0 * dx0;
double a1 = 0.5 - dx1 * dx1;
// Guard against negative/zero contributions (like 2D/3D/4D)
if (a0 > 0) {
value += a0 * a0 * a0 * a0 * (g0 * dx0);
}
if (a1 > 0) {
value += a1 * a1 * a1 * a1 * (g1 * dx1);
}
}
return value;
}
}
/**
* 1D Simplex noise - ultra fast implementation with pre-computed gradients.
* Performance: ~10-15% faster than OpenSimplex2F_noise1 due to reduced memory indirection.
* This version uses ctx->permGrad1 which is pre-populated to avoid the extra
* memory lookup through ctx->perm[idx].
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1_fast(struct OpenSimplex2F_context *ctx, double x)
{
// Skew the input space to determine the simplex cell
int xsv = (int) fastFloor(x);
double xsi = x - xsv;
// For 1D, we only have two points: origin and one point at distance 1
// The simplex is just a line segment from (0,0) to (1,1) in 1D
//
// Note: xsi is in [0, 1), but distances to endpoints can exceed 0.5
// when xsi is close to 0 or 1, making a0/a1 potentially negative.
// We must clamp/protect these to avoid incorrect contributions.
{
double value = 0.0;
int base_idx = xsv & PMASK;
// Use pre-computed gradient values from ctx->permGrad1
// This avoids the extra memory indirection through ctx->perm[idx]
// which is the key optimization for this "fast" variant
// ctx->permGrad1[i] already contains GRADIENTS_1D[ctx->perm[i]]
// so we look up using base_idx directly
// Get gradient values from the pre-computed table
// These have been pre-divided by N1 in setup_gradients()
// so we don't need to multiply by N1 here (matching 2D/3D/4D convention)
double g0 = ctx->permGrad1[base_idx].dx;
double g1 = ctx->permGrad1[(base_idx + 1) & PMASK].dx;
// Compute contributions from both endpoints
{
double dx0 = xsi;
double dx1 = xsi - 1.0;
double a0 = 0.5 - dx0 * dx0;
double a1 = 0.5 - dx1 * dx1;
// Evaluate both endpoints
// Guard against negative/zero contributions (like 2D/3D/4D)
if (a0 > 0) {
value += a0 * a0 * a0 * a0 * (g0 * dx0);
}
if (a1 > 0) {
value += a1 * a1 * a1 * a1 * (g1 * dx1);
}
}
return value;
}
}
/*
* 2D Simplex noise base.
* Lookup table implementation inspired by DigitalShadow.
*/
static double noise2_Base(struct OpenSimplex2F_context *ctx, double xs, double ys)
{
double value = 0;
int i;
// Get base points and offsets
int xsb = fastFloor(xs), ysb = fastFloor(ys);
double xsi = xs - xsb, ysi = ys - ysb;
// Index to point list
int index = (int)((ysi - xsi) / 2 + 1);
double ssi = (xsi + ysi) * -0.211324865405187;
double xi = xsi + ssi, yi = ysi + ssi;
// Point contributions
for (i = 0; i < 3; i++) {
struct LatticePoint2D c = *(LOOKUP_2D[index + i]);
double dx = xi + c.dx, dy = yi + c.dy;
double attn = 0.5 - dx * dx - dy * dy;
if (attn <= 0) continue;
int pxm = (xsb + c.xsv) & PMASK, pym = (ysb + c.ysv) & PMASK;
struct Grad2 grad = ctx->permGrad2[ctx->perm[pxm] ^ pym];
double extrapolation = grad.dx * dx + grad.dy * dy;
attn *= attn;
value += attn * attn * extrapolation;
}
return value;
}
/**
* Generate overlapping cubic lattices for 3D Re-oriented BCC noise.
* Lookup table implementation inspired by DigitalShadow.
* It was actually faster to narrow down the points in the loop itself,
* than to build up the index with enough info to isolate 4 points.
*/
static double noise3_BCC(struct OpenSimplex2F_context *ctx, double xr, double yr, double zr)
{
// Get base and offsets inside cube of first lattice.
int xrb = fastFloor(xr), yrb = fastFloor(yr), zrb = fastFloor(zr);
double xri = xr - xrb, yri = yr - yrb, zri = zr - zrb;
// Identify which octant of the cube we're in. This determines which cell
// in the other cubic lattice we're in, and also narrows down one point on each.
int xht = (int)(xri + 0.5), yht = (int)(yri + 0.5), zht = (int)(zri + 0.5);
int index = (xht << 0) | (yht << 1) | (zht << 2);
// Point contributions
double value = 0;
struct LatticePoint3D *c = LOOKUP_3D[index];
while (c != NULL) {
double dxr = xri + c->dxr, dyr = yri + c->dyr, dzr = zri + c->dzr;
double attn = 0.5 - dxr * dxr - dyr * dyr - dzr * dzr;
if (attn < 0) {
c = c->nextOnFailure;
} else {
int pxm = (xrb + c->xrv) & PMASK, pym = (yrb + c->yrv) & PMASK, pzm = (zrb + c->zrv) & PMASK;
struct Grad3 grad = ctx->permGrad3[ctx->perm[ctx->perm[pxm] ^ pym] ^ pzm];
double extrapolation = grad.dx * dxr + grad.dy * dyr + grad.dz * dzr;
attn *= attn;
value += attn * attn * extrapolation;
c = c->nextOnSuccess;
}
}
return value;
}
/**
* 2D Simplex noise, standard lattice orientation.
*/
double OpenSimplex2F_noise2(struct OpenSimplex2F_context *ctx, double x, double y)
{
// Get points for A2* lattice
double s = 0.366025403784439 * (x + y);
double xs = x + s, ys = y + s;
return noise2_Base(ctx, xs, ys);
}
/**
* 2D Simplex noise, with Y pointing down the main diagonal.
* Might be better for a 2D sandbox style game, where Y is vertical.
* Probably slightly less optimal for heightmaps or continent maps.
*/
double OpenSimplex2F_noise2_XBeforeY(struct OpenSimplex2F_context *ctx, double x, double y)
{
// Skew transform and rotation baked into one.
double xx = x * 0.7071067811865476;
double yy = y * 1.224744871380249;
return noise2_Base(ctx, yy + xx, yy - xx);
}
/**
* 3D Re-oriented 4-point BCC noise, classic orientation.
* Proper substitute for 3D Simplex in light of Forbidden Formulae.
* Use noise3_XYBeforeZ or noise3_XZBeforeY instead, wherever appropriate.
*/
double OpenSimplex2F_noise3_Classic(struct OpenSimplex2F_context *ctx, double x, double y, double z)
{
// Re-orient the cubic lattices via rotation, to produce the expected look on cardinal planar slices.
// If texturing objects that don't tend to have cardinal plane faces, you could even remove this.
// Orthonormal rotation. Not a skew transform.
double r = (2.0 / 3.0) * (x + y + z);
double xr = r - x, yr = r - y, zr = r - z;
// Evaluate both lattices to form a BCC lattice.
return noise3_BCC(ctx, xr, yr, zr);
}
/**
* 3D Re-oriented 4-point BCC noise, with better visual isotropy in (X, Y).
* Recommended for 3D terrain and time-varied animations.
* The Z coordinate should always be the "different" coordinate in your use case.
* If Y is vertical in world coordinates, call noise3_XYBeforeZ(x, z, Y) or use noise3_XZBeforeY.
* If Z is vertical in world coordinates, call noise3_XYBeforeZ(x, y, Z).
* For a time varied animation, call noise3_XYBeforeZ(x, y, T).
*/
double OpenSimplex2F_noise3_XYBeforeZ(struct OpenSimplex2F_context *ctx, double x, double y, double z)
{
// Re-orient the cubic lattices without skewing, to make X and Y triangular like 2D.