Skip to content

Commit 09f1264

Browse files
committed
feat: Custom noise type - enable user defined function
1 parent a12ee5a commit 09f1264

File tree

12 files changed

+434
-3
lines changed

12 files changed

+434
-3
lines changed

.github/go-mod-tidy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go get "github.com/aquilax/go-perlin"
2727
go get "github.com/ojrac/opensimplex-go"
2828
go get "github.com/pkg/errors"
2929
go get "github.com/stretchr/testify"
30+
go get "gonum.org/v1/plot/..."
3031

3132
echo '* Run go tidy ...'
3233
go mod tidy

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ In the example below, 1 dimmentional method `noise.Generator.Eval64(x)` was used
1717
// const seed = 100 // noise pattern ID
1818
// const smoothness = 100 // noise smoothness
1919
//
20+
// // noiseType choises
2021
// noiseType := noise.Perlin
2122
// noiseType := noise.OpenSimplex
23+
// noiseType := noise.Custom
2224
n, err := noise.New(noiseType, seed)
2325

2426
yy := n.Eval64(x / smoothness) // yy is between -1.0 and 1.0 of float64
@@ -27,6 +29,7 @@ y := (yy + 1) / 2 * 500 // y is between 0 and 500
2729

2830
![](./_example/2d/2d_perlin.png)
2931
![](./_example/2d/2d_opensimplex.png)
32+
![](./_example/2d/2d_pseudorandom.png)
3033

3134
- [Source](./_example/2d)
3235

@@ -84,6 +87,10 @@ go get "github.com/KEINOS/go-noise"
8487

8588
### Constructor
8689

