2012-05-23 17:01:30 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-20 13:44:27 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "noise/fractal.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
util::noise::fractal::fractal (unsigned _octaves,
|
|
|
|
double _frequency,
|
|
|
|
double _lacunarity):
|
|
|
|
octaves (_octaves),
|
|
|
|
frequency (_frequency),
|
|
|
|
lacunarity (_lacunarity)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2012-05-24 21:25:18 +10:00
|
|
|
util::noise::fractal::fractal ():
|
|
|
|
octaves (1),
|
|
|
|
frequency (0.1),
|
|
|
|
lacunarity (0.6)
|
|
|
|
{ ; }
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
|
|
|
util::noise::fractal::~fractal ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
util::noise::fractal::eval (double, double) const
|
|
|
|
{ unreachable (); }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-25 15:19:07 +10:00
|
|
|
template <typename B>
|
|
|
|
util::noise::fbm<B>::fbm (unsigned _octaves,
|
|
|
|
double _frequency,
|
|
|
|
double _lacunarity,
|
|
|
|
basis::seed_t _seed):
|
|
|
|
fractal (_octaves, _frequency, _lacunarity),
|
|
|
|
basis (_seed)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
template <typename B>
|
|
|
|
util::noise::fbm<B>::fbm ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename B>
|
|
|
|
double
|
|
|
|
util::noise::fbm<B>::eval (double x, double y) const {
|
|
|
|
double total = 0.0;
|
|
|
|
double f = this->frequency;
|
2012-05-24 15:04:06 +10:00
|
|
|
double a = 1.0;
|
|
|
|
double a_sum = 0.0;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
for (size_t i = 0; i < this->octaves; ++i) {
|
|
|
|
total += basis.eval (x * f, y * f) * a;
|
|
|
|
|
|
|
|
f *= 2.0;
|
2012-05-24 15:04:06 +10:00
|
|
|
|
|
|
|
a_sum += a;
|
2012-05-23 17:01:30 +10:00
|
|
|
a *= this->lacunarity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-23 20:31:59 +10:00
|
|
|
template struct util::noise::fbm<util::noise::cellular>;
|
2012-05-23 17:01:30 +10:00
|
|
|
template struct util::noise::fbm<util::noise::gradient<lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<util::noise::gradient<lerp::quintic>>;
|
|
|
|
template struct util::noise::fbm<util::noise::value<lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<util::noise::value<lerp::quintic>>;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-25 15:19:07 +10:00
|
|
|
template <typename B>
|
|
|
|
util::noise::musgrave<B>::musgrave (unsigned _octaves,
|
|
|
|
double _frequency,
|
|
|
|
double _lacunarity,
|
|
|
|
basis::seed_t _seed):
|
|
|
|
fractal (_octaves, _frequency, _lacunarity),
|
|
|
|
basis (_seed)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
template <typename B>
|
|
|
|
util::noise::musgrave<B>::musgrave ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename B>
|
|
|
|
double
|
|
|
|
util::noise::musgrave<B>::eval (double x, double y) const {
|
|
|
|
double total = 0.0;
|
|
|
|
double f = this->frequency;
|
|
|
|
double a = 1.0;
|
|
|
|
|
|
|
|
double weight = 1.0;
|
|
|
|
double offset = 1.0;
|
|
|
|
double gain = 2.0;
|
|
|
|
|
|
|
|
double signal;
|
|
|
|
|
|
|
|
signal = basis.eval (x * f, y * f);
|
|
|
|
signal = fabs (signal);
|
|
|
|
signal = offset - signal;
|
|
|
|
signal *= signal;
|
|
|
|
total = signal;
|
|
|
|
|
|
|
|
for (size_t i = 1; i < this->octaves; ++i) {
|
|
|
|
f *= 2.0;
|
|
|
|
a *= this->lacunarity;
|
2015-04-13 18:06:08 +10:00
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
weight = signal * gain;
|
|
|
|
weight = max (0.0, min (1.0, weight));
|
|
|
|
|
|
|
|
signal = basis.eval (x * f, y * f);
|
|
|
|
signal = fabs (signal);
|
|
|
|
signal = offset - signal;
|
|
|
|
signal *= signal;
|
|
|
|
|
|
|
|
signal *= weight;
|
|
|
|
total += signal * a;
|
|
|
|
total = min (1.0, max (0.0, total));
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:31:59 +10:00
|
|
|
template struct util::noise::musgrave<util::noise::cellular>;
|
2012-05-23 17:01:30 +10:00
|
|
|
template struct util::noise::musgrave<util::noise::gradient<lerp::linear>>;
|
|
|
|
template struct util::noise::musgrave<util::noise::gradient<lerp::quintic>>;
|
|
|
|
template struct util::noise::musgrave<util::noise::value<lerp::linear>>;
|
|
|
|
template struct util::noise::musgrave<util::noise::value<lerp::quintic>>;
|
|
|
|
|