1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/darkroom/objects/sphere.hpp

223 lines
7.2 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2013 Bolero MURAKAMI
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_OBJECTS_SPHERE_HPP
#define SPROUT_DARKROOM_OBJECTS_SPHERE_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/tuple/tuple.hpp>
#include <sprout/tuple/functions.hpp>
2012-02-28 09:54:39 +00:00
#include <sprout/math/constants.hpp>
#include <sprout/math/atan2.hpp>
#include <sprout/math/sqrt.hpp>
2011-11-26 06:20:35 +00:00
#include <sprout/darkroom/access/access.hpp>
#include <sprout/darkroom/coords/vector.hpp>
#include <sprout/darkroom/rays/ray.hpp>
#include <sprout/darkroom/materials/material.hpp>
namespace sprout {
namespace darkroom {
namespace objects {
//
// basic_sphere
//
template<typename Material, typename Position = sprout::darkroom::coords::vector3d_t>
2011-11-26 06:20:35 +00:00
class basic_sphere {
public:
typedef Material material_type;
typedef Position position_type;
typedef typename sprout::darkroom::access::unit<position_type>::type unit_type;
typedef unit_type radius_type;
public:
template<typename Ray>
struct intersection {
typedef sprout::tuples::tuple<
bool,
unit_type,
position_type,
position_type,
decltype(sprout::darkroom::materials::calculate_material(
2011-11-26 06:20:35 +00:00
std::declval<material_type const&>(),
std::declval<unit_type const&>(),
std::declval<unit_type const&>()
2013-12-04 14:38:33 +00:00
)),
bool
2011-11-26 06:20:35 +00:00
> type;
};
private:
2013-12-04 14:38:33 +00:00
typedef sprout::tuples::tuple<int, bool, unit_type> zwo_type;
2011-11-26 06:20:35 +00:00
typedef sprout::tuples::tuple<position_type, position_type> drei_type;
struct zw {
2013-12-04 14:38:33 +00:00
SPROUT_STATIC_CONSTEXPR std::size_t hit_side = 0;
SPROUT_STATIC_CONSTEXPR std::size_t does_intersect = 1;
SPROUT_STATIC_CONSTEXPR std::size_t distance = 2;
2011-11-26 06:20:35 +00:00
};
struct dr {
SPROUT_STATIC_CONSTEXPR std::size_t point_of_intersection = 0;
SPROUT_STATIC_CONSTEXPR std::size_t normal = 1;
};
private:
position_type pos_;
radius_type rad_;
material_type mat_;
private:
SPROUT_STATIC_CONSTEXPR zwo_type
zweitens_1(unit_type const& i1, unit_type const& i2) {
return zwo_type(
2013-12-04 14:38:33 +00:00
i2 > 0
? i1 < 0 ? -1 : 1
: 0
,
i2 > 0,
2011-11-26 06:20:35 +00:00
i2 > 0
? i1 < 0 ? i2 : i1
: -1
2011-11-26 06:20:35 +00:00
);
}
SPROUT_STATIC_CONSTEXPR zwo_type
zweitens(bool neg, unit_type const& b, unit_type const& det) {
2011-11-26 06:20:35 +00:00
return neg
? zweitens_1(b - det, b + det)
2013-12-04 14:38:33 +00:00
: zwo_type(0, false, -1)
2011-11-26 06:20:35 +00:00
;
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR drei_type
drittens_1(typename sprout::darkroom::access::unit<Ray>::type point_of_intersection) const {
2011-11-26 06:20:35 +00:00
return drei_type(
point_of_intersection,
sprout::darkroom::coords::normalize(
sprout::darkroom::coords::sub(point_of_intersection, pos_)
)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR drei_type
drittens(Ray const& ray, bool neg, unit_type const& distance) const {
2011-11-26 06:20:35 +00:00
return neg
? drittens_1<Ray>(sprout::darkroom::rays::point_of_intersection(ray, distance))
2011-11-26 06:20:35 +00:00
: drei_type(
sprout::tuples::make<position_type>(0, 0, 0),
sprout::tuples::make<position_type>(1, 1, 1)
2011-11-26 06:20:35 +00:00
)
;
}
template<typename Ray, typename Vec>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
2014-01-07 14:45:09 +00:00
intersect_6(zwo_type const& zwo, drei_type const& drei,bool is_from_inside, Vec const& normal) const {
2011-11-26 06:20:35 +00:00
return typename intersection<Ray>::type(
sprout::tuples::get<zw::does_intersect>(zwo),
sprout::tuples::get<zw::distance>(zwo),
sprout::tuples::get<dr::point_of_intersection>(drei),
is_from_inside
? sprout::darkroom::coords::negate(normal)
: normal
,
sprout::darkroom::materials::calculate_material( // ! Spherical
2011-11-26 06:20:35 +00:00
mat_,
2012-07-06 14:10:49 +00:00
sprout::atan2(
sprout::darkroom::coords::z(normal),
sprout::darkroom::coords::x(normal)
)
2012-02-28 09:54:39 +00:00
/ sprout::math::pi<unit_type>()
,
2012-07-06 14:10:49 +00:00
sprout::atan2(
sprout::darkroom::coords::y(normal),
2012-07-06 14:10:49 +00:00
sprout::sqrt(
sprout::darkroom::coords::x(normal) * sprout::darkroom::coords::x(normal)
+ sprout::darkroom::coords::z(normal) * sprout::darkroom::coords::z(normal)
)
)
2012-05-04 13:13:17 +00:00
/ sprout::math::half_pi<unit_type>()
2013-12-04 14:38:33 +00:00
),
is_from_inside
2011-11-26 06:20:35 +00:00
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
2013-12-04 14:38:33 +00:00
intersect_5(Ray const& ray, zwo_type const& zwo, drei_type const& drei) const {
2014-01-07 14:45:09 +00:00
return intersect_6<Ray>(
zwo,
drei,
sprout::darkroom::coords::dot(sprout::tuples::get<dr::normal>(drei), sprout::darkroom::rays::direction(ray)) > 0,
sprout::tuples::get<dr::normal>(drei)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_4(Ray const& ray, zwo_type const& zwo) const {
2013-12-04 14:38:33 +00:00
return intersect_5(
ray,
2011-11-26 06:20:35 +00:00
zwo,
drittens(
ray,
sprout::tuples::get<zw::does_intersect>(zwo),
sprout::tuples::get<zw::distance>(zwo)
)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_3(Ray const& ray, unit_type const& b, unit_type const& det_sq) const {
2011-11-26 06:20:35 +00:00
return intersect_4(
ray,
zweitens(
det_sq > 0,
b,
det_sq > 0 ? sprout::sqrt(det_sq) : unit_type(0)
2011-11-26 06:20:35 +00:00
)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_2(Ray const& ray, position_type const& v, unit_type const& b) const {
2011-11-26 06:20:35 +00:00
return intersect_3(
ray,
b,
b * b - sprout::darkroom::coords::length_sq(v) + rad_ * rad_
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_1(Ray const& ray, position_type const& v) const {
2011-11-26 06:20:35 +00:00
return intersect_2(
ray,
v,
-sprout::darkroom::coords::dot(v, sprout::darkroom::rays::direction(ray))
);
}
public:
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR basic_sphere(position_type const& pos, radius_type rad, material_type const& mat)
2011-11-26 06:20:35 +00:00
: pos_(pos)
, rad_(rad)
, mat_(mat)
2012-07-05 12:38:02 +00:00
{}
2011-11-26 06:20:35 +00:00
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect(Ray const& ray) const {
2011-11-26 06:20:35 +00:00
return intersect_1(
ray,
sprout::darkroom::coords::sub(sprout::darkroom::rays::position(ray), pos_)
);
}
};
//
// make_sphere
//
template<typename Material, typename Position, typename Radius>
inline SPROUT_CONSTEXPR sprout::darkroom::objects::basic_sphere<Material, Position>
2011-11-26 06:20:35 +00:00
make_sphere(Position const& pos, Radius const& rad, Material const& mat) {
return sprout::darkroom::objects::basic_sphere<Material, Position>(pos, rad, mat);
}
} // namespace objects
} // namespace darkroom
} // namespace sprout
#endif // #ifndef SPROUT_DARKROOM_OBJECTS_SPHERE_HPP