本文主要是介绍Signed distance fields (SDFs) and Truncated Signed Distance Field(TSDF),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. Signed distance fields (SDFs)
笔记来源:
[1] Signed distance fields (SDFs)
[2] Signed Distance Function (SDF): Implicit curves or surfaces
[3] Ray Marching and Signed Distance Functions
[4] Truncated Signed Distance Function
[5] Wiki/Signed distance function
1.1 What is SDF?
SDF是一种物体表面或形状的隐式表示方法
Signed distance functions, or SDFs for short, when passed the coordinates of a point in space, return the shortest distance between that point and some surface. The sign of the return value indicates whether the point is inside that surface or outside (hence signed distance function).
SDF输入:平面/空间点的位置
SDF输出:该点到最近的物体表面的距离
例1:平面内2D圆的SDF表示
圆心: ( a , b ) 、半径: r = 1 圆的表达式: ( x − a ) 2 + ( y − b ) 2 − 1 = 0 d ( x , y ) = ( x − a ) 2 + ( x − b ) 2 − 1 \text{圆心:}(a,b)、\text{半径:}r=1\\ ~\\ \text{圆的表达式}:(x-a)^2+(y-b)^2-1=0\\ ~\\ d(x,y)=\sqrt{(x-a)^2+(x-b)^2 }-1 圆心:(a,b)、半径:r=1 圆的表达式:(x−a)2+(y−b)2−1=0 d(x,y)=(x−a)2+(x−b)2−1
平面内各个点到圆的最近距离,若该点在圆外则距离为正( d ( x , y ) > 0 d(x,y)\gt 0 d(x,y)>0),若该点刚好在圆上则距离为0( d ( x , y ) = 0 d(x,y)=0 d(x,y)=0),若该点在圆内则距离为负( d ( x , y ) < 0 d(x,y)\lt 0 d(x,y)<0)
例2:点到线段的距离
一点P到线段AB的距离等价于线段QP的长度,其中Q为线段AB上离点P最近的点
于是线段AB的SDF表示:
S A B ( x , y ) = λ A + ( 1 − λ ) B S_{AB}(x,y)=\lambda A+(1-\lambda)B SAB(x,y)=λA+(1−λ)B
其中 λ \lambda λ为
w ⃗ = P − A 、 v ⃗ = B − A λ = < w ⃗ , v ⃗ > ∣ ∣ v ⃗ ∣ ∣ 2 \vec{w}=P-A、\vec{v}=B-A\\ ~\\ \lambda=\frac{<\vec{w},\vec{v}>}{||\vec{v}||^2} w=P−A、v=B−A λ=∣∣v∣∣2<w,v>
如果 0 ≤ λ ≤ 1 0\leq \lambda\leq 1 0≤λ≤1,则点在线段AB上,若 λ < 0 \lambda\lt 0 λ<0或者 λ > 1 \lambda\gt1 λ>1,则点在直线AB上但不在线段AB上
类似的,在3D空间中,我们也可以通过单个SDF表示物体表面
下图来自:Inigo Quilez/articles/distance functions
我们也可以通过各个简单物体的SDF进行布尔运算表示任何复杂物体表面
Constructive Solid Geometry(CSG)
Constructive solid geometry, or CSG for short, is a method of creating complex geometric shapes from simple ones via boolean operations. CSG is built on 3 primitive operations: intersection ( ∩ ), union ( ∪ ), and difference ( − )
1.2 Ray Marching
Ray Tracing 和 Ray Marching 的区别
In raytracing, the scene is typically defined in terms of explicit geometry: triangles, spheres, etc. To find the intersection between the view ray and the scene, we do a series of geometric intersection tests: where does this ray intersect with this triangle, if at all? What about this one? What about this sphere?
In raymarching, the entire scene is defined in terms of a signed distance function. To find the intersection between the view ray and the scene, we start at the camera, and move a point along the view ray, bit by bit. At each step, we ask “Is this point inside the scene surface?”, or alternately phrased, “Does the SDF evaluate to a negative number at this point?“. If it does, we’re done! We hit something. If it’s not, we keep going up to some maximum number of steps along the ray.
We could just step along a very small increment of the view ray every time, but we can do much better than this (both in terms of speed and in terms of accuracy) using “sphere tracing”. Instead of taking a tiny step, we take the maximum step we know is safe without going through the surface: we step by the distance to the surface, which the SDF provides us!
In this diagram, p 0 p_0 p0 is the camera. The blue line lies along the ray direction cast from the camera through the view plane. The first step taken is quite large: it steps by the shortest distance to the surface. Since the point on the surface closest to p 0 p_0 p0 doesn’t lie along the view ray, we keep stepping until we eventually get to the surface, at p 4 p_4 p4
在1.1小节中我们表示完了物体表面,接下来我们考虑如何画出SDF的结果,即渲染
物体表面最终呈现的颜色是由光线反射角度、材质等等决定的
我们需要回答两个问题:
(1)Does the ray hit the surface?
(2)If so,where,at what angle is the surface?
If the ray doesn’t hit the surface, we just colour in the pixel white (or transparent).
If it does, we need to know what angle the surface is at to know how to shade it.
先来看看第一个问题ray是否击中了物体表面?
我们沿着ray向前进,计算ray上一些点离最近物体表面的距离,如果距离为0表明ray与物体相交,若沿着ray前进时计算的距离没有为0的情况,意味着这条ray与物体并没有相交,通常在这种情况下,我们会在前进一定步数后截断,不再对该条ray上的点计算距离
在计算机图形学中大多数 lighting model 使用表面法向量来计算某种材质表面上某个点的颜色,当scene由显式几何定义时,如三角面元,法向量在三角面元的三个顶点指定,其他点的法向量由插值计算得到。当scene由SDF定义时,我们使用梯度来计算法向量。
如果ray击中了物体表面,那么我们如何计算表面的法向量?
方案一:可以由深度图计算梯度进而得到每个点的法向量
方案二:直接对SDF计算梯度
有了物体表面法向量,之后就可以计算该物体在具体场景中的颜色了,本篇仅聚焦于SDF,故不再展开
2.Truncated Signed Distance Field(TSDF)
Truncated Signed Distance Function (TSDF) is a simplified way to represent 3D shapes or surfaces, which stores the distance information for points near the surface up to a certain threshold. It is used in applications like 3D reconstruction and surface rendering to create accurate and efficient representations of 3D scenes.
Truncation: In a Truncated Signed Distance Function, we limit or “truncate” the range of distances that we care about. This means that we only consider distances up to a certain threshold, and ignore any distances beyond that. Truncation is useful because it simplifies the representation of the 3D shape and reduces the amount of data we need to store.
This trunction is essential to limit the influence of voxels far from the surface, ensuring more accurate integration close to the surface.
The main purpose of TSDF is to represent and reconstruct 3D surfaces or shapes from different measurements or observations, like depth images from a 3D camera or point clouds from a LiDAR sensor. By combining these measurements using TSDF, we can create a more accurate and complete representation of the 3D surface.
下图来自:截断符号距离 | TSDF, Truncated Signed Distance Function
这篇关于Signed distance fields (SDFs) and Truncated Signed Distance Field(TSDF)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!