summaryrefslogtreecommitdiff
path: root/sample.py
diff options
context:
space:
mode:
Diffstat (limited to 'sample.py')
-rw-r--r--sample.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/sample.py b/sample.py
index b494384..0b2ffc6 100644
--- a/sample.py
+++ b/sample.py
@@ -1,4 +1,5 @@
import numpy as np
+from transform import rotate
def uniform_sphere(size=None, dtype=np.double):
"""
@@ -27,3 +28,29 @@ def uniform_sphere(size=None, dtype=np.double):
points[:,2] = u
return points
+
+def flashlight(phi=np.pi/4, direction=(0,0,1), size=None, dtype=np.double):
+ theta, u = np.random.uniform(0.0, 2*np.pi, size), \
+ np.random.uniform(np.cos(phi), 1, size)
+
+ c = np.sqrt(1-u**2)
+
+ if np.equal(direction, (0,0,1)).all():
+ rotation_axis = (0,0,1)
+ rotation_angle = 0.0
+ else:
+ rotation_axis = np.cross((0,0,1), direction)
+ rotation_angle = \
+ -np.arccos(np.dot(direction, (0,0,1))/np.linalg.norm(direction))
+
+ if size is None:
+ return rotate(np.array([c*np.cos(theta), c*np.sin(theta), u]),
+ rotation_angle, rotation_axis)
+
+ points = np.empty((size, 3), dtype)
+
+ points[:,0] = c*np.cos(theta)
+ points[:,1] = c*np.sin(theta)
+ points[:,2] = u
+
+ return rotate(points, rotation_angle, rotation_axis)