2015-06-01 17:21:51 +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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __UTIL_NOISE_FRACTAL_RMF_IPP
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
#define __UTIL_NOISE_FRACTAL_RMF_IPP
|
|
|
|
|
|
|
|
|
|
|
|
namespace util { namespace noise { namespace fractal {
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T, typename B>
|
2015-06-02 16:13:12 +10:00
|
|
|
rmf<T,B>::rmf (seed_t _seed,
|
|
|
|
unsigned _octaves,
|
2015-06-02 00:15:02 +10:00
|
|
|
T _H,
|
|
|
|
T _offset,
|
|
|
|
T _frequency,
|
|
|
|
T _lacunarity,
|
|
|
|
T _amplitude,
|
2015-06-02 16:13:12 +10:00
|
|
|
T _gain):
|
|
|
|
base<T,B> (_seed,
|
|
|
|
_octaves,
|
|
|
|
_H,
|
|
|
|
_frequency,
|
|
|
|
_lacunarity,
|
|
|
|
_amplitude,
|
|
|
|
_gain),
|
|
|
|
m_offset (_offset)
|
2015-06-01 17:21:51 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T, typename B>
|
2015-06-02 16:13:12 +10:00
|
|
|
rmf<T,B>::rmf (seed_t _seed):
|
|
|
|
rmf<T,B> (_seed,
|
|
|
|
DEFAULT_OCTAVES,
|
2015-06-02 00:15:02 +10:00
|
|
|
DEFAULT_H,
|
2015-06-01 17:21:51 +10:00
|
|
|
DEFAULT_FREQUENCY,
|
|
|
|
DEFAULT_LACUNARITY,
|
|
|
|
DEFAULT_AMPLITUDE,
|
|
|
|
DEFAULT_GAIN,
|
2015-06-02 16:13:12 +10:00
|
|
|
DEFAULT_OFFSET)
|
2015-06-01 17:21:51 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-06-02 16:13:12 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-06-02 00:15:02 +10:00
|
|
|
// we use the name 'amplitude' instead of musgrave's 'gain'.
|
|
|
|
// assumes basis distribution [-1,1] and offset ~= 1
|
2015-06-01 17:21:51 +10:00
|
|
|
template <typename T, typename B>
|
2015-06-30 21:56:30 +10:00
|
|
|
T
|
2015-06-01 17:21:51 +10:00
|
|
|
rmf<T,B>::operator() (util::point<2,T> p) const
|
|
|
|
{
|
2015-06-02 16:13:12 +10:00
|
|
|
T scale = this->m_invAH;
|
2015-06-01 17:21:51 +10:00
|
|
|
|
|
|
|
T signal = 0;
|
|
|
|
T result = 0;
|
|
|
|
T weight = 1;
|
|
|
|
|
2015-06-02 16:13:12 +10:00
|
|
|
p *= this->m_frequency;
|
2015-06-01 17:21:51 +10:00
|
|
|
|
2015-06-02 16:13:12 +10:00
|
|
|
for (size_t i = 0; i < this->m_octaves; ++i) {
|
2015-06-01 17:21:51 +10:00
|
|
|
// generates ridged noise
|
2015-06-02 16:13:12 +10:00
|
|
|
signal = this->m_basis (p);
|
2015-06-01 17:21:51 +10:00
|
|
|
signal = std::fabs (signal);
|
2015-06-02 16:13:12 +10:00
|
|
|
signal = m_offset - signal;
|
2015-06-01 17:21:51 +10:00
|
|
|
|
|
|
|
// sharpens the ridges
|
|
|
|
signal *= signal;
|
|
|
|
|
|
|
|
// influence by sharpness of previous iteration
|
|
|
|
signal *= weight;
|
|
|
|
|
|
|
|
// contribute to the weight
|
2015-06-02 16:13:12 +10:00
|
|
|
weight = signal * this->m_amplitude;
|
2015-06-01 17:21:51 +10:00
|
|
|
weight = limit (weight, 0, 1);
|
|
|
|
|
|
|
|
// record and continue
|
2015-06-02 00:15:02 +10:00
|
|
|
result += signal * scale;
|
2015-06-01 17:21:51 +10:00
|
|
|
|
2015-06-02 16:13:12 +10:00
|
|
|
scale *= this->m_invGH;
|
2015-06-17 03:06:05 +10:00
|
|
|
|
|
|
|
p += T{1};
|
2015-06-02 16:13:12 +10:00
|
|
|
p *= this->m_lacunarity;
|
2015-06-01 17:21:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-15 21:07:41 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T, typename B>
|
|
|
|
T
|
|
|
|
rmf<T,B>::offset (void) const
|
|
|
|
{
|
|
|
|
return m_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T, typename B>
|
|
|
|
T
|
|
|
|
rmf<T,B>::offset (T _offset)
|
|
|
|
{
|
|
|
|
return m_offset = _offset;
|
|
|
|
}
|
2015-06-01 17:21:51 +10:00
|
|
|
} } }
|