Skip to content

Commit 12247dc

Browse files
authored
Merge pull request #1 from welshcoder/welshcoder-c++11/14-compat
Made code C++11 and C++14 compatible
2 parents aea5057 + 40a46f1 commit 12247dc

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed

PerlinNoise.hpp

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
# include <random>
4040
# include <type_traits>
4141

42+
43+
#if __cpp_lib_clamp
44+
#define CLAMPFUNC std::clamp<value_type>
45+
#else
46+
#define CLAMPFUNC clamp
47+
#endif
48+
49+
#if __has_cpp_attribute(nodiscard) >= 201907L
50+
#define NODISCARD [[nodiscard]]
51+
#else
52+
#define NODISCARD
53+
#endif
54+
4255
namespace siv
4356
{
4457
# ifdef __cpp_lib_concepts
@@ -48,6 +61,9 @@ namespace siv
4861
# endif
4962
class BasicPerlinNoise
5063
{
64+
template< bool B, class T = void >
65+
using enable_if_t = typename std::enable_if<B,T>::type;
66+
5167
public:
5268

5369
using value_type = Float;
@@ -56,19 +72,28 @@ namespace siv
5672

5773
std::uint8_t p[512];
5874

59-
[[nodiscard]]
75+
#if !__cpp_lib_clamp
76+
static constexpr value_type clamp(const value_type& v, const value_type&lo, const value_type& hi) noexcept
77+
{
78+
if(v<=lo) return lo;
79+
if(v>=hi) return hi;
80+
return v;
81+
}
82+
#endif
83+
84+
NODISCARD
6085
static constexpr value_type Fade(value_type t) noexcept
6186
{
6287
return t * t * t * (t * (t * 6 - 15) + 10);
6388
}
6489

65-
[[nodiscard]]
90+
NODISCARD
6691
static constexpr value_type Lerp(value_type t, value_type a, value_type b) noexcept
6792
{
6893
return a + t * (b - a);
6994
}
7095

71-
[[nodiscard]]
96+
NODISCARD
7297
static constexpr value_type Grad(std::uint8_t hash, value_type x, value_type y, value_type z) noexcept
7398
{
7499
const std::uint8_t h = hash & 15;
@@ -77,7 +102,7 @@ namespace siv
77102
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
78103
}
79104

80-
[[nodiscard]]
105+
NODISCARD
81106
static constexpr value_type Weight(std::int32_t octaves) noexcept
82107
{
83108
value_type amp = 1;
@@ -94,9 +119,7 @@ namespace siv
94119

95120
public:
96121

97-
# if __has_cpp_attribute(nodiscard) >= 201907L
98-
[[nodiscard]]
99-
# endif
122+
NODISCARD
100123
explicit BasicPerlinNoise(std::uint32_t seed = std::default_random_engine::default_seed)
101124
{
102125
reseed(seed);
@@ -105,11 +128,9 @@ namespace siv
105128
# ifdef __cpp_lib_concepts
106129
template <std::uniform_random_bit_generator URNG>
107130
# else
108-
template <class URNG, std::enable_if_t<!std::is_arithmetic_v<URNG>>* = nullptr>
109-
# endif
110-
# if __has_cpp_attribute(nodiscard) >= 201907L
111-
[[nodiscard]]
131+
template <class URNG, enable_if_t<!std::is_arithmetic<URNG>::value> * = nullptr>
112132
# endif
133+
NODISCARD
113134
explicit BasicPerlinNoise(URNG&& urng)
114135
{
115136
reseed(std::forward<URNG>(urng));
@@ -133,7 +154,7 @@ namespace siv
133154
# ifdef __cpp_lib_concepts
134155
template <std::uniform_random_bit_generator URNG>
135156
# else
136-
template <class URNG, std::enable_if_t<!std::is_arithmetic_v<URNG>>* = nullptr>
157+
template <class URNG, enable_if_t<!std::is_arithmetic<URNG>::value>* = nullptr>
137158
# endif
138159
void reseed(URNG&& urng)
139160
{
@@ -154,19 +175,19 @@ namespace siv
154175
//
155176
// Noise [-1, 1]
156177
//
157-
[[nodiscard]]
178+
158179
value_type noise1D(value_type x) const noexcept
159180
{
160181
return noise3D(x, 0, 0);
161182
}
162183

163-
[[nodiscard]]
184+
NODISCARD
164185
value_type noise2D(value_type x, value_type y) const noexcept
165186
{
166187
return noise3D(x, y, 0);
167188
}
168189

169-
[[nodiscard]]
190+
NODISCARD
170191
value_type noise3D(value_type x, value_type y, value_type z) const noexcept
171192
{
172193
const std::int32_t X = static_cast<std::int32_t>(std::floor(x)) & 255;
@@ -198,21 +219,21 @@ namespace siv
198219
//
199220
// Noise [0, 1]
200221
//
201-
[[nodiscard]]
222+
NODISCARD
202223
value_type noise1D_0_1(value_type x) const noexcept
203224
{
204225
return noise1D(x)
205226
* value_type(0.5) + value_type(0.5);
206227
}
207228

208-
[[nodiscard]]
229+
NODISCARD
209230
value_type noise2D_0_1(value_type x, value_type y) const noexcept
210231
{
211232
return noise2D(x, y)
212233
* value_type(0.5) + value_type(0.5);
213234
}
214235

215-
[[nodiscard]]
236+
NODISCARD
216237
value_type noise3D_0_1(value_type x, value_type y, value_type z) const noexcept
217238
{
218239
return noise3D(x, y, z)
@@ -224,7 +245,7 @@ namespace siv
224245
// Accumulated octave noise
225246
// * Return value can be outside the range [-1, 1]
226247
//
227-
[[nodiscard]]
248+
NODISCARD
228249
value_type accumulatedOctaveNoise1D(value_type x, std::int32_t octaves) const noexcept
229250
{
230251
value_type result = 0;
@@ -240,7 +261,7 @@ namespace siv
240261
return result; // unnormalized
241262
}
242263

243-
[[nodiscard]]
264+
NODISCARD
244265
value_type accumulatedOctaveNoise2D(value_type x, value_type y, std::int32_t octaves) const noexcept
245266
{
246267
value_type result = 0;
@@ -257,7 +278,7 @@ namespace siv
257278
return result; // unnormalized
258279
}
259280

260-
[[nodiscard]]
281+
NODISCARD
261282
value_type accumulatedOctaveNoise3D(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
262283
{
263284
value_type result = 0;
@@ -279,21 +300,21 @@ namespace siv
279300
//
280301
// Normalized octave noise [-1, 1]
281302
//
282-
[[nodiscard]]
303+
NODISCARD
283304
value_type normalizedOctaveNoise1D(value_type x, std::int32_t octaves) const noexcept
284305
{
285306
return accumulatedOctaveNoise1D(x, octaves)
286307
/ Weight(octaves);
287308
}
288309

289-
[[nodiscard]]
310+
NODISCARD
290311
value_type normalizedOctaveNoise2D(value_type x, value_type y, std::int32_t octaves) const noexcept
291312
{
292313
return accumulatedOctaveNoise2D(x, y, octaves)
293314
/ Weight(octaves);
294315
}
295316

296-
[[nodiscard]]
317+
NODISCARD
297318
value_type normalizedOctaveNoise3D(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
298319
{
299320
return accumulatedOctaveNoise3D(x, y, z, octaves)
@@ -304,46 +325,46 @@ namespace siv
304325
//
305326
// Accumulated octave noise clamped within the range [0, 1]
306327
//
307-
[[nodiscard]]
328+
NODISCARD
308329
value_type accumulatedOctaveNoise1D_0_1(value_type x, std::int32_t octaves) const noexcept
309330
{
310-
return std::clamp<value_type>(accumulatedOctaveNoise1D(x, octaves)
331+
return CLAMPFUNC(accumulatedOctaveNoise1D(x, octaves)
311332
* value_type(0.5) + value_type(0.5), 0, 1);
312333
}
313334

314-
[[nodiscard]]
335+
NODISCARD
315336
value_type accumulatedOctaveNoise2D_0_1(value_type x, value_type y, std::int32_t octaves) const noexcept
316337
{
317-
return std::clamp<value_type>(accumulatedOctaveNoise2D(x, y, octaves)
338+
return CLAMPFUNC(accumulatedOctaveNoise2D(x, y, octaves)
318339
* value_type(0.5) + value_type(0.5), 0, 1);
319340
}
320341

321-
[[nodiscard]]
342+
NODISCARD
322343
value_type accumulatedOctaveNoise3D_0_1(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
323344
{
324-
return std::clamp<value_type>(accumulatedOctaveNoise3D(x, y, z, octaves)
345+
return CLAMPFUNC(accumulatedOctaveNoise3D(x, y, z, octaves)
325346
* value_type(0.5) + value_type(0.5), 0, 1);
326347
}
327348

328349
///////////////////////////////////////
329350
//
330351
// Normalized octave noise [0, 1]
331352
//
332-
[[nodiscard]]
353+
NODISCARD
333354
value_type normalizedOctaveNoise1D_0_1(value_type x, std::int32_t octaves) const noexcept
334355
{
335356
return normalizedOctaveNoise1D(x, octaves)
336357
* value_type(0.5) + value_type(0.5);
337358
}
338359

339-
[[nodiscard]]
360+
NODISCARD
340361
value_type normalizedOctaveNoise2D_0_1(value_type x, value_type y, std::int32_t octaves) const noexcept
341362
{
342363
return normalizedOctaveNoise2D(x, y, octaves)
343364
* value_type(0.5) + value_type(0.5);
344365
}
345366

346-
[[nodiscard]]
367+
NODISCARD
347368
value_type normalizedOctaveNoise3D_0_1(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
348369
{
349370
return normalizedOctaveNoise3D(x, y, z, octaves)

0 commit comments

Comments
 (0)