Skip to content

[Type] Introduce clamped scalar ensuring value not outside bounds - #6209

Open
alxbilger wants to merge 1 commit into
sofa-framework:masterfrom
alxbilger:clampedscalar
Open

[Type] Introduce clamped scalar ensuring value not outside bounds#6209
alxbilger wants to merge 1 commit into
sofa-framework:masterfrom
alxbilger:clampedscalar

Conversation

@alxbilger

@alxbilger alxbilger commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces a new foundational type, sofa::type::ClampedScalar. This class template wraps a standard scalar value and guarantees that the stored value cannot fall outside predefined minimum and maximum bounds (min/max).

Key Changes & Implementation Details

  1. New Type (ClampedScalar):

    • A templated class that encapsulates a scalar value and two immutable bounds ($\text{minBound}, \text{maxBound}$).
    • The class implements clamping logic in several key areas:
      • Construction: Initial values are automatically clamped upon object creation.
      • Assignment/Setting (operator=): Any attempt to set the value uses clamping, ensuring state integrity.
      • Input/Output Operators: Stream extraction (>>) reads a raw value and clamps it before updating the internal state. Stream insertion (<<) only outputs the valid, clamped value.
      • Prediction: A calculateClamped(T rawValue) method allows users to predict what the bound-constrained result would be without modifying the object's actual state.
  2. Integration into VisualMesh Component:

    • The d_elementSpace data member in sofa::component::visual::VisualMesh has been updated from Data<SReal> to Data<type::ClampedScalar<SReal>>.
    • This change ensures that the element space parameter (which must logically be between 0 and 1) is enforced and clamped correctly, making the mesh visualization component more robust.
  3. Framework Updates:

    • Added ClampedScalar.h, ClampedScalar.cpp, and corresponding test files (ClampedScalar_test.cpp) to Sofa/framework/Type.
    • Updated CMake files across the framework to correctly include and build this new type.

⚠️ Breaking because of the change of type in VisualMesh.

Companion PR for the GUI: sofa-framework/SofaGLFW#292

clamped.mp4

Local gemma-4-e4b used for documentation and unit tests.

[with-all-tests]


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@alxbilger alxbilger added pr: breaking Change possibly inducing a compilation error pr: status to review To notify reviewers to review this pull-request pr: new feature Implement a new feature pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI labels Jul 27, 2026

@fredroy fredroy left a comment

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.

Should a ClampedIntegral be implemented also ?

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

Labels

pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI pr: breaking Change possibly inducing a compilation error pr: new feature Implement a new feature pr: status to review To notify reviewers to review this pull-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants