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>
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&)
412 throw Exception::OutOfMemory(__FILE__, __LINE__,
size *
sizeof(ValueType));
416 template <
typename ValueType>
422 dimension_(dimension),
423 spacing_(dimension / ((
double)data.size()-1)),
431 catch (std::bad_alloc&)
433 throw Exception::OutOfMemory(__FILE__, __LINE__, data.size() *
sizeof(ValueType));
438 template <
class ValueType>
452 catch (std::bad_alloc&)
455 throw Exception::OutOfMemory(__FILE__, __LINE__,
size *
sizeof(ValueType));
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)
734 data_.resize(new_size);
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&)
787 template <
typename ValueType>
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 )
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);
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 )
919 for (
Size i=window_pos; i<data_.size(); i++)
922 data_[i] = adapt_single.
getData();
#define BALL_CREATE(name)
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
TRegularData1D< float > RegularData1D
CoordinateType dimension_
The dimension (length)
const ValueType & getClosestValue(const CoordinateType &x) const
const ValueType & operator[](const IndexType &index) const
BALL_INLINE size_type max_size() const
ValueType getInterpolatedValue(const CoordinateType &x) const
BALL_INLINE const CoordinateType & getDimension() const
BALL_INLINE size_type size() const
void rescale(const IndexType &new_size)
IndexType getLowerIndex(const CoordinateType &x) const
CoordinateType origin_
The origin of the data set.
std::vector< ValueType > VectorType
The type containing an STL vector of the corresponding ValueType.
virtual void clear()
Clear the contents.
std::vector< ValueType >::pointer pointer
IndexType getClosestIndex(const CoordinateType &x) const
void getEnclosingValues(const CoordinateType &x, ValueType &lower, ValueType &upper) const
BALL_INLINE const CoordinateType & getOrigin() const
BALL_INLINE void setDimension(const CoordinateType &dimension)
std::vector< ValueType >::iterator iterator
void resize(const IndexType &size)
bool operator==(const TRegularData1D &data) const
Equality operator.
const ValueType & getData(const IndexType &index) const
BALL_INLINE void setOrigin(const CoordinateType &origin)
BALL_INLINE IndexType getSize() const
Return the number of points in the data set.
std::vector< ValueType >::iterator Iterator
A mutable iterator.
double CoordinateType
The coordinate type.
ValueType calculateSD() const
std::vector< ValueType >::difference_type difference_type
std::vector< ValueType >::size_type size_type
std::vector< ValueType >::const_iterator const_iterator
BALL_INLINE void swap(TRegularData1D< ValueType > &data)
ValueType calculateMean() const
virtual ~TRegularData1D()
Destructor.
TRegularData1D()
Default constructor.
Position IndexType
The IndexType.
BALL_INLINE Iterator end()
TRegularData1D & operator=(const TRegularData1D< ValueType > &data)
VectorType data_
The data.
CoordinateType getCoordinates(const IndexType &index) const
std::vector< ValueType >::reference reference
ValueType operator()(const CoordinateType &x) const
BALL_INLINE ConstIterator begin() const
ValueType & getClosestValue(const CoordinateType &x)
BALL_INLINE bool empty() const
Empty predicate.
BALL_INLINE Iterator begin()
CoordinateType spacing_
The spacing.
std::vector< ValueType >::const_iterator ConstIterator
A constant iterator.
void binaryWrite(const String &filename) const
void binaryRead(const String &filename)
ValueType & getData(const IndexType &index)
std::vector< ValueType >::const_reference const_reference
bool isInside(const CoordinateType &x) const
Test whether a point is inside the grid.
void getEnclosingIndices(const CoordinateType &x, Position &lower, Position &upper) const
BALL_INLINE const CoordinateType & getSpacing() const
BALL_INLINE ConstIterator end() const
The block data type for reading and writing binary data.
const T & getData() const
void setData(const T &data)