Do not rebuild the rotation matrices after every arithmetic operation on quaternion#481
Do not rebuild the rotation matrices after every arithmetic operation on quaternion#481Rombur wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors rotation handling by removing the ScanPath::rotate method and making Quaternion::build_rotation_matrices a public method that is called explicitly after interpolation in ScanPath::get_quaternion, rather than automatically inside every arithmetic operation. The feedback suggests optimizing performance by refactoring Quaternion::operator/= to avoid redundant matrix building, caching the last computed quaternion in ScanPath to prevent duplicate Slerp interpolations, and adding a validity assertion in build_rotation_matrices for safety.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| @@ -337,6 +324,7 @@ Quaternion ScanPath::get_quaternion(double const time) const | |||
| interpolated_rotation /= segment_start_rotation; | |||
There was a problem hiding this comment.
Note that Quaternion::operator/= currently constructs a temporary Quaternion for the inverse, which internally calls build_rotation_matrices(). Since this temporary's rotation matrices are never used, this results in an unnecessary matrix build during the Slerp interpolation.
Consider refactoring Quaternion::operator/= in source/Quaternion.cc to perform the division directly (by negating the imaginary components of the divisor and performing the Hamilton product) without creating a temporary Quaternion object. This will avoid the redundant matrix building overhead.
| return get_quaternion(time).rotate(point); | ||
| } | ||
|
|
||
| Quaternion ScanPath::get_quaternion(double const time) const |
There was a problem hiding this comment.
Since get_quaternion(time) is called both in update_time and get_bounding_box for the same time step, the expensive Slerp interpolation and matrix building are performed twice per time step.
Consider caching the last computed quaternion and its corresponding time using mutable member variables in ScanPath (e.g., mutable double _cached_quaternion_time and mutable Quaternion _cached_quaternion). If time == _cached_quaternion_time, you can return the cached quaternion directly, avoiding redundant computations.
| build_rotation_matrices(); | ||
| } | ||
|
|
||
| void Quaternion::build_rotation_matrices() |
There was a problem hiding this comment.
Currently we are rebuilding the rotation matrices after every arithmetic operation on quaternion. This was fine because we were doing very few arithmetic operations. This changed with #479, where we do lots of operations. With this PR, we have to explicitly build the matrices which is done at most once every time step.
Drive-by change: remove
ScanPath::rotate. There is already a function to rotate aPointwhen the quaternion is known and so, this is basically a duplicate.