#Dev

Inigo Quilez Champions Vector-Based Mathematics Over Trigonometry in Computer Graphics

Startups Reporter
4 min read

Veteran graphics programmer Inigo Quilez makes a compelling case for eliminating trigonometric functions from core 3D algorithms, demonstrating how vector operations can achieve more elegant, efficient, and stable solutions.

In the world of computer graphics, where mathematical precision meets visual artistry, Inigo Quilez stands as a seasoned voice advocating for a fundamental shift in how programmers approach spatial computations. With a career spanning decades in demoscene, shader development, and fractal mathematics, Quilez has published a influential argument against the overuse of trigonometric functions in 3D rendering engines.

Quilez, who has been learning computer graphics since 1994, contends that when he encounters trigonometric functions like asin, acos, atan, sin, cos or tan at the core of 3D algorithms, he assumes the code was written by an inexperienced programmer that needs review. This strong stance isn't merely a matter of preference but stems from deep mathematical insight into how vector operations can replace trigonometry with more elegant and efficient solutions.

The fundamental problem Quilez identifies is that trigonometric functions introduce unnecessary complexity, computational expense, and potential numerical instability when working with vectors and geometry. While he acknowledges that trigonometry has its place for data input and feeding algorithms, its emergence deep within algorithm internals is what he takes issue with.

"These days I'm at a point where if I see some asin, acos, atan, sin, cos or tan in the middle of a 3D algorithm, I'll assume the code was written by an inexperienced programmer and that it needs review," Quilez explains in his 2013 article on the topic.

The core of Quilez's argument lies in the power of dot and cross products, which he believes encode all the information needed for orientation-related operations. "These two products respectively capture all that you've learnt about cosine and sine of angles," he writes. "Actually, cosine and sine are really dot and cross products in disguise, which has been taken out of a geometric context and ripped of physicality."

To illustrate his point, Quilez presents a common scenario in graphics programming: orienting an object in a given direction. The conventional approach involves calculating the angle between vectors using the dot product and acos(), computing the rotation axis via the cross product, normalizing it, and then using these values to create a rotation matrix.

This typical implementation contains several mathematical inefficiencies:

  1. An acos() call to extract an angle
  2. An immediate cos() call on the result (essentially computing cos(acos(x)) which equals x)
  3. A normalization operation that may be unnecessary
  4. Clamping operations to handle floating-point precision issues

Quilez demonstrates how this entire process can be rewritten using only vector operations, eliminating trigonometric functions entirely. His optimized solution computes the rotation matrix directly from the dot product and cross product of the vectors, resulting in code that's not only more efficient but also more mathematically elegant.

The benefits of this approach extend beyond performance:

  • Improved numerical stability by eliminating problematic inverse trigonometric functions
  • Reduced computational overhead by removing expensive transcendental function calls
  • More intuitive code that operates directly on vectors rather than intermediate angles
  • Elimination of special case handling for edge cases like parallel vectors

Quilez's work has particular relevance in modern graphics programming where performance optimization remains crucial, especially in real-time rendering contexts like game development and interactive applications. His approach aligns with broader trends in computer science toward more fundamental, first-principles solutions that often yield better results than higher-level abstractions.

The implications of Quilez's methodology extend beyond this specific example. He suggests that many trigonometric identities commonly used in graphics programming are essentially statements about particular vector configurations. By developing intuition about dot products, cross products, and projections, programmers can often bypass trigonometric identities entirely.

"Part of the problem with overusing trigonometry in 3D rendering codebases comes from poorly designed third party APIs, like that of rotationAxisAngle(v,a) or WebXR stereo projection information, which work on angles and force their users to use angles too, infecting our codebases," Quilez notes.

For those interested in exploring Quilez's work further, his website iquilezles.org serves as a comprehensive repository of articles, shaders, and mathematical insights that have influenced generations of graphics programmers. His avoiding trigonometry article provides the complete technical details of his approach, including code examples and mathematical derivations.

As computer graphics continues to evolve with the rise of real-time ray tracing, neural rendering, and other advanced techniques, Quilez's emphasis on fundamental mathematical operations remains highly relevant. His work serves as a reminder that sometimes the most elegant solutions come from looking deeper into mathematics rather than reaching for convenient but suboptimal abstractions.

Comments

Loading comments...