Vedo: Coloring faces of imported meshes

Created on 8 Jul 2018  路  19Comments  路  Source: marcomusy/vedo

Hi @marcomusy,
I am trying to use your library to visualize data from thermal comfort studies. I have several manikins like the one shown below and I intend to color individual polygons based on the amount of thermal radiation incident on them (as shown in the screencapture in the second picture)
vtk
picture1
The part about loading the geometry is super simple due to the methods in your vtkPlotter class. I could not find a way to interact with the individual faces of the obj through any of the methods. I also tried to understand the methods and attributes available at runtime through the debugger in Pycharm but could not get much far. Any suggestions?

Thanks,
Sarith

help wanted testing-phase

All 19 comments

Hi
I'm adding now this functionality and let you know asap
M

@marcomusy Thanks. Please let me know if I can help out in any way. I just got started with VTK but I do have some experience in Python and data visualization.

please have a look at examples/mesh_coloring.py

mcol

This is pretty incredible!!

I just ran your example file with some of my manikins and it works great..
2018-07-09 15_16_12-window
..and just for fun, I tried a more complex model (with nearly 100,000 surfaces and missing geometry) and that loads too!
2018-07-09 15_13_13-window

I think this library could be used to visualize a whole lot of building science data in 3D.
cc: @mostaphaRoudsari

Thanks,
keep in mind you can control the scalarbar to have it horizontal e.i.:
vp.addScalarBar(man3, horizontal=True)

@marcomusy Thanks for the tip. Which API do you to refer to for Python-specifc functionality within VTK? I have come across an API and documentation for the C++ version, however, it appears that not all the classes and methods have been ported over to the Python version.

I typically use the latest version of vtk (8.1 as it comes in anaconda) so I guess all the c++ methods are ported to python, older vtk versions might not have the latest features. I don't think that there is anything like a specific python api for reference...
If you load any mesh at command line you should be able to get a message of which version you are running, e.g.:
./plotter.py anymesh.obj

I don't think that there is anything like a specific python api for reference...

That's the case with most libraries ported to Python, except PyQT. Anyway, thank you for getting this project started. I will circle back in case I come up with more "interesting" queries.

vtk documentation is not very good..
if you have any other questions/query don't hesitate to ask,
cheers
M

data

code:

from vtkplotter import *
import numpy as np

sc = load('scan_c.obj')
data_label = np.load('scan_labels.npy').reshape(-1)
sc.pointColors(data_label, alpha=1, mode='scalars')
show(sc, Text('seg', c='k'), bg='w', interactive=True)

question:

1,2: Shown in OpenFlipper and vtkplotter, difference caused by ?interpolation???
3,4: How to color a single element(vertex or face) without interpolation?
view

PS: In scan.obj, only v and vt are stored, but f indexes to vn. It can be opened in meshlab and OpenFlipper, exception handling should be done, can you handle such abnormal data?

PS: How to set transparent background?

Hi @LogWell please pull the latest commit and try the following:

from vtkplotter import load, show, colors
import numpy as np

sc = load('scan_c.obj')
data_label = np.load('scan_labels.npy').reshape(-1)

# Optionally build a lookup table of colors:
#                     scalar, color
lut = colors.makeLUT([( 0,   'pink'),
                      ( 1,   'green'),
                      ( 3,   'yellow'),
                      ], 
                      N=4, # specify total nr. of colors
                    )

sc.pointColors(data_label, cmap=lut).addScalarBar()

#sc.mapPointsToCells() # this would translate point data to cell data

# avoid interpolating cell colors:
sc.mapper.InterpolateScalarsBeforeMappingOff()
show(sc, bg='w')

image

PS: In scan.obj, only v and vt are stored, but f indexes to vn. It can be opened in meshlab and OpenFlipper, exception handling should be done, can you handle such abnormal data?

Unfortunately vtk seems unable to handle this faulty data..

