trimesh.points

points.py

Functions dealing with (n, d) points.

class trimesh.points.PointCloud(vertices, colors=None, metadata=None, **kwargs)

Bases: Geometry3D

Hold 3D points in an object which can be visualized in a scene.

__init__(vertices, colors=None, metadata=None, **kwargs)

Load an array of points into a PointCloud object.

Parameters:
  • vertices ((n, 3) float) – Points in space

  • colors ((n, 4) uint8 or None) – RGBA colors for each point

  • metadata (dict or None) – Metadata about points

apply_transform(transform)

Apply a homogeneous transformation to the PointCloud object in- place.

Parameters:

transform ((4, 4) float) – Homogeneous transformation to apply to PointCloud

property bounds

The axis aligned bounds of the PointCloud

Returns:

bounds – Minimum, Maximum verteex

Return type:

(2, 3) float

property centroid

The mean vertex position

Returns:

centroid – Mean vertex position

Return type:

(3,) float

property colors

Stored per- point color

Returns:

colors – Per- point RGBA color

Return type:

(len(self.vertices), 4) np.uint8

property convex_hull

A convex hull of every point.

Returns:

convex_hull – A watertight mesh of the hull of the points

Return type:

trimesh.Trimesh

copy()

Safely get a copy of the current point cloud.

Copied objects will have emptied caches to avoid memory issues and so may be slow on initial operations until caches are regenerated.

Current object will not have its cache cleared.

Returns:

copied – Copy of current point cloud

Return type:

trimesh.PointCloud

crc()

Get a CRC hash of the current vertices.

Returns:

crc – Hash of self.vertices

Return type:

int

export(file_obj=None, file_type=None, **kwargs)

Export the current pointcloud to a file object. If file_obj is a filename, file will be written there. Supported formats are xyz :param file_obj: str, file name where to save the pointcloud

None, if you would like this function to return the export blob

Parameters:

file_type (str) – Which file type to export as. If file name is passed this is not required

property extents

The size of the axis aligned bounds

Returns:

extents – Edge length of axis aligned bounding box

Return type:

(3,) float

hash()

Get a hash of the current vertices.

Returns:

hash – Hash of self.vertices

Return type:

str

property is_empty

Are there any vertices defined or not.

Returns:

empty – True if no vertices defined

Return type:

bool

property kdtree

Return a scipy.spatial.cKDTree of the vertices of the mesh. Not cached as this lead to observed memory issues and segfaults.

Returns:

tree – Contains mesh.vertices

Return type:

scipy.spatial.cKDTree

merge_vertices()

Merge vertices closer than tol.merge (default: 1e-8)

query(input_points, **kwargs)

Find the the closest points and associated attributes from this PointCloud. :param input_points: Input query points :type input_points: (n, 3) float :param kwargs: Arguments for proximity.query_from_points :type kwargs: dict :param result: Result of the query. :type result: proximity.NearestQueryResult

scene()

A scene containing just the PointCloud

Returns:

scene – Scene object containing this PointCloud

Return type:

trimesh.Scene

property shape

Get the shape of the pointcloud

Returns:

shape – Shape of vertex array

Return type:

(2,) int

show(**kwargs)

Open a viewer window displaying the current PointCloud

property vertices

Vertices of the PointCloud

Returns:

vertices – Points in the PointCloud

Return type:

(n, 3) float

trimesh.points.k_means(points, k, **kwargs)

Find k centroids that attempt to minimize the k- means problem: https://en.wikipedia.org/wiki/Metric_k-center

Parameters:
  • points ((n, d) float) – Points in space

  • k (int) – Number of centroids to compute

  • **kwargs (dict) – Passed directly to scipy.cluster.vq.kmeans

Returns:

  • centroids ((k, d) float) – Points in some space

  • labels ((n) int) – Indexes for which points belong to which centroid

trimesh.points.major_axis(points)

Returns an approximate vector representing the major axis of the passed points.

Parameters:

points ((n, dimension) float) – Points in space

Returns:

axis – Vector along approximate major axis

Return type:

(dimension,) float

trimesh.points.plane_fit(points)

Fit a plane to points using SVD.

Parameters:

points ((n, 3) float or (p, n, 3,) float) – 3D points in space Second option allows to simultaneously compute p centroids and normals

Returns:

  • C ((3,) float or (p, 3,) float) – Point on the plane

  • N ((3,) float or (p, 3,) float) – Unit normal vector of plane

trimesh.points.plot_points(points, show=True)

Plot an (n, 3) list of points using matplotlib

Parameters:
  • points ((n, 3) float) – Points in space

  • show (bool) – If False, will not show until plt.show() is called

trimesh.points.point_plane_distance(points, plane_normal, plane_origin=None)

The minimum perpendicular distance of a point to a plane.

Parameters:
  • points ((n, 3) float) – Points in space

  • plane_normal ((3,) float) – Unit normal vector

  • plane_origin ((3,) float) – Plane origin in space

Returns:

distances – Distance from point to plane

Return type:

(n,) float

trimesh.points.project_to_plane(points, plane_normal, plane_origin, transform=None, return_transform=False, return_planar=True)

Project (n, 3) points onto a plane.

Parameters:
  • points ((n, 3) float) – Points in space.

  • plane_normal ((3,) float) – Unit normal vector of plane

  • plane_origin ((3,)) – Origin point of plane

  • transform (None or (4, 4) float) – Homogeneous transform, if specified, normal+origin are overridden

  • return_transform (bool) – Returns the (4, 4) matrix used or not

  • return_planar (bool) – Return (n, 2) points rather than (n, 3) points

trimesh.points.radial_sort(points, origin, normal, start=None)

Sorts a set of points radially (by angle) around an axis specified by origin and normal vector.

Parameters:
  • points ((n, 3) float) – Points in space

  • origin ((3,) float) – Origin to sort around

  • normal ((3,) float) – Vector to sort around

  • start ((3,) float) – Vector to specify start position in counter-clockwise order viewing in direction of normal, MUST not be parallel with normal

Returns:

ordered – Same as input points but reordered

Return type:

(n, 3) float

trimesh.points.remove_close(points, radius)

Given an (n, m) array of points return a subset of points where no point is closer than radius.

Parameters:
  • points ((n, dimension) float) – Points in space

  • radius (float) – Minimum radius between result points

Returns:

  • culled ((m, dimension) float) – Points in space

  • mask ((n,) bool) – Which points from the original points were returned

trimesh.points.tsp(points, start=0)

Find an ordering of points where each is visited and the next point is the closest in euclidean distance, and if there are multiple points with equal distance go to an arbitrary one.

Assumes every point is visitable from every other point, i.e. the travelling salesman problem on a fully connected graph. It is not a MINIMUM traversal; rather it is a “not totally goofy traversal, quickly.” On random points this traversal is often ~20x shorter than random ordering, and executes on 1000 points in around 29ms on a 2014 i7.

Parameters:
  • points ((n, dimension) float) – ND points in space

  • start (int) – The index of points we should start at

Returns:

  • traversal ((n,) int) – Ordered traversal visiting every point

  • distances ((n - 1,) float) – The euclidean distance between points in traversal