Skip to content

Added quick scalar implementations of distance, distance_sq, lerp, min, max, clamp, and abs to vec4.#17

Closed
mcdubhghlas wants to merge 3 commits into
Redot-Engine:masterfrom
mcdubhghlas:master
Closed

Added quick scalar implementations of distance, distance_sq, lerp, min, max, clamp, and abs to vec4.#17
mcdubhghlas wants to merge 3 commits into
Redot-Engine:masterfrom
mcdubhghlas:master

Conversation

@mcdubhghlas
Copy link
Copy Markdown
Member

@mcdubhghlas mcdubhghlas commented Apr 8, 2026

Title.

We will still need to make tests, but these are just generic implementations of them. I will go back over them later, as needed, to improve performance via simd intrinsics and such. But I'll keep these as defaults/scalar.

Summary by CodeRabbit

  • New Features
    • Expanded the math library with new Vector4 utilities: distance and distance-squared calculations, linear interpolation, component-wise min/max and absolute value operations, and component-wise clamping. These additions simplify common vector math tasks and make numerical operations more concise and expressive in client code.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

Added seven exported draco::math utility functions operating on Vector4: distance, distance_sq, lerp, min, max, clamp, and abs. All are component-wise or magnitude-based helpers; no existing functions or control flow were modified.

Changes

Cohort / File(s) Summary
Vector4 Math Utilities
engine/native/core/math/vector4.cppm
Added 7 exported draco::math::Vector4 helpers: distance, distance_sq (magnitude-based), lerp (linear interpolation), min/max (component-wise extrema), clamp (component-wise clamp), and abs (component-wise absolute).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • Arctis-Fireblight
  • AR-DEV-1
  • JoltedJon

Poem

🐰 I hopped through code where vectors play,
Seven helpers now light the way,
Distance, lerp, clamp snug and tight,
Min, max, abs — all feeling right,
Fourfold math — a rabbit's delight.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding scalar utility functions (distance, distance_sq, lerp, min, max, clamp, abs) to Vector4.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
engine/native/core/math/vector4.cppm (1)

173-221: Please add tests for the new exported helpers.

Since these are new public math APIs, add unit tests for nominal and edge cases (t=0/1, negative components for abs, and clamp boundaries) before merge.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@engine/native/core/math/vector4.cppm` around lines 173 - 221, Add unit tests
covering the new public Vector4 helpers: distance, distance_sq, lerp, min, max,
clamp, and abs; for each function include nominal cases and edge cases (e.g.,
distance/distance_sq for identical and different vectors, lerp with t=0 and t=1
and an intermediate t, min/max with mixed-sign components, clamp where
components equal x_min and x_max and x outside bounds, and abs with
negative/zero/positive components). Place tests in the math/vector4 test suite
and use the Vector4 constructor and equality/approx helpers already in test
infra to assert expected results and floating-point tolerances. Ensure tests
cover constexpr-friendly functions where possible and include negative-component
scenarios and boundary values to prevent regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@engine/native/core/math/vector4.cppm`:
- Around line 199-206: The return statement in the constexpr Vector4 max(const
Vector4& a, const Vector4& b) noexcept is missing the terminating semicolon
after the braced initializer; edit the Vector4 max function so the return {
std::max(...), ... } expression ends with a semicolon to fix the compilation
error.

---

Nitpick comments:
In `@engine/native/core/math/vector4.cppm`:
- Around line 173-221: Add unit tests covering the new public Vector4 helpers:
distance, distance_sq, lerp, min, max, clamp, and abs; for each function include
nominal cases and edge cases (e.g., distance/distance_sq for identical and
different vectors, lerp with t=0 and t=1 and an intermediate t, min/max with
mixed-sign components, clamp where components equal x_min and x_max and x
outside bounds, and abs with negative/zero/positive components). Place tests in
the math/vector4 test suite and use the Vector4 constructor and equality/approx
helpers already in test infra to assert expected results and floating-point
tolerances. Ensure tests cover constexpr-friendly functions where possible and
include negative-component scenarios and boundary values to prevent regressions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bd0c3d94-7b79-4572-8a41-b1580e128259

📥 Commits

Reviewing files that changed from the base of the PR and between 17595c0 and 63c528d.

📒 Files selected for processing (1)
  • engine/native/core/math/vector4.cppm

