5 #ifndef BALL_DATATYPE_REGULARDATA1D_H 6 #define BALL_DATATYPE_REGULARDATA1D_H 12 #ifndef BALL_SYSTEM_FILE_H 16 #ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H 40 template <
typename ValueType>
89 TRegularData1D(const CoordinateType& origin, const CoordinateType& dimension, const CoordinateType& spacing);
99 TRegularData1D(const VectorType& data, const CoordinateType& origin = 0.0, const CoordinateType& dimension = 1.0);
105 virtual
void clear();
117 TRegularData1D& operator = (const TRegularData1D<ValueType>& data);
123 TRegularData1D& operator = (const VectorType& data);
130 bool operator == (const TRegularData1D& data) const;
360 template <
typename ValueType>
369 template <
typename ValueType>
374 template <
typename ValueType>
376 : origin_(data.origin_),
377 dimension_(data.dimension_),
378 spacing_(data.spacing_),
386 catch (std::bad_alloc&)
392 template <
typename ValueType>
398 dimension_(dimension),
410 catch (std::bad_alloc&)
416 template <
typename ValueType>
422 dimension_(dimension),
423 spacing_(dimension / ((
double)data.
size()-1)),
431 catch (std::bad_alloc&)
438 template <
class ValueType>
446 spacing_ = dimension_ / (
double)(size - 1);
452 catch (std::bad_alloc&)
459 template <
typename ValueType>
464 static ValueType default_value = ValueType();
465 std::fill(data_.begin(), data_.end(), default_value);
468 template <
typename ValueType>
479 catch (std::bad_alloc&)
488 template <
typename ValueType>
496 catch (std::bad_alloc&)
505 template <
typename ValueType>
508 return (origin_ == data.
origin_ 510 && data_ == data.
data_);
513 template <
class ValueType>
517 return ((r >= origin_) && (r <= (origin_ + dimension_)));
520 template <
typename ValueType>
524 if (index >= data_.size())
531 template <
typename ValueType>
535 if (index >= data_.size())
542 template <
typename ValueType>
547 if (!isInside(x) || (data_.size() < 2))
552 if (lower == data_.size() - 1)
555 lower = data_.size() - 2;
560 template <
typename ValueType>
563 ValueType& lower, ValueType& upper)
const 567 getEnclosingIndices(x, lower_index, upper_index);
568 lower = data_[lower_index];
569 upper = data_[upper_index];
572 template <
typename ValueType>
580 return operator () (x);
583 template <
typename ValueType>
588 if ((index >= data_.size()) || (data_.size() == 0))
596 template <
typename ValueType>
600 if ((x < origin_) || (x > (origin_ + dimension_)))
608 template <
typename ValueType>
612 if ((x < origin_) || (x > (origin_ + dimension_)))
620 template <
typename ValueType>
624 if ((x < origin_) || (x > (origin_ + dimension_)))
634 template <
typename ValueType>
638 if ((x < origin_) || (x > (origin_ + dimension_)))
648 template <
typename ValueType>
654 for (
IndexType i = 0; i < data_points; i++)
662 template <
typename ValueType>
667 ValueType stddev = 0;
668 ValueType mean = this->calculateMean();
669 for (
IndexType i = 0; i < data_points; i++)
671 stddev += (pow(data_[i]-mean,2));
673 stddev /= (data_points-1);
674 stddev = sqrt(stddev);
678 template <
typename ValueType>
683 if (left_index == data_.size() - 1)
686 return data_[data_.size() - 1];
690 double d = 1.0 - (((x - origin_) - (
double)left_index * spacing_) / spacing_);
691 return data_[left_index] * d + (1.0 - d) * data_[left_index + 1];
694 template <
typename ValueType>
699 if (data_.size() > 0)
701 dimension_ *= (
double)new_size / (
double)data_.size();
707 data_.resize(new_size);
709 catch (std::bad_alloc&)
717 template <
typename ValueType>
731 if (data_.size() == 0)
740 if ((data_.size() == 1) && (new_size > 1))
742 ValueType old_value = data_[0];
743 data_.resize(new_size);
746 data_[i] = old_value;
758 for (
Size i = 0; i < new_size; i++)
765 if (old_idx >= (data_.size() - 1))
767 old_idx = data_.size() - 2;
770 new_data[i] = data_[old_idx] * (1 - factor3) + factor3 * data_[old_idx + 1];
776 catch (std::bad_alloc&)
786 template <
typename ValueType>
788 std::ostream& operator << (std::ostream& os, const TRegularData1D<ValueType>& data)
793 << data.
getSize() - 1 << std::endl;
796 std::copy(data.
begin(), data.
end(), std::ostream_iterator<ValueType>(os,
"\n"));
801 template <
typename ValueType>
819 std::copy(std::istream_iterator<ValueType>(is),
820 std::istream_iterator<ValueType>(),
827 template <
typename ValueType>
830 File outfile(filename, std::ios::out|std::ios::binary);
843 adapt_size.
setData(data_.size());
844 outfile << adapt_size;
846 adapt_coordinate.
setData(origin_);
847 outfile << adapt_coordinate;
849 adapt_coordinate.
setData(dimension_);
850 outfile << adapt_coordinate;
852 adapt_coordinate.setData(spacing_);
853 outfile << adapt_coordinate;
856 Index window_pos = 0;
857 while (((
int)data_.size() - (1024 + window_pos)) >= 0 )
859 adapt_block.
setData(*(BlockValueType*)&(data_[window_pos]));
860 outfile << adapt_block;
865 for (
Size i = window_pos; i < data_.size(); i++)
867 adapt_single.
setData(data_[i]);
868 outfile << adapt_single;
875 template <
typename ValueType>
878 File infile(filename, std::ios::in|std::ios::binary);
888 infile >> adapt_size;
889 Size new_size = adapt_size.getData();
891 infile >> adapt_coordinate;
892 origin_ = adapt_coordinate.
getData();
894 infile >> adapt_coordinate;
895 dimension_ = adapt_coordinate.getData();
897 infile >> adapt_coordinate;
898 spacing_ = adapt_coordinate.getData();
900 data_.resize(new_size);
903 Index window_pos = 0;
905 while ( ((
int)data_.size() - (1024 + window_pos)) >= 0 )
907 infile >> adapt_block;
908 *(BlockValueType*)(&(data_[window_pos])) = adapt_block.
getData();
919 for (
Size i=window_pos; i<data_.size(); i++)
921 infile >> adapt_single;
922 data_[i] = adapt_single.
getData();
930 #endif // BALL_DATATYPE_REGULARDATA1D_H The block data type for reading and writing binary data.
IndexType getClosestIndex(const CoordinateType &x) const
BALL_INLINE void setOrigin(const CoordinateType &origin)
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::pointer pointer
TRegularData1D< float > RegularData1D
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::iterator Iterator
A mutable iterator.
BALL_INLINE IndexType getSize() const
Return the number of points in the data set.
BALL_INLINE void swap(TRegularData1D< ValueType > &data)
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::difference_type difference_type
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::size_type size_type
BALL_INLINE Iterator begin()
bool isInside(const CoordinateType &x) const
Test whether a point is inside the grid.
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::reference reference
CoordinateType spacing_
The spacing.
BALL_INLINE const CoordinateType & getDimension() const
void setData(const T &data)
CoordinateType origin_
The origin of the data set.
CoordinateType getCoordinates(const IndexType &index) const
void binaryWrite(const String &filename) const
virtual void clear()
Clear the contents.
ValueType getInterpolatedValue(const CoordinateType &x) const
ValueType operator()(const CoordinateType &x) const
const ValueType & getClosestValue(const CoordinateType &x) const
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::const_iterator ConstIterator
A constant iterator.
const T & getData() const
BALL_INLINE void setDimension(const CoordinateType &dimension)
BALL_INLINE const CoordinateType & getOrigin() const
void rescale(const IndexType &new_size)
void getEnclosingValues(const CoordinateType &x, ValueType &lower, ValueType &upper) const
BALL_INLINE ConstIterator end() const
BALL_INLINE const CoordinateType & getSpacing() const
TRegularData1D()
Default constructor.
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::const_reference const_reference
BALL_INLINE ConstIterator begin() const
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::iterator iterator
BALL_INLINE size_type size() const
void binaryRead(const String &filename)
std::vector< std::complex< ComplexTraits::ComplexPrecision > >::const_iterator const_iterator
const ValueType & getData(const IndexType &index) const
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
BALL_INLINE size_type max_size() const
VectorType data_
The data.
ValueType calculateSD() const
std::complex< ComplexTraits::ComplexPrecision > value_type
std::vector< std::complex< ComplexTraits::ComplexPrecision > > VectorType
The type containing an STL vector of the corresponding ValueType.
const ValueType & operator[](const IndexType &index) const
BALL_INLINE bool empty() const
Empty predicate.
BALL_EXPORT bool operator==(const String &s1, const String &s2)
void resize(const IndexType &size)
void getEnclosingIndices(const CoordinateType &x, Position &lower, Position &upper) const
BALL_INLINE Iterator end()
ValueType calculateMean() const
CoordinateType dimension_
The dimension (length)
bool operator==(const TRegularData1D &data) const
Equality operator.
IndexType getLowerIndex(const CoordinateType &x) const
#define BALL_CREATE(name)