Skip to content

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

Merged
xezon merged 4 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/sphere-zero-init
Jul 15, 2026
Merged

refactor(sphere): Zero initialize SphereClass by default and add Is_Valid function#2855
xezon merged 4 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 hardens SphereClass by giving the default constructor well-defined zero state (Center=(0,0,0), Radius=0) and introducing Is_Valid() (Radius > 0.0f) to replace scattered raw Radius == 0.0f comparisons throughout the codebase. Callers that previously passed explicit zero arguments to the constructor are updated to use the default, and several dead-code SphereClass local variables that were constructed but never consumed are removed.

  • sphere.h: Default constructor now zero-initializes both members; Is_Valid() is added and adopted in Add_Sphere and Add_Spheres, which also correctly handles the previously-unguarded negative-radius edge case.
  • sortingrenderer.cpp: Branches reordered to use Is_Valid() — the happy path (sort) now comes first and the fallback (unsorted list) is in the else; logic is semantically equivalent to the prior Radius <= 0.0 guard.
  • Dead-code cleanup (boxrobj.cpp, dazzle.cpp, W3DVolumetricShadow.cpp in both Generals/ and GeneralsMD/): SphereClass variables that were declared (and in one case even populated) but never read are removed.

Confidence Score: 5/5

Safe to merge — the default constructor change is strictly additive (zero-init replaces undefined state) and all affected call sites already passed explicit zeros, so no observable behavior changes at those points.

Every call site that previously passed Vector3(0,0,0), 0.0f explicitly now relies on the zero-init constructor, producing identical state. The Is_Valid() substitution in Add_Sphere and Add_Spheres only differs from the old Radius == 0.0f check for the unreachable negative-radius case, which is an improvement. Removed variables were confirmed unused. The sorting-renderer branch flip preserves the same two code paths with no logic change.

No files require special attention.

Important Files Changed

Filename Overview
Core/Libraries/Source/WWVegas/WWMath/sphere.h Core change: default constructor now zero-initializes Center and Radius; adds Is_Valid() (Radius > 0.0f); updates Add_Sphere and Add_Spheres to use Is_Valid() instead of raw == 0.0f comparisons.
Core/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp Flips if/else branches to use Is_Valid() for bounding sphere check; eliminates a local SphereClass variable in the overload; logic is equivalent to the original Radius <= 0.0 check.
Core/Libraries/Source/WWVegas/WW3D2/collect.cpp Replaces explicit SphereClass(Vector3(0,0,0),0) with default constructor; semantically identical after zero-init constructor change.
Core/Libraries/Source/WWVegas/WW3D2/dynamesh.cpp Replaces explicit zero-initialized SphereClass constructor with default constructor before Get_Bounding_Sphere call; behavior unchanged.
Core/Tools/W3DView/ViewerScene.cpp Replaces explicit zero-init SphereClass constructor with default constructor in Get_Bounding_Sphere accumulation loop; semantically identical.
Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp Removes dead-code SphereClass variable that was declared and populated but never used.
Generals/Code/Libraries/Source/WWVegas/WW3D2/dazzle.cpp Removes three dead-code SphereClass variable declarations immediately before DX8Wrapper draw calls with no intervening usage.
Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp Removes unused SphereClass bsphere local variable from renderShadows; confirmed bsphere was never referenced after declaration.
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp Same dead-code removal as Generals/ counterpart.
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dazzle.cpp Same dead-code removal as Generals/ counterpart.
GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp Same unused variable removal as Generals/ counterpart.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["SphereClass()"] -->|"new: Center=(0,0,0), Radius=0"| B[Zero-initialized sphere]
    B --> C{Is_Valid?}
    C -->|"Radius > 0.0f"| D[Valid sphere]
    C -->|"Radius <= 0.0f"| E[Invalid / empty sphere]

    D --> F["Add_Sphere(s)"]
    F --> G{s.Is_Valid?}
    G -->|No| H[Return early]
    G -->|Yes| I[Compute enclosing sphere]

    D --> J["Add_Spheres(s0, s1)"]
    J --> K{s0.Is_Valid?}
    K -->|Yes| L[Expand s0 to include s1]
    K -->|No| M[Return s1]

    B --> N["sortingrenderer.cpp"]
    N --> O{bounding_sphere.Is_Valid?}
    O -->|Yes| P[Sort: compute transformed center]
    O -->|No| Q[Unsorted list]
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["SphereClass()"] -->|"new: Center=(0,0,0), Radius=0"| B[Zero-initialized sphere]
    B --> C{Is_Valid?}
    C -->|"Radius > 0.0f"| D[Valid sphere]
    C -->|"Radius <= 0.0f"| E[Invalid / empty sphere]

    D --> F["Add_Sphere(s)"]
    F --> G{s.Is_Valid?}
    G -->|No| H[Return early]
    G -->|Yes| I[Compute enclosing sphere]

    D --> J["Add_Spheres(s0, s1)"]
    J --> K{s0.Is_Valid?}
    K -->|Yes| L[Expand s0 to include s1]
    K -->|No| M[Return s1]

    B --> N["sortingrenderer.cpp"]
    N --> O{bounding_sphere.Is_Valid?}
    O -->|Yes| P[Sort: compute transformed center]
    O -->|No| Q[Unsorted list]
Loading

Reviews (4): Last reviewed commit: "Add f" | 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

@xezon xezon merged commit 36ba5b0 into TheSuperHackers:main Jul 15, 2026
17 checks passed
@stephanmeesters stephanmeesters deleted the refactor/sphere-zero-init branch July 16, 2026 08:05
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