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+
4255namespace 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,27 @@ namespace siv
94119
95120 public:
96121
97- # if __has_cpp_attribute(nodiscard) >= 201907L
98- [[nodiscard]]
99- # endif
122+ NODISCARD BasicPerlinNoise ()
123+ {
124+ deserialize (
125+ std::array<uint8_t ,256 >(
126+ 151 ,160 ,137 ,91 ,90 ,15 ,131 ,13 ,201 ,95 ,96 ,53 ,194 ,233 , 7 ,225 ,140 ,36 ,103 ,30 ,69 ,142 ,
127+ 8 ,99 ,37 ,240 ,21 ,10 ,23 ,190 ,6 ,148 ,247 ,120 ,234 ,75 ,0 ,26 ,197 ,62 ,94 ,252 ,219 ,203 ,117 ,
128+ 35 ,11 ,32 ,57 ,177 ,33 ,88 ,237 ,149 ,56 ,87 ,174 ,20 ,125 ,136 ,171 ,168 , 68 ,175 ,74 ,165 ,71 ,
129+ 134 ,139 ,48 ,27 ,166 ,77 ,146 ,158 ,231 ,83 ,111 ,229 ,122 ,60 ,211 ,133 ,230 ,220 ,105 ,92 ,41 ,
130+ 55 ,46 ,245 ,40 ,244 ,102 ,143 ,54 ,65 ,25 ,63 ,161 , 1 ,216 ,80 ,73 ,209 ,76 ,132 ,187 ,208 , 89 ,
131+ 18 ,169 ,200 ,196 ,135 ,130 ,116 ,188 ,159 ,86 ,164 , 100 ,109 ,198 ,173 ,186 , 3 , 64 ,52 ,217 ,
132+ 226 ,250 ,124 ,123 , 5 ,202 , 38 ,147 ,118 ,126 ,255 ,82 ,85 ,212 ,207 ,206 ,59 ,227 ,47 ,16 ,58 ,
133+ 17 ,182 ,189 ,28 ,42 ,223 ,183 ,170 ,213 ,119 ,248 ,152 ,2 ,44 ,154 ,163 ,70 ,221 ,153 ,101 ,155 ,
134+ 167 ,43 ,172 ,9 ,129 ,22 ,39 ,253 ,19 ,98 ,108 ,110 ,79 ,113 ,224 ,232 ,178 ,185 , 112 ,104 ,218 ,
135+ 246 ,97 ,228 ,251 , 34 ,242 ,193 ,238 ,210 ,144 , 12 ,191 ,179 ,162 ,241 ,81 ,51 ,145 ,235 ,249 ,
136+ 14 ,239 ,107 ,49 ,192 ,214 ,31 ,181 ,199 ,106 ,157 ,184 ,84 ,204 ,176 ,115 ,121 ,50 ,45 ,127 , 4 ,
137+ 150 ,254 ,138 ,236 ,205 ,93 ,222 ,114 ,67 ,29 ,24 ,72 ,243 ,141 ,128 ,195 ,78 ,66 ,215 ,61 ,156 ,180
138+ )
139+ );
140+ }
141+
142+ NODISCARD
100143 explicit BasicPerlinNoise (std::uint32_t seed = std::default_random_engine::default_seed)
101144 {
102145 reseed (seed);
@@ -105,11 +148,9 @@ namespace siv
105148 # ifdef __cpp_lib_concepts
106149 template <std::uniform_random_bit_generator URNG>
107150 # 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]]
151+ template <class URNG , enable_if_t <!std::is_arithmetic<URNG>::value> * = nullptr >
112152 # endif
153+ NODISCARD
113154 explicit BasicPerlinNoise (URNG&& urng)
114155 {
115156 reseed (std::forward<URNG>(urng));
@@ -133,7 +174,7 @@ namespace siv
133174 # ifdef __cpp_lib_concepts
134175 template <std::uniform_random_bit_generator URNG>
135176 # else
136- template <class URNG , std:: enable_if_t <!std::is_arithmetic_v <URNG>>* = nullptr >
177+ template <class URNG , enable_if_t <!std::is_arithmetic <URNG>::value >* = nullptr >
137178 # endif
138179 void reseed (URNG&& urng)
139180 {
@@ -154,19 +195,19 @@ namespace siv
154195 //
155196 // Noise [-1, 1]
156197 //
157- [[nodiscard]]
198+
158199 value_type noise1D (value_type x) const noexcept
159200 {
160201 return noise3D (x, 0 , 0 );
161202 }
162203
163- [[nodiscard]]
204+ NODISCARD
164205 value_type noise2D (value_type x, value_type y) const noexcept
165206 {
166207 return noise3D (x, y, 0 );
167208 }
168209
169- [[nodiscard]]
210+ NODISCARD
170211 value_type noise3D (value_type x, value_type y, value_type z) const noexcept
171212 {
172213 const std::int32_t X = static_cast <std::int32_t >(std::floor (x)) & 255 ;
@@ -198,21 +239,21 @@ namespace siv
198239 //
199240 // Noise [0, 1]
200241 //
201- [[nodiscard]]
242+ NODISCARD
202243 value_type noise1D_0_1 (value_type x) const noexcept
203244 {
204245 return noise1D (x)
205246 * value_type (0.5 ) + value_type (0.5 );
206247 }
207248
208- [[nodiscard]]
249+ NODISCARD
209250 value_type noise2D_0_1 (value_type x, value_type y) const noexcept
210251 {
211252 return noise2D (x, y)
212253 * value_type (0.5 ) + value_type (0.5 );
213254 }
214255
215- [[nodiscard]]
256+ NODISCARD
216257 value_type noise3D_0_1 (value_type x, value_type y, value_type z) const noexcept
217258 {
218259 return noise3D (x, y, z)
@@ -224,7 +265,7 @@ namespace siv
224265 // Accumulated octave noise
225266 // * Return value can be outside the range [-1, 1]
226267 //
227- [[nodiscard]]
268+ NODISCARD
228269 value_type accumulatedOctaveNoise1D (value_type x, std::int32_t octaves) const noexcept
229270 {
230271 value_type result = 0 ;
@@ -240,7 +281,7 @@ namespace siv
240281 return result; // unnormalized
241282 }
242283
243- [[nodiscard]]
284+ NODISCARD
244285 value_type accumulatedOctaveNoise2D (value_type x, value_type y, std::int32_t octaves) const noexcept
245286 {
246287 value_type result = 0 ;
@@ -257,7 +298,7 @@ namespace siv
257298 return result; // unnormalized
258299 }
259300
260- [[nodiscard]]
301+ NODISCARD
261302 value_type accumulatedOctaveNoise3D (value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
262303 {
263304 value_type result = 0 ;
@@ -279,21 +320,21 @@ namespace siv
279320 //
280321 // Normalized octave noise [-1, 1]
281322 //
282- [[nodiscard]]
323+ NODISCARD
283324 value_type normalizedOctaveNoise1D (value_type x, std::int32_t octaves) const noexcept
284325 {
285326 return accumulatedOctaveNoise1D (x, octaves)
286327 / Weight (octaves);
287328 }
288329
289- [[nodiscard]]
330+ NODISCARD
290331 value_type normalizedOctaveNoise2D (value_type x, value_type y, std::int32_t octaves) const noexcept
291332 {
292333 return accumulatedOctaveNoise2D (x, y, octaves)
293334 / Weight (octaves);
294335 }
295336
296- [[nodiscard]]
337+ NODISCARD
297338 value_type normalizedOctaveNoise3D (value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
298339 {
299340 return accumulatedOctaveNoise3D (x, y, z, octaves)
@@ -304,46 +345,46 @@ namespace siv
304345 //
305346 // Accumulated octave noise clamped within the range [0, 1]
306347 //
307- [[nodiscard]]
348+ NODISCARD
308349 value_type accumulatedOctaveNoise1D_0_1 (value_type x, std::int32_t octaves) const noexcept
309350 {
310- return std::clamp<value_type> (accumulatedOctaveNoise1D (x, octaves)
351+ return CLAMPFUNC (accumulatedOctaveNoise1D (x, octaves)
311352 * value_type (0.5 ) + value_type (0.5 ), 0 , 1 );
312353 }
313354
314- [[nodiscard]]
355+ NODISCARD
315356 value_type accumulatedOctaveNoise2D_0_1 (value_type x, value_type y, std::int32_t octaves) const noexcept
316357 {
317- return std::clamp<value_type> (accumulatedOctaveNoise2D (x, y, octaves)
358+ return CLAMPFUNC (accumulatedOctaveNoise2D (x, y, octaves)
318359 * value_type (0.5 ) + value_type (0.5 ), 0 , 1 );
319360 }
320361
321- [[nodiscard]]
362+ NODISCARD
322363 value_type accumulatedOctaveNoise3D_0_1 (value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
323364 {
324- return std::clamp<value_type> (accumulatedOctaveNoise3D (x, y, z, octaves)
365+ return CLAMPFUNC (accumulatedOctaveNoise3D (x, y, z, octaves)
325366 * value_type (0.5 ) + value_type (0.5 ), 0 , 1 );
326367 }
327368
328369 // /////////////////////////////////////
329370 //
330371 // Normalized octave noise [0, 1]
331372 //
332- [[nodiscard]]
373+ NODISCARD
333374 value_type normalizedOctaveNoise1D_0_1 (value_type x, std::int32_t octaves) const noexcept
334375 {
335376 return normalizedOctaveNoise1D (x, octaves)
336377 * value_type (0.5 ) + value_type (0.5 );
337378 }
338379
339- [[nodiscard]]
380+ NODISCARD
340381 value_type normalizedOctaveNoise2D_0_1 (value_type x, value_type y, std::int32_t octaves) const noexcept
341382 {
342383 return normalizedOctaveNoise2D (x, y, octaves)
343384 * value_type (0.5 ) + value_type (0.5 );
344385 }
345386
346- [[nodiscard]]
387+ NODISCARD
347388 value_type normalizedOctaveNoise3D_0_1 (value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept
348389 {
349390 return normalizedOctaveNoise3D (x, y, z, octaves)
@@ -404,4 +445,4 @@ namespace siv
404445 };
405446
406447 using PerlinNoise = BasicPerlinNoise<double >;
407- }
448+ }
0 commit comments