Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(noise): fix naming issues / file organisation #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions noise/noise.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub fn Generator.new() Generator {
}

// randomize is a function that shuffle the permutation set inside the Generator struct
// will not shuffle if rand.seed is not changed
pub fn (mut generator Generator) randomize() {
generator.perm = rand.shuffle_clone(permutations) or { panic(err) }
pub fn (mut gen Generator) randomize() {
gen.perm = rand.shuffle_clone(permutations) or { panic(err) }
}
126 changes: 126 additions & 0 deletions noise/perlin.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
module noise

// perlin_2d is a function that return a single value of perlin gen for a given 2d position
pub fn (gen Generator) perlin_2d(x f64, y f64) f64 {
xi := int(x) & 0xFF
yi := int(y) & 0xFF

xf := x - int(x)
yf := y - int(y)

u := fade(xf)
v := fade(yf)

pxi := gen.perm[xi]
pxi1 := gen.perm[xi + 1]

aa := gen.perm[pxi + yi]
ab := gen.perm[pxi + yi + 1]
ba := gen.perm[pxi1 + yi]
bb := gen.perm[pxi1 + yi + 1]

x1 := lerp(perlin_grad_2d(aa, xf, yf), perlin_grad_2d(ba, xf - 1, yf), u)
x2 := lerp(perlin_grad_2d(ab, xf, yf - 1), perlin_grad_2d(bb, xf - 1, yf - 1), u)

return (lerp(x1, x2, v) + 1) / 2
}

// perlin_3d is a function that return a single value of perlin gen for a given 3d position
pub fn (gen Generator) perlin_3d(x f64, y f64, z f64) f64 {
xi := int(x) & 0xFF
yi := int(y) & 0xFF
zi := int(z) & 0xFF
xf := x - int(x)
yf := y - int(y)
zf := z - int(z)
u := fade(xf)
v := fade(yf)
w := fade(zf)

pxi := gen.perm[xi]
pxi_yi := gen.perm[pxi + yi]
pxi_yi1 := gen.perm[pxi + yi + 1]
pxi1 := gen.perm[xi + 1]
pxi1_yi := gen.perm[pxi1 + yi]
pxi1_yi1 := gen.perm[pxi1 + yi + 1]

aaa := gen.perm[pxi_yi + zi]
aba := gen.perm[pxi_yi1 + zi]
aab := gen.perm[pxi_yi + zi + 1]
abb := gen.perm[pxi_yi1 + zi + 1]
baa := gen.perm[pxi1_yi + zi]
bba := gen.perm[pxi1_yi1 + zi]
bab := gen.perm[pxi1_yi + zi + 1]
bbb := gen.perm[pxi1_yi1 + zi + 1]

mut x1 := lerp(perlin_grad_3d(aaa, xf, yf, zf), perlin_grad_3d(baa, xf - 1, yf, zf),
u)
mut x2 := lerp(perlin_grad_3d(aba, xf, yf - 1, zf), perlin_grad_3d(bba, xf - 1, yf - 1,
zf), u)
y1 := lerp(x1, x2, v)
x1 = lerp(perlin_grad_3d(aab, xf, yf, zf - 1), perlin_grad_3d(bab, xf - 1, yf, zf - 1),
u)
x2 = lerp(perlin_grad_3d(abb, xf, yf - 1, zf - 1), perlin_grad_3d(bbb, xf - 1, yf - 1,
zf - 1), u)
y2 := lerp(x1, x2, v)

return (lerp(y1, y2, w) + 1) / 2
}

// perlin_grad_3d is a function that returns a single value of gradient gen for a given 3d position
fn perlin_grad_3d(hash int, x f64, y f64, z f64) f64 {
match hash & 0xF {
0x0 { return x + y }
0x1 { return -x + y }
0x2 { return x - y }
0x3 { return -x - y }
0x4 { return x + z }
0x5 { return -x + z }
0x6 { return x - z }
0x7 { return -x - z }
0x8 { return y + z }
0x9 { return -y + z }
0xA { return y - z }
0xB { return -y - z }
0xC { return y + x }
0xD { return -y + z }
0xE { return y - x }
0xF { return -y - z }
else { return 0 }
}
}

// lerp is a function that return a linear interpolation value for 2 given values and a factor
@[inline]
fn lerp(a f64, b f64, x f64) f64 {
return a + x * (b - a)
}

// fade is a function that return a fade value for a given value
@[inline]
fn fade(t f64) f64 {
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}

// perlin_grad_2d is a function that return a gradient value for a given hash and 2d position
fn perlin_grad_2d(hash int, x f64, y f64) f64 {
match hash & 0xF {
0x0 { return x + y }
0x1 { return -x + y }
0x2 { return x - y }
0x3 { return -x - y }
0x4 { return x }
0x5 { return -x }
0x6 { return x }
0x7 { return -x }
0x8 { return y }
0x9 { return -y }
0xA { return y }
0xB { return -y }
0xC { return y + x }
0xD { return -y }
0xE { return y - x }
0xF { return -y }
else { return 0 }
}
}
61 changes: 0 additions & 61 deletions noise/perlin2d.v

This file was deleted.

16 changes: 0 additions & 16 deletions noise/perlin2d_test.v

This file was deleted.

62 changes: 0 additions & 62 deletions noise/perlin3d.v

This file was deleted.

16 changes: 0 additions & 16 deletions noise/perlin3d_test.v

This file was deleted.

22 changes: 22 additions & 0 deletions noise/perlin_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module noise

import rand
import vsl.float.float64

fn test_perlin_2d() {
rand.seed([u32(3155200429), u32(3208395956)])
mut gen := Generator.new()
gen.randomize()
result := gen.perlin_2d(0.125, 0.125)
expected := 0.4948387311305851
assert float64.tolerance(result, expected, 1.0e-6)
}

fn test_perlin_3d() {
rand.seed([u32(3155200429), u32(3208395956)])
mut gen := Generator.new()
gen.randomize()
result := gen.perlin_3d(0.125, 0.125, 0.125)
expected := 0.3713334855776509
assert float64.tolerance(result, expected, 1.0e-6)
}
Comment on lines +15 to +22
Copy link

@coderabbitai coderabbitai bot Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Reduce code duplication and apply consistent improvements.

The test follows good practices but has duplicated setup code. Consider extracting common setup into a helper function.

+fn setup_generator() !Generator {
+	rand.seed([u32(3155200429), u32(3208395956)])
+	mut gen := Generator.new()
+	gen.randomize()
+	return gen
+}
+
 fn test_perlin_3d() {
-	rand.seed([u32(3155200429), u32(3208395956)])
-	mut gen := Generator.new()
-	gen.randomize()
+	mut gen := setup_generator()!
+	// Test case: Known value at (0.125, 0.125, 0.125)
+	// Expected value derived from reference implementation
 	result := gen.perlin_3d(0.125, 0.125, 0.125)
 	expected := 0.3713334855776509
-	assert float64.tolerance(result, expected, 1.0e-6)
+	assert float64.tolerance(result, expected, tolerance)
+
+	// Additional test cases for edge conditions
+	edge_cases := [
+		(0.0, 0.0, 0.0),
+		(1.0, 1.0, 1.0),
+		(-0.5, 0.5, 0.0)
+	]
+	for point in edge_cases {
+		_ := gen.perlin_3d(point.0, point.1, point.2) // Ensure no panics
+	}
 }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PottierLoic could you use this suggestion from here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look asap, thanks

Loading
Loading