90+
```go
91+
noise.New(noiseType noise.Algo, seed int64) (noise.Generator, error)
92+
```
93+
8794
```go
8895
import "github.com/KEINOS/go-noise"
8996

_example/2d/2d_pseudorandom.png

20.3 KB
Loading

_example/2d/main.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"log"
5+
"math/rand"
56

67
"github.com/KEINOS/go-noise"
78
"gonum.org/v1/plot"
@@ -25,6 +26,13 @@ const (
2526
smoothness = 100
2627
)
2728

29+
// rnd is the rand.Rand used by PseudoRrandom() which is the Custom noise generator.
30+
var rnd *rand.Rand
31+
32+
// ----------------------------------------------------------------------------
33+
// Main
34+
// ----------------------------------------------------------------------------
35+
2836
func main() {
2937
{
3038
// Create Perlin noise generator with seed 100.
@@ -53,11 +61,34 @@ func main() {
5361
log.Fatal(err)
5462
}
5563
}
64+
65+
{
66+
// Create user-defined Pseudorandom noise generator with seed 100.
67+
gen, err := noise.New(noise.Custom, seed)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
// Set user-defined noise function.
73+
if err := gen.SetEval64(PseudoRrandom); err != nil {
74+
log.Fatal(err)
75+
}
76+
77+
pathFile := "./2d_pseudorandom.png"
78+
79+
if err := GenGraphImage(gen, "User Custom Noise (Pseudo Random)", pathFile); err != nil {
80+
log.Fatal(err)
81+
}
82+
}
5683
}
5784

85+
// ----------------------------------------------------------------------------
86+
// Functions
87+
// ----------------------------------------------------------------------------
88+
5889
// GenGraphImage generates a graph image with the specified generator and saves
5990
// it to the specified path.
60-
func GenGraphImage(gen noise.Noise, title string, pathFile string) error {
91+
func GenGraphImage(gen noise.Generator, title string, pathFile string) error {
6192
// Create plotter.
6293
pltr := GenPlotter(title)
6394

@@ -103,3 +134,25 @@ func GenPlotter(titile string) *plot.Plot {
103134
func Normalize(v float64) float64 {
104135
return (v + 1) / 2 * float64(height)
105136
}
137+
138+
// PseudoRrandom is a pseudo-random noise function for use as a user-defined
139+
// noise generator for the Eval64 method.
140+
func PseudoRrandom(seed int64, dim ...float64) float64 {
141+
if rnd == nil {
142+
// Set the seed.
143+
rnd = rand.New(rand.NewSource(seed))
144+
}
145+
146+
for i := 0; i < len(dim); i++ {
147+
v := int(dim[i])
148+
149+
for ii := 0; ii < v; ii++ {
150+
//nolint:gosec // Use of weak random number generation is intended for simple examples.
151+
_ = rnd.Float64()
152+
}
153+
}
154+
155+
s := rnd.Float64() // 0.0 - 0.9999...
156+
157+
return s*2 - 1 // Convert [0.0,1.0) to [-1.0,1.0]
158+
}

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ require (
1010
)
1111

1212
require (
13+
gioui.org v0.0.0-20210308172011-57750fc8a0a6 // indirect
14+
git.sr.ht/~sbinet/gg v0.3.1 // indirect
15+
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
1316
github.com/davecgh/go-spew v1.1.0 // indirect
17+
github.com/go-fonts/liberation v0.2.0 // indirect
18+
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 // indirect
19+
github.com/go-pdf/fpdf v0.6.0 // indirect
20+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
1421
github.com/pmezard/go-difflib v1.0.0 // indirect
22+
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 // indirect
23+
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
24+
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197 // indirect
25+
golang.org/x/text v0.3.7 // indirect
26+
gonum.org/v1/plot v0.11.0 // indirect
1527
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
28+
rsc.io/pdf v0.1.1 // indirect
1629
)

go.sum

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,105 @@
1+
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
2+
gioui.org v0.0.0-20210308172011-57750fc8a0a6 h1:K72hopUosKG3ntOPNG4OzzbuhxGuVf06fa2la1/H/Ho=
3+
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
4+
git.sr.ht/~sbinet/gg v0.3.1 h1:LNhjNn8DerC8f9DHLz6lS0YYul/b602DUxDgGkd/Aik=
5+
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
6+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
7+
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
8+
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
9+
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
10+
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw=
11+
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM=
112
github.com/aquilax/go-perlin v1.1.0 h1:Gg+3jQ24wT4Y5GI7TCRLmYarzUG0k+n/JATFqOimb7s=
213
github.com/aquilax/go-perlin v1.1.0/go.mod h1:z9Rl7EM4BZY0Ikp2fEN1I5mKSOJ26HQpk0O2TBdN2HE=
14+
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
15+
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
316
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
417
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
18+
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
19+
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
20+
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
21+
github.com/go-fonts/liberation v0.2.0 h1:jAkAWJP4S+OsrPLZM4/eC9iW7CtHy+HBXrEwZXWo5VM=
22+
github.com/go-fonts/liberation v0.2.0/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=
23+
github.com/go-fonts/stix v0.1.0/go.mod h1:w/c1f0ldAUlJmLBvlbkvVXLAD+tAMqobIIQpmnUIzUY=
24+
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
25+
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 h1:6zl3BbBhdnMkpSj2YY30qV3gDcVBGtFgVsV3+/i+mKQ=
26+
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81/go.mod h1:SX0U8uGpxhq9o2S/CELCSUxEWWAuoCUcVCQWv7G2OCk=
27+
github.com/go-pdf/fpdf v0.5.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
28+
github.com/go-pdf/fpdf v0.6.0 h1:MlgtGIfsdMEEQJr2le6b/HNr1ZlQwxyWr77r2aj2U/8=
29+
github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
30+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
31+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
32+
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
33+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
534
github.com/ojrac/opensimplex-go v1.0.2 h1:l4vs0D+JCakcu5OV0kJ99oEaWJfggSc9jiLpxaWvSzs=
635
github.com/ojrac/opensimplex-go v1.0.2/go.mod h1:NwbXFFbXcdGgIFdiA7/REME+7n/lOf1TuEbLiZYOWnM=
36+
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
37+
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
38+
github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
39+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
740
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
841
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
942
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1043
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
44+
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
45+
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
1146
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
47+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1248
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
1349
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
50+
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
51+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
52+
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
53+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
54+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
55+
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
56+
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 h1:n9HxLrNxWWtEb1cA950nuEEj3QnKbtsCJ6KjcgisNUs=
57+
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3/go.mod h1:NOZ3BPKG0ec/BKJQgnvsSFpcKLM5xXVWnvZS97DWHgE=
58+
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
59+
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
60+
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
61+
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
62+
golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
63+
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
64+
golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
65+
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
66+
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
67+
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
68+
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 h1:TcHcE0vrmgzNH1v3ppjcMGbhG5+9fMuvOmUYwNEF4q4=
69+
golang.org/x/image v0.0.0-20220302094943-723b81ca9867/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
70+
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
71+
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
72+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
73+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
74+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
75+
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
76+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
77+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
78+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
79+
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
80+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
81+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
82+
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
83+
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197 h1:7+SpRyhoo46QjKkYInQXpcfxx3TYFEYkn131lwGE9/0=
84+
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
85+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
86+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
87+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
88+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
89+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
90+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
91+
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
92+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
93+
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
94+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
95+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
96+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
97+
gonum.org/v1/plot v0.11.0 h1:z2ZkgNqW34d0oYUzd80RRlc0L9kWtenqK4kflZG1lGc=
98+
gonum.org/v1/plot v0.11.0/go.mod h1:fH9YnKnDKax0u5EzHVXvhN5HJwtMFWIOLNuhgUahbCQ=
1499
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
15100
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16101
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
17102
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
103+
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
104+
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=
105+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

noise.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package noise
22

33
import (
4+
"github.com/KEINOS/go-noise/pkg/custom"
45
"github.com/KEINOS/go-noise/pkg/opensimplex"
56
"github.com/KEINOS/go-noise/pkg/perlin"
67
"github.com/pkg/errors"
@@ -16,6 +17,8 @@ const (
1617
Perlin
1718
// OpenSimplex noise type.
1819
OpenSimplex
20+
// Custom uses the user-defined function to generate noise.
21+
Custom
1922
)
2023

2124
// ----------------------------------------------------------------------------
@@ -46,6 +49,8 @@ type Generator interface {
4649
// Implementations of this method must return the same value if the seed
4750
// value is the same.
4851
Eval64(dim ...float64) float64
52+
SetEval32(f func(seed int64, dim ...float32) float32) error
53+
SetEval64(f func(seed int64, dim ...float64) float64) error
4954
}
5055

5156
// ----------------------------------------------------------------------------
@@ -59,6 +64,8 @@ func New(noiseType Algo, seed int64) (Generator, error) {
5964
return perlin.New(seed), nil
6065
case OpenSimplex:
6166
return opensimplex.New(seed), nil
67+
case Custom:
68+
return custom.New(seed), nil
6269
}
6370

6471
return nil, errors.New("unknown noise type")

pkg/custom/custom.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Package custom provides a noise generator using the user-defined function.
3+
Which implements github.com/KEINOS/go-noise/noise interface.
4+
5+
You need to define the function before calling `Eval32` or `Eval64`.
6+
*/
7+
package custom
8+
9+
import (
10+
"math/rand"
11+
"time"
12+
13+
"github.com/pkg/errors"
14+
)
15+
16+
// Generator holds parameters of user-defined noise generator.
17+
type Generator struct {
18+
// func32 is the user-defined function for float32.
19+
func32 func(seed int64, dim ...float32) float32
20+
// func64 is the user-defined function for float64.
21+
func64 func(seed int64, dim ...float64) float64
22+
// Seed holds the seed value for the noise.
23+
Seed int64
24+
}
25+
26+
// New returns a seeded Perlin noise instance.
27+
func New(seed int64) *Generator {
28+
// Rand that uses random values from src to generate other random values.
29+
return &Generator{
30+
Seed: seed,
31+
}
32+
}
33+
34+
// Eval32 returns a float32 noise value using the user-defined function for the given coordinates.
35+
func (n *Generator) Eval32(dim ...float32) float32 {
36+
return n.func32(n.Seed, dim...)
37+
}
38+
39+
// Eval64 returns a float64 noise value using the user-defined function for the given coordinates.
40+
func (n *Generator) Eval64(dim ...float64) float64 {
41+
return n.func64(n.Seed, dim...)
42+
}
43+
44+
// SetEval32 sets the user-defined noise generator function for float32.
45+
func (n *Generator) SetEval32(f func(seed int64, dim ...float32) float32) error {
46+
// Check the function.
47+
rand.Seed(time.Now().UnixNano())
48+
49+
//nolint:gosec // Use of weak random number generation is intended here.
50+
dummyInput := rand.Float32()
51+
52+
if v := f(n.Seed, dummyInput, dummyInput); v <= -1 || 1 < v {
53+
return errors.New("the function must return a value between -1 and 1")
54+
}
55+
56+
n.func32 = f
57+
58+
return nil
59+
}
60+
61+
// SetEval64 sets the user-defined noise generator function for float64.
62+
func (n *Generator) SetEval64(f func(seed int64, dim ...float64) float64) error {
63+
// Check the function.
64+
rand.Seed(time.Now().UnixNano())
65+
66+
//nolint:gosec // Use of weak random number generation is intended here.
67+
dummyInput := rand.Float64()
68+
69+
if v := f(n.Seed, dummyInput, dummyInput); v <= -1 || 1 < v {
70+
return errors.New("the function must return a value between -1 and 1")
71+
}
72+
73+
n.func64 = f
74+
75+
return nil
76+
}

0 commit comments

Comments
 (0)