klampt.vis.colorize module
Colorize an object to show heatmaps, false color images, etc.
- klampt.vis.colorize.colorize(object, value, colormap=None, feature=None, vrange=None, lighting=None)[source]
Colorizes an object according to some value. Useful for making heatmaps, false color images, etc. Can only be used with point clouds and triangle meshes.
Examples:
colorize(point_cloud,'z','plasma')
: sets the point cloud to a rainbow colorization (the ‘plasma’ colormap in Matplotlib) depending on z.colorize(trimesh,'nz')
: colorizes the trimesh according to the z component of each triangle normal.colorize(trimesh,[v1_val,v2_val,...,vm_val])
: colorizes each vertex of the triangle mesh by the default colormap. (here m is the # of vertices)colorize(trimesh,[t1_rgb,t2_rgb,...,tn_rgb])
: colorizes each triangle of the triangle mesh to an assigned color. (here n is the # of triangles)colorize(trimesh,'index','random',feature='vertices')
: assigns random vertex colors.colorize(trimesh,segments,'random')
: if clusters is a list of segment IDs (len(segments) = # triangles), this assigns a random color to each segment.colorize(trimesh,segments,'random',lighting=[0,0,-1])
: similar to above, but each triangle is shaded as though the scene was lighted by a downward-facing directional light (noonday sun).colorize(heightmap,'z','plasma')
: sets the heightmap to a rainbow colorization (the ‘plasma’ colormap in Matplotlib) depending on z.colorize(heightmap,'segment','random')
: if heightmap has a property named ‘segment’ mapping each point to a segment id, this assigns a random color to each segment.
- Parameters:
object –
either an object with both an
appearance()
andgeometry()
method, aGeometry3D
, aPointCloud
, aHeightmap
, or anAppearance
.In the first case, the associated appearance is updated.
For Geometry3Ds, the return value is an Appearance that can be used via Appearance.drawGL(object).
For PointClouds, color attributes are added as an ‘rgb’ or ‘rgba’ channel, if color information is not already present.
For Heightmaps, color attributes are added to the color attribute.
For Appearances, the face/vertex colors are assigned. Note that
feature
cannot be “auto”.
value (str or list) –
a named feature of the geometry or a list of values or RGB/RGBA colors. Valid named features include:
’p’ or ‘position’: assigns RGB color by position
’n’ or ‘normal’: assigns RGB color by normal (range [-1,1] if vrange=None)
’x’: assigns value by position x
’y’: assigns value by position y
’z’: assigns value by position z
’nx’: assigns value by normal x (range [-1,1] if vrange=None)
’ny’: assigns value by normal y (range [-1,1] if vrange=None)
’nz’: assigns value by normal x (range [-1,1] if vrange=None)
’index’: assigns value by feature index
’height’: assigns value by heightmap height
Any point cloud / heightmap property name: assigns value by property value.
If this is a list, its length should equal the number of features (either vertices or faces) in the object’s geometry.
colormap (str, optional) – either a Matplotlib colormap identifier or ‘random’. By default, ‘viridis’ is used. If ‘random’ is used, a random color is assigned to each unique value.
feature (str, optional) – if this cannot determine whether to use the vertices or faces of a triangle mesh,
feature
indicates which feature to use (either ‘vertices’ or ‘faces’).vrange (pair, optional) – if values are 1D, this maps the value range to u=(v-vrange[0])/(vrange[1]-vrange[0]) before passing to the colormap. If not provided, the range is determined automatically by the min/max of values.
lighting (3-list or callable, optional) – if a 3-list, this is interpreted as the direction of a directional light. The items are shaded as though the light were illuminating the scene. If it’s a callable, this is a function
f(p,n)
of the position and normal of a point or triangle, which should return a value in the range [0,1] indicating the overall brightness of the item.