Skip to content

refactor(sphere): Zero initialize SphereClass by default and add Is_Valid function#2855

Open
stephanmeesters wants to merge 3 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/sphere-zero-init
Open

refactor(sphere): Zero initialize SphereClass by default and add Is_Valid function#2855
stephanmeesters wants to merge 3 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/sphere-zero-init

Conversation

@stephanmeesters

@stephanmeesters stephanmeesters commented Jul 5, 2026

Copy link
Copy Markdown
  • Changes the constructor of SphereClass to zero-init its members.
  • Adds the Is_Valid function
  • Removes verbose EA comments (2nd commit)
  • Remove unused instances of SphereClass

@greptile-apps

greptile-apps Bot commented Jul 5, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors SphereClass to zero-initialise its members in the default constructor and introduces an Is_Valid() predicate (Radius > 0.0) that replaces scattered inline radius checks throughout the codebase. Unused SphereClass local variables (in boxrobj.cpp, dazzle.cpp, and W3DVolumetricShadow.cpp) are removed as dead code.

  • sphere.h: Default constructor now explicitly sets Center = {0,0,0} and Radius = 0.0f; Add_Sphere and Add_Spheres are updated to use Is_Valid() instead of == 0.0f; branch order in Add_Spheres is inverted to match the positive-first idiom.
  • sortingrenderer.cpp: The if/else branches are reorganised so the valid-sphere (sorted) path is the primary branch and the unsorted fallback is the else; semantics are unchanged.
  • Dead-code removal: Six SphereClass instances across four files were constructed but their values never consumed; all are deleted.

Confidence Score: 5/5

Safe to merge — all logic changes are equivalent rewrites of existing radius checks, and dead-code removals have no runtime impact.

The default-constructor zero-init eliminates undefined reads of uninitialised members. The Is_Valid() predicate precisely matches the previous inline checks, the sortingrenderer branch reorder is semantically identical, and removed variables were genuinely unused. The only nit is a float/double literal mismatch in Is_Valid() with no behavioural impact.

sphere.h warrants a quick look for the 0.0 vs 0.0f literal in Is_Valid(); no other files need special attention.

Important Files Changed

Filename Overview
Core/Libraries/Source/WWVegas/WWMath/sphere.h Default constructor now zero-initialises Center and Radius; adds Is_Valid() (Radius > 0.0); updates Add_Sphere and Add_Spheres to use the new predicate. Minor: Is_Valid uses double literal 0.0 instead of 0.0f.
Core/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp If/else branches reorganised to test Is_Valid() first; semantics unchanged (Radius > 0 → sorted list, else → unsorted list). Inline SphereClass variable replaced with temporary.
Core/Libraries/Source/WWVegas/WW3D2/collect.cpp SphereClass(Vector3(0,0,0),0) replaced with SphereClass(); semantically identical now that the default constructor zero-initialises members.
Core/Libraries/Source/WWVegas/WW3D2/dynamesh.cpp Explicit zero-init constructor call replaced with SphereClass(); no behavioural change.
Core/Tools/W3DView/ViewerScene.cpp Explicit SphereClass(Vector3(0,0,0), 0.0f) replaced with SphereClass(); no behavioural change.
Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp Removed unused SphereClass local variable and its Get_Obj_Space_Bounding_Sphere call; result was never consumed.
Generals/Code/Libraries/Source/WWVegas/WW3D2/dazzle.cpp Removed three unused SphereClass local variables that were constructed but never read.
Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp Removed unused SphereClass bsphere local variable; was declared but never used.
GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp Mirror of Generals/; removed unused SphereClass bsphere declaration in renderShadows.
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp Mirror of Generals/boxrobj.cpp; removed unused SphereClass variable and Get_Obj_Space_Bounding_Sphere call.
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dazzle.cpp Mirror of Generals/dazzle.cpp; removed three unused SphereClass local variables.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["Insert_Triangles(bounding_sphere, ...)"] --> B{"bounding_sphere.Is_Valid()\n(Radius > 0.0f)"}
    B -- Yes --> C["Transform center via world×view matrix"]
    C --> D["Insert_To_Sorted_List(state)"]
    B -- No --> E["state->transformed_center = (0,0,0)"]
    E --> F["unsorted_list.push_back(state)"]

    G["Add_Spheres(s0, s1)"] --> H{"s0.Is_Valid()"}
    H -- Yes --> I["result = s0\nresult.Add_Sphere(s1)\nreturn result"]
    H -- No --> J["return s1"]

    K["SphereClass::Add_Sphere(s)"] --> L{"!s.Is_Valid()"}
    L -- Yes --> M["return (no-op)"]
    L -- No --> N["Compute enclosing sphere"]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["Insert_Triangles(bounding_sphere, ...)"] --> B{"bounding_sphere.Is_Valid()\n(Radius > 0.0f)"}
    B -- Yes --> C["Transform center via world×view matrix"]
    C --> D["Insert_To_Sorted_List(state)"]
    B -- No --> E["state->transformed_center = (0,0,0)"]
    E --> F["unsorted_list.push_back(state)"]

    G["Add_Spheres(s0, s1)"] --> H{"s0.Is_Valid()"}
    H -- Yes --> I["result = s0\nresult.Add_Sphere(s1)\nreturn result"]
    H -- No --> J["return s1"]

    K["SphereClass::Add_Sphere(s)"] --> L{"!s.Is_Valid()"}
    L -- Yes --> M["return (no-op)"]
    L -- No --> N["Compute enclosing sphere"]
Loading

Reviews (3): Last reviewed commit: "Rename IsEmpty to Is_Valid and flip logi..." | Re-trigger Greptile

@stephanmeesters stephanmeesters changed the title refactor(sphere): Zero initialize by default and add IsEmpty function refactor(sphere): Zero initialize Sphere by default and add IsEmpty function Jul 5, 2026
@stephanmeesters stephanmeesters added Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing labels Jul 5, 2026

@Caball009 Caball009 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While looking at instances of SphereClass I noticed a couple of places where they're unused:

SphereClass sphere;
Get_Obj_Space_Bounding_Sphere(sphere);

I realize it's slightly outside the scope of this PR, but perhaps these can be removed as well.

Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dazzle.cpp Outdated
@stephanmeesters stephanmeesters changed the title refactor(sphere): Zero initialize Sphere by default and add IsEmpty function refactor(sphere): Zero initialize SphereClass by default and add IsEmpty function Jul 5, 2026

@Caball009 Caball009 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't really have an opinion on the removal of the EA / Westwood comments, but the code changes look fine to me.

Comment thread Core/Libraries/Source/WWVegas/WWMath/sphere.h
Comment thread Core/Libraries/Source/WWVegas/WWMath/sphere.h Outdated
Comment thread Core/Libraries/Source/WWVegas/WWMath/sphere.h Outdated
Comment thread Core/Libraries/Source/WWVegas/WWMath/sphere.h Outdated
@stephanmeesters stephanmeesters changed the title refactor(sphere): Zero initialize SphereClass by default and add IsEmpty function refactor(sphere): Zero initialize SphereClass by default and add Is_Valid function Jul 8, 2026
@stephanmeesters stephanmeesters force-pushed the refactor/sphere-zero-init branch from 4f6ec5c to 09e0333 Compare July 8, 2026 19:56

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good refactor


inline bool SphereClass::Is_Valid() const
{
return Radius > 0.0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

f

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

Labels

Gen Relates to Generals Refactor Edits the code with insignificant behavior changes, is never user facing ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants