2015-06-01 18:42:10 +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_BASIS_VALUE_IPP
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
#define __UTIL_NOISE_BASIS_VALIE_IPP
|
|
|
|
|
2015-07-01 23:32:27 +10:00
|
|
|
#include "../rand.hpp"
|
2015-10-06 15:45:26 +11:00
|
|
|
#include "../../types.hpp"
|
|
|
|
|
2015-06-01 18:42:10 +10:00
|
|
|
|
|
|
|
namespace util { namespace noise { namespace basis {
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-10-07 00:14:13 +11:00
|
|
|
template <size_t S, typename T, template <typename> class L>
|
2015-10-06 15:45:26 +11:00
|
|
|
value<S,T,L>::value (seed_t _seed):
|
2015-06-02 17:07:42 +10:00
|
|
|
m_seed (_seed)
|
2015-06-01 18:42:10 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2015-10-07 00:14:13 +11:00
|
|
|
template <size_t S, typename T, template <typename> class L>
|
2015-06-01 18:42:10 +10:00
|
|
|
util::range<T>
|
2015-10-06 15:45:26 +11:00
|
|
|
value<S,T,L>::bounds (void) const
|
2015-06-01 18:42:10 +10:00
|
|
|
{
|
|
|
|
return { -1, 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-02 17:07:42 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2015-10-07 00:14:13 +11:00
|
|
|
template <size_t S, typename T, template <typename> class L>
|
2015-06-30 21:56:30 +10:00
|
|
|
seed_t
|
2015-10-06 15:45:26 +11:00
|
|
|
value<S,T,L>::seed (void) const
|
2015-06-02 17:07:42 +10:00
|
|
|
{
|
|
|
|
return m_seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2015-10-07 00:14:13 +11:00
|
|
|
template <size_t S, typename T, template <typename> class L>
|
2015-06-02 17:07:42 +10:00
|
|
|
seed_t
|
2015-10-06 15:45:26 +11:00
|
|
|
value<S,T,L>::seed (seed_t _seed)
|
2015-06-02 17:07:42 +10:00
|
|
|
{
|
|
|
|
return m_seed = _seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-01 18:42:10 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2015-10-07 00:14:13 +11:00
|
|
|
template <size_t S, typename T, template <typename> class L>
|
2015-06-30 21:56:30 +10:00
|
|
|
T
|
2015-10-08 12:20:25 +11:00
|
|
|
value<S,T,L>::operator() (util::point<S,T> p) const noexcept
|
2015-06-01 18:42:10 +10:00
|
|
|
{
|
|
|
|
// extract integer and fractional parts. be careful to always round down
|
2015-09-29 18:07:12 +10:00
|
|
|
auto p_int = floor (p).template cast<intmax_t> ();
|
2015-06-17 03:05:16 +10:00
|
|
|
auto p_rem = p - p_int;
|
2015-06-01 18:42:10 +10:00
|
|
|
|
|
|
|
// generate the corner points
|
2015-10-06 15:45:26 +11:00
|
|
|
std::array<pointi<S>,pow(2,S)> p_;
|
|
|
|
std::transform (std::begin (this->CORNERS), std::end (this->CORNERS),
|
|
|
|
std::begin (p_),
|
|
|
|
[p_int] (auto i) { return p_int + i; });
|
2015-06-01 18:42:10 +10:00
|
|
|
|
2015-10-06 15:45:26 +11:00
|
|
|
// Generate the corner values
|
|
|
|
std::array<T,pow(2,S)> g_;
|
|
|
|
std::transform (std::begin (p_), std::end (p_),
|
|
|
|
std::begin (g_),
|
2015-10-07 00:14:13 +11:00
|
|
|
[this] (auto i) { return rand::scalar<value_t> (m_seed, i) * 2 - 1; });
|
2015-06-01 18:42:10 +10:00
|
|
|
|
|
|
|
// Interpolate on one dimension, then the other.
|
2015-10-06 15:45:26 +11:00
|
|
|
T l_[pow(2,S)];
|
|
|
|
std::copy (std::begin (g_), std::end (g_), std::begin (l_));
|
2015-06-01 18:42:10 +10:00
|
|
|
|
2015-10-06 15:45:26 +11:00
|
|
|
for (size_t i = S; i; --i)
|
|
|
|
for (size_t j = 0; j < std::pow(2,i); j += 2)
|
2015-10-07 00:14:13 +11:00
|
|
|
l_[j / 2] = L<T>() (l_[j], l_[j+1], p_rem[S-i]);
|
2015-10-06 15:45:26 +11:00
|
|
|
return l_[0];
|
2015-06-01 18:42:10 +10:00
|
|
|
}
|
|
|
|
} } }
|