-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ff_cgpt_noise_transcript_2.lua
4973 lines (4303 loc) · 202 KB
/
_ff_cgpt_noise_transcript_2.lua
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
•• I need a debugging function for a lua map script in filter forge:
1. It will need a global array for `r`,`g`,`b`,`a` values at the `x`,`y` coorinates relative to the `get_sample` function that returns the final `r`,`g`,`b`,`a` channels to the map script component.
2. the function required will be a `try_catch` function, to be placed in a `noise4d` function, which takes the current `x`,`y` sample coordinates. a table and four coordinate indices as arguments.
3. It should initialize the global `x`,`y` sample array to build up an r,g,b,a image (using the OUTPUT_HEIGHT and OUTPUT_WIDTH of the image currently loaded into Filter Forge). The array should be available to the main `get_sample(x,y) function of the map script
4. It should determine the length of the table and test if the indices are `nil` or out-of-bounds for the table arg, and assign `0` in the first case or `1` in the second case to the test sample array. If neither case is true, the current value should be normalized to [0,1] then assigned to the sample array at the current `x`,`y` coordinates.
5. Test results for the first arg go in the `r` channel, the second arg goes in the `g` channel, the third arg goes in the `b` channel, and the fourth arg goes in the `a` channel.
6. If errors are detected, the `catch` variable becomes `true`.
A conditional inserted after `try_catch` monitors the boolean `catch` and interrupts the host function if it becomes `true` during the current render pass, returning `0` from `noise4d`.
>>> failed
•• I need a debugging function for a lua map script in filter forge: its pupose is to evaluate indices used in `noise4d` for `nil` or out-of-bounds conditions. a `map` array will be used to generate feedback as `r`, `g`, `b`, `a` channels that can be used to render the current sample in the main `get_sample` function. A conditional inserted after `try_catch` monitors the boolean `catch_n` and interrupts the host function if it becomes `true` during the current render pass, returning `77777` from `noise4d`. a conditional in `get_sample` will determine if `noise4d == 77777` then retrieve the `r`, `g`, `b`, `a` values from `map` so that `get_sample` can return them.
-- global variables
local map = {}
-- in `prepare`: initialize global `map` array
local xa = OUTPUT_WIDTH
local ya = OUTPUT_HEIGHT
-- xa = math.floor(xa) already int
-- ya = math.floor(ya) already int
for ym = 1, ya do
map[ym] = {}
for xm = 1, xa do
map[ym][xm] = {0, 0, 0, 0}
end
end
-- in `noise4d`: include
catch_n = try_catch(x, y, p, inx, iny, inz, inw)
if catch_n then return -77777 end
-- `try_catch` function
local function try_catch(x, y, p, inx, iny, inz, inw)
local length = # p
local catch = false
local ra, ga, ba, aa
-- revision test each arg for `nil` or out-of-bounds
-- set fail values or normalize arg values
if not p[inx] then
ra = 0
catch = true
elseif (p[inx] < 1 or p[inx] > length) then
ra = 1
catch = true
elseif p[inx]
ra = p[inx] / 255
end
if not p[iny] then
ga = 0
catch = true
elseif (p[iny] < 1 or p[iny] > length) then
ga = 1
catch = true
elseif p[iny]
ga = p[iny] / 255
end
if not p[inz] then
ba = 0
catch = true
elseif (p[inz] < 1 or p[inz] > length) then
ba = 1
catch = true
elseif p[inz]
ba = p[inz] / 255
end
if not p[inw] then
catch = true
ra = 1
elseif (p[inw] < 1 or p[inw] > length) then
ra = 1
catch = true
elseif p[inw]
aa = p[inw] / 255
end
-- store values for `r`, `g`, `b`, `a` channels of the current sample of get_sample function in `map`
map[y][x] = {ra, ga, ba, aa}
-- check for errors
if catch then
return catch
end
end
--[[
call this function from `noise4d` function with the appropriate arguments. the `map` global variable will be updated with each call to `try_catch`. `map` will store values for the `r`, `g`, `b`, `a` channels of the current sample based on the test results. if an error is detected, the `catch` variable will be set to true, and returned to `noise4d`. the statementafter `try_catch` should monitor `catch_n`; if `true` a special value of `77777` will be returned by `noise4d`, bypassing any remaining function code.
]]--
-- in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise == -77777 then
-- retrieve channels from `map`
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
else
-- process noise normally
r, g, b, a = noise, noise, noise, 1.0
end
return r, g, b, a
end
--[[
in this function, if the noise4d function returns a value of -77777, indicating an error condition, the function retrieves the r, g, b, a values from map and returns them. otherwise, it processes the noise value normally and returns it.
]]--
Note that in the try_catch function, you have some syntax errors in the conditional statements where you're checking for the existence of the p[inx], p[iny], p[inz], and p[inw] indices. You need to add `then` after the `elseif` statements and add a missing colon (:) after `elseif p[inx] and elseif p[iny]`. Here's the corrected version of the try_catch function:
lua code
local function try_catch(x, y, p, inx, iny, inz, inw)
local length = # p
local catch = false
local ra, ga, ba, aa
-- revision test each arg for `nil` or out-of-bounds
-- set fail values or normalize arg values
if not p[inx] then
ra = 0
catch = true
elseif (p[inx] < 1 or p[inx] > length) then
ra = 1
catch = true
elseif p[inx] then
ra = p[inx] / 255
end
if not p[iny] then
ga = 0
catch = true
elseif (p[iny] < 1 or p[iny] > length) then
ga = 1
catch = true
elseif p[iny] then
ga = p[iny] / 255
end
if not p[inz] then
ba = 0
catch = true
elseif (p[inz] < 1 or p[inz] > length) then
ba = 1
catch = true
elseif p[inz] then
ba = p[inz] / 255
end
if not p[inw] then
catch = true
ra = 1
elseif (p[inw] < 1 or p[inw] > length) then
ra = 1
catch = true
elseif p[inw] then
aa = p[inw] / 255
end
-- store values for `r`, `g
•• I didn't see where you said to add missing colon (`:`)
-- global variables
local map = {}
-- in `prepare`: initialize global `map` array
local xa = OUTPUT_WIDTH
local ya = OUTPUT_HEIGHT
-- xa = math.floor(xa) already int
-- ya = math.floor(ya) already int
for ym = 1, ya do
map[ym] = {}
for xm = 1, xa do
map[ym][xm] = {0, 0, 0, 0}
end
end
-- in `noise4d`: include
catch_n = try_catch(x, y, p, inx, iny, inz, inw)
if catch_n then return -77777 end
-- `try_catch` function
local function try_catch(x, y, p, inx, iny, inz, inw)
local length = # p
local catch = false
local ra, ga, ba, aa
-- revision test each arg for `nil` or out-of-bounds
-- set fail values or normalize arg values
if not p[inx] then
ra = 0
catch = true
elseif (p[inx] < 1 or p[inx] > length) then
ra = 1
catch = true
elseif p[inx] then
ra = p[inx] / 255
end
if not p[iny] then
ga = 0
catch = true
elseif (p[iny] < 1 or p[iny] > length) then
ga = 1
catch = true
elseif p[iny] then
ga = p[iny] / 255
end
if not p[inz] then
ba = 0
catch = true
elseif (p[inz] < 1 or p[inz] > length) then
ba = 1
catch = true
elseif p[inz] then
ba = p[inz] / 255
end
if not p[inw] then
catch = true
ra = 1
elseif (p[inw] < 1 or p[inw] > length) then
ra = 1
catch = true
elseif p[inw] then
aa = p[inw] / 255
end
-- store values for `r`, `g`, `b`, `a` channels of the current sample of get_sample function in `map`
map[y][x] = {ra, ga, ba, aa}
-- check for errors
if catch then
return catch
end
end
--[[
call this function from `noise4d` function with the appropriate arguments. the `map` global variable will be updated with each call to `try_catch`. `map` will store values for the `r`, `g`, `b`, `a` channels of the current sample based on the test results. if an error is detected, the `catch` variable will be set to true, and returned to `noise4d`. the statementafter `try_catch` should monitor `catch_n`; if `true` a special value of `77777` will be returned by `noise4d`, bypassing any remaining function code.
]]--
-- in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise4d == 77777 then
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
end
I apologize for the confusion. Yes, you are correct, I did not mention the missing colon in my previous message. Thank you for pointing that out.
Regarding the code changes you suggested, they look good to me. The try_catch function now checks for errors and returns a boolean value indicating whether an error occurred or not. Additionally, the noise4d function now calls the try_catch function and checks its return value to handle any errors. Finally, the get_sample function now retrieves the channels from the map table if noise4d is equal to 77777.
Great job! Let me know if you have any other questions or concerns.
•• isn't there a proper syntax for assigning a name variable (for a method, I think) in Lua?
In Lua, there is a special syntax for assigning a name variable to a method called method definition syntax. This syntax allows you to define a method and assign it to a variable name in a single statement.
Here's an example of method definition syntax in Lua:
lua code
local obj = {}
function obj:myMethod(param1, param2)
-- method implementation
end
In this example, myMethod is defined as a method of obj, and it takes two parameters param1 and param2. The : before the method name indicates that it is a method, and the obj before the : specifies the object that the method is being defined for.
You can then call the method on an instance of obj like this:
java code
local myObj = {}
obj:myMethod(123, "abc")
In this example, myObj is an instance of obj, and myMethod is called on it with arguments 123 and "abc".
•• I think this is something different:
-- A script for Filter Forge that generates a Koch Snowflake:
name = "Koch Snowflake"
--[[
Koch Snowflake - (2D) The Koch Snowflake, a fractal named after the Swedish mathematician Helge von Koch, is a fractal generated by iteratively adding smaller and smaller triangles to the sides of an equilateral triangle (or recursively subdividing an equilateral triangle into smaller equilateral triangles, and replacing the middle third of each side with two sides that form an equilateral triangle). The resulting shape has an infinite perimeter, but a finite area.
Image: https://commons.wikimedia.org/wiki/File:KochFlake.svg, https://upload.wikimedia.org/wikipedia/commons/0/05/Koch_flake_6.png
Programs that can generate the Koch Snowflake include Processing, MATLAB, Python, Fractal, Fractint, XaoS, Apophysis, Fractal eXtreme, Ultra Fractal and Mandelbulber.
]]--
-- Set up the input parameters
parameters =
{
{name = "Iterations", control = "slider", min = 1, max = 6, default = 4},
{name = "Size", control = "slider", min = 10, max = 500, default = 200},
{name = "Thickness", control = "slider", min = 1, max = 10, default = 3},
}
This is a script for the Filter Forge software that generates a Koch Snowflake fractal.
The script sets the name of the filter to "Koch Snowflake" and defines three input parameters using a table named "parameters". Each parameter has a name, a control type (slider in this case), a minimum value, a maximum value, and a default value. The three parameters are:
Iterations: specifies the number of times the fractal will be iteratively generated.
Size: specifies the size of the fractal.
Thickness: specifies the thickness of the lines used to draw the fractal.
•• I've never seen that used in a map scipt before.
It's true that it's not a commonly used feature in Lua scripts, especially in smaller scripts. However, it can be useful in larger projects or when working in teams, as it can help make code more readable and easier to understand. By giving meaningful names to functions or methods, it becomes easier for other developers to understand the code and how it works.
In addition, it can also be helpful to organize code into modules, which can then be loaded and used as needed. By using namespaced functions or methods, conflicts between different modules or libraries can be avoided. This is especially important in larger projects where multiple people may be working on different parts of the codebase.
Overall, while it may not be used as frequently in Lua scripts as in other languages, namespacing and assigning named variables to methods can still be a useful technique in certain contexts.
•• Filter Forge locked up trying to run this script:
```
-- global variables
local map = {}
function prepare()
-- inputs and precalculation.
seed = get_intslider_input(SEED)
lacunarity = get_slider_input(LACUNARITY)
octaves = get_intslider_input(OCTAVES)
frequency = get_slider_input(FREQUENCY)
amplitude = get_slider_input(AMPLITUDE)
amplitude = math.max(amplitude, 0.0001)
persistence = get_slider_input(PERSISTENCE)
-- initialize the permutation table with the given `seed`
math.randomseed(seed)
p = {}
for i = 1, 256 do
p[i] = i - 1
end;
for i = 256, 2, -1 do
local j = math.random(i)
p[i], p[j] = p[j], p[i]
end;
-- extend the `p` table to avoid overflow
for i = 257, 512 do
p[i] = p[i - 256]
end;
-- initialize permutation table for 2-4 dimensions
p2 = {}; p3 = {}; p4 = {}
table.move(p, 1, 512, 1, p2)
table.move(p, 1, 512, 1, p3)
table.move(p, 1, 512, 1, p4)
-- shuffle the permutation table for each dimension
for i = 256, 2, -1 do
local j = math.random(i)
p2[i], p2[j] = p2[j], p2[i]
p3[i], p3[j] = p3[j], p3[i]
p4[i], p4[j] = p4[j], p4[i]
end;
-- in this update, the initial value of each permutation index changed from 0 to 1, as has the range of the math.random function to be inclusive of the upper bound. the index range for the for loops was updated accordingly.
-- `try_catch` in `prepare`: initialize global `map` array
-- local xa = 600 -- OUTPUT_WIDTH -- syntax check swap
-- local ya = 600 -- OUTPUT_HEIGHT -- syntax check swap
local xa = OUTPUT_WIDTH
local ya = OUTPUT_HEIGHT
-- xa = math.floor(xa) already int
-- ya = math.floor(ya) already int
for ym = 1, ya do
map[ym] = {}
for xm = 1, xa do
map[ym][xm] = {0, 0, 0, 0}
end
end
end;
function get_sample(x, y)
-- inputs and calculation.
z = get_sample_grayscale(x,y,Z)
w = get_sample_grayscale(x,y,W)
t = get_sample_grayscale(x,y,T)
-- test_variables(p[x], p[y], p[z], p[w])
-- image generation code
local r = perlin4d(x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
local g = 0 -- perlin4d(x+1, y-1,z+1, w-1, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
local b = 0 -- perlin4d(x-2, y+2, z-2, w+2, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
local a = 1
-- `try_catch`in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise4d == 77777 then
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
end
return r, g, b, a
end;
function test_args(p, ...)
local args = {...}
local n = #args
local p_range = #p
local arg_errors = {}
for i = 1, n do
local v = args[i]
if v == nil then
arg_errors[#arg_errors + 1] = "V|" .. i .. " is nil."
elseif type(v) ~= "table" and type(v) == "number" then
if (v%1 == 0) and (v < 1 or v > p_range) then
arg_errors[#arg_errors + 1] = "V|" .. i .. " = " .. v .. " OOB: [1," .. p_range .. "]."
end;
end;
end;
if #arg_errors > 0 then
local all_args = ""
for i, v in ipairs(args) do
all_args = all_args .. tostring(v) .. ", "
end
all_args = all_args:sub(1, -3)
error("<!> " .. table.concat(arg_errors, " ") .. "All arguments: " .. all_args)
end;
return unpack(args, 1, n)
end;
--[[
`test_args` keeps track of any errors in a table, and then if there are any errors, it concatenates all of the arguments together and includes them in the error message.
]]--
function test_variables(r, g, b, a)
local variables = {r, g, b, a}
local nil_values = false
for i=1, 4 do
if variables[i] == nil then
variables[i] = 0
nil_values = true
elseif variables[i] ~= nil then
variables[i] = variables[i]
else
variables[i] = 1
nil_values = true
end;
end;
if nil_values then
return unpack(variables)
end;
end;
function perlin4d(x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- test_args(p4, x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t)
-- scale the input coordinates based on the `frequency`
x = x * frequency
y = y * frequency
z = z or x+1 * frequency
w = w or y+1 * frequency
t = t or 0.0
local total = 0
local max_value = (1 - persistence ^ octaves) / (1 - persistence) -- was max_value = 0
local amplitude = amplitude -- might have been intended for scale.
for i = 1, octaves do
-- calculate the value of each octave
local value = noise4d(x, y, z, w, p4)
-- scale the value by the `amplitude` and add to the total
total = total + value * amplitude
-- update the maximum value and reduce the amplitude for the next octave
max_value = max_value + amplitude
amplitude = amplitude * persistence
-- scale the input coordinates for the next octave
x = x * lacunarity
y = y * lacunarity
z = z * lacunarity
w = w * lacunarity
if t ~= nil then
t = t * lacunarity
end;
end;
return total -- was total / max_value -- normalize the result by dividing by the maximum value
end;
--[[
note that the function now checks if `z` and `t` are provided and uses `noise3d` instead of `noise2d` if `z` is provided. also, `lacunarity` is used to scale the input coordinates for each octave and `t` is also scaled by `lacunarity` if it is provided.
the input parameters for the perlin4d function and their respective data types:
x: float
y: float
z: float (optional)
t: float (optional)
lacunarity: float
octaves: int
frequency: float
amplitude: float
persistence: float
• `z` and `t` are optional parameters and if they are not provided, the function will generate 2d noise.
Lacunarity, Octaves, Frequency, Amplitude, Persistence, Z, T, Seed
]]--
--[[
function noise2d(x, y, p)
local xi = math.floor(x) % 256
local yi = math.floor(y) % 256
local xf = x - math.floor(x)
local yf = y - math.floor(y)
local u = fade(xf)
local v = fade(yf)
local aa, ab, ba, bb = p[p[xi]+yi], p[p[xi+1]+yi], p[p[xi]+yi+1], p[p[xi+1]+yi+1]
local x1 = lerp(grad(aa, xf, yf), grad(ba, xf-1, yf), u)
local x2 = lerp(grad(ab, xf, yf-1), grad(bb, xf-1, yf-1), u)
return lerp(x1, x2, v)
end;
]]--
--[[
function noise3d(x, y, z, p)
local X = math.floor(x) % 256
local Y = math.floor(y) % 256
local Z = math.floor(z) % 256
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
local u = fade(x)
local v = fade(y)
local w = fade(z)
local A = p[X] + Y
local AA = p[A] + Z
local AB = p[A + 1] + Z
local B = p[X + 1] + Y
local BA = p[B] + Z
local BB = p[B + 1] + Z
return lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z),
grad(p[BA], x - 1, y, z)),
lerp(u, grad(p[AB], x, y - 1, z),
grad(p[BB], x - 1, y - 1, z))),
lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1),
grad(p[BA + 1], x - 1, y, z - 1)),
lerp(u, grad(p[AB + 1], x, y - 1, z - 1),
grad(p[BB + 1], x - 1, y - 1, z - 1))))
end;
--[[
this version of the function includes the changes to the fade and grad functions you requested, as well as the changes to the noise2d function. the noise3d function now uses the fade function to calculate the interpolation weights for the x, y, and z coordinates, and uses the grad function to calculate the gradients at each of the eight corners of the cube surrounding the input point. the gradients are then linearly interpolated to produce the final noise value.
]]--
function noise4d(x, y, z, w, p4)
-- test_args(p4, x, y, z, w)
-- adjusted to integer coordinates from float inputs `(x,y,z,w)` = [0,1]. this alone does not prevent `i` == `0`, so adjustment needed for proper indexing. simple addition or subtration will always put `i` == `0` at some point. what is needed is a ground to wheel relationship where `p4` = [1,256] and interaction with coordinates drive `p4` in a circle over any distance without risk of `-i` == `0`.
-- coordinate operations:
local ix = math.floor(x * 255) + 1
local iy = math.floor(y * 255) + 1
local iz = math.floor(z * 255) + 1
local iw = math.floor(w * 255) + 1
-- test_args(p4, ix, iy, iz, iw) -- getting `o,o,o.o,o.o` when each should be int [1,256].
local fx = (x * 255) - ix
local fy = (y * 255) - iy
local fz = (z * 255) - iz
local fw = (w * 255) - iw
-- `try_catch` in `noise4d`: include
catch_1 = try_catch(x, y, p4, ix, iy, iz, iw)
if catch_1 then return -77777 end
local u = fade(fx)
local v = fade(fy)
local w = fade(fz)
local x = fade(fw)
-- index looping acheived using `offset_index` function.
local i1 = offset_index(p4, p4[ix], iy)
local i2 = offset_index(p4, p4[offset_index(p4, p4[ix], 1)], iy)
local i3 = offset_index(p4, p4[i1], iz)
local i4 = offset_index(p4, p4[offset_index(p4, p4[i1], 1)] , iz)
local i5 = offset_index(p4, p4[i2], iz)
local i6 = offset_index(p4, p4[offset_index(p4, p4[i2], 1)], iz)
local in1 = grad4d(p4[offset_index(p4, p4[i3], iw)], fx, fy, fz, fw) -- Indexing operations
local in2 = grad4d(p4[offset_index(p4, p4[i4], iw)], fx, fy, fz - 1, fw) -- Indexing operations
local in3 = grad4d(p4[offset_index(p4, p4[i5], iw)], fx, fy - 1, fz, fw) -- Indexing operations
local in4 = grad4d(p4[offset_index(p4, p4[i6], iw)], fx, fy - 1, fz - 1, fw) -- Indexing operations
local in5 = grad4d(p4[offset_index(p4, p4[i3], (iw + 1))], fx, fy, fz, fw - 1) -- Indexing operations
local in6 = grad4d(p4[offset_index(p4, p4[i4], (iw + 1))], fx, fy, fz - 1, fw - 1) -- Indexing operations
local in7 = grad4d(p4[offset_index(p4, p4[i5], (iw + 1))], fx, fy - 1, fz, fw - 1) -- Indexing operations
local in8 = grad4d(p4[offset_index(p4, p4[i6], (iw + 1))], fx, fy - 1, fz - 1, fw - 1) -- Indexing operations
local x1 = lerp(u, dot4d(in1, fx, fy, fz, fw), dot4d(in5, fx - 1, fy, fz, fw)) -- coordinate and indexing operations
local x2 = lerp(u, dot4d(in2, fx, fy, fz - 1, fw), dot4d(in6, fx - 1, fy, fz - 1, fw)) -- coordinate and indexing operations
local y1 = lerp(v, x1, x2) -- coordinate operation
local x3 = lerp(u, dot4d(in3, fx, fy - 1, fz, fw), dot4d(in7, fx - 1, fy - 1, fz, fw)) -- coordinate and indexing operations
local x4 = lerp(u, dot4d(in4, fx, fy - 1, fz - 1, fw), dot4d(in8, fx - 1, fy - 1, fz - 1, fw)) -- coordinate and indexing operations
local y2 = lerp(v, x3, x4) -- coordinate operation
local z1 = lerp(w, y1, y2) -- coordinate operation
return z1
end;
--[[
this implementation uses the grad4d function to calculate the dot products based on the gradient vectors obtained from the p4 table using the hash4d function.
]]--
-- function to offset index within range [1, table_size]
function offset_index(tbl, current_index, offset)
local table_size = #tbl
local new_index = current_index + offset
if new_index > table_size then
new_index = new_index - table_size
elseif new_index < 1 then
new_index = new_index + table_size
end
return new_index
end;
--[[
the `offset_index` function takes care of ensuring that the indices are within the range [1, table_size] and returns a valid index, so, for example, the indices used in `i1`, `i2`, `i3`, `i4`, `i5`, and `i6` should be valid indices for the `p4` table.
]]--
local grad4d_hash = {
{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
{0,-1,1,1}, {0,-1,1,-1}, {0,-1,-1,1}, {0,-1,-1,-1},
{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
{-1,0,1,1}, {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
{-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1}, {-1,-1,0,-1},
{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
{-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}
}
function grad4d(hash, x, y, z, w)
local h = hash % 32
return grad4d_hash[h+1][1]*x + grad4d_hash[h+1][2]*y + grad4d_hash[h+1][3]*z + grad4d_hash[h+1][4]*w
end;
--[[
the grad4d function takes a hash value and four input coordinates x, y, z, and w. it computes a hash value modulo 32 to get an index into the grad4d table, and uses the corresponding gradient vector to compute the dot product with the input coordinates.
]]--
function dot4d(g, x, y, z, w)
return g[1]*x + g[2]*y + g[3]*z + g[4]*w
end;
--[[
this function takes in four coordinates x, y, z, w and a gradient vector g and returns the dot product of the gradient vector and the input coordinates. this is used in the noise4d function to calculate the dot product of the gradient vector and the distance vector in each of the 16 corners of the 4d hypercube.
]]--
function fade(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end;
--[[
the fade function is a mathematical function used in noise generation algorithms to smooth out the transitions between different levels of noise. it takes a single parameter t, which is a value between 0 and 1. the fade function typically returns a value that is interpolated between 0 and 1 based on the input value t. this implementation is known as perlin's quintic polynomial. it is commonly used in perlin noise algorithms and other noise generation algorithms.
]]--
function lerp(x, y, t)
-- linearly interpolates between two values `x` and `y` by a given amount `t`
return x + (y - x) * t
end;
function wrap(x, min_val, max_val)
local range = max_val - min_val + 1
return ((x - min_val) % range) + min_val
end;
-- `try_catch` function
function try_catch(x, y, p, inx, iny, inz, inw)
local length = # p
local catch = false
local ra, ga, ba, aa
-- revision test each arg for `nil` or out-of-bounds
-- set fail values or normalize arg values
if not p[inx] then
ra = 0
catch = true
elseif (p[inx] < 1 or p[inx] > length) then
ra = 1
catch = true
elseif p[inx] then
ra = p[inx] / 255
end
if not p[iny] then
ga = 0
catch = true
elseif (p[iny] < 1 or p[iny] > length) then
ga = 1
catch = true
elseif p[iny] then
ga = p[iny] / 255
end
if not p[inz] then
ba = 0
catch = true
elseif (p[inz] < 1 or p[inz] > length) then
ba = 1
catch = true
elseif p[inz] then
ba = p[inz] / 255
end
if not p[inw] then
catch = true
ra = 1
elseif (p[inw] < 1 or p[inw] > length) then
ra = 1
catch = true
elseif p[inw] then
aa = p[inw] / 255
end
-- store values for `r`, `g`, `b`, `a` channels of the current sample of get_sample function in `map`
map[y][x] = {ra, ga, ba, aa}
-- check for errors
if catch then
return catch
end
end
--[[
instance in `noise4d`: rename `catch_`..instance#.. and include
catch_n = try_catch(x, y, p, inx, iny, inz, inw)
if catch_n then return -77777 end
call this function from `noise4d` function with the appropriate arguments. the `map` global variable will be updated with each call to `try_catch`. `map` will store values for the `r`, `g`, `b`, `a` channels of the current sample based on the test results. if an error is detected, the `catch` variable will be set to true, and returned to `noise4d`. the statementafter `try_catch` should monitor `catch_n`; if `true` a special value of `77777` will be returned by `noise4d`, bypassing any remaining function code.
]]--
```
Do you see a likely cause?
It seems that the issue might be related to the fact that the noise4d variable used in the get_sample function is not defined anywhere in the script, and it is being checked for equality with the value 77777. This is likely causing the script to get stuck in an infinite loop and locking up the program.
To fix this issue, you can either define noise4d as a global variable at the beginning of the script or remove the if-statement that checks for its value in the get_sample function. Additionally, the test_variables function seems to be incomplete, and it is missing the ending of the for loop. You should add the closing end statement at the end of the function.
•• I revised this section:
```
function get_sample(x, y)
-- inputs and calculation.
z = get_sample_grayscale(x,y,Z)
w = get_sample_grayscale(x,y,W)
t = get_sample_grayscale(x,y,T)
-- test_variables(p[x], p[y], p[z], p[w])
-- image generation code
local noise = noise4d(x, y, z, w, p4)
-- local r = perlin4d(x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local g = 0 -- perlin4d(x+1, y-1,z+1, w-1, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local b = 0 -- perlin4d(x-2, y+2, z-2, w+2, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local a = 1
-- `try_catch`in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise == 77777 then
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
end
return r, g, b, a
end;
```
Good?
Yes, the revision looks good! Just one minor suggestion: since get_sample_grayscale is called three times with the same inputs, you could store the result in a local variable and reuse it, like this:
less code
function get_sample(x, y)
-- inputs and calculation.
local zwt = get_sample_grayscale(x,y,Z)
local z, w, t = zwt, zwt, zwt
-- test_variables(p[x], p[y], p[z], p[w])
-- image generation code
local noise = noise4d(x, y, z, w, p4)
-- local r = perlin4d(x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local g = 0 -- perlin4d(x+1, y-1,z+1, w-1, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local b = 0 -- perlin4d(x-2, y+2, z-2, w+2, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local a = 1
-- `try_catch`in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise == 77777 then
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
end
return r, g, b, a
end;
This avoids unnecessary function calls and might improve performance slightly.
•• `z,`w`,`t` are separate inputs.
I see, thank you for clarifying. In that case, your revision is correct. Here's the updated section:
less code
function get_sample(x, y, z, w, t)
-- test_variables(p[x], p[y], p[z], p[w])
-- image generation code
local noise = noise4d(x, y, z, w, p4)
-- local r = perlin4d(x, y, z, w, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local g = 0 -- perlin4d(x+1, y-1,z+1, w-1, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local b = 0 -- perlin4d(x-2, y+2, z-2, w+2, lacunarity, octaves, frequency, amplitude, persistence, t, p4)
-- local a = 1
-- `try_catch`in `get_sample`: if `noise4d == 77777` then retrieve channels from `map`
if noise == 77777 then
r, g, b, a = map[y][x][1], map[y][x][2], map[y][x][3], map[y][x][4]
end
return r, g, b, a
end;
This code takes four inputs x, y, z, and w, as well as an additional input t. It calculates the 4D noise value using these inputs and stores the result in noise. If the noise value is 77777, it retrieves the corresponding color channels from map. Finally, it returns the color values r, g, b, and a.
•• It still locks up Filter Forge. Am debugging further, checking for variable conflict I might also have missed.
I see. It's good that you're continuing to investigate the issue. Let me know if you need any further assistance.
•• •• is this semantically valid>:
```
local i1 = (p4[ix] + iy % 255) + 1
local i2 = (p4[ix + 1] + iy % 255) + 1
local i3 = (p4[i1] + iz % 255) + 1
local i4 = (p4[i2] + iz % 255) + 1
local i5 = (p4[i3] + iw % 255) + 1
local i6 = (p4[i4] + iw % 255) + 1
local in1 = grad(p4[i5], fx, fy, fz, fw)
local in2 = grad(p4[i6], fx - 1, fy, fz, fw)
local in3 = grad(p4[(i5 + 1 % 255) + 1], fx, fy - 1, fz, fw)
local in4 = grad(p4[(i6 + 1 % 255) + 1], fx - 1, fy - 1, fz, fw)
local in5 = grad(p4[(i5 + 1 + 256 % 255) + 1], fx, fy, fz - 1, fw)
local in6 = grad(p4[(i6 + 1 + 256 % 255) + 1], fx - 1, fy, fz - 1, fw)
local in7 = grad(p4[(i5 + 1 + 256 * 2 % 255) + 1], fx, fy - 1, fz - 1, fw)
local in8 = grad(p4[(i6 + 1 + 256 * 2 % 255) + 1], fx - 1, fy - 1, fz - 1, fw)
```
Yes, this code is semantically valid. It is using modular arithmetic to index into the p4 table, which is a common technique used in Perlin noise implementations to create seamless noise patterns.
The variables i1 through i6 are computed using the modulo operator to ensure that they remain within the range of the p4 table, and then the grad() function is called with these indices to compute the gradient vectors.
Overall, this code appears to be a valid implementation of Perlin noise in four dimensions.
•• function noise4d(x, y, z, w, p4)
-- test_args(p4, x, y, z, w)
-- adjusted to integer coordinates from float inputs `(x,y,z,w)` = [0,1].
-- coordinate operations:
local ix = math.floor(x * 255) + 1
local iy = math.floor(y * 255) + 1
local iz = math.floor(z * 255) + 1
local iw = math.floor(w * 255) + 1
-- `try_catch` in `noise4d`: include
catch_1 = try_catch(x, y, p4, ix, iy, iz, iw)
if catch_1 then return -77777 end
-- test_args(p4, ix, iy, iz, iw) -- getting `o,o,o.o,o.o` when each should be int [1,256].
local fx = (x * 255) - ix
local fy = (y * 255) - iy
local fz = (z * 255) - iz
local fw = (w * 255) - iw
-- `try_catch` in `noise4d`: include
catch_2 = try_catch(x, y, p4, fx, fy, fz, fw)
if catch_2 then return -77777 end
local u = fade(fx)
local v = fade(fy)
local uu = fade(fz) --conflict `w`
local vv = fade(fw) --conflict `x` and does not appear to get used
-- indexing operations
local i1 = (p4[ix] + iy % 255) + 1
local i2 = (p4[ix + 1] + iy % 255) + 1
local i3 = (p4[i1] + iz % 255) + 1
local i4 = (p4[i2] + iz % 255) + 1
local i5 = (p4[i3] + iw % 255) + 1
local i6 = (p4[i4] + iw % 255) + 1
-- indexing operations
local in1 = grad(p4[i5], fx, fy, fz, fw)
local in2 = grad(p4[i6], fx - 1, fy, fz, fw)
local in3 = grad(p4[(i5 + 1 % 255) + 1], fx, fy - 1, fz, fw)
local in4 = grad(p4[(i6 + 1 % 255) + 1], fx - 1, fy - 1, fz, fw)
local in5 = grad(p4[(i5 + 1 + 256 % 255) + 1], fx, fy, fz - 1, fw)
local in6 = grad(p4[(i6 + 1 + 256 % 255) + 1], fx - 1, fy, fz - 1, fw)
local in7 = grad(p4[(i5 + 1 + 256 * 2 % 255) + 1], fx, fy - 1, fz - 1, fw)
local in8 = grad(p4[(i6 + 1 + 256 * 2 % 255) + 1], fx - 1, fy - 1, fz - 1, fw)
-- coordinate and indexing operations
local x1 = lerp(u, dot4d(in1, fx, fy, fz, fw), dot4d(in5, fx - 1, fy, fz, fw))
local x2 = lerp(u, dot4d(in2, fx, fy, fz - 1, fw), dot4d(in6, fx - 1, fy, fz - 1, fw))
local y1 = lerp(v, x1, x2) -- coordinate operation
local x3 = lerp(u, dot4d(in3, fx, fy - 1, fz, fw), dot4d(in7, fx - 1, fy - 1, fz, fw))
local x4 = lerp(u, dot4d(in4, fx, fy - 1, fz - 1, fw), dot4d(in8, fx - 1, fy - 1, fz - 1, fw))
local y2 = lerp(v, x3, x4) -- coordinate operation
local z1 = lerp(uu, y1, y2) -- coordinate operation--conflict `w`
return z1
end;
The noise4d function appears to be implementing Perlin noise generation in 4 dimensions. Here's a breakdown of the different parts of the code:
Coordinate adjustments: The input coordinates x, y, z, and w are first scaled to integer coordinates in the range [1, 256].
Input value error checking: Two instances of a try_catch function are used to check for potential input value errors before proceeding with the noise generation. If an error is detected, the function returns a default value of -77777.
Coordinate adjustments (continued): The fractional parts of the input coordinates are computed for later use.
Coordinate fade function: The fade function is called to compute the fade values for the fractional coordinates. This function is used to smooth the noise between integer coordinates.
Indexing operations: The integer coordinates are used to index into a permutation table p4 to obtain gradient vectors for each of the 16 corners of the 4D hypercube.
Gradient computations: The gradient vectors are used to compute dot products with the fractional coordinates for each of the 16 corners. These dot products are then interpolated using the lerp function to obtain a noise value for each of the 8 vertices of the hypercube.
Coordinate adjustments (continued): The noise values at the 8 vertices are further interpolated using the lerp function and the fractional coordinate in the 4th dimension to obtain the final noise value at the input coordinates.
Overall, this code generates 4D Perlin noise values for any given set of input coordinates.
•• revise `test_vars` to emulate `test_args` (omitting the table arg `p`). revised test criteria are: test for `nil` and set `errors = true`, test numbers for `int` or `float`, identify type if not `nil` or number and set `errors = true`. `if errors` repeat for each variable. return `vars`, `n`.
```
Here is the revised test_vars function:
lua code
function test_vars(...)
local vars = {...}
local n = select("#", ...)
local errors = false
for i = 1, n do
local v = vars[i]
if v == nil then
errors = true
elseif type(v) ~= "number" then
local is_int = math.floor(v) == v
local is_float = not is_int and type(v) == "number"
if not (is_int or is_float) then
errors = true
end
end
end
if errors then
local var_names = {}
for i = 1, n do
local name = "var_" .. i
table.insert(var_names, name)
_G[name] = vars[i]
end
error("Invalid input: " .. table.concat(var_names, ", "))
end
return vars, n
end
This revised function will test each variable for nil and set errors to true if found. It will also test each number for int or float and set errors to true if neither of those conditions are met. If errors is true, it will set global variables with each variable's value and raise an error with their names. Otherwise, it will return the vars table and n.
•• revise `test_vars` to emulate `test_args` (omitting the table arg `p`). `test_args` keeps track of any errors in a table, and then if there are any errors, it concatenates all of the arguments together and includes them in the error message. create `vars` and `var_errors` tables. test each var in arguments. revised test criteria are: test for `nil` (set var_errors + 1, error text "nil", and `errors = true`), test numbers for `int` or `float` (store "nil" or "NaN" for error text), identify type if not `nil` or number and set `errors = true`. repeat for each variable. `if errors` return unpack(`vars`, `n`).
```
function test_args(p, ...)
local args = {...}
local n = #args
local p_range = #p
local arg_errors = {}
for i = 1, n do
local v = args[i]
if v == nil then
arg_errors[#arg_errors + 1] = "V|" .. i .. " is nil."
elseif type(v) ~= "table" and type(v) == "number" then
if (v%1 == 0) and (v < 1 or v > p_range) then
arg_errors[#arg_errors + 1] = "V|" .. i .. " = " .. v .. " OOB: [1," .. p_range .. "]."
end;
end;
end;
if #arg_errors > 0 then
local all_args = ""
for i, v in ipairs(args) do
all_args = all_args .. tostring(v) .. ", "
end
all_args = all_args:sub(1, -3)
error("<!> " .. table.concat(arg_errors, " ") .. "All arguments: " .. all_args)
end;
return unpack(args, 1, n)
end;
--[[
`test_args` keeps track of any errors in a table, and then if there are any errors, it concatenates all of the arguments together and includes them in the error message.
]]--
function test_vars(r, g, b, a)
local variables = {r, g, b, a}
local nil_values = false
for i=1, 4 do
if variables[i] == nil then
variables[i] = 0
nil_values = true
elseif variables[i] ~= nil then
variables[i] = variables[i]
else
variables[i] = 1
nil_values = true
end;
end;
if nil_values then
return unpack(variables)
end;
end;
```
Here's the revised test_vars function:
lua code
function test_vars(r, g, b, a)
local vars = {r, g, b, a}
local var_errors = {}
local n = 4
local errors = false
for i = 1, n do
local v = vars[i]
if v == nil then
var_errors[#var_errors + 1] = "V|" .. i .. " is nil."
errors = true
elseif type(v) ~= "number" then
var_errors[#var_errors + 1] = "V|" .. i .. " is not a number."
errors = true
elseif v % 1 ~= 0 and v ~= math.floor(v) then
var_errors[#var_errors + 1] = "V|" .. i .. " is not an integer or float."
errors = true
end
end
if errors then
local all_vars = ""