Improved interface

*MappedNoiseBase provides a gain and offset control on the noise value
*AbstractXDNoise provides a way to contain different noises in a std
container and access the main "GetValue" member function
*Applied minor optimisation to Perlin4D (previously forgotten)
This commit is contained in:
Remi Beges
2012-07-24 14:21:56 +02:00
parent 10a436b481
commit 354d7ed002
22 changed files with 303 additions and 1313 deletions

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACT2DNOISE_HPP
#define NAZARA_ABSTRACT2DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
template <typename T> class NAZARA_API NzAbstract2DNoise : public NzMappedNoiseBase<T>
{
public:
virtual T GetMappedValue(T x, T y);
virtual T GetValue(T x, T y, T resolution) = 0;
virtual T GetValue(T x, T y);
};
#include <Nazara/Noise/Abstract2DNoise.inl>
#endif // NAZARA_ABSTRACT2DNOISE_HPP

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Basic.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Debug.hpp>
template <typename T>
T NzAbstract2DNoise<T>::GetMappedValue(T x, T y)
{
return GetValue(x,y,this->m_resolution) * this->m_gain + this->m_offset;
}
template <typename T>
T NzAbstract2DNoise<T>::GetValue(T x, T y)
{
return GetValue(x,y,this->m_resolution);
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACT3DNOISE_HPP
#define NAZARA_ABSTRACT3DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
template <typename T> class NAZARA_API NzAbstract3DNoise : public NzMappedNoiseBase
{
public:
virtual T GetMappedValue(T x, T y, T z);
virtual T GetValue(T x, T y, T z) = 0;
virtual T GetValue(T x, T y, T z, T resolution);
};
#include <Nazara/Noise/Abstract3DNoise.inl>
#endif // NAZARA_ABSTRACT3DNOISE_HPP

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Basic.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Debug.hpp>
template <typename T>
T NzAbstract3DNoise<T>::GetMappedValue(T x, T y, T z)
{
return GetValue(x,y,z) * m_gain + m_offset;
}
template <typename T>
T NzAbstract3DNoise<T>::GetValue(T x, T y, T z, T resolution)
{
return GetValue(x,y,z,this->m_resolution);
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACT4DNOISE_HPP
#define NAZARA_ABSTRACT4DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
template <typename T> class NAZARA_API NzAbstract4DNoise : public NzMappedNoiseBase
{
public:
virtual T GetMappedValue(T x, T y, T z, T w);
virtual T GetValue(T x, T y, T z, T w) = 0;
virtual T GetValue(T x, T y, T z, T w, T resolution);
};
#include <Nazara/Noise/Abstract4DNoise.inl>
#endif // NAZARA_ABSTRACT4DNOISE_HPP

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Basic.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Debug.hpp>
template <typename T>
T NzAbstract4DNoise<T>::GetMappedValue(T x, T y, T z, T w)
{
return GetValue(x,y,z,w) * m_gain + m_offset;
}
template <typename T>
T NzAbstract3DNoise<T>::GetValue(T x, T y, T z, T resolution)
{
return GetValue(x,y,z,w,this->m_resolution);
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,32 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MAPPEDNOISEBASE_HPP
#define NAZARA_MAPPEDNOISEBASE_HPP
#include <Nazara/Prerequesites.hpp>
template <typename T> class NzMappedNoiseBase
{
public:
NzMappedNoiseBase();
~NzMappedNoiseBase() = default;
T GetGain() const;
T GetOffset() const;
T GetResolution() const;
void SetGain(T gain);
void SetOffset(T offset);
void SetResolution(T resolution);
protected:
T m_gain;
T m_offset;
T m_resolution;
};
#include <Nazara/Noise/MappedNoiseBase.inl>
#endif // NAZARA_MAPPEDNOISEBASE_HPP

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Basic.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <stdexcept>
#include <Nazara/Noise/Debug.hpp>
template <typename T>
NzMappedNoiseBase<T>::NzMappedNoiseBase() : m_gain(1), m_offset(0), m_resolution(30)
{
}
template <typename T>
T NzMappedNoiseBase<T>::GetGain() const
{
return m_gain;
}
template <typename T>
T NzMappedNoiseBase<T>::GetOffset() const
{
return m_offset;
}
template <typename T>
T NzMappedNoiseBase<T>::GetResolution() const
{
return m_resolution;
}
template <typename T>
void NzMappedNoiseBase<T>::SetGain(T gain)
{
m_gain = gain;
}
template <typename T>
void NzMappedNoiseBase<T>::SetOffset(T offset)
{
m_offset = offset;
}
template <typename T>
void NzMappedNoiseBase<T>::SetResolution(T resolution)
{
if (NzNumberEquals(resolution, static_cast<T>(0.0)))
{
NzStringStream ss;
ss << __FILE__ << ':' << __LINE__ << ": Division by zero";
throw std::domain_error(ss.ToString());
}
m_resolution = static_cast<T>(1.0)/resolution;
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
#include <Nazara/Math/Vector2.hpp>
template <typename T> class NAZARA_API NzPerlin2D : public NzNoiseBase
template <typename T> class NAZARA_API NzPerlin2D : public NzAbstract2DNoise<T>, public NzNoiseBase
{
public:
NzPerlin2D();
T GetValue(T x, T y, T res);
T GetValue(T x, T y, T resolution);
~NzPerlin2D() = default;
protected:
private:

View File

@@ -18,10 +18,10 @@ NzPerlin2D<T>::NzPerlin2D()
}
template <typename T>
T NzPerlin2D<T>::GetValue(T x, T y, T res)
T NzPerlin2D<T>::GetValue(T x, T y, T resolution)
{
x /= res;
y /= res;
x *= resolution;
y *= resolution;
x0 = fastfloor(x);
y0 = fastfloor(y);

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
#include <Nazara/Math/Vector3.hpp>
template <typename T> class NAZARA_API NzPerlin3D : public NzNoiseBase
template <typename T> class NAZARA_API NzPerlin3D : public NzAbstract3DNoise<T>, public NzNoiseBase
{
public:
NzPerlin3D();
T GetValue(T x, T y, T z, T res);
T GetValue(T x, T y, T z, T resolution);
~NzPerlin3D() = default;
protected:
private:

View File

@@ -22,11 +22,11 @@ NzPerlin3D<T>::NzPerlin3D()
}
template <typename T>
T NzPerlin3D<T>::GetValue(T x, T y, T z, T res)
T NzPerlin3D<T>::GetValue(T x, T y, T z, T resolution)
{
x /= res;
y /= res;
z /= res;
x *= resolution;
y *= resolution;
z *= resolution;
x0 = fastfloor(x);
y0 = fastfloor(y);

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
#include <Nazara/Math/Vector4.hpp>
template <typename T> class NAZARA_API NzPerlin4D : public NzNoiseBase
template <typename T> class NAZARA_API NzPerlin4D : public NzAbstract4DNoise<T>, public NzNoiseBase
{
public:
NzPerlin4D();
T GetValue(T x, T y, T z, T w, T res);
T GetValue(T x, T y, T z, T w, T resolution);
~NzPerlin4D() = default;
protected:
private:
@@ -26,7 +27,6 @@ template <typename T> class NAZARA_API NzPerlin4D : public NzNoiseBase
T Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14;
T s[4],t[4],u[4],v[4];
T Cx,Cy,Cz,Cw;
T nx,ny,nz,nw;
T tmp;
NzVector4<T> temp;

View File

@@ -27,17 +27,17 @@ NzPerlin4D<T>::NzPerlin4D()
}
template <typename T>
T NzPerlin4D<T>::GetValue(T x, T y, T z, T w, T res)
T NzPerlin4D<T>::GetValue(T x, T y, T z, T w, T resolution)
{
nx = x/res;
ny = y/res;
nz = z/res;
nw = w/res;
x *= resolution;
y *= resolution;
z *= resolution;
w *= resolution;
x0 = fastfloor(nx);
y0 = fastfloor(ny);
z0 = fastfloor(nz);
w0 = fastfloor(nw);
x0 = fastfloor(x);
y0 = fastfloor(y);
z0 = fastfloor(z);
w0 = fastfloor(w);
ii = x0 & 255;
jj = y0 & 255;
@@ -64,10 +64,10 @@ T NzPerlin4D<T>::GetValue(T x, T y, T z, T w, T res)
gi14 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31;
gi15 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31;
temp.x = nx-x0;
temp.y = ny-y0;
temp.z = nz-z0;
temp.w = nw-w0;
temp.x = x-x0;
temp.y = y-y0;
temp.z = z-z0;
temp.w = w-w0;
Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10);
Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10);
@@ -76,55 +76,55 @@ T NzPerlin4D<T>::GetValue(T x, T y, T z, T w, T res)
s[0] = gradient4[gi0][0]*temp.x + gradient4[gi0][1]*temp.y + gradient4[gi0][2]*temp.z + gradient4[gi0][3]*temp.w;
temp.x = nx-(x0+1);
temp.x = x-(x0+1);
t[0] = gradient4[gi1][0]*temp.x + gradient4[gi1][1]*temp.y + gradient4[gi1][2]*temp.z + gradient4[gi1][3]*temp.w;
temp.y = ny-(y0+1);
temp.y = y-(y0+1);
v[0] = gradient4[gi3][0]*temp.x + gradient4[gi3][1]*temp.y + gradient4[gi3][2]*temp.z + gradient4[gi3][3]*temp.w;
temp.x = nx-x0;
temp.x = x-x0;
u[0] = gradient4[gi2][0]*temp.x + gradient4[gi2][1]*temp.y + gradient4[gi2][2]*temp.z + gradient4[gi2][3]*temp.w;
temp.y = ny-y0;
temp.z = nz-(z0+1);
temp.y = y-y0;
temp.z = z-(z0+1);
s[1] = gradient4[gi4][0]*temp.x + gradient4[gi4][1]*temp.y + gradient4[gi4][2]*temp.z + gradient4[gi4][3]*temp.w;
temp.x = nx-(x0+1);
temp.x = x-(x0+1);
t[1] = gradient4[gi5][0]*temp.x + gradient4[gi5][1]*temp.y + gradient4[gi5][2]*temp.z + gradient4[gi5][3]*temp.w;
temp.y = ny-(y0+1);
temp.y = y-(y0+1);
v[1] = gradient4[gi7][0]*temp.x + gradient4[gi7][1]*temp.y + gradient4[gi7][2]*temp.z + gradient4[gi7][3]*temp.w;
temp.x = nx-x0;
temp.x = x-x0;
u[1] = gradient4[gi6][0]*temp.x + gradient4[gi6][1]*temp.y + gradient4[gi6][2]*temp.z + gradient4[gi6][3]*temp.w;
temp.y = ny-y0;
temp.z = nz-z0;
temp.w = nw-(w0+1);
temp.y = y-y0;
temp.z = z-z0;
temp.w = w-(w0+1);
s[2] = gradient4[gi8][0]*temp.x + gradient4[gi8][1]*temp.y + gradient4[gi8][2]*temp.z + gradient4[gi8][3]*temp.w;
temp.x = nx-(x0+1);
temp.x = x-(x0+1);
t[2] = gradient4[gi9][0]*temp.x + gradient4[gi9][1]*temp.y + gradient4[gi9][2]*temp.z + gradient4[gi9][3]*temp.w;
temp.y = ny-(y0+1);
temp.y = y-(y0+1);
v[2] = gradient4[gi11][0]*temp.x + gradient4[gi11][1]*temp.y + gradient4[gi11][2]*temp.z + gradient4[gi11][3]*temp.w;
temp.x = nx-x0;
temp.x = x-x0;
u[2] = gradient4[gi10][0]*temp.x + gradient4[gi10][1]*temp.y + gradient4[gi10][2]*temp.z + gradient4[gi10][3]*temp.w;
temp.y = ny-y0;
temp.z = nz-(z0+1);
temp.y = y-y0;
temp.z = z-(z0+1);
s[3] = gradient4[gi12][0]*temp.x + gradient4[gi12][1]*temp.y + gradient4[gi12][2]*temp.z + gradient4[gi12][3]*temp.w;
temp.x = nx-(x0+1);
temp.x = x-(x0+1);
t[3] = gradient4[gi13][0]*temp.x + gradient4[gi13][1]*temp.y + gradient4[gi13][2]*temp.z + gradient4[gi13][3]*temp.w;
temp.y = ny-(y0+1);
temp.y = y-(y0+1);
v[3] = gradient4[gi15][0]*temp.x + gradient4[gi15][1]*temp.y + gradient4[gi15][2]*temp.z + gradient4[gi15][3]*temp.w;
temp.x = nx-x0;
temp.x = x-x0;
u[3] = gradient4[gi14][0]*temp.x + gradient4[gi14][1]*temp.y + gradient4[gi14][2]*temp.z + gradient4[gi14][3]*temp.w;
Li1 = s[0] + Cx*(t[0]-s[0]);

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
#include <Nazara/Math/Vector2.hpp>
template <typename T> class NAZARA_API NzSimplex2D : public NzNoiseBase
template <typename T> class NAZARA_API NzSimplex2D : public NzAbstract2DNoise<T>, public NzNoiseBase
{
public:
NzSimplex2D();
T GetValue(T x, T y, T res);
T GetValue(T x, T y, T resolution);
~NzSimplex2D() = default;
protected:
private:

View File

@@ -21,10 +21,10 @@ NzSimplex2D<T>::NzSimplex2D()
}
template <typename T>
T NzSimplex2D<T>::GetValue(T x, T y, T res)
T NzSimplex2D<T>::GetValue(T x, T y, T resolution)
{
x /= res;
y /= res;
x *= resolution;
y *= resolution;
sum = (x + y) * SkewCoeff2D;
skewedCubeOrigin.x = fastfloor(x + sum);

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
#include <Nazara/Math/Vector3.hpp>
template <typename T> class NAZARA_API NzSimplex3D : public NzNoiseBase
template <typename T> class NAZARA_API NzSimplex3D : public NzAbstract3DNoise<T>, public NzNoiseBase
{
public:
NzSimplex3D();
T GetValue(T x, T y, T z, T res);
T GetValue(T x, T y, T z, T resolution);
~NzSimplex3D() = default;
protected:
private:

View File

@@ -22,11 +22,11 @@ NzSimplex3D<T>::NzSimplex3D()
}
template <typename T>
T NzSimplex3D<T>::GetValue(T x, T y, T z, T res)
T NzSimplex3D<T>::GetValue(T x, T y, T z, T resolution)
{
x /= res;
y /= res;
z /= res;
x *= resolution;
y *= resolution;
z *= resolution;
sum = (x + y + z) * SkewCoeff3D;
skewedCubeOrigin.x = fastfloor(x + sum);

View File

@@ -9,13 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
#include <Nazara/Math/Vector4.hpp>
template <typename T> class NAZARA_API NzSimplex4D : public NzNoiseBase
template <typename T> class NAZARA_API NzSimplex4D : public NzAbstract4DNoise<T>, public NzNoiseBase
{
public:
NzSimplex4D();
T GetValue(T x, T y, T z, T w, T res);
T GetValue(T x, T y, T z, T w, T resolution);
~NzSimplex4D() = default;
protected:
private:

View File

@@ -46,12 +46,12 @@ NzSimplex4D<T>::NzSimplex4D()
}
template <typename T>
T NzSimplex4D<T>::GetValue(T x, T y, T z, T w, T res)
T NzSimplex4D<T>::GetValue(T x, T y, T z, T w, T resolution)
{
x /= res;
y /= res;
z /= res;
w /= res;
x *= resolution;
y *= resolution;
z *= resolution;
w *= resolution;
sum = (x + y + z + w) * SkewCoeff4D;
skewedCubeOrigin.x = fastfloor(x + sum);