Euphoria
intersection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <optional>
4 
5 #include "base/vec2.h"
6 #include "base/vec3.h"
7 
8 
9 namespace eu
10 {
11  struct Ray2f;
12  struct Ray3f;
13  struct UnitRay3f;
14  struct Aabb;
15  struct Plane;
16 }
17 
18 namespace eu::core
19 {
20  // defined elsewhere
21  struct Sphere;
22 
23  // defined later
24  struct Ray3AabbResult;
25  struct Ray2Ray2Result;
26 
27 
29  // collision functions
30 
31  // --------------------------------
32  // ray - aabb
33 
34  Ray3AabbResult
35  get_intersection(const UnitRay3f& r, const Aabb& aabb);
36 
37  float
38  get_intersection(const UnitRay3f& r, const Plane& p);
39 
40 
41 
42  // --------------------------------
43  // ray - ray
44 
45  Ray2Ray2Result
46  get_intersection(const Ray2f& lhs, const Ray2f& rhs);
47 
48 
49 
50  // --------------------------------
51  // plane - point
52 
53  float
54  get_distance_between(const Plane& plane, const vec3f& p);
55 
56  vec3f
57  get_closest_point(const Plane& plane, const vec3f& p);
58 
59 
60 
61  // --------------------------------
62  // ray - point
63 
64  float
65  get_distance_between(const UnitRay3f& ray, const vec3f& p);
66 
67  vec3f
68  get_closest_point(const UnitRay3f& ray, const vec3f& c);
69 
70 
71 
72  // --------------------------------
73  // sphere - sphere
74 
75  bool
76  get_intersection(const Sphere& lhs, const vec3f& lhs_center, const Sphere& rhs, const vec3f& rhs_center);
77 
78 
79 
80  // --------------------------------
81  // sphere - point
82 
83  bool
84  contains_point(const Sphere& sphere, const vec3f& sphere_center, const vec3f& point);
85 
86  vec3f
87  get_closest_point(const Sphere& sphere, const vec3f& sphere_center, const vec3f& point);
88 
89 
90 
91  // --------------------------------
92  // sphere - ray
93 
94  // returns distance along the ray where intersection occured or -1 if nothing occured
95  float
96  get_intersection(const UnitRay3f& r, const Sphere& sphere, const vec3f& sphere_center);
97 
98 
99 
100  // --------------------------------
101  // aabb - point
102 
103  bool
104  contains_point(const Aabb& aabb, const vec3f& point);
105 
106  vec3f
107  get_closest_point(const Aabb& aabb, const vec3f& point);
108 
109 
110 
111  // --------------------------------
112  // point inside 2d geoms
113 
114  bool
115  is_point_in_triangle(const vec2f& a, const vec2f& b, const vec2f& c, const vec2f& p);
116 
117 
118 
119 
120  // --------------------------------
121  // 3d ray to triangle
122 
123  std::optional<float>
125  (
126  const UnitRay3f& ray,
127  const vec3f& v0, const vec3f& v1, const vec3f& v2
128  );
129 
130 
131 
132 
134  // collision results
135 
136  // todo(Gustav): replace with std::optional?
138  {
139  bool collision;
141 
143  float u;
144  float v;
145  };
146 
147  // todo(Gustav): replace with std::optional?
149  {
151 
152  float start;
153  float end;
154  };
155 
156 }
157 
bool contains_point(const Sphere &sphere, const vec3f &sphere_center, const vec3f &point)
vec3f get_closest_point(const Plane &plane, const vec3f &point)
float get_distance_between(const Plane &plane, const vec3f &p)
bool is_point_in_triangle(const vec2f &a, const vec2f &b, const vec2f &c, const vec2f &p)
std::optional< float > get_intersection_ray_triangle(const UnitRay3f &ray, const vec3f &v0, const vec3f &v1, const vec3f &v2)
This implements the Möller–Trumbore intersection algorithm.
std::optional< float > get_intersection(const UnitRay3f &ray, const CollisionMesh &mesh)
Definition: assert.h:90
Definition: vec2.h:33