Skip to content

Commit 3a65c22

Browse files
committed
Fixed calculation for intersections
Adjusted the logic which determines an intersection between tetrahedron and plane Added new test case for specific data which caused issues beforehand
1 parent a7961ee commit 3a65c22

2 files changed

Lines changed: 70 additions & 34 deletions

File tree

src/marching.jl

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,77 @@
2525
This method can be used both for the evaluation of plane sections and for
2626
the evaluation of function isosurfaces.
2727
"""
28-
function tet_x_plane!(
28+
function assign_coordinates_at_point_in_plane!(
2929
ixcoord,
3030
ixvalues,
31-
pointlist,
31+
coordinates,
32+
function_values,
33+
node_index,
34+
intersection_count
35+
)
36+
37+
@views ixcoord[:, intersection_count] .= coordinates[:, node_index]
38+
ixvalues[intersection_count] = function_values[node_index]
39+
40+
return nothing
41+
42+
end
43+
44+
function assign_coordinates_at_intersection!(
45+
ixcoord,
46+
ixvalues,
47+
coordinates,
48+
function_values,
49+
planeq_values_start,
50+
planeq_values_end,
51+
node_index_start,
52+
node_index_end,
53+
intersection_count
54+
)
55+
56+
t = planeq_values_start / (planeq_values_start - planeq_values_end)
57+
@views @. ixcoord[:, intersection_count] = coordinates[:, node_index_start] + t * (coordinates[:, node_index_end] - coordinates[:, node_index_start])
58+
ixvalues[intersection_count] = function_values[node_index_start] + t * (function_values[node_index_end] - function_values[node_index_start])
59+
60+
return nothing
61+
end
62+
63+
function calculate_plane_tetrahedron_intersection!(
64+
ixcoord,
65+
ixvalues,
66+
coordinates,
3267
node_indices,
3368
planeq_values,
3469
function_values;
3570
tol = 0.0
3671
)
3772

38-
# If all nodes lie on one side of the plane, no intersection
39-
@fastmath if (
40-
mapreduce(a -> a < -tol, *, planeq_values) ||
41-
mapreduce(a -> a > tol, *, planeq_values)
42-
)
43-
return 0
44-
end
45-
# Interpolate coordinates and function_values according to
46-
# evaluation of the plane equation
47-
intersection_count = 0
48-
# List to check whether a node has already been visited, when checking for possible intersections
49-
visited_list = @MArray zeros(Bool, 4)
50-
@inbounds @simd for n1 in 1:3
51-
N1 = node_indices[n1]
52-
@inbounds @fastmath @simd for n2 in (n1 + 1):4
53-
N2 = node_indices[n2]
5473

55-
if planeq_values[n1] * planeq_values[n2] < tol && !visited_list[n1] && !visited_list[n2]
74+
amount_intersections = 0
5675

57-
abs(planeq_values[n1]) < tol && (visited_list[n1] = true)
58-
abs(planeq_values[n2]) < tol && (visited_list[n2] = true)
59-
60-
intersection_count += 1
61-
t = planeq_values[n1] / (planeq_values[n1] - planeq_values[n2])
62-
ixcoord[1, intersection_count] = pointlist[1, N1] + t * (pointlist[1, N2] - pointlist[1, N1])
63-
ixcoord[2, intersection_count] = pointlist[2, N1] + t * (pointlist[2, N2] - pointlist[2, N1])
64-
ixcoord[3, intersection_count] = pointlist[3, N1] + t * (pointlist[3, N2] - pointlist[3, N1])
65-
ixvalues[intersection_count] = function_values[N1] + t * (function_values[N2] - function_values[N1])
76+
@inbounds for n1 in 1:4
77+
N1 = node_indices[n1]
78+
if abs(planeq_values[n1]) < tol
79+
amount_intersections += 1
80+
assign_coordinates_at_point_in_plane!(ixcoord, ixvalues, coordinates, function_values, N1, amount_intersections)
81+
else
82+
for n2 in (n1 + 1):4
83+
N2 = node_indices[n2]
84+
if (abs(planeq_values[n2]) < tol) # We do not allow the 2nd node to be in the plane
85+
continue
86+
end
87+
if planeq_values[n1] * planeq_values[n2] < tol^2
88+
amount_intersections += 1
89+
assign_coordinates_at_intersection!(ixcoord, ixvalues, coordinates, function_values, planeq_values[n1], planeq_values[n2], N1, N2, amount_intersections)
90+
end
6691
end
6792
end
6893
end
69-
if intersection_count > 4
70-
@warn "computed $intersection_count intersection points of a tetrahedron and a plane. Expected at most 4."
94+
95+
if amount_intersections > 4
96+
@warn "computed $(amount_intersections) intersection points of a tetrahedron and a plane. Expected at most 4."
7197
end
72-
return intersection_count
98+
return amount_intersections
7399
end
74100

75101
"""
@@ -231,7 +257,7 @@ function marching_tetrahedra(
231257
planeq[2] = all_planeq[node_indices[2]]
232258
planeq[3] = all_planeq[node_indices[3]]
233259
planeq[4] = all_planeq[node_indices[4]]
234-
nxs = tet_x_plane!(
260+
nxs = calculate_plane_tetrahedron_intersection!(
235261
ixcoord,
236262
ixvalues,
237263
coord,

test/runtests.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ DocMeta.setdocmeta!(GridVisualizeTools, :DocTestSetup, :(using GridVisualizeTool
44
doctest(GridVisualizeTools)
55

66

7-
@testset "tet_x_plane" begin
7+
@testset "calculate_plane_tetrahedron_intersection" begin
88
#Testing amount of intersected edges of (x,y,z)-tetrahedron (0,0,0),(1,0,0),(1,1,0),(1,1,1) with plane x+y-1=0
9-
@test GridVisualizeTools.tet_x_plane!(
9+
@test GridVisualizeTools.calculate_plane_tetrahedron_intersection!(
1010
[0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0],
1111
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
1212
[0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0; 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0; 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0],
@@ -15,6 +15,16 @@ doctest(GridVisualizeTools)
1515
[0, 0, 0, 0, 0, 0, 0, 1],
1616
tol = 1.0e-12
1717
) == 3
18+
#Testing amount of intersected edges for specific tetrahedron with plane x = 0.5
19+
@test GridVisualizeTools.calculate_plane_tetrahedron_intersection!(
20+
[0.5 0.5 0.500000001260275 0.5000000012855993 0.0 0.0; 0.7 1.0 0.13480492277555536 0.8372051924096586 0.0 0.0; 0.0 0.1 0.19515864670162444 0.18583544507073008 0.0 0.0],
21+
[1.460741148435986e-32, 1.2130506551371849e-32, 0.19103133044823084, 0.22244570933305352, 0.0, 0.0],
22+
[0.5 0.5 0.48305 0.55059; 0.7165 0.8 0.76944 0.76519; 0.12501 0.0 0.16147 0.16147],
23+
Int32[1, 2, 3, 4],
24+
[0.0, 0.0, -0.016950000077486038, 0.05059000104665756],
25+
[0.18823234602961633, 1.519472645376028e-32, 0.2384620788839228, 0.2550147563453821],
26+
tol = 1.0e-12
27+
) == 3
1828
end
1929

2030

0 commit comments

Comments
 (0)