Comment on lines +199 to +206
[[nodiscard]] constexpr Vector4 max(const Vector4& a, const Vector4& b) noexcept {
return {
std::max(a.x, b.x),
std::max(a.y, b.y),
std::max(a.z, b.z),
std::max(a.w, b.w),
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Fix compile error in max return statement.

Line 205 is missing the semicolon after the braced return value, which breaks compilation.

🛠️ Minimal fix
     [[nodiscard]] constexpr Vector4 max(const Vector4& a, const Vector4& b) noexcept {
         return {
             std::max(a.x, b.x),
             std::max(a.y, b.y),
             std::max(a.z, b.z),
             std::max(a.w, b.w),
-        }
+        };
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[[nodiscard]] constexpr Vector4 max(const Vector4& a, const Vector4& b) noexcept {
return {
std::max(a.x, b.x),
std::max(a.y, b.y),
std::max(a.z, b.z),
std::max(a.w, b.w),
}
}
[[nodiscard]] constexpr Vector4 max(const Vector4& a, const Vector4& b) noexcept {
return {
std::max(a.x, b.x),
std::max(a.y, b.y),
std::max(a.z, b.z),
std::max(a.w, b.w),
};
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@engine/native/core/math/vector4.cppm` around lines 199 - 206, The return
statement in the constexpr Vector4 max(const Vector4& a, const Vector4& b)
noexcept is missing the terminating semicolon after the braced initializer; edit
the Vector4 max function so the return { std::max(...), ... } expression ends
with a semicolon to fix the compilation error.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
engine/native/core/math/vector4.cppm (2)

188-196: Consider clarifying the comment.

The comment "Returns smallest vector" could be misread as returning the vector with smallest magnitude. Consider "Returns component-wise minimum" to be precise.

📝 Suggested comment improvement
-    // Returns smallest vector between a and b.
+    // Returns component-wise minimum of a and b.
     [[nodiscard]] constexpr Vector4 min(const Vector4& a, const Vector4& b) noexcept {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@engine/native/core/math/vector4.cppm` around lines 188 - 196, The comment for
the function Vector4 min is ambiguous; update it to read something like "Returns
component-wise minimum of two vectors" so it's clear the function compares each
component separately (x, y, z, w) rather than computing the vector with the
smallest magnitude; change the comment above the constexpr Vector4 min(const
Vector4& a, const Vector4& b) noexcept accordingly.

178-181: Minor inconsistency: argument order differs from distance.

distance computes length(b - a) while distance_sq computes length_sq(a - b). While mathematically equivalent (squared magnitude is symmetric), aligning the argument order improves readability.

♻️ Suggested fix for consistency
     // Return squared distance between two vectors.
     [[nodiscard]] FORCEINLINE float distance_sq(const Vector4& a, const Vector4& b) noexcept {
-        return length_sq(a - b);
+        return length_sq(b - a);
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@engine/native/core/math/vector4.cppm` around lines 178 - 181, The distance_sq
implementation uses a - b while distance uses b - a; make them consistent by
changing Vector4::distance_sq to call length_sq(b - a) so its operand order
matches distance (functions: distance and distance_sq, and helper length_sq).
Update the expression in distance_sq accordingly to improve readability.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@engine/native/core/math/vector4.cppm`:
- Around line 188-196: The comment for the function Vector4 min is ambiguous;
update it to read something like "Returns component-wise minimum of two vectors"
so it's clear the function compares each component separately (x, y, z, w)
rather than computing the vector with the smallest magnitude; change the comment
above the constexpr Vector4 min(const Vector4& a, const Vector4& b) noexcept
accordingly.
- Around line 178-181: The distance_sq implementation uses a - b while distance
uses b - a; make them consistent by changing Vector4::distance_sq to call
length_sq(b - a) so its operand order matches distance (functions: distance and
distance_sq, and helper length_sq). Update the expression in distance_sq
accordingly to improve readability.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c0dfe1de-954c-4785-a3dc-7938a2b882fb

📥 Commits

Reviewing files that changed from the base of the PR and between 63c528d and f975c6c.

📒 Files selected for processing (1)
  • engine/native/core/math/vector4.cppm

@mcdubhghlas
Copy link
Copy Markdown
Member Author

Closing, waiting for APEIRO's PR first.

@mcdubhghlas mcdubhghlas closed this Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant