My First Open Source Contribution

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.
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.

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.

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.

- 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:

- 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. ๐

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