forked from pmgbergen/porepy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New function: half_space_int - find points that lie in the intersecti…
…on of half planes Also with unit tests
- Loading branch information
Runar
committed
Feb 2, 2017
1 parent
905fb69
commit adee794
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
*.pyc | ||
*.py# | ||
*.py~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|