@@ -3,7 +3,6 @@ using Printf
33using Parameters # helps setting default parameters in structures
44using SpecialFunctions: erfc
55using GeoParams
6- using ScatteredInterpolation
76using StaticArrays
87
98# Setup_geometry
@@ -1475,7 +1474,7 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
14751474 xb = Rot3D (End[1 ]- Start[1 ],End[2 ]- Start[2 ], 0.0 , cosd (StrikeAngle), sind (StrikeAngle), 1.0 , 0.0 )
14761475
14771476 # dl
1478- dl = Length/ n_seg;
1477+ dl = trench . Length/ n_seg;
14791478 l = 0 # length at the trench position
14801479 D = @SVector [Top[1 ,2 ], Bottom[1 ,2 ], Bottom[1 ,2 ],Top[1 ,2 ] ]
14811480
@@ -1510,20 +1509,11 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
15101509 # indexes of the segment
15111510 ind_seg = ind_s[ind]
15121511
1513- # Prepare the variable to interpolate {I put here because to allow also a variation of thickness of the slab}
1514- L = @SVector [l,l,ln,ln];
1515-
1516- # Interpolations
1517- points = @SMatrix [pa[1 ] pb[1 ] pc[1 ] pd[1 ]; pa[2 ] pb[2 ] pc[2 ] pd[2 ]]
1518-
1519- itp1 = ScatteredInterpolation. interpolate (Shepard (), points, D);
1520- itp2 = ScatteredInterpolation. interpolate (Shepard (), points, L);
1521-
15221512 # Loop over the chosen particles and interpolate the current value of L and D.
15231513 for ip in ind_seg
1524- point_ = [ Yrot[ip], Z[ip]] ;
1525- d[ip] = ScatteredInterpolation . evaluate (itp1, point_)[ 1 ];
1526- ls[ip] = ScatteredInterpolation . evaluate (itp2, point_)[ 1 ];
1514+ point_ = ( Yrot[ip], Z[ip]) ;
1515+ d[ip] = - distance_to_linesegment ( point_, pa, pd)
1516+ ls[ip] = distance_to_linesegment ( point_, pb, pa) + l
15271517 end
15281518
15291519 # Update l
@@ -1532,6 +1522,40 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
15321522end
15331523
15341524
1525+ """
1526+ distance_to_linesegment(p::NTuple{2,_T}, v::NTuple{2,_T}, w::NTuple{2,_T})
1527+
1528+ Computes the distance normal distance from a point `p` to a line segment defined by the points `v` and `w`.
1529+ """
1530+ function distance_to_linesegment (p:: NTuple{2,_T} , v:: NTuple{2,_T} , w:: NTuple{2,_T} ) where _T<: Number
1531+ dx = w[1 ] - v[1 ]
1532+ dy = w[2 ] - v[2 ]
1533+ l2 = dx* dx + dy* dy # i.e. |w-v|^2 - avoid a sqrt
1534+ if l2 == 0.0
1535+ dx = p[1 ] - v[1 ]
1536+ dy = p[2 ] - v[2 ]
1537+ return sqrt (dx* dx + dy* dy) # v == w case
1538+ end
1539+ # Consider the line extending the segment, parameterized as v + t (w - v).
1540+ # We find projection of point p onto the line.
1541+ # It falls where t = [(p-v) . (w-v)] / |w-v|^2
1542+ t = ((p[1 ] - v[1 ])* dx + (p[2 ] - v[2 ])* dy) / l2
1543+ if t < 0.0
1544+ dx = p[1 ] - v[1 ]
1545+ dy = p[2 ] - v[2 ]
1546+ return sqrt (dx* dx + dy* dy) # Beyond the 'v' end of the segment
1547+ elseif t > 1.0
1548+ dx = p[1 ] - w[1 ]
1549+ dy = p[2 ] - w[2 ]
1550+ return sqrt (dx* dx + dy* dy) # Beyond the 'w' end of the segment
1551+ end
1552+ projection_x = v[1 ] + t * dx
1553+ projection_y = v[2 ] + t * dy
1554+ dx = p[1 ] - projection_x
1555+ dy = p[2 ] - projection_y
1556+ return sqrt (dx* dx + dy* dy)
1557+ end
1558+
15351559"""
15361560 addSlab!(Phase, Temp, Grid::AbstractGeneralGrid, trench::Trench; phase = ConstantPhase(1), T = nothing)
15371561
0 commit comments