2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-11-26 06:20:35 +00:00
|
|
|
#ifndef SPROUT_DARKROOM_COORDS_VECTOR_HPP
|
|
|
|
#define SPROUT_DARKROOM_COORDS_VECTOR_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/tuple/tuple.hpp>
|
|
|
|
#include <sprout/tuple/functions.hpp>
|
|
|
|
#include <sprout/utility/forward.hpp>
|
2012-05-09 01:15:19 +00:00
|
|
|
#include <sprout/math/sqrt.hpp>
|
2011-11-26 06:20:35 +00:00
|
|
|
#include <sprout/darkroom/access/access.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace darkroom {
|
|
|
|
namespace coords {
|
|
|
|
//
|
|
|
|
// x
|
|
|
|
// y
|
|
|
|
// z
|
|
|
|
//
|
|
|
|
template<typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR auto
|
|
|
|
x(T&& t)
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t))))
|
|
|
|
-> decltype(sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t)))
|
2011-11-26 06:20:35 +00:00
|
|
|
{
|
2014-02-22 07:32:51 +00:00
|
|
|
return sprout::darkroom::access::get<0>(SPROUT_FORWARD(T, t));
|
2011-11-26 06:20:35 +00:00
|
|
|
}
|
|
|
|
template<typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR auto
|
|
|
|
y(T&& t)
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t))))
|
|
|
|
-> decltype(sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t)))
|
2011-11-26 06:20:35 +00:00
|
|
|
{
|
2014-02-22 07:32:51 +00:00
|
|
|
return sprout::darkroom::access::get<1>(SPROUT_FORWARD(T, t));
|
2011-11-26 06:20:35 +00:00
|
|
|
}
|
|
|
|
template<typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR auto
|
|
|
|
z(T&& t)
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t))))
|
|
|
|
-> decltype(sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t)))
|
2011-11-26 06:20:35 +00:00
|
|
|
{
|
2014-02-22 07:32:51 +00:00
|
|
|
return sprout::darkroom::access::get<2>(SPROUT_FORWARD(T, t));
|
2011-11-26 06:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2013-03-26 17:02:16 +00:00
|
|
|
// vector3d_t
|
2011-11-26 06:20:35 +00:00
|
|
|
// vector3d
|
|
|
|
//
|
2013-03-26 17:02:16 +00:00
|
|
|
typedef sprout::tuples::tuple<double, double, double> vector3d_t;
|
|
|
|
|
|
|
|
inline SPROUT_CONSTEXPR sprout::darkroom::coords::vector3d_t
|
|
|
|
vector3d(double x = 0, double y = 0, double z = 0) {
|
|
|
|
return sprout::darkroom::coords::vector3d_t(x, y, z);
|
|
|
|
}
|
2011-11-26 06:20:35 +00:00
|
|
|
|
2013-12-05 16:57:31 +00:00
|
|
|
//
|
|
|
|
// is_zero
|
|
|
|
//
|
|
|
|
template<typename Vector>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_zero(Vector const& vec) {
|
|
|
|
return sprout::darkroom::coords::x(vec) == 0
|
|
|
|
&& sprout::darkroom::coords::y(vec) == 0
|
|
|
|
&& sprout::darkroom::coords::z(vec) == 0
|
|
|
|
;
|
|
|
|
}
|
2011-11-26 06:20:35 +00:00
|
|
|
//
|
|
|
|
// length_sq
|
|
|
|
//
|
|
|
|
template<typename Vector>
|
2012-04-04 13:23:41 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::unit<Vector>::type
|
2011-11-26 06:20:35 +00:00
|
|
|
length_sq(Vector const& vec) {
|
|
|
|
return sprout::darkroom::coords::x(vec) * sprout::darkroom::coords::x(vec)
|
|
|
|
+ sprout::darkroom::coords::y(vec) * sprout::darkroom::coords::y(vec)
|
|
|
|
+ sprout::darkroom::coords::z(vec) * sprout::darkroom::coords::z(vec)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// length
|
|
|
|
//
|
|
|
|
template<typename Vector>
|
2012-04-04 13:23:41 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::unit<Vector>::type
|
2011-11-26 06:20:35 +00:00
|
|
|
length(Vector const& vec) {
|
2012-07-06 14:10:49 +00:00
|
|
|
return sprout::sqrt(sprout::darkroom::coords::length_sq(vec));
|
2011-11-26 06:20:35 +00:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// add
|
|
|
|
//
|
|
|
|
template<typename Vector1, typename Vector2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector1
|
|
|
|
add(Vector1 const& lhs, Vector2 const& rhs) {
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::tuples::remake<Vector1>(
|
2011-11-26 06:20:35 +00:00
|
|
|
lhs,
|
|
|
|
sprout::darkroom::coords::x(lhs) + sprout::darkroom::coords::x(rhs),
|
|
|
|
sprout::darkroom::coords::y(lhs) + sprout::darkroom::coords::y(rhs),
|
|
|
|
sprout::darkroom::coords::z(lhs) + sprout::darkroom::coords::z(rhs)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// sub
|
|
|
|
//
|
|
|
|
template<typename Vector1, typename Vector2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector1
|
|
|
|
sub(Vector1 const& lhs, Vector2 const& rhs) {
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::tuples::remake<Vector1>(
|
2011-11-26 06:20:35 +00:00
|
|
|
lhs,
|
|
|
|
sprout::darkroom::coords::x(lhs) - sprout::darkroom::coords::x(rhs),
|
|
|
|
sprout::darkroom::coords::y(lhs) - sprout::darkroom::coords::y(rhs),
|
|
|
|
sprout::darkroom::coords::z(lhs) - sprout::darkroom::coords::z(rhs)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// scale
|
|
|
|
//
|
|
|
|
template<typename Vector, typename Fac>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector
|
|
|
|
scale(Vector const& lhs, Fac const& rhs) {
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::tuples::remake<Vector>(
|
2011-11-26 06:20:35 +00:00
|
|
|
lhs,
|
|
|
|
sprout::darkroom::coords::x(lhs) * rhs,
|
|
|
|
sprout::darkroom::coords::y(lhs) * rhs,
|
|
|
|
sprout::darkroom::coords::z(lhs) * rhs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
2012-08-14 09:17:48 +00:00
|
|
|
// negate
|
|
|
|
//
|
|
|
|
template<typename Vector>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector
|
|
|
|
negate(Vector const& vec) {
|
2012-08-14 09:17:48 +00:00
|
|
|
return sprout::tuples::remake<Vector>(
|
|
|
|
vec,
|
|
|
|
-sprout::darkroom::coords::x(vec),
|
|
|
|
-sprout::darkroom::coords::y(vec),
|
|
|
|
-sprout::darkroom::coords::z(vec)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
2011-11-26 06:20:35 +00:00
|
|
|
// dot
|
|
|
|
//
|
|
|
|
template<typename Vector>
|
2012-04-04 13:23:41 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::darkroom::access::unit<Vector>::type
|
2011-11-26 06:20:35 +00:00
|
|
|
dot(Vector const& lhs, Vector const& rhs) {
|
|
|
|
return sprout::darkroom::coords::x(lhs) * sprout::darkroom::coords::x(rhs)
|
|
|
|
+ sprout::darkroom::coords::y(lhs) * sprout::darkroom::coords::y(rhs)
|
|
|
|
+ sprout::darkroom::coords::z(lhs) * sprout::darkroom::coords::z(rhs)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
//
|
2011-12-12 08:23:54 +00:00
|
|
|
// cross
|
|
|
|
//
|
|
|
|
template<typename Vector1, typename Vector2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector1
|
|
|
|
cross(Vector1 const& lhs, Vector2 const& rhs) {
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::tuples::remake<Vector1>(
|
2011-12-12 08:23:54 +00:00
|
|
|
lhs,
|
|
|
|
sprout::darkroom::coords::y(lhs) * sprout::darkroom::coords::z(rhs)
|
|
|
|
- sprout::darkroom::coords::z(lhs) * sprout::darkroom::coords::y(rhs)
|
|
|
|
,
|
|
|
|
sprout::darkroom::coords::y(lhs) * sprout::darkroom::coords::x(rhs)
|
|
|
|
- sprout::darkroom::coords::x(lhs) * sprout::darkroom::coords::y(rhs)
|
|
|
|
,
|
|
|
|
sprout::darkroom::coords::x(lhs) * sprout::darkroom::coords::y(rhs)
|
|
|
|
- sprout::darkroom::coords::y(lhs) * sprout::darkroom::coords::x(rhs)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
2011-11-26 06:20:35 +00:00
|
|
|
// normalize
|
|
|
|
//
|
|
|
|
namespace detail {
|
|
|
|
template<typename Vector>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector
|
|
|
|
normalize_impl(Vector const& vec, typename sprout::darkroom::access::unit<Vector>::type const& len) {
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::tuples::remake<Vector>(
|
2011-11-26 06:20:35 +00:00
|
|
|
vec,
|
|
|
|
sprout::darkroom::coords::x(vec) / len,
|
|
|
|
sprout::darkroom::coords::y(vec) / len,
|
|
|
|
sprout::darkroom::coords::z(vec) / len
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
template<typename Vector>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector
|
|
|
|
normalize(Vector const& vec) {
|
2011-11-26 06:20:35 +00:00
|
|
|
return sprout::darkroom::coords::detail::normalize_impl(
|
|
|
|
vec,
|
|
|
|
sprout::darkroom::coords::length(vec)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
2011-12-12 08:23:54 +00:00
|
|
|
// resize
|
|
|
|
//
|
|
|
|
template<typename Vector, typename Fac>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Vector
|
|
|
|
resize(Vector const& lhs, Fac const& rhs) {
|
2011-12-12 08:23:54 +00:00
|
|
|
return sprout::darkroom::coords::detail::normalize_impl(
|
|
|
|
lhs,
|
|
|
|
sprout::darkroom::coords::length(lhs) / rhs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
2011-11-26 06:20:35 +00:00
|
|
|
// reflect
|
|
|
|
//
|
|
|
|
template<typename Incident, typename Normal>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR Incident
|
|
|
|
reflect(Incident const& incid, Normal const& nor) {
|
2011-11-26 06:20:35 +00:00
|
|
|
return sprout::darkroom::coords::sub(
|
|
|
|
incid,
|
|
|
|
sprout::darkroom::coords::scale(nor, sprout::darkroom::coords::dot(incid, nor) * 2)
|
|
|
|
);
|
|
|
|
}
|
2013-12-05 16:57:31 +00:00
|
|
|
//
|
|
|
|
// refract
|
|
|
|
//
|
|
|
|
namespace detail {
|
|
|
|
template<typename Incident, typename Normal, typename Refraction, typename InNor, typename K>
|
|
|
|
inline SPROUT_CONSTEXPR Incident
|
|
|
|
refract_impl_1(Incident const& incid, Normal const& nor, Refraction const& eta, InNor const& t, K const& k) {
|
|
|
|
return k < 0 ? sprout::tuples::remake<Incident>(incid, 0, 0, 0)
|
2013-12-12 14:29:50 +00:00
|
|
|
: sprout::darkroom::coords::scale(
|
|
|
|
sprout::darkroom::coords::add(incid, sprout::darkroom::coords::scale(nor, t - sprout::math::sqrt(k))),
|
|
|
|
1 / eta
|
2013-12-05 16:57:31 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Incident, typename Normal, typename Refraction, typename InNor>
|
|
|
|
inline SPROUT_CONSTEXPR Incident
|
|
|
|
refract_impl(Incident const& incid, Normal const& nor, Refraction const& eta, InNor const& t) {
|
|
|
|
return sprout::darkroom::coords::detail::refract_impl_1(
|
|
|
|
incid, nor, eta,
|
2013-12-12 14:29:50 +00:00
|
|
|
t, eta * eta + t * t - 1
|
2013-12-05 16:57:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
template<typename Incident, typename Normal, typename Refraction>
|
|
|
|
inline SPROUT_CONSTEXPR Incident
|
|
|
|
refract(Incident const& incid, Normal const& nor, Refraction const& eta) {
|
|
|
|
return sprout::darkroom::coords::detail::refract_impl(
|
|
|
|
incid, nor, eta,
|
2013-12-12 14:29:50 +00:00
|
|
|
-sprout::darkroom::coords::dot(incid, nor)
|
2013-12-05 16:57:31 +00:00
|
|
|
);
|
|
|
|
}
|
2011-11-26 06:20:35 +00:00
|
|
|
} // namespace coords
|
|
|
|
} // namespace darkroom
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DARKROOM_COORDS_VECTOR_HPP
|