INET Framework for OMNeT++/OMNEST
inet::math::Rasterized2DFunction< R, X, Y > Class Template Reference

Rasterizes a function into a grid where each cell is a constant value with the mean of the original function. More...

#include <CompoundFunctions.h>

Inheritance diagram for inet::math::Rasterized2DFunction< R, X, Y >:
inet::math::FunctionBase< R, Domain< X, Y > > inet::math::IFunction< R, Domain< X, Y > >

Public Member Functions

 Rasterized2DFunction (X startX, X endX, int sizeX, Y startY, Y endY, int sizeY, const Ptr< const IFunction< R, Domain< X, Y >>> &function)
 
virtual R getValue (const Point< X, Y > &p) const override
 
virtual void partition (const Interval< X, Y > &i, const std::function< void(const Interval< X, Y > &, const IFunction< R, Domain< X, Y >> *)> callback) const override
 
- Public Member Functions inherited from inet::math::FunctionBase< R, Domain< X, Y > >
virtual void partition (const typename Domain< X, Y > ::I &i, const std::function< void(const typename Domain< X, Y > ::I &, const IFunction< R, Domain< X, Y > > *)> callback) const override
 Subdivides the provided domain and calls back f with the subdomains and the corresponding potentially simpler domain limited functions. More...
 
virtual Interval< R > getRange () const override
 Returns the valid range of the function as an interval. More...
 
virtual Interval< R > getRange (const typename Domain< X, Y > ::I &i) const override
 Returns the valid range of the function as an interval for the given domain. More...
 
virtual Domain< X, Y > ::I getDomain () const override
 Returns the valid domain of the function as an interval. More...
 
virtual bool isFinite () const override
 Returns true if the function value is finite in the whole domain. More...
 
virtual bool isFinite (const typename Domain< X, Y > ::I &i) const override
 Returns true if the function value is finite in the given domain. More...
 
virtual bool isNonZero () const override
 Returns true if the function value is non-zero in the whole domain. More...
 
virtual bool isNonZero (const typename Domain< X, Y > ::I &i) const override
 Returns true if the function value is non-zero in the given domain. More...
 
virtual R getMin () const override
 Returns the minimum value for the whole domain. More...
 
virtual R getMin (const typename Domain< X, Y > ::I &i) const override
 Returns the minimum value for the given domain. More...
 
virtual R getMax () const override
 Returns the maximum value for the whole domain. More...
 
virtual R getMax (const typename Domain< X, Y > ::I &i) const override
 Returns the maximum value for the given domain. More...
 
virtual R getMean () const override
 Returns the mean value for the whole domain. More...
 
virtual R getMean (const typename Domain< X, Y > ::I &i) const override
 Returns the mean value for the given domain. More...
 
virtual R getIntegral () const override
 Returns the integral value for the whole domain. More...
 
virtual R getIntegral (const typename Domain< X, Y > ::I &i) const override
 Returns the integral value for the given domain. More...
 
virtual const Ptr< const IFunction< R, Domain< X, Y > > > add (const Ptr< const IFunction< R, Domain< X, Y > >> &o) const override
 Adds the provided function to this function. More...
 
virtual const Ptr< const IFunction< R, Domain< X, Y > > > subtract (const Ptr< const IFunction< R, Domain< X, Y > >> &o) const override
 Substracts the provided function from this function. More...
 
virtual const Ptr< const IFunction< R, Domain< X, Y > > > multiply (const Ptr< const IFunction< double, Domain< X, Y > >> &o) const override
 Multiplies the provided function with this function. More...
 
virtual const Ptr< const IFunction< double, Domain< X, Y > > > divide (const Ptr< const IFunction< R, Domain< X, Y > >> &o) const override
 Divides this function with the provided function. More...
 
virtual std::ostream & printOn (std::ostream &os) const override
 
virtual void print (std::ostream &os, int level=0) const override
 Prints this function in human readable form to the provided stream for the whole domain. More...
 
