Friday, February 10, 2012

Snell's Law, Part The Second

{<−− Part I }                                                       {Part III  −−>}
Part 1 gives Snell's Law as a vector equation.   I have only accounted for a case in two dimensions, with N vertical in the plane.  Before going further, a little cleanup is necessary.  The initial diagram should be altered to match the solution, and I need to determine my relationship to a certain ± sign.

Time for a brief word about
VECTORS
■  A vector V is a line segment directed from a point a to another point b, written
           V = (b - a).
We subtract: tip − tail.   This subtraction (b - a) gives a new point, which I will call  v.  V can still be written as a difference of two points:
           V = (v − 0)
where 0 is the zero vector, which has all components zero. 
Hopefully, that sounds silly; subtracting zero doesn't do anything.  That is exactly what I want:

■ Any directed line segment is equivalent to a vector with the same direction and magnitude (length) whose tail is situated at the origin.
Example: Given points a = (a1,  a2) and b = (b1, b2)
        

Generalization:
■ All vectors with the same direction and magnitude, wherever they lie in space, are equivalent. 

■The Dot Product:  Given two vectors,  R = (r1, r2)  and   S = (s1, s2),
        
In three space, given  R= (r1, r2, r3)  and  S = (s1, s2, s3)
        

■ The result of the dot product is a regular-ass number (a scalar), not a vector.

■ The dot product of two normalized vectors gives the cosine of the angle between them.
Given  R = (cos α, sin α ),   S = (cosβ  , sinβ )
        
This is enough to evaluate the solution offered in Part 1:
Vector Orientation
(Fig. 2)  - From Part 1 

Here again, the length of all vectors is 1.  The labeled points are then
        


The dashed line I' has the same direction and magnitude as I. Subtracting  tip − tail,
        

The two are the same.  But the angle between I' and N is  is (π − θ1), not θ1.  (Mouse over the image).
 Hence the angle between I and N  should also be (π − θ1).  Is it?
        

Yes.   I want   I•N   to give cosθ1, so I will change its direction:
(Fig. 3)  Corrected, N-Up

This is the correct diagram for the result in Part 1.  This is the definition of I that I used, and the formula should already be correct.

DEFAULT ORIENTATION
There is a trap waiting for me, and it's time to fall in.  The ± need to go.

What happens if the light comes from the other side?  In Fig. 3, what if I is on the right?  The mirrors of I and T share the same cosine.  The dot product does not distinguish between +θ and  −θ.   There are several ways to resolve this, which I am putting off to part 5.

I have chosen to set the default orientation in advance.  I've decided to cheat.  The computation of snell's law will happen in object space.  This is permitted by  a series of simplifying assumptions for real-time, which allow me to compute a reference plane once for the entire object:
  1.  The surface in question is a sheet.
    It has a top side and a bottom side.  It is not necessarily flat and, for caustics, probably should not be.  Picutre a lake.
  2. There is an up-direction.
     For a lake, on Earth, this is defined by gravity.  Air above, lake below.
  3.   I will call the unit vector in this direction the reference normal, or the facing normal.  In local coordinate space I will  label it Z.
  4. The reference normal can be in any direction in the game world.
    The lake can be upside down.  What matters is that the direction be defined.  Medium 1 will always lie on the positive side of the surface.  Specifically,
  5. From the global coordinate space, the quadrant in which the light source lies, relative to the surface is already determined.
Example:
Pick any of the diagrams.  Place a boat on the surface of the water.   Now add waves.  The waves can pass freely through the diagram.  We could make a boat stability simulation: genetic algorithm builds boat, boat drops in water, waves pass through and rock the boat about.  A good boat does not sink.
In all of these cases, our 2D view stays put.  The surface normal changes as the waves pass through, but the air is always above, the water below.

To look at the boat from the side, we can rotate around it.  Of course, we leave the sky up, and the earth down.  The z-axis stays put.  This is the heart of my simplifying assumption.  I *think* it is why liquid surfaces are usually sheet meshes in a game engine, and not enclosed volumes.

