MCPcopy Create free account
hub / github.com/ANYbotics/grid_map / rayTriangleIntersect

Method rayTriangleIntersect

grid_map_pcl/src/GridMapPclConverter.cpp:83–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81}
82
83bool GridMapPclConverter::rayTriangleIntersect(const Eigen::Vector3f& point, const Eigen::Vector3f& ray,
84 const Eigen::Matrix3f& triangleVertexMatrix, Eigen::Vector3f& intersectionPoint) {
85 // Algorithm here is adapted from:
86 // http://softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm#intersect_RayTriangle()
87 //
88 // Original copyright notice:
89 // Copyright 2001, softSurfer (www.softsurfer.com)
90 // This code may be freely used and modified for any purpose
91 // providing that this copyright notice is included with it.
92
93 const Eigen::Vector3f a = triangleVertexMatrix.row(0);
94 const Eigen::Vector3f b = triangleVertexMatrix.row(1);
95 const Eigen::Vector3f c = triangleVertexMatrix.row(2);
96 const Eigen::Vector3f u = b - a;
97 const Eigen::Vector3f v = c - a;
98 const Eigen::Vector3f n = u.cross(v);
99 const float n_dot_ray = n.dot(ray);
100
101 if (std::fabs(n_dot_ray) < 1e-9) {
102 return false;
103 }
104
105 const float r = n.dot(a - point) / n_dot_ray;
106
107 if (r < 0) {
108 return false;
109 }
110
111 // Note(alexmillane): The addition of this comparison delta (not in the
112 // original algorithm) means that rays intersecting the edge of triangles are
113 // treated at hits.
114 constexpr float delta = 1e-5;
115
116 const Eigen::Vector3f w = point + r * ray - a;
117 const float denominator = u.dot(v) * u.dot(v) - u.dot(u) * v.dot(v);
118 const float s_numerator = u.dot(v) * w.dot(v) - v.dot(v) * w.dot(u);
119 const float s = s_numerator / denominator;
120 if (s < (0 - delta) || s > (1 + delta)) {
121 return false;
122 }
123
124 const float t_numerator = u.dot(v) * w.dot(u) - u.dot(u) * w.dot(v);
125 const float t = t_numerator / denominator;
126 if (t < (0 - delta) || s + t > (1 + delta)) {
127 return false;
128 }
129
130 intersectionPoint = a + s * u + t * v;
131
132 return true;
133}
134
135} /* namespace grid_map */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected