klampt.model.geometry module

Utility functions for operating on geometry. See the Geometry3D documentation for the core geometry class.

New in version 0.8.6.

[functions moved here from klampt.model.sensing]

Working with geometric primitives

box() and sphere() are aliases for the functions in klampt.model.create.primitives.

Working with point clouds

point_cloud_normals() estimates normals from a normal-free PointCloud.

The fit_plane(), fit_plane3(), and PlaneFitter class help with plane estimation.

point_cloud_simplify() simplifies a PointCloud.

point_cloud_colors() and point_cloud_set_colors() sets / gets colors from a PointCloud.

Other utilities

merge() can put together TriangleMesh and PointCloud geometries into unified geometries.

triangle_normals() computes triangle normals for a TriangleMesh.

klampt.model.geometry.box(width, depth, height, center=None, R=None, t=None, world=None, name=None, mass=inf, type='TriangleMesh')[source]

Alias for klampt.model.create.primitives.box()

klampt.model.geometry.sphere(radius, center=None, R=None, t=None, world=None, name=None, mass=inf, type='TriangleMesh')[source]

Alias for klampt.model.create.primitives.sphere()

class klampt.model.geometry.PlaneFitter(points=None)[source]

Bases: object

Online fitting of planes through 3D point clouds

normal

best-fit normal

Type

3-vector

centroid

centroid of points

Type

3-vector

count

# of points

Type

int

sse

fitting sum of squared errors

Type

float

cov

covariance of points

Type

3x3 array

plane_equation()[source]

Returns (a,b,c,d) with ax+by+cz+d=0 the plane equation

goodness_of_fit()[source]

Returns corrected RMSE

add_point(pt)[source]

Online estimation of best fit plane

merge(fitter, inplace=False)[source]

Online merging of two plane fitters.

If inplace = False, returns a new PlaneFitter. If inplace = True, self is updated with the result.

distance(pt)[source]

Returns the signed distance to this plane

klampt.model.geometry.point_cloud_simplify(pc, radius)[source]

Simplifies a point cloud by averaging points within neighborhoods. Uses a fast hash grid data structure.

Parameters
Return type

Union[Geometry3D, PointCloud]

klampt.model.geometry.point_cloud_normals(pc, estimation_radius=None, estimation_knn=None, estimation_viewpoint=None, add=True)[source]

Returns the normals of the point cloud. If pc has the standard normal_x, normal_y, normal_z properties, these will be returned. Otherwise, they will be estimated using plane fitting.

The plane fitting method uses scipy nearest neighbor detection if scipy is available. Otherwise it uses a spatial grid. The process is as follows:

  • If estimation_radius is provided, then it will use neighbors within this range. For a spatial grid, this is the grid size.

  • If estimation_knn is provided, then planes will be fit to these number of neighbors.

  • If neither is provided, then estimation_radius is set to 3 * max dimension of the point cloud / sqrt(N).

  • If not enough points are within a neighborhood (either 4 or estimation_knn, whichever is larger), then the normal is set to 0.

  • If estimation_viewpoint is provided, this must be a 3-list. The normals are oriented such that they point toward the viewpoint.

If add=True, estimated normals will be added to the point cloud under the normal_x, normal_y, normal_z properties.

Returns

A N x 3 numpy array of normals.

Return type

ndarray

klampt.model.geometry.fit_plane3(point1, point2, point3)[source]

Returns a 3D plane equation fitting the 3 points.

The result is (a,b,c,d) with the plane equation ax+by+cz+d=0

Return type

Tuple[float, float, float, float]

klampt.model.geometry.fit_plane(points)[source]

Returns a 3D plane equation that is a least squares fit through the points (len(points) >= 3).

Return type

Tuple[float, float, float, float]

klampt.model.geometry.fit_plane_centroid(points)[source]

Similar to fit_plane(), but returns a (centroid,normal) pair.

Return type

Tuple[Sequence[float], Sequence[float]]

klampt.model.geometry.point_cloud_colors(pc, format='rgb')[source]

Returns the colors of the point cloud in the given format. If the point cloud has no colors, this returns None. If the point cloud has no colors but has opacity, this returns white colors.

Parameters
  • pc (PointCloud) – the point cloud

  • format

    describes the output color format, either:

    • ’rgb’: packed 32bit int, with the hex format 0xrrggbb (only 24

      bits used),

    • ’bgr’: packed 32bit int, with the hex format 0xbbggrr (only 24

      bits used),

    • ’rgba’: packed 32bit int, with the hex format 0xrrggbbaa,

    • ’bgra’: packed 32bit int, with the hex format 0xbbggrraa,

    • ’argb’: packed 32bit int, with the hex format 0xaarrggbb,

    • ’abgr’: packed 32bit int, with the hex format 0xaabbggrr,

    • (‘r’,’g’,’b’): triple with each channel in range [0,1]

    • (‘r’,’g’,’b’,’a’): tuple with each channel in range [0,1]

    • ’channels’: returns a list of channels, in the form (r,g,b) or

      (r,g,b,a), where each value in the channel has range [0,1].

    • ’opacity’: returns opacity only, in the range [0,1].

Returns

A an array of pc.numPoints() colors corresponding to the points in the point cloud. If format=’channels’, the return value is a tuple (r,g,b) or (r,g,b,a).

Return type

ndarray

klampt.model.geometry.point_cloud_set_colors(pc, colors, color_format='rgb', pc_property='auto')[source]

Sets the colors of a point cloud.

Parameters
  • pc (PointCloud) – the point cloud

  • colors (list or numpy.ndarray) – the array of colors, and each color can be either ints, tuples, or channels, depending on color_format.

  • color_format

    describes the format of each element of colors, and can be:

    • ’rgb’: packed 32bit int, with the hex format 0xrrggbb (only 24

      bits used),

    • ’bgr’: packed 32bit int, with the hex format 0xbbggrr (only 24

      bits used),

    • ’rgba’: packed 32bit int, with the hex format 0xrrggbbaa,

    • ’bgra’: packed 32bit int, with the hex format 0xbbggrraa,

    • ’argb’: packed 32bit int, with the hex format 0xaarrggbb,

    • ’abgr’: packed 32bit int, with the hex format 0xaabbggrr,

    • (‘r’,’g’,’b’): triple with each channel in range [0,1]. Also use this if colors is an n x 3 numpy array.

    • (‘r’,’g’,’b’,’a’): tuple with each channel in range [0,1]. Also use this if colors is an n x 4 numpy array.

    • ’channels’: colors is a list of 3 or 4 channels, in the form

      (r,g,b) or (r,g,b,a), where each element in a channel has range [0,1].

    • ’opacity’: opacity only, in the range [0,1].

  • pc_property (str) – describes to which property the colors should be set. ‘auto’ determines chooses the property from the point cloud if it’s already colored, or color_format if not. ‘channels’ sets the ‘r’, ‘g’, ‘b’, and optionally ‘a’ properties.

Return type

None

klampt.model.geometry.triangle_normals(trimesh)[source]

Returns a list or numpy array of (outward) triangle normals for the triangle mesh defined by vertices verts and triangles tris.

Parameters

trimesh (TriangleMesh or Geometry3D) –

Returns

An N x 3 matrix of triangle normals with N the number of triangles.

Return type

ndarray

klampt.model.geometry.merge(*items)[source]

Merges one or more geometries into a single geometry.

Parameters

items – Each arg must be a Geometry3D, TriangleMesh, PointCloud, RobotLinkModel, RigidObjectModel, or TerrainModel.

Returns

A merged geometry. If all underlying geometries have PointCloud type, then the result will have PointCloud type. Otherwise, they must all be convertable to TriangleMesh, and the result will be of TriangleMesh type.

Return type

Geometry3D