I will not resolve the sign of θ for each ray. I will compute a Very Large Number of rays, rotate the surface, perform A Lot More, and so on.  I also believe this assumption will generalize further: I want to compute (fake) global lighting and caustics in rings at each point, using a MuchSmallerNumber of indiviual light paths.


FORMULA, N-UP:
Back to the diagram.  In this case,  the L is some positive angle less than 90 degrees from N.  I take the positive square root for sinθ1; the first component of I is −sinθ1, the first component of T  is (positive) sinθ2, as shown in the diagrams.  And thus the formula:

This was actually the first formula I came up with.  Since I had peeked at the solution in the nVidia paper, I knew they had a ± I did not, and I backed up to recreate the difficulty which leads to its adoption.  I found it very instructive.  I had played around with the cross product and determinant, but still did not have a clear picture of how to resolve sign in a consistent manner, at different places in the transform.

{THIS IS ALL PRETEND}
I am making all of this up. I could be wrong about anything!  I could also be a seven-legged, scabby froodle.  But I'm a bit more worried about being
 Super
    Wrong.
Let me know!
{<−− Part I }                                                                {Part III  −−>} 
{CodeCogs does LaTeX equations for free.  Thanks, guys}

Thursday, February 9, 2012

Snell's Law: Part The First

When light crosses from one medium to another of a different density, its speed and wavelength change.  Where it strikes the new surface at an angle, the transmitted wave is refracted, that is, the light changes direction.

Light which intersects a surface is incident.  The light which passes through the surface is transmitted.  The line perpendicular to a surface at a point p is normal to the surface at that point.   According to Snell's Law, we can assign to each medium a constant value η (eta), called its index of refraction (IOR), which satisfies the following relationship:
(1)                                                   η1 sin θ1 = η2 sin θ2 
where η1 and  η2 are the indices of refreaction of the first and second media, respectively, θ1 is the angle between the incident ray and the surface normal, and θ2 is the angle between the transmitted ray and the normal.
Snell's Law
Fig. 1

In Fig. 1, incident ray I strikes a new medium, and the transmitted ray T is refracted.  Each is shown as the hypotenuse of a triangle.  If both I  and T are drawn with a length of 1 (if they lie on a unit circle), then the opposite sides of the triangles will have lengths sin θ1 and sin θ2   (mouse over the image).  According to Snell's law, the relationship between these two lengths and their corresponding angles is completely determined by the constant ratio η1/ η2:
 
I will explore this equation in depth over several blog entries.

Assumptions
{  Edit: this section has been reworked.  I seek clarity in method and presentation.  If a question appeared here and vanished, I will visit it at a later point.  If you think I should retain, and strike, prior text  ( like so ) let me know.   Thanks to Peter Schaefer for assistance with assumption ii. }

i) The surface normal is vertical in the reference plane.
ii) The transmitted light will lie in the same plane as the incident ray and the surface normal.  In the diagram, there is a single transmitted ray.

The first assumption is made for simplicity.  For rigid geometric objects, relationships between angles are invariant under translation and rotation.  For example, if you print out Fig.1 and set it in front of you on the table, spinning the page around will not change the measure of any of the angles.  This problem is easier to solve for N = (0, 1) than for arbitrary N.

I assume (ii) as part of the definition of Snell's Law.  That is, it is a physical result. 
The difficulty is this.  As mentioned in the nVidia paper, equation (1) alone does not appear to restrict T to the same plane as  I  and N.  For, what is to prevent you from cheerily rotating T around the dotted line and forming a cone?  Nothing.  The interior angle remains θ2 all the way 'round. Thus, by the Who's Going to Stop Me Theorem T need not lie in the given plane.  This may be done independently of a cheerful disposition.
However, I have given the incident ray and surface normal as vectors.  Together, I and N give the plane in which T will lie.  That is the point of the diagram.  I will state the restriction mathematically and account for it later, when I move the result to 3 dimensional space.

