Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 44

Introduction to Signed Distance Functions (SDFs)

Recommended resources

Hello! Welcome back to our course on diffusion models.

In our last lesson, we dove into Neural Radiance Fields (NeRFs), exploring how a simple MLP can represent a 3D scene's color and density to synthesize photorealistic novel views. This introduced the powerful concept of implicit neural representations.

Today, we will explore another fundamental implicit representation for 3D geometry: Signed Distance Functions (SDFs). Your learning outcome for this lesson is to explain signed distance functions (SDFs) for 3D shape representation. We will cover the geometric intuition behind SDFs, see how they can be represented by neural networks, and compare their properties to the NeRFs we've just studied.

Understanding SDFs is crucial. They provide a different, often more direct, way to represent geometry than density fields. This representation is at the core of many advanced 3D generative modeling techniques, including methods like Score Distillation Sampling (SDS) that we will encounter later in the course when we tackle text-to-3D generation.

1. The Geometry of Signed Distance

At its core, a Signed Distance Function is a very elegant way to describe a shape. Instead of listing the points that make up a shape's surface, an SDF defines the shape by telling you, for any point in space, how far it is from the surface.

Formally, an SDF is a function that takes a 3D coordinate and returns a scalar value representing the shortest distance to a surface . The "signed" part is key:

  • If is outside the shape, the distance is positive.
  • If is inside the shape, the distance is negative.
  • If is exactly on the surface, the distance is zero.

This means the surface itself is implicitly defined as the zero level-set of the function: .

This video provides an excellent mathematical introduction to SDFs. It starts by motivating their use with a rendering technique called ray-marching, defines them formally, and shows how they can be manipulated.

Signed Distance Functions & Ray-Marching

Watch these sections from 'Signed Distance Functions & Ray-Marching' by Sum and Product to build a solid foundation. The video explains the core concepts of ray-marching, the formal definition of an SDF, and how simple SDFs can be combined to create complex shapes.

Watch the following segments: Introduction to Ray-Marching (00:00 - 04:51): Understand how knowing the distance to the nearest object can make rendering efficient. SDF Definition (04:51 - 06:55): Focus on the formal definition, the meaning of the sign, and the concept of level sets. Transforming and Combining SDFs (14:19 - 18:48): Observe how boolean operations (union, intersection, difference) can be approximated using min and max operations on SDF values. This demonstrates their compositional power.

As you saw, one of the elegant properties of SDFs is their compositionality. You can combine primitive shapes using simple mathematical operations:

  • Union (): min(sdf_A(x), sdf_B(x))
  • Intersection (): max(sdf_A(x), sdf_B(x))
  • Difference (A \ B): max(sdf_A(x), -sdf_B(x))

While these are approximations that can have issues at sharp corners, they provide a powerful procedural modeling toolkit.


2. From Analytical Functions to Neural Networks: DeepSDF

The examples in the video (circles, rectangles) have simple analytical formulas. But how can we represent the SDF for a complex, arbitrary shape like a car or a chair? This is where neural networks come in.

The seminal work DeepSDF proposed using a Multi-Layer Perceptron (MLP) to learn a continuous SDF for a class of shapes. The idea is to train the network to act as the function , where are the network's weights.

The simplest version would train one network per shape. However, the real power comes from training a single network to represent an entire category of shapes (e.g., all chairs). This is achieved by conditioning the network on a latent code , a low-dimensional vector that captures the unique geometry of a specific shape instance.

The network architecture then becomes: , where is the predicted signed distance.

DeepSDF Architectures: Single Shape vs. Coded Shape
Diagram illustrating the difference between a DeepSDF for a single shape (left) and a conditional model for multiple shapes (right). The latter takes an additional latent 'Code' vector as input, allowing it to represent a whole family of objects.

The following reading explains this concept clearly, detailing how the model is trained and how the latent codes are learned.

3D Generative Modeling with DeepSDF

This article, '3D Generative Modeling with DeepSDF', gives a concise and clear explanation of the DeepSDF architecture and its training paradigm. It's perfect for understanding the leap from geometric SDFs to learned neural representations.

Read the sections 'signed distance functions' and '[DeepSDF: Learning Continuous Signed Distance Functions for Shape Representation]'. Focus on how the training problem is framed as a regression task and how the 'auto-decoder' architecture is used to learn the latent shape codes.

The training process for DeepSDF is a supervised learning task:

  1. Data: For a given shape, we need a large set of 3D points and their corresponding ground-truth signed distance values . This data is typically pre-computed from a high-quality mesh.
  2. Model: The MLP takes a point and the shape's latent code as input and predicts a distance value .
  3. Loss: The network weights and the latent codes are optimized simultaneously to minimize a regression loss, typically the L1 loss between the predicted and true distances: .

For more detail on the specific loss function, which often includes a clamp operation to focus the network's capacity on accurately representing the region near the surface, you can refer to the "DeepSDF" section (3.1) in the report Neural Signed Distance Functions for 3D Shape ....

Test your understanding!

What does a single training sample for a conditional DeepSDF model consist of? And why is the training approach for the latent codes called an "auto-decoder"?

Show answer

A single training sample consists of a 3D point , its ground-truth signed distance value , and the index of the shape it belongs to (so the model can use the corresponding latent code ).

It's called an "auto-decoder" because, unlike a standard autoencoder which has both an encoder and a decoder, this architecture only has a decoder. The latent codes are not produced by an encoder network; instead, they are treated as trainable parameters and optimized directly via backpropagation along with the network weights. The model learns to "decode" the shape from these optimized latent vectors.


3. Practical Usage: Rendering and Mesh Extraction

Once we have a trained neural SDF, we can use it to visualize the 3D shape. There are two primary methods:

  1. Rendering with Ray Marching: As introduced in the first video, we can render the shape directly. For each pixel, we march a ray from the camera. At each step, we query the SDF to find the distance to the surface. This value tells us the maximum safe step size we can take along the ray. We repeat this until we are very close to the zero-crossing, which indicates we've hit the surface.

  2. Mesh Extraction with Marching Cubes: A more common approach is to first extract an explicit mesh representation from the implicit function. The Marching Cubes algorithm is perfect for this. We evaluate the SDF on a dense 3D grid of points. The algorithm then identifies all the grid cells where the SDF transitions from negative to positive, meaning the surface passes through them, and generates the corresponding triangle mesh.

A key advantage of SDFs for rendering is the ease of calculating surface normals. The normal at a surface point is simply the gradient of the SDF at that point, . For a neural SDF, this gradient can be calculated analytically and efficiently using autodifferentiation. These normals are essential for realistic lighting calculations.

This short clip provides a great summary of how a trained DeepSDF is used in practice.

CSC2547 DeepSDF Learning Continuous Signed Distance Functions for Shape Representation

This segment from a presentation on DeepSDF summarizes the two main ways to use a learned SDF: direct rendering via raycasting and mesh extraction via Marching Cubes.

Watch from 06:05 to 06:47. Note the distinction between raycasting for direct rendering and Marching Cubes for mesh extraction.


4. SDFs vs. NeRFs

Let's compare the two implicit representations we've now seen.

Feature Neural Radiance Field (NeRF) Signed Distance Function (SDF)
Primary Output Volume Density () and Color () Signed Distance ()
Surface Definition Implicitly defined by regions of high density. Can be "fuzzy" or semi-transparent. Sharply defined as the zero level-set of the function. Guarantees a watertight surface.
Rendering Method Volume Rendering (integrating color and density contributions along a ray). Ray Marching / Sphere Tracing (taking discrete steps towards the surface).
Surface Normals Not directly available. Can be approximated from the gradient of the density field, but can be noisy. Naturally derived from the gradient of the distance function (), providing high-quality normals.
Mesh Extraction Possible, but extracting a high-quality mesh from a density field can be challenging. Straightforward and robust using algorithms like Marching Cubes.

The learned latent space of a conditional DeepSDF is also very powerful for generative tasks. By interpolating between the latent codes of two shapes, you can generate smooth transitions between them, as shown here.

DeepSDF: Learned Continuous Signed Distance Functions for Shape Representation
This image demonstrates linear interpolation within the learned latent space of DeepSDF. By moving through the space between a "chair" latent code and a "car" latent code (a simplified example), the model can generate plausible intermediate shapes, highlighting the power of this continuous representation for generative modeling.

Conclusion

In this lesson, we've defined Signed Distance Functions as a powerful implicit representation for 3D geometry. We saw how they work, how they can be learned with neural networks like DeepSDF, and how they compare to NeRFs.

Key Takeaways:

  • An SDF represents a shape by a function that gives the distance to the surface from any point in space, with a sign indicating inside/outside.
  • The surface is defined as the zero level-set of the SDF.
  • DeepSDF uses an MLP to learn a continuous SDF, often conditioned on a latent vector to represent an entire class of shapes.
  • SDFs are well-suited for clean surface extraction (e.g., via Marching Cubes) and provide high-quality surface normals via the gradient, which is critical for rendering.
  • Compared to NeRFs, SDFs offer a more direct representation of geometry, while NeRFs are designed to represent appearance and volumetric effects for view synthesis.

Next Lesson Preview:

Having explored both NeRFs and SDFs, we now have a solid conceptual understanding of the two most important implicit representations for 3D generation. We will now shift gears back to implementation. In the next lesson, we will begin the hands-on process of building a NeRF renderer from scratch, starting with the very first component: implementing ray generation for a pinhole camera model.