GENIEGenerator
Loading...
Searching...
No Matches
genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType > Class Template Reference

A class template that performs bilinear interpolation on a non-uniform grid with an implementation similar to that of genie::BLI2DNonUnifGrid. More...

#include <BLI2DNonUnifObjectGrid.h>

Inheritance diagram for genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >:
[legend]

Public Member Functions

 BLI2DNonUnifObjectGrid (const std::vector< XType > *X, const std::vector< YType > *Y, const std::vector< ZObject > *Z, bool extrapolate=false)
XType x_min () const
 Retrieve the minimum x value.
XType x_max () const
 Retrieve the maximum x value.
YType y_min () const
 Retrieve the minimum y value.
YType y_max () const
 Retrieve the maximum y value.
IndexType index_Z (IndexType ix, IndexType iy) const
ZObject interpolate (double x, double y) const

Protected Member Functions

template<typename Type>
bool get_bound_indices (const std::vector< Type > *vec, Type val, int &lower_index, int &upper_index) const

Protected Attributes

const std::vector< XType > * fX
 Pointer to the vector of x coordinates.
const std::vector< YType > * fY
 Pointer to the vector of y coordinates.
const std::vector< ZObject > * fZ
 Pointer to the vector of z coordinate objects.
bool fExtrapolate

Detailed Description

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
class genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >

A class template that performs bilinear interpolation on a non-uniform grid with an implementation similar to that of genie::BLI2DNonUnifGrid.

The main differences between this class template and genie::BLI2DNonUnifGrid are

Values for the z coordinate can be any arbitrary object Object
that implements the member functions operator*(double),
operator*(const Object&), and operator+(const Object&)

Rather than C-style arrays, the grid values are accessed via
pointers to std::vector objects

Upper and lower bounds on the grid are found using
std::lower_bound() rather than a manual linear search

The genie::BLI2DNonUnifGrid object does not take ownership of the
grid vectors, which must be stored elsewhere
Template Parameters
ZObjectType of the object describing each z coordinate
IndexTypeType to use when computing indices in the vectors
XTypeType used to represent x coordinates
YTypeType used to represent y coordinates
Todo
Think about how to have this class inherit from the other BLI2D classes
Author
Steven Gardiner <gardiner \at fnal.gov> Fermi National Accelerator Laboratory
Created:\n August 23, 2018
License:\n Copyright (c) 2003-2025, The GENIE Collaboration
For the full text of the license visit http://copyright.genie-mc.org or see $GENIE/LICENSE

Definition at line 49 of file BLI2DNonUnifObjectGrid.h.

Constructor & Destructor Documentation

◆ BLI2DNonUnifObjectGrid()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::BLI2DNonUnifObjectGrid ( const std::vector< XType > * X,
const std::vector< YType > * Y,
const std::vector< ZObject > * Z,
bool extrapolate = false )
inline
Parameters
[in]XPointer to a vector of x coordinates
[in]YPointer to a vector of y coordinates
[in]ZPointer to a vector of z coordinates
[in]extrapolateWhether to allow bilinear extrapolation (true) or to use the grid endpoints (false) when evaluating z values outside of the grid

Definition at line 59 of file BLI2DNonUnifObjectGrid.h.

62 {}
A class template that performs bilinear interpolation on a non-uniform grid with an implementation si...
const std::vector< ZObject > * fZ
Pointer to the vector of z coordinate objects.
const std::vector< YType > * fY
Pointer to the vector of y coordinates.
const std::vector< XType > * fX
Pointer to the vector of x coordinates.

References fExtrapolate, fX, fY, and fZ.

Member Function Documentation

◆ get_bound_indices()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
template<typename Type>
bool genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::get_bound_indices ( const std::vector< Type > * vec,
Type val,
int & lower_index,
int & upper_index ) const
inlineprotected

Determines the indices for the two gridpoints surrounding a requested x or y coordinate. If the x or y coordinate is outside of the grid, this function returns the two closest grid points.

Parameters
[in]vecA vector of grid point coordinates
[in]valThe requested x or y coordinate
[out]lower_indexThe index of the closest grid point less than or equal to the requested value, or the lower of the two nearest grid points if the value falls outside of the grid
[out]upper_indexThe index of the closest grid point greater than the requested value, or the higher of the two nearest grid points if the value falls outside of the grid
Returns
true if the requested value is within the grid, or false otherwise
Todo
Check that the vector contains at least two entries

Definition at line 161 of file BLI2DNonUnifObjectGrid.h.

163 {
164 /// \todo Check that the vector contains at least two entries
165
166 bool within = true;
167
169 Iterator begin = vec->begin();
170 Iterator end = vec->end();
171
172 // std::lower_bound returns an iterator to the first element of the
173 // container which is not less than the supplied value
175
177
178 // Check whether the requested grid point is within the grid limits
179 if (not_less_point == begin) {
181 // first element of vec > val
182 if (*begin != val) within = false;
183 }
184 else if (not_less_point == end) {
185 // last element of vec < val
186 within = false;
187 lower_point = end - 2;
188 }
189 else {
190 // x is within the grid limits
192 }
193
196
197 return within;
198 }

Referenced by interpolate().

◆ index_Z()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
IndexType genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::index_Z ( IndexType ix,
IndexType iy ) const
inline

Calculates the index in the vector of z coordinates that corresponds to a given set of x and y indices

Parameters
[in]ixIndex of the desired grid point on the x axis
[in]iyIndex of the desired grid point on the y axis

Definition at line 80 of file BLI2DNonUnifObjectGrid.h.

80 {
81 IndexType num_y_points = fY->size();
82
83 return (num_y_points * ix) + iy;
84 }

References fY.

Referenced by interpolate().

◆ interpolate()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
ZObject genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::interpolate ( double x,
double y ) const
inline

Uses bilinear interpolation to compute the z coordinate (represented by an object of type ZObject) that corresponds to the given x and y coordinates

Definition at line 89 of file BLI2DNonUnifObjectGrid.h.

90 {
92
93 // For points outside the grid, evaluate the function at the end points
94 // unless extrapolation is enabled.
95 XType evalx = x;
96 if (!fExtrapolate) {
97 evalx = std::min(x, x_max());
99 }
100
101 YType evaly = y;
102 if (!fExtrapolate) {
103 evaly = std::min(y, y_max());
105 }
106
107 // Find the indices of the grid points on either side of the
108 // desired x and y values. If the desired point is outside of
109 // the x or y grid limits, get the indices of the two closest
110 // grid points to use for possible extrapolation.
113
114 // Get the x and y values corresponding to the lower (x1, y1) and
115 // upper (x2, y2) bounds found previously
116 XType x1 = fX->at( ix_lo );
117 XType x2 = fX->at( ix_hi );
118 YType y1 = fY->at( iy_lo );
119 YType y2 = fY->at( iy_hi );
120
121 // Retrieve the z values corresponding to each of the four locations
122 // that will be used for the bilinear interpolation
123 const ZObject& z11 = fZ->at( this->index_Z(ix_lo, iy_lo) );
124 const ZObject& z21 = fZ->at( this->index_Z(ix_hi, iy_lo) );
125 const ZObject& z12 = fZ->at( this->index_Z(ix_lo, iy_hi) );
126 const ZObject& z22 = fZ->at( this->index_Z(ix_hi, iy_hi) );
127
128 // Perform the interpolation (first y, then x)
129 ZObject z1 = z11 * (y2-evaly)/(y2-y1) + z12 * (evaly-y1)/(y2-y1);
130 ZObject z2 = z21 * (y2-evaly)/(y2-y1) + z22 * (evaly-y1)/(y2-y1);
131 ZObject z = z1 * (x2-evalx)/(x2-x1) + z2 * (evalx-x1)/(x2-x1);
132
133 return z;
134 }
bool get_bound_indices(const std::vector< Type > *vec, Type val, int &lower_index, int &upper_index) const
YType y_max() const
Retrieve the maximum y value.
XType x_min() const
Retrieve the minimum x value.
YType y_min() const
Retrieve the minimum y value.
IndexType index_Z(IndexType ix, IndexType iy) const
XType x_max() const
Retrieve the maximum x value.

References fExtrapolate, fX, fY, fZ, get_bound_indices(), index_Z(), x_max(), x_min(), y_max(), and y_min().

◆ x_max()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
XType genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::x_max ( ) const
inline

Retrieve the maximum x value.

Definition at line 68 of file BLI2DNonUnifObjectGrid.h.

68{ return fX->back(); }

References fX.

Referenced by interpolate().

◆ x_min()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
XType genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::x_min ( ) const
inline

Retrieve the minimum x value.

Definition at line 65 of file BLI2DNonUnifObjectGrid.h.

65{ return fX->front(); }

References fX.

Referenced by interpolate().

◆ y_max()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
YType genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::y_max ( ) const
inline

Retrieve the maximum y value.

Definition at line 74 of file BLI2DNonUnifObjectGrid.h.

74{ return fY->back(); }

References fY.

Referenced by interpolate().

◆ y_min()

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
YType genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::y_min ( ) const
inline

Retrieve the minimum y value.

Definition at line 71 of file BLI2DNonUnifObjectGrid.h.

71{ return fY->front(); }

References fY.

Referenced by interpolate().

Member Data Documentation

◆ fExtrapolate

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
bool genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fExtrapolate
protected

Whether to allow bilinear extrapolation (true) or to compute z values for x and coordinates outside of the grid using the grid endpoints (false)

Definition at line 146 of file BLI2DNonUnifObjectGrid.h.

Referenced by BLI2DNonUnifObjectGrid(), and interpolate().

◆ fX

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
const std::vector<XType>* genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fX
protected

Pointer to the vector of x coordinates.

Definition at line 138 of file BLI2DNonUnifObjectGrid.h.

Referenced by BLI2DNonUnifObjectGrid(), interpolate(), x_max(), and x_min().

◆ fY

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
const std::vector<YType>* genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fY
protected

Pointer to the vector of y coordinates.

Definition at line 139 of file BLI2DNonUnifObjectGrid.h.

Referenced by BLI2DNonUnifObjectGrid(), index_Z(), interpolate(), y_max(), and y_min().

◆ fZ

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
const std::vector<ZObject>* genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fZ
protected

Pointer to the vector of z coordinate objects.

Definition at line 142 of file BLI2DNonUnifObjectGrid.h.

Referenced by BLI2DNonUnifObjectGrid(), and interpolate().


The documentation for this class was generated from the following file: