def test_hypothesis(self, v): """ Test normalize's invariants with 'random' input from hypothesis. """ norm_v = np.linalg.norm(v) hy.assume(norm_v > 1e-8) nt.assert_almost_equal(np.linalg.norm(normalize(v)), 1) nt.assert_almost_equal(normalize(v) * norm_v, v)
def __init__(self, material=Material(), position=(0, 0, 0), normal=(0, 1, 0)): super().__init__(material) check_finite(position, normal) self._position = np.array(position, dtype="float") self._normal = normalize(np.array(normal))
def _build_coordinate_system(self, vague_up, optical_axis): """ Given the rough user specified setup of a camera, create an orthonormal coordinate system. Parameters ---------- vague_up : numpy.ndarray_like the general upwards direction of the camera. optical_axis : numpy.ndarray_like the direction in which the optical axis of the camera points - will be orthongonal to the image plane. """ vague_up = normalize(vague_up) self._optical_axis = normalize(optical_axis) self._righthand = np.cross(vague_up, self._optical_axis) self._up = np.cross(self._optical_axis, self._righthand)
def test_invalid_examples(self): """ Test if normalize rejects invalid parameters as expected. """ with self.assertRaises(ValueError): normalize((np.inf, 0, 0)) with self.assertRaises(ValueError): normalize((np.nan, 0, 0)) with self.assertRaises(ValueError): normalize((-np.inf, 0, 0)) with self.assertRaises(ZeroDivisionError): normalize((0, 0, 0))
def test_ray(self): """ Test if the initial rays are calculated correctly - Beware that the indexing is following numpy's convention: x is vertical & y is horizontal. """ c = PerspectiveCamera() r = c.ray((50, 50), (100, 100), False) nt.assert_almost_equal(r.position, (0, 0, 0)) nt.assert_almost_equal(r.direction, (0, 0, 1)) r = c.ray((0, 0), (100, 100), False) nt.assert_almost_equal(r.direction, normalize((1, 1, 1))) r = c.ray((100, 0), (100, 100), False) nt.assert_almost_equal(r.direction, normalize((1, -1, 1))) r = c.ray((0, 100), (100, 100), False) nt.assert_almost_equal(r.direction, normalize((-1, 1, 1))) r = c.ray((100, 100), (100, 100), False) nt.assert_almost_equal(r.direction, normalize((-1, -1, 1)))
def outgoing_direction(self, normal, incoming_direction): """ Given a surface normal and an incoming direction, determine the direction in which the path continues. normal : numpy.ndarray_like of shape (3, ) the surface normal at the intersection point incoming_direction : numpy.ndarray_like of shape (3, ) the direction from which light hits the surface Returns ------- outgoing direction : numpy.ndarray_like of shape (3, 0) the direction in which light is reflected from the surface """ # BaseClass randomly (not uniformly) samples the hemisphere point_on_sphere = normalize(np.random.uniform(0, 1, size=(3, ))) # ensure it is in the same hemisphere as the normal if np.dot(normal, point_on_sphere) < 0: point_on_sphere = -point_on_sphere return normalize(normal + point_on_sphere)
def normal(self, x): """ Given a point on the surface of the sphere instance and returns the surface normal at that point. Parameters ---------- x : numpy.ndarray_like point on the geometry instance Returns ------- normal : numpy.ndarray_like normal vector of the geometry surface at this point """ return normalize(x - self.position)
def __init__(self, position=(0, 0, 0), direction=(1, 0, 0)): check_finite(position, direction) self._position = np.array(position).astype(np.float64) self._direction = normalize(direction).astype(np.float64)