5 #ifndef BALL_DATATYPE_REGULARDATA1D_H
6 #define BALL_DATATYPE_REGULARDATA1D_H
12 #ifndef BALL_SYSTEM_FILE_H
16 #ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
39 template <
typename ValueType>
88 TRegularData1D(const CoordinateType& origin, const CoordinateType& dimension, const CoordinateType& spacing);
98 TRegularData1D(const VectorType& data, const CoordinateType& origin = 0.0, const CoordinateType& dimension = 1.0);
104 virtual
void clear();
116 TRegularData1D& operator = (const TRegularData1D<ValueType>& data);
122 TRegularData1D& operator = (const VectorType& data);
130 bool operator == (const TRegularData1D& data) const;
359 template <
typename ValueType>
368 template <
typename ValueType>
373 template <
typename ValueType>
375 : origin_(data.origin_),
376 dimension_(data.dimension_),
377 spacing_(data.spacing_),
385 catch (std::bad_alloc&)
391 template <
typename ValueType>
397 dimension_(dimension),
409 catch (std::bad_alloc&)
411 throw Exception::OutOfMemory(__FILE__, __LINE__, size *
sizeof(ValueType));
415 template <
typename ValueType>
417 (
const typename TRegularData1D<ValueType>::VectorType& data,
418 const typename TRegularData1D<ValueType>::CoordinateType& origin,
419 const typename TRegularData1D<ValueType>::CoordinateType& dimension)
421 dimension_(dimension),
422 spacing_(dimension / ((
double)data.size()-1)),
430 catch (std::bad_alloc&)
432 throw Exception::OutOfMemory(__FILE__, __LINE__, data.size() *
sizeof(ValueType));
437 template <
class ValueType>
439 (
const typename TRegularData1D<ValueType>::IndexType& size)
451 catch (std::bad_alloc&)
454 throw Exception::OutOfMemory(__FILE__, __LINE__, size *
sizeof(ValueType));
458 template <
typename ValueType>
463 static ValueType default_value = ValueType();
464 std::fill(data_.begin(), data_.end(), default_value);
467 template <
typename ValueType>
478 catch (std::bad_alloc&)
487 template <
typename ValueType>
495 catch (std::bad_alloc&)
504 template <
typename ValueType>
507 return (origin_ == data.
origin_
509 && data_ == data.
data_);
512 template <
class ValueType>
516 return ((r >= origin_) && (r <= (origin_ + dimension_)));
519 template <
typename ValueType>
523 if (index >= data_.size())
530 template <
typename ValueType>
534 if (index >= data_.size())
541 template <
typename ValueType>
546 if (!isInside(x) || (data_.size() < 2))
551 if (lower == data_.size() - 1)
554 lower = data_.size() - 2;
559 template <
typename ValueType>
562 ValueType& lower, ValueType& upper)
const
566 getEnclosingIndices(x, lower_index, upper_index);
567 lower = data_[lower_index];
568 upper = data_[upper_index];
571 template <
typename ValueType>
579 return operator () (x);
582 template <
typename ValueType>
587 if ((index >= data_.size()) || (data_.size() == 0))
595 template <
typename ValueType>
599 if ((x < origin_) || (x > (origin_ + dimension_)))
607 template <
typename ValueType>
611 if ((x < origin_) || (x > (origin_ + dimension_)))
619 template <
typename ValueType>
623 if ((x < origin_) || (x > (origin_ + dimension_)))
633 template <
typename ValueType>
637 if ((x < origin_) || (x > (origin_ + dimension_)))
647 template <
typename ValueType>
653 for (
IndexType i = 0; i < data_points; i++)
661 template <
typename ValueType>
666 ValueType stddev = 0;
667 ValueType mean = this->calculateMean();
668 for (
IndexType i = 0; i < data_points; i++)
670 stddev += (pow(data_[i]-mean,2));
672 stddev /= (data_points-1);
673 stddev = sqrt(stddev);
677 template <
typename ValueType>
682 if (left_index == data_.size() - 1)
685 return data_[data_.size() - 1];
689 double d = 1.0 - (((x - origin_) - (
double)left_index * spacing_) / spacing_);
690 return data_[left_index] * d + (1.0 - d) * data_[left_index + 1];
693 template <
typename ValueType>
698 if (data_.size() > 0)
700 dimension_ *= (
double)new_size / (
double)data_.size();
706 data_.resize(new_size);
708 catch (std::bad_alloc&)
716 template <
typename ValueType>
730 if (data_.size() == 0)
733 data_.resize(new_size);
739 if ((data_.size() == 1) && (new_size > 1))
741 ValueType old_value = data_[0];
742 data_.resize(new_size);
745 data_[i] = old_value;
757 for (
Size i = 0; i < new_size; i++)
764 if (old_idx >= (data_.size() - 1))
766 old_idx = data_.size() - 2;
769 new_data[i] = data_[old_idx] * (1 - factor3) + factor3 * data_[old_idx + 1];
775 catch (std::bad_alloc&)
786 template <
typename ValueType>
787 std::ostream& operator << (std::ostream& os, const TRegularData1D<ValueType>& data)
790 os << data.getOrigin() << std::endl
791 << data.getOrigin() + data.getDimension() << std::endl
792 << data.getSize() - 1 << std::endl;
795 std::copy(data.begin(), data.end(), std::ostream_iterator<ValueType>(os,
"\n"));
800 template <
typename ValueType>
818 std::copy(std::istream_iterator<ValueType>(is),
819 std::istream_iterator<ValueType>(),
826 template <
typename ValueType>
829 File outfile(filename, std::ios::out|std::ios::binary);
842 adapt_size.
setData(data_.size());
843 outfile << adapt_size;
845 adapt_coordinate.
setData(origin_);
846 outfile << adapt_coordinate;
848 adapt_coordinate.
setData(dimension_);
849 outfile << adapt_coordinate;
851 adapt_coordinate.setData(spacing_);
852 outfile << adapt_coordinate;
855 Index window_pos = 0;
856 while (((
int)data_.size() - (1024 + window_pos)) >= 0 )
859 outfile << adapt_block;
864 for (
Size i = window_pos; i < data_.size(); i++)
866 adapt_single.
setData(data_[i]);
867 outfile << adapt_single;
874 template <
typename ValueType>
877 File infile(filename, std::ios::in|std::ios::binary);
887 infile >> adapt_size;
888 Size new_size = adapt_size.getData();
890 infile >> adapt_coordinate;
891 origin_ = adapt_coordinate.
getData();
893 infile >> adapt_coordinate;
894 dimension_ = adapt_coordinate.getData();
896 infile >> adapt_coordinate;
897 spacing_ = adapt_coordinate.getData();
899 data_.resize(new_size);
902 Index window_pos = 0;
904 while ( ((
int)data_.size() - (1024 + window_pos)) >= 0 )
906 infile >> adapt_block;
918 for (
Size i=window_pos; i<data_.size(); i++)
920 infile >> adapt_single;
921 data_[i] = adapt_single.
getData();
929 #endif // BALL_DATATYPE_REGULARDATA1D_H