DHART
Loading...
Searching...
No Matches
nanort_raytracer.cpp
Go to the documentation of this file.
1#define NANORT_USE_CPP11_FEATURE
2#include <ray_data.h>
3#include <meshinfo.h>
4#include "nanort.h"
5#include <iostream>
6#include <fstream>
7#include <vector>
8
9
11 nanort::TriangleIntersector<double, nanort::TriangleIntersection<double>>
12 (m->vertices, m->faces, sizeof(double) * 3)
13{
14 // Set the mesh data
15 this->mesh = m;
16
17 // Setup a no hit (for safety)
18 hit.u = -1;
19 hit.v = -1;
20 hit.t = -1;
21 hit.prim_id = -1;
22
23 // Setup the ray
24 // Set origin of ray
25 this->ray.org[0] = 0.0;
26 this->ray.org[1] = 0.0;
27 this->ray.org[2] = 0.0;
28
29 this->ray.dir[0] = 0.0;
30 this->ray.dir[1] = 0.0;
31 this->ray.dir[2] = 0.0;
32
33 // Set max and min location
34 this->ray.min_t = 0.0f;
35 this->ray.max_t = 20000.0f;
36}
37
39 // Mesh data was constructed with new, delete here
40 delete[] this->mesh->vertices;
41 delete[] this->mesh->faces;
42 std::cout << "Destroyed" << std::endl;
43}
44
45namespace HF::nanoGeom {
46
51 {
52 /*
53 Not used
54 Just a clarity function for now to show that the Raycasting really needs a list of inputs
55 but the class is abstracting them.
56
57 */
58
59
60 // Performs an intersection of a ray and mesh (via BVH object)
61 // Modifies the input (by reference) isect
62
63 // perform intersection of ray on BVH
64 // &isect is modified and the u,v,t values of intersection are changed in place
65 bool hit = accel.Traverse(ray, triangle_intersector, &isect);
66 if (hit)
67 return true;
68
69 return false;
70 }
71
73 {
74 bool ret = false;
75
76 // Setup nanort tracer BVH options
77 nanort::BVHBuildOptions<double> build_options; // Use default option
78 build_options.cache_bbox = false;
79
80 // Construct datatype using verts and indices for building BVH
81 nanort::TriangleMesh<double> triangle_mesh(mesh.vertices, mesh.faces, sizeof(double) * 3);
82 nanort::TriangleSAHPred<double> triangle_pred(mesh.vertices, mesh.faces, sizeof(double) * 3);
83
84 // build BVH
86 ret = accel.Build(mesh.num_faces, triangle_mesh, triangle_pred, build_options);
87
88 // This shouldn't fail
89 assert(ret);
90
91 // Return the BVH object
92 return accel;
93 }
95 return nanoRT_BVH(mesh.faces, mesh.vertices, mesh.num_vertices, mesh.num_faces);
96 }
97
99 {
100 // Performs an intersection of a ray and mesh (via BVH object)
101 // Modifies the input (by reference) isect
102
103 // perform intersection of ray on BVH
104 // &isect is modified and the u,v,t values of intersection are changed in place
105 bool hit = accel.Traverse(intersector.ray, intersector, &intersector.hit);
106 if (hit) {
107 // Translate the point along the direction vector
108 intersector.point[0] = intersector.ray.org[0] + (intersector.ray.dir[0] * intersector.hit.t);
109 intersector.point[1] = intersector.ray.org[1] + (intersector.ray.dir[1] * intersector.hit.t);
110 intersector.point[2] = intersector.ray.org[2] + (intersector.ray.dir[2] * intersector.hit.t);
111
112 return true;
113 }
114
115
116 return false;
117 }
118}; // end namespace
119
120namespace HF::RayTracer{
121
123 // Get the index and vertex arrays of the meshinfo
124 auto mi_vertices = MI.GetVertexPointer().CopyArray();
125 vertices.resize(mi_vertices.size());
126 for (int i = 0; i < mi_vertices.size(); i++)
127 vertices[i] = static_cast<vertex_t>(mi_vertices[i]);
128
129 // Convert indices to unsigned integer because that's what nanoRT uses
130 auto mi_indices = MI.GetIndexPointer().CopyArray();
131 indices.resize(mi_indices.size());
132 for (int i = 0; i < mi_indices.size(); i++)
133 indices[i] = static_cast<unsigned int>(mi_indices[i]);
134
135 // Build the BVH
136 bvh = HF::nanoGeom::nanoRT_BVH<vertex_t>(indices.data(), vertices.data(), vertices.size() / 3, indices.size() / 3);
137
138 // Create a new intersector. Note: This can't be held as a member by value since you can't even construct this object without
139 // the proper input arguments, however the input arguments cannot be created until we've copied the data from
140 // the mesh and converted it to the proper types. Using a pointer allows us to construct an intersector later.
141 intersector = std::unique_ptr<Intersector>(new Intersector(vertices.data(), indices.data(), sizeof(real_t) * 3));
142 }
144 // Get the index and vertex arrays of the meshinfo
145 auto mi_vertices = MI.GetVertexPointer().CopyArray();
146 vertices.resize(mi_vertices.size());
147 for (int i = 0; i < mi_vertices.size(); i++)
148 vertices[i] = static_cast<vertex_t>(mi_vertices[i]);
149
150 // Convert indices to unsigned integer because that's what nanoRT uses
151 auto mi_indices = MI.GetIndexPointer().CopyArray();
152 indices.resize(mi_indices.size());
153 for (int i = 0; i < mi_indices.size(); i++)
154 indices[i] = static_cast<unsigned int>(mi_indices[i]);
155
156 // Build the BVH
157 bvh = HF::nanoGeom::nanoRT_BVH<vertex_t>(indices.data(), vertices.data(), vertices.size() / 3, indices.size() / 3);
158
159 // Create a new intersector. Note: This can't be held as a member by value since you can't even construct this object without
160 // the proper input arguments, however the input arguments cannot be created until we've copied the data from
161 // the mesh and converted it to the proper types. Using a pointer allows us to construct an intersector later.
162 intersector = std::unique_ptr<Intersector>(new Intersector(vertices.data(), indices.data(), sizeof(real_t) * 3));
163 }
164}
Contains definitions for the MeshInfo class.
Cast rays to determine if and where they intersect geometry.
unsigned int * faces
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:28
nanort::BVHAccel< double > nanoRT_BVH(Mesh &mesh)
size_t num_faces
Definition: meshinfo.h:21
size_t num_vertices
Definition: meshinfo.h:20
double * vertices
Definition: meshinfo.h:22
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)
Definition: nanort.h:91
A collection of vertices and indices representing geometry.
Definition: meshinfo.h:124
const array_and_size< int > GetIndexPointer() const
Get a pointer to the index array of this mesh.
Definition: meshinfo.cpp:326
const array_and_size< numeric_type > GetVertexPointer() const
Get a pointer to the vertex array of this mesh.
Definition: meshinfo.cpp:336
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
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
NanoRTRayTracer(const HF::Geometry::MeshInfo< float > &MI)
Construct a new raytracer with an instance of meshinfo.
std::unique_ptr< Intersector > intersector
Triangle Intersector.
Definition: ray_data.h:146
std::vector< real_t > vertices
Definition: ray_data.h:150
NanoBVH bvh
A NanoRT BVH.
Definition: ray_data.h:147
nanort::TriangleIntersector< vertex_t, Intersection > Intersector
Definition: ray_data.h:141
std::vector< unsigned int > indices
Definition: ray_data.h:151