Skip to content

Commit

Permalink
make cmap(on="") work out its default cell or point data
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Nov 27, 2023
1 parent f69ac9f commit 37b5e3b
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 135 deletions.
2 changes: 1 addition & 1 deletion docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ examples/other/pygmsh_cut.py ust cut tetmesh to gen ugrid

#### Broken Projects
umap_viewer3d
trackviewer (some problems with removing a track)
trackviewer (some problems with removing a track, and z spacing)


#### Broken Exports to .npz:
Expand Down
10 changes: 6 additions & 4 deletions docs/documentation.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://en.wikipedia.org/wiki/MIT_License)
[![Anaconda-Server Badge](https://anaconda.org/conda-forge/vedo/badges/version.svg)](https://anaconda.org/conda-forge/vedo)
[![Ubuntu 23.04 package](https://repology.org/badge/version-for-repo/ubuntu_23_04/vedo.svg)](https://repology.org/project/vedo/versions)
[![Ubuntu 23.10 package](https://repology.org/badge/version-for-repo/ubuntu_23_10/vedo.svg)](https://repology.org/project/vedo/versions)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5842090.svg)](https://doi.org/10.5281/zenodo.5842090)

![](https://user-images.githubusercontent.com/32848391/46815773-dc919500-cd7b-11e8-8e80-8b83f760a303.png)
![](https://vedo.embl.es/images/feats/teapot_banner.png)


A python module for scientific analysis of 3D objects and
point clouds based on [VTK](https://www.vtk.org/) and [numpy](http://www.numpy.org/).

Check out the [GitHub repository here](https://github.com/marcomusy/vedo).
Check out the [GitHub repository here](https://github.com/marcomusy/vedo)
and the [vedo main page here](https://vedo.embl.es).

Find at this [link](https://vedo.embl.es/autodocs/v2023.4.7/vedo.html) the documentation for the older version v2023.4.7.

Expand Down Expand Up @@ -100,7 +101,8 @@ You can also find online tutorials at:
- [3D Visualization of STL Files](https://www.youtube.com/watch?v=llq9-oJXepQ)


- [Creating an interactive 3D geological model](https://www.youtube.com/watch?v=raiIft8VeRU&t=1s) by A. Pollack (SCRF).
- [Creating an interactive 3D geological model](https://www.youtube.com/watch?v=raiIft8VeRU&t=1s) by A. Pollack (SCRF). See a more updated example
[here](https://github.com/marcomusy/vedo/blob/master/examples/advanced/geological_model.py).

- ["vedo", a python module for scientific analysis and visualization of 3D data](https://www.youtube.com/watch?v=MhIoetdxwc0&t=39s), I2K Conference, by M. Musy (EMBL).

Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/line2mesh_tri.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
msh = shape.generate_mesh(invert=True)
msh.smooth() # make the triangles more uniform
msh.compute_quality() # add a measure of triangle quality
msh.cmap(cmap, on="cells")
msh.cmap(cmap)

contour = Line(shape).c("red4").lw(5)
labels = contour.labels("id")
Expand Down
11 changes: 5 additions & 6 deletions examples/advanced/measure_curvature.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
plt.at(0).show(msh, "Original shape")

# Use built-in curvature method
msh1 = msh.clone().compute_curvature(method=0).cmap('viridis')
msh1.add_scalarbar(horizontal=True, size=(100, None))
msh1 = msh.clone().compute_curvature(method=0)
msh1.cmap('viridis').add_scalarbar()
plt.at(1).show(msh1, "Gaussian curvature", azimuth=30, elevation=30)

# Use sphere-fit curvature
Expand All @@ -33,14 +33,13 @@

msh2.pointdata['Spherefit_Curvature'] = curvature
msh2.pointdata['Spherefit_Curvature_Residue'] = residues
msh2.cmap('viridis', msh2.pointdata['Spherefit_Curvature'])
msh2.add_scalarbar(horizontal=True, size=(100, None))
msh2.cmap('viridis', 'Spherefit_Curvature')
msh2.add_scalarbar()
plt.at(2).show(msh2, "Sphere-fitted curvature")

# Show fit residues
msh3 = msh2.clone()
msh3.cmap('jet', msh2.pointdata['Spherefit_Curvature_Residue'])
msh3.add_scalarbar(horizontal=True, size=(100, None))
msh3.cmap('jet', 'Spherefit_Curvature_Residue').add_scalarbar()
plt.at(3).show(msh3, 'Sphere-fitted curvature\nFit residues')

plt.interactive().close()
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/meshquality.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# generate a numpy array for mesh quality
mesh.compute_quality(metric=6)
mesh.cmap("RdYlBu", on="cells")
mesh.cmap("RdYlBu")

hist = histogram(mesh.celldata["Quality"], xtitle="mesh quality", ac="w")
# make it smaller and position it, use_bounds makes the cam
Expand Down
6 changes: 4 additions & 2 deletions examples/basic/colorlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

# Color the lines based on the scalar value using the 'Accent' colormap,
# and add a scalar bar to the plot
lines = Lines(l1, l2).lw(8).cmap('Accent', dist, on='cells').add_scalarbar('length')
lines = Lines(l1, l2, lw=8)
lines.celldata["distance"] = dist
lines.cmap('Accent').add_scalarbar('length')

# Define a callback function to print the length of the clicked line segment
def clickfunc(evt):
if evt.object:
# Get the ID of the closest point on the clicked line segment
idl = evt.object.closest_point(evt.picked3d, return_cell_id=True)
# Print the length of the line segment with 3 decimal places
print('clicked line', idl, 'length =', precision(dist[idl],3))
print('clicked line', idl, 'length =', dist[idl])

# Create a plotter with the mouse click callback function and show the lines
plt = Plotter(axes=1, bg2='lightblue')
Expand Down
14 changes: 6 additions & 8 deletions examples/basic/mesh_coloring.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
"""Specify a colors for cells
"""Specify color mapping for cells
and points of a Mesh"""
from vedo import dataurl, Plotter, Mesh

plt = Plotter(N=3, axes=11)

##################################### add a cell array
man1 = Mesh(dataurl+"man_low.vtk").linewidth(0.1)
nv = man1.ncells # nr. of cells
scals = range(nv) # coloring by the index of cell

nv = man1.ncells # nr. of cells
scals = range(nv) # coloring by the index of cell
man1.cmap("Paired", scals, on='cells').add_scalarbar("cell nr")
plt.at(0).show(man1, __doc__, elevation=-60)


##################################### Point coloring
man2 = Mesh(dataurl+"man_low.vtk")
scals = man2.vertices[:, 0] + 37 # pick x coordinates of vertices

scals = man2.vertices[:, 0] + 37 # pick x coordinates of vertices
man2.cmap("hot", scals)
man2.add_scalarbar(horizontal=True)
plt.at(1).show(man2, "mesh.cmap()")


##################################### Cell coloring
man3 = Mesh(dataurl+"man_low.vtk")
scals = man3.cell_centers[:, 2] + 37 # pick z coordinates of cells
scals = man3.cell_centers[:, 2] + 37 # pick z coordinates of cells
man3.cmap("afmhot", scals, on='cells')

# add a fancier 3D scalar bar embedded in the scene
# Add a fancier 3D scalar bar embedded in the 3d scene
man3.add_scalarbar3d(size=[0.2,3])
man3.scalarbar.scale(1.1).rotate_x(90).shift([0,2,0])

Expand Down
8 changes: 3 additions & 5 deletions examples/basic/mesh_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@

# assign color map with specified opacities
try:
import colorcet # https://colorcet.holoviz.org
import colorcet
# https://colorcet.holoviz.org
mycmap = colorcet.bmy
alphas = np.linspace(0.8, 0.2, num=len(mycmap))
except ModuleNotFoundError:
printc("colorcet is not available, use custom cmap", c='y')
printc("pip install colorcet", c='y')
mycmap = ["darkblue", "magenta", (1, 1, 0)]
alphas = [0.8, 0.6, 0.2]

# - OR by generating a palette between 2 colors:
#mycmap = makePalette('pink', 'green', N=500, hsv=True)
#alphas = 1

man.cmap(mycmap, scals, alpha=alphas).add_scalarbar()

show(man, __doc__, viewup="z", axes=7).close()
Expand Down
2 changes: 1 addition & 1 deletion examples/pyplot/embed_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import matplotlib.pyplot as plt
from vedo import *

tmsh = TetMesh(dataurl+"limb_ugrid.vtk")
tmsh = TetMesh(dataurl+"limb.vtu")
msh = tmsh.tomesh().shrink(0.8)

# Create a histogram with matplotlib
Expand Down
10 changes: 7 additions & 3 deletions examples/volumetric/tet_astyle.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Load a tetrahedral mesh and show it in different styles."""
from vedo import *
from vedo import TetMesh, show, dataurl

tetm = TetMesh(dataurl+'limb_ugrid.vtk')
# Load a tetrahedral mesh from file
tetm = TetMesh(dataurl + 'limb.vtu')
tetm.compute_cell_size()
print(tetm)
tetm.cmap("Blues_r", "chem_0", on="cells").add_scalarbar()

# Assign a color to each tetrahedron based on the value of "chem_0"
tetm.celldata.select('chem_0').cmap("Blues_r").add_scalarbar()

# Make a copy of tetm and shrink the tets
msh = tetm.clone().shrink(0.5).tomesh().add_scalarbar()

# Show the two meshes side by side with comments
show([(tetm, __doc__), (msh, "..shrunk tetrahedra")], N=2).close()

23 changes: 15 additions & 8 deletions examples/volumetric/tet_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@
(2, 5, 5),
]

# Cells are defined by a list of 4 vertex indices
# note that "cells" and "tetrahedrons" are the same thing
tets = [[0,1,2,3], [4,5,6,7], [8,9,10,11]]

vals = [10.0, 20.0, 30.0] # some cell scalar values
# Define a scalar value for each cell we have created
values = np.array([10.0, 20.0, 30.0])

# Create the TeTMesh object
# Create the TeTMesh object and assign any number of data arrays to it
tm = TetMesh([points, tets])
tm.celldata["myscalar"] = vals
tm.celldata["myscalar1"] = values
tm.celldata["myscalar2"] = -values / 10
tm.pointdata["myvector"] = np.random.rand(tm.npoints)
# ...

tm.cmap('jet', 'myscalar', on='cells').add_scalarbar()
print(tm)

tm.celldata.select("myscalar2").cmap('jet').add_scalarbar()
# tm.color('green') # or set a single color

printc("tetmesh.dataset:", type(tm)) # vedo.tetmesh.TetMesh
printc("#vertices :", tm.vertices.size)
printc("#cells :", len(tm.cells))
# Create labels for the vertices
labels = tm.labels2d('id', scale=2)

show(tm, axes=1).close()
show(tm, labels, __doc__, axes=1).close()
5 changes: 3 additions & 2 deletions examples/volumetric/tet_cut1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Cut a TetMesh with an arbitrary polygonal Mesh"""
from vedo import *

sphere = Sphere(r=500).x(400).color('green5',0.2).wireframe()
sphere = Sphere(r=500).x(400)
sphere.color('green5', 0.2).wireframe()

tmesh = TetMesh(dataurl + 'limb_ugrid.vtk')
tmesh = TetMesh(dataurl + 'limb.vtu')
print(tmesh)

ugrid = tmesh.cut_with_mesh(sphere, invert=True).cmap("Reds_r")
Expand Down
7 changes: 4 additions & 3 deletions examples/volumetric/tet_cut2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

sphere = Sphere(r=500).x(400).c('green', 0.1)

tetm1 = TetMesh(dataurl+'limb_ugrid.vtk')
tetm1 = TetMesh(dataurl+'limb.vtu')
tetm1.cmap('jet', tetm1.vertices[:, 2], name="ProximoDistal")

# Clone and cut the TetMesh, this returns a UnstructuredGrid:
Expand All @@ -21,12 +21,13 @@

# Cut tetm, but the output will keep only the tets on the boundary:
ugrid3 = tetm1.clone().cut_with_mesh(sphere, on_boundary=True)
tetm3 = TetMesh(ugrid3).cmap("Reds", "chem_0", on="cells")
tetm3 = TetMesh(ugrid3)
tetm3.celldata.select("chem_0").cmap("Reds")
print(tetm3)

show([
(ugrid1,sphere, __doc__),
(tetm2, sphere, "Keep only tets that lie\ncompletely outside the Sphere"),
(tetm2, sphere, "Keep only tets that lie\ncompletely outside of the Sphere"),
(tetm3, sphere, "Keep only tets that lie\nexactly on the Sphere"),
],
N=3, axes=dict(xtitle='x in :mum'),
Expand Down
2 changes: 1 addition & 1 deletion examples/volumetric/tet_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
f1 = 0.005 # control the tetras resolution
f2 = 0.15 # control the nr of seeds

tmesh = TetMesh(dataurl + "limb_ugrid.vtk")
tmesh = TetMesh(dataurl + "limb.vtu")
surf = tmesh.tomesh(fill=False)
txt = Text2D(__doc__, font="Brachium")

Expand Down
7 changes: 5 additions & 2 deletions examples/volumetric/tet_isos_slice.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Thresholding and slicing a TetMesh
from vedo import TetMesh, dataurl, show

tmsh = TetMesh(dataurl+'limb_ugrid.vtk').color('Spectral')
tmsh.cmap('hot').add_scalarbar3d('chem_0 expression', c='k')
tmsh = TetMesh(dataurl+'limb.vtu')
print(tmsh)

tmsh.celldata.select('chem_0').cmap('hot')
tmsh.add_scalarbar3d('chem_0 expression', c='k')

vals = [0.2, 0.3, 0.8]
isos = tmsh.isosurface(vals)
Expand Down
7 changes: 4 additions & 3 deletions examples/volumetric/tet_threshold.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Threshold a TetMesh with a scalar array"""
from vedo import *

tetm = TetMesh(dataurl+'limb_ugrid.vtk')
tetm = TetMesh(dataurl + "limb.vtu")

# Threshold the tetrahedral mesh for values in the range:
tetm.threshold(above=0.9, below=1)
tetm.cmap('Accent', 'chem_0', on='cells')
tetm.add_scalarbar3d('chem_0 expression levels', c='k', italic=1)

tetm.celldata.select("chem_0").cmap("Accent")
tetm.add_scalarbar3d("chem_0 expression levels", c="k", italic=1)

show(tetm, __doc__, axes=1).close()
1 change: 1 addition & 0 deletions tests/test_pipeline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ cd ~/Projects/napari-vedo-bridge
# conda create -y -n napari-env -c conda-forge python=3.9
# conda activate napari-env
# python -m pip install "napari[all]"
# cd ../vedo && pip install . && cd -
conda activate napari-env
python $VDIR/examples/other/napari1.py
napari
Expand Down
7 changes: 5 additions & 2 deletions vedo/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,11 @@ def printc(
"""

if not vedo.settings.enable_print_color or not _terminal_has_colors:
print(*strings, end=end, flush=flush)
return
if return_string:
return ''.join(strings)
else:
print(*strings, end=end, flush=flush)
return

try: # -------------------------------------------------------------

Expand Down
Loading

0 comments on commit 37b5e3b

Please sign in to comment.