PS: How to set transparent background?

from vtkplotter import settings
settings.screenshotTransparentBackground = True

NOTE: The red dot in the leftmost yellow circle in the previous image may be caused by abnormal data scan.obj , correct as shown in fig_1, same as in vtkplotter.

Q:

  1. How to color single point rather transm to face, like in fig_2? ref here?
  1. Thanks for your example of using colors.makeLUT. I'm a little confused about setting the correct color in fig_3.

fig

  1. consider the following where points can be overlapped to mesh if you wish:
from vtkplotter import load, show, makeLUT, Points
import numpy as np

sc = load('/home/musy/downloads/scan/scan_c.obj')
data_label = np.load('/home/musy/downloads/scan/scan_labels.npy').reshape(-1)

# build a lookup table of colors:
#              scalar, color
lut = makeLUT([( 0,   'red'),
               ( 1,   'green'),
               ( 3,   'gold'),
               ], 
               N=4, # specify total nr. of colors
              )

sc.pointColors(data_label, cmap=lut).addScalarBar()

# avoid interpolating cell colors:
sc.mapper.InterpolateScalarsBeforeMappingOff()

pts = Points(sc.coordinates()).pointColors(data_label, cmap=lut)

show(sc, pts, N=2, bg='w')

image

  1. make sure you have pulled the latest version from git.
    note that in your scalar file you only have values 0,1,3 and 2 is absent.
    Plus you have a typo "bule" instead of "blue".

Let me know if you can reproduce the above figure.

In fact, there are four labels in total(maybe more). The current obj file only contains three of them (label 2 may represent skirt).

For a batch of data, I can map 0,1,2,3 to four colors, and then color it according to the label information.

@LogWell

I realized that my first try at fixing this was not great.. :) I modified the makeLUT() function, so the above now becomes:

from vtkplotter import load, show, makeLUT, Points
import numpy as np

sc = load('/home/musy/downloads/scan/scan_c.obj')
data_label = np.load('/home/musy/downloads/scan/scan_labels.npy').reshape(-1)

# build a lookup table of colors:
#              scalar, color  alpha
lut = makeLUT([( 0,   'red'       ),
               ( 1,   'green', 0.5),
               ( 3,   'gold'      ),
               ],
              interpolate=False,
              )

sc.pointColors(data_label, cmap=lut).addScalarBar()

# avoid interpolating cell colors:
sc.mapper.InterpolateScalarsBeforeMappingOff()

pts = Points(sc.coordinates()).pointColors(data_label, cmap=lut)

show(sc, pts, N=2, bg='w')

this also includes setting opacities if a 3rd element is given in the list.
Hope this works for you.

Hi, marcomusy~

data

The result seems strange after I subdivided the mesh. It looks a little bit like a flip from the inside view, but the normal of faces are correct in meshlab.

How to solve this problem?

Hi @LogWell
this looks related to a transparency problem in vtk.. though I must say I don't understand exactly why that happens.. This works for me:

from vtkplotter import *

settings.useDepthPeeling = True

m = load('00000.obj').texture('registered_tex_1_m.png')
m.show(axes=4)

@marcomusy How to render a mesh like this(you can distinguish between inside and outside):

20200816_15:26:45
20200816_15:28:34
image

various possibilities..:
if the points are projectable onto a plane (which seems to be the case) you can discard the existing triangles and recreate a new mesh with delaunay2D
or
try to recreate it with recoSurface()
besides
you can use clean(tol=0.01) to preprocess and reduce the points and/or use smoothMLS2D() to make the surface smoother.

finally - once you have the mesh - you can color the outside and inside differently with color() and backColor()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CMengich picture CMengich  路  5Comments

MarkusRosen picture MarkusRosen  路  5Comments

kemo993 picture kemo993  路  6Comments

vianamp picture vianamp  路  3Comments

Yoorthiziri picture Yoorthiziri  路  5Comments