def test_fresnel_coefs(self):
        self.assertEqual(geo.fresnel_coefs(1.0, 0, 1.0), (0.0, 1.0))
        R,T = geo.fresnel_coefs(1.6, 0.5, 1.0)
        self.assertAlmostEqual(R, 0.072538667, places=6)
        self.assertAlmostEqual(T, 0.927461332, places=6)

        #angles which exceed TIR should return nans:
        tir_angle = geo.tir_angle(1.6, 1.0)
        self.assertTrue(all(numpy.isnan(geo.fresnel_coefs(1.6,
                                                          tir_angle + 0.01,
                                                          1.0))))
def refract_ray(ray, normal, ni, nt):
    #TODO: docstring
#normal is a numpy array.
    #assumption: normal is pointed towards ni (the incoming rays)
    eta = ni / nt #ratio of indices
    c1 = -numpy.dot(ray.direction, normal)
    cs2 = 1 - eta**2 * (1 - c1**2)
    if cs2 > 0:
        #ray is transmitted
        trans_ray = copy.copy(ray)
        trans_ray.direction = eta * ray.direction +\
                              (eta * c1 - numpy.sqrt(cs2)) * normal
        theta_i = -numpy.arccos(c1)
        R, T = geo.fresnel_coefs(ni, theta_i, nt)
        trans_ray.intensity *= T
    else:
        #TIR occurs in this case
        R = 1.0
        trans_ray = None
    ray = reflect_ray(ray, normal) #note: modified in-place
    ray.intensity *= R
    return trans_ray, ray #transmitted, reflected