Onward.


For computation on graphics hadware, it is desirable to express Snell's Law in vector notation.   Thus,

Call the incident vector  I, the normal to surface N, and the transmission vector T. Assume I, T and N are normalized (each has a length of 1), and all lie in the same plane.  Then,
GIVEN: η1, η2, the indicies of refraction of the two media,
LET:
(2)  N = (0, 1)
      I = (–sinθ1, cosθ1)
     T = (sinθ2, –cosθ2)

The dot product of two normalized vectors gives the cosine of the angle between them.  Hence,
(3)   I•N = cosθ1

I will also need the following trigonometric identity:



The Problem: Give T as a vector equation in η1, η2, N, and I.
This requires two steps , which may be done in any order.

I. Calculate T for any given η1, η2, N and I.
If I rewrite sinθ1,  sinθ2  and  cosθ2  in terms of  cosθ1 = I • N,  I can give each component of T as a vector equation.

sinθ1 :  By (3) and (4), sinθ1 becomes

sinθ2 :  By Snell's Law (1),  sinθ2 = (η1 /η2)sinθ1   and by (4),

cosθ2 :  Substituting this value of sinθ2  into (4):


We can now give T = (sinθ2, -cosθ2):

This is correct, but in the vector T = (t1, t2), t1 and t2 have been solved separately.  I seek slightly more than this; I want to solve both simultaneously.  Hence,

II. Give T as the sum of two vectors.
Specifically, I want to write T as a linear combination of the two given vectors, N and I:
(9)        T      =     αI     +    βN ,         α, β constant:
       

This is another way of writing a pair of equations, with unknowns α and β:
(10)       sinθ2 = –α sinθ1
           –cosθ2 =  α cosθ1 +  β
α: The first equation gives α directly.   α =  –(sinθ2/sinθ1)   which, by (1), is
(11)       α = –η1/η2
β: Substituting this value of α into the second equation,
(12)       β = (η1/ η2)cosθ1 − cosθ2

Now the two steps can be merged.  Using the values for cosθ1 and cosθ2 in (3) and (7),

Substituting the values for α and β in (11) and (13) into T = αI + βN  gives  the desired relation:
                   

I have not derived a new formula, nor have I developed one of my own.  The above equation is an exact restatement of Snell's Law, in vector notation.

{Thanks to CodeCogs for their free LaTeX equation editor and image generation.}

Numerical Methods

Sometimes, while I am working, I will run across sentences like this:
...which can be computed according to the following formula given by SoAndSo, SomeDate:  {SomeFormula}.

I will stop reading, close the paper, and work out the relationship for myself.

There are several reasons.

The first is about me.  I doubt solutions until I can demonstrate them to myself.  I do not value memorization at the expense of comprehension.
We may rearrange a problem however we wish using the rules of algebra and geometry.  If mathematics is a language, it has only a few words.  The rest is concision and clarity.  I think the student who can manipulate 12th century Persian Algebra and weild a protractor, compass and straightedge, and know what she is up to, may gain ground as quickly as she like on University coursework in computer science and programming.

I seek to describe and solve problems in acoustics, wave propagation, and signal processing.  The state of sound design tools is poor, and their advancement is hindered by errors of logic and observation, accepted as common knowledge and understanding.  To identify, describe, and solve these problems, it is sufficient to construct mathematical descriptions of sound, and account for the physical properties in a consistent way.  The errors are basic.  They can be demonstrated with a walk down the street; walking around a streetlight; listening to a train.  To make the properties of waves more intuitive and accessible --and to insure that they are correctly accounted for-- I have constructed a series of analogies to light: construction of an image, its properties, and the mechanics of wave propagation.  My solutions will follow directly from known mathematical results, and observation of the physical world.  I work out each problem from scratch. I teach myself the necessary math, mostly out of old Dover press paperbacks.

