Skip to content

Commit

Permalink
New function: half_space_int - find points that lie in the intersecti…
Browse files Browse the repository at this point in the history
…on of half planes

Also with unit tests
  • Loading branch information
Runar committed Feb 2, 2017
1 parent 905fb69 commit adee794
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pyc
*.py#
*.py~
48 changes: 48 additions & 0 deletions half_space_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np


def half_space_int(n,x0, pts):
"""
Find the points that lie in the intersection of half spaces (3D)
Parameters
----------
n : ndarray
This is the normal vectors of the half planes. The normal
vectors is assumed to point out of the half spaces.
x0 : ndarray
Point on the boundary of the half-spaces. Half space i is given
by all points satisfying (x - x0[:,i])*n[:,i]<=0
pts : ndarray
The points to be tested if they are in the intersection of all
half-spaces or not.
Returns
-------
out : ndarray
A logical array with length equal number of pts.
out[i] is True if pts[:,i] is in all half-spaces
Examples
--------
>>> import numpy as np
>>> n = np.array([[0,1],[1,0],[0,0]])
>>> x0 = np.array([[0,-1],[0,0],[0,0]])
>>> pts = np.array([[-1,1,4],[2,1,-2],[0,0,0]])
>>> out = half_plane_int(n,x0,pts)
array([False, True, False], dtype=bool)
"""
assert n.shape[0] == 3, ' only 3D supported'
assert x0.shape[0] == 3, ' only 3D supported'
assert pts.shape[0] == 3, ' only 3D supported'
assert n.shape[1] == x0.shape[1], 'ther must be the same number of normal vectors as points'
n_pts = pts.shape[1]
in_hull = np.zeros(n_pts)
x0 = np.repeat(x0[:,:,np.newaxis], n_pts,axis=2)
n = np.repeat( n[:,:,np.newaxis], n_pts,axis=2)
for i in range(x0.shape[1]):
in_hull += np.sum((pts - x0[:,i,:])*n[:,i,:],axis=0)<=0

return in_hull==x0.shape[1]
26 changes: 26 additions & 0 deletions tests/test_half_space_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import numpy as np

from utils import half_space_int


def test_one_half_space():

n = np.array([[-1],[0],[0]])
x0 = np.array([[0],[0],[0]])
pts = np.array([[1,-1],[0,0],[0,0]])
out = half_space_int.half_space_int(n,x0,pts)
assert np.all(out == np.array([True,False]))

def test_two_half_spaces():
n = np.array([[-1,0],[0,-1],[0,0]])
x0 = np.array([[0,0],[0,1],[0,0]])
pts = np.array([[1,-1,1,0],[2,0,2,0],[0,0,0,0]])
out = half_space_int.half_space_int(n,x0,pts)
assert np.all(out == np.array([True,False,True,False]))


if __name__ == '__main__':
test_one_half_space()
test_two_half_spaces()

0 comments on commit adee794

Please sign in to comment.