如何建立射线/球体相交的二次方程?
我正在研究光线追踪器的数学原理,但我并没有遵循我读过的关于该主题的几乎每一篇文章中所做的转变。这就是我所拥有的:
球体公式:
(X - Cx)^2 + (Y - Cy)^2 + (Z - Cz)^2 - R^2 = 0
其中 R 是半径,C 是中心,X、Y、Z 都是球体内的点。
直线公式:
X + DxT、Y + DyT、Z + DzT
其中 D 是直线的归一化方向向量,X、Y、Z 是直线上的所有点,T 是直线上某个点的参数线。
将直线的分量代入球方程,可得:
(X + DxT - Cx)^2 + (Y + DyT - Cy)^2 + (Z + DzT - Cz)^2 - R^2 = 0
我遵循了这一点之前的所有内容(至少我认为我这样做了),但是我读过的每一篇教程都会从它跳转到二次方程而不解释它(这是从一个网站复制的,所以术语是与我的示例略有不同):
A = Xd^2 + Yd^2 + Zd^2
B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2
我知道如何使用二次公式求解 T,但我不明白它们是如何解决的由上式可得二次方程。我假设这只是我早已忘记的一些常见数学知识,但在谷歌上搜索“如何建立二次方程”也没有真正产生任何结果。
我真的很想在继续之前了解如何完成这一步,因为我不喜欢编写我不完全掌握的代码。
I'm researching the math for a ray tracer, but I'm not following a transition that is made in just about every article I've read on the subject. This is what I have:
Formula for a sphere:
(X - Cx)^2 + (Y - Cy)^2 + (Z - Cz)^2 - R^2 = 0
Where R is the radius, C is the center, and X, Y, Z are all the points in the sphere.
Formula for a line:
X + DxT, Y + DyT, Z + DzT
where D is a normalized direction vector for the line and X, Y, Z are all the points on the line, and T is a parameter for some point on the line.
By substituting the components of the line into the sphere equation, we get:
(X + DxT - Cx)^2 + (Y + DyT - Cy)^2 + (Z + DzT - Cz)^2 - R^2 = 0
I follow everything up to that point (at least I think I do), but then every tutorial I've read makes a jump from that to a quadratic equation without explaining it (this is copied from one of the sites, so the terms are a little different from my example):
A = Xd^2 + Yd^2 + Zd^2
B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2
I get how to then solve for T using the quadratic formula, but I don't understand how they get to the quadratic equation from the above formulas. I'm assuming that's just some piece of common math knowledge that I've long since forgotten, but googling for "How to set up a quadratic equation" hasn't really yielded anything either.
I'd really like to understand how to get to this step before moving on, as I don't like writing code I don't fully grasp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是每个步骤的详细演练;希望这会让事情变得清晰。三维球体的方程为:
(xa)^2 + (yb)^2 + (zc)^2 = r^2
其中
是球体的中心,
r
是球体的半径。如果点
满足该方程,则它位于球体上。射线的参数方程为:
X = xo + xd*t
Y = yo + yd*t
Z = zo + zd*t
其中
是射线的原点,
是相机射线的方向。为了找到交点,我们想要看看射线上的哪些点与球体上的点相同。所以我们将射线方程代入球体方程:
(xo + xd*t - a)^2 + (yo + yd*t - b)^2 + (zo + zd*t - c)^2 = r^2
扩展为:
请注意,这是一个形式为
At^2 + Bt + C = 0
的二次方程,其中:A = (xd^2 + yd^2 + zd^2)
B = [2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]]
我们可以应用未知变量的一般二次公式,即是:
B^2 - 4AC
部分称为“判别式”。根据判别式的值,我们将得到该方程的零个、一个或两个解:如果小于零,则解为虚数,并且射线和球体在实数中不相交
如果它等于 0,那么光线正好在 1 点处与球体相交(它与球体正好相切)。
如果它等于
如果它大于零,则光线正好在 2 个点处与球体相交。
如果判别式表明没有解决方案,那么您就完成了!射线不与球体相交。如果判别式指示至少一个解,您可以求解
t
以确定交点。这两个解是:较小的解是光线第一次撞击球体的点。
Here's a detailed walkthrough of each step; hopefully this will make things crystal clear. The equation for a three-dimensional sphere is:
(x-a)^2 + (y-b)^2 + (z-c)^2 = r^2
with
<a, b, c>
being the center of the sphere andr
its radius. The point<x, y, z>
is on the sphere if it satisfies this equation.The parametric equations for a ray are:
X = xo + xd*t
Y = yo + yd*t
Z = zo + zd*t
where
<xo, yo, zo>
is the origin of the ray, and<xd,yd,yd>
is camera ray's direction.To find the intersection, we want to see what points on the ray are the same as points on the sphere. So we substitute the ray equation into the sphere equation:
(xo + xd*t - a)^2 + (yo + yd*t - b)^2 + (zo + zd*t - c)^2 = r^2
which expands to:
Notice that this is a quadratic equation in the form
At^2 + Bt + C = 0
, with:A = (xd^2 + yd^2 + zd^2)
B = [2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]]
C = [(xo - a)^2 + (yo - b)^2 + (zo - c^)2 - r^2]
We can apply the general quadratic formula for an unknown variable, which is:
The
B^2 - 4AC
portion is called the "discriminant". Depending on the value of the discriminant, we will get zero, one, or two solutions to this equation:If it is less than zero, the solution is an imaginary number, and the ray and sphere do not intersect in the real plane.
If it is equal to zero, then the ray intersects the sphere at exactly 1 point (it is exactly tangent to the sphere).
If it is greater than zero, then the ray intersects the sphere at exactly 2 points.
If the discriminant indicates that there's no solution, then you're done! The ray doesn't intersect the sphere. If the discriminant indicates at least one solution, you can solve for
t
to determine the intersection point. The two solutions are:The smaller solution is the point at which the ray first hits the sphere.
从这里开始:
展开三个平方项,因此您有一个很长的表达式:(
这是由于使用公式
(x+y+z)^2 = x^2 + y^2 + z^2 + 2xy + 2xz + 2yz
)然后分组,得到 T^2、T 和 1 的因子:
这些因子是上面给出的 A、B、C。这是 T 的二次方程,可以使用二次公式求解。
From here:
Expand the three squared terms, so you have a long expression:
(This is due to using the formula
(x+y+z)^2 = x^2 + y^2 + z^2 + 2xy + 2xz + 2yz
)Then group so you have factors of T^2, T, and 1:
These factors are the A,B,C given above. This is a quadratic equation for T, and can be solved using the quadratic formula.