Modifies a mesh3d object so that values of a function are bounded.
clipMesh3d(mesh, fn, bound = 0, greater = TRUE,
minVertices = 0, plot = FALSE, keepValues = FALSE,
keepTags = FALSE)
clipObj3d(ids = tagged3d(tags), fn, bound = 0, greater = TRUE,
minVertices = 0,
replace = TRUE, tags)
If plot = FALSE
,
clipMesh3d
returns new mesh3d object in which all vertices (approximately) satisfy the
clipping condition. Note that the order of vertices will likely
differ from the original order, and new vertices will be added near
the boundary (and if minVertices > 0
, in the
interior). If in addition keepValues = TRUE
,
a component named "values"
will be added to the
mesh containing the values for each vertex.
If keepTags = TRUE
, the tags
component
described below will be added to the output mesh.
If plot = TRUE
, the result will be
plotted with shade3d
and its result returned.
clipObj3d
is called for the side effect of modifying
the scene. It returns a list of new RGL id values
corresponding to the ids
passed as arguments.
A mesh3d
object.
A function used to determine clipping, or a vector of values from such a function, with one value per vertex.
The value(s) of fn
on the clipping boundary.
Logical; whether to keep fn >= bound
or not.
See Details below.
Logical; whether or not to plot the mesh.
Logical; whether to save the function values at
each vertex when plot = FALSE
.
Whether to keep the "tags"
component
of the result; see details below.
The RGL id value(s) of objects to clip.
Object tags; an alternate way to specify ids
. Ignored if ids
is
given.
Should the ids
objects be deleted after the clipped
ones are drawn?
If keepTags = TRUE
, a "tags"
element will be
added to the result. It will be a vector with one entry per
point, segment, triangle and quad in the output mesh. (These tags are not related
to the tags used to identify rgl objects.)
The mesh tags may be used to show the correspondence between
the parts of
the input mesh and output mesh.
By default, the tags are constructed as a numerical sequence
over points,
segments, triangles and
quads in the input mesh, in that order, starting from one. This is the same order
used for colours when
shading with
meshColor == "faces"
.
For example, start with a mesh with one point, two segments, three
triangles and four
quads, but no tags
member.
It would implicitly tag the parts from one to ten as
c(1, # the point
2:3, # the two segments
4:6, # the three triangles
7:10) # the four quads
If clipping deleted the segments and the first triangle, the output would contain the seven element result
mesh$tags <- c(1, # the point remains
# no segments now
5:6, # the two remaining triangles
# were previously items 5 and 6
7:10) # the four quads
The tags
output may contain repetitions. For example,
when a triangle is partially clipped and replaced by several
smaller triangles, entries for all of them will contain the value
corresponding to the original triangle.
The mesh$tags
component may be supplied as part of the input
mesh as any type of vector;
the output will propagate values to the new mesh. The input length
must match the total number of points, segments, triangles and
quads in the input mesh or an error will be raised.
Duncan Murdoch
These functions transform a mesh3d object or other
RGL objects by removing parts where fn
violates
the bound.
For clipMesh3d
the fn
argument can be any
of the following:
a character vector naming a function (with special
names "x"
, "y"
, and "z"
corresponding
to functions returning those coordinates)
a function
a numeric vector with one value per vertex
NULL
, indicating that the numeric values
are saved in mesh$values
For clipObj3d
any of the above except NULL
may be used.
If fn
is a numeric vector, with one value per vertex, those values will be
used in the test.
If it is a function with formal arguments x
,
y
and z
, it will receive the coordinates of
vertices in those arguments, otherwise it will receive
the coordinates in a single n x 3 matrix. The function
should be vectorized and return one value per vertex,
to check against the bound.
These operations are performed on the mesh:
First, all quads are converted to triangles.
Next, each vertex is checked against the condition.
Modifications to triangles depend
on how many of the vertices satisfy the condition
(fn >= bound
or fn <= bound
, depending on greater
)
for inclusion.
If no vertices in a triangle satisfy the condition, the triangle is omitted.
If one vertex satisfies the condition, the other two vertices
in that triangle are shrunk towards it by assuming fn
is locally linear.
If two vertices satisfy the condition, the third vertex is shrunk along each edge towards each other vertex, forming a quadrilateral made of two new triangles.
If all vertices satisfy the condition, they are included with no modifications.
Modifications to line segments are similar: the segment will be shortened if it crosses the boundary, or omitted if it is entirely out of bounds. Points, spheres, text and sprites will just be kept or rejected.
The minVertices
argument is used to improve the
approximation to the boundary when fn
is a non-linear
function. In that case, the interpolation described above
can be inaccurate. If minVertices
is set to a
positive
number (e.g. 10000
), then each object is modified
by subdivision to have at least that number of vertices,
so that pieces are smaller and the linear interpolation
is more accurate. In the clipObj3d
function,
minVertices
can be a vector, with entries corresponding
to each of the entries in ids
.
See https://stackoverflow.com/q/56242470/2554330 and https://laustep.github.io/stlahblog/posts/MeshClipping.html for a motivating example.
See contourLines3d
and
filledContour3d
for ways to display function
values without clipping.
# Show the problem that minVertices solves:
cube <- cube3d(col = rainbow(6), meshColor = "faces")
# This function only has one argument, so it will
# be passed x, y and z in columns of a matrix
vecnorm <- function(vals) apply(vals, 1, function(row) sqrt(sum(row^2)))
open3d()
mfrow3d(2, 2, sharedMouse = TRUE)
id1 <- shade3d(cube)
# All vertices have norm sqrt(3), so this clips nothing:
clipObj3d(id1, fn = vecnorm, bound = sqrt(2))
next3d()
id2 <- wire3d(cube, lit = FALSE)
clipObj3d(id2, fn = vecnorm, bound = sqrt(2))
# This subdivides the cube, and does proper clipping:
next3d()
id3 <- shade3d(cube)
clipObj3d(id3, fn = vecnorm, bound = sqrt(2), minVertices = 200)
next3d()
id4 <- wire3d(cube, lit = FALSE)
clipObj3d(id4, fn = vecnorm, bound = sqrt(2), minVertices = 200)
Run the code above in your browser using DataLab