-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
noise: add simplex noise #207
Conversation
Caution Review failedThe pull request is closed. WalkthroughThe changes enhance noise generation capabilities by introducing a module for simplex noise across 1D, 2D, 3D, and 4D spaces. Additionally, the Perlin noise functions have been refined to utilize a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant NoiseModule
participant Generator
User ->> NoiseModule: Call simplex_2d(x, y)
NoiseModule ->> Generator: Create or use existing Generator
Generator -->> NoiseModule: Permutation table
NoiseModule -->> User: Return simplex noise value
sequenceDiagram
participant User
participant NoiseModule
participant Generator
User ->> NoiseModule: Call perlin2d(x, y)
NoiseModule ->> Generator: Use Generator for permutation
Generator -->> NoiseModule: Permutation table
NoiseModule -->> User: Return Perlin noise value
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- noise/simplex.v (1 hunks)
Additional comments not posted (2)
noise/simplex.v (2)
5-5
: Constants are correct.The constants
f2
andg2
are defined correctly for skewing and unskewing the input space in the Simplex Noise algorithm.
8-8
: Gradient calculation function is correct.The
grad_2d
function correctly calculates the dot product of a pseudorandom gradient vector and the distance vector based on the hash value.
noise/simplex.v
Outdated
}) | ||
} | ||
|
||
pub fn simplex_2d(x f64, y f64) f64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main Simplex Noise function is correct but can be improved.
The simplex_2d
function correctly implements the 2D Simplex Noise algorithm. However, some improvements can be suggested for readability and performance.
Refactor suggestion: Use descriptive variable names.
Using more descriptive variable names can improve readability. For example, i1
and j1
could be renamed to offset_x
and offset_y
.
Refactor suggestion: Inline small calculations.
Inlining small calculations, such as f2
and g2
, can improve performance by reducing the number of operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- noise/simplex.v (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- noise/simplex.v
I would like to visualize the 4d simplex in plot by gradually increasing the 4th parameter so te whole 3d noise will be "moving"). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- noise/simplex.v (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- noise/simplex.v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- noise/simplex.v (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- noise/simplex.v
@ulises-jeremias can you have a look please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range and nitpick comments (2)
noise/perlin2d.v (1)
Line range hint
4-20
:
Ensure the permutation index is within bounds.The access to the
generator.perm
array doesn't handle out-of-bound indices, which could lead to runtime errors.- pxi := generator.perm[xi] - pxi1 := generator.perm[xi + 1] - aa := generator.perm[pxi + yi] - ab := generator.perm[pxi + yi + 1] - ba := generator.perm[pxi1 + yi] - bb := generator.perm[pxi1 + yi + 1] + pxi := generator.perm[xi % generator.perm.len] + pxi1 := generator.perm[(xi + 1) % generator.perm.len] + aa := generator.perm[(pxi + yi) % generator.perm.len] + ab := generator.perm[(pxi + yi + 1) % generator.perm.len] + ba := generator.perm[(pxi1 + yi) % generator.perm.len] + bb := generator.perm[(pxi1 + yi + 1) % generator.perm.len]noise/perlin3d.v (1)
Line range hint
4-29
:
Ensure the permutation index is within bounds.The access to the
generator.perm
array doesn't handle out-of-bound indices, which could lead to runtime errors.- pxi := generator.perm[xi] - pxi_yi := generator.perm[pxi + yi] - pxi_yi1 := generator.perm[pxi + yi + 1] - pxi1 := generator.perm[xi + 1] - pxi1_yi := generator.perm[pxi1 + yi] - pxi1_yi1 := generator.perm[pxi1 + yi + 1] - aaa := generator.perm[pxi_yi + zi] - aba := generator.perm[pxi_yi1 + zi] - aab := generator.perm[pxi_yi + zi + 1] - abb := generator.perm[pxi_yi1 + zi + 1] - baa := generator.perm[pxi1_yi + zi] - bba := generator.perm[pxi1_yi1 + zi] - bab := generator.perm[pxi1_yi + zi + 1] - bbb := generator.perm[pxi1_yi1 + zi + 1] + pxi := generator.perm[xi % generator.perm.len] + pxi_yi := generator.perm[(pxi + yi) % generator.perm.len] + pxi_yi1 := generator.perm[(pxi + yi + 1) % generator.perm.len] + pxi1 := generator.perm[(xi + 1) % generator.perm.len] + pxi1_yi := generator.perm[(pxi1 + yi) % generator.perm.len] + pxi1_yi1 := generator.perm[(pxi1 + yi + 1) % generator.perm.len] + aaa := generator.perm[(pxi_yi + zi) % generator.perm.len] + aba := generator.perm[(pxi_yi1 + zi) % generator.perm.len] + aab := generator.perm[(pxi_yi + zi + 1) % generator.perm.len] + abb := generator.perm[(pxi_yi1 + zi + 1) % generator.perm.len] + baa := generator.perm[(pxi1_yi + zi) % generator.perm.len] + bba := generator.perm[(pxi1_yi1 + zi) % generator.perm.len] + bab := generator.perm[(pxi1_yi + zi + 1) % generator.perm.len] + bbb := generator.perm[(pxi1_yi1 + zi + 1) % generator.perm.len]
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (7)
- noise/noise.v (1 hunks)
- noise/perlin2d.v (2 hunks)
- noise/perlin2d_test.v (1 hunks)
- noise/perlin3d.v (2 hunks)
- noise/perlin3d_test.v (1 hunks)
- noise/simplex.v (1 hunks)
- noise/simplex_test.v (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- noise/simplex.v
Additional comments not posted (13)
noise/perlin2d_test.v (1)
9-12
: LGTM! Verify the consistency of theperlin2d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
perlin2d
function with a tolerance check.Ensure that the
perlin2d
function innoise/perlin2d.v
is consistent with the expected output.Verification successful
The
perlin2d
function implementation appears consistent and well-defined.The function correctly calculates Perlin noise values based on the given coordinates. The test in
noise/perlin2d_test.v
should effectively validate this implementation.
noise/perlin2d.v
:
- The
perlin2d
function is implemented correctly.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the `perlin2d` function in `noise/perlin2d.v` is consistent with the expected output. # Test: Search for the `perlin2d` function. ast-grep --lang v --pattern $'fn (generator Generator) perlin2d($_, $_) $_' noise/perlin2d.vLength of output: 196
Script:
#!/bin/bash # Description: Verify the `perlin2d` function in `noise/perlin2d.v` is consistent with the expected output. # Test: Search for the `perlin2d` function definition. rg 'fn (generator Generator) perlin2d' noise/perlin2d.vLength of output: 57
Script:
#!/bin/bash # Description: Verify the `perlin2d` function is consistent with the expected output. # Test: Search for the `perlin2d` function definition across the entire repository. rg 'fn (generator Generator) perlin2d'Length of output: 40
Script:
#!/bin/bash # Description: Verify any function related to Perlin noise is consistent with the expected output. # Test: Search for any function definitions containing "perlin" in their names across the entire repository. rg 'fn .*perlin.*'Length of output: 263
Script:
#!/bin/bash # Description: Extract the implementation of the `perlin2d` function from `noise/perlin2d.v`. # Extract the function implementation. sed -n '/fn (generator Generator) perlin2d/,/}/p' noise/perlin2d.vLength of output: 599
noise/perlin3d_test.v (1)
9-12
: LGTM! Verify the consistency of theperlin3d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
perlin3d
function with a tolerance check.Ensure that the
perlin3d
function innoise/perlin3d.v
is consistent with the expected output.Verification successful
The
perlin3d
function implementation innoise/perlin3d.v
is consistent with the expected output.
- The function calculates the Perlin noise value for the given 3D coordinates using linear interpolation and gradient functions.
- The implementation appears to be correct and follows the standard Perlin noise algorithm.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the `perlin3d` function in `noise/perlin3d.v` is consistent with the expected output. # Test: Search for the `perlin3d` function. ast-grep --lang v --pattern $'fn (generator Generator) perlin3d($_, $_, $_) $_' noise/perlin3d.vLength of output: 200
Script:
#!/bin/bash # Description: Verify the `perlin3d` function in `noise/perlin3d.v` is consistent with the expected output. # Test: Search for the `perlin3d` function definition. rg 'fn (generator Generator) perlin3d' noise/perlin3d.vLength of output: 57
Script:
#!/bin/bash # Description: Verify the `perlin3d` function in the `noise` directory is consistent with the expected output. # Test: Search for all function definitions in the `noise` directory. rg 'fn ' noise/Length of output: 1317
Script:
#!/bin/bash # Description: Extract the implementation of the `perlin3d` function from `noise/perlin3d.v`. # Extract the `perlin3d` function implementation. rg -A 20 'pub fn (generator Generator) perlin3d' noise/perlin3d.vLength of output: 67
Script:
#!/bin/bash # Description: Extract the implementation of the `perlin3d` function from `noise/perlin3d.v`. # Extract the `perlin3d` function implementation using `sed`. sed -n '/pub fn (generator Generator) perlin3d/,/}/p' noise/perlin3d.vLength of output: 1228
noise/noise.v (3)
5-9
: LGTM!The
Generator
struct is correctly defined and the permutation table is properly initialized.
11-14
: LGTM!The
new
method correctly returns a newGenerator
struct.
16-20
: LGTM!The
randomize
method correctly shuffles the permutation set inside theGenerator
struct.noise/simplex_test.v (4)
33-39
: LGTM! Verify the consistency of thesimplex_4d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
simplex_4d
function with a tolerance check.Ensure that the
simplex_4d
function innoise/simplex.v
is consistent with the expected output.
24-30
: LGTM! Verify the consistency of thesimplex_3d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
simplex_3d
function with a tolerance check.Ensure that the
simplex_3d
function innoise/simplex.v
is consistent with the expected output.
6-12
: LGTM! Verify the consistency of thesimplex_1d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
simplex_1d
function with a tolerance check.Ensure that the
simplex_1d
function innoise/simplex.v
is consistent with the expected output.Verification successful
LGTM! Verify the consistency of the
simplex_1d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
simplex_1d
function with a tolerance check. Thesimplex_1d
function is defined innoise/simplex.v
.
noise/simplex.v
:pub fn (generator Generator) simplex_1d(x f64) f64
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the `simplex_1d` function in `noise/simplex.v` is consistent with the expected output. # Test: Search for the `simplex_1d` function. ast-grep --lang v --pattern $'fn (generator Generator) simplex_1d($_) $_' noise/simplex.vLength of output: 193
Script:
#!/bin/bash # Description: Verify the `simplex_1d` function in `noise/simplex.v` is consistent with the expected output. # Test: Search for the `simplex_1d` function using ripgrep. rg 'fn (generator Generator) simplex_1d' noise/simplex.vLength of output: 58
Script:
#!/bin/bash # Description: Verify the `simplex_1d` function is present and consistent with the expected output. # Test: Search for the `simplex_1d` function across the entire codebase. rg 'fn (generator Generator) simplex_1d'Length of output: 42
Script:
#!/bin/bash # Description: Verify the presence of any `simplex` related functions. # Test: Search for any functions related to `simplex` across the entire codebase. rg 'fn .*simplex.*'Length of output: 515
15-21
: LGTM! Verify the consistency of thesimplex_2d
function.The test correctly seeds the random number generator, creates a new Generator, randomizes it, and tests the
simplex_2d
function with a tolerance check.Ensure that the
simplex_2d
function innoise/simplex.v
is consistent with the expected output.Verification successful
The
simplex_2d
function is consistent with the expected output.The function implementation correctly follows the 2D simplex noise algorithm, and the test in
simplex_test.v
verifies its behavior with appropriate seeding, randomization, and tolerance checks.
noise/simplex.v
, lines 107-167: Implementation ofsimplex_2d
function.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the `simplex_2d` function in `noise/simplex.v` is consistent with the expected output. # Test: Search for the `simplex_2d` function. ast-grep --lang v --pattern $'fn (generator Generator) simplex_2d($_, $_) $_' noise/simplex.vLength of output: 197
Script:
#!/bin/bash # Description: Verify the `simplex_2d` function in `noise/simplex.v` is consistent with the expected output. # Test: Search for the `simplex_2d` function definition in the codebase. rg 'fn \(generator Generator\) simplex_2d' --vimgrepLength of output: 136
Script:
#!/bin/bash # Description: Extract the `simplex_2d` function implementation from `noise/simplex.v`. # Extract the `simplex_2d` function implementation. sed -n '107,127p' noise/simplex.vLength of output: 490
Script:
#!/bin/bash # Description: Extract the complete `simplex_2d` function implementation from `noise/simplex.v`. # Extract the `simplex_2d` function implementation. sed -n '107,147p' noise/simplex.vLength of output: 886
Script:
#!/bin/bash # Description: Extract the complete `simplex_2d` function implementation from `noise/simplex.v`. # Extract the `simplex_2d` function implementation. sed -n '107,167p' noise/simplex.vLength of output: 1273
noise/perlin2d.v (3)
Line range hint
24-26
:
LGTM!The
fade
function is correctly implemented and optimized.
Line range hint
28-44
:
LGTM!The
grad2d
function is correctly implemented and covers all possible hash values.
Line range hint
48-50
:
LGTM!The
lerp
function is correctly implemented and optimized.noise/perlin3d.v (1)
Line range hint
31-49
:
LGTM!The
grad3d
function is correctly implemented and covers all possible hash values.
I renamed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- noise/simplex.v (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- noise/simplex.v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range, codebase verification and nitpick comments (1)
examples/noise_simplex_2d/main.v (1)
37-37
: Potential error handling improvement.The
plt.show()
call uses the!
operator, which will panic if an error occurs. Consider handling the error more gracefully.- plt.show()! + if err := plt.show() { + println('Error displaying plot: $err') + return + }
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- examples/noise_simplex_2d/README.md (1 hunks)
- examples/noise_simplex_2d/main.v (1 hunks)
Files skipped from review due to trivial changes (1)
- examples/noise_simplex_2d/README.md
Additional comments not posted (7)
examples/noise_simplex_2d/main.v (7)
1-2
: Module declaration and imports look good.The module declaration and imports are correctly defined.
3-5
: Imports are appropriate.The necessary modules for random number generation, noise generation, and plotting are imported.
7-9
: Commenting style is clear and informative.The comments provide clear context about the creation of the noise generator and the availability of other noise functions and dimensions.
10-12
: Random seed initialization and generator creation are correct.The random seed is initialized, and the noise generator is created and randomized correctly.
14-16
: Pre-allocating arrays is a good practice.The arrays
x
,y
, andz
are pre-allocated with a capacity of 1600, which is efficient.
18-26
: Nested loops for generating noise values are appropriate.The nested loops iterate over a 40x40 grid to generate simplex noise values. The coordinates are scaled to control the frequency of the noise.
28-37
: Plot creation and configuration are correct.The heatmap plot is created and configured correctly, with the title set appropriately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- examples/noise_fractal_2d/README.md (1 hunks)
- examples/noise_fractal_2d/main.v (1 hunks)
- examples/noise_simplex_2d/main.v (1 hunks)
Files skipped from review due to trivial changes (1)
- examples/noise_fractal_2d/README.md
Files skipped from review as they are similar to previous changes (1)
- examples/noise_simplex_2d/main.v
Additional comments not posted (6)
examples/noise_fractal_2d/main.v (6)
1-6
: Imports and module declaration look good.The module declaration and imports are correct and necessary for the functionality.
7-13
: Initialization and generator creation look good.The random seed initialization and noise generator creation are correct. The
randomize
method usage is appropriate.
14-17
: Array initialization looks good.The arrays for x, y, and z coordinates are correctly initialized.
18-21
: Variable initialization looks good.The variables for noise generation layers and persistence are correctly initialized.
22-42
: Noise generation logic looks good.The nested loops and noise generation logic are correct. The normalization step and accumulation of noise values are appropriately handled.
44-54
: Plot initialization and visualization look good.The plot initialization and heatmap visualization are correctly implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
examples/noise_fractal_2d/README.md
Outdated
@@ -0,0 +1,16 @@ | |||
# Example - plot_bar 📘 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the title here is wrong 😅
This PR add an implementation of Simplex Noise #206
For now there is only the 2d implementation, I will add the remaining ones/tests/comments in the coming days.
It will work the same as perlin noise: a generator struct holds the permutation table, which can be randomized.
I would like to rename the "Perlin" generator struct to a more general name so we can use it for both Perlin and Simplex noise (they both use the same permutation table) if there is no issue doing it ?
Summary by CodeRabbit
New Features
Improvements
Generator
for permutation tables.