1 #ifndef _QWINTERPOLATOR_H_
2 #define _QWINTERPOLATOR_H_
22 #define mycout QwMessage
23 #define mycerr QwError
24 #define myendl QwLog::endl
50 template <
class value_t =
float,
unsigned int value_n = 1>
64 const std::vector<coord_t>& max,
65 const std::vector<coord_t>& step) {
130 mycerr <<
"Dimensions of the grid should be strictly positive!" <<
myendl;
140 std::vector<coord_t>(
fNDim,max),
141 std::vector<coord_t>(
fNDim,step));
145 const std::vector<coord_t>& max,
146 const std::vector<coord_t>& step) {
150 if (min.size() !=
fNDim || min.size() !=
fNDim || step.size() !=
fNDim)
return;
153 for (
size_t i = 0; i <
fMin.size(); i++) {
156 fSize[i] =
static_cast<unsigned int>(int_part) + (frac_part > 0.5 ? 1 : 0);
161 for (
unsigned int i = 0; i < value_n; i++) {
164 }
catch (std::bad_alloc&
e) {
165 mycerr <<
"QwInterpolator could not allocate memory for values!" <<
myendl;
182 {
return fWrap.at(dim); }
185 {
fWrap.at(dim) = wrap; }
187 if (wrap.size() !=
fNDim)
return;
193 {
return fRedux.at(dim); }
196 {
fRedux.at(dim) = redux; }
198 for (
unsigned int dim = 0; dim <
fNDim; dim++)
202 if (redux.size() !=
fNDim)
return;
217 unsigned int* cover =
new unsigned int[
fSize[dim]];
218 unsigned int* total =
new unsigned int[fSize[dim]];
219 for (
unsigned int i = 0; i < fSize[dim]; i++) {
224 value_t value[value_n];
225 for (
unsigned int linear_index = 0; linear_index <
fMaximumEntries; linear_index++) {
228 if (
Get(linear_index,value) && value[0] != 0)
229 cover[f_cell_index[dim]]++;
233 for (
size_t i = 0; i < fSize[dim]; i++) {
236 mycout <<
"bin " << i <<
", coord " << coord[dim] <<
": "
237 << double(cover[i]) / double(total[i]) * 100 <<
"%"<<
myendl;
254 if (value_n != 1)
return false;
255 if (
fNDim != 1)
return false;
256 return Set(&coord, &value);
260 if (value_n != 1)
return false;
261 return Set(coord, &value);
268 bool written =
false;
269 unsigned int linear_index;
270 for (
unsigned int dim = 0; dim <
fNDim; dim++) {
272 if (
fWrap[dim] == 0)
continue;
277 for (
size_t wrap = 0; wrap <
fWrap[dim]; wrap++) {
281 status &=
Set(linear_index, value);
285 status &=
Set(linear_index, value);
294 status &=
Set(linear_index, value);
300 bool Set(
const unsigned int linear_index,
const value_t& value) {
301 if (value_n != 1)
return false;
302 return Set(linear_index, &value);
305 bool Set(
const unsigned int linear_index,
const value_t* value) {
306 if (!
Check(linear_index))
return false;
307 for (
unsigned int i = 0; i < value_n; i++)
308 fValues[i][linear_index] = value[i];
314 bool Set(
const unsigned int* cell_index,
const value_t& value) {
315 if (value_n != 1)
return false;
316 return Set(cell_index, &value);
319 bool Set(
const unsigned int* cell_index,
const value_t* value) {
320 if (!
Check(cell_index))
return false;
321 return Set(
Index(cell_index),value);
330 if (value_n != 1)
return false;
331 if (
fNDim != 1)
return false;
333 if (
GetValue(&coord, &value))
return value;
338 if (value_n != 1)
return 0;
340 if (
GetValue(coord, &value))
return value;
345 if (value_n != 1)
return false;
350 for (
unsigned int i = 0; i < value_n; i++) value[i] = 0.0;
351 if (!
Check(coord))
return false;
352 value_t result[value_n];
356 if (!
Linear(coord, result))
return false;
361 default:
return false;
363 for (
unsigned int i = 0; i < value_n; i++) value[i] = result[i];
372 bool WriteText(std::ostream& stream)
const;
375 std::ofstream file(filename.c_str());
376 if (! file.is_open())
return false;
387 bool ReadText(std::istream& stream);
390 std::ifstream file(filename.c_str());
391 if (! file.is_open())
return false;
412 unsigned int Index(
const unsigned int* cell_index)
const;
414 unsigned int Index(
const unsigned int* cell_index,
const unsigned int offset)
const;
417 void Cell(
const coord_t coord,
unsigned int& cell_index,
double& cell_local,
const unsigned int dim)
const;
419 void Cell(
const coord_t* coord,
unsigned int* cell_index,
double* cell_local)
const;
421 void Cell(
const coord_t* coord,
unsigned int* cell_index)
const;
423 void Cell(
unsigned int linear_index,
unsigned int* cell_index)
const;
426 void Coord(
const unsigned int* cell_index,
coord_t* coord)
const;
428 void Coord(
const unsigned int linear_index,
coord_t* coord)
const;
437 for (
unsigned int dim = 0; dim <
fNDim; dim++)
449 bool Check(
const unsigned int* cell_index)
const;
451 bool Check(
const unsigned int linear_index)
const;
454 value_t
Get(
const unsigned int* cell_index)
const {
455 if (value_n != 1)
return 0;
460 value_t
Get(
const unsigned int index)
const {
464 bool Get(
const unsigned int index, value_t* value)
const {
465 for (
unsigned int i = 0; i < value_n; i++)
479 template <
class value_t,
unsigned int value_n>
482 value_t* value)
const
485 Cell(coord, f_cell_index, f_cell_local);
487 for (
unsigned int i = 0; i < value_n; i++) value[i] = 0.0;
490 for (
unsigned int offset = 0; offset < (1U << fNDim); offset++) {
491 value_t neighbor[value_n];
492 if (! Get(Index(f_cell_index,offset), neighbor))
return false;
495 for (
unsigned int dim = 0; dim < fNDim; dim++)
496 fac *= ((offset >> dim) & 0x1)? f_cell_local[dim] : (1 - f_cell_local[dim]);
498 for (
unsigned int i = 0; i < value_n; i++)
499 value[i] += fac * neighbor[i];
509 template <
class value_t,
unsigned int value_n>
512 value_t* value)
const
515 Nearest(coord, f_cell_index);
516 return Get(Index(f_cell_index), value);
524 template <
class value_t,
unsigned int value_n>
527 for (
unsigned int dim = 0; dim < fNDim; dim++)
528 if (fWrap[dim] == 0 && (coord[dim] < fMin[dim] || coord[dim] > fMax[dim]))
539 template <
class value_t,
unsigned int value_n>
542 for (
unsigned int dim = 0; dim < fNDim; dim++)
543 if (cell_index[dim] >= fSize[dim])
554 template <
class value_t,
unsigned int value_n>
557 if (linear_index >= fMaximumEntries)
568 template <
class value_t,
unsigned int value_n>
570 const unsigned int* cell_index)
const
572 unsigned int linear_index = 0;
574 for (
unsigned int dim = 0; dim < fNDim; dim++)
576 linear_index += fExtent[dim] * cell_index[dim];
586 template <
class value_t,
unsigned int value_n>
588 const unsigned int* cell_index,
589 const unsigned int pattern)
const
591 unsigned int linear_index = 0;
593 for (
unsigned int dim = 0; dim < fNDim; dim++) {
595 unsigned int offset = (pattern >> dim) & 0x1;
596 linear_index += fExtent[dim] * (cell_index[dim] + offset);
608 template <
class value_t,
unsigned int value_n>
611 unsigned int &cell_index,
613 const unsigned int dim)
const
616 double norm_coord = (coord - fMin[dim]) / fStep[dim];
618 double frac_part, int_part;
619 frac_part = modf(norm_coord, &int_part);
620 cell_local = frac_part;
621 cell_index =
static_cast<int>(int_part);
623 if (fWrap[dim] > 0) cell_index %= (fSize[dim] - fWrap[dim]);
632 template <
class value_t,
unsigned int value_n>
635 unsigned int* cell_index,
636 double* cell_local)
const
639 for (
unsigned int dim = 0; dim < fNDim; dim++)
640 Cell(coord[dim], cell_index[dim], cell_local[dim], dim);
648 template <
class value_t,
unsigned int value_n>
651 unsigned int* cell_index)
const
654 Cell(coord, cell_index, f_cell_local);
662 template <
class value_t,
unsigned int value_n>
664 unsigned int linear_index,
665 unsigned int* cell_index)
const
668 for (
int dim = fNDim-1; dim >= 0; dim--) {
669 cell_index[dim] = linear_index / fExtent[dim];
670 linear_index -= cell_index[dim] * fExtent[dim];
679 template <
class value_t,
unsigned int value_n>
681 const unsigned int* cell_index,
684 for (
unsigned int dim = 0; dim < fNDim; dim++)
685 coord[dim] = fMin[dim] + cell_index[dim] * fStep[dim];
693 template <
class value_t,
unsigned int value_n>
695 const unsigned int linear_index,
698 Cell(linear_index,f_cell_index);
699 Coord(f_cell_index,coord);
706 template <
class value_t,
unsigned int value_n>
710 mycout <<
"Writing text stream: ";
712 stream << fNDim <<
"\t" << value_n << std::endl;
714 for (
unsigned int dim = 0; dim < fNDim; dim++)
715 stream << dim <<
"\t" << fMin[dim] <<
"\t" << fMax[dim] <<
"\t" << fStep[dim] << std::endl;
717 stream << fValues[0].size() << std::endl;
718 unsigned int entries = fValues[0].size();
719 for (
unsigned int index = 0; index < entries; index++) {
721 for (
unsigned int i = 0; i < value_n; i++) {
722 stream << fValues[i][index] <<
"\t";
726 if (index % (entries / 10) == 0)
728 if (index % (entries / 10) != 0
729 && index % (entries / 40) == 0)
732 stream <<
"end" << std::endl;
742 template <
class value_t,
unsigned int value_n>
746 mycout <<
"Reading text stream: ";
749 stream >> fNDim >> n;
750 if (n != value_n)
return false;
751 SetDimensions(fNDim);
753 for (
unsigned int dim = 0; dim < fNDim; dim++)
754 stream >> dim >> fMin[dim] >> fMax[dim] >> fStep[dim];
755 SetMinimumMaximumStep(fMin, fMax, fStep);
757 unsigned int entries;
759 for (
unsigned int index = 0; index < entries; index++) {
761 for (
unsigned int i = 0; i < value_n; i++) {
762 stream >> fValues[i][index];
765 if (index % (entries / 10) == 0)
767 if (index % (entries / 10) != 0
768 && index % (entries / 40) == 0)
776 if (end ==
"end")
return true;
791 template <
class value_t,
unsigned int value_n>
794 std::ofstream file(filename.c_str(), std::ios::binary);
795 if (! file.is_open())
return false;
797 mycout <<
"Writing binary file: ";
799 uint32_t n = value_n;
800 uint32_t size =
sizeof(value_t);
801 file.write(reinterpret_cast<const char*>(&n),
sizeof(n));
802 file.write(reinterpret_cast<const char*>(&size),
sizeof(size));
804 uint32_t ndim = fNDim;
805 file.write(reinterpret_cast<const char*>(&ndim),
sizeof(ndim));
806 for (
unsigned int dim = 0; dim < fNDim; dim++) {
807 file.write(reinterpret_cast<const char*>(&fMin[dim]),
sizeof(fMin[dim]));
808 file.write(reinterpret_cast<const char*>(&fMax[dim]),
sizeof(fMax[dim]));
809 file.write(reinterpret_cast<const char*>(&fStep[dim]),
sizeof(fStep[dim]));
811 uint32_t entries = fValues[0].size();
812 file.write(reinterpret_cast<const char*>(&entries),
sizeof(entries));
813 for (
unsigned int index = 0; index < entries; index++) {
815 for (
unsigned int i = 0; i < value_n; i++) {
816 value_t value = fValues[i][index];
817 file.write(reinterpret_cast<const char*>(&value),
sizeof(value));
820 if (index % (entries / 10) == 0)
822 if (index % (entries / 10) != 0
823 && index % (entries / 40) == 0)
836 template <
class value_t,
unsigned int value_n>
839 std::ifstream file(filename.c_str(), std::ios::binary);
840 if (! file.is_open())
return false;
842 mycout <<
"Reading binary file: ";
844 file.seekg(0, std::ios::end);
846 file.seekg(0, std::ios::beg);
848 file.read(reinterpret_cast<char*>(&n),
sizeof(n));
849 file.read(reinterpret_cast<char*>(&size),
sizeof(size));
850 if (n != value_n)
return false;
851 if (size !=
sizeof(value_t))
return false;
854 file.read(reinterpret_cast<char*>(&ndim),
sizeof(ndim));
857 for (
unsigned int dim = 0; dim < fNDim; dim++) {
858 file.read(reinterpret_cast<char*>(&fMin[dim]),
sizeof(fMin[dim]));
859 file.read(reinterpret_cast<char*>(&fMax[dim]),
sizeof(fMax[dim]));
860 file.read(reinterpret_cast<char*>(&fStep[dim]),
sizeof(fStep[dim]));
862 SetMinimumMaximumStep(fMin, fMax, fStep);
864 uint32_t maximum_entries;
865 file.read(reinterpret_cast<char*>(&maximum_entries),
sizeof(maximum_entries));
866 if (maximum_entries != fMaximumEntries)
return false;
867 int value_size =
sizeof(value_t);
868 unsigned int entries = fValues[0].size();
869 for (
unsigned int index = 0; index < entries; index++) {
871 for (
unsigned int i = 0; i < value_n; i++) {
872 file.read((
char*)(&fValues[i][index]),value_size);
875 if (index % (entries / 10) == 0)
877 if (index % (entries / 10) != 0
878 && index % (entries / 40) == 0)
887 #endif // _QWINTERPOLATOR_H_
bool Set(const unsigned int linear_index, const value_t &value)
Set a single value at a linearized index (false if not possible)
std::vector< coord_t > fStep
Step size in each dimension.
QwInterpolator(const unsigned int ndim=1)
Constructor with number of dimensions.
unsigned int fMaximumEntries
Maximum number of values.
bool GetValue(const coord_t *coord, double &value) const
Get the interpolated value at coordinate (zero if out of bounds)
virtual ~QwInterpolator()
Destructor.
unsigned int GetWrapCoordinate(const unsigned int dim) const
Get wrapping coordinate.
void PrintCoverage(const unsigned int dim)
Print coverage map for all bins in one dimension.
EQwInterpolationMethod
Allowed interpolation methods.
value_t Get(const unsigned int index) const
Get a single value by linearized index (unchecked)
QwInterpolator(const std::string &filename)
Constructor with file name.
bool Set(const unsigned int linear_index, const value_t *value)
Set a set of values at a linearized index (false if not possible)
unsigned int GetCurrentEntries() const
Get the current number of entries.
std::vector< coord_t > fMin
Minimum in each dimension.
bool Set(const unsigned int *cell_index, const value_t &value)
Set a single value at a grid point (false if out of bounds)
std::vector< size_t > fExtent
coord_t GetMaximum(const unsigned int dim) const
Get maximum in dimension.
unsigned int fNDim
Number of dimensions in coordinates.
bool ReadText(std::istream &stream)
Read the grid from text.
bool Set(const coord_t &coord, const value_t &value)
Set a single value at a coordinate (false if not possible)
void SetInterpolationMethod(const EQwInterpolationMethod method)
Set the interpolation method.
std::vector< coord_t > fMax
Maximum in each dimension.
void SetDataReductionFactor(const unsigned int dim, const unsigned int redux)
Set data reduction factor.
bool Check(const coord_t *coord) const
Check for boundaries with coordinate argument.
void SetWrapCoordinate(const unsigned int dim, const size_t wrap=1)
Set wrapping coordinate.
QwInterpolator(const std::vector< coord_t > &min, const std::vector< coord_t > &max, const std::vector< coord_t > &step)
Constructor with minimum, maximum, and step size.
std::vector< size_t > fSize
Number of points in each dimension.
bool ReadBinaryFile(const std::string &filename)
Read the grid values from binary file.
bool WriteBinaryFile(const std::string &filename) const
Write the grid values to binary file.
A logfile class, based on an identical class in the Hermes analyzer.
std::vector< size_t > fRedux
Data reduction factor.
int GetDataReductionFactor(const unsigned int dim) const
Get data reduction factor.
EQwInterpolationMethod GetInterpolationMethod() const
Get the interpolation method.
bool Get(const unsigned int index, value_t *value) const
Get a vector value by linearized index (unchecked)
void Cell(const coord_t coord, unsigned int &cell_index, double &cell_local, const unsigned int dim) const
Return the cell index and local coordinates in one dimension (unchecked)
unsigned int * f_cell_index
Pre-allocated cell index.
value_t Get(const unsigned int *cell_index) const
Get a single value by cell index (unchecked)
value_t GetValue(const coord_t *coord) const
Get the interpolated value at coordinate (zero if out of bounds)
void Coord(const unsigned int *cell_index, coord_t *coord) const
Return the coordinates based on the cell index (unchecked)
unsigned int Index(const coord_t *coord) const
Return the linearized index based on the point coordinates (unchecked)
coord_t GetMinimum(const unsigned int dim) const
Get minimum in dimension.
bool ReadTextFile(const std::string &filename)
Read the grid from text file.
void Nearest(const coord_t *coord, unsigned int *cell_index) const
Return the cell index closest to the coordinate (could be above) (unchecked)
bool Set(const coord_t *coord, const value_t &value)
Set a single value at a coordinate (false if not possible)
void SetDataReductionFactor(const unsigned int redux)
bool InBounds(const coord_t *coord) const
Return true if the coordinate is in bounds.
std::vector< value_t > fValues[value_n]
Table with pointers to arrays of values.
bool Set(const unsigned int *cell_index, const value_t *value)
Set a set of values at a grid point (false if out of bounds)
bool Linear(const coord_t *coord, value_t *value) const
Linear interpolation (unchecked)
bool WriteTextFile(const std::string &filename) const
Write the grid to text file.
void SetDataReductionFactor(const std::vector< unsigned int > &redux)
unsigned int fCurrentEntries
Number of values read in.
bool GetValue(const coord_t *coord, double *value) const
Get the interpolated value at coordinate (zero if out of bounds)
std::vector< size_t > fWrap
Wrap around this coordinate.
double * f_cell_local
Pre-allocated local coordinates.
bool WriteTextScreen() const
Write the grid to screen.
void SetMinimumMaximumStep(const std::vector< coord_t > &min, const std::vector< coord_t > &max, const std::vector< coord_t > &step)
Set minimum, maximum, and step size to different values.
void SetDimensions(const unsigned int ndim)
Set the number of coordinate dimensions and resize vectors.
coord_t GetStepSize(const unsigned int dim) const
Get minimum in dimension.
unsigned int GetMaximumEntries() const
Get the maximum number of entries.
bool NearestNeighbor(const coord_t *coord, value_t *value) const
Nearest-neighbor (unchecked)
void SetMinimumMaximumStep(const coord_t min, const coord_t max, const coord_t step)
Set minimum, maximum, and step size to single values.
EQwInterpolationMethod fInterpolationMethod
Interpolation method.
bool Set(const coord_t *coord, const value_t *value)
Set a set of values at a coordinate (false if not possible)
A multi-dimensional grid of values with interpolation methods.
void SetWrapCoordinate(const std::vector< size_t > &wrap)
bool WriteText(std::ostream &stream) const
Write the grid as text.
value_t GetValue(const coord_t &coord) const
Get the interpolated value at coordinate (zero if out of bounds)
static std::ostream & flush(std::ostream &)
Flush the streams.