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/polygon/triangle.hpp

181 lines
6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2016-02-25 09:48:28 +00:00
Copyright (c) 2011-2016 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)
=============================================================================*/
2012-08-20 16:39:09 +00:00
#ifndef SPROUT_DARKROOM_OBJECTS_POLYGON_TRIANGLE_HPP
#define SPROUT_DARKROOM_OBJECTS_POLYGON_TRIANGLE_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/array.hpp>
2012-08-20 16:39:09 +00:00
#include <sprout/tuple/tuple.hpp>
#include <sprout/tuple/functions.hpp>
#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_triangle
//
template<typename Material, typename Vertex = sprout::darkroom::coords::vector3d_t>
2012-08-20 16:39:09 +00:00
class basic_triangle {
public:
typedef Material material_type;
typedef Vertex position_type;
typedef typename sprout::darkroom::access::unit<position_type>::type unit_type;
typedef sprout::array<position_type, 3> vertices_type;
public:
template<typename Ray>
struct intersection {
typedef sprout::tuples::tuple<
bool,
unit_type,
position_type,
position_type,
decltype(sprout::darkroom::materials::calculate_material(
2012-08-20 16:39:09 +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
2012-08-20 16:39:09 +00:00
> type;
};
private:
material_type mat_;
vertices_type vertices_;
position_type normal_;
private:
template<typename Vector>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR bool
2013-06-17 14:44:18 +00:00
within_test(Vector const& d0, Vector const& d1) const {
2012-08-20 16:39:09 +00:00
return sprout::darkroom::coords::dot(
sprout::darkroom::coords::cross(d0, d1),
normal_
)
>= 0
;
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
2013-07-22 13:00:09 +00:00
intersect_5(Ray const& ray, int /*hit_side*/, bool does_intersect, unit_type distance) const {
2012-08-20 16:39:09 +00:00
return typename intersection<Ray>::type(
does_intersect,
distance,
does_intersect
? sprout::darkroom::rays::point_of_intersection(ray, distance)
: position_type(0, 0, 0)
,
2014-01-05 13:20:27 +00:00
normal_,// !!! TEMP: hit_side > 0 ? normal_ : sprout::darkroom::coords::negate(normal_),
2013-12-04 14:38:33 +00:00
sprout::darkroom::materials::calculate_material(mat_, unit_type(0), unit_type(0)), // ???
2014-01-05 13:20:27 +00:00
false // !!! TEMP: is_from_inside
2012-08-20 16:39:09 +00:00
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_4(Ray const& ray, position_type const& v, int hit_side, bool does_intersect) const {
2012-08-20 16:39:09 +00:00
return intersect_5(
ray,
hit_side,
does_intersect,
does_intersect
? sprout::darkroom::coords::length(v)
: unit_type(-1)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_3(Ray const& ray, position_type const& v, position_type const& p, int hit_side, bool exist_forward) const {
2012-08-20 16:39:09 +00:00
return intersect_4(
ray,
v,
hit_side,
exist_forward
&& within_test(
sprout::darkroom::coords::sub(p, vertices_[0]),
sprout::darkroom::coords::sub(vertices_[1], vertices_[0])
)
&& within_test(
sprout::darkroom::coords::sub(p, vertices_[1]),
sprout::darkroom::coords::sub(vertices_[2], vertices_[1])
)
&& within_test(
sprout::darkroom::coords::sub(p, vertices_[2]),
sprout::darkroom::coords::sub(vertices_[0], vertices_[2])
)
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_2(Ray const& ray, unit_type const& t, int hit_side) const {
2012-08-20 16:39:09 +00:00
return intersect_3(
ray,
sprout::darkroom::coords::scale(sprout::darkroom::rays::direction(ray), t),
sprout::darkroom::coords::add(
sprout::darkroom::rays::position(ray),
sprout::darkroom::coords::scale(sprout::darkroom::rays::direction(ray), t)
),
hit_side,
// ???
2012-08-20 16:39:09 +00:00
// hit_side > 0 && t >= 0
t >= 0
);
}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect_1(Ray const& ray, unit_type const& xpn, unit_type const& vn) const {
2012-08-20 16:39:09 +00:00
return intersect_2(
ray,
xpn / vn,
vn >= 0 ? 1 : -1
);
}
public:
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR basic_triangle(material_type const& mat, position_type const& v0, position_type const& v1, position_type const& v2)
2012-08-20 16:39:09 +00:00
: mat_(mat)
, vertices_{{v0, v1, v2}}
, normal_(
sprout::darkroom::coords::normalize(
sprout::darkroom::coords::cross(
sprout::darkroom::coords::sub(v2, v0),
sprout::darkroom::coords::sub(v1, v0)
)
)
)
{}
template<typename Ray>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR typename intersection<Ray>::type
intersect(Ray const& ray) const {
2012-08-20 16:39:09 +00:00
return intersect_1(
ray,
sprout::darkroom::coords::dot(
sprout::darkroom::coords::sub(vertices_[0], sprout::darkroom::rays::position(ray)),
normal_
),
sprout::darkroom::coords::dot(
sprout::darkroom::rays::direction(ray),
normal_
)
);
}
};
//
// make_triangle
//
template<typename Material, typename Vertex>
inline SPROUT_CONSTEXPR sprout::darkroom::objects::basic_triangle<Material, Vertex>
2012-10-05 15:58:56 +00:00
make_triangle(Material const& mat, Vertex const& v0, Vertex const& v1, Vertex const& v2) {
2012-08-20 16:39:09 +00:00
return sprout::darkroom::objects::basic_triangle<Material, Vertex>(mat, v0, v1, v2);
}
} // namespace objects
} // namespace darkroom
} // namespace sprout
#endif // #ifndef SPROUT_DARKROOM_OBJECTS_POLYGON_TRIANGLE_HPP