virtual void print (std::ostream &os, const typename Domain< X, Y > ::I &i, int level=0) const override
 Prints this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printPartitioning (std::ostream &os, const typename Domain< X, Y > ::I &i, int level) const override
 Prints the partitioning of this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printPartition (std::ostream &os, const typename Domain< X, Y > ::I &i, int level=0) const override
 Prints a single partition of this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printStructure (std::ostream &os, int level=0) const override
 Prints the internal data structure of this function in a human readable form to the provided stream. More...
 
- Public Member Functions inherited from inet::math::IFunction< R, Domain< X, Y > >
virtual ~IFunction ()
 
virtual R getValue (const typename Domain< X, Y > ::P &p) const=0
 Returns the value of the function at the given point. More...
 

Protected Member Functions

void call (const Interval< X, Y > &i, const std::function< void(const Interval< X, Y > &, const IFunction< R, Domain< X, Y >> *)> callback) const
 

Protected Attributes

const X startX
 
const X endX
 
const X stepX
 
int sizeX
 
const Y startY
 
const Y endY
 
const Y stepY
 
int sizeY
 
const Ptr< const IFunction< R, Domain< X, Y > > > function
 

Detailed Description

template<typename R, typename X, typename Y>
class inet::math::Rasterized2DFunction< R, X, Y >

Rasterizes a function into a grid where each cell is a constant value with the mean of the original function.

Constructor & Destructor Documentation

◆ Rasterized2DFunction()

template<typename R , typename X , typename Y >
inet::math::Rasterized2DFunction< R, X, Y >::Rasterized2DFunction ( startX,
endX,
int  sizeX,
startY,
endY,
int  sizeY,
const Ptr< const IFunction< R, Domain< X, Y >>> &  function 
)
inline
553  :
554  startX(startX), endX(endX), stepX((endX - startX) / (sizeX - 1)), sizeX(sizeX),
555  startY(startY), endY(endY), stepY((endY - startY) / (sizeY - 1)), sizeY(sizeY),
556  function(function)
557  {
558  }

Member Function Documentation

◆ call()

template<typename R , typename X , typename Y >
void inet::math::Rasterized2DFunction< R, X, Y >::call ( const Interval< X, Y > &  i,
const std::function< void(const Interval< X, Y > &, const IFunction< R, Domain< X, Y >> *)>  callback 
) const
inlineprotected
545  {
546  if (!i.isEmpty()) {
547  ConstantFunction<R, Domain<X, Y>> g(R(0));
548  callback(i, &g);
549  }
550  }

◆ getValue()

template<typename R , typename X , typename Y >
virtual R inet::math::Rasterized2DFunction< R, X, Y >::getValue ( const Point< X, Y > &  p) const
inlineoverridevirtual
560  {
561  X x = std::get<0>(p);
562  Y y = std::get<1>(p);
563  int indexX = std::floor(toDouble(x - startX) / toDouble(stepX));
564  int indexY = std::floor(toDouble(y - startY) / toDouble(stepY));
565  if (indexX < 0 || indexX > sizeX - 2 || indexY < 0 || indexY > sizeY - 2)
566  return R(0);
567  else {
568  X x1 = startX + stepX * indexX;
569  X x2 = x1 + stepX;
570  Y y1 = startY + stepY * indexY;
571  Y y2 = y1 + stepY;
572  Point<X, Y> lower(x1, y1);
573  Point<X, Y> upper(x2, y2);
574  Interval<X, Y> interval(lower, upper, 0b11, 0b00, 0b00);
575  return function->getMean(interval);
576  }
577  }

◆ partition()

