Chi-Tech
raytracer_3D_polyhedron.cc
Go to the documentation of this file.
1#include "raytracing.h"
2
3#include "mesh/Cell/cell.h"
5
6//###################################################################
7/**Performs raytracing within a 3D Polyhedron.*/
9 Vector3 &pos_i,
10 Vector3 &omega_i,
11 bool& intersection_found,
12 bool& backward_tolerance_hit,
14{
15 const auto& grid = Grid();
16 chi_mesh::Vector3 ip = pos_i; //Intersection point
17
18 std::vector<RayTracerOutputInformation> triangle_intersections;
19
20 size_t num_faces = cell.faces_.size();
21 triangle_intersections.reserve(num_faces*4); //Guessing 4 tris per face
22 for (int f=0; f<num_faces; f++)
23 {
24 const auto& face = cell.faces_[f];
25
26 size_t num_sides = face.vertex_ids_.size();
27 for (size_t s=0; s<num_sides; s++)
28 {
29 size_t v0_index = face.vertex_ids_[s];
30 size_t v1_index = (s<(num_sides-1)) ? //if not last vertex
31 face.vertex_ids_[s + 1] :
32 face.vertex_ids_[0]; //else
33 const auto& v0 = grid.vertices[v0_index];
34 const auto& v1 = grid.vertices[v1_index];
35 const auto& v2 = cell.faces_[f].centroid_;
36
37 auto v01 = v1 - v0;
38 auto v02 = v2 - v0;
39 auto n_est = v01.Cross(v02);
40
41 if (n_est.Dot(omega_i) < 0.0) continue;
42
44
45 bool intersects =
46 chi_mesh::CheckLineIntersectTriangle2(v0,v1,v2,pos_i,omega_i,ip);
47
48 if (intersects)
49 {
50 triangle_oi.distance_to_surface = (ip - pos_i).Norm();
51 triangle_oi.pos_f = ip;
52
53 triangle_oi.destination_face_index = f;
54 triangle_oi.destination_face_neighbor = cell.faces_[f].neighbor_id_;
55
56 intersection_found = true;
57 triangle_intersections.emplace_back(std::move(triangle_oi));
58 if (not perform_concavity_checks_) break;
59 }//if intersects
60 }//for side
61
62 if (intersection_found and (not perform_concavity_checks_)) break;
63 }//for faces
64
65 //======================================== Determine closest intersection
66 if (not perform_concavity_checks_ and not triangle_intersections.empty())
67 oi = triangle_intersections.back();
68 else if (perform_concavity_checks_ and not triangle_intersections.empty())
69 {
70 auto closest_intersection = &triangle_intersections.back();
71 for (auto& intersection : triangle_intersections)
72 if (intersection.distance_to_surface <
73 closest_intersection->distance_to_surface)
74 closest_intersection = &intersection;
75
76 oi = *closest_intersection;
77 }
78 else
79 {
81 blank_oi.distance_to_surface = 1.0e15;
82 oi = blank_oi;
83 }
84}
std::vector< CellFace > faces_
Definition: cell.h:82
bool perform_concavity_checks_
Definition: raytracing.h:29
void TracePolyhedron(const Cell &cell, Vector3 &pos_i, Vector3 &omega_i, bool &intersection_found, bool &backward_tolerance_hit, RayTracerOutputInformation &oi)
const chi_mesh::MeshContinuum & Grid() const
Definition: raytracer.cc:7
bool CheckLineIntersectTriangle2(const chi_mesh::Vector3 &tri_point0, const chi_mesh::Vector3 &tri_point1, const chi_mesh::Vector3 &tri_point2, const chi_mesh::Vector3 &ray_posi, const chi_mesh::Vector3 &ray_dir, chi_mesh::Vector3 &intersection_point, double *distance_to_intersection=nullptr)