H3D API  2.4.1
SimplexNoise.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
28 // Adapted from SimplexNoise1234, Copyright 2003-2011, Stefan Gustavson
29 // which was released as public domain software without any restrictions
30 //
32 
33 #ifndef __SIMPLEXNOISE1234_H__
34 #define __SIMPLEXNOISE1234_H__
35 
36 namespace H3D {
37 
38  class SimplexNoise1234 {
39  // This is a clean, fast, modern and free Perlin Simplex noise class in C++.
40  // Being a stand-alone class with no external dependencies, it is
41  // highly reusable without source code modifications.
42 
43  public:
44  SimplexNoise1234();
45  virtual ~SimplexNoise1234() {};
46 
50  static float noise( float x );
51  static float noise( float x, float y );
52  static float noise( float x, float y, float z );
53  static float noise( float x, float y, float z, float w );
54 
57  // comment from SU: not implemented yet... :(
58  static float pnoise( float x, int px );
59  static float pnoise( float x, float y, int px, int py );
60  static float pnoise( float x, float y, float z, int px, int py, int pz );
61  static float pnoise( float x, float y, float z, float w,
62  int px, int py, int pz, int pw );
63 
64  private:
65  static unsigned char perm[];
66  static unsigned char simplex[][4];
67  static float grad( int hash, float x );
68  static float grad( int hash, float x, float y );
69  static float grad( int hash, float x, float y , float z );
70  static float grad( int hash, float x, float y, float z, float t );
71 
72 
73  /*
74  * Permutation table. This is just a random jumble of all numbers 0-255,
75  * repeated twice to avoid wrapping the index at 255 for each lookup.
76  * This needs to be exactly the same for all instances on all platforms,
77  * so it's easiest to just keep it as static explicit data.
78  * This also removes the need for any initialisation of this class.
79  *
80  * Note that making this an int[] instead of a char[] might make the
81  * code run faster on platforms with a high penalty for unaligned single
82  * byte addressing. Intel x86 is generally single-byte-friendly, but
83  * some other CPUs are faster with 4-aligned reads.
84  * However, a char[] is smaller, which avoids cache trashing, and that
85  * is probably the most important aspect on most architectures.
86  * This array is accessed a *lot* by the noise functions.
87  * A vector-valued noise over 3D accesses it 96 times, and a
88  * float-valued 4D noise 64 times. We want this to fit in the cache!
89  */
90 
91 
92 
93  };
94 }
95 
96 #endif
97 
H3DDouble x
H3D API namespace.
Definition: Anchor.h:38