BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
regularData2D.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_DATATYPE_REGULARDATA2D_H
6 #define BALL_DATATYPE_REGULARDATA2D_H
7 
8 #ifndef BALL_MATHS_VECTOR2_H
9 # include <BALL/MATHS/vector2.h>
10 #endif
11 
12 #ifndef BALL_SYSTEM_FILE_H
13 # include <BALL/SYSTEM/file.h>
14 #endif
15 
16 #ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
18 #endif
19 
20 #include <iostream>
21 #include <fstream>
22 #include <iterator>
23 #include <algorithm>
24 
25 namespace BALL
26 {
36  template <typename ValueType>
38  {
39  public:
40 
42 
43 
46 
48  class IndexType
49  {
50  public:
51  inline IndexType() : x(0), y(0) {}
52  inline IndexType(Position p) : x(p), y(p) {}
53  inline IndexType(Position p, Position q) : x(p), y(q) {}
54 
59 
60  };
61 
63  typedef std::vector<ValueType> VectorType;
67  typedef typename std::vector<ValueType>::iterator Iterator;
69  typedef typename std::vector<ValueType>::const_iterator ConstIterator;
71 
72  // STL compatibility types
73  //
74  typedef ValueType value_type;
75  typedef typename std::vector<ValueType>::iterator iterator;
76  typedef typename std::vector<ValueType>::const_iterator const_iterator;
77  typedef typename std::vector<ValueType>::reference reference;
78  typedef typename std::vector<ValueType>::const_reference const_reference;
79  typedef typename std::vector<ValueType>::pointer pointer;
80  typedef typename std::vector<ValueType>::difference_type difference_type;
81  typedef typename std::vector<ValueType>::size_type size_type;
82 
86 
91 
96 
103  TRegularData2D(const CoordinateType& origin, const CoordinateType& dimension, const CoordinateType& spacing);
104 
105  /* Constructor.
106  * This constructor takes the size of the grid (as the number of grid points per dimension)
107  * as input and (optionally) the origin and the dimension (in grid <em>coordinates</em>).
108  * @throw Exception::OutOfMemory if the memory for the grid could not be allocated
109  */
110  TRegularData2D(const IndexType& size,
111  const CoordinateType& origin = CoordinateType(0.0),
112  const CoordinateType& dimension = CoordinateType(1.0));
113 
116  virtual ~TRegularData2D();
117 
121  virtual void clear();
123 
127 
134 
135 
139 
144  bool operator == (const TRegularData2D<ValueType>& data) const;
145 
147  BALL_INLINE bool operator != (const TRegularData2D<ValueType>& data) const { return !this->operator == (data); }
148 
150  BALL_INLINE bool empty() const { return data_.empty(); }
151 
153  bool isInside(const CoordinateType& x) const;
155 
159 
160  BALL_INLINE ConstIterator begin() const { return data_.begin(); }
162  BALL_INLINE ConstIterator end() const { return data_.end(); }
164  BALL_INLINE Iterator begin() { return data_.begin(); }
166  BALL_INLINE Iterator end() { return data_.end(); }
168 
172  // STL compatibility
173  BALL_INLINE size_type size() const { return data_.size(); }
174  BALL_INLINE size_type max_size() const { return data_.max_size(); }
175  BALL_INLINE void swap(TRegularData2D<ValueType>& data) { std::swap(*this, data); }
176 
177 
182  const ValueType& getData(const IndexType& index) const;
183 
188  ValueType& getData(const IndexType& index);
189 
194  const ValueType& getData(Position index) const;
195 
200  ValueType& getData(Position index);
201 
206  const ValueType& operator [] (const IndexType& index) const { return data_[index.x + size_.x * index.y]; }
207 
212  ValueType& operator [] (const IndexType& index) { return data_[index.x + size_.x * index.y]; }
213 
218  const ValueType& operator [] (Position index) const { return data_[index]; }
219 
224  ValueType& operator [] (Position index) { return data_[index]; }
225 
234  ValueType operator () (const CoordinateType& x) const;
235 
242  ValueType getInterpolatedValue(const CoordinateType& x) const;
243 
250  const ValueType& getClosestValue(const CoordinateType& x) const;
251 
258  ValueType& getClosestValue(const CoordinateType& x);
259 
263  IndexType getLowerIndex(const CoordinateType& v) const;
264 
270  IndexType getClosestIndex(const CoordinateType& v) const;
271 
277  inline const IndexType& getSize() const { return size_; }
278 
283  inline const CoordinateType& getOrigin() const { return origin_; }
284 
289  const CoordinateType& getSpacing() const { return spacing_; }
290 
293  void setOrigin(const CoordinateType& origin);
294 
300  const CoordinateType& getDimension() const { return dimension_; }
301 
307  void setDimension(const CoordinateType& dimension) { dimension_ = dimension; }
308 
321  void resize(const IndexType& new_size);
322 
332  void rescale(const IndexType& new_size);
333 
338  CoordinateType getCoordinates(const IndexType& index) const;
339 
345 
360  (const CoordinateType& r, Position& ll, Position& lr, Position& ul, Position& ur) const;
361 
366  void getEnclosingValues
367  (const CoordinateType& r, ValueType& ll, ValueType& lr, ValueType& ul, ValueType& ur) const;
368 
372  ValueType calculateMean() const;
373 
377  ValueType calculateSD() const;
378 
382  void binaryWrite(const String& filename) const;
383 
387  void binaryRead(const String& filename);
389 
390 
391  protected:
392 
395 
398 
401 
404 
406  IndexType size_;
407 
409  typedef struct { ValueType bt[1024]; } BlockValueType;
410  };
411 
415 
416  // default constructor.
417  template <class ValueType>
419  : data_(),
420  origin_(0.0),
421  dimension_(0.0),
422  spacing_(1.0),
423  size_(0)
424  {
425  }
426 
427  // copy constructor
428  template <class ValueType>
430  : data_(),
431  origin_(data.origin_),
432  dimension_(data.dimension_),
433  spacing_(data.spacing_),
434  size_(data.size_)
435  {
436  try
437  {
438  data_ = data.data_;
439  }
440  catch (std::bad_alloc&)
441  {
442  data_.resize(0);
443  throw Exception::OutOfMemory(__FILE__, __LINE__, data.data_.size() * sizeof(ValueType));
444  }
445  }
446 
447  template <class ValueType>
449  (const typename TRegularData2D<ValueType>::IndexType& size,
450  const typename TRegularData2D<ValueType>::CoordinateType& origin,
451  const typename TRegularData2D<ValueType>::CoordinateType& dimension)
452  : data_(),
453  origin_(origin),
454  dimension_(dimension),
455  spacing_(0.0, 0.0),
456  size_(size)
457  {
458  // Compute the grid spacing
459  spacing_.x = dimension_.x / (double)(size_.x - 1);
460  spacing_.y = dimension_.y / (double)(size_.y - 1);
461 
462  // Compute the number of grid points
463  size_type number_of_points = size_.x * size_.y;
464  try
465  {
466  data_.resize(number_of_points);
467  }
468  catch (std::bad_alloc&)
469  {
470  data_.resize(0);
471  throw Exception::OutOfMemory(__FILE__, __LINE__, number_of_points * sizeof(ValueType));
472  }
473  }
474 
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)
480  : data_(),
481  origin_(origin),
482  dimension_(dimension),
483  spacing_(spacing),
484  size_(0)
485  {
486  // Compute the grid size
487  size_.x = (Size)(dimension_.x / spacing_.x + 0.5) + 1;
488  size_.y = (Size)(dimension_.y / spacing_.y + 0.5) + 1;
489 
490  // Compute the number of grid points
491  size_type size = size_.x * size_.y;
492  try
493  {
494  data_ .resize(size);
495  }
496  catch (std::bad_alloc&)
497  {
498  data_.resize(0);
499  throw Exception::OutOfMemory(__FILE__, __LINE__, size * sizeof(ValueType));
500  }
501 
502  // Adjust the spacing -- dimension has precedence.
503  spacing_.x = dimension_.x / (double)(size_.x - 1);
504  spacing_.y = dimension_.y / (double)(size_.y - 1);
505  }
506 
507  template <class ValueType>
509  {
510  }
511 
512  // assignment operator
513  template <typename ValueType>
517  {
518  // Avoid self assignment
519  if (&rhs != this)
520  {
521  // Copy the coordinate-related attributes and
522  // the size.
523  origin_ = rhs.origin_;
524  dimension_ = rhs.dimension_;
525  spacing_ = rhs.spacing_;
526  size_ = rhs.size_;
527 
528  // Copy the data itself and rethrow allocation exceptions.
529  try
530  {
531  data_ = rhs.data_;
532  }
533  catch (std::bad_alloc&)
534  {
535  data_.resize(0);
536  throw Exception::OutOfMemory(__FILE__, __LINE__, rhs.data_.size() * sizeof(ValueType));
537  }
538  }
539 
540  return *this;
541  }
542 
543  template <typename ValueType>
545  {
546  // If the old size equals the new size, we're done.
547  if ((size.x == size_.x) && (size_.y == size.y))
548  {
549  return;
550  }
551 
552  // If the new grid is empty, this whole thing is quite easy.
553  if ((size.x == 0) || (size.y == 0))
554  {
555  data_.resize(0);
556  dimension_.set(0.0);
557  return;
558  }
559 
560  // Compute the new array size.
561  size_type new_size = (size_type)(size.x * size.y);
562 
563  // Catch any bad_allocs thrown by vector::resize
564  try
565  {
566  // Create a new temporary array.
567  TRegularData2D<ValueType> old_data(*this);
568 
569  // Resize the data to its new size.
570  data_.resize(new_size);
571  spacing_.x = dimension_.x / (double)(size.x - 1);
572  spacing_.y = dimension_.y / (double)(size.y - 1);
573 
574  // Walk over the new grid and copy the (interpolated) old stuff back.
575  CoordinateType v;
576  for (size_type i = 0; i < new_size; i++)
577  {
578  Position x = i % size.x;
579  Position y = i / size.x;
580  v.x = origin_.x + x * spacing_.x;
581  v.y = origin_.y + y * spacing_.y;
582  data_[i] = old_data(v);
583  }
584 
585  // Correct the grid dimension. Origin and spacing remain constant.
586  size_ = size;
587  }
588  catch (std::bad_alloc&)
589  {
590  throw Exception::OutOfMemory(__FILE__, __LINE__, new_size * (Size)sizeof(ValueType));
591  }
592  }
593 
594  template <typename ValueType>
596  {
597  // If the old size equals the new size, we're done.
598  if (size.x == size_.x && size_.y == size.y)
599  {
600  return;
601  }
602 
603  // If the new grid is empty, this whole thing is quite easy.
604  if ((size.x == 0) || (size.y == 0))
605  {
606  data_.resize(0);
607  dimension_.set(0.0, 0.0);
608  return;
609  }
610 
611  // Compute the new array size.
612  size_type new_size = (size_type)(size.x * size.y);
613 
614  // Catch any bad_allocs thrown by vector::resize
615  try
616  {
617  // Create a new temporary array.
618  std::vector<ValueType> old_data(data_);
619 
620  // Resize the data to its new size.
621  data_.resize(new_size);
622 
623  // walk over the new grid and copy the old stuff back.
624  static ValueType default_value = (ValueType)0;
625  for (size_type i = 0; i < new_size; i++)
626  {
627  size_type x = i % size.x;
628  size_type y = i / size.x;
629  if (x >= size_.x || y >= size_.y)
630  {
631  data_[i] = default_value;
632  }
633  else
634  {
635  data_[i] = old_data[x + y * size_.x];
636  }
637  }
638 
639  // Correct the grid dimension. Origin and spacing remain constant.
640  dimension_.x *= (double)size.x / (double)size_.x;
641  dimension_.y *= (double)size.y / (double)size_.y;
642  size_ = size;
643  }
644  catch (std::bad_alloc&)
645  {
646  throw Exception::OutOfMemory(__FILE__, __LINE__, new_size * (Size)sizeof(ValueType));
647  }
648  }
649 
650  template <class ValueType>
652  {
653  origin_ = origin;
654  }
655 
656  template <class ValueType>
659  {
660  return ((r.x >= origin_.x) && (r.x <= (origin_.x + dimension_.x))
661  && (r.y >= origin_.y) && (r.y <= (origin_.y + dimension_.y)));
662  }
663 
664  template <class ValueType>
665  BALL_INLINE
666  const ValueType& TRegularData2D<ValueType>::getData
667  (const typename TRegularData2D<ValueType>::IndexType& index) const
668  {
669  size_type pos = index.x + index.y * size_.x;
670  if (pos >= data_.size())
671  {
672  throw Exception::OutOfGrid(__FILE__, __LINE__);
673  }
674  return data_[pos];
675  }
676 
677  template <class ValueType>
678  BALL_INLINE
679  ValueType& TRegularData2D<ValueType>::getData(const typename TRegularData2D<ValueType>::IndexType& index)
680  {
681  size_type pos = index.x + index.y * size_.x;
682  if (pos >= data_.size())
683  {
684  throw Exception::OutOfGrid(__FILE__, __LINE__);
685  }
686  return data_[pos];
687  }
688 
689  template <class ValueType>
690  BALL_INLINE
691  const ValueType& TRegularData2D<ValueType>::getData(Position index) const
692  {
693  if (index >= data_.size())
694  {
695  throw Exception::OutOfGrid(__FILE__, __LINE__);
696  }
697  return data_[index];
698  }
699 
700  template <class ValueType>
701  BALL_INLINE
703  {
704  if (index >= data_.size())
705  {
706  throw Exception::OutOfGrid(__FILE__, __LINE__);
707  }
708  return data_[index];
709  }
710 
711  template <class ValueType>
712  BALL_INLINE
714  (const typename TRegularData2D<ValueType>::IndexType& index) const
715  {
716  if ((index.x >= size_.x) || (index.y >= size_.y))
717  {
718  throw Exception::OutOfGrid(__FILE__, __LINE__);
719  }
720 
721  CoordinateType r(origin_.x + index.x * spacing_.x,
722  origin_.y + index.y * spacing_.y);
723 
724  return r;
725  }
726 
727  template <class ValueType>
728  BALL_INLINE
729  typename TRegularData2D<ValueType>::CoordinateType
731  {
732  if (position >= data_.size())
733  {
734  throw Exception::OutOfGrid(__FILE__, __LINE__);
735  }
736 
737  Position x = (Position)(position % size_.x);
738  Position y = (Position)(position / size_.x);
739 
740  return CoordinateType(origin_.x + (double)x * spacing_.x,
741  origin_.y + (double)y * spacing_.y);
742  }
743 
744  template <typename ValueType>
748  Position& ll, Position& lr, Position& ul, Position& ur) const
749  {
750  if (!isInside(r))
751  {
752  throw Exception::OutOfGrid(__FILE__, __LINE__);
753  }
754 
755  // Calculate the grid indices of the lower left front corner
756  // of the enclosing rectangle
757  IndexType position;
758  position.x = (Position)((r.x - origin_.x) / spacing_.x);
759  position.y = (Position)((r.y - origin_.y) / spacing_.y);
760 
761  // Calculate the (linear) indices of the four rectangle corners
762  ll = position.x + size_.x * position.y;
763  lr = ll + 1;
764  ul = ll + size_.x;
765  ur = ul + 1;
766  }
767 
768  template <typename ValueType>
772  ValueType& ll, ValueType& lr, ValueType& ul, ValueType& ur) const
773  {
774  if (!isInside(r))
775  {
776  throw Exception::OutOfGrid(__FILE__, __LINE__);
777  }
778 
779  // compute the four grid indices forming the enclosing rectangle
780  Position ll_id, lr_id, ul_id, ur_id;
781  getEnclosingIndices(r, ll_id, lr_id, ul_id, ur_id);
782 
783  // Retrieve the grid values
784  ll = data_[ll_id];
785  lr = data_[lr_id];
786  ul = data_[ul_id];
787  ur = data_[ur_id];
788  }
789 
790  template <typename ValueType>
794  {
795  if (!isInside(r))
796  {
797  throw Exception::OutOfGrid(__FILE__, __LINE__);
798  }
799 
800  return this->operator () (r);
801  }
802 
803  template <typename ValueType>
807  {
808  CoordinateType h(r - origin_);
809  Position x = (Position)(h.x / spacing_.x);
810  Position y = (Position)(h.y / spacing_.y);
811 
812  // correct for numerical inaccuracies
813  if (x >= (size_.x - 1))
814  {
815  x = size_.x - 2;
816  }
817  if (y >= (size_.y - 1))
818  {
819  y = size_.y - 2;
820  }
821 
822  Size l = x + size_.x * y;
823  CoordinateType r_0(origin_.x + (double)x * spacing_.x,
824  origin_.y + (double)y * spacing_.y);
825 
826  double dx = 1.0 - ((r.x - r_0.x) / spacing_.x);
827  double dy = 1.0 - ((r.y - r_0.y) / spacing_.y);
828 
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);
833  }
834 
835  template <typename ValueType>
839  {
840  if (!isInside(r))
841  {
842  throw Exception::OutOfGrid(__FILE__, __LINE__);
843  }
844 
845  static IndexType position;
846  position.x = (Position)((r.x - origin_.x) / spacing_.x);
847  position.y = (Position)((r.y - origin_.y) / spacing_.y);
848 
849  return position;
850  }
851 
852  template <typename ValueType>
856  {
857  if (!isInside(r))
858  {
859  throw Exception::OutOfGrid(__FILE__, __LINE__);
860  }
861 
862  static IndexType position;
863  position.x = (Position)((r.x - origin_.x) / spacing_.x + 0.5);
864  position.y = (Position)((r.y - origin_.y) / spacing_.y + 0.5);
865 
866  return position;
867  }
868 
869  template <typename ValueType>
872  (const typename TRegularData2D<ValueType>::CoordinateType& r) const
873  {
874  if (!isInside(r))
875  {
876  throw Exception::OutOfGrid(__FILE__, __LINE__);
877  }
878 
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);
882 
883  return operator [] (position);
884  }
885 
886  template <typename ValueType>
889  (const typename TRegularData2D<ValueType>::CoordinateType& r)
890  {
891  if (!isInside(r))
892  {
893  throw Exception::OutOfGrid(__FILE__, __LINE__);
894  }
895 
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);
899 
900  return operator [] (position);
901  }
902 
903  template <typename ValueType>
906  {
907  Position data_points = (size_.x * size_.y);
908  ValueType mean = 0;
909  for (Position i = 0; i < data_points; i++)
910  {
911  mean += data_[i];
912  }
913  mean /= data_points;
914  return mean;
915  }
916 
917  template <typename ValueType>
920  {
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++)
925  {
926  stddev += (pow(data_(i)-mean,2));
927  }
928  stddev /= (data_points-1);
929  stddev = sqrt(stddev);
930  return stddev;
931  }
932 
933  template <typename ValueType>
935  {
936  data_.resize(0);
937 
938  origin_.set(0.0);
939  dimension_.set(0.0, 0.0);
940  size_.x = 0;
941  size_.y = 0;
942  spacing_.set(1.0, 1.0);
943  }
944 
945  template <typename ValueType>
947  {
948  return ((origin_ == data.origin_)
949  && (dimension_ == data.dimension_)
950  && (size_.x == data.size_.x)
951  && (size_.y == data.size_.y)
952  && (data_ == data.data_));
953  }
954 
955 
958 
959  template <typename ValueType>
960  std::ostream& operator << (std::ostream& os, const TRegularData2D<ValueType>& data)
961  {
962  // Write the grid origin, dimension, and number of grid points
963  os << data.getOrigin().x << " " << data.getOrigin().y
964  << std::endl
965  << data.getOrigin().x + data.getDimension().x << " "
966  << data.getOrigin().y + data.getDimension().y
967  << std::endl
968  << data.getSize().x - 1 << " " << data.getSize().y - 1
969  << std::endl;
970 
971  // Write the array contents.
972  std::copy(data.begin(), data.end(), std::ostream_iterator<ValueType>(os, "\n"));
973  return os;
974  }
975 
977  template <typename ValueType>
978  std::istream& operator >> (std::istream& is, TRegularData2D<ValueType>& grid)
979  {
983 
984  is >> origin.x >> origin.y;
985  is >> dimension.x >> dimension.y;
986  is >> size.x >> size.y;
987 
988  dimension -= origin;
989  size.x++;
990  size.y++;
991 
992  grid.resize(size);
993  grid.setOrigin(origin);
994  grid.setDimension(dimension);
995 
996  std::copy(std::istream_iterator<ValueType>(is),
997  std::istream_iterator<ValueType>(),
998  grid.begin());
999  // std::copy_n(std::istream_iterator<ValueType>(is), grid.size(), grid.begin());
1000 
1001  return is;
1002  }
1004 
1005  template <typename ValueType>
1007  {
1008  File outfile(filename.c_str(), std::ios::out|std::ios::binary);
1009  if (!outfile.isValid())
1010  throw Exception::FileNotFound(__FILE__, __LINE__, filename);
1011 
1012  // write all information we need to recreate the grid
1014  BinaryFileAdaptor<ValueType> adapt_single;
1015  BinaryFileAdaptor<float> adapt_float;
1016 
1017  BinaryFileAdaptor<Size> adapt_size;
1018 
1019  adapt_size.setData(data_.size());
1020  outfile << adapt_size;
1021 
1022  // NOTE: we do not use the binary file adaptor to write out the Vector2-variables here,
1023  // the reason is a bit stupid: the data layout of Vector2 has a three-byte "hole"
1024  // that is automatically padded by the compiler to ensure the correct alignment.
1025  // valgrind is very unhappy when we write these uninitialized values out, even
1026  // though they are entirely harmless. To prevent false-positives in the error checking,
1027  // we write out the members of the vectors instead. This even saves some bytes in the
1028  // output (not that it would matter...)
1029  adapt_float.setData(origin_.x);
1030  outfile << adapt_float;
1031  adapt_float.setData(origin_.y);
1032  outfile << adapt_float;
1033 
1034  adapt_float.setData(dimension_.x);
1035  outfile << adapt_float;
1036  adapt_float.setData(dimension_.y);
1037  outfile << adapt_float;
1038 
1039  adapt_float.setData(spacing_.x);
1040  outfile << adapt_float;
1041  adapt_float.setData(spacing_.y);
1042  outfile << adapt_float;
1043 
1044  BinaryFileAdaptor<IndexType> adapt_index;
1045  adapt_index.setData(size_);
1046  outfile << adapt_index;
1047 
1048  // we slide a window of BLOCK_SIZE over our data.
1049  const int BLOCK_SIZE = 1024;
1050  Index window_pos = 0;
1051 
1052  while (((int)data_.size() - (BLOCK_SIZE + window_pos)) >= 0)
1053  {
1054  adapt_block.setData(* (BlockValueType*)&(data_[window_pos]));
1055  outfile << adapt_block;
1056  window_pos += BLOCK_SIZE;
1057  }
1058 
1059  // Now we have to write the remaining data one by one.
1060  for (Size i = window_pos; i < data_.size(); i++)
1061  {
1062  adapt_single.setData(data_[i]);
1063  outfile << adapt_single;
1064  }
1065 
1066  // That's it.
1067  outfile.close();
1068  }
1069 
1070  template <typename ValueType>
1072  {
1073  File infile(filename, std::ios::in|std::ios::binary);
1074  if (!infile.isValid())
1075  {
1076  throw Exception::FileNotFound(__FILE__, __LINE__, filename);
1077  }
1078 
1080  BinaryFileAdaptor< ValueType > adapt_single;
1081 
1082  // read all information we need to recreate the grid
1083  BinaryFileAdaptor<Size> adapt_size;
1084  BinaryFileAdaptor<float> adapt_float;
1085 
1086  infile >> adapt_size;
1087  Size new_size = adapt_size.getData();
1088 
1089  infile >> adapt_float;
1090  origin_.x = adapt_float.getData();
1091  infile >> adapt_float;
1092  origin_.y = adapt_float.getData();
1093 
1094  infile >> adapt_float;
1095  dimension_.x = adapt_float.getData();
1096  infile >> adapt_float;
1097  dimension_.y = adapt_float.getData();
1098 
1099  infile >> adapt_float;
1100  spacing_.x = adapt_float.getData();
1101  infile >> adapt_float;
1102  spacing_.y = adapt_float.getData();
1103 
1104  BinaryFileAdaptor<IndexType> adapt_index;
1105  infile >> adapt_index;
1106  size_ = adapt_index.getData();
1107 
1108  data_.resize(new_size);
1109 
1110  // we slide a window of size 1024 over our data
1111  Index window_pos = 0;
1112 
1113  while ( ((int)data_.size() - (1024 + window_pos)) >= 0 )
1114  {
1115  infile >> adapt_block;
1116  *(BlockValueType*)(&(data_[window_pos])) = adapt_block.getData();
1117  /*
1118  for (Size i=0; i<1024; i++)
1119  {
1120  data_[i+window_pos] = adapt_block.getData().bt[i];
1121  }
1122  */
1123  window_pos+=1024;
1124  }
1125 
1126  // now we have to read the remaining data one by one
1127  for (Size i=window_pos; i<data_.size(); i++)
1128  {
1129  infile >> adapt_single;
1130  data_[i] = adapt_single.getData();
1131  }
1132 
1133  // that's it. I hope...
1134  infile.close();
1135  }
1136  } // namespace BALL
1137 
1138 #endif // BALL_DATATYPE_TREGULARDATA2D_H