|
Shinya Kitaoka |
120a6e |
#include <cmath> // pow()</cmath>
|
|
Toshihiro Shimizu |
890ddd |
#include "iwa_noise1234.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
namespace {
|
|
Shinya Kitaoka |
120a6e |
double perlin_noise_3d_(const double x, const double y, const double z,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_start // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_end // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double persistence // Not 0
|
|
Shinya Kitaoka |
120a6e |
// 1/4 or 1/2 or 1/sqrt(3) or 1/sqrt(2) or 1 or ...
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
double total = 0;
|
|
Shinya Kitaoka |
120a6e |
Noise1234 pn;
|
|
Shinya Kitaoka |
120a6e |
for (int ii = octaves_start; ii <= octaves_end; ++ii) {
|
|
Shinya Kitaoka |
120a6e |
const double frequency = pow(2.0, ii); // 1,2,4,8...
|
|
Shinya Kitaoka |
120a6e |
const double amplitude = pow(persistence, ii);
|
|
Shinya Kitaoka |
120a6e |
total += pn.noise(x * frequency, y * frequency, z * frequency) * amplitude;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
return total;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
double perlin_noise_minmax_(const int octaves_start // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_end // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double persistence // Not 0
|
|
Shinya Kitaoka |
120a6e |
// 1/4 or 1/2 or 1/sqrt(3) or 1/sqrt(2) or 1 or ...
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
double total = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int ii = octaves_start; ii <= octaves_end; ++ii) {
|
|
Shinya Kitaoka |
120a6e |
total += pow(persistence, ii);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
return total;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
#include <stdexcept> // std::domain_error(-)</stdexcept>
|
|
Shinya Kitaoka |
120a6e |
#include <limits> // std::numeric_limits</limits>
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_ifx_common.h" /* igs::image::rgba */
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_perlin_noise.h"
|
|
Shinya Kitaoka |
120a6e |
namespace {
|
|
Toshihiro Shimizu |
890ddd |
template <class t=""></class>
|
|
Shinya Kitaoka |
120a6e |
void change_(T *image_array, const int height // pixel
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int width // pixel
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int channels, const bool alpha_rendering_sw,
|
|
Shinya Kitaoka |
120a6e |
const double a11 // geometry of 2D affine transformation
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a12, const double a13, const double a21,
|
|
Shinya Kitaoka |
120a6e |
const double a22, const double a23, const double zz,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_start // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_end // 0<=
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double persistence // Not 0
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
const int max_div = std::numeric_limits<t>::max();</t>
|
|
Shinya Kitaoka |
120a6e |
const int max_div_2 = max_div / 2;
|
|
Shinya Kitaoka |
120a6e |
// 255 / 2 --> 127
|
|
Shinya Kitaoka |
120a6e |
// 65535 / 2 --> 32767
|
|
Shinya Kitaoka |
120a6e |
// const double max_mul = static_cast<double>(max_div_2+0.999999);</double>
|
|
Shinya Kitaoka |
120a6e |
// const double max_off = static_cast<double>(max_div_2+1);</double>
|
|
Shinya Kitaoka |
120a6e |
const double max_mul = static_cast<double>(max_div_2 + 0.499999);</double>
|
|
Shinya Kitaoka |
120a6e |
const double max_off = static_cast<double>(max_div_2 + 1.5);</double>
|
|
Shinya Kitaoka |
120a6e |
/*
|
|
Shinya Kitaoka |
120a6e |
-1 .............. 0 ......... 1
|
|
Shinya Kitaoka |
120a6e |
x127+0.499999
|
|
Shinya Kitaoka |
120a6e |
------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
-127+0.499999 ... 0 ......... 127+0.499999
|
|
Shinya Kitaoka |
120a6e |
+127+1.5 ... 127+1.5 ... 127+1.5
|
|
Shinya Kitaoka |
120a6e |
------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
1.000001 ........ 127+1.5 ... 255.999999
|
|
Shinya Kitaoka |
120a6e |
integer
|
|
Shinya Kitaoka |
120a6e |
------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
1 ............... 128 ....... 255
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
const double maxi =
|
|
Shinya Kitaoka |
120a6e |
perlin_noise_minmax_(octaves_start, octaves_end, persistence);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgba;
|
|
Shinya Kitaoka |
120a6e |
T *image_crnt = image_array;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < height; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < width; ++xx, image_crnt += channels) {
|
|
Shinya Kitaoka |
120a6e |
const T val = static_cast<t>(</t>
|
|
Shinya Kitaoka |
120a6e |
perlin_noise_3d_(xx * a11 + yy * a12 + a13, xx * a21 + yy * a22 + a23,
|
|
Shinya Kitaoka |
120a6e |
zz, octaves_start, octaves_end, persistence) /
|
|
Shinya Kitaoka |
120a6e |
maxi * max_mul +
|
|
Shinya Kitaoka |
120a6e |
max_off);
|
|
Shinya Kitaoka |
120a6e |
for (int zz = 0; zz < channels; ++zz) {
|
|
Shinya Kitaoka |
120a6e |
if (!alpha_rendering_sw && (alp == zz)) {
|
|
Shinya Kitaoka |
120a6e |
image_crnt[zz] = static_cast<t>(max_div);</t>
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
image_crnt[zz] = val;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
// #include "igs_geometry2d.h"
|
|
Toshihiro Shimizu |
890ddd |
void igs::perlin_noise::change(
|
|
Shinya Kitaoka |
120a6e |
unsigned char *image_array, const int height // pixel
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int width // pixel
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int channels, const int bits, const bool alpha_rendering_sw,
|
|
Shinya Kitaoka |
120a6e |
const double a11 // geometry of 2D affine transformation
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a12, const double a13, const double a21, const double a22,
|
|
Shinya Kitaoka |
120a6e |
const double a23, const double zz, const int octaves_start // 0...
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int octaves_end // 0...
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double persistence // not 0
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
// igs::geometry2d::affine af(a11 , a12 , a13 , a21 , a22 , a23);
|
|
Shinya Kitaoka |
120a6e |
// igs::geometry2d::translate();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (std::numeric_limits<unsigned char="">::digits == bits) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_(image_array, height, width, channels, alpha_rendering_sw, a11, a12,
|
|
Shinya Kitaoka |
120a6e |
a13, a21, a22, a23, zz, octaves_start, octaves_end, persistence);
|
|
Shinya Kitaoka |
120a6e |
} else if (std::numeric_limits<unsigned short="">::digits == bits) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_(reinterpret_cast<unsigned *="" short="">(image_array), height, width,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, alpha_rendering_sw, a11, a12, a13, a21, a22, a23, zz,
|
|
Shinya Kitaoka |
120a6e |
octaves_start, octaves_end, persistence);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad bits,Not uchar/ushort");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|