CW-ToF rendering (offline)

This tutorial renders a continuous-wave time-of-flight (CW-ToF) measurement of the Cornell box with CWToFPathTracerInline, with antithetic sampling.

A CW-ToF camera modulates its light and weights each light path by the modulation at the path’s length \(\ell\): here \(\cos(2\pi\,\ell / \lambda)\) with a wavelength \(\lambda = 0.01\), a hundredth of the box’s size. The image shows only the indirect light (computeDirect off), which is what causes multipath errors in real CW-ToF cameras. With such a short wavelength most of it cancels, leaving rings and stripes where the path length changes slowly: around the points of the walls closest to the camera, and along the edges.

It uses the same scene file as the other tutorials (see Modulated light).

Indirect CW-ToF measurement of the Cornell box

The three images below show what this measurement keeps. With the modulation shifted to \([0, 1]\) (unsignedModulation), the full measurement (left) is dominated by the direct light, which shows the rings of the modulation clearly; its indirect part alone (middle) is smooth and dim. The signed, zero-mean measurement of this tutorial (right, as above) removes the constant part of the modulation, so only the indirect light that does not cancel is left. Each image has 8,192 samples per pixel; the script is modulated_components.py (python modulated_components.py cwtof).

CW-ToF with the modulation in [0, 1] (direct + indirect, indirect) and in [-1, 1] (indirect)

1. Load the scene

import matplotlib
matplotlib.use("Agg")  # no window
import matplotlib.pyplot as plt
import numpy as np
import falcorcomp as falcor

testbed = falcor.Testbed(create_window=False)
testbed.load_scene("cornell-box/scene-v4-nolight.pbrt")
testbed.resize_frame_buffer(512, 512)
testbed.scene.camera.aspectRatio = 1.0
testbed.clock.pause()

2. Build the render graph

VBufferRT finds the primary hits. LaserLight places the light, and the execution edge add_edge("Light", "Tracer") makes it run first: with isLightSourceLaser = false it is a point light, and laserCollocated puts it at the camera, as in a CW-ToF camera. The tracer’s modulation is set by:

  • waveform and modulationWavelength: the modulation \(w(\ell / \lambda - \phi)\), with \(\lambda\) in scene units. phase (\(\phi\), in periods) shifts it; real CW-ToF cameras take four measurements with phases 0, 0.25, 0.5 and 0.75 to recover depth.

  • useAntitheticSampling: pair every sampled path with an antithetic path half a wavelength longer or shorter, whose modulation has the opposite sign (see Antithetic sampling).

  • useSingleChannel: keep the luminance, as a single signed value per pixel.

AccumulatePass averages the frames.

graph = testbed.create_render_graph("CWToF")
graph.create_pass("VBuffer", "VBufferRT", {"samplePattern": "Center", "sampleCount": 1})
# A point light at the camera, as in a CW-ToF camera.
graph.create_pass("Light", "LaserLight", {
    "isLightSourceLaser": False, "laserCollocated": True, "laserPower": [10.0, 10.0, 10.0],
})
graph.create_pass("Tracer", "CWToFPathTracerInline", {
    "samplesPerPixel": 8, "maxBounces": 3,
    "computeDirect": False,  # indirect light only: the multipath part of the measurement
    "waveform": "cos", "modulationWavelength": 0.01, "phase": 0.0,
    "useAntitheticSampling": True,
    "useSingleChannel": True, "singleChannel": "luminance",
})
graph.create_pass("Accumulate", "AccumulatePass", {"precisionMode": "SingleCompensated"})

graph.add_edge("VBuffer.vbuffer", "Tracer.vbuffer")
graph.add_edge("VBuffer.viewW", "Tracer.viewW")
graph.add_edge("Light", "Tracer")
graph.add_edge("Tracer.color", "Accumulate.input")
graph.mark_output("Accumulate.output")
testbed.render_graph = graph

3. Render

for _ in range(1024):  # 1024 frames x 8 spp = 8192 spp
    testbed.frame()

4. Save the image

The measurement is signed, so it is saved as a NumPy array and shown with a diverging colormap whose range is the 95th percentile of its magnitude.

image = graph.get_output("Accumulate.output").to_numpy()[..., 0]  # one channel (luminance)
np.save("cwtof.npy", image)
# The measurement is signed: show it with a diverging colormap, 0 in white.
limit = np.percentile(np.abs(image), 95)
plt.imsave("cwtof.png", image, cmap="bwr", vmin=-limit, vmax=limit)

The full script: cwtof_offline.py.

For an equal-time comparison of antithetic and naive sampling, and how it changes with the modulation wavelength, see Antithetic sampling for modulated light.