5#ifndef BALL_DATATYPE_REGULARDATA2D_H
6#define BALL_DATATYPE_REGULARDATA2D_H
8#ifndef BALL_MATHS_VECTOR2_H
12#ifndef BALL_SYSTEM_FILE_H
16#ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
36 template <
typename ValueType>
67 typedef typename std::vector<ValueType>::iterator
Iterator;
75 typedef typename std::vector<ValueType>::iterator
iterator;
77 typedef typename std::vector<ValueType>::reference
reference;
79 typedef typename std::vector<ValueType>::pointer
pointer;
81 typedef typename std::vector<ValueType>::size_type
size_type;
417 template <
class ValueType>
428 template <
class ValueType>
440 catch (std::bad_alloc&)
447 template <
class ValueType>
454 dimension_(dimension),
466 data_.resize(number_of_points);
468 catch (std::bad_alloc&)
471 throw Exception::OutOfMemory(__FILE__, __LINE__, number_of_points *
sizeof(ValueType));
475 template <
class ValueType>
482 dimension_(dimension),
496 catch (std::bad_alloc&)
499 throw Exception::OutOfMemory(__FILE__, __LINE__,
size *
sizeof(ValueType));
507 template <
class ValueType>
513 template <
typename ValueType>
533 catch (std::bad_alloc&)
543 template <
typename ValueType>
570 data_.resize(new_size);
582 data_[i] = old_data(v);
588 catch (std::bad_alloc&)
594 template <
typename ValueType>
618 std::vector<ValueType> old_data(
data_);
621 data_.resize(new_size);
631 data_[i] = default_value;
644 catch (std::bad_alloc&)
650 template <
class ValueType>
656 template <
class ValueType>
664 template <
class ValueType>
669 size_type pos = index.
x + index.
y * size_.x;
670 if (pos >= data_.size())
677 template <
class ValueType>
681 size_type pos = index.x + index.y * size_.x;
682 if (pos >= data_.size())
689 template <
class ValueType>
693 if (index >=
data_.size())
700 template <
class ValueType>
704 if (index >=
data_.size())
711 template <
class ValueType>
716 if ((index.
x >= size_.x) || (index.
y >= size_.y))
721 CoordinateType r(origin_.x + index.
x * spacing_.x,
722 origin_.y + index.
y * spacing_.y);
727 template <
class ValueType>
732 if (position >=
data_.size())
744 template <
typename ValueType>
762 ll = position.
x +
size_.x * position.
y;
768 template <
typename ValueType>
780 Position ll_id, lr_id, ul_id, ur_id;
790 template <
typename ValueType>
800 return this->operator () (r);
803 template <
typename ValueType>
813 if (x >= (
size_.x - 1))
817 if (y >= (
size_.y - 1))
826 double dx = 1.0 - ((r.x - r_0.
x) /
spacing_.x);
827 double dy = 1.0 - ((r.y - r_0.
y) /
spacing_.y);
829 return data_[l] * dx * dy
830 +
data_[l + 1] * (1.0 - dx) * dy
832 +
data_[l +
size_.x + 1] * (1.0 - dx) * (1.0 - dy);
835 template <
typename ValueType>
852 template <
typename ValueType>
869 template <
typename ValueType>
879 static IndexType position;
880 position.x = (Position)((r.
x - origin_.x) / spacing_.x + 0.5);
881 position.y = (Position)((r.
y - origin_.y) / spacing_.y + 0.5);
883 return operator [] (position);
886 template <
typename ValueType>
897 position.x = (
Position)((r.x - origin_.x) / spacing_.x + 0.5);
898 position.y = (
Position)((r.y - origin_.y) / spacing_.y + 0.5);
900 return operator [] (position);
903 template <
typename ValueType>
909 for (
Position i = 0; i < data_points; i++)
917 template <
typename ValueType>
924 for (
Position i = 0; i < data_points; i++)
926 stddev += (pow(
data_(i)-mean,2));
928 stddev /= (data_points-1);
929 stddev = sqrt(stddev);
933 template <
typename ValueType>
945 template <
typename ValueType>
959 template <
typename ValueType>
972 std::copy(data.
begin(), data.
end(), std::ostream_iterator<ValueType>(os,
"\n"));
977 template <
typename ValueType>
984 is >> origin.
x >> origin.
y;
985 is >> dimension.
x >> dimension.
y;
986 is >> size.
x >> size.
y;
996 std::copy(std::istream_iterator<ValueType>(is),
997 std::istream_iterator<ValueType>(),
1005 template <
typename ValueType>
1008 File outfile(filename.
c_str(), std::ios::out|std::ios::binary);
1020 outfile << adapt_size;
1030 outfile << adapt_float;
1032 outfile << adapt_float;
1035 outfile << adapt_float;
1037 outfile << adapt_float;
1040 outfile << adapt_float;
1042 outfile << adapt_float;
1046 outfile << adapt_index;
1049 const int BLOCK_SIZE = 1024;
1050 Index window_pos = 0;
1052 while (((
int)
data_.size() - (BLOCK_SIZE + window_pos)) >= 0)
1055 outfile << adapt_block;
1056 window_pos += BLOCK_SIZE;
1060 for (
Size i = window_pos; i <
data_.size(); i++)
1063 outfile << adapt_single;
1070 template <
typename ValueType>
1073 File infile(filename, std::ios::in|std::ios::binary);
1074 if (!infile.isValid())
1086 infile >> adapt_size;
1089 infile >> adapt_float;
1091 infile >> adapt_float;
1094 infile >> adapt_float;
1096 infile >> adapt_float;
1099 infile >> adapt_float;
1101 infile >> adapt_float;
1105 infile >> adapt_index;
1108 data_.resize(new_size);
1111 Index window_pos = 0;
1113 while ( ((
int)
data_.size() - (1024 + window_pos)) >= 0 )
1115 infile >> adapt_block;
1127 for (
Size i=window_pos; i<
data_.size(); i++)
1129 infile >> adapt_single;
1130 data_[i] = adapt_single.getData();
const T & getData() const
void setData(const T &data)
const char * c_str() const BALL_NOEXCEPT
The index type used to refer to a specific element in the grid (x-, and y-index).
IndexType(Position p, Position q)
std::vector< ValueType > VectorType
The type containing an STL vector of the appropriate type.
ValueType & getClosestValue(const CoordinateType &x)
BALL_INLINE Iterator begin()
CoordinateType getCoordinates(Position index) const
const ValueType & getData(const IndexType &index) const
BALL_INLINE ConstIterator end() const
std::vector< ValueType >::iterator Iterator
A mutable iterator.
const ValueType & getClosestValue(const CoordinateType &x) const
BALL_INLINE void swap(TRegularData2D< ValueType > &data)
TRegularData2D(const TRegularData2D< ValueType > &data)
TVector2< float > CoordinateType
The coordinate type.
BALL_INLINE size_type size() const
const CoordinateType & getSpacing() const
BALL_INLINE size_type max_size() const
TRegularData2D(const CoordinateType &origin, const CoordinateType &dimension, const CoordinateType &spacing)
std::vector< ValueType >::iterator iterator
ValueType & getData(Position index)
std::vector< ValueType >::const_iterator const_iterator
const ValueType & operator[](const IndexType &index) const
std::vector< ValueType >::reference reference
BALL_INLINE Iterator end()
ValueType operator()(const CoordinateType &x) const
void getEnclosingValues(const CoordinateType &r, ValueType &ll, ValueType &lr, ValueType &ul, ValueType &ur) const
const IndexType & getSize() const
TRegularData2D & operator=(const TRegularData2D< ValueType > &data)
BALL_INLINE bool empty() const
Empty predicate.
void rescale(const IndexType &new_size)
void binaryRead(const String &filename)
void setOrigin(const CoordinateType &origin)
IndexType getClosestIndex(const CoordinateType &v) const
ValueType & getData(const IndexType &index)
bool isInside(const CoordinateType &x) const
Test if a given point is inside the grid.
CoordinateType getCoordinates(const IndexType &index) const
const CoordinateType & getOrigin() const
const ValueType & getData(Position index) const
ValueType getInterpolatedValue(const CoordinateType &x) const
std::vector< ValueType >::const_reference const_reference
TRegularData2D(const IndexType &size, const CoordinateType &origin=CoordinateType(0.0), const CoordinateType &dimension=CoordinateType(1.0))
IndexType getLowerIndex(const CoordinateType &v) const
std::vector< ValueType >::size_type size_type
virtual ~TRegularData2D()
bool operator==(const TRegularData2D< ValueType > &data) const
BALL_INLINE bool operator!=(const TRegularData2D< ValueType > &data) const
Inequality operator.
void setDimension(const CoordinateType &dimension)
CoordinateType dimension_
ValueType calculateSD() const
ValueType calculateMean() const
std::vector< ValueType >::const_iterator ConstIterator
A nonmutable iterator.
const CoordinateType & getDimension() const
void getEnclosingIndices(const CoordinateType &r, Position &ll, Position &lr, Position &ul, Position &ur) const
std::vector< ValueType >::difference_type difference_type
BALL_INLINE ConstIterator begin() const
void resize(const IndexType &new_size)
void binaryWrite(const String &filename) const
std::vector< ValueType >::pointer pointer
#define BALL_CREATE(name)
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
TRegularData2D< float > RegularData2D
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
The block data type for reading and writing binary data.