From c0309c22edfd45ffe6f0e1516567de7db623823f Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Wed, 14 Dec 2011 20:14:13 +0900 Subject: [PATCH] =?UTF-8?q?sprout/darkroom/materials/plaid.hpp=20=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=20sprout/string.hpp=20make=5Fstring=20=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0,=20operator=20basic=5Fstring=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/array.hpp | 2 +- sprout/darkroom/materials.hpp | 2 + sprout/darkroom/materials/material.hpp | 40 --------- sprout/darkroom/materials/plaid.hpp | 115 +++++++++++++++++++++++++ sprout/darkroom/materials/uniform.hpp | 53 ++++++++++++ sprout/string.hpp | 70 +++++++++++++-- 6 files changed, 235 insertions(+), 47 deletions(-) create mode 100644 sprout/darkroom/materials/plaid.hpp create mode 100644 sprout/darkroom/materials/uniform.hpp diff --git a/sprout/array.hpp b/sprout/array.hpp index e3c3e150..03a12c9a 100644 --- a/sprout/array.hpp +++ b/sprout/array.hpp @@ -248,7 +248,7 @@ namespace sprout { // template SPROUT_CONSTEXPR inline sprout::array make_array(Types&&... args) { - return sprout::array::type, sizeof...(Types)>{{sprout::forward(args)...}}; + return sprout::array{{sprout::forward(args)...}}; } // diff --git a/sprout/darkroom/materials.hpp b/sprout/darkroom/materials.hpp index f9c21c68..d91e8e97 100644 --- a/sprout/darkroom/materials.hpp +++ b/sprout/darkroom/materials.hpp @@ -3,5 +3,7 @@ #include #include +#include +#include #endif // #ifndef SPROUT_DARKROOM_MATERIALS_HPP diff --git a/sprout/darkroom/materials/material.hpp b/sprout/darkroom/materials/material.hpp index 4ca44d0d..05b703a1 100644 --- a/sprout/darkroom/materials/material.hpp +++ b/sprout/darkroom/materials/material.hpp @@ -77,46 +77,6 @@ namespace sprout { // material // typedef sprout::tuples::tuple material; - - // - // uniform_element - // - template - 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 - SPROUT_CONSTEXPR result_type operator()(Unit const&, Unit const&) const { - return elem_; - } - }; - // - // make_uniform - // - template - SPROUT_CONSTEXPR sprout::darkroom::materials::uniform_element - make_uniform(Element const& elem) { - return sprout::darkroom::materials::uniform_element(elem); - } - // - // make_uniform_material_image - // - template - SPROUT_CONSTEXPR sprout::tuples::tuple< - sprout::darkroom::materials::uniform_element, - sprout::darkroom::materials::uniform_element - > 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 diff --git a/sprout/darkroom/materials/plaid.hpp b/sprout/darkroom/materials/plaid.hpp new file mode 100644 index 00000000..5e1d170c --- /dev/null +++ b/sprout/darkroom/materials/plaid.hpp @@ -0,0 +1,115 @@ +#ifndef SPROUT_DARKROOM_MATERIALS_PLAID_HPP +#define SPROUT_DARKROOM_MATERIALS_PLAID_HPP + +#include +#include +#include + +namespace sprout { + namespace darkroom { + namespace materials { + // + // plaid_element + // + template + 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 + 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 + 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 + SPROUT_CONSTEXPR sprout::darkroom::materials::plaid_element + make_plaid(Element const& elem1, Element const& elem2) { + return sprout::darkroom::materials::plaid_element(elem1, elem2); + } + template + SPROUT_CONSTEXPR sprout::darkroom::materials::plaid_element + make_plaid(Element const& elem1, Element const& elem2, Unit const& scale) { + return sprout::darkroom::materials::plaid_element(elem1, elem2, scale); + } + // + // make_plaid_material_image + // + template + SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::darkroom::materials::plaid_element, + sprout::darkroom::materials::plaid_element + > 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 + SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::darkroom::materials::plaid_element, + sprout::darkroom::materials::plaid_element + > 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 diff --git a/sprout/darkroom/materials/uniform.hpp b/sprout/darkroom/materials/uniform.hpp new file mode 100644 index 00000000..b26955c3 --- /dev/null +++ b/sprout/darkroom/materials/uniform.hpp @@ -0,0 +1,53 @@ +#ifndef SPROUT_DARKROOM_MATERIALS_UNIFORM_HPP +#define SPROUT_DARKROOM_MATERIALS_UNIFORM_HPP + +#include +#include + +namespace sprout { + namespace darkroom { + namespace materials { + // + // uniform_element + // + template + 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 + SPROUT_CONSTEXPR result_type operator()(Unit const&, Unit const&) const { + return elem_; + } + }; + // + // make_uniform + // + template + SPROUT_CONSTEXPR sprout::darkroom::materials::uniform_element + make_uniform(Element const& elem) { + return sprout::darkroom::materials::uniform_element(elem); + } + // + // make_uniform_material_image + // + template + SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::darkroom::materials::uniform_element, + sprout::darkroom::materials::uniform_element + > 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 diff --git a/sprout/string.hpp b/sprout/string.hpp index a02490f4..6e8cb4d5 100644 --- a/sprout/string.hpp +++ b/sprout/string.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -214,7 +215,7 @@ namespace sprout { n2 ); } - template + template static SPROUT_CONSTEXPR basic_string from_c_str_impl( value_type const* s, size_type n, @@ -254,6 +255,15 @@ namespace sprout { ); } #endif + template + static SPROUT_CONSTEXPR basic_string implicit_conversion_impl( + T const(& elems)[M], + size_type len, + sprout::index_tuple + ) + { + return sprout::basic_string{{(Indexes < M - 1 ? elems[Indexes] : T())...}, len}; + } public: static SPROUT_CONSTEXPR basic_string from_c_str(value_type const* s, size_type n) { return !(N < n) @@ -497,6 +507,15 @@ namespace sprout { ; } // others: + template + SPROUT_CONSTEXPR operator basic_string() 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 + template SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl_1( T const(& arr)[N], typename sprout::basic_string::size_type n, @@ -821,7 +843,7 @@ namespace sprout { { return sprout::basic_string{{(Indexes < n ? arr[Indexes] : T())...}, n}; } - template + template SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl( T const(& arr)[N], sprout::index_tuple @@ -830,9 +852,6 @@ namespace sprout { return to_string_impl_1(arr, sprout::char_traits::length(arr), sprout::index_tuple()); } } // namespace detail - // - // to_string - // template SPROUT_CONSTEXPR inline sprout::basic_string 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::from_c_str(s); } + // + // make_string + // + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::basic_string make_string_impl_1( + sprout::array const& arr, + std::size_t n, + sprout::index_tuple + ) + { + return sprout::basic_string{{(Indexes < n ? arr[Indexes] : T())...}, n}; + } + template + SPROUT_CONSTEXPR inline sprout::basic_string make_string_impl( + sprout::array const& arr, + sprout::index_tuple + ) + { + return sprout::detail::make_string_impl_1( + arr, + sprout::char_traits::length(arr.begin()), + sprout::index_tuple() + ); + } + } // namespace detail + template + SPROUT_CONSTEXPR inline sprout::basic_string::type, 1 + sizeof...(Types)> + make_string(T&& t, Types&&... args) { + return sprout::detail::make_string_impl( + sprout::make_array::type>( + sprout::forward(t), + sprout::forward(args)..., + T() + ), + typename sprout::index_range<0, 1 + sizeof...(Types)>::type() + ); + } + // // operator+ //