template<typename R , typename X , typename Y >
virtual void inet::math::Rasterized2DFunction< R, X, Y >::partition ( const Interval< X, Y > &  i,
const std::function< void(const Interval< X, Y > &, const IFunction< R, Domain< X, Y >> *)>  callback 
) const
inlineoverridevirtual
579  {
580  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(getLowerBound<X>(), getLowerBound<Y>()), Point<X, Y>(X(startX), Y(startY)), 0b00, 0b00, 0b00)), callback);
581  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(X(startX), getLowerBound<Y>()), Point<X, Y>(X(endX), Y(startY)), 0b10, 0b00, 0b00)), callback);
582  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(X(endX), getLowerBound<Y>()), Point<X, Y>(getUpperBound<X>(), Y(startY)), 0b10, 0b00, 0b00)), callback);
583 
584  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(getLowerBound<X>(), Y(startY)), Point<X, Y>(X(startX), Y(endY)), 0b01, 0b00, 0b00)), callback);
585  const auto& i1 = i.getIntersected(Interval<X, Y>(Point<X, Y>(X(startX), Y(startY)), Point<X, Y>(X(endX), Y(endY)), 0b11, 0b00, 0b00));
586  if (!i1.isEmpty()) {
587  int startIndexX = std::max(0, (int)std::floor(toDouble(std::get<0>(i1.getLower()) - startX) / toDouble(stepX)));
588  int endIndexX = std::min(sizeX - 1, (int)std::ceil(toDouble(std::get<0>(i1.getUpper()) - startX) / toDouble(stepX)));
589  int startIndexY = std::max(0, (int)std::floor(toDouble(std::get<1>(i1.getLower()) - startY) / toDouble(stepY)));
590  int endIndexY = std::min(sizeY - 1, (int)std::ceil(toDouble(std::get<1>(i1.getUpper()) - startY) / toDouble(stepY)));
591  int countX = endIndexX - startIndexX;
592  int countY = endIndexY - startIndexY;
593  R *means = new R[countX * countY];
594  for (int indexX = 0; indexX < countX; indexX++)
595  for (int indexY = 0; indexY < countY; indexY++)
596  means[indexY * countX + indexX] = R(0);
597  Point<X, Y> lower(startX + stepX * startIndexX, startY + stepY * startIndexY);
598  Point<X, Y> upper(startX + stepX * (endIndexX + 1), startY + stepY * (endIndexY + 1));
599  Interval<X, Y> interval(lower, upper, 0b11, 0b00, 0b00);
600  function->partition(interval, [&] (const Interval<X, Y>& i2, const IFunction<R, Domain<X, Y>> *f2) {
601  if (f2->isNonZero(i2)) {
602  int partitionStartIndexX = std::max(0, (int)std::floor(toDouble(std::get<0>(i2.getLower()) - startX) / toDouble(stepX)));
603  int partitionEndIndexX = std::min(sizeX - 1, (int)std::ceil(toDouble(std::get<0>(i2.getUpper()) - startX) / toDouble(stepX)));
604  int partitionStartIndexY = std::max(0, (int)std::floor(toDouble(std::get<1>(i2.getLower()) - startY) / toDouble(stepY)));
605  int partitionEndIndexY = std::min(sizeY - 1, (int)std::ceil(toDouble(std::get<1>(i2.getUpper()) - startY) / toDouble(stepY)));
606  for (int indexX = partitionStartIndexX; indexX < partitionEndIndexX; indexX++) {
607  for (int indexY = partitionStartIndexY; indexY < partitionEndIndexY; indexY++) {
608  Point<X, Y> lowerCell(startX + stepX * indexX, startY + stepY * indexY);
609  Point<X, Y> upperCell(startX + stepX * (indexX + 1), startY + stepY * (indexY + 1));
610  Interval<X, Y> cellInterval(lowerCell, upperCell, 0b11, 0b00, 0b00);
611  const auto& i3 = i2.getIntersected(cellInterval);
612  if (!i3.isEmpty()) {
613  R mean = f2->getMean(i3);
614  means[indexY * countX + indexX] += mean * i3.getVolume();
615  }
616  }
617  }
618  }
619  });
620  for (int indexX = startIndexX; indexX < endIndexX; indexX++) {
621  for (int indexY = startIndexY; indexY < endIndexY; indexY++) {
622  Point<X, Y> lowerCell(startX + stepX * indexX, startY + stepY * indexY);
623  Point<X, Y> upperCell(startX + stepX * (indexX + 1), startY + stepY * (indexY + 1));
624  Interval<X, Y> cellInterval(lowerCell, upperCell, 0b11, 0b00, 0b00);
625  const auto& i2 = i1.getIntersected(cellInterval);
626  if (!i2.isEmpty()) {
627  ConstantFunction<R, Domain<X, Y>> h(means[indexY * countX + indexX] / (toDouble(stepX) * toDouble(stepY)));
628  callback(i2, &h);
629  }
630  }
631  }
632  delete[] means;
633  }
634  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(X(endX), Y(startY)), Point<X, Y>(getUpperBound<X>(), Y(endY)), 0b11, 0b00, 0b00)), callback);
635 
636  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(getLowerBound<X>(), Y(endY)), Point<X, Y>(X(startX), getUpperBound<Y>()), 0b01, 0b00, 0b00)), callback);
637  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(X(startX), Y(endY)), Point<X, Y>(X(endX), getUpperBound<Y>()), 0b11, 0b00, 0b00)), callback);
638  call(i.getIntersected(Interval<X, Y>(Point<X, Y>(X(endX), Y(endY)), Point<X, Y>(getUpperBound<X>(), getUpperBound<Y>()), 0b11, 0b00, 0b00)), callback);
639  }

