Panda3D
 All Classes Functions Variables Enumerations
perlinNoise.cxx
00001 // Filename: perlinNoise.cxx
00002 // Created by:  drose (05Oct05)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "perlinNoise.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: PerlinNoise::Constructor
00019 //       Access: Protected
00020 //  Description: Randomizes the tables to make a unique noise
00021 //               function.
00022 //
00023 //               If seed is nonzero, it is used to define the tables;
00024 //               if it is zero a random seed is generated.
00025 ////////////////////////////////////////////////////////////////////
00026 PerlinNoise::
00027 PerlinNoise(int table_size, unsigned long seed) :
00028   _table_size(table_size),
00029   _table_size_mask(table_size - 1),
00030   _randomizer(seed)
00031 {
00032   // It is necessary for _table_size to be a power of 2.
00033 #ifndef NDEBUG
00034   if (_table_size != 0) {
00035     bool table_size_power_2 = ((_table_size ^ _table_size_mask) == (_table_size + _table_size_mask));
00036     nassertd(table_size_power_2) {
00037       _table_size = 0;
00038       _table_size_mask = 0;
00039     }
00040   }
00041 #endif  // NDEBUG
00042 
00043   // The _index table is just a randomly shuffled index
00044   // table.
00045   _index.reserve(_table_size * 2);
00046   int i;
00047   for (i = 0; i < _table_size; ++i) {
00048     _index.push_back(i);
00049   }
00050   for (i = 0; i < _table_size; ++i) {
00051     int j = _randomizer.random_int(_table_size);
00052     nassertv(j >= 0 && j < _table_size);
00053     int t = _index[i];
00054     _index[i] = _index[j];
00055     _index[j] = t;
00056   }
00057 
00058   // We double up _index so we don't need to perform modulo
00059   // arithmetic.
00060   for (i = 0; i < _table_size; ++i) {
00061     _index.push_back(_index[i]);
00062   }
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: PerlinNoise::Copy Constructor
00067 //       Access: Protected
00068 //  Description: Makes an exact copy of the existing PerlinNoise
00069 //               object, including its random seed.
00070 ////////////////////////////////////////////////////////////////////
00071 PerlinNoise::
00072 PerlinNoise(const PerlinNoise &copy) :
00073   _table_size(copy._table_size),
00074   _table_size_mask(copy._table_size_mask),
00075   _randomizer(copy._randomizer),
00076   _index(copy._index)
00077 {
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: PerlinNoise::Copy Assignment Operator
00082 //       Access: Protected
00083 //  Description: Makes an exact copy of the existing PerlinNoise
00084 //               object, including its random seed.
00085 ////////////////////////////////////////////////////////////////////
00086 void PerlinNoise::
00087 operator = (const PerlinNoise &copy) {
00088   _table_size = copy._table_size;
00089   _table_size_mask = copy._table_size_mask;
00090   _randomizer = copy._randomizer;
00091   _index = copy._index;
00092 }
 All Classes Functions Variables Enumerations