generalized stereographic projection to cope with all three directions (x,y,Z)

This commit is contained in:
Philip Eisenlohr 2021-02-27 11:15:01 -05:00
parent e85f12fd2f
commit ea763fd941
2 changed files with 19 additions and 14 deletions

View File

@ -193,7 +193,7 @@ def scale_to_coprime(v):
return m
def project_stereographic(vector,normalize=False):
def project_stereographic(vector,normalize=False,direction='z'):
"""
Apply stereographic projection to vector.
@ -203,16 +203,21 @@ def project_stereographic(vector,normalize=False):
Vector coordinates to be projected.
normalize : bool
Ensure unit length for vector. Defaults to False.
direction : str or int
Projection direction 'x'|0, 'y'|1, or 'z'|2.
Defaults to 'z'.
Returns
-------
coordinates : numpy.ndarray of shape (...,2)
coordinates : numpy.ndarray of shape (...,3)
Projected coordinates.
"""
v_ = vector/np.linalg.norm(vector,axis=-1,keepdims=True) if normalize else vector
return np.block([v_[...,:2]/(1+np.abs(v_[...,2:3])),
np.zeros_like(v_[...,2:3])])
shift = 2-('xyzXYZ'.index(direction)%3 if isinstance(direction,str) else int(direction))
v_ = np.roll(vector/np.linalg.norm(vector,axis=-1,keepdims=True) if normalize else vector,
shift,axis=-1)
return np.roll(np.block([v_[...,:2]/(1+np.abs(v_[...,2:3])),np.zeros_like(v_[...,2:3])]),
-shift,axis=-1)
def execution_stamp(class_name,function_name=None):

View File

@ -49,17 +49,17 @@ class TestUtil:
dist_sampled = np.histogram(centers[selected],bins)[0]/N_samples*np.sum(dist)
assert np.sqrt(((dist - dist_sampled) ** 2).mean()) < .025 and selected.shape[0]==N_samples
@pytest.mark.parametrize('point,normalize,answer',
@pytest.mark.parametrize('point,normalize,direction,answer',
[
([1,0,0],False,[1,0,0]),
([1,0,0],True, [1,0,0]),
([0,1,1],False,[0,0.5,0]),
([0,1,1],True, [0,0.41421356,0]),
([1,1,1],False,[0.5,0.5,0]),
([1,1,1],True, [0.3660254, 0.3660254, 0]),
([1,0,0],False,'z',[1,0,0]),
([1,0,0],True, 'z',[1,0,0]),
([0,1,1],False,'z',[0,0.5,0]),
([0,1,1],True, 'y',[0,0,0.41421356]),
([1,1,1],False,'x',[0,0.5,0.5]),
([1,1,1],True, 'y',[0.3660254, 0,0.3660254]),
])
def test_project_stereographic(self,point,normalize,answer):
assert np.allclose(util.project_stereographic(np.array(point),normalize=normalize),answer)
def test_project_stereographic(self,point,normalize,direction,answer):
assert np.allclose(util.project_stereographic(np.array(point),normalize=normalize,direction=direction),answer)
@pytest.mark.parametrize('fro,to,mode,answer',
[