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;
121 virtual void clear();
182 const ValueType&
getData(
const IndexType& index)
const;
188 ValueType&
getData(
const IndexType& index);
321 void resize(
const IndexType& new_size);
332 void rescale(
const IndexType& new_size);
367 (
const CoordinateType& r, ValueType& ll, ValueType& lr, ValueType& ul, ValueType& ur)
const;
417 template <
class ValueType>
428 template <
class ValueType>
431 origin_(data.origin_),
432 dimension_(data.dimension_),
433 spacing_(data.spacing_),
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>
477 (
const typename TRegularData2D<ValueType>::CoordinateType& origin,
478 const typename TRegularData2D<ValueType>::CoordinateType& dimension,
479 const typename TRegularData2D<ValueType>::CoordinateType& spacing)
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>
524 dimension_ = rhs.dimension_;
525 spacing_ = rhs.spacing_;
533 catch (std::bad_alloc&)
543 template <
typename ValueType>
547 if ((size.
x == size_.x) && (size_.y == size.
y))
553 if ((size.
x == 0) || (size.
y == 0))
570 data_.resize(new_size);
571 spacing_.x = dimension_.x / (
double)(size.
x - 1);
572 spacing_.y = dimension_.y / (
double)(size.
y - 1);
580 v.
x = origin_.x + x * spacing_.x;
581 v.
y = origin_.y + y * spacing_.y;
582 data_[i] = old_data(v);
588 catch (std::bad_alloc&)
594 template <
typename ValueType>
598 if (size.
x == size_.x && size_.y == size.
y)
604 if ((size.
x == 0) || (size.
y == 0))
607 dimension_.set(0.0, 0.0);
618 std::vector<ValueType> old_data(data_);
621 data_.resize(new_size);
624 static ValueType default_value = (ValueType)0;
629 if (x >= size_.x || y >= size_.y)
631 data_[i] = default_value;
635 data_[i] = old_data[x + y * size_.x];
640 dimension_.x *= (
double)size.
x / (
double)size_.x;
641 dimension_.y *= (
double)size.
y / (
double)size_.y;
644 catch (std::bad_alloc&)
650 template <
class ValueType>
656 template <
class ValueType>
660 return ((r.
x >= origin_.x) && (r.
x <= (origin_.x + dimension_.x))
661 && (r.
y >= origin_.y) && (r.
y <= (origin_.y + dimension_.y)));
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())
684 throw Exception::OutOfGrid(__FILE__, __LINE__);
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>
729 typename TRegularData2D<ValueType>::CoordinateType
732 if (position >= data_.size())
741 origin_.y + (
double)y * spacing_.y);
744 template <
typename ValueType>
758 position.
x = (
Position)((r.
x - origin_.x) / spacing_.x);
759 position.
y = (
Position)((r.
y - origin_.y) / spacing_.y);
762 ll = position.
x + size_.x * position.
y;
768 template <
typename ValueType>
772 ValueType& ll, ValueType& lr, ValueType& ul, ValueType& ur)
const
780 Position ll_id, lr_id, ul_id, ur_id;
781 getEnclosingIndices(r, 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))
822 Size l = x + size_.x * y;
824 origin_.y + (
double)y * spacing_.y);
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
831 + data_[l + size_.x] * dx * (1.0 - dy)
832 + data_[l + size_.x + 1] * (1.0 - dx) * (1.0 - dy);
835 template <
typename ValueType>
846 position.
x = (
Position)((r.
x - origin_.x) / spacing_.x);
847 position.
y = (
Position)((r.
y - origin_.y) / spacing_.y);
852 template <
typename ValueType>
863 position.
x = (
Position)((r.
x - origin_.x) / spacing_.x + 0.5);
864 position.
y = (
Position)((r.
y - origin_.y) / spacing_.y + 0.5);
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>
889 (
const typename TRegularData2D<ValueType>::CoordinateType& r)
893 throw Exception::OutOfGrid(__FILE__, __LINE__);
896 static IndexType position;
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>
907 Position data_points = (size_.x * size_.y);
909 for (
Position i = 0; i < data_points; i++)
917 template <
typename ValueType>
921 Position data_points = (size_.x * size_.y);
922 ValueType stddev = 0;
923 ValueType mean = this->calculateMean();
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>
939 dimension_.set(0.0, 0.0);
942 spacing_.set(1.0, 1.0);
945 template <
typename ValueType>
948 return ((origin_ == data.
origin_)
950 && (size_.x == data.
size_.x)
951 && (size_.y == data.
size_.y)
952 && (data_ == data.
data_));
959 template <
typename ValueType>
960 std::ostream& operator << (std::ostream& os, const TRegularData2D<ValueType>& data)
963 os << data.getOrigin().x <<
" " << data.getOrigin().y
965 << data.getOrigin().x + data.getDimension().x <<
" "
966 << data.getOrigin().y + data.getDimension().y
968 << data.getSize().x - 1 <<
" " << data.getSize().y - 1
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);
1009 if (!outfile.isValid())
1019 adapt_size.
setData(data_.size());
1020 outfile << adapt_size;
1029 adapt_float.
setData(origin_.x);
1030 outfile << adapt_float;
1031 adapt_float.
setData(origin_.y);
1032 outfile << adapt_float;
1034 adapt_float.setData(dimension_.x);
1035 outfile << adapt_float;
1036 adapt_float.setData(dimension_.y);
1037 outfile << adapt_float;
1039 adapt_float.setData(spacing_.x);
1040 outfile << adapt_float;
1041 adapt_float.setData(spacing_.y);
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++)
1062 adapt_single.
setData(data_[i]);
1063 outfile << adapt_single;
1070 template <
typename ValueType>
1073 File infile(filename, std::ios::in|std::ios::binary);
1086 infile >> adapt_size;
1087 Size new_size = adapt_size.getData();
1089 infile >> adapt_float;
1090 origin_.x = adapt_float.
getData();
1091 infile >> adapt_float;
1092 origin_.y = adapt_float.getData();
1094 infile >> adapt_float;
1095 dimension_.x = adapt_float.getData();
1096 infile >> adapt_float;
1097 dimension_.y = adapt_float.getData();
1099 infile >> adapt_float;
1100 spacing_.x = adapt_float.getData();
1101 infile >> adapt_float;
1102 spacing_.y = adapt_float.getData();
1105 infile >> adapt_index;
1106 size_ = adapt_index.
getData();
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();
1138 #endif // BALL_DATATYPE_TREGULARDATA2D_H