sprout/darkroom/materials/plaid.hpp 追加

sprout/string.hpp make_string 追加, operator basic_string 追加
This commit is contained in:
bolero-MURAKAMI 2011-12-14 20:14:13 +09:00
parent 3aee992fc9
commit c0309c22ed
6 changed files with 235 additions and 47 deletions

View file

@ -248,7 +248,7 @@ namespace sprout {
//
template<typename T, typename... Types>
SPROUT_CONSTEXPR inline sprout::array<T, sizeof...(Types)> make_array(Types&&... args) {
return sprout::array<typename std::decay<T>::type, sizeof...(Types)>{{sprout::forward<Types>(args)...}};
return sprout::array<T, sizeof...(Types)>{{sprout::forward<Types>(args)...}};
}
//

View file

@ -3,5 +3,7 @@
#include <sprout/config.hpp>
#include <sprout/darkroom/materials/material.hpp>
#include <sprout/darkroom/materials/uniform.hpp>
#include <sprout/darkroom/materials/plaid.hpp>
#endif // #ifndef SPROUT_DARKROOM_MATERIALS_HPP

View file

@ -77,46 +77,6 @@ namespace sprout {
// material
//
typedef sprout::tuples::tuple<sprout::darkroom::colors::rgb_f, double> material;
//
// uniform_element
//
template<typename Element>
class uniform_element {
public:
typedef Element result_type;
private:
result_type elem_;
public:
SPROUT_CONSTEXPR explicit uniform_element(result_type const& elem)
: elem_(elem)
{}
template<typename Unit>
SPROUT_CONSTEXPR result_type operator()(Unit const&, Unit const&) const {
return elem_;
}
};
//
// make_uniform
//
template<typename Element>
SPROUT_CONSTEXPR sprout::darkroom::materials::uniform_element<Element>
make_uniform(Element const& elem) {
return sprout::darkroom::materials::uniform_element<Element>(elem);
}
//
// make_uniform_material_image
//
template<typename Color, typename Reflection>
SPROUT_CONSTEXPR sprout::tuples::tuple<
sprout::darkroom::materials::uniform_element<Color>,
sprout::darkroom::materials::uniform_element<Reflection>
> make_uniform_material_image(Color const& col, Reflection const& ref) {
return sprout::tuples::make_tuple(
sprout::darkroom::materials::make_uniform(col),
sprout::darkroom::materials::make_uniform(ref)
);
}
} // namespace materials
} // namespace darkroom
} // namespace sprout

View file

