5#ifndef BALL_DATATYPE_REGULARDATA3D_H
6#define BALL_DATATYPE_REGULARDATA3D_H
8#ifndef BALL_MATHS_VECTOR3_H
12#ifndef BALL_SYSTEM_FILE_H
16#ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
20#ifndef BALL_MATHS_COMMON_H
44 template <
typename ValueType>
76 typedef typename std::vector<ValueType>::iterator
Iterator;
84 typedef typename std::vector<ValueType>::iterator
iterator;
86 typedef typename std::vector<ValueType>::reference
reference;
88 typedef typename std::vector<ValueType>::pointer
pointer;
90 typedef typename std::vector<ValueType>::size_type
size_type;
121 (
const IndexType&
size,
480 result.
x *= (
size_.x - 1);
481 result.
y *= (
size_.y - 1);
482 result.
z *= (
size_.z - 1);
518 template <
class ValueType>
530 template <
class ValueType>
546 catch (std::bad_alloc&)
553 template <
class ValueType>
560 dimension_(dimension),
574 data_.resize(number_of_points);
576 catch (std::bad_alloc&)
579 throw Exception::OutOfMemory(__FILE__, __LINE__, number_of_points *
sizeof(ValueType));
584 template <
class ValueType>
591 dimension_(dimension),
607 catch (std::bad_alloc&)
610 throw Exception::OutOfMemory(__FILE__, __LINE__,
size *
sizeof(ValueType));
619 template <
class ValueType>
628 dimension_(x_axis+y_axis+z_axis),
631 is_orthogonal_(false)
634 spacing_.x = x_axis.getLength() / (new_size.x - 1.);
635 spacing_.y = y_axis.getLength() / (new_size.y - 1.);
636 spacing_.z = z_axis.getLength() / (new_size.z - 1.);
643 catch (std::bad_alloc&)
646 throw Exception::OutOfMemory(__FILE__, __LINE__,
size *
sizeof(ValueType));
665 double determinant = 1. / (a*(e*i - f*
h) - b*(d*i - f*g) +
c*(d*
h - e*g));
680 template <
class ValueType>
685 template <
typename ValueType>
708 catch (std::bad_alloc&)
718 template <
typename ValueType>
743 std::vector<ValueType> old_data(
data_);
746 data_.resize(new_size);
757 data_[i] = default_value;
774 catch (std::bad_alloc&)
781 template <
typename ValueType>
810 data_.resize(new_size);
831 catch (std::bad_alloc&)
838 template <
class ValueType>
857 return !( (ri.
x < 0) || (ri.
y < 0) || (ri.
z < 0)
862 template <
class ValueType>
867 size_type pos = index.
x + index.
y * size_.x + index.
z * size_.x * size_.y;
868 if (pos >= data_.size())
875 template <
class ValueType>
882 template <
typename ValueType>
887 size_type pos = index.
x + index.
y * size_.x + index.
z * size_.x * size_.y;
888 if (pos >= data_.size())
895 template <
class ValueType>
899 if (index >=
data_.size())
906 template <
class ValueType>
910 if (index >=
data_.size())
917 template <
class ValueType>
922 if ((index.
x >= size_.x) || (index.
y >= size_.y) || (index.
z >= size_.z))
929 CoordinateType r(origin_.x + index.
x * spacing_.x,
930 origin_.y + index.
y * spacing_.y,
931 origin_.z + index.
z * spacing_.z);
936 CoordinateType r(index.
x, index.
y, index.
z);
937 r = mapToCartesian_(r);
943 template <
class ValueType>
948 if (position >=
data_.size())
972 template <
typename ValueType>
1003 llf = position.
x +
size_.x * position.
y
1006 luf = llf +
size_.x;
1010 lub = llb +
size_.x;
1014 template <
typename ValueType>
1027 Position llf_idx, rlf_idx, luf_idx, ruf_idx, llb_idx, rlb_idx, lub_idx, rub_idx;
1028 getEnclosingIndices(r, llf_idx, rlf_idx, luf_idx, ruf_idx, llb_idx, rlb_idx, lub_idx, rub_idx);
1031 llf =
data_[llf_idx];
1032 rlf =
data_[rlf_idx];
1033 luf =
data_[luf_idx];
1034 ruf =
data_[ruf_idx];
1035 llb =
data_[llb_idx];
1036 rlb =
data_[rlb_idx];
1037 lub =
data_[lub_idx];
1038 rub =
data_[rub_idx];
1041 template <
typename ValueType>
1050 return operator () (r);
1053 template <
typename ValueType>
1071 while (x >= (
size_.x - 1)) x--;
1072 while (y >= (
size_.y - 1)) y--;
1073 while (z >= (
size_.z - 1)) z--;
1086 while (x >= (
size_.x - 1)) x--;
1087 while (y >= (
size_.y - 1)) y--;
1088 while (z >= (
size_.z - 1)) z--;
1093 r_0.
x = lower_pos.
x;
1094 r_0.
y = lower_pos.
y;
1095 r_0.
z = lower_pos.
z;
1106 return data_[l] * dx * dy * dz
1107 +
data_[l + 1] * (1. - dx) * dy * dz
1108 +
data_[l + Nx] * dx * (1. - dy) * dz
1109 +
data_[l + Nx + 1] * (1. - dx) * (1. - dy) * dz
1110 +
data_[l + Nxy] * dx * dy * (1. - dz)
1111 +
data_[l + Nxy + 1] * (1. - dx) * dy * (1. - dz)
1112 +
data_[l + Nxy + Nx] * dx * (1. - dy) * (1. - dz)
1113 +
data_[l + Nxy + Nx + 1] * (1. - dx) * (1. - dy) * (1. - dz);
1116 template <
typename ValueType>
1145 template <
typename ValueType>
1173 template <
typename ValueType>
1183 static IndexType position;
1187 position.x = (Position)((r.
x - origin_.x) / spacing_.x + 0.5);
1188 position.y = (Position)((r.
y - origin_.y) / spacing_.y + 0.5);
1189 position.z = (Position)((r.
z - origin_.z) / spacing_.z + 0.5);
1193 static Vector3 pos = mapInverse_(r);
1199 return operator [] (position);
1202 template <
typename ValueType>
1216 position.x = (
Position)((r.x - origin_.x) / spacing_.x + 0.5);
1217 position.y = (
Position)((r.y - origin_.y) / spacing_.y + 0.5);
1218 position.z = (
Position)((r.z - origin_.z) / spacing_.z + 0.5);
1222 static Vector3 pos = mapInverse_(r);
1229 return operator [] (position);
1232 template <
typename ValueType>
1238 for (
Position i = 0; i < data_points; i++)
1242 mean /= data_points;
1246 template <
typename ValueType>
1253 for (
Position i = 0; i < data_points; i++)
1255 stddev += (pow(
data_[i]-mean,2));
1257 stddev /= (data_points-1);
1258 stddev = sqrt(stddev);
1262 template <
typename ValueType>
1277 template <
typename ValueType>
1289 template <
typename ValueType>
1292 File outfile(filename, std::ios::out|std::ios::binary);
1305 outfile << adapt_size;
1308 outfile << adapt_coordinate;
1311 outfile << adapt_coordinate;
1314 outfile << adapt_coordinate;
1318 outfile << adapt_index;
1321 Index window_pos = 0;
1323 while ( ((
int)
data_.size() - (1024 + window_pos)) >= 0 )
1326 outfile << adapt_block;
1331 for (
Size i=window_pos; i<
data_.size(); i++)
1334 outfile << adapt_single;
1341 template <
typename ValueType>
1344 File infile(filename, std::ios::in|std::ios::binary);
1356 infile >> adapt_size;
1359 infile >> adapt_coordinate;
1362 infile >> adapt_coordinate;
1365 infile >> adapt_coordinate;
1369 infile >> adapt_index;
1372 data_.resize(new_size);
1375 Index window_pos = 0;
1377 while ( ((
int)
data_.size() - (1024 + window_pos)) >= 0 )
1379 infile >> adapt_block;
1391 for (
Size i=window_pos; i<
data_.size(); i++)
1393 infile >> adapt_single;
1394 data_[i] = adapt_single.getData();
1404 template <
typename ValueType>
1418 std::copy(grid.
begin(), grid.
end(), std::ostream_iterator<ValueType>(os,
"\n"));
1424 template <
typename ValueType>
1431 is >> origin.
x >> origin.
y >> origin.
z;
1432 is >> dimension.
x >> dimension.
y >> dimension.
z;
1433 is >> size.
x >> size.
y >> size.
z;
1435 dimension -= origin;
1443 std::copy(std::istream_iterator<ValueType>(is), std::istream_iterator<ValueType>(), grid.
begin());
const T & getData() const
void setData(const T &data)
The index type used to refer to a specific element in the grid (x-, y-, and z-index).
IndexType(Position p, Position q, Position r)
void getEnclosingValues(const CoordinateType &r, ValueType &llf, ValueType &rlf, ValueType &luf, ValueType &ruf, ValueType &llb, ValueType &rlb, ValueType &lub, ValueType &rub) const
BALL_INLINE ConstIterator begin() const
BALL_INLINE Iterator begin()
void setOrigin(const CoordinateType &origin)
std::vector< double > inverse_mapping_
std::vector< ValueType > VectorType
The type containing an STL vector of the appropriate type.
const IndexType & getSize() const
bool isInside(const CoordinateType &r) const
Test if a given point is inside the grid.
const CoordinateType mapToCartesian_(CoordinateType r) const
map indices to Cartesian coordinates
std::vector< ValueType >::const_reference const_reference
void binaryWriteRaw(const String &filename) const
std::vector< ValueType >::size_type size_type
TVector3< float > CoordinateType
The coordinate type.
std::vector< ValueType >::const_iterator const_iterator
BALL_INLINE bool operator!=(const TRegularData3D< ValueType > &grid) const
void rescale(const IndexType &new_size)
const ValueType & getClosestValue(const CoordinateType &x) const
BALL_INLINE bool empty() const
Empty predicate.
bool operator==(const TRegularData3D< ValueType > &grid) const
ValueType & getData(Position index)
ValueType calculateSD() const
TRegularData3D(const TRegularData3D< ValueType > &grid)
void resize(const IndexType &size)
std::vector< ValueType >::iterator iterator
const CoordinateType mapInverse_(CoordinateType r) const
map Cartesian coordinates to indices (note: this does not yet convert the double values to Index)
CoordinateType dimension_
ValueType calculateMean() const
TRegularData3D(const IndexType &size, const CoordinateType &origin=CoordinateType(0.0), const CoordinateType &dimension=CoordinateType(1.0))
TRegularData3D & operator=(const TRegularData3D< ValueType > &data)
BALL_INLINE Iterator end()
ValueType & getClosestValue(const CoordinateType &x)
void binaryRead(const String &filename)
const CoordinateType & getDimension() const
const vector< ValueType > & getData() const
Get the full data.
std::vector< ValueType >::const_iterator ConstIterator
A nonmutable iterator.
BALL_INLINE size_type max_size() const
void getEnclosingIndices(const CoordinateType &r, Position &llf, Position &rlf, Position &luf, Position &ruf, Position &llb, Position &rlb, Position &lub, Position &rub) const
IndexType getClosestIndex(const CoordinateType &v) const
IndexType getLowerIndex(const CoordinateType &v) const
CoordinateType getCoordinates(Position index) const
std::vector< ValueType >::iterator Iterator
A mutable iterator.
std::vector< double > mapping_
const ValueType & getData(Position index) const
std::vector< ValueType >::difference_type difference_type
BALL_INLINE ConstIterator end() const
virtual ~TRegularData3D()
BALL_INLINE bool isOrthogonal() const
State whether grid is orthogonal or not.
std::vector< ValueType >::reference reference
TRegularData3D(const CoordinateType &origin, const CoordinateType &dimension, const CoordinateType &spacing)
const CoordinateType & getOrigin() const
const ValueType & operator[](const IndexType &index) const
ValueType getInterpolatedValue(const CoordinateType &x) const
const ValueType & getData(const IndexType &index) const
CoordinateType getCoordinates(const IndexType &index) const
std::vector< ValueType >::pointer pointer
void binaryWrite(const String &filename) const
TRegularData3D(const CoordinateType &origin, const CoordinateType &x_axis, const CoordinateType &y_axis, const CoordinateType &z_axis, const IndexType &size)
const CoordinateType & getSpacing() const
BALL_INLINE void swap(TRegularData3D< ValueType > &grid)
ValueType & getData(const IndexType &index)
ValueType operator()(const CoordinateType &x) const
void setDimension(const CoordinateType &dimension)
BALL_INLINE size_type size() const
#define BALL_CREATE(name)
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
BALL_EXTERN_VARIABLE const double h
BALL_EXTERN_VARIABLE const double c
TRegularData3D< float > RegularData3D
TVector3< float > Vector3
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
The block data type for reading and writing binary data.