# Path: tracer/optics.py
# def fresnel(ray_dirs, normals, n1, n2):
# def reflections(ray_dirs, normals):  
# def refractions(n1, n2, ray_dirs, normals):
# def refr_idx_hartmann(wavelength, a, b, c, d, e):
#     R = (Rs + Rp)/2


import math
import unittest
import numpy as N
from tracer import optics
    def test_reverse_normal(self):
        """When the normal points into the new material, refractions ok"""
        n1 = N.r_[1., 1.]
        n2 = N.r_[1., 1.5]
        dir = N.c_[[0, -1, 1], [0, -1, -1]]/math.sqrt(2)
        norm = N.c_[[0, -1, 0]]
        refr, dirs = optics.refractions(n1, n2, dir, norm)

        self.failUnless(refr.all(), "Some rays did not refract")
        correct_refrs = N.c_[dir[:,0], -N.sqrt([0, 7./9, 2./9])]
        N.testing.assert_array_almost_equal(dirs, correct_refrs)
    
    def test_single_ref_idx(self):
        """Single refractive index is acceptable for refractions"""
        n1 = 1.
        n2 = 1.5
        dir = N.c_[[0, -1, 1], [0, -1, -1]]/math.sqrt(2)
        norm = N.c_[[0, -1, 0]]
        refr, dirs = optics.refractions(n1, n2, dir, norm)

        self.failUnless(refr.all(), "Some rays did not refract")
        correct_refrs = N.c_[N.sqrt([0, 7./9, 2./9])*N.r_[0,-1,1], -N.sqrt([0, 7./9, 2./9])]
        N.testing.assert_array_almost_equal(dirs, correct_refrs)
    
    def test_TIR(self):
        """With rays exiting a glass, some are TIR'ed, some not"""
        n1 = 1.5
        n2 = 1.
        dir = N.c_[[0, -1, 0], N.r_[0, -1, -1]/math.sqrt(2)]
        norm = N.c_[[0, -1, 0]]

# Path: tracer/optics.py
# def fresnel(ray_dirs, normals, n1, n2):
# def reflections(ray_dirs, normals):  
# def refractions(n1, n2, ray_dirs, normals):
# def refr_idx_hartmann(wavelength, a, b, c, d, e):
#     R = (Rs + Rp)/2


# Path: tests/test_optics.py
import math
import unittest
import numpy as N
from tracer import optics

    def test_reverse_normal(self):
        """When the normal points into the new material, refractions ok"""
        n1 = N.r_[1., 1.]
        n2 = N.r_[1., 1.5]
        dir = N.c_[[0, -1, 1], [0, -1, -1]]/math.sqrt(2)
        norm = N.c_[[0, -1, 0]]
        refr, dirs = optics.refractions(n1, n2, dir, norm)

        self.failUnless(refr.all(), "Some rays did not refract")
        correct_refrs = N.c_[dir[:,0], -N.sqrt([0, 7./9, 2./9])]
        N.testing.assert_array_almost_equal(dirs, correct_refrs)
    
    def test_single_ref_idx(self):
        """Single refractive index is acceptable for refractions"""
        n1 = 1.
        n2 = 1.5
        dir = N.c_[[0, -1, 1], [0, -1, -1]]/math.sqrt(2)
        norm = N.c_[[0, -1, 0]]
        refr, dirs = optics.refractions(n1, n2, dir, norm)

        self.failUnless(refr.all(), "Some rays did not refract")
        correct_refrs = N.c_[N.sqrt([0, 7./9, 2./9])*N.r_[0,-1,1], -N.sqrt([0, 7./9, 2./9])]
        N.testing.assert_array_almost_equal(dirs, correct_refrs)
    
    def test_TIR(self):
        """With rays exiting a glass, some are TIR'ed, some not"""
        n1 = 1.5
        n2 = 1.
        dir = N.c_[[0, -1, 0], N.r_[0, -1, -1]/math.sqrt(2)]
        norm = N.c_[[0, -1, 0]]

        refr, dirs = optics.refractions(n1, n2, dir, norm)