CoolProp 8.0.0
An open-source fluid property and humid air property database
RegionAtlas.h
Go to the documentation of this file.
1#ifndef COOLPROP_REGION_REGION_ATLAS_H
2#define COOLPROP_REGION_REGION_ATLAS_H
3
4#include <cstddef>
5#include <vector>
6
8
9namespace CoolProp {
10namespace region {
11
12// A registry of Regions with bounding-box-first dispatch.
13//
14// find_region(a, b) scans the regions in registration order:
15// 1. For each region, check the cheap O(1) AABB membership. Skip if
16// the point is outside the AABB.
17// 2. On an AABB hit, call the region's curve_contains (which evaluates
18// the two BoundaryCurves at a) to confirm or reject.
19// 3. Return the first region whose curve check passes, or -1 if none
20// claims the point.
21//
22// AABBs are stored separately as a flat SoA in a small vector so the
23// first-pass scan is cache-friendly; the per-region BoundaryCurves are
24// only touched when a hit needs confirming.
25//
26// Registration order is part of the public contract: when regions are
27// expected to be disjoint but their AABBs overlap (common — the
28// bounding boxes of e.g. LIQUID and VAPOR overlap in (h, p) even though
29// the regions themselves are disjoint), the first match wins. Callers
30// who want to detect ambiguity can use `find_all_curve_hits` (returns a
31// vector of all curve-passing region indices), which is intended for
32// debug / test code, not the hot path.
34{
35 public:
36 RegionAtlas() = default;
37
38 // Move only — Regions hold std::unique_ptr<BoundaryCurve>.
39 RegionAtlas(const RegionAtlas&) = delete;
43 ~RegionAtlas() = default;
44
45 // Append a new region. Returns its integer index.
46 std::size_t add(Region region);
47
48 // Returns the index of the first region whose AABB contains (a, b)
49 // AND whose curve envelope contains (a, b); -1 if none.
50 [[nodiscard]] int find_region(double a, double b) const noexcept;
51
52 // Debug / test helper: return the indices of every region whose
53 // curve envelope contains (a, b). Useful for asserting disjointness.
54 [[nodiscard]] std::vector<std::size_t> find_all_curve_hits(double a, double b) const;
55
56 [[nodiscard]] std::size_t size() const noexcept {
57 return regions_.size();
58 }
59 [[nodiscard]] const Region& region(std::size_t i) const noexcept {
60 return regions_[i];
61 }
62
63 private:
64 std::vector<Region> regions_;
65 // SoA AABB cache for fast first-pass scanning.
66 std::vector<double> a_lo_;
67 std::vector<double> a_hi_;
68 std::vector<double> b_min_;
69 std::vector<double> b_max_;
70};
71
72} // namespace region
73} // namespace CoolProp
74
75#endif // COOLPROP_REGION_REGION_ATLAS_H