DHART
Loading...
Searching...
No Matches
ray_data.h
Go to the documentation of this file.
1#pragma once
2#define NANORT_USE_CPP11_FEATURE
3#include "nanort.h"
4#include <HitStruct.h>
5#include <iostream>
6#include <array>
7
8#undef max
9// Forward Declares
10namespace HF::Geometry {
11 template <typename T> class MeshInfo;
12}
13namespace HF::nanoGeom {
14 struct Mesh;
15}
16
17
18namespace HF::RayTracer{
19 // Define struct of doubles to be used for ray and high precision raycasting
20 struct double3 {
22 double3(double xx, double yy, double zz) {
23 x = xx;
24 y = yy;
25 z = zz;
26 }
27 double3(const double* p) {
28 x = p[0];
29 y = p[1];
30 z = p[2];
31 }
32 double3 operator*(double f) const { return double3(x * f, y * f, z * f); }
33 double3 operator-(const double3& f2) const {
34 return double3(x - f2.x, y - f2.y, z - f2.z);
35 }
36 double3 operator*(const double3& f2) const {
37 return double3(x * f2.x, y * f2.y, z * f2.z);
38 }
39 double3 operator+(const double3& f2) const {
40 return double3(x + f2.x, y + f2.y, z + f2.z);
41 }
43 x += f2.x;
44 y += f2.y;
45 z += f2.z;
46 return (*this);
47 }
48 double3 operator/(const double3& f2) const {
49 return double3(x / f2.x, y / f2.y, z / f2.z);
50 }
51 double operator[](int i) const { return (&x)[i]; }
52 double& operator[](int i) { return (&x)[i]; }
53
54 double3 neg() { return double3(-x, -y, -z); }
55
56 double length() { return sqrt(x * x + y * y + z * z); }
57
58 void normalize() {
59 double len = length();
60 if (fabs(len) > 1.0e-6) {
61 double inv_len = 1.0 / len;
62 x *= inv_len;
63 y *= inv_len;
64 z *= inv_len;
65 }
66 }
67
68 double x, y, z;
69 // double pad; // for alignment
70 };
71
72} // namespace
73
74
75// Derive our own class from the triangle intersector so we can store some extra data
77 public nanort::TriangleIntersector<double, nanort::TriangleIntersection<double> > {
78
79public:
80
81 // Don't let the default empty constructor work since it needs the mesh
82 nanoRT_Data() = delete;
83 // Add a mesh object
85 // Add a ray object to be used for intersections
87 // Add a hit object to be referenced
89 // Add a distance attribute to store intersection distance
90 double dist = -1;
91 double point[3] = { -1,-1,-1 };
92
93 // Set initialization of class by passing a mesh to create a nanort::TriangleIntersector
95
96 // Destructor
98};
99
100
101
102namespace HF::nanoGeom {
103
104 // Convenience method not used now but here for clarity
109
110
111 template <typename T>
112 inline nanort::BVHAccel<T> nanoRT_BVH(unsigned int * indices, T * vertices, int num_vertices, int num_indices)
113 {
114 // Setup nanort tracer BVH options
115 nanort::BVHBuildOptions<T> build_options; // Use default option
116 build_options.cache_bbox = false;
117
118 // Construct datatype using verts and indices for building BVH
119 nanort::TriangleMesh<T> triangle_mesh(vertices, indices, sizeof(T) * 3);
120 nanort::TriangleSAHPred<T> triangle_pred(vertices, indices, sizeof(T) * 3);
121
122 // build BVH using NanoRT Method (Replace this assert with an exception)
124 accel.Build(num_indices, triangle_mesh, triangle_pred, build_options);
125
126 // Return the BVH object
127 return accel;
128 }
130
131 bool nanoRT_Intersect(Mesh& mesh, nanort::BVHAccel<double>& accel, nanoRT_Data& intersector);
132}
133namespace HF::RayTracer {
134
136
137 private:
139 using real_t = double;
144
145 // Add a hit object to be referenced
146 std::unique_ptr<Intersector> intersector;
148
149 const real_t min_dist = 0.0;
150 std::vector<real_t> vertices; //< Internal vertex array
151 std::vector<unsigned int> indices; //< Internal index array
152
153 template <typename dist_type = real_t, typename N>
154 inline NanoRay ConstructRay(const N& origin, const N& direction, dist_type max_dist = std::numeric_limits<dist_type>::max()) {
155 NanoRay out_ray;
156 out_ray.org[0] = origin[0]; out_ray.org[1] = origin[1]; out_ray.org[2] = origin[2];
157 out_ray.dir[0] = direction[0]; out_ray.dir[1] = direction[1]; out_ray.dir[2] = direction[2];
158 out_ray.min_t = min_dist; out_ray.max_t = max_dist;
159
160 return out_ray;
161 }
162
164 Intersection out_hit;
165 out_hit.u = -1; out_hit.v = -1; out_hit.t = -1; out_hit.prim_id = -1;
166 return out_hit;
167 }
168
169 template <typename point_type, typename dist_type>
170 inline void MovePoint(point_type & point, const point_type & dir, dist_type dist) {
171 point[0] += (dir[0] * dist);
172 point[1] += (dir[1] * dist);
173 point[2] += (dir[2] * dist);
174 }
175
176 public:
177
181
182
183 template<typename point_type, typename dist_type = real_t>
185 const point_type& origin,
186 const point_type& dir,
187 dist_type distance = -1.0,
188 int mesh_id = -1)
189 {
190 dist_type max_dist = (distance < 0) ? std::numeric_limits<dist_type>::max() : distance;
191
192 NanoRay ray = ConstructRay<dist_type>(origin, dir, max_dist);
193 Intersection hit = CreateHit();
194
195 // Create a new intersector every time
196 NanoRTRayTracer::Intersector temp_intersector(this->vertices.data(), this->indices.data(), sizeof(real_t)*3);
197
198 bool did_intersect = bvh.Traverse<Intersector>(ray, temp_intersector, &hit);
199
200 if (did_intersect)
201 return HitStruct{ hit.t, 0 };
202 else
203 return HitStruct();
204
205 }
206
207 template<typename point_type>
208 inline bool Occluded(
209 const point_type& origin,
210 const point_type& dir,
211 float distance = -1,
212 int mesh_id = -1)
213 {
214 return Intersect(origin, dir, distance, mesh_id).DidHit();
215 }
216
217 template<typename point_type>
218 inline bool PointIntersection(
219 point_type & origin,
220 const point_type & dir,
221 float distance = -1,
222 int mesh_id = -1
223 ) {
224 auto res = Intersect(origin, dir, distance, mesh_id);
225
226 // If it intersected, move the node and return true, otherwise do nothing and return false.
227 if (res.DidHit()) {
228 MovePoint(origin, dir, res.distance);
229 return true;
230 }
231 else
232 return false;
233 }
234 };
235}
Cast rays to determine if and where they intersect geometry.
Manipulate and load geometry from disk.
Definition: meshinfo.cpp:21
nanort::BVHAccel< double > nanoRT_BVH(Mesh &mesh)
bool nanoRT_Intersect(Mesh &mesh, nanort::BVHAccel< double > &accel, nanoRT_Data &intersector)
bool nanoRT_RayCast(nanort::BVHAccel< double > &accel, nanort::TriangleIntersector< double, nanort::TriangleIntersection< double > > &triangle_intersector, nanort::Ray< double > &ray, nanort::TriangleIntersection< double > &isect)
A collection of vertices and indices representing geometry.
Definition: meshinfo.h:124
A simple hit struct to carry all relevant information about hits.
Definition: HitStruct.h:7
T dir[3]
Definition: nanort.h:496
T org[3]
Definition: nanort.h:495
BVH build option.
Definition: nanort.h:566
Bounding Volume Hierarchy acceleration.
Definition: nanort.h:705
bool Traverse(const Ray< T > &ray, const I &intersector, H *isect, const BVHTraceOptions &options=BVHTraceOptions()) const
Traverse into BVH along ray and find closest hit point & primitive if found.
Definition: nanort.h:2505
bool Build(const unsigned int num_primitives, const Prim &p, const Pred &pred, const BVHBuildOptions< T > &options=BVHBuildOptions< T >())
Build BVH for input primitives.
Definition: nanort.h:1907
double3 operator-(const double3 &f2) const
Definition: ray_data.h:33
double & operator[](int i)
Definition: ray_data.h:52
double3 & operator+=(const double3 &f2)
Definition: ray_data.h:42
double3 operator+(const double3 &f2) const
Definition: ray_data.h:39
double3(double xx, double yy, double zz)
Definition: ray_data.h:22
double3 operator/(const double3 &f2) const
Definition: ray_data.h:48
double3 operator*(double f) const
Definition: ray_data.h:32
double operator[](int i) const
Definition: ray_data.h:51
double3(const double *p)
Definition: ray_data.h:27
double3 operator*(const double3 &f2) const
Definition: ray_data.h:36
double dist
Definition: ray_data.h:90
nanoRT_Data()=delete
HF::nanoGeom::Mesh * mesh
Definition: ray_data.h:84
nanort::Ray< double > ray
Definition: ray_data.h:86
double point[3]
Definition: ray_data.h:91
nanort::TriangleIntersection< double > hit
Definition: ray_data.h:88
bool Occluded(const point_type &origin, const point_type &dir, float distance=-1, int mesh_id=-1)
Definition: ray_data.h:208
std::unique_ptr< Intersector > intersector
Triangle Intersector.
Definition: ray_data.h:146
void MovePoint(point_type &point, const point_type &dir, dist_type dist)
Definition: ray_data.h:170
std::vector< real_t > vertices
Definition: ray_data.h:150
bool PointIntersection(point_type &origin, const point_type &dir, float distance=-1, int mesh_id=-1)
Definition: ray_data.h:218
NanoBVH bvh
A NanoRT BVH.
Definition: ray_data.h:147
HitStruct< real_t > Intersect(const point_type &origin, const point_type &dir, dist_type distance=-1.0, int mesh_id=-1)
Definition: ray_data.h:184
std::vector< unsigned int > indices
Definition: ray_data.h:151
NanoRay ConstructRay(const N &origin, const N &direction, dist_type max_dist=std::numeric_limits< dist_type >::max())
Definition: ray_data.h:154