Skip to content

refactor(basetype): Add intersect and min/max functions to Region and Coord types - #3271

Open
stephanmeesters wants to merge 2 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/intersect-min-max
Open

refactor(basetype): Add intersect and min/max functions to Region and Coord types#3271
stephanmeesters wants to merge 2 commits into
TheSuperHackers:mainfrom
stephanmeesters:refactor/intersect-min-max

Conversation

@stephanmeesters

@stephanmeesters stephanmeesters commented Sep 8, 2026

Copy link
Copy Markdown

Split off from #3245 where an intersect function was needed for IRegion2D.

In the current PR we implement intersect functions for IRegion3D, IRegion2D, Region3D and Region2D. For that purpose it also adds min and max to the Coords variants.

In the second commit it replaces various usages with these new functions. Cases where the replacement was not straight-forward were skipped, some cases also looked more like a union operation so those were also skipped.

AI was used to implement this, every line was checked.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Add coordinate min/max and region intersection helpers

✨ Enhancement 🕐 20-40 Minutes

Grey Divider

AI Description

• Adds component-wise min/max helpers to integer and floating-point 2D/3D coordinates.
• Adds mutating intersection operations to all 2D and 3D region variants.
• Replaces duplicated window and pathfinding bounds-clamping logic with shared helpers.
Diagram

classDiagram
  class Coord2D {
    +min(other)
    +max(other)
  }
  class ICoord2D {
    +min(other)
    +max(other)
  }
  class Region2D {
    +intersect(other)
  }
  class IRegion2D {
    +intersect(other)
  }
  class Coord3D {
    +min(other)
    +max(other)
  }
  class ICoord3D {
    +min(other)
    +max(other)
  }
  class Region3D {
    +intersect(other)
  }
  class IRegion3D {
    +intersect(other)
  }
  Region2D *-- Coord2D : bounds
  IRegion2D *-- ICoord2D : bounds
  Region3D *-- Coord3D : bounds
  IRegion3D *-- ICoord3D : bounds
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Non-mutating free functions
  • ➕ Make value transformation explicit at call sites
  • ➕ Allow source bounds to remain unchanged
  • ➕ Can return a new region suitable for expression composition
  • ➖ Require additional assignments or temporary objects
  • ➖ Do not match the existing mutable POD-style APIs
  • ➖ Produce greater call-site churn for this legacy codebase
2. Templated coordinate and region types
  • ➕ Eliminate duplication between integer and floating-point variants
  • ➕ Centralize dimension-independent min, max, and intersection behavior
  • ➖ Greatly expands the scope and compatibility risk
  • ➖ Could disrupt serialization, ABI assumptions, or legacy call sites
  • ➖ Provides limited immediate benefit for this focused refactor

Recommendation: Keep the PR's mutating member-function approach because it fits the existing region and coordinate design and cleanly replaces repeated clamping logic. A templated redesign could reduce duplication but should be a separate, compatibility-focused change; tests for overlapping and disjoint regions would strengthen the current approach.

Files changed (3) +106 / -76

Enhancement (1) +98 / -12
BaseType.hAdd coordinate min/max and region intersection APIs +98/-12

Add coordinate min/max and region intersection APIs

• Adds mutating component-wise 'min' and 'max' methods to all 2D and 3D integer and floating-point coordinate types. Adds 'intersect' to corresponding region types and reuses coordinate helpers when constructing 3D bounds from points.

Core/Libraries/Include/Lib/BaseType.h

Refactor (2) +8 / -64
GameWindow.cppNormalize window bounds with coordinate min/max helpers +3/-20

Normalize window bounds with coordinate min/max helpers

• Replaces manual axis-by-axis endpoint swapping with the new component-wise coordinate operations while preserving the original lower endpoint for the upper-bound calculation.

Core/GameEngine/Source/GameClient/GUI/GameWindow.cpp

AIPathfind.cppUse shared bounds clipping in pathfinding +5/-44

Use shared bounds clipping in pathfinding

• Replaces repeated coordinate clamps with 'min' and region 'intersect' operations during zone calculation, zone modification, and object-footprint classification. This centralizes two-dimensional bounds clipping behavior.

Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can tweak Display preferences with a live preview to see your comment before it ships

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@greptile-apps

