Cuberite
A lightweight, fast and extensible game server for Minecraft
InterpolNoise.h
Go to the documentation of this file.
1 
2 // InterpolNoise.h
3 
4 // Implements the cInterpolNoise class template representing a noise that interpolates the values between integer coords from a single set of neighbors
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "Noise.h"
13 
14 #define FAST_FLOOR(x) (((x) < 0) ? ((static_cast<int>(x)) - 1) : (static_cast<int>(x)))
15 
16 
17 
18 
19 
21 // cInterpolCell2D:
22 
23 template <typename T>
25 {
26 public:
28  const cNoise & a_Noise,
29  NOISE_DATATYPE * a_Array,
30  int a_SizeX, int a_SizeY,
31  const NOISE_DATATYPE * a_FracX,
32  const NOISE_DATATYPE * a_FracY
33  ):
34  m_Noise(a_Noise),
36  m_CurFloorX(0),
37  m_CurFloorY(0),
38  m_Array(a_Array),
39  m_SizeX(a_SizeX),
40  m_SizeY(a_SizeY),
41  m_FracX(a_FracX),
42  m_FracY(a_FracY)
43  {
44  }
45 
46 
48  void Generate(
49  int a_FromX, int a_ToX,
50  int a_FromY, int a_ToY
51  )
52  {
53  for (int y = a_FromY; y < a_ToY; y++)
54  {
55  NOISE_DATATYPE Interp[2];
56  NOISE_DATATYPE FracY = T::coeff(m_FracY[y]);
57  Interp[0] = Lerp((*m_WorkRnds)[0][0], (*m_WorkRnds)[0][1], FracY);
58  Interp[1] = Lerp((*m_WorkRnds)[1][0], (*m_WorkRnds)[1][1], FracY);
59  int idx = y * m_SizeX + a_FromX;
60  for (int x = a_FromX; x < a_ToX; x++)
61  {
62  m_Array[idx++] = Lerp(Interp[0], Interp[1], T::coeff(m_FracX[x]));
63  } // for x
64  } // for y
65  }
66 
67 
69  void InitWorkRnds(int a_FloorX, int a_FloorY)
70  {
71  m_CurFloorX = a_FloorX;
72  m_CurFloorY = a_FloorY;
73  (*m_WorkRnds)[0][0] = m_Noise.IntNoise2D(m_CurFloorX, m_CurFloorY);
74  (*m_WorkRnds)[0][1] = m_Noise.IntNoise2D(m_CurFloorX, m_CurFloorY + 1);
75  (*m_WorkRnds)[1][0] = m_Noise.IntNoise2D(m_CurFloorX + 1, m_CurFloorY);
76  (*m_WorkRnds)[1][1] = m_Noise.IntNoise2D(m_CurFloorX + 1, m_CurFloorY + 1);
77  }
78 
79 
81  void Move(int a_NewFloorX, int a_NewFloorY)
82  {
83  // Swap the doublebuffer:
84  int OldFloorX = m_CurFloorX;
85  int OldFloorY = m_CurFloorY;
86  Workspace * OldWorkRnds = m_WorkRnds;
88 
89  // Reuse as much of the old workspace as possible:
90  // TODO: Try out if simply calculating all 4 elements each time is faster than this monster loop
91  int DiffX = OldFloorX - a_NewFloorX;
92  int DiffY = OldFloorY - a_NewFloorY;
93  for (int x = 0; x < 2; x++)
94  {
95  int cx = a_NewFloorX + x;
96  int OldX = x - DiffX; // Where would this X be in the old grid?
97  for (int y = 0; y < 2; y++)
98  {
99  int cy = a_NewFloorY + y;
100  int OldY = y - DiffY; // Where would this Y be in the old grid?
101  if ((OldX >= 0) && (OldX < 2) && (OldY >= 0) && (OldY < 2))
102  {
103  (*m_WorkRnds)[x][y] = (*OldWorkRnds)[OldX][OldY];
104  }
105  else
106  {
107  (*m_WorkRnds)[x][y] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise2D(cx, cy));
108  }
109  }
110  }
111  m_CurFloorX = a_NewFloorX;
112  m_CurFloorY = a_NewFloorY;
113  }
114 
115 protected:
116  typedef NOISE_DATATYPE Workspace[2][2];
117 
119  const cNoise & m_Noise;
120 
123 
126 
129 
132 
135 
138 
142 } ;
143 
144 
145 
146 
147 
149 // cInterpolCell3D:
150 
155 template <typename T>
157 {
158 public:
160  const cNoise & a_Noise,
161  NOISE_DATATYPE * a_Array,
162  int a_SizeX, int a_SizeY, int a_SizeZ,
163  const NOISE_DATATYPE * a_FracX,
164  const NOISE_DATATYPE * a_FracY,
165  const NOISE_DATATYPE * a_FracZ
166  ):
167  m_Noise(a_Noise),
169  m_CurFloorX(0),
170  m_CurFloorY(0),
171  m_CurFloorZ(0),
172  m_Array(a_Array),
173  m_SizeX(a_SizeX),
174  m_SizeY(a_SizeY),
175  m_SizeZ(a_SizeZ),
176  m_FracX(a_FracX),
177  m_FracY(a_FracY),
178  m_FracZ(a_FracZ)
179  {
180  }
181 
182 
184  void Generate(
185  int a_FromX, int a_ToX,
186  int a_FromY, int a_ToY,
187  int a_FromZ, int a_ToZ
188  )
189  {
190  for (int z = a_FromZ; z < a_ToZ; z++)
191  {
192  int idxZ = z * m_SizeX * m_SizeY;
193  NOISE_DATATYPE Interp2[2][2];
194  NOISE_DATATYPE FracZ = T::coeff(m_FracZ[z]);
195  for (int x = 0; x < 2; x++)
196  {
197  for (int y = 0; y < 2; y++)
198  {
199  Interp2[x][y] = Lerp((*m_WorkRnds)[x][y][0], (*m_WorkRnds)[x][y][1], FracZ);
200  }
201  }
202  for (int y = a_FromY; y < a_ToY; y++)
203  {
204  NOISE_DATATYPE Interp[2];
205  NOISE_DATATYPE FracY = T::coeff(m_FracY[y]);
206  Interp[0] = Lerp(Interp2[0][0], Interp2[0][1], FracY);
207  Interp[1] = Lerp(Interp2[1][0], Interp2[1][1], FracY);
208  int idx = idxZ + y * m_SizeX + a_FromX;
209  for (int x = a_FromX; x < a_ToX; x++)
210  {
211  m_Array[idx++] = Lerp(Interp[0], Interp[1], T::coeff(m_FracX[x]));
212  } // for x
213  } // for y
214  } // for z
215  }
216 
217 
219  void InitWorkRnds(int a_FloorX, int a_FloorY, int a_FloorZ)
220  {
221  m_CurFloorX = a_FloorX;
222  m_CurFloorY = a_FloorY;
223  m_CurFloorZ = a_FloorZ;
224  (*m_WorkRnds)[0][0][0] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX, m_CurFloorY, m_CurFloorZ));
225  (*m_WorkRnds)[0][0][1] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX, m_CurFloorY, m_CurFloorZ + 1));
226  (*m_WorkRnds)[0][1][0] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX, m_CurFloorY + 1, m_CurFloorZ));
227  (*m_WorkRnds)[0][1][1] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX, m_CurFloorY + 1, m_CurFloorZ + 1));
228  (*m_WorkRnds)[1][0][0] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX + 1, m_CurFloorY, m_CurFloorZ));
229  (*m_WorkRnds)[1][0][1] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX + 1, m_CurFloorY, m_CurFloorZ + 1));
230  (*m_WorkRnds)[1][1][0] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX + 1, m_CurFloorY + 1, m_CurFloorZ));
231  (*m_WorkRnds)[1][1][1] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(m_CurFloorX + 1, m_CurFloorY + 1, m_CurFloorZ + 1));
232  }
233 
234 
236  void Move(int a_NewFloorX, int a_NewFloorY, int a_NewFloorZ)
237  {
238  // Swap the doublebuffer:
239  int OldFloorX = m_CurFloorX;
240  int OldFloorY = m_CurFloorY;
241  int OldFloorZ = m_CurFloorZ;
242  Workspace * OldWorkRnds = m_WorkRnds;
244 
245  // Reuse as much of the old workspace as possible:
246  // TODO: Try out if simply calculating all 8 elements each time is faster than this monster loop
247  int DiffX = OldFloorX - a_NewFloorX;
248  int DiffY = OldFloorY - a_NewFloorY;
249  int DiffZ = OldFloorZ - a_NewFloorZ;
250  for (int x = 0; x < 2; x++)
251  {
252  int cx = a_NewFloorX + x;
253  int OldX = x - DiffX; // Where would this X be in the old grid?
254  for (int y = 0; y < 2; y++)
255  {
256  int cy = a_NewFloorY + y;
257  int OldY = y - DiffY; // Where would this Y be in the old grid?
258  for (int z = 0; z < 2; z++)
259  {
260  int cz = a_NewFloorZ + z;
261  int OldZ = z - DiffZ;
262  if ((OldX >= 0) && (OldX < 2) && (OldY >= 0) && (OldY < 2) && (OldZ >= 0) && (OldZ < 2))
263  {
264  (*m_WorkRnds)[x][y][z] = (*OldWorkRnds)[OldX][OldY][OldZ];
265  }
266  else
267  {
268  (*m_WorkRnds)[x][y][z] = static_cast<NOISE_DATATYPE>(m_Noise.IntNoise3D(cx, cy, cz));
269  }
270  } // for z
271  } // for y
272  } // for x
273  m_CurFloorX = a_NewFloorX;
274  m_CurFloorY = a_NewFloorY;
275  m_CurFloorZ = a_NewFloorZ;
276  }
277 
278 protected:
279  typedef NOISE_DATATYPE Workspace[2][2][2];
280 
282  const cNoise & m_Noise;
283 
286 
289 
292 
295 
298 
301 
306 } ;
307 
308 
309 
310 
311 
313 // cInterpolNoise:
314 
315 template <typename T>
317 {
319  static const int MAX_SIZE = 256;
320 
321 public:
322  cInterpolNoise(int a_Seed):
323  m_Noise(a_Seed)
324  {
325  }
326 
327 
329  void SetSeed(int a_Seed)
330  {
331  m_Noise.SetSeed(a_Seed);
332  }
333 
334 
337  NOISE_DATATYPE * a_Array,
338  int a_SizeX, int a_SizeY,
339  NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX,
340  NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY
341  ) const
342  {
343  ASSERT(a_SizeX > 0);
344  ASSERT(a_SizeY > 0);
345  ASSERT(a_SizeX < MAX_SIZE);
346  ASSERT(a_SizeY < MAX_SIZE);
347  ASSERT(a_StartX < a_EndX);
348  ASSERT(a_StartY < a_EndY);
349 
350  // Calculate the integral and fractional parts of each coord:
351  int FloorX[MAX_SIZE];
352  int FloorY[MAX_SIZE];
353  NOISE_DATATYPE FracX[MAX_SIZE];
354  NOISE_DATATYPE FracY[MAX_SIZE];
355  int SameX[MAX_SIZE];
356  int SameY[MAX_SIZE];
357  int NumSameX, NumSameY;
358  CalcFloorFrac(a_SizeX, a_StartX, a_EndX, FloorX, FracX, SameX, NumSameX);
359  CalcFloorFrac(a_SizeY, a_StartY, a_EndY, FloorY, FracY, SameY, NumSameY);
360 
361  cInterpolCell2D<T> Cell(m_Noise, a_Array, a_SizeX, a_SizeY, FracX, FracY);
362 
363  Cell.InitWorkRnds(FloorX[0], FloorY[0]);
364 
365  // Calculate query values using Cell:
366  int FromY = 0;
367  for (int y = 0; y < NumSameY; y++)
368  {
369  int ToY = FromY + SameY[y];
370  int FromX = 0;
371  int CurFloorY = FloorY[FromY];
372  for (int x = 0; x < NumSameX; x++)
373  {
374  int ToX = FromX + SameX[x];
375  Cell.Generate(FromX, ToX, FromY, ToY);
376  Cell.Move(FloorX[ToX], CurFloorY);
377  FromX = ToX;
378  } // for x
379  Cell.Move(FloorX[0], FloorY[ToY]);
380  FromY = ToY;
381  } // for y
382  }
383 
384 
387  NOISE_DATATYPE * a_Array,
388  int a_SizeX, int a_SizeY, int a_SizeZ,
389  NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX,
390  NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY,
391  NOISE_DATATYPE a_StartZ, NOISE_DATATYPE a_EndZ
392  ) const
393  {
394  // Check params:
395  ASSERT(a_SizeX > 1);
396  ASSERT(a_SizeY > 1);
397 
398  ASSERT(a_SizeX < MAX_SIZE);
399  ASSERT(a_SizeY < MAX_SIZE);
400  ASSERT(a_SizeZ < MAX_SIZE);
401  ASSERT(a_StartX < a_EndX);
402  ASSERT(a_StartY < a_EndY);
403  ASSERT(a_StartZ < a_EndZ);
404 
405  // Calculate the integral and fractional parts of each coord:
406  int FloorX[MAX_SIZE];
407  int FloorY[MAX_SIZE];
408  int FloorZ[MAX_SIZE];
409  NOISE_DATATYPE FracX[MAX_SIZE];
410  NOISE_DATATYPE FracY[MAX_SIZE];
411  NOISE_DATATYPE FracZ[MAX_SIZE];
412  int SameX[MAX_SIZE];
413  int SameY[MAX_SIZE];
414  int SameZ[MAX_SIZE];
415  int NumSameX, NumSameY, NumSameZ;
416  CalcFloorFrac(a_SizeX, a_StartX, a_EndX, FloorX, FracX, SameX, NumSameX);
417  CalcFloorFrac(a_SizeY, a_StartY, a_EndY, FloorY, FracY, SameY, NumSameY);
418  CalcFloorFrac(a_SizeZ, a_StartZ, a_EndZ, FloorZ, FracZ, SameZ, NumSameZ);
419 
420  cInterpolCell3D<T> Cell(
421  m_Noise, a_Array,
422  a_SizeX, a_SizeY, a_SizeZ,
423  FracX, FracY, FracZ
424  );
425 
426  Cell.InitWorkRnds(FloorX[0], FloorY[0], FloorZ[0]);
427 
428  // Calculate query values using Cell:
429  int FromZ = 0;
430  for (int z = 0; z < NumSameZ; z++)
431  {
432  int ToZ = FromZ + SameZ[z];
433  int CurFloorZ = FloorZ[FromZ];
434  int FromY = 0;
435  for (int y = 0; y < NumSameY; y++)
436  {
437  int ToY = FromY + SameY[y];
438  int CurFloorY = FloorY[FromY];
439  int FromX = 0;
440  for (int x = 0; x < NumSameX; x++)
441  {
442  int ToX = FromX + SameX[x];
443  Cell.Generate(FromX, ToX, FromY, ToY, FromZ, ToZ);
444  Cell.Move(FloorX[ToX], CurFloorY, CurFloorZ);
445  FromX = ToX;
446  }
447  Cell.Move(FloorX[0], FloorY[ToY], CurFloorZ);
448  FromY = ToY;
449  } // for y
450  Cell.Move(FloorX[0], FloorY[0], FloorZ[ToZ]);
451  FromZ = ToZ;
452  } // for z
453  }
454 
455 protected:
456 
459 
460 
467  int a_Size,
468  NOISE_DATATYPE a_Start, NOISE_DATATYPE a_End,
469  int * a_Floor, NOISE_DATATYPE * a_Frac,
470  int * a_Same, int & a_NumSame
471  ) const
472  {
473  ASSERT(a_Size > 0);
474 
475  // Calculate the floor and frac values:
476  NOISE_DATATYPE val = a_Start;
477  NOISE_DATATYPE dif = (a_End - a_Start) / (a_Size - 1);
478  for (int i = 0; i < a_Size; i++)
479  {
480  a_Floor[i] = FAST_FLOOR(val);
481  a_Frac[i] = val - a_Floor[i];
482  val += dif;
483  }
484 
485  // Mark up the same floor values into a_Same / a_NumSame:
486  int CurFloor = a_Floor[0];
487  int LastSame = 0;
488  a_NumSame = 0;
489  for (int i = 1; i < a_Size; i++)
490  {
491  if (a_Floor[i] != CurFloor)
492  {
493  a_Same[a_NumSame] = i - LastSame;
494  LastSame = i;
495  a_NumSame += 1;
496  CurFloor = a_Floor[i];
497  }
498  } // for i - a_Floor[]
499  if (LastSame < a_Size)
500  {
501  a_Same[a_NumSame] = a_Size - LastSame;
502  a_NumSame += 1;
503  }
504  }
505 };
506 
507 
508 
509 
510 
514 {
516  {
517  return a_Val * a_Val * a_Val * (a_Val * (a_Val * 6 - 15) + 10);
518  }
519 };
520 
522 
523 
524 
#define ASSERT(x)
Definition: Globals.h:276
cInterpolNoise< Interp5Deg > cInterp5DegNoise
#define FAST_FLOOR(x)
Definition: InterpolNoise.h:14
NOISE_DATATYPE Lerp(NOISE_DATATYPE a_Val1, NOISE_DATATYPE a_Val2, NOISE_DATATYPE a_Ratio)
Linearly interpolates between two values.
Definition: Noise.h:324
float NOISE_DATATYPE
The datatype used by all the noise generators.
Definition: Noise.h:9
NOISE_DATATYPE Workspace[2][2]
Workspace m_Workspace1
Buffer 1 for workspace doublebuffering, used in Move()
const NOISE_DATATYPE * m_FracX
Arrays holding the fractional values of the coords in each direction.
int m_CurFloorX
Coords of the currently calculated m_WorkRnds[].
void Generate(int a_FromX, int a_ToX, int a_FromY, int a_ToY)
Generates part of the output noise array using the current m_WorkRnds[] values.
Definition: InterpolNoise.h:48
Workspace * m_WorkRnds
The current random values; points to either m_Workspace1 or m_Workspace2 (doublebuffering)
int m_SizeX
Dimensions of the output array.
Workspace m_Workspace2
Buffer 2 for workspace doublebuffering, used in Move()
void Move(int a_NewFloorX, int a_NewFloorY)
Updates m_WorkRnds[] for the new integral coords.
Definition: InterpolNoise.h:81
cInterpolCell2D(const cNoise &a_Noise, NOISE_DATATYPE *a_Array, int a_SizeX, int a_SizeY, const NOISE_DATATYPE *a_FracX, const NOISE_DATATYPE *a_FracY)
Definition: InterpolNoise.h:27
const cNoise & m_Noise
The noise used for generating the values at integral coords.
NOISE_DATATYPE * m_Array
The output array to generate into.
const NOISE_DATATYPE * m_FracY
void InitWorkRnds(int a_FloorX, int a_FloorY)
Initializes m_WorkRnds[] with the specified values of the noise at the specified integral coords.
Definition: InterpolNoise.h:69
Holds a cache of the last calculated integral noise values and interpolates between them en masse.
NOISE_DATATYPE Workspace[2][2][2]
const NOISE_DATATYPE * m_FracZ
Workspace * m_WorkRnds
The current random values; points to either m_Workspace1 or m_Workspace2 (doublebuffering)
int m_SizeX
Dimensions of the output array.
void Generate(int a_FromX, int a_ToX, int a_FromY, int a_ToY, int a_FromZ, int a_ToZ)
Generates part of the output array using current m_WorkRnds[].
int m_CurFloorX
The integral coords of the currently calculated WorkRnds[].
void InitWorkRnds(int a_FloorX, int a_FloorY, int a_FloorZ)
Initializes m_WorkRnds[] with the specified Floor values.
Workspace m_Workspace2
Buffer 2 for workspace doublebuffering, used in Move()
cInterpolCell3D(const cNoise &a_Noise, NOISE_DATATYPE *a_Array, int a_SizeX, int a_SizeY, int a_SizeZ, const NOISE_DATATYPE *a_FracX, const NOISE_DATATYPE *a_FracY, const NOISE_DATATYPE *a_FracZ)
const NOISE_DATATYPE * m_FracY
void Move(int a_NewFloorX, int a_NewFloorY, int a_NewFloorZ)
Updates m_WorkRnds[] for the new Floor values.
Workspace m_Workspace1
Buffer 1 for workspace doublebuffering, used in Move()
NOISE_DATATYPE * m_Array
The output array where the noise is calculated.
const cNoise & m_Noise
The noise used for generating the values at integral coords.
const NOISE_DATATYPE * m_FracX
Arrays holding the fractional values of the coords in each direction.
void Generate3D(NOISE_DATATYPE *a_Array, int a_SizeX, int a_SizeY, int a_SizeZ, NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, NOISE_DATATYPE a_StartZ, NOISE_DATATYPE a_EndZ) const
Fills a 3D array with the values of the noise.
cNoise m_Noise
The noise used for the underlying value generation.
void SetSeed(int a_Seed)
Sets a new seed for the generators.
cInterpolNoise(int a_Seed)
static const int MAX_SIZE
Maximum size, for each direction, of the generated array.
void CalcFloorFrac(int a_Size, NOISE_DATATYPE a_Start, NOISE_DATATYPE a_End, int *a_Floor, NOISE_DATATYPE *a_Frac, int *a_Same, int &a_NumSame) const
Calculates the integral and fractional parts along one axis.
void Generate2D(NOISE_DATATYPE *a_Array, int a_SizeX, int a_SizeY, NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY) const
Fills a 2D array with the values of the noise.
A fifth-degree curve for interpolating.
static NOISE_DATATYPE coeff(NOISE_DATATYPE a_Val)
Definition: Noise.h:20
NOISE_DATATYPE IntNoise2D(int a_X, int a_Y) const
Definition: Noise.h:198
void SetSeed(int a_Seed)
Definition: Noise.h:52
NOISE_DATATYPE IntNoise3D(int a_X, int a_Y, int a_Z) const
Definition: Noise.h:210