Patents and Mathematics
The second reason is about mathematics.  All mathematical results follow inescapably from the definitions and rules of manipulation.  (Which is not to say easy.)  A valid result is one which can [and will] be obtained independently by anyone [and everyone].  The proof is demonstration.  The bracketed words are the heart of the matter.

Not everyone is clear about this.  That is a problem, and each person has to decide how to handle it.   I choose not to be slowed down.  I do not have time to pretend that a person can own math.  I sequester myself; I will not copy another person's mind. I have always worked this way, and I love it.  The history of mathematics is free to use, and any results I derive from it are equally free.  So therein I will work.

I am not overly fond of Modern Abstract Mathematics.  Proponents often confuse themselves with the tools they advocate, and say what anyone can understand  in a way that only a few people can.  The proofs usually run backward, never identifying the structure of the problem.  So I practice going backward in time, to obtain the answer.  Each time I go back, what I find is worth the trip.

There is more at stake than money, time, or the enjoyment of mathematics.  Math belongs to no one.  We are ethically responsible to make it accessible.  We also have a rational responsibility.  Mathematics is a logically elaborated structure; within it appeal to authority is baseless.  Any declaration of ownership can be shown to be invalid.
The last two sentences are exact.  There is no other ground to take.

It's alright.  I won't use or examine methods which claim to ownership.  If you Own algorithms, I won't read your paper; it can sit quiet on the shelf. I do not want to know anything about what you are up to.  If we stumble into each other, there will be no question.   All I will need is a chalkboard.

Onward.

Example, With Train
 Recently, I was reading a paper on efficient computation of caustics.  Here is where I stopped:
"Let's forget about reflection for a moment and see how transmitted photons are refracted according to Snell's Law, which states that:  η1 sin θ1 = η2 sin θ2,   otherwise written as:
IOR = η1/η 2 = sin θ2/sinθ1
In the preceding equations, η1 and η2 are the indices of refraction for the respective materials, and θ1 and θ2 are the incident and refraction angles, as shown in Figure 2-2. The index of refraction, IOR, can then simply be written as the ratio of the sines of the angles of the incident and refracted rays.
Snell's Law is not easy to code with this formulation, because it only imposes one restriction, making the computation of the refracted ray nontrivial. Assuming that the incident, transmitted, and surface normal rays are co-planar, a variety of coder-friendly formulas can be used, such as the one in Foley et al. 1996: [....]"
[from, http://http.developer.nvidia.com/GPUGems/gpugems_ch02.html . Italics mine.  Diagram omited.]

I have left out the next line.  I glanced at it, but it was unfamiliar so I stopped, closed the paper, and began working from scratch.  The citation was just the icing.

   This is from a book called GPUGems which is available for free on nVidia's website. This is a great idea, and I hope more companies catch on.  I work alone on a limited budget and the GPUGems series, and nVidia in general, distinguish themselves for free numerical methods, heaped with examples and explanations.  When I am employed, one of my first treats to myself will be to purchase the whole series.
It makes me think selling numerical methods is easy, as long as you don't patent them.  You print them, and then you can sell them.  People will buy them so they can use them.  There's a word for this kind of cooperative-competitive market model, but I just can't remember what it is....


In this case, I do not think there is a problem.  Citation is essential to research and the propagation of knowledge.  I can investigate further if I want, and the author is a good professional  neighbor to boot.  But why don't we solve the problem anyway?  How do people resist?

Problem:  Assuming that the incident, transmitted, and surface normal rays are co-planar, derive a [varitey of] coder-friendly formula[s] for the direction of the transmitted ray.
___________
Update:  I am deeply indebted to Professor Herbert Gross for his clear and concise language.
  Inescapable at last supplies what is wanting in the troublesome expression, self-evident.
Professor Gross' video lectures at MIT's Open Course Ware can be found here.