Member Data Documentation

◆ endX

template<typename R , typename X , typename Y >
const X inet::math::Rasterized2DFunction< R, X, Y >::endX
protected

◆ endY

template<typename R , typename X , typename Y >
const Y inet::math::Rasterized2DFunction< R, X, Y >::endY
protected

◆ function

template<typename R , typename X , typename Y >
const Ptr<const IFunction<R, Domain<X, Y> > > inet::math::Rasterized2DFunction< R, X, Y >::function
protected

◆ sizeX

template<typename R , typename X , typename Y >
int inet::math::Rasterized2DFunction< R, X, Y >::sizeX
protected

◆ sizeY

template<typename R , typename X , typename Y >
int inet::math::Rasterized2DFunction< R, X, Y >::sizeY
protected

◆ startX

template<typename R , typename X , typename Y >
const X inet::math::Rasterized2DFunction< R, X, Y >::startX
protected

◆ startY

template<typename R , typename X , typename Y >
const Y inet::math::Rasterized2DFunction< R, X, Y >::startY
protected

◆ stepX

template<typename R , typename X , typename Y >
const X inet::math::Rasterized2DFunction< R, X, Y >::stepX
protected

◆ stepY

template<typename R , typename X , typename Y >
const Y inet::math::Rasterized2DFunction< R, X, Y >::stepY
protected

The documentation for this class was generated from the following file:
inet::math::Rasterized2DFunction::stepY
const Y stepY
Definition: CompoundFunctions.h:540
inet::sctp::min
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SctpAssociation.h:261
inet::math::Rasterized2DFunction::sizeX
int sizeX
Definition: CompoundFunctions.h:537
inet::math::toDouble
double toDouble(const T v)
Definition: Point.h:28
inet::units::units::g
milli< kg >::type g
Definition: Units.h:1071
inet::math::Rasterized2DFunction::stepX
const X stepX
Definition: CompoundFunctions.h:536
inet::units::constants::R
const value< double, compose< units::J, compose< pow< units::mol, -1 >, pow< units::kg, -1 > > > > R(8.314472)
inet::math::Rasterized2DFunction::endX
const X endX
Definition: CompoundFunctions.h:535
inet::math::Rasterized2DFunction::startX
const X startX
Definition: CompoundFunctions.h:534
inet::math::Rasterized2DFunction::startY
const Y startY
Definition: CompoundFunctions.h:538
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::math::Rasterized2DFunction::endY
const Y endY
Definition: CompoundFunctions.h:539
inet::math::Rasterized2DFunction::sizeY
int sizeY
Definition: CompoundFunctions.h:541
inet::math::Rasterized2DFunction::call
void call(const Interval< X, Y > &i, const std::function< void(const Interval< X, Y > &, const IFunction< R, Domain< X, Y >> *)> callback) const
Definition: CompoundFunctions.h:545
inet::units::constants::h
const value< double, compose< units::J, units::s > > h(6.62606896e-34)