-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathray2surf.m
77 lines (73 loc) · 1.9 KB
/
ray2surf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function [p, e0] = ray2surf(node, elem, p0, v0, e0)
%
% [p,e0]=ray2surf(node,elem,p0,v0,e0)
%
% Determine the entry position and element for a ray to intersect a mesh
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% node: the mesh coordinate list
% elem: the tetrahedral mesh element list, 4 columns
% p0: origin of the ray
% v0: direction vector of the ray
% e0: search direction: '>' forward search, '<' backward search, '-' bidirectional
%
% output:
% p: the intersection position
% e0: if found, the index of the intersecting element ID
%
% this file is part of Mesh-based Monte Carlo (MMC)
%
% License: GPLv3, see http://mcx.sf.net/mmc/ for details
%
p = p0;
if (size(elem, 2) == 3)
face = elem;
else
face = volface(elem);
end
[t, u, v, idx] = raytrace(p0, v0, node, face);
if (isempty(idx))
error('ray does not intersect with the mesh');
else
t = t(idx);
if (e0 == '>')
% idx1=find(t>=0);
idx1 = find(t >= 1e-10);
elseif (e0 == '<')
idx1 = find(t <= 0);
elseif (isnan(e0) || e0 == '-')
idx1 = 1:length(t);
else
error('ray direction specifier is not recognized');
end
if (isempty(idx1))
error('no intersection is found along the ray direction');
end
t0 = abs(t(idx1));
[tmin, loc] = min(t0);
faceidx = idx(idx1(loc));
% update source position
p = p0 + t(idx1(loc)) * v0;
if (nargout < 2)
return
end
% find initial element id
if (size(elem, 2) == 3)
e0 = faceidx;
else
felem = sort(face(faceidx, :));
f = elem;
f = [f(:, [1, 2, 3])
f(:, [2, 1, 4])
f(:, [1, 3, 4])
f(:, [2, 4, 3])];
[tf, loc] = ismember(felem, sort(f, 2), 'rows');
loc = mod(loc, size(elem, 1));
if (loc == 0)
loc = size(elem, 1);
end
e0 = loc;
end
end