@@ -145,6 +145,7 @@ Input parameters:
145145- `func`: n_points vector of piecewise linear function values
146146- `planes`: vector of plane equations `ax+by+cz+d=0`,each stored as vector [a,b,c,d]
147147- `flevels`: vector of function isolevels
148+ - `return_parent_cells`: if true, the parent cell indices are returned as a forth argument
148149
149150Keyword arguments:
150151- `tol`: tolerance for tet x plane intersection
@@ -154,10 +155,11 @@ Keyword arguments:
154155- `Tp`: type of points returned
155156- `Tf`: type of facets returned
156157
157- Return values: (points, tris, values)
158+ Return values: (points, tris, values, [parents] )
158159- `points`: vector of points (Tp)
159160- `tris`: vector of triangles (Tf)
160161- `values`: vector of function values (Tv)
162+ - `parents`: vector of parent cell indices according to the `tris`
161163
162164These can be readily turned into a mesh with function values on it.
163165
@@ -170,6 +172,7 @@ function marching_tetrahedra(
170172 func,
171173 planes,
172174 flevels;
175+ return_parent_cells = false ,
173176 tol = 1.0e-12 ,
174177 primepoints = zeros (0 , 0 ),
175178 primevalues = zeros (0 ),
@@ -183,6 +186,7 @@ function marching_tetrahedra(
183186 [func],
184187 planes,
185188 flevels;
189+ return_parent_cells,
186190 tol,
187191 primepoints,
188192 primevalues,
@@ -198,6 +202,7 @@ function marching_tetrahedra(
198202 allfuncs,
199203 planes,
200204 flevels;
205+ return_parent_cells = false ,
201206 tol = 1.0e-12 ,
202207 primepoints = zeros (0 , 0 ),
203208 primevalues = zeros (0 ),
@@ -215,6 +220,7 @@ function marching_tetrahedra(
215220 all_ixfaces = Vector {Tf} (undef, 0 )
216221 all_ixcoord = Vector {Tp} (undef, 0 )
217222 all_ixvalues = Vector {Tv} (undef, 0 )
223+ all_parentcells = Vector {Ti} (undef, 0 )
218224
219225 @assert (length (primevalues) == size (primepoints, 2 ))
220226 for iprime in 1 : size (primepoints, 2 )
@@ -231,7 +237,7 @@ function marching_tetrahedra(
231237 # Function to evaluate plane equation
232238 @inbounds @fastmath plane_equation (plane, coord) = coord[1 ] * plane[1 ] + coord[2 ] * plane[2 ] + coord[3 ] * plane[3 ] + plane[4 ]
233239
234- function pushtris (ns, ixcoord, ixvalues)
240+ function pushtris (ns, ixcoord, ixvalues, itet )
235241 # number of intersection points can be 3 or 4
236242 if ns >= 3
237243 last_i = length (all_ixvalues)
@@ -240,8 +246,10 @@ function marching_tetrahedra(
240246 push! (all_ixvalues, ixvalues[is]) # todo consider nan_replacement here
241247 end
242248 push! (all_ixfaces, (last_i + 1 , last_i + 2 , last_i + 3 ))
249+ push! (all_parentcells, itet)
243250 if ns == 4
244251 push! (all_ixfaces, (last_i + 3 , last_i + 2 , last_i + 4 ))
252+ push! (all_parentcells, itet)
245253 end
246254 end
247255 return nothing
@@ -274,7 +282,7 @@ function marching_tetrahedra(
274282 func;
275283 tol = tol
276284 )
277- pushtris (nxs, ixcoord, ixvalues)
285+ pushtris (nxs, ixcoord, ixvalues, itet )
278286 end
279287 end
280288
@@ -297,7 +305,12 @@ function marching_tetrahedra(
297305 calcxs ()
298306 end
299307 end
300- return all_ixcoord, all_ixfaces, all_ixvalues
308+
309+ if return_parent_cells
310+ return all_ixcoord, all_ixfaces, all_ixvalues, all_parentcells
311+ else
312+ return all_ixcoord, all_ixfaces, all_ixvalues
313+ end
301314end
302315
303316"""
@@ -312,6 +325,7 @@ Input:
312325 func: function on the grid nodes to be evaluated
313326 lines: vector of line definitions [a,b,c], s.t., ax + by + c = 0 defines a line
314327 levels: vector of levels for the iso-surface
328+ return_parent_cells: if true, a vector of parent cell indices according to the adjacencies is returned as a forth argument
315329 Tc: scalar type of coordinates
316330 Tp: vector type of coordinates
317331 Tv: scalar type of function values
@@ -320,6 +334,7 @@ Output:
320334 points: vector of 2D points of the intersections of the grid with the iso-surfaces or lines
321335 adjacencies: vector of 2D vectors storing connected points in the grid
322336 value: interpolated values of `func` at the intersection points
337+ (optional) parents: indices on the parent grid cells
323338
324339Note that passing both nonempty `lines` and `levels` will create a result with both types of points mixed.
325340"""
@@ -329,11 +344,12 @@ function marching_triangles(
329344 func,
330345 lines,
331346 levels;
347+ return_parent_cells = false ,
332348 Tc = T,
333349 Tp = SVector{2 , Tc},
334350 Tv = Float64
335351 ) where {T <: Number , Ti <: Number }
336- return marching_triangles ([coord], [cellnodes], [func], lines, levels; Tc, Tp, Tv)
352+ return marching_triangles ([coord], [cellnodes], [func], lines, levels, return_parent_cells ; Tc, Tp, Tv)
337353end
338354
339355
@@ -349,13 +365,15 @@ function marching_triangles(
349365 funcs,
350366 lines,
351367 levels;
368+ return_parent_cells = false ,
352369 Tc = T,
353370 Tp = SVector{2 , Tc},
354371 Tv = Float64
355372 ) where {T <: Number , Ti <: Number }
356373 points = Vector {Tp} (undef, 0 )
357374 values = Vector {Tv} (undef, 0 )
358375 adjacencies = Vector {SVector{2, Ti}} (undef, 0 )
376+ parents = Vector {Ti} (undef, 0 )
359377
360378 for igrid in 1 : length (coords)
361379 func = funcs[igrid]
@@ -366,7 +384,7 @@ function marching_triangles(
366384
367385 # the objective_func is used to determine the intersection (line equation or iso levels)
368386 # the value_func is used to interpolate values at the intersections
369- function isect (tri_nodes, objective_func, value_func)
387+ function isect (tri_nodes, objective_func, value_func, itri )
370388 (i1, i2, i3) = (1 , 2 , 3 )
371389
372390 # 3 values of the objective function
@@ -415,6 +433,7 @@ function marching_triangles(
415433 push! (values, value2)
416434 # connect last two points
417435 push! (adjacencies, SVector {2, Ti} ((length (points) - 1 , length (points))))
436+ push! (parents, itri)
418437 end
419438
420439 return
@@ -432,7 +451,7 @@ function marching_triangles(
432451 objective_values,
433452 tri_nodes
434453 )
435- @views isect (tri_nodes, objective_values, func)
454+ @views isect (tri_nodes, objective_values, func, itri )
436455 end
437456
438457 for line in lines
@@ -444,10 +463,15 @@ function marching_triangles(
444463 objective_values,
445464 tri_nodes
446465 )
447- @views isect (tri_nodes, objective_values, func)
466+ @views isect (tri_nodes, objective_values, func, itri )
448467 end
449468
450469 end
451470 end
452- return points, adjacencies, values
471+
472+ if return_parent_cells
473+ return points, adjacencies, values, parents
474+ else
475+ return points, adjacencies, values
476+ end
453477end
0 commit comments