6#include "kernel/adjacency/adjactor.hpp"
7#include "kernel/adjacency/base.hpp"
8#include "kernel/adjacency/graph.hpp"
9#include "kernel/shape.hpp"
10#include "kernel/util/math.hpp"
11#include "kernel/util/tiny_algebra.hpp"
27 template<
typename Vector_>
37 template<
typename Vector_>
38 std::ostream& operator<<(std::ostream& out,
const Ray<Vector_>& ray)
40 out <<
"Ray{ origin: " << ray.
origin <<
", direction: " << ray.
direction <<
"}";
62 template<
typename DT_>
83 template<
typename DT_>
86 out <<
"IntersectionData{ t_entry: " << d.
t_entry <<
", t_exit: " << d.
t_exit <<
"}";
96 template<
typename DT_>
103 template<
typename DT_>
104 std::optional<IntersectionData<DT_>>
121 return std::make_optional(
merge(a.value(), b.value()));
135 template<
typename DT_>
196 if(dir_parallel && aligned)
226 return std::make_optional<IntersectionDataType>(t1, t2);
228 else if(dir_parallel && !aligned)
238 if(t >= DT_(0.0) && DT_(0.0) <= u && u <= DT_(1.0))
241 return std::make_optional<IntersectionDataType>(t);
279 const Vector3D e1 = reorder ? c - b : b - a;
280 const Vector3D e2 = reorder ? a - b : c - a;
287 if(det > -
eps && det <
eps)
306 const auto world_to_plane = [&](
const Vector3D& v)
313 const auto plane_to_world = [&](
const Vector2D& v) {
return a + v[0] * e1 + v[1] * e2; };
322 DataType t1(std::numeric_limits<DataType>::max());
331 const Vector2D p1 = origin + i.value().t_entry * direction;
332 const Vector2D p2 = origin + i.value().t_exit * direction;
334 t1 =
Math::min(t1, (plane_to_world(p1) - r.
origin).norm_euclid() / ray_length);
335 t2 =
Math::max(t2, (plane_to_world(p2) - r.
origin).norm_euclid() / ray_length);
337 return std::make_optional<IntersectionDataType>(t1, t2);
357 Tiny::cross(qvec, tvec, e1);
370 return std::make_optional<IntersectionDataType>(t);
381 return (a[0] * b[1]) - (a[1] * b[0]);
387 Tiny::cross(result, a, b);
392 template<
typename MeshType_>
393 std::optional<IntersectionData<typename MeshType_::CoordType>>
394 facet_raycast(
const MeshType_& mesh,
const Ray<typename MeshType_::VertexType>& r,
Index facet)
396 using DataType =
typename MeshType_::CoordType;
397 using IntersectionData = IntersectionData<DataType>;
398 using RIP = RayIntersectionPrimitives<DataType>;
400 using ShapeType =
typename MeshType_::ShapeType;
402 using FacetShapeType =
typename Shape::FaceTraits<ShapeType, ShapeType::dimension - 1>::ShapeType;
403 static constexpr int facet_dim = FacetShapeType::dimension;
404 static constexpr int world_dim = MeshType_::world_dim;
406 const auto& v_at_f = mesh.template get_index_set<facet_dim, 0>();
407 const auto& vtx = mesh.get_vertex_set();
409 if constexpr(std::is_same_v<FacetShapeType, Shape::Vertex> && world_dim == 1)
412 DataType origin = r.origin[0];
413 DataType direction = r.direction[0];
415 DataType facet_coord = mesh.get_vertex_set()[facet][0];
417 if(direction > 0 && facet >= origin)
419 return std::make_optional<IntersectionData>((facet_coord - origin) / direction);
421 else if(direction < 0 && facet <= origin)
423 return std::make_optional<IntersectionData>((origin - facet_coord) / direction);
431 if constexpr(std::is_same_v<FacetShapeType, Shape::Hypercube<1>> && world_dim == 2)
433 return RIP::ray_segment_intersection(r, vtx[v_at_f(facet, 0)], vtx[v_at_f(facet, 1)]);
436 if constexpr(std::is_same_v<FacetShapeType, Shape::Simplex<1>> && world_dim == 2)
438 return RIP::ray_segment_intersection(r, vtx[v_at_f(facet, 0)], vtx[v_at_f(facet, 1)]);
441 if constexpr(std::is_same_v<FacetShapeType, Shape::Simplex<2>> && world_dim == 3)
443 return RIP::ray_triangle_intersection(r, vtx[v_at_f(facet, 0)], vtx[v_at_f(facet, 1)], vtx[v_at_f(facet, 2)]);
446 if constexpr(std::is_same_v<FacetShapeType, Shape::Hypercube<2>> && world_dim == 3)
449 auto i1 = RIP::ray_triangle_intersection(r, vtx[v_at_f(facet, 0)], vtx[v_at_f(facet, 1)], vtx[v_at_f(facet, 2)]);
450 auto i2 = RIP::ray_triangle_intersection(r, vtx[v_at_f(facet, 2)], vtx[v_at_f(facet, 1)], vtx[v_at_f(facet, 3)]);
452 return merge(i1, i2);
455 XABORTM(
"Unsupported raycast case");
461 template<
typename MeshType_>
478 using Vector =
typename MeshType::VertexType;
521 const auto& f_at_c =
_mesh.template get_index_set<shape_dim, facet_dim>();
537 if(!result && hit.
t_exit > eps)
#define ASSERT(expr)
Debug-Assertion macro definition.
#define XABORTM(msg)
Abortion macro definition with custom message.
Composite Adjactor implementation.
ImageIterator image_end(Index domain_node) const
Returns an iterator for the first position past the last adjacent image node.
ImageIterator image_begin(Index domain_node) const
Returns an iterator for the first adjacent image node.
Adjacency Graph implementation.
Intersection tests between Rays and primitives.
Tiny::Vector< DataType, 2 > Vector2D
2D vector/point type
std::optional< IntersectionDataType > IntersectionResultType
Intersection result type.
static IntersectionResultType ray_segment_intersection(const Ray< Vector2D > &r, const Vector2D &a, const Vector2D &b)
2D ray-segment intersection
static IntersectionResultType ray_triangle_intersection(const Ray< Vector3D > &r, const Vector3D &a, const Vector3D &b, const Vector3D &c)
static constexpr DataType eps
Precision.
Tiny::Vector< DataType, 3 > Vector3D
3D vector/point type
Utility class for casting rays originating from a mesh vertex.
static constexpr int shape_dim
Dimension of the mesh shape.
VertexRaycaster(const MeshType &mesh)
Constructor.
IntersectionResultType cast(const Index vertex, const Vector &direction) const
Cast a ray from the given vertex in the given direction.
MeshType_ MeshType
Mesh type.
const MeshType & _mesh
Reference to mesh.
typename MeshType::CoordType DataType
Data type.
static constexpr int facet_dim
Dimension of the facets to test against.
typename MeshType::VertexType Vector
Vector type.
Adjacency::Graph _c_at_v
Precomputes mapping from vertices to adjacent shape_dim-cells.
std::optional< IntersectionDataType > IntersectionResultType
Intersection result type.
IntersectionData< DT_ > merge(const IntersectionData< DT_ > &a, const IntersectionData< DT_ > &b)
Merge two intersection datas.
T_ abs(T_ x)
Returns the absolute value.
T_ min(T_ a, T_ b)
Returns the minimum of two values.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
std::uint64_t Index
Index data type.
Contains information about a ray-primitive intersection.
IntersectionData(DT_ t1, DT_ t2)
Constructor.
DT_ t_entry
Distance along ray of entry point. Relative to ray direction.
DT_ t_exit
Distance along ray of exit point. Relative to ray direction.
IntersectionData(DT_ t)
Constructor.
Vector_ origin
Ray origin.
Vector_ direction
Ray direction.