Panda3D
perlinNoise2.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file perlinNoise2.cxx
10  * @author drose
11  * @date 2005-10-05
12  */
13 
14 #include "perlinNoise2.h"
15 #include "cmath.h"
16 
17 /**
18  * Returns the noise function of the three inputs.
19  */
21 noise(const LVecBase2d &value) const {
22  // If this triggers, you passed in 0 for table_size.
23  nassertr(!_index.empty(), make_nan(0.0));
24 
25  // Convert the vector to our local coordinate space.
26  LVecBase2d vec = _input_xform.xform_point(value);
27 
28  double x = vec[0];
29  double y = vec[1];
30 
31  // Find unit square that contains point.
32  double xf = cfloor(x);
33  double yf = cfloor(y);
34 
35  int X = ((int)xf) & _table_size_mask;
36  int Y = ((int)yf) & _table_size_mask;
37 
38  // Find relative x,y of point in square.
39  x -= xf;
40  y -= yf;
41 
42  // Compute fade curves for each of x,y.
43  double u = fade(x);
44  double v = fade(y);
45 
46  // Hash coordinates of the 4 square corners (A, B, A + 1, and B + 1)
47  nassertr(X >= 0 && X + 1 < _index.size(), make_nan(0.0));
48  int A = _index[X] + Y;
49  int B = _index[X + 1] + Y;
50 
51  nassertr(A >= 0 && A + 1 < _index.size(), make_nan(0.0));
52  nassertr(B >= 0 && B + 1 < _index.size(), make_nan(0.0));
53 
54  // and add blended results from 4 corners of square.
55  double result =
56  lerp(v, lerp(u, grad(_index[A], x, y),
57  grad(_index[B], x - 1, y)),
58  lerp(u, grad(_index[A + 1], x, y - 1),
59  grad(_index[B + 1], x - 1, y - 1)));
60 
61  return result;
62 }
63 
64 /**
65  * Come up with a random rotation to apply to the input coordinates. This
66  * will reduce the problem of the singularities on the axes, by sending the
67  * axes in some crazy direction.
68  */
69 void PerlinNoise2::
70 init_unscaled_xform() {
71  double rot = _randomizer.random_real(360.0f);
72  _unscaled_xform = LMatrix3d::rotate_mat(rot);
73 
74  // And come up with a random translation too, just so the singularity at (0,
75  // 0) is also unpredicatable.
76  _unscaled_xform.set_row(2, LVecBase2d(_randomizer.random_real_unit(),
77  _randomizer.random_real_unit()));
78 }
double noise(double x, double y) const
Returns the noise function of the three inputs.
Definition: perlinNoise2.I:100
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.