Panda3D
 All Classes Functions Variables Enumerations
perlinNoise2.cxx
1 // Filename: perlinNoise2.cxx
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 #include "perlinNoise2.h"
16 #include "cmath.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: PerlinNoise2::noise
20 // Access: Published
21 // Description: Returns the noise function of the three inputs.
22 ////////////////////////////////////////////////////////////////////
23 double PerlinNoise2::
24 noise(const LVecBase2d &value) const {
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  int A = _index[X] + Y;
48  int B = _index[X + 1] + Y;
49 
50  // and add blended results from 4 corners of square.
51  double result =
52  lerp(v, lerp(u, grad(_index[A], x, y),
53  grad(_index[B], x - 1, y)),
54  lerp(u, grad(_index[A + 1], x, y - 1),
55  grad(_index[B + 1], x - 1, y - 1)));
56 
57  return result;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: PerlinNoise2::init_unscaled_xform
62 // Access: Private
63 // Description: Come up with a random rotation to apply to the input
64 // coordinates. This will reduce the problem of the
65 // singularities on the axes, by sending the axes in
66 // some crazy direction.
67 ////////////////////////////////////////////////////////////////////
68 void PerlinNoise2::
69 init_unscaled_xform() {
70  double rot = _randomizer.random_real(360.0f);
71  _unscaled_xform = LMatrix3d::rotate_mat(rot);
72 
73  // And come up with a random translation too, just so the
74  // singularity at (0, 0) is also unpredicatable.
75  _unscaled_xform.set_row(2, LVecBase2d(_randomizer.random_real_unit(),
76  _randomizer.random_real_unit()));
77 }
static LMatrix3d rotate_mat(double angle)
Returns a matrix that rotates by the given angle in degrees counterclockwise.
Definition: lmatrix.h:8346
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:1241
double noise(double x, double y) const
Returns the noise function of the three inputs.
Definition: perlinNoise2.I:122
void set_row(int row, const LVecBase3d &v)
Replaces the indicated row of the matrix from a three-component vector.
Definition: lmatrix.h:7223
LVecBase2d xform_point(const LVecBase2d &v) const
The matrix transforms a 2-component point (including translation component) and returns the result...
Definition: lmatrix.h:7650