# ColouredTriMesh¶

class menpo.shape.ColouredTriMesh(points, trilist=None, colours=None, copy=True)[source]

Bases: TriMesh

Combines a TriMesh with a colour per vertex.

Parameters: points ((n_points, n_dims) ndarray) – The array representing the points. trilist ((M, 3) ndarray or None, optional) – The triangle list. If None, a Delaunay triangulation of the points will be used instead. colours ((N, 3) ndarray, optional) – The floating point RGB colour per vertex. If not given, grey will be assigned to each vertex. copy (bool, optional) – If False, the points, trilist and colours will not be copied on assignment. In general this should only be used if you know what you are doing. ValueError – If the number of colour values does not match the number of vertices.
_view_2d(figure_id=None, new_figure=False, image_view=True, render_lines=True, line_colour='r', line_style='-', line_width=1.0, render_markers=True, marker_style='o', marker_size=5, marker_face_colour='k', marker_edge_colour='k', marker_edge_width=1.0, render_numbering=False, numbers_horizontal_align='center', numbers_vertical_align='bottom', numbers_font_name='sans-serif', numbers_font_size=10, numbers_font_style='normal', numbers_font_weight='normal', numbers_font_colour='k', render_axes=True, axes_font_name='sans-serif', axes_font_size=10, axes_font_style='normal', axes_font_weight='normal', axes_x_limits=None, axes_y_limits=None, axes_x_ticks=None, axes_y_ticks=None, figure_size=(10, 8), label=None)[source]

Visualization of the TriMesh in 2D. Currently, explicit coloured TriMesh viewing is not supported, and therefore viewing falls back to uncoloured 2D TriMesh viewing.

Returns: figure_id (object, optional) – The id of the figure to be used. new_figure (bool, optional) – If True, a new figure is created. image_view (bool, optional) – If True the ColouredTriMesh will be viewed as if it is in the image coordinate system. render_lines (bool, optional) – If True, the edges will be rendered. line_colour (See Below, optional) – The colour of the lines. Example options:{r, g, b, c, m, k, w} or (3, ) ndarray  line_style ({-, --, -., :}, optional) – The style of the lines. line_width (float, optional) – The width of the lines. render_markers (bool, optional) – If True, the markers will be rendered. marker_style (See Below, optional) – The style of the markers. Example options{., ,, o, v, ^, <, >, +, x, D, d, s, p, *, h, H, 1, 2, 3, 4, 8}  marker_size (int, optional) – The size of the markers in points. marker_face_colour (See Below, optional) – The face (filling) colour of the markers. Example options{r, g, b, c, m, k, w} or (3, ) ndarray  marker_edge_colour (See Below, optional) – The edge colour of the markers. Example options{r, g, b, c, m, k, w} or (3, ) ndarray  marker_edge_width (float, optional) – The width of the markers’ edge. render_numbering (bool, optional) – If True, the landmarks will be numbered. numbers_horizontal_align ({center, right, left}, optional) – The horizontal alignment of the numbers’ texts. numbers_vertical_align ({center, top, bottom, baseline}, optional) – The vertical alignment of the numbers’ texts. numbers_font_name (See Below, optional) – The font of the numbers. Example options{serif, sans-serif, cursive, fantasy, monospace}  numbers_font_size (int, optional) – The font size of the numbers. numbers_font_style ({normal, italic, oblique}, optional) – The font style of the numbers. numbers_font_weight (See Below, optional) – The font weight of the numbers. Example options{ultralight, light, normal, regular, book, medium, roman, semibold, demibold, demi, bold, heavy, extra bold, black}  numbers_font_colour (See Below, optional) – The font colour of the numbers. Example options{r, g, b, c, m, k, w} or (3, ) ndarray  render_axes (bool, optional) – If True, the axes will be rendered. axes_font_name (See Below, optional) – The font of the axes. Example options{serif, sans-serif, cursive, fantasy, monospace}  axes_font_size (int, optional) – The font size of the axes. axes_font_style ({normal, italic, oblique}, optional) – The font style of the axes. axes_font_weight (See Below, optional) – The font weight of the axes. Example options{ultralight, light, normal, regular, book, medium, roman, semibold, demibold, demi, bold, heavy, extra bold, black}  axes_x_limits (float or (float, float) or None, optional) – The limits of the x axis. If float, then it sets padding on the right and left of the TriMesh as a percentage of the TriMesh’s width. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically. axes_y_limits ((float, float) tuple or None, optional) – The limits of the y axis. If float, then it sets padding on the top and bottom of the TriMesh as a percentage of the TriMesh’s height. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically. axes_x_ticks (list or tuple or None, optional) – The ticks of the x axis. axes_y_ticks (list or tuple or None, optional) – The ticks of the y axis. figure_size ((float, float) tuple or None, optional) – The size of the figure in inches. label (str, optional) – The name entry in case of a legend. viewer (PointGraphViewer2d) – The viewer object. warning – 2D Viewing of Coloured TriMeshes is not supported, automatically falls back to 2D TriMesh viewing.
_view_landmarks_2d(group=None, with_labels=None, without_labels=None, figure_id=None, new_figure=False, image_view=True, render_lines=True, line_colour=None, line_style='-', line_width=1, render_markers=True, marker_style='o', marker_size=5, marker_face_colour=None, marker_edge_colour=None, marker_edge_width=1.0, render_numbering=False, numbers_horizontal_align='center', numbers_vertical_align='bottom', numbers_font_name='sans-serif', numbers_font_size=10, numbers_font_style='normal', numbers_font_weight='normal', numbers_font_colour='k', render_legend=False, legend_title='', legend_font_name='sans-serif', legend_font_style='normal', legend_font_size=10, legend_font_weight='normal', legend_marker_scale=None, legend_location=2, legend_bbox_to_anchor=(1.05, 1.0), legend_border_axes_pad=None, legend_n_columns=1, legend_horizontal_spacing=None, legend_vertical_spacing=None, legend_border=True, legend_border_padding=None, legend_shadow=False, legend_rounded_corners=False, render_axes=False, axes_font_name='sans-serif', axes_font_size=10, axes_font_style='normal', axes_font_weight='normal', axes_x_limits=None, axes_y_limits=None, axes_x_ticks=None, axes_y_ticks=None, figure_size=(10, 8))

Visualize the landmarks. This method will appear on the Image as view_landmarks if the Image is 2D.

Parameters:
• group (str orNone optional) – The landmark group to be visualized. If None and there are more than one landmark groups, an error is raised.
• with_labels (None or str or list of str, optional) – If not None, only show the given label(s). Should not be used with the without_labels kwarg.
• without_labels (None or str or list of str, optional) – If not None, show all except the given label(s). Should not be used with the with_labels kwarg.
• figure_id (object, optional) – The id of the figure to be used.
• new_figure (bool, optional) – If True, a new figure is created.
• image_view (bool, optional) – If True the PointCloud will be viewed as if it is in the image coordinate system.
• render_lines (bool, optional) – If True, the edges will be rendered.
• line_colour (See Below, optional) –

The colour of the lines. Example options:

{r, g, b, c, m, k, w}
or
(3, ) ndarray

• line_style ({-, --, -., :}, optional) – The style of the lines.
• line_width (float, optional) – The width of the lines.
• render_markers (bool, optional) – If True, the markers will be rendered.
• marker_style (See Below, optional) –

The style of the markers. Example options

{., ,, o, v, ^, <, >, +, x, D, d, s, p, *, h, H, 1, 2, 3, 4, 8}

• marker_size (int, optional) – The size of the markers in points.
• marker_face_colour (See Below, optional) –

The face (filling) colour of the markers. Example options

{r, g, b, c, m, k, w}
or
(3, ) ndarray

• marker_edge_colour (See Below, optional) –

The edge colour of the markers. Example options

{r, g, b, c, m, k, w}
or
(3, ) ndarray

• marker_edge_width (float, optional) – The width of the markers’ edge.
• render_numbering (bool, optional) – If True, the landmarks will be numbered.
• numbers_horizontal_align ({center, right, left}, optional) – The horizontal alignment of the numbers’ texts.
• numbers_vertical_align ({center, top, bottom, baseline}, optional) – The vertical alignment of the numbers’ texts.
• numbers_font_name (See Below, optional) –

The font of the numbers. Example options

{serif, sans-serif, cursive, fantasy, monospace}

• numbers_font_size (int, optional) – The font size of the numbers.
• numbers_font_style ({normal, italic, oblique}, optional) – The font style of the numbers.
• numbers_font_weight (See Below, optional) –

The font weight of the numbers. Example options

{ultralight, light, normal, regular, book, medium, roman,
semibold, demibold, demi, bold, heavy, extra bold, black}

• numbers_font_colour (See Below, optional) –

The font colour of the numbers. Example options

{r, g, b, c, m, k, w}
or
(3, ) ndarray

• render_legend (bool, optional) – If True, the legend will be rendered.
• legend_title (str, optional) – The title of the legend.
• legend_font_name (See below, optional) –

The font of the legend. Example options

{serif, sans-serif, cursive, fantasy, monospace}

• legend_font_style ({normal, italic, oblique}, optional) – The font style of the legend.
• legend_font_size (int, optional) – The font size of the legend.
• legend_font_weight (See Below, optional) –

The font weight of the legend. Example options

{ultralight, light, normal, regular, book, medium, roman,
semibold, demibold, demi, bold, heavy, extra bold, black}

• legend_marker_scale (float, optional) – The relative size of the legend markers with respect to the original
• legend_location (int, optional) –

The location of the legend. The predefined values are:

 ‘best’ 0 ‘upper right’ 1 ‘upper left’ 2 ‘lower left’ 3 ‘lower right’ 4 ‘right’ 5 ‘center left’ 6 ‘center right’ 7 ‘lower center’ 8 ‘upper center’ 9 ‘center’ 10
• legend_bbox_to_anchor ((float, float) tuple, optional) – The bbox that the legend will be anchored.
• legend_border_axes_pad (float, optional) – The pad between the axes and legend border.
• legend_n_columns (int, optional) – The number of the legend’s columns.
• legend_horizontal_spacing (float, optional) – The spacing between the columns.
• legend_vertical_spacing (float, optional) – The vertical space between the legend entries.
• legend_border (bool, optional) – If True, a frame will be drawn around the legend.
• legend_border_padding (float, optional) – The fractional whitespace inside the legend border.
• legend_shadow (bool, optional) – If True, a shadow will be drawn behind legend.
• legend_rounded_corners (bool, optional) – If True, the frame’s corners will be rounded (fancybox).
• render_axes (bool, optional) – If True, the axes will be rendered.
• axes_font_name (See Below, optional) –

The font of the axes. Example options

{serif, sans-serif, cursive, fantasy, monospace}

• axes_font_size (int, optional) – The font size of the axes.
• axes_font_style ({normal, italic, oblique}, optional) – The font style of the axes.
• axes_font_weight (See Below, optional) –

The font weight of the axes. Example options

{ultralight, light, normal, regular, book, medium, roman,
semibold,demibold, demi, bold, heavy, extra bold, black}

• axes_x_limits (float or (float, float) or None, optional) – The limits of the x axis. If float, then it sets padding on the right and left of the PointCloud as a percentage of the PointCloud’s width. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
• axes_y_limits ((float, float) tuple or None, optional) – The limits of the y axis. If float, then it sets padding on the top and bottom of the PointCloud as a percentage of the PointCloud’s height. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
• axes_x_ticks (list or tuple or None, optional) – The ticks of the x axis.
• axes_y_ticks (list or tuple or None, optional) – The ticks of the y axis.
• figure_size ((float, float) tuple or None optional) – The size of the figure in inches.
Raises:
• ValueError – If both with_labels and without_labels are passed.
• ValueError – If the landmark manager doesn’t contain the provided group label.
as_pointgraph(copy=True, skip_checks=False)

Converts the TriMesh to a PointUndirectedGraph.

Parameters: copy (bool, optional) – If True, the graph will be a copy. skip_checks (bool, optional) – If True, no checks will be performed. pointgraph (PointUndirectedGraph) – The point graph.
as_vector(**kwargs)

Returns a flattened representation of the object as a single vector.

Returns: vector ((N,) ndarray) – The core representation of the object, flattened into a single vector. Note that this is always a view back on to the original object, but is not writable.
boundary_tri_index()

Boolean index into triangles that are at the edge of the TriMesh

Returns: boundary_tri_index ((n_tris,) ndarray) – For each triangle (ABC), returns whether any of it’s edges is not also an edge of another triangle (and so this triangle exists on the boundary of the TriMesh)
bounding_box()

Return a bounding box from two corner points as a directed graph. The the first point (0) should be nearest the origin. In the case of an image, this ordering would appear as:

0<--3
|   ^
|   |
v   |
1-->2


In the case of a pointcloud, the ordering will appear as:

3<--2
|   ^
|   |
v   |
0-->1

Returns: bounding_box (PointDirectedGraph) – The axis aligned bounding box of the PointCloud.
bounds(boundary=0)

The minimum to maximum extent of the PointCloud. An optional boundary argument can be provided to expand the bounds by a constant margin.

Parameters: boundary (float) – A optional padding distance that is added to the bounds. Default is 0, meaning the max/min of tightest possible containing square/cube/hypercube is returned. min_b ((n_dims,) ndarray) – The minimum extent of the PointCloud and boundary along each dimension max_b ((n_dims,) ndarray) – The maximum extent of the PointCloud and boundary along each dimension
centre()

The mean of all the points in this PointCloud (centre of mass).

Returns: centre ((n_dims) ndarray) – The mean of this PointCloud’s points.
centre_of_bounds()

The centre of the absolute bounds of this PointCloud. Contrast with centre(), which is the mean point position.

Returns: centre (n_dims ndarray) – The centre of the bounds of this PointCloud.
constrain_to_bounds(bounds)

Returns a copy of this PointCloud, constrained to lie exactly within the given bounds. Any points outside the bounds will be ‘snapped’ to lie exactly on the boundary.

Parameters: bounds ((n_dims, n_dims) tuple of scalars) – The bounds to constrain this pointcloud within. constrained (PointCloud) – The constrained pointcloud.
copy()

Generate an efficient copy of this object.

Note that Numpy arrays and other Copyable objects on self will be deeply copied. Dictionaries and sets will be shallow copied, and everything else will be assigned (no copy will be made).

Classes that store state other than numpy arrays and immutable types should overwrite this method to ensure all state is copied.

Returns: type(self) – A copy of this object
distance_to(pointcloud, **kwargs)

Returns a distance matrix between this PointCloud and another. By default the Euclidean distance is calculated - see scipy.spatial.distance.cdist for valid kwargs to change the metric and other properties.

Parameters: pointcloud (PointCloud) – The second pointcloud to compute distances between. This must be of the same dimension as this PointCloud. distance_matrix ((n_points, n_points) ndarray) – The symmetric pairwise distance matrix between the two PointClouds s.t. distance_matrix[i, j] is the distance between the i’th point of this PointCloud and the j’th point of the input PointCloud.
edge_indices()

An unordered index into points that rebuilds the edges of this TriMesh.

Note that there will be two edges present in cases where two triangles ‘share’ an edge. Consider unique_edge_indices() for a single index for each physical edge on the TriMesh.

Returns: edge_indices ((n_tris * 3, 2) ndarray) – For each triangle (ABC), returns the pair of point indices that rebuild AB, AC, BC. All edge indices are concatenated for a total of n_tris * 3 edge_indices. The ordering is done so that all AB vectors are first in the returned list, followed by BC, then CA.
edge_lengths()

The length of each edge in this TriMesh.

Note that there will be two edges present in cases where two triangles ‘share’ an edge. Consider unique_edge_indices() for a single index for each physical edge on the TriMesh. The ordering matches the case for edges and edge_indices.

Returns: edge_lengths ((n_tris * 3, ) ndarray) – Scalar euclidean lengths for each edge in this TriMesh.
edge_vectors()

A vector of edges of each triangle face.

Note that there will be two edges present in cases where two triangles ‘share’ an edge. Consider unique_edge_vectors() for a single vector for each physical edge on the TriMesh.

Returns: edges ((n_tris * 3, n_dims) ndarray) – For each triangle (ABC), returns the edge vectors AB, BC, CA. All edges are concatenated for a total of n_tris * 3 edges. The ordering is done so that all AB vectors are first in the returned list, followed by BC, then CA.
from_mask(mask)[source]

A 1D boolean array with the same number of elements as the number of points in the ColouredTriMesh. This is then broadcast across the dimensions of the mesh and returns a new mesh containing only those points that were True in the mask.

Parameters: mask ((n_points,) ndarray) – 1D array of booleans mesh (ColouredTriMesh) – A new mesh that has been masked.
from_tri_mask(tri_mask)

A 1D boolean array with the same number of elements as the number of triangles in the TriMesh. This is then broadcast across the dimensions of the mesh and returns a new mesh containing only those triangles that were True in the mask.

Parameters: mask ((n_tris,) ndarray) – 1D array of booleans mesh (TriMesh) – A new mesh that has been masked by triangles.
from_vector(vector)

Build a new instance of the object from it’s vectorized state.

self is used to fill out the missing state required to rebuild a full object from it’s standardized flattened state. This is the default implementation, which is which is a deepcopy of the object followed by a call to from_vector_inplace(). This method can be overridden for a performance benefit if desired.

Parameters: vector ((n_parameters,) ndarray) – Flattened representation of the object. object (type(self)) – An new instance of this class.
from_vector_inplace(vector)

Deprecated. Use the non-mutating API, from_vector.

For internal usage in performance-sensitive spots, see _from_vector_inplace()

Parameters: vector ((n_parameters,) ndarray) – Flattened representation of this object
h_points()

Convert poincloud to a homogeneous array: (n_dims + 1, n_points)

Type: type(self)
has_nan_values()

Tests if the vectorized form of the object contains nan values or not. This is particularly useful for objects with unknown values that have been mapped to nan values.

Returns: has_nan_values (bool) – If the vectorized object contains nan values.
classmethod init_2d_grid(shape, spacing=None, colours=None)[source]

Create a ColouredTriMesh that exists on a regular 2D grid. The first dimension is the number of rows in the grid and the second dimension of the shape is the number of columns. spacing optionally allows the definition of the distance between points (uniform over points). The spacing may be different for rows and columns.

The triangulation will be right-handed and the diagonal will go from the top left to the bottom right of a square on the grid.

Parameters: shape (tuple of 2 int) – The size of the grid to create, this defines the number of points across each dimension in the grid. The first element is the number of rows and the second is the number of columns. spacing (int or tuple of 2 int, optional) – The spacing between points. If a single int is provided, this is applied uniformly across each dimension. If a tuple is provided, the spacing is applied non-uniformly as defined e.g. (2, 3) gives a spacing of 2 for the rows and 3 for the columns. colours ((N, 3) ndarray, optional) – The floating point RGB colour per vertex. If not given, grey will be assigned to each vertex. trimesh (TriMesh) – A TriMesh arranged in a grid.
classmethod init_from_depth_image(depth_image, colours=None)[source]

Return a 3D textured triangular mesh from the given depth image. The depth image is assumed to represent height/depth values and the XY coordinates are assumed to unit spaced and represent image coordinates. This is particularly useful for visualising depth values that have been recovered from images.

The optionally passed texture will be textured mapped onto the planar surface using the correct texture coordinates for an image of the same shape as depth_image.

Parameters: depth_image (Image or subclass) – A single channel image that contains depth values - as commonly returned by RGBD cameras, for example. colours ((N, 3) ndarray, optional) – The floating point RGB colour per vertex. If not given, grey will be assigned to each vertex. depth_cloud (type(cls)) – A new 3D TriMesh with unit XY coordinates and the given depth values as Z coordinates. The trilist is constructed as in init_2d_grid().
mean_edge_length(unique=True)

The mean length of each edge in this TriMesh.

Parameters: unique (bool, optional) – If True, each shared edge will only be counted once towards the average. If false, shared edges will be counted twice. mean_edge_length (float) – The mean length of each edge in this TriMesh
mean_tri_area()

The mean area of each triangle face in this TriMesh.

Returns: mean_tri_area (float) – The mean area of each triangle face in this TriMesh ValueError – If mesh is not 3D
norm(**kwargs)

Returns the norm of this PointCloud. This is a translation and rotation invariant measure of the point cloud’s intrinsic size - in other words, it is always taken around the point cloud’s centre.

By default, the Frobenius norm is taken, but this can be changed by setting kwargs - see numpy.linalg.norm for valid options.

Returns: norm (float) – The norm of this PointCloud
range(boundary=0)

The range of the extent of the PointCloud.

Parameters: boundary (float) – A optional padding distance that is used to extend the bounds from which the range is computed. Default is 0, no extension is performed. range ((n_dims,) ndarray) – The range of the PointCloud extent in each dimension.
tojson()

Convert this TriMesh to a dictionary representation suitable for inclusion in the LJSON landmark format. Note that this enforces a simpler representation, and as such is not suitable for a permanent serialization of a TriMesh (to be clear, TriMesh‘s serialized as part of a landmark set will be rebuilt as a PointUndirectedGraph).

Returns: json (dict) – Dictionary with points and connectivity keys.
tri_areas()

The area of each triangle face.

Returns: areas ((n_tris,) ndarray) – Area of each triangle, ordered as the trilist is ValueError – If mesh is not 2D or 3D
tri_normals()

Compute the triangle face normals from the current set of points and triangle list. Only valid for 3D dimensional meshes.

Returns: normals ((n_tris, 3) ndarray) – Normal at each triangle face. ValueError – If mesh is not 3D
unique_edge_indices()

An unordered index into points that rebuilds the unique edges of this TriMesh.

Note that each physical edge will only be counted once in this method (i.e. edges shared between neighbouring triangles are only counted once not twice). The ordering should be considered random.

Returns: unique_edge_indices ((n_unique_edges, 2) ndarray) – Return a point index that rebuilds all edges present in this TriMesh only once.
unique_edge_lengths()

The length of each edge in this TriMesh.

Note that each physical edge will only be counted once in this method (i.e. edges shared between neighbouring triangles are only counted once not twice). The ordering should be considered random.

Returns: edge_lengths ((n_tris * 3, ) ndarray) – Scalar euclidean lengths for each edge in this TriMesh.
unique_edge_vectors()

An unordered vector of unique edges for the whole TriMesh.

Note that each physical edge will only be counted once in this method (i.e. edges shared between neighbouring triangles are only counted once not twice). The ordering should be considered random.

Returns: unique_edge_vectors ((n_unique_edges, n_dims) ndarray) – Vectors for each unique edge in this TriMesh.
vertex_normals()

Compute the per-vertex normals from the current set of points and triangle list. Only valid for 3D dimensional meshes.

Returns: normals ((n_points, 3) ndarray) – Normal at each point. ValueError – If mesh is not 3D
view_widget(browser_style='buttons', figure_size=(10, 8), style='coloured')

Visualization of the TriMesh using an interactive widget.

Parameters: browser_style ({'buttons', 'slider'}, optional) – It defines whether the selector of the objects will have the form of plus/minus buttons or a slider. figure_size ((int, int) tuple, optional) – The initial size of the rendered figure. style ({'coloured', 'minimal'}, optional) – If 'coloured', then the style of the widget will be coloured. If minimal, then the style is simple using black and white colours.
has_landmarks

Whether the object has landmarks.

Type: bool
landmarks

The landmarks object.

n_dims

The number of dimensions in the pointcloud.

Type: int
n_landmark_groups

The number of landmark groups on this object.

Type: int
n_parameters

The length of the vector that this object produces.

Type: int
n_points

The number of points in the pointcloud.

Type: int
n_tris

The number of triangles in the triangle list.

Type: int