开发者

2D optical ray tracing

I am working on program that demonstrate how various lens works and for that I needed a 2D ray tracing library or algorithm.

I searched for ray tracing and mostly found things that u开发者_StackOverflow中文版ses to create a scene in 3D, while my requirement is a simple interactive lens simulation.

So any point towards 2D ray tracing algorithm or library is welcome. I am using Python.


I started this group because I'm interested in this kind of thing,

http://groups.google.com/group/python-ray-tracing-community/web/list-of-python-statistical-ray-tracers .

Here you will find a (non-exhaustive) list of python ray tracers that should point you in the right direction. I also have a ray tracer written in Python that does what you need but it has not yet been released!


This problem has two parts:

  1. Finding out what the ray hits
  2. Calculating the ray after refraction

For finding out what the ray hits I'd discretize the form of the lens. So you just need to test against straight lines.

Pseudo-code to find the first hit:

Line hitLine=null;
double minA=+Infinity;
foreach(line in Lines)
{
  Solve (Ray.Start + a*Ray.Direction) == (line.P1+b*(line.P2-line.P1)) for a and b
  if(0<=b<=1) //hit the line-piece
  {
    if(0<=a<minA)
    {
      minA=a;
      hitLine=line;
    }
  }
}

On the hit apply the vector version snellius-law to the ray-direction and set the new starting point to the point of incident. Then start raytracing again from that point.

You also need to take care that the ray doesn't hit the same line-piece immediately again. Do that by either blacklisting that one line-piece or just setting the position as a bit beyond the line-piece (position+=epsilon*direction)

Repeat until there are no more hits i.e. the ray leaves the box.


Not sure that solves your problem, but at least for the "find out what the ray hits" you can always "embed" a 2D scene into a 3D scene by making each line (x0,y0)-(x1,y0) into a quad (x0,y0,z=0)-(x1,y1,z=1), then change all yours rays (ex,ey)+r*(dx,dy) into a "3D" ray (ex,ey,z=0.5)+r*(dx,dy,0.f). Then you can use any 3D ray tracing library like embree or optix to trace those rays and find the "quads" that were hit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