Panda3D
perlinNoise3.I
1 // Filename: perlinNoise3.I
2 // Created by: drose (05Oct05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: PerlinNoise3::Default Constructor
18 // Access: Published
19 // Description: Randomizes the tables to make a unique noise
20 // function. Uses a default scale (noise frequency),
21 // table size, and seed.
22 ////////////////////////////////////////////////////////////////////
23 INLINE PerlinNoise3::
25  PerlinNoise(256, 0)
26 {
27  init_unscaled_xform();
28  _input_xform = _unscaled_xform;
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: PerlinNoise3::Constructor
33 // Access: Published
34 // Description: Randomizes the tables to make a unique noise
35 // function.
36 //
37 // If seed is nonzero, it is used to define the tables;
38 // if it is zero a random seed is generated.
39 ////////////////////////////////////////////////////////////////////
40 INLINE PerlinNoise3::
41 PerlinNoise3(double sx, double sy, double sz,
42  int table_size, unsigned long seed) :
43  PerlinNoise(table_size, seed)
44 {
45  init_unscaled_xform();
46  set_scale(sx, sy, sz);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: PerlinNoise3::Copy Constructor
51 // Access: Published
52 // Description: Makes an exact copy of the existing PerlinNoise
53 // object, including its random seed.
54 ////////////////////////////////////////////////////////////////////
55 INLINE PerlinNoise3::
56 PerlinNoise3(const PerlinNoise3 &copy) :
57  PerlinNoise(copy),
58  _unscaled_xform(copy._unscaled_xform),
59  _input_xform(copy._input_xform)
60 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: PerlinNoise3::Copy Assignment Operator
65 // Access: Published
66 // Description: Makes an exact copy of the existing PerlinNoise
67 // object, including its random seed.
68 ////////////////////////////////////////////////////////////////////
69 INLINE void PerlinNoise3::
70 operator = (const PerlinNoise3 &copy) {
71  PerlinNoise::operator = (copy);
72  _unscaled_xform = copy._unscaled_xform;
73  _input_xform = copy._input_xform;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: PerlinNoise3::set_scale
78 // Access: Published
79 // Description: Changes the scale (frequency) of the noise.
80 ////////////////////////////////////////////////////////////////////
81 INLINE void PerlinNoise3::
82 set_scale(double scale) {
83  set_scale(scale, scale, scale);
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: PerlinNoise3::set_scale
88 // Access: Published
89 // Description: Changes the scale (frequency) of the noise.
90 ////////////////////////////////////////////////////////////////////
91 INLINE void PerlinNoise3::
92 set_scale(double x, double y, double z) {
93  set_scale(LVecBase3d(x, y, z));
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: PerlinNoise3::set_scale
98 // Access: Published
99 // Description: Changes the scale (frequency) of the noise.
100 ////////////////////////////////////////////////////////////////////
101 INLINE void PerlinNoise3::
102 set_scale(const LVecBase3f &value) {
103  set_scale(value[0], value[1], value[2]);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: PerlinNoise3::set_scale
108 // Access: Published
109 // Description: Changes the scale (frequency) of the noise.
110 ////////////////////////////////////////////////////////////////////
111 INLINE void PerlinNoise3::
112 set_scale(const LVecBase3d &value) {
113  _input_xform = LMatrix4d::scale_mat(1.0f / value[0], 1.0f / value[1], 1.0f / value[2]) * _unscaled_xform;
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: PerlinNoise3::noise
118 // Access: Published
119 // Description: Returns the noise function of the three inputs.
120 ////////////////////////////////////////////////////////////////////
121 INLINE double PerlinNoise3::
122 noise(double x, double y, double z) const {
123  return noise(LVecBase3d(x, y, z));
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: PerlinNoise3::noise
128 // Access: Published
129 // Description: Returns the noise function of the three inputs.
130 ////////////////////////////////////////////////////////////////////
131 INLINE float PerlinNoise3::
132 noise(const LVecBase3f &value) const {
133  return (float)noise(value[0], value[1], value[2]);
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: PerlinNoise3::operator ()
138 // Access: Published
139 // Description: Returns the noise function of the three inputs.
140 ////////////////////////////////////////////////////////////////////
141 INLINE double PerlinNoise3::
142 operator ()(double x, double y, double z) const {
143  return noise(x, y, z);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: PerlinNoise3::noise
148 // Access: Published
149 // Description: Returns the noise function of the three inputs.
150 ////////////////////////////////////////////////////////////////////
151 INLINE float PerlinNoise3::
152 operator ()(const LVecBase3f &value) const {
153  return noise(value);
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: PerlinNoise3::noise
158 // Access: Published
159 // Description: Returns the noise function of the three inputs.
160 ////////////////////////////////////////////////////////////////////
161 INLINE double PerlinNoise3::
162 operator ()(const LVecBase3d &value) const {
163  return noise(value);
164 }
165 
166 ////////////////////////////////////////////////////////////////////
167 // Function: PerlinNoise3::grad
168 // Access: Private, Static
169 // Description: Returns the dot product of a random gradient vector
170 // (determined by the hash code) with the indicated
171 // offset vector.
172 ////////////////////////////////////////////////////////////////////
173 INLINE double PerlinNoise3::
174 grad(int hash, double x, double y, double z) {
175  // Convert low 4 bits of hash code into 12 gradient directions.
176  /*
177  This is Perlin's reference code, but the switch statement below is
178  slightly faster and produces exactly the same results.
179  int h = hash & 15;
180  double u = (h < 8) ? x : y;
181  double v = (h < 4) ? y : ((h == 12 || h == 14) ? x : z);
182  return ((h & 1) ? -u : u) + ((h & 2) ? -v : v);
183  */
184 
185  switch (hash & 15) {
186  case 0: return x + y;
187  case 1: return -x + y;
188  case 2: return x - y;
189  case 3: return -x - y;
190 
191  case 4: return x + z;
192  case 5: return -x + z;
193  case 6: return x - z;
194  case 7: return -x - z;
195 
196  case 8: return y + z;
197  case 9: return -y + z;
198  case 10: return y - z;
199  case 11: return -y - z;
200 
201  case 12: return x + y;
202  case 13: return -y + z;
203  case 14: return -x + y;
204  case 15: return -y - z;
205  }
206 
207  nassertr(false, 0);
208  return 0;
209 }
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:31
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
void operator=(const PerlinNoise3 &copy)
Makes an exact copy of the existing PerlinNoise object, including its random seed.
Definition: perlinNoise3.I:70
static LMatrix4d scale_mat(const LVecBase3d &scale)
Returns a matrix that applies the indicated scale in each of the three axes.
Definition: lmatrix.h:6721
PerlinNoise3()
Randomizes the tables to make a unique noise function.
Definition: perlinNoise3.I:24
This class provides an implementation of Perlin noise for 3 variables.
Definition: perlinNoise3.h:28
void set_scale(double scale)
Changes the scale (frequency) of the noise.
Definition: perlinNoise3.I:82
double operator()(double x, double y, double z) const
Returns the noise function of the three inputs.
Definition: perlinNoise3.I:142
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1471
double noise(double x, double y, double z) const
Returns the noise function of the three inputs.
Definition: perlinNoise3.I:122