Skip to content

Commit 11a4bca

Browse files
author
Dan Liebault
committed
Refactor du code
- Conversion de méthodes en `const`, simplification du destructeur, et utilisation de `constexpr` pour les dimensions de l'image. - Les types de certaines variables ont été modifiés pour améliorer la précision, et la fonction `FastFloor` a été mise à jour pour utiliser `static_cast`.
1 parent e7f6e39 commit 11a4bca

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

R3DVoxel/utils/perlin.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,42 @@ PerlinNoise::PerlinNoise(unsigned int seed) : m_seed(seed)
99
perm.insert(perm.end(), perm.begin(), perm.end());
1010
}
1111

12-
PerlinNoise::~PerlinNoise()
13-
{
14-
}
15-
16-
float PerlinNoise::perlin(float x, float y)
12+
float PerlinNoise::perlin(float x, float y) const
1713
{
1814
x *= 0.00999999978f;
1915
y *= 0.00999999978f;
2016

2117
int x0 = FastFloor(x);
2218
int y0 = FastFloor(y);
2319

24-
float f_x0 = x - static_cast<float>(x0);
25-
float f_y0 = y - static_cast<float>(y0);
26-
float f_x1 = f_x0 - 1.0f;
27-
float f_y1 = f_y0 - 1.0f;
20+
const float f_x0 = x - static_cast<float>(x0);
21+
const float f_y0 = y - static_cast<float>(y0);
22+
const float f_x1 = f_x0 - 1.0f;
23+
const float f_y1 = f_y0 - 1.0f;
2824

29-
float xs = fade(f_x0);
30-
float ys = fade(f_y0);
25+
const float xs = static_cast<float>(fade(f_x0));
26+
const float ys = static_cast<float>(fade(f_y0));
3127

3228
x0 *= 501125321;
3329
y0 *= 1136930381;
34-
int x1 = x0 + 501125321;
35-
int y1 = y0 + 1136930381;
30+
const int x1 = x0 + 501125321;
31+
const int y1 = y0 + 1136930381;
3632

37-
float n0 = lerp(xs, dotgridgradient(x0, y0, f_x0, f_y0), dotgridgradient(x1, y0, f_x1, f_y0));
38-
float n1 = lerp(xs, dotgridgradient(x0, y1, f_x0, f_y1), dotgridgradient(x1, y1, f_x1, f_y1));
33+
const double n0 = lerp(xs, dotgridgradient(x0, y0, f_x0, f_y0), dotgridgradient(x1, y0, f_x1, f_y0));
34+
const double n1 = lerp(xs, dotgridgradient(x0, y1, f_x0, f_y1), dotgridgradient(x1, y1, f_x1, f_y1));
3935

40-
return lerp(ys, n0, n1) * 1.4247691104677813f;
36+
const auto r = lerp(ys, static_cast<float>(n0), static_cast<float>(n1));
37+
return static_cast<float>(r) * 1.4247691104677813f;
4138
}
4239

43-
void PerlinNoise::exportppm()
40+
void PerlinNoise::exportppm() const
4441
{
4542
std::ofstream file;
4643
file.open("image.ppm");
4744

4845
// Image
49-
const int image_width = 500;
50-
const int image_height = 500;
46+
constexpr int image_width = 500;
47+
constexpr int image_height = 500;
5148

5249
// Render
5350
file << "P3\n" << image_width << ' ' << image_height << "\n255\n";
@@ -74,33 +71,33 @@ void PerlinNoise::exportppm()
7471
file.close();
7572
}
7673

77-
float PerlinNoise::dotgridgradient(int ix, int iy, float x, float y)
74+
float PerlinNoise::dotgridgradient(int ix, int iy, float x, float y) const
7875
{
7976
int hash = m_seed ^ ix ^ iy;
8077
hash *= 0x27d4eb2d;
8178
hash ^= hash >> 15;
8279
hash &= 127 << 1;
8380

84-
float xg = Lookup<float>::Gradients2D[hash];
85-
float yg = Lookup<float>::Gradients2D[hash | 1];
81+
const float xg = Lookup<float>::Gradients2D[hash];
82+
const float yg = Lookup<float>::Gradients2D[hash | 1];
8683

8784
return x * xg + y * yg;
8885
}
8986

90-
double PerlinNoise::gradiant(int hash, double x, double y)
87+
double PerlinNoise::gradiant(int hash, double x, double y) const
9188
{
9289
int h = hash & 7;
9390
double u = h < 4 ? x : y;
9491
double v = h < 4 ? y : x;
9592
return ((h & 1) ? -u : u) + ((h & 2) ? -2.0 * v : 2.0 * v);
9693
}
9794

98-
double PerlinNoise::lerp(float t, float a, float b)
95+
double PerlinNoise::lerp(float t, float a, float b) const
9996
{
10097
return a + t * (b - a);
10198
}
10299

103-
double PerlinNoise::fade(double t)
100+
double PerlinNoise::fade(double t) const
104101
{
105102
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
106103
}

R3DVoxel/utils/perlin.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ const T Lookup<T>::Gradients2D[] =
5454
-0.38268343236509f, -0.923879532511287f, -0.923879532511287f, -0.38268343236509f, -0.923879532511287f, 0.38268343236509f, -0.38268343236509f, 0.923879532511287f,
5555
};
5656

57-
static int FastFloor(float f) { return f >= 0 ? (int)f : (int)f - 1; }
57+
static int FastFloor(float f) { return f >= 0 ? static_cast<int>(f) : static_cast<int>(f) - 1; }
5858

5959
class PerlinNoise
6060
{
6161
public:
6262
PerlinNoise(unsigned int seed);
63-
~PerlinNoise();
63+
~PerlinNoise() = default;
6464

65-
float perlin(float x, float y);
66-
void exportppm();
65+
float perlin(float x, float y) const;
66+
void exportppm() const;
6767

6868
private:
69-
float dotgridgradient(int ix, int iy, float x, float y);
70-
double gradiant(int hash, double x, double y);
71-
double lerp(float t, float a, float b);
72-
double fade(double t);
69+
float dotgridgradient(int ix, int iy, float x, float y) const;
70+
double gradiant(int hash, double x, double y) const;
71+
double lerp(float t, float a, float b) const;
72+
double fade(double t) const;
7373

7474
std::vector<int> perm;
7575
int m_seed;

0 commit comments

Comments
 (0)