mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
fix next, prev
This commit is contained in:
parent
e9910b090d
commit
cdb10c0fbe
6 changed files with 221 additions and 5 deletions
|
@ -12,7 +12,7 @@ namespace sprout {
|
|||
return !n ? 0
|
||||
: *s == c ? s
|
||||
: sprout::detail::memchr_impl(s + 1, c, n - 1)
|
||||
;
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
|
|
|
@ -5,5 +5,6 @@
|
|||
#include <sprout/darkroom/objects/intersect.hpp>
|
||||
#include <sprout/darkroom/objects/sphere.hpp>
|
||||
#include <sprout/darkroom/objects/aa_plane.hpp>
|
||||
#include <sprout/darkroom/objects/polygon.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_DARKROOM_OBJECTS_HPP
|
||||
|
|
7
sprout/darkroom/objects/polygon.hpp
Normal file
7
sprout/darkroom/objects/polygon.hpp
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef SPROUT_DARKROOM_OBJECTS_POLYGON_HPP
|
||||
#define SPROUT_DARKROOM_OBJECTS_POLYGON_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/darkroom/objects/polygon/triangle.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_DARKROOM_OBJECTS_POLYGON_HPP
|
204
sprout/darkroom/objects/polygon/triangle.hpp
Normal file
204
sprout/darkroom/objects/polygon/triangle.hpp
Normal file
|
@ -0,0 +1,204 @@
|
|||
#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>
|
||||
#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>
|
||||
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::calc_material(
|
||||
std::declval<material_type const&>(),
|
||||
std::declval<unit_type const&>(),
|
||||
std::declval<unit_type const&>()
|
||||
))
|
||||
> type;
|
||||
};
|
||||
private:
|
||||
material_type mat_;
|
||||
vertices_type vertices_;
|
||||
position_type normal_;
|
||||
private:
|
||||
template<typename Vector>
|
||||
SPROUT_CONSTEXPR bool within_test(Vector const& d0, Vector const& d1) {
|
||||
return sprout::darkroom::coords::dot(
|
||||
sprout::darkroom::coords::cross(d0, d1),
|
||||
normal_
|
||||
)
|
||||
>= 0
|
||||
;
|
||||
}
|
||||
template<typename Ray>
|
||||
SPROUT_CONSTEXPR typename intersection<Ray>::type intersect_5(
|
||||
Ray const& ray,
|
||||
int hit_side,
|
||||
bool does_intersect,
|
||||
unit_type distance
|
||||
) const
|
||||
{
|
||||
return typename intersection<Ray>::type(
|
||||
does_intersect,
|
||||
distance,
|
||||
does_intersect
|
||||
? sprout::darkroom::rays::point_of_intersection(ray, distance)
|
||||
: position_type(0, 0, 0)
|
||||
,
|
||||
hit_side > 0 ? normal_ : sprout::darkroom::coords::negate(normal_),
|
||||
sprout::darkroom::materials::calc_material(mat_, unit_type(0), unit_type(0)) // !
|
||||
);
|
||||
}
|
||||
template<typename Ray>
|
||||
SPROUT_CONSTEXPR typename intersection<Ray>::type intersect_4(
|
||||
Ray const& ray,
|
||||
position_type const& v,
|
||||
int hit_side,
|
||||
bool does_intersect
|
||||
) const
|
||||
{
|
||||
return intersect_5(
|
||||
ray,
|
||||
hit_side,
|
||||
does_intersect,
|
||||
does_intersect
|
||||
? sprout::darkroom::coords::length(v)
|
||||
: unit_type(-1)
|
||||
);
|
||||
}
|
||||
template<typename Ray>
|
||||
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
|
||||
{
|
||||
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>
|
||||
SPROUT_CONSTEXPR typename intersection<Ray>::type intersect_2(
|
||||
Ray const& ray,
|
||||
unit_type const& t,
|
||||
int hit_side
|
||||
) const
|
||||
{
|
||||
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,
|
||||
// !!!
|
||||
// hit_side > 0 && t >= 0
|
||||
t >= 0
|
||||
);
|
||||
}
|
||||
template<typename Ray>
|
||||
SPROUT_CONSTEXPR typename intersection<Ray>::type intersect_1(
|
||||
Ray const& ray,
|
||||
unit_type const& xpn,
|
||||
unit_type const& vn
|
||||
) const
|
||||
{
|
||||
return intersect_2(
|
||||
ray,
|
||||
xpn / vn,
|
||||
vn >= 0 ? 1 : -1
|
||||
);
|
||||
}
|
||||
public:
|
||||
SPROUT_CONSTEXPR basic_triangle(
|
||||
material_type const& mat,
|
||||
position_type const& v0,
|
||||
position_type const& v1,
|
||||
position_type const& v2
|
||||
)
|
||||
: 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>
|
||||
SPROUT_CONSTEXPR typename intersection<Ray>::type intersect(Ray const& ray) const {
|
||||
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>
|
||||
make_triangle(
|
||||
Material const& mat,
|
||||
Vertex const& v0,
|
||||
Vertex const& v1,
|
||||
Vertex const& v2
|
||||
)
|
||||
{
|
||||
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
|
|
@ -25,7 +25,8 @@ namespace sprout {
|
|||
void*
|
||||
)
|
||||
{
|
||||
return std::next(sprout::forward<ForwardIterator>(it));
|
||||
using std::next;
|
||||
return next(sprout::forward<ForwardIterator>(it));
|
||||
}
|
||||
|
||||
template<typename RandomAccessIterator>
|
||||
|
@ -47,7 +48,8 @@ namespace sprout {
|
|||
void*
|
||||
)
|
||||
{
|
||||
return std::next(sprout::forward<ForwardIterator>(it), n);
|
||||
using std::next;
|
||||
return next(sprout::forward<ForwardIterator>(it), n);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
|
|
|
@ -25,7 +25,8 @@ namespace sprout {
|
|||
void*
|
||||
)
|
||||
{
|
||||
return std::prev(sprout::forward<BidirectionalIterator>(it));
|
||||
using std::prev;
|
||||
return prev(sprout::forward<BidirectionalIterator>(it));
|
||||
}
|
||||
|
||||
template<typename RandomAccessIterator>
|
||||
|
@ -47,7 +48,8 @@ namespace sprout {
|
|||
void*
|
||||
)
|
||||
{
|
||||
return std::prev(sprout::forward<BidirectionalIterator>(it), n);
|
||||
using std::prev;
|
||||
return prev(sprout::forward<BidirectionalIterator>(it), n);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue