Working with polytopes

Every polytope function of pycapacity (robot.*_polytope, human.*_polytope, …) returns a pycapacity.objects.Polytope. This page shows what the object holds and what can be done with it.

Representations

A polytope can be described by its vertices or by a set of inequalities, and a Polytope keeps both:

attribute

content

vertices

\(\mathcal{V}\)-representation: the vertices as columns, an array of shape (dimension, number of vertices)

H, d

\(\mathcal{H}\)-representation: the inequalities \(Hx \leq d\), one row per facet

face_indices, faces

triangulated faces (indices of the vertices, and their coordinates), used for plotting and meshes

The missing representation is computed when needed, or explicitly with find_vertices(), find_halfplanes() and find_faces(). The conversions are exact: only the extreme points are kept as vertices, each facet gives one inequality, and flat (lower dimensional) polytopes are supported, for example the velocity polytope of a planar arm seen in 3D, which is a polygon.

Creating polytopes

Besides the capacity functions, a polytope can be created directly:

import numpy as np
from pycapacity.objects import Polytope

P1 = Polytope(vertices=V)             # V: points as columns, (dimension, N)
P2 = Polytope(H=H, d=d)               # {x | Hx <= d}
P3 = Polytope.from_points(X)          # convex hull of a point cloud (only its extreme points are kept)

Operations

Polytopes are combined with the usual Python operators:

operation

meaning

example use

P1 + P2

Minkowski sum

the forces two arms can apply together on an object

P1 & P2

intersection

the forces each of two arms can apply on its own

A @ P

linear map \(\{Ax ~|~ x \in P\}\)

a polytope expressed in another frame (R.T @ P), a projection onto a plane

P + x, P - x

translation

the capacity left once a known force is applied

a * P

scaling (a negative a also reflects the polytope)

a safety margin, units

sum([P1, P2, P3])

Minkowski sum of several polytopes

several arms or fingers

and queried with:

  • P.contains(x): whether a point (a vector) or several points (as columns) are inside the polytope

  • P.volume(): the volume (the area in 2D)

  • P.chebyshev_ball(): the largest ball inside the polytope, as an pycapacity.objects.Ellipsoid

  • P.reduce(): removes redundant inequalities and non-extreme points, for example after an intersection

Example: two arms holding an object

import numpy as np
from pycapacity.robot import force_polytope

rng = np.random.default_rng(1)
J1, J2 = rng.standard_normal((3, 7)), rng.standard_normal((3, 7))   # two 7-joint arms
t_max = 30 * np.ones(7)
P1 = force_polytope(J1, t_max, -t_max)
P2 = force_polytope(J2, t_max, -t_max)

both = P1 + P2        # forces the two arms can apply together
common = P1 & P2      # forces each of them can apply alone

# can they carry a 3 kg object, also with a 20 N push in x? (forces as columns)
f_task = np.array([[0, 0, -3 * 9.81], [20, 0, -3 * 9.81]]).T
print(both.contains(f_task), P1.contains(f_task))

# capacity left once the object is held
remaining = both - np.array([0, 0, 3 * 9.81])

# the force polytope of arm 1 in a tool frame rotated by 30 degrees around z
a = np.deg2rad(30)
R = np.array([[np.cos(a), -np.sin(a), 0], [np.sin(a), np.cos(a), 0], [0, 0, 1]])
P1_tool = R.T @ P1

print(P1.volume(), both.volume(), common.volume())
Force polytopes of two arms, their Minkowski sum and their intersection

The complete script, with plotting, is examples/scripts/polytope_operations.py (see on Github).

More examples

API: pycapacity.objects.