Skip to content

Commit 9e1bcf4

Browse files
Merge pull request #286 from Morpho-lang/updatemodules
plotfield now supports Selections
2 parents a12cd62 + 63df8a7 commit 9e1bcf4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

help/plot.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Plotfield accepts a number of optional arguments to control what is displayed:
6767
* `grade` - Draw the specified grade.
6868
* `colormap` - A `Colormap` object to use. The field is automatically scaled.
6969
* `scalebar` - A `Scalebar` object to use.
70+
* `selection` - Only elements in a provided `Selection` are drawn.
7071
* `style` - Plot style. See below.
7172
* `filter` and `transmit` - Used by the `povray` module to indicate transparency.
7273
* `cmin` and `cmax` - Can be used to define the data range covered.

modules/plot.morpho

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ fn plotselection(mesh, selection, grade=nil, filter=nil, transmit=nil) {
213213

214214
/** Visualizes a field
215215
* @param[in] field - the field
216+
* @param[in] selection - Selection to use
216217
* @param[in] grade - Grade to show
217218
* @param[in] colormap - Colormap to use
218219
* @param[in] style - style to use
219220
* @param[in] scale - whether or not to scale values */
220-
fn plotfield(field, grade=nil, colormap=nil, style=nil, scale=true, filter=nil, transmit=nil, scalebar=nil, cmin=nil, cmax=nil) {
221+
fn plotfield(field, selection=nil, grade=nil, colormap=nil, style=nil, scale=true, filter=nil, transmit=nil, scalebar=nil, cmin=nil, cmax=nil) {
221222
var mesh = field.mesh()
222223
var shape = field.shape()
224+
var sel = selection
223225

224226
var ngrades = shape.count()
225227
var showgrades = [] // Grades to show
@@ -242,7 +244,20 @@ fn plotfield(field, grade=nil, colormap=nil, style=nil, scale=true, filter=nil,
242244
Error("PltFldInt", "Can't use 'interpolate' style: Field lacks values on vertices.").throw()
243245
}
244246

247+
if (sel) { // Ensure we have elements to plot
248+
if (sel.count(2)==0) { // If the selection didn't include facets, infer them from the vertices
249+
sel=sel.clone()
250+
sel.addgrade(2)
251+
} else if (sel.count(0)==0) { // If the selection didn't include vertices, infer them from the facets
252+
sel=sel.clone()
253+
sel.addgrade(0)
254+
}
255+
256+
if (sel.count(2)==0 || sel.count(0)==0) Error("PltSlctnEmpty", "Selection is empty.").throw()
257+
}
258+
245259
for (i in 0...nv) {
260+
if (sel && !sel[0, i]) continue
246261
var val = field[0, i]
247262
if (!isnil(cmin)) val = max(cmin, val)
248263
if (!isnil(cmax)) val = min(cmax, val)
@@ -260,6 +275,7 @@ fn plotfield(field, grade=nil, colormap=nil, style=nil, scale=true, filter=nil,
260275
var cmat = Matrix(3, nel) // Make a color matrix
261276

262277
for (i in 0...nel) {
278+
if (sel && !sel[g, i]) continue
263279
var val = field[g, i]
264280
if (!isnil(cmin)) val = max(cmin, val)
265281
if (!isnil(cmax)) val = min(cmax, val)
@@ -272,7 +288,7 @@ fn plotfield(field, grade=nil, colormap=nil, style=nil, scale=true, filter=nil,
272288
}
273289
}
274290

275-
var out = plotmesh(mesh, grade=showgrades, color=col, filter=filter, transmit=transmit)
291+
var out = plotmesh(mesh, selection=sel, grade=showgrades, color=col, filter=filter, transmit=transmit)
276292

277293
if (isobject(scalebar)) {
278294
var s = scalebar

0 commit comments

Comments
 (0)