@ -0,0 +1,115 @@
#ifndef SPROUT_DARKROOM_MATERIALS_PLAID_HPP
#define SPROUT_DARKROOM_MATERIALS_PLAID_HPP
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/tuple/tuple.hpp>
namespace sprout {
namespace darkroom {
namespace materials {
//
// plaid_element
//
template<typename Element, typename Scale = double>
class plaid_element {
public:
typedef Element result_type;
typedef Scale unit_type;
private:
static SPROUT_CONSTEXPR unit_type fmod(unit_type const& n, unit_type const& d) {
return n - std::intmax_t(n / d) * d;
}
private:
result_type elem1_;
result_type elem2_;
unit_type scale_;
private:
template<typename Unit>
SPROUT_CONSTEXPR result_type calc_1(Unit const& u, Unit const& v) const {
return (u >= 0 && v >= 0) || (u < 0 && v < 0)
? elem1_
: elem2_
;
}
public:
SPROUT_CONSTEXPR plaid_element(
result_type const& elem1,
result_type const& elem2,
unit_type const& scale = 1
)
: elem1_(elem1)
, elem2_(elem2)
, scale_(scale)
{}
template<typename Unit>
SPROUT_CONSTEXPR result_type operator()(Unit const& u, Unit const& v) const {
return calc_1(
(u < 0
? scale_ + fmod(u, scale_)
: fmod(u, scale_)
)
- scale_ / 2
,
(v < 0
? scale_ + fmod(v, scale_)
: fmod(v, scale_)
)
- scale_ / 2
);
}
};
//
// make_plaid
//
template<typename Element>
SPROUT_CONSTEXPR sprout::darkroom::materials::plaid_element<Element>
make_plaid(Element const& elem1, Element const& elem2) {
return sprout::darkroom::materials::plaid_element<Element>(elem1, elem2);
}
template<typename Element, typename Unit>
SPROUT_CONSTEXPR sprout::darkroom::materials::plaid_element<Element>
make_plaid(Element const& elem1, Element const& elem2, Unit const& scale) {
return sprout::darkroom::materials::plaid_element<Element, Unit>(elem1, elem2, scale);
}
//
// make_plaid_material_image
//
template<typename Color, typename Reflection>
SPROUT_CONSTEXPR sprout::tuples::tuple<
sprout::darkroom::materials::plaid_element<Color>,
sprout::darkroom::materials::plaid_element<Reflection>
> make_plaid_material_image(
Color const& col1,
Color const& col2,
Reflection const& ref1,
Reflection const& ref2
)
{
return sprout::tuples::make_tuple(
sprout::darkroom::materials::make_plaid(col1, col2),
sprout::darkroom::materials::make_plaid(ref1, ref2)
);
}
template<typename Color, typename Reflection, typename Unit>
SPROUT_CONSTEXPR sprout::tuples::tuple<
sprout::darkroom::materials::plaid_element<Color, Unit>,
sprout::darkroom::materials::plaid_element<Reflection, Unit>
> make_plaid_material_image(
Color const& col1,
Color const& col2,
Reflection const& ref1,
Reflection const& ref2,
Unit const& scale
)
{
return sprout::tuples::make_tuple(
sprout::darkroom::materials::make_plaid(col1, col2, scale),
sprout::darkroom::materials::make_plaid(ref1, ref2, scale)
);
}
} // namespace materials
} // namespace darkroom
} // namespace sprout
#endif // #ifndef SPROUT_DARKROOM_MATERIALS_PLAID_HPP

View file

@ -0,0 +1,53 @@
#ifndef SPROUT_DARKROOM_MATERIALS_UNIFORM_HPP
#define SPROUT_DARKROOM_MATERIALS_UNIFORM_HPP
#include <sprout/config.hpp>
#include <sprout/tuple/tuple.hpp>
namespace sprout {
namespace darkroom {
namespace materials {
//
// uniform_element
//
template<typename Element>
class uniform_element {
public:
typedef Element result_type;
private:
result_type elem_;
public:
SPROUT_CONSTEXPR explicit uniform_element(result_type const& elem)
: elem_(elem)
{}
template<typename Unit>
SPROUT_CONSTEXPR result_type operator()(Unit const&, Unit const&) const {
return elem_;
}
};
//
// make_uniform
//
template<typename Element>
SPROUT_CONSTEXPR sprout::darkroom::materials::uniform_element<Element>
make_uniform(Element const& elem) {
return sprout::darkroom::materials::uniform_element<Element>(elem);
}
//
// make_uniform_material_image
//
template<typename Color, typename Reflection>
SPROUT_CONSTEXPR sprout::tuples::tuple<
sprout::darkroom::materials::uniform_element<Color>,
sprout::darkroom::materials::uniform_element<Reflection>
> make_uniform_material_image(Color const& col, Reflection const& ref) {
return sprout::tuples::make_tuple(
sprout::darkroom::materials::make_uniform(col),
sprout::darkroom::materials::make_uniform(ref)
);
}
} // namespace materials
} // namespace darkroom
} // namespace sprout
#endif // #ifndef SPROUT_DARKROOM_MATERIALS_UNIFORM_HPP

View file

