72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
#pragma once
|
|
#include "vde/core/point.h"
|
|
#include "vde/core/line.h"
|
|
#include "vde/core/triangle.h"
|
|
#include "vde/core/aabb.h"
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
namespace vde::collision {
|
|
using core::Point3D;
|
|
using core::Vector3D;
|
|
using core::Triangle3D;
|
|
using core::Ray3Dd;
|
|
using core::AABB3D;
|
|
|
|
struct RayTriResult {
|
|
double t;
|
|
double u, v;
|
|
Point3D point;
|
|
};
|
|
|
|
// Ray-Triangle (Möller-Trumbore)
|
|
std::optional<RayTriResult> ray_triangle_intersect(
|
|
const Point3D& origin, const Vector3D& dir, const Triangle3D& tri);
|
|
|
|
inline std::optional<RayTriResult> ray_triangle_intersect(
|
|
const Ray3Dd& ray, const Triangle3D& tri) {
|
|
return ray_triangle_intersect(ray.origin(), ray.direction(), tri);
|
|
}
|
|
|
|
// Ray-AABB (slab method). Returns true if hit, with t interval.
|
|
bool ray_aabb_intersect(const Ray3Dd& ray, const AABB3D& box,
|
|
double& tmin_out, double& tmax_out);
|
|
|
|
// Ray-Sphere
|
|
struct RaySphereResult {
|
|
double t;
|
|
Point3D point;
|
|
};
|
|
std::optional<RaySphereResult> ray_sphere_intersect(
|
|
const Point3D& origin, const Vector3D& dir,
|
|
const Point3D& center, double radius);
|
|
|
|
inline std::optional<RaySphereResult> ray_sphere_intersect(
|
|
const Ray3Dd& ray, const Point3D& center, double radius) {
|
|
return ray_sphere_intersect(ray.origin(), ray.direction(), center, radius);
|
|
}
|
|
|
|
// Ray-Plane (returns t value)
|
|
std::optional<double> ray_plane_intersect(
|
|
const Point3D& origin, const Vector3D& dir,
|
|
const Point3D& plane_point, const Vector3D& plane_normal);
|
|
|
|
inline std::optional<double> ray_plane_intersect(
|
|
const Ray3Dd& ray, const Point3D& plane_point,
|
|
const Vector3D& plane_normal) {
|
|
return ray_plane_intersect(ray.origin(), ray.direction(),
|
|
plane_point, plane_normal);
|
|
}
|
|
|
|
// Ray-Mesh — traverse all triangles, return closest hit
|
|
std::optional<RayTriResult> ray_mesh_intersect(
|
|
const Point3D& origin, const Vector3D& dir,
|
|
const std::vector<Triangle3D>& triangles);
|
|
|
|
inline std::optional<RayTriResult> ray_mesh_intersect(
|
|
const Ray3Dd& ray, const std::vector<Triangle3D>& triangles) {
|
|
return ray_mesh_intersect(ray.origin(), ray.direction(), triangles);
|
|
}
|
|
|
|
} // namespace vde::collision
|