9#ifndef EMBREE_RAY_TRACER
10#define EMBREE_RAY_TRACER
15#include <corecrt_math_defines.h>
19#define _USE_MATH_DEFINES
22 template <
typename T>
class MeshInfo;
40 double x;
double y;
double z;
127 std::array<Vector3D, 3>
GetTriangle(
unsigned int geomID,
unsigned int primID)
const;
138 int InsertGeom(RTCGeometry& geom,
int id = -1);
164 unsigned int geom_id,
165 unsigned int prim_id,
234 float max_distance = -1,
365 const std::array<float, 3>& origin,
366 const std::array<float, 3>& direction,
529 bool AddMesh(std::vector<std::array<float, 3>>& Mesh,
int ID,
bool Commit =
false);
712 std::array<float, 3>& origin,
713 const std::array<float, 3>& dir,
878 std::vector<std::array<float, 3>>& origins,
879 std::vector<std::array<float, 3>>& directions,
880 bool use_parallel =
true,
881 float max_distance = -1,
969 const std::vector<std::array<float, 3>>& origins,
970 const std::vector<std::array<float, 3>>& directions,
971 float max_distance = -1
972 ,
bool use_parallel =
true
1000 template <
typename return_type =
double,
class N,
class V>
1004 float max_distance = -1.0f,
int mesh_id = -0.1f)
1006 return Intersect<return_type>(node[0], node[1], node[2], direction[0], direction[1], direction[2], max_distance, mesh_id);
1040 template <
typename return_type =
double,
typename numeric1 =
double,
typename numeric2 =
double>
1042 numeric1 x, numeric1 y, numeric1 z,
1043 numeric2 dx, numeric2 dy, numeric2 dz,
1044 float distance = -1.0f,
int mesh_id = -1)
1052 dx,dy,dz, distance, mesh_id
1059 if (!(this->use_precise))
1060 out_struct.
distance = result.ray.tfar;
1068 out_struct.
meshid = result.hit.geomID;
1097 template <
typename N,
typename V,
typename return_type>
1101 return_type& out_distance,
1103 float max_distance = -1.0f)
1108 out_meshid = result.
meshid;
1140 template <
typename return_type,
typename N,
typename V>
1143 const V & directions,
1144 float max_distance = -1.0f,
1145 const bool use_parallel =
false)
1147 const int n = nodes.size();
1149 std::vector<HitStruct<return_type>> results (nodes.size());
1151 #pragma omp parallel for schedule(dynamic, 256) if (use_parallel)
1152 for (
int i = 0; i < n; i++) {
1153 const auto& node = nodes[i];
1154 const auto& direction = directions[i];
1155 results[i] = Intersect<return_type>(node, direction);
1185 template <
typename N,
typename V>
1189 float max_distance = -1.0f,
1193 origin[0], origin[1], origin[2],
1194 direction[0], direction[1], direction[2],
1195 max_distance, mesh_id
1232 template <
typename numeric1,
typename numeric2,
typename dist_type =
float>
1234 numeric1 x, numeric1 y, numeric1 z,
1235 numeric2 dx, numeric2 dy, numeric2 dz,
1236 dist_type max_distance = -1.0,
1242 max_distance, mesh_id
1323 const Vector3D& origin,
1324 const Vector3D& direction,
1327 const Vector3D& v3);
Cast rays to determine if and where they intersect geometry.
A vertex. Used internally in Embree.
A triangle. Used internally in Embree.
bool DidIntersect(int mesh_id)
double RayTriangleIntersection(const Vector3D &origin, const Vector3D &direction, const Vector3D &v1, const Vector3D &v2, const Vector3D &v3)
Determine the distance between a ray's origin and it's point of intersection with a triangle.
Manipulate and load geometry from disk.
A collection of vertices and indices representing geometry.
Vector3D operator*(const double &a) const
Vector3D operator-(const Vector3D &v2) const
Vector3D::Vector3D(double x, double y, double z)
A wrapper for Intel's Embree Library.
RTCIntersectContext context
Context to cast rays within.
std::vector< char > PointIntersections(std::vector< std::array< float, 3 > > &origins, std::vector< std::array< float, 3 > > &directions, bool use_parallel=true, float max_distance=-1, int mesh_id=-1)
Cast multiple rays and recieve hitpoints in return.
bool use_precise
If true, use custom triangle intersection intersection instead of embree's.
void SetupScene()
Performs all the necessary operations to set up the scene.
double CalculatePreciseDistance(unsigned int geom_id, unsigned int prim_id, const Vector3D &origin, const Vector3D &direction) const
Calculate the distance from origin to the point of intersection using an algorithm with higher precis...
~EmbreeRayTracer()
Custom destructor to ensure cleanup of embree resources.
std::vector< RTCGeometry > geometry
bool Occluded(const N &origin, const V &direction, float max_distance=-1.0f, int mesh_id=-1)
Determine if there is an intersection with any geometry.
RTCDevice device
All objects in Embree are created from this. https://www.embree.org/api.html#device-object.
std::array< Vector3D, 3 > GetTriangle(unsigned int geomID, unsigned int primID) const
Get the vertices for a specific triangle in a mesh.
Vertex * Vertices
Vertex buffer. Is used in multiple places but contents are dumped.
bool AddMesh(std::vector< std::array< float, 3 > > &Mesh, int ID, bool Commit=false)
Add a new mesh to this raytracer's BVH with the specified ID.
Triangle * triangles
Triangle buffer. Is used in multiple places, but contents are dumped.
int InsertGeom(RTCGeometry &geom, int id=-1)
Attach geometry to the current scene.
void operator=(const EmbreeRayTracer &ERT2)
Increment reference counters to prevent destruction when a copy is made.
RTCScene scene
Container for a set of geometries, and the BVH. https://www.embree.org/api.html#scene-object.
bool IntersectOutputArguments(const N &node, const V &direction, return_type &out_distance, int &out_meshid, float max_distance=-1.0f)
Cast a ray from origin in direction and update the parameters instead of returning a hitstruct.
RTCGeometry ConstructGeometryFromBuffers(std::vector< Triangle > &tris, std::vector< Vertex > &verts)
Create a new instance of RTCGeometry from a triangle and vertex buffer.
bool PointIntersection(std::array< float, 3 > &origin, const std::array< float, 3 > &dir, float distance=-1, int mesh_id=-1)
Cast a ray and overwrite the origin with the hitpoint if it intersects any geometry.
bool Occluded_IMPL(float x, float y, float z, float dx, float dy, float dz, float distance=-1, int mesh_id=-1)
Implementation for fundamental occlusion ray intersection.
RTCRayHit Intersect_IMPL(float x, float y, float z, float dx, float dy, float dz, float max_distance=-1, int mesh_id=-1)
Implementation for fundamental ray intersection.
bool Occluded(numeric1 x, numeric1 y, numeric1 z, numeric2 dx, numeric2 dy, numeric2 dz, dist_type max_distance=-1.0, int mesh_id=-1)
Determine if there is an intersection with any geometry.
HitStruct< return_type > Intersect(const N &node, const V &direction, float max_distance=-1.0f, int mesh_id=-0.1f)
Cast a ray from origin in direction.
HitStruct< return_type > Intersect(numeric1 x, numeric1 y, numeric1 z, numeric2 dx, numeric2 dy, numeric2 dz, float distance=-1.0f, int mesh_id=-1)
Cast a ray from origin in direction.
std::vector< char > Occlusions(const std::vector< std::array< float, 3 > > &origins, const std::vector< std::array< float, 3 > > &directions, float max_distance=-1, bool use_parallel=true)
Cast multiple occlusion rays in parallel.
std::vector< HitStruct< return_type > > Intersections(const N &nodes, const V &directions, float max_distance=-1.0f, const bool use_parallel=false)
Cast multiple rays in parallel.
A simple hit struct to carry all relevant information about hits.
bool DidHit() const
Determine whether or not this hitstruct contains a hit.
numeric_type distance
Distance from the origin point to the hit point. Set to -1 if no hit was recorded.
int meshid
The ID of the hit mesh. Set to -1 if no hit was recorded.