@ -9,6 +9,7 @@
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/array.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/operation/fixed/push_back.hpp>
@ -214,7 +215,7 @@ namespace sprout {
n2
);
}
template<std::ptrdiff_t...Indexes>
template<std::ptrdiff_t... Indexes>
static SPROUT_CONSTEXPR basic_string<T, N, Traits> from_c_str_impl(
value_type const* s,
size_type n,
@ -254,6 +255,15 @@ namespace sprout {
);
}
#endif
template<std::size_t M, std::ptrdiff_t... Indexes>
static SPROUT_CONSTEXPR basic_string<T, sizeof...(Indexes), Traits> implicit_conversion_impl(
T const(& elems)[M],
size_type len,
sprout::index_tuple<Indexes...>
)
{
return sprout::basic_string<T, sizeof...(Indexes), Traits>{{(Indexes < M - 1 ? elems[Indexes] : T())...}, len};
}
public:
static SPROUT_CONSTEXPR basic_string<T, N, Traits> from_c_str(value_type const* s, size_type n) {
return !(N < n)
@ -497,6 +507,15 @@ namespace sprout {
;
}
// others:
template<std::size_t N2>
SPROUT_CONSTEXPR operator basic_string<T, N2, Traits>() const {
static_assert(N <= N2, "basic_string<>: implicit conversion to small string");
return implicit_conversion_impl(
elems,
len,
typename sprout::index_range<0, N2>::type()
);
}
pointer c_array() SPROUT_NOEXCEPT {
return &elems[0];
}
@ -811,8 +830,11 @@ namespace sprout {
}
};
//
// to_string
//
namespace detail {
template<typename T, std::size_t N, std::ptrdiff_t...Indexes>
template<typename T, std::size_t N, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline sprout::basic_string<T, N - 1> to_string_impl_1(
T const(& arr)[N],
typename sprout::basic_string<T, N - 1>::size_type n,
@ -821,7 +843,7 @@ namespace sprout {
{
return sprout::basic_string<T, N - 1>{{(Indexes < n ? arr[Indexes] : T())...}, n};
}
template<typename T, std::size_t N, std::ptrdiff_t...Indexes>
template<typename T, std::size_t N, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline sprout::basic_string<T, N - 1> to_string_impl(
T const(& arr)[N],
sprout::index_tuple<Indexes...>
@ -830,9 +852,6 @@ namespace sprout {
return to_string_impl_1(arr, sprout::char_traits<T>::length(arr), sprout::index_tuple<Indexes...>());
}
} // namespace detail
//
// to_string
//
template<typename T, std::size_t N>
SPROUT_CONSTEXPR inline sprout::basic_string<T, N - 1> to_string(T const(& arr)[N]) {
return sprout::detail::to_string_impl(arr, typename sprout::index_range<0, N - 1>::type());
@ -850,6 +869,45 @@ namespace sprout {
return sprout::basic_string<T, N>::from_c_str(s);
}
//
// make_string
//
namespace detail {
template<typename T, std::size_t N, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline sprout::basic_string<T, N - 1> make_string_impl_1(
sprout::array<T, N> const& arr,
std::size_t n,
sprout::index_tuple<Indexes...>
)
{
return sprout::basic_string<T, N - 1>{{(Indexes < n ? arr[Indexes] : T())...}, n};
}
template<typename T, std::size_t N, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline sprout::basic_string<T, N - 1> make_string_impl(
sprout::array<T, N> const& arr,
sprout::index_tuple<Indexes...>
)
{
return sprout::detail::make_string_impl_1(
arr,
sprout::char_traits<T>::length(arr.begin()),
sprout::index_tuple<Indexes...>()
);
}
} // namespace detail
template<typename T, typename... Types>
SPROUT_CONSTEXPR inline sprout::basic_string<typename std::decay<T>::type, 1 + sizeof...(Types)>
make_string(T&& t, Types&&... args) {
return sprout::detail::make_string_impl(
sprout::make_array<typename std::decay<T>::type>(
sprout::forward<T>(t),
sprout::forward<Types>(args)...,
T()
),
typename sprout::index_range<0, 1 + sizeof...(Types)>::type()
);
}
//
// operator+
//