Home

My First Open Source Contribution

Doodle of a blob-shaped doctor with a stethoscope, a forced smile, a blue tear and a thumbs up next to a monitor with four black panels
Four views of a CT scan. On my computer, all four were black. Everything is fine.

TL;DR

  • My medical scan viewer showed black screens on my graphics card.
  • The cause: one division by zero deep inside vtk.js, an open source 3D library.
  • The fix: two lines. They're now part of vtk.js 38.0.3, and they're my first contribution to a project that isn't mine.

The bug

My viewer shows a CT scan from three sides, plus a 3D view. On my PC (NVIDIA GTX 1070, Chrome on Linux), all four views were black.

  • It only happened when the camera looked exactly along one of the scan's axes.
  • That's precisely how slice views look at a scan.
  • From an angle, everything rendered fine.
Every pixel sends a ray into the box. At an angle, the rays hit the ball. Exactly straight, every ray missed on my graphics card and the screen went black. With my fix, the straight rays hit again.

Why

  • vtk.js draws a volume by sending one ray per pixel into the box that holds the scan.
  • To find where a ray enters the box, it divides: distance to a wall รท the ray's speed toward that wall.
  • Looking straight along an axis, the sideways speed is 0, so the code divides by zero.
  • It expected the answer to be infinity: "this ray never reaches that wall." The spec doesn't promise that: "Dividing by zero does not cause an exception but does result in an unspecified value." (OpenGL ES 3.00)
  • Whatever my graphics card made of it, every ray missed.
Three 3D drawings of a pixel screen sending parallel rays into a box with a ball. Angled rays hit the ball and the screen shows it. Orange rays running exactly along the box pass through it and the screen is black. The same straight rays with the fix hit the ball again.
Left: rays at an angle hit the ball. Middle: rays exactly along the box, before my fix. Every ray misses and the screen stays black. Right: the same rays with my fix.

A patch for one

  • That evening, I patched my own copy of vtk.js. The views came back.
  • For four weeks, my viewer ran on that patch.
  • The catch: it only helped me, and every upgrade could break it.
Doodle of three blob-shaped doctors at their own monitors: one cries at a black screen, one facepalms at a black screen, and one in pixel sunglasses gives a thumbs up at a screen showing a CT scan
Works on my machine.

Proving it

A maintainer can't do much with "it's black on my machine." So I built a test page: one sphere, a straight and an angled camera, and a measurement of how much of the screen lights up.

Grid of four renders. Straight camera, drawn as a horizontal arrow into a cube: 0 percent before, 17.9 percent after. Angled camera, drawn as a diagonal arrow: 18.1 percent before and after. Each lit cell shows a sphere.
How much of the screen lit up on my GTX 1070, with the released vtk.js (before) and with my change (after). The sphere pictures are drawn in software.
  • Straight: 0% before, about 18% after.
  • Angled: fine either way.
  • Why vtk.js's own tests never caught it: they render in software, and in software every camera works.

The fix

  • I opened the pull request late on a Saturday night. My first version skipped the division, one axis at a time.
  • On Monday, maintainer finetjul suggested something simpler:
GitHub review comment by finetjul: Why not simply replace inDir components with infinity wherever a rayDirIS component is (near) 0? The rest of the function logic was fine, no?
The question that made my fix simpler.
  • finetjul was right. I switched, and the final fix is two lines:
vec3 isParallel = vec3(equal(rayDirIS, vec3(0.0)));
vec3 invDir = mix(1.0 / (rayDirIS + isParallel), vec3(infinity), isParallel);
  • Line 1 marks every direction that is exactly zero.
  • Line 2 divides by 1 there instead of 0, then puts infinity in its place.
  • Nothing divides by zero anymore.

Merged

  • As a first-time contributor, I needed a maintainer's OK before the tests could run. All four checks passed.
  • 28 October, 09:00 UTC: approved with "LGTM" (looks good to me).
  • One minute later: merged. Five minutes after that: released in vtk.js 38.0.3. ๐ŸŽ‰
GitHub timeline: finetjul approved the changes with LGTM, the pull request was merged into Kitware master, and the github-actions bot commented that the PR is included in version 38.0.3
Approved, merged, released.

Anyone on vtk.js 38.0.3 or later now gets those two lines.