greptile-apps Bot commented Sep 8, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds component-wise coordinate clamping and region-intersection helpers, then uses them to simplify GUI normalization, pathfinding bounds clipping, and 3D point-bound construction.

  • Adds mutating min and max operations to integer and floating-point coordinate types.
  • Adds intersect operations to 2D and 3D region types.
  • Replaces repeated per-axis bounds logic with the new helpers.
  • The replacements preserve existing runtime behavior, but the public min/max names are not safe for every supported Windows include order.

Confidence Score: 4/5

The current call sites preserve existing behavior, so the PR is generally safe to merge, although the Windows macro compatibility of the new shared API should be hardened.

No current runtime regression was found in the bounds refactors; the remaining concern is a non-blocking portability limitation that prevents future min/max member calls from compiling in supported translation units where Windows defines the corresponding macros after BaseType.h.

Files Needing Attention: Core/Libraries/Include/Lib/BaseType.h

Important Files Changed

Filename Overview
Core/Libraries/Include/Lib/BaseType.h Adds the shared coordinate and region operations; their behavior is straightforward, but the min/max names conflict with Windows macros in some consumers.
Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp Replaces explicit pathfinding-bound clamps with equivalent coordinate and region helper calls.
Core/GameEngine/Source/GameClient/GUI/GameWindow.cpp Replaces manual axis swaps with equivalent component-wise min/max normalization.
Prompt To Fix All With AI
### Issue 1
Core/Libraries/Include/Lib/BaseType.h:335-349
**Windows macros break coordinate API**

The new public member names `min` and `max` collide with the function-like Windows macros in translation units such as WorldBuilder, whose precompiled header includes `BaseType.h` before `afxwin.h`. After MFC defines those macros, a call such as `coord.min(other)` is expanded as a malformed two-argument macro invocation and fails to compile. This makes these shared coordinate APIs unusable from that supported component. Please use macro-safe names or ensure `NOMINMAX` is consistently defined for all consumers. The same issue applies to the new `ICoord2D`, `Coord3D`, and `ICoord3D` methods.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "Apply new min, max, and intersect functi..." | Re-trigger Greptile

Comment on lines +335 to +349
void min( const Coord2D &other )
{
if (x > other.x)
x = other.x;
if (y > other.y)
y = other.y;
}

void max( const Coord2D &other )
{
if (x < other.x)
x = other.x;
if (y < other.y)
y = other.y;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Windows macros break coordinate API

The new public member names min and max collide with the function-like Windows macros in translation units such as WorldBuilder, whose precompiled header includes BaseType.h before afxwin.h. After MFC defines those macros, a call such as coord.min(other) is expanded as a malformed two-argument macro invocation and fails to compile. This makes these shared coordinate APIs unusable from that supported component. Please use macro-safe names or ensure NOMINMAX is consistently defined for all consumers. The same issue applies to the new ICoord2D, Coord3D, and ICoord3D methods.

Prompt To Fix With AI
This is a comment left during a code review.
Path: Core/Libraries/Include/Lib/BaseType.h
Line: 335-349

Comment:
**Windows macros break coordinate API**

The new public member names `min` and `max` collide with the function-like Windows macros in translation units such as WorldBuilder, whose precompiled header includes `BaseType.h` before `afxwin.h`. After MFC defines those macros, a call such as `coord.min(other)` is expanded as a malformed two-argument macro invocation and fails to compile. This makes these shared coordinate APIs unusable from that supported component. Please use macro-safe names or ensure `NOMINMAX` is consistently defined for all consumers. The same issue applies to the new `ICoord2D`, `Coord3D`, and `ICoord3D` methods.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

NOMINMAX is set through WWLib\always.h

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That addresses the WorldBuilder case if WWLib/always.h is guaranteed to be included before any Windows/MFC header in its precompiled header. My concern only remains for other consumers of the shared BaseType.h that do not include always.h first, or include afxwin.h before it; those can still have min/max defined when calling coord.min(other). If the project guarantees that include order for every consumer, this comment can be dismissed.

@stephanmeesters stephanmeesters added Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing Minor Severity: Minor < Major < Critical < Blocker labels Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker 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.

1 participant