Skip to content

Conversation

@Breakdown-Dog
Copy link
Contributor

Objective

  • The functions logical_viewport_size and physical_viewport_size in camera.rs share a very similar pattern.
  • Refactor code to reduce duplication.

Solution

  • extract to a shared function.

Testing

  • Ran cargo test and all the tests pass.

@mnmaita mnmaita added C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Straightforward Simple bug fixes and API improvements, docs, test and examples A-Camera User-facing camera APIs and controllers. labels Nov 2, 2025
return Err(ViewportConversionError::PastNearPlane);
}

// Stretching ndc depth to value via near plane and negating result to be in positive room again.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to delete this comment as well? If not, you can place it back above let depth …

@kfc35
Copy link
Contributor

kfc35 commented Nov 8, 2025

Beyond the deletion of the comment, this LGTM, with the caveat that this is my first pull request I am reviewing in the codebase and I’m also new to Rust

let viewport_position =
(ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * target_rect.size() + target_rect.min;
Ok(viewport_position)
Ok((viewport_position, ndc_space_coords.z))
Copy link
Contributor

@kfc35 kfc35 Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does truncate() when creating viewport_position mutate ndc_space_coords at all, specifically the z-component? if it does, the value for z might not be what is expected here, and it might be safer for this function to return depth (since depth in the previous logic seemed to be created before viewport_position)

(New here so take this with a grain of salt!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The z component’s value isn’t changed, but I understand why it’s confusing.

The truncate() function consumes the entire ndc_space_coords struct. The reason it still works is due to Rust’s Non-Lexicographical Lifetimes (NLL). The compiler is smart enough to recognize that even though the whole struct is moved, the z field was never actually needed by truncate(), so it remains valid for the final Ok return.

However, I completely agree with your sentiment. Relying on this advanced compiler behavior is not ideal practice. It makes the code less clear and more fragile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Camera User-facing camera APIs and controllers. C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants