Sphere Signed Distance Function
Implement a function to query the signed distance function (SDF) for a sphere, which is crucial in 3D reconstruction and surface representations. This function calculates the distance from any point in space to the nearest surface of the sphere.
The concept of SDF is essential in understanding how to represent and manipulate 3D objects, as it enables powerful operations like Boolean combinations, morphing, and ray marching. The SDF for a sphere can be understood as the difference between the Euclidean distance from a point to the sphere's center and the sphere's radius.
To compute the SDF for a sphere, consider the following steps:
- Calculate the Euclidean distance between the query point p and the sphere's center c.
- Subtract the sphere's radius r from this distance to obtain the signed distance.
This technique is widely used in computer vision and graphics applications.
Example:
sphere_sdf([2, 0, 0], [0, 0, 0], 1)
1.0
Point at [2,0,0], sphere centered at origin with radius 1: distance to center = sqrt(2² + 0² + 0²) = 2
- SDF = 2 - 1 = 1.0 Point is 1 unit outside the sphere.
Constraints:
- point: query point [x, y, z]
- center: sphere center [cx, cy, cz]
- radius: sphere radius
- Return signed distance, rounded to 4 decimal places
- Background Knowledge
A signed distance function (SDF) is a scalar function that, given a point in space p∈R3, returns the (signed) shortest distance to a surface. For a solid object, the sign convention is usually: negative inside the object, zero on the surface, and positive outside. The surface itself is the zero level set of the SDF: all points p such that SDF(p)=0. This makes SDFs powerful implicit shape representations in graphics, 3D reconstruction, and robotics.
For a sphere, the geometry is particularly simple: a sphere is the set of points at a fixed distance r (the radius) from a center c. The Euclidean distance ∥p−c∥ already tells you how far a point p is from the center. To get distance to the surface, you subtract the radius: if ∥p−c∥>r, you are outside; if it’s equal to r, you are on the surface; if it’s less than r, you are inside. Making this signed naturally leads to the formula given in the problem.
- Algorithm/Approach
The general pattern for SDF queries is:
- Compute the unsigned distance from the query point p to the shape’s defining primitive (for a sphere: distance to center).
- Adjust this distance so that it becomes zero on the surface, and its sign matches the “inside vs outside” convention.
- Return this scalar value as the SDF value.
For a sphere, this boils down to:
- Compute d=∥p−c∥.
- Return d−r as the SDF value.
- Step-by-Step Strategy
Assume 3D vectors with components (x,y,z):
- Input:
- Query point p=(px,py,pz)
- Sphere center c=(cx,cy,cz)
- Sphere radius r
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.