2D 光线追踪

发布于 2024-10-05 13:02:39 字数 239 浏览 2 评论 0原文

我正在开发一个程序来演示各种镜头的工作原理以及我所需要的 2D 光线追踪库或算法。

我搜索了光线追踪,主要找到了用于创建 3D 场景的东西,而我的要求是简单的交互式镜头模拟。

因此,任何有关 2D 光线追踪算法或库的观点都是受欢迎的。我正在使用Python。

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 uses 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

信仰 2024-10-12 13:02:39

我创建这个小组是因为我对这种事情感兴趣,

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

在这里,您将找到一个(非详尽的)Python 光线追踪器列表,它应该为您指明正确的方向。我还有一个用 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!

Smile简单爱 2024-10-12 13:02:39

这个问题有两个部分:

  1. 找出光线击中的物体
  2. 计算折射后的光线

为了找出光线击中的物体,我将离散透镜的形状。所以你只需要针对直线进行测试。

找到第一个命中的伪代码:

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;
    }
  }
}

在命中时,将矢量版本 snellius-law 应用于射线方向,并将新的起点设置为事件点。然后从该点再次开始光线追踪。

您还需要注意光线不要立即再次击中同一个线段。通过将该一条线段列入黑名单或仅将位置设置为线段之外一点(位置+=ε*方向)来实现这一点,

重复直到不再有命中,即光线离开框。

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.

得不到的就毁灭 2024-10-12 13:02:39

不确定这是否能解决您的问题,但至少对于“找出光线击中的内容”,您始终可以通过将每条线 (x0,y0)-(x1,y0) 制作为四边形 (x0,y0,z=0)-(x1,y1,z=1),然后将所有光线 (ex,ey)+r*(dx,dy) 更改为“3D”光线 (ex,ey, z=0.5)+r*(dx,dy,0.f)。然后,您可以使用任何 3D 光线追踪库(如 embree 或 optix)来追踪这些光线并找到被击中的“四边形”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文