Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
return a;
}
+// -1->0, 0->1, 1->0
+double contour(double v)
+{
+ v = fabs(v);
+ if(v >= 1.0)
+ return 0.0;
+ return (1.0-v);
+}
+
+double noise3d_param(const NoiseParams ¶m, double x, double y, double z)
+{
+ double s = param.pos_scale;
+ x /= s;
+ y /= s;
+ z /= s;
+
+ if(param.type == NOISE_CONSTANT_ONE)
+ {
+ return 1.0;
+ }
+ else if(param.type == NOISE_PERLIN)
+ {
+ return param.noise_scale*noise3d_perlin(x,y,z, param.seed,
+ param.octaves,
+ param.persistence);
+ }
+ else if(param.type == NOISE_PERLIN_ABS)
+ {
+ return param.noise_scale*noise3d_perlin_abs(x,y,z, param.seed,
+ param.octaves,
+ param.persistence);
+ }
+ else if(param.type == NOISE_PERLIN_CONTOUR)
+ {
+ return contour(param.noise_scale*noise3d_perlin(x,y,z,
+ param.seed, param.octaves,
+ param.persistence));
+ }
+ else if(param.type == NOISE_PERLIN_CONTOUR_FLIP_YZ)
+ {
+ return contour(param.noise_scale*noise3d_perlin(x,z,y,
+ param.seed, param.octaves,
+ param.persistence));
+ }
+ else assert(0);
+}
+
/*
NoiseBuffer
*/
m_size_z = 0;
}
-void NoiseBuffer::create(int seed, int octaves, double persistence,
- double pos_scale,
+void NoiseBuffer::create(const NoiseParams ¶m,
double first_x, double first_y, double first_z,
double last_x, double last_y, double last_z,
double samplelength_x, double samplelength_y, double samplelength_z)
m_size_y = (last_y - m_start_y)/samplelength_y + 2;
m_size_z = (last_z - m_start_z)/samplelength_z + 2;
- /*dstream<<"m_size_x="<<m_size_x<<", m_size_y="<<m_size_y
- <<", m_size_z="<<m_size_z<<std::endl;*/
-
m_data = new double[m_size_x*m_size_y*m_size_z];
for(int x=0; x<m_size_x; x++)
for(int y=0; y<m_size_y; y++)
for(int z=0; z<m_size_z; z++)
{
- double xd = (m_start_x + (double)x*m_samplelength_x)/pos_scale;
- double yd = (m_start_y + (double)y*m_samplelength_y)/pos_scale;
- double zd = (m_start_z + (double)z*m_samplelength_z)/pos_scale;
- intSet(x,y,z, noise3d_perlin(xd,yd,zd,seed,octaves,persistence));
+ double xd = (m_start_x + (double)x*m_samplelength_x);
+ double yd = (m_start_y + (double)y*m_samplelength_y);
+ double zd = (m_start_z + (double)z*m_samplelength_z);
+ double a = noise3d_param(param, xd,yd,zd);
+ intSet(x,y,z, a);
+ }
+}
+
+void NoiseBuffer::multiply(const NoiseParams ¶m)
+{
+ assert(m_data != NULL);
+
+ for(int x=0; x<m_size_x; x++)
+ for(int y=0; y<m_size_y; y++)
+ for(int z=0; z<m_size_z; z++)
+ {
+ double xd = (m_start_x + (double)x*m_samplelength_x);
+ double yd = (m_start_y + (double)y*m_samplelength_y);
+ double zd = (m_start_z + (double)z*m_samplelength_z);
+ double a = noise3d_param(param, xd,yd,zd);
+ intMultiply(x,y,z, a);
}
}
+// Deprecated
+void NoiseBuffer::create(int seed, int octaves, double persistence,
+ bool abs,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z)
+{
+ NoiseParams param;
+ param.type = abs ? NOISE_PERLIN_ABS : NOISE_PERLIN;
+ param.seed = seed;
+ param.octaves = octaves;
+ param.persistence = persistence;
+
+ create(param, first_x, first_y, first_z,
+ last_x, last_y, last_z,
+ samplelength_x, samplelength_y, samplelength_z);
+}
+
void NoiseBuffer::intSet(int x, int y, int z, double d)
{
int i = m_size_x*m_size_y*z + m_size_x*y + x;
m_data[i] = d;
}
+void NoiseBuffer::intMultiply(int x, int y, int z, double d)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ m_data[i] = m_data[i] * d;
+}
+
double NoiseBuffer::intGet(int x, int y, int z)
{
int i = m_size_x*m_size_y*z + m_size_x*y + x;
return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
}
+/*bool NoiseBuffer::contains(double x, double y, double z)
+{
+ x -= m_start_x;
+ y -= m_start_y;
+ z -= m_start_z;
+ x /= m_samplelength_x;
+ y /= m_samplelength_y;
+ z /= m_samplelength_z;
+ if(x <= 0.0 || x >= m_size_x)
+}*/
+