GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
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 >:
Inheritance graph
[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. More...
 
XType x_max () const
 Retrieve the maximum x value. More...
 
YType y_min () const
 Retrieve the minimum y value. More...
 
YType y_max () const
 Retrieve the maximum y value. More...
 
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. More...
 
const std::vector< YType > * fY
 Pointer to the vector of y coordinates. More...
 
const std::vector< ZObject > * fZ
 Pointer to the vector of z coordinate objects. More...
 
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 fnal.gov> Fermi National Accelerator Laboratory
Created:
August 23, 2018
License:
Copyright (c) 2003-2024, 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

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.

61  : fX(X), fY(Y), fZ(Z), fExtrapolate(extrapolate)
62  {}
const std::vector< ZObject > * fZ
Pointer to the vector of z coordinate objects.
const std::vector< XType > * fX
Pointer to the vector of x coordinates.
const std::vector< YType > * fY
Pointer to the vector of y coordinates.

Member Function Documentation

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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

163  {
164  /// \todo Check that the vector contains at least two entries
165 
166  bool within = true;
167 
168  typedef typename std::vector<Type>::const_iterator Iterator;
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
174  Iterator not_less_point = std::lower_bound(begin, end, val);
175 
176  Iterator lower_point;
177 
178  // Check whether the requested grid point is within the grid limits
179  if (not_less_point == begin) {
180  lower_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
191  lower_point = not_less_point - 1;
192  }
193 
194  lower_index = std::distance(begin, lower_point);
195  upper_index = lower_index + 1;
196 
197  return within;
198  }
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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

80  {
81  IndexType num_y_points = fY->size();
82 
83  return (num_y_points * ix) + iy;
84  }
const std::vector< YType > * fY
Pointer to the vector of y coordinates.
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  {
91  IndexType ix_lo, ix_hi, iy_lo, iy_hi;
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());
98  evalx = std::max(evalx, x_min());
99  }
100 
101  YType evaly = y;
102  if (!fExtrapolate) {
103  evaly = std::min(y, y_max());
104  evaly = std::max(evaly, y_min());
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.
111  get_bound_indices(fX, evalx, ix_lo, ix_hi);
112  get_bound_indices(fY, evaly, iy_lo, iy_hi);
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  }
IndexType index_Z(IndexType ix, IndexType iy) const
YType y_max() const
Retrieve the maximum y value.
const std::vector< ZObject > * fZ
Pointer to the vector of z coordinate objects.
YType y_min() const
Retrieve the minimum y value.
const std::vector< XType > * fX
Pointer to the vector of x coordinates.
XType x_max() const
Retrieve the maximum x value.
const std::vector< YType > * fY
Pointer to the vector of y coordinates.
XType x_min() const
Retrieve the minimum x value.
bool get_bound_indices(const std::vector< Type > *vec, Type val, int &lower_index, int &upper_index) const
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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

68 { return fX->back(); }
const std::vector< XType > * fX
Pointer to the vector of x coordinates.
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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

65 { return fX->front(); }
const std::vector< XType > * fX
Pointer to the vector of x coordinates.
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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

74 { return fY->back(); }
const std::vector< YType > * fY
Pointer to the vector of y coordinates.
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.

Referenced by genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

71 { return fY->front(); }
const std::vector< YType > * fY
Pointer to the vector of y coordinates.

Member Data Documentation

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 genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().

template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
const std::vector<XType>* genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fX
protected
template<typename ZObject, typename IndexType = int, typename XType = double, typename YType = double>
const std::vector<YType>* genie::BLI2DNonUnifObjectGrid< ZObject, IndexType, XType, YType >::fY
protected
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 genie::BLI2DNonUnifObjectGrid< genie::TabulatedLabFrameHadronTensor::TableEntry >::interpolate().


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