Polytope manipulation example: Minkowski sum and intersection

This example shows how to manipulate polytopes using the pycapacity package. In particular, it shows how to perform Minkowski sum and intersection of polytopes using the Polytope object’s operators + and & respectively.

from pycapacity.objects import Polytope # import polytope object
import numpy as np

# this seed is used to generate the same image
# as in the examples in the docs
np.random.seed(12345)

N = 10 # hundred vertices
m = 3   # space dimension

points1 = np.array(np.random.rand(m,N))*2-1 # points
points2 = np.array(np.random.rand(m,N))*2-1 # points

# create a polytope object
p1 = Polytope()
p1.find_from_point_cloud(points1)
p2 = Polytope()
p2.find_from_point_cloud(points2)

# plotting the polytope
import matplotlib.pyplot as plt
from pycapacity.visual import * # pycapacity visualisation tools

fig = plt.figure(4)
# draw polytopes
plot_polytope(plot=fig, polytope=p1, label='polytope p1', face_color="blue", edge_color='black',  alpha=0.2)
plot_polytope(plot=fig, polytope=p2, label='polytope p2', face_color="red", edge_color='black', alpha=0.2)
plt.legend()
plt.show()

The output of the code is a visualisation of the two polytopes

../_images/polyman_two.png

Minkowski sum of polytopes

pycapacity package allows to perform Minkowski sum of polytopes using the operator +. So p1 + p2 will return a new polytope object which is the Minkowski sum of p1 and p2 defined as \(p_{sum}=p1\oplus p2\)

# calculate the Minkowski sum of p1 and p2
p_sum = p1 + p2

# plotting the polytope and the points
fig = plt.figure(5)
# draw polytopes
plot_polytope(plot=fig, polytope=p1, label='polytope p1', face_color="blue", edge_color='black',  alpha=0.2)
plot_polytope(plot=fig, polytope=p2, label='polytope p2', face_color="red", edge_color='black', alpha=0.2)
plot_polytope(plot=fig, polytope=p_sum, label='polytope p1$\oplus$p2', face_color="yellow", edge_color='black', alpha=0.2)
plt.legend()
plt.show()

The output of the code is a visualisation of the two polytopes and the Minkowski sum polytope

../_images/polyman_sum.png

Intersection sum of polytopes

pycapacity package allows to calculate the intersection of polytopes using the operator &. So p1 & p2 will return a new polytope object which is the intersection of p1 and p2 defined as \(p_{sum}=p1\cap p2\)

# calculate the intersection sum of p1 and p2
p_int = p1 & p2

# plotting the polytope and the points
fig = plt.figure(5)
# draw polytopes
plot_polytope(plot=fig, polytope=p1, label='polytope p1', face_color="blue", edge_color='black',  alpha=0.2)
plot_polytope(plot=fig, polytope=p2, label='polytope p2', face_color="red", edge_color='black', alpha=0.2)
plot_polytope(plot=fig, polytope=p_int, label='polytope p1$\cap$p2', face_color="green", edge_color='black', alpha=1)
plt.legend()
plt.show()

The output of the code is a visualisation of the two polytopes and the intersection polytope

../_images/polyman_int.png

More operations

Besides + and & the Polytope object supports:

  • p + x and p - x: translation by a vector x

  • a * p: scaling by a scalar (a negative a reflects the polytope)

  • R @ p: linear image of the polytope, for example in another frame (R a rotation matrix), or a projection

  • p.contains(points): are the points (one point or points as columns) inside the polytope

  • p.volume(): volume (area in 2D, zero for a flat polytope)

  • p.reduce(): minimal representations - only the extreme points and one inequality per facet (e.g. after an intersection)

  • Polytope.from_points(points): the convex hull of a point cloud

  • sum([p1, p2, p3]): Minkowski sum of a list of polytopes

The conversions are exact: the half-plane representation has one inequality per facet, the vertex representation only the extreme points, and flat polytopes (for example the velocity polytope of a planar robot seen in 3D) are supported - their half-plane representation contains the equalities of their plane as pairs of opposite inequalities.

Example: two 7-joint arms hold the same object (full script examples/scripts/polytope_operations.py).

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))
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 hold an object of 3 kg?
f_hold = np.array([0, 0, 3 * 9.81])
print(both.contains(f_hold), P1.contains(f_hold))
# what is left once the object is held
remaining = both - f_hold
# the polytope of the first arm in a frame rotated 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(), P1_tool.volume(), both.volume(), common.volume())
../_images/polyman_ops.png