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 and the sphere's center .
- Subtract the sphere's radius 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
- Inputs:
pointis the query location ,centeris the sphere's centre , andradiusis . - Offset: the vector from the centre to the point is .
- Distance to the centre: its Euclidean length is .
- Signed distance: subtract the radius, , which rounded to 4 decimal places gives
1.0. - Reading the sign: a positive value means the point lies outside the sphere, exactly 1 unit beyond the surface along the axis. A point on the surface such as gives , and the centre itself gives , the deepest point inside.
- Notice the result is a single number: how far the nearest surface is and on which side, not the direction.
Constraints
- point: query point [x, y, z]
- center: sphere center [cx, cy, cz]
- radius: sphere radius
- Return signed distance, rounded to 4 decimal places