question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Line segments not coloured correctly with data read in from VTU files

See original GitHub issue

Hi @marcomusy,

Another VTU-related problem. I was trying to plot line segments with different colors used to denote different lithology units as revealed from drilling. The original data was prepared into vtu files with segments as cells. The following picture was originally plotted using PyVista:

test_knots_igen=020

Not something I am satisfied with but at least it is able to show segments with different colors. I then tried to plot something similar using vedo and here’s what I got:

surface_input

As you can see, the segments are not properly colored as expected although the colorbar was showing correctly. Any ideas on how to get the expected picture plotted? Thanks very much!

Here are all the data needed to plot the picture: vedo_issue.zip

The following code was used to plot the figure:

from vedo import *
import numpy as np

settings.defaultFont = 'Theemim'
settings.multiSamples=8

# Plot the two drillholes
l2400_vtu = 'L2400E.vtu'
l3200_vtu = 'L3200E.vtu'

l2400 = UGrid(l2400_vtu).tomesh().lineWidth(20).lc('w')
l3200 = UGrid(l3200_vtu).tomesh().lineWidth(20)

# Lookup table for drillhole lithology
lut_table = [
    # Value, color, alpha, category
    (1., 'gray', 1, 'Overburden'),
    (2., 'bronw', 1, 'Fault'),
    (3., 'red', 1, 'Fault (graphite)'),
    (4., 'green', 1, 'Basement'),
]
lut = buildLUT(lut_table)
l2400.cmap(lut, 'Lithology', on='cells')
l3200.cmap(lut, 'Lithology', on='cells')
l2400.addScalarBar3D(
    categories=lut_table,
    title="Lithology",
    labelSize=2,
    pos=(606500, 6371000, -50),
    sx=40,
    sy=1200,
)
# l3200.addScalarBar3D(
#     categories=lut_table,
#     title="Lithology",
#     labelSize=2,
#     pos=(606500, 6371500, -50),
# )

# Create a UGrid object from surface_input.vtu file
igen = 20
vtu_file = 'test_knots_igen=' + '{:03d}'.format(igen) + '.vtu'
ug_cell = UGrid(vtu_file)
ug_node = ug_cell.clone()

# Get the mesh objects for both cells and nodes
mesh_cell = ug_cell.tomesh().lineWidth(5).lineColor('blue').alpha(0.5)
mesh_cell.cmap("coolwarm", 'names001', on='cells')
mesh_cell.addScalarBar3D(
    title='Cell conductivity (S/m)',
    titleXOffset=-2.5,
    labelSize=1.5,
    pos=(603550, 6371500, -50),
    sx=30,
    sy=1200,
)

mesh_node = ug_node.tomesh()

# We need to build a look up table for our color bar, and now it supports
# using category names as labels instead of the numerical values
# This was implemented upon my request
lut_table = [
    # Value, color, alpha, category
    (1.2, 'cyan', 1, r'\pm200'),
    (2.2, 'red', 1, '\pm200'),
    (3.2, 'dodgerblue', 1, '\pm400'),
    (4.2, 'purple', 1, '\pm500'),
    (5.2, 'green', 1, '\pm150'),
]
lut = buildLUT(lut_table)

# Adde color maps to both nodes and cells
mesh_node.cmap(lut, 'nodeGroup', on='points')
mesh_node.pointSize(25)

# Options for axes
group = mesh_node + mesh_cell
axes = Axes(group,
            xtitle='Easting (m)',
            ytitle='Northing (m)',
            ztitle='Elevation (m)',
            xTitlePosition=0.55,
            xLabelSize=0.018,
            yTitlePosition=0.65,
            yLabelRotation=-90,
            yLabelSize=0.018,
            # yLabelOffset=-1.5,
            # yTitleOffset=-1.12,
            yTitleRotation=180,
            axesLineWidth=4,
            xrange=mesh_cell.xbounds(),
            yrange=mesh_cell.ybounds(),
            # yzShift=1,
            zxGrid=True,
            )
# manually move the Z axis back in place
for a in axes.unpack():
    # print(a.name)
    if ("zAxis" in a.name or "zM" in a.name or "zN" in a.name
        or "zxGrid" in a.name or "zTipCone" in a.name or 'ztitle' in a.name):
        yb = group.ybounds()
        a.shift(0, (yb[1]-yb[0]), 0)

mesh_node.addScalarBar3D(
    categories=lut_table,
    title='Search volumes (m)',
    titleRotation=180,
    titleXOffset=-4,
    labelSize=2,
    labelOffset=1.5,
    labelRotation=-90,
    pos=(606000, 6371000, -50),
    sx=40,
    sy=1200,
)
# put scalarbar vertical and shift it up a bit
mesh_node.scalarbar.rotateZ(90, locally=True).shift(-1200, -1000, 0)
# mesh_node.scalarbar.shift(-1000, -500, 0)

# Also rotate the scalarbar showing cell conductivity
mesh_cell.scalarbar.rotateX(90, locally=True).rotateY(-45, locally=True).rotateZ(-15, locally=True)

plt = Plotter()
plt.camera.SetPosition( [602592.516, 6368179.979, 2912.739] )
plt.camera.SetFocalPoint( [604701.971, 6370768.195, -30.009] )
plt.camera.SetViewUp( [0.472, 0.466, 0.748] )
plt.camera.SetDistance( 4450.667 )
plt.camera.SetClippingRange( [2453.388, 6975.564] )
# Define the window size in pixels
# size = [3940, 2160]
size = [2560, 1600]
# plt.show(mesh_node, mesh_cell, axes, l2400, l3200, size=size, zoom=1.2,
#          interactive=False)
plt.show(mesh_node, mesh_cell, axes, l2400, l3200, size=size,
         interactive=True)

# filename = 'surface_input_no_point_size.png'
filename = 'surface_input.png'
screenshot(filename)

from wand import image

with image.Image(filename=filename) as imag:
    imag.trim(color=None, fuzz=0)
    imag.save(filename=filename)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
marcomusycommented, Aug 17, 2021

Cool,

The reason why some of the edges disappeared was because of VTK? As long as there is a solution to it, I can keep using it.

Yes. Sadly VTK starting from version 8.2 suffers of artifacts when plotting lines and transparent objects. Shrinking a bit the triangles is probably helping with that…

The program for the above rendering is this:

#!/usr/bin/env python3
#
from vedo import *

settings.defaultFont = 'Theemim'
settings.multiSamples= 8
wsize = [2060, 1200]

# Plot the two drillholes
l2400_vtu = 'L2400E.vtu'
l3200_vtu = 'L3200E.vtu'

# Lookup table for drillhole lithology
lut_table = [
    # Value, color, alpha, category
    (1., 'gray',  1, 'Overburden'),
    (2., 'bronw', 1, 'Fault'),
    (3., 'red',   1, 'Fault\n(graphite)'),
    (4., 'green', 1, 'Basement'),
]
lut = buildLUT(lut_table)

l2400 = UGrid(l2400_vtu).tomesh().clean().lineWidth(20)
l2400.cmap(lut, 'Lithology', on='cells')
l2400.property.RenderLinesAsTubesOn()
l2400.addScalarBar3D(
    categories=lut_table,
    title="Lithology",
    titleSize=2,
    titleRotation=180,
    titleXOffset=-2.,
    labelSize=2,
    pos=(606300, 6371000, -50),
    sx=60,
    sy=800,
)
l2400.scalarbar.rotateZ(-45, locally=True).rotateX(45, locally=True)

l3200 = UGrid(l3200_vtu).tomesh().clean().lineWidth(20)
l3200.cmap(lut, 'Lithology', on='cells')
l3200.property.RenderLinesAsTubesOn()
# no need of scalarbar here

# Create a UGrid object from surface_input.vtu file
igen = 20
vtu_file = 'test_knots_igen=' + '{:03d}'.format(igen) + '.vtu'
ug_cell = UGrid(vtu_file)
ug_node = ug_cell.clone()

# Get the mesh objects for both cells and nodes
mesh_cell = ug_cell.tomesh(shrink=0.85).clean().alpha(0.6) #see below
mesh_cell.cmap("coolwarm", 'names001', on='cells').lighting("off")
mesh_cell.addScalarBar3D(
    title='Cell conductivity (S/m)',
    titleSize=2,
    titleRotation=180,
    titleXOffset=-3,
    labelSize=1.5,
    pos=(603550, 6371000, 0),
    sx=40,
    sy=1000,
)

# overlay the lines manually because vtk doesn't do a good job with anitialiasing
mesh_wire = Mesh(ug_cell.tomesh().clean()).wireframe()
mesh_wire.lineWidth(3).lineColor('blue4').lighting("off")

# We need to build a look up table for our color bar, and now it supports
# using category names as labels instead of the numerical values
lut_table = [
    # Value, color, alpha, category
    (1.2, 'cyan', 1, r'\pm200'),
    (2.2, 'red', 1, '\pm200'),
    (3.2, 'dodgerblue', 1, '\pm400'),
    (4.2, 'purple', 1, '\pm500'),
    (5.2, 'green', 1, '\pm150'),
]
lut = buildLUT(lut_table)

mesh_node = ug_node.tomesh().clean()

# Add color maps to both nodes and cells
mesh_node.cmap(lut, 'nodeGroup', on='points').pointSize(25)
mesh_node.addScalarBar3D(
    categories=lut_table,
    title='Search volumes (m)',
    titleRotation=180,
    titleXOffset=-4,
    labelSize=2,
    labelOffset=1.5,
    labelRotation=-90,
    pos=(606000, 6371000, -50),
    sx=40,
    sy=1200,
)
# put scalarbar vertical and shift it up a bit
mesh_node.scalarbar.rotateZ(90, locally=True).shift(-1200, -1000, 0)

# Options for axes
group = mesh_node + mesh_cell
axes = Axes(group,
            xtitle='Easting (m)',
            ytitle='Northing (m)',
            ztitle='Elevation (m)',
            xTitlePosition=0.55,
            xLabelSize=0.018,
            yTitlePosition=0.65,
            yLabelRotation=-90,
            yLabelSize=0.018,
            yTitleRotation=180,
            zAxisRotation=-40,
            axesLineWidth=4,
            xrange=mesh_cell.xbounds(),
            yrange=mesh_cell.ybounds(),
            zShiftAlongY=1.0,
            zxGrid2=True,
            xyFrameLine=True,
            gridLineWidth=2,
)

plt = Plotter(size=wsize)
plt.camera.SetPosition( [602867.008, 6368629.132, 2610.594] )
plt.camera.SetFocalPoint( [604678.698, 6370852.003, 83.235] )
plt.camera.SetViewUp( [0.446, 0.489, 0.75] )
plt.camera.SetDistance( 3822.423 )
plt.show(mesh_node, mesh_cell, mesh_wire, l2400, l3200, axes, resetcam=False)
1reaction
XushanLucommented, Aug 17, 2021

Ah, yes! That should be a cmap thing. Tried it and it surely works. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

VTK file (*.vtu) not being correctly read - Stack Overflow
The answer for anyone who may come across this is rather trivial - the <DataArray> attribute should be NumberOfComponents .
Read more >
VTK file (*.vtu) not being correctly read - ParaView Support
Hi, I'm having a problem with creating VTK data files for use with ParaView. I'm writing out point data as an unstructured grid...
Read more >
vtk-examples
Create a solid colored triangle. TubeFilter, Give lines a thickness (produce a cylinder around lines. VectorFieldNonZeroExtraction, Extract non- ...
Read more >
GMS:Bugfixes GMS - XMS Wiki
11970 CLN inputs incorrectly read by h5 version of MODFLOW-USG. The FELEV input from the CLN is not correctly echoed in the OUT...
Read more >
38 questions with answers in PARAVIEW | Science topic
It draws the sphere, but my particles are in the box (VMD reads the xyz file correctly, but i cannot add a color)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found