13#ifndef ML_INTEGER_VECTOR_H
14#define ML_INTEGER_VECTOR_H
38 template <
typename CompIntType, VectorDimIdxType NumDim>
47 enum { NumberOfDimensions = NumDim };
110 template<
class TVectorBase>
122 enum { NumberOfDimensions = ParentClass::NumberOfDimensions };
132 memset(this->array, 0,
static_cast<size_t>(NumberOfDimensions)*
sizeof(
ComponentType));
154 this->array[d] = v2.array[d];
161 template <
typename TVectorBase2>
170 for (; d < numDims; ++d)
174 for (; d < NumberOfDimensions; ++d)
184 for (
VectorDimIdxType d=1; d < NumberOfDimensions; ++d){ this->array[d] = p.array[d]; }
201 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d]=v; }
207 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] = v.array[d]; }
225 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] += b.array[d]; }
236 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] -= b.array[d]; }
247 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] *= b.array[d]; }
254 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ retVec.array[d] += b; }
261 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ retVec.array[d] -= b; }
268 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ retVec.array[d] *= b; }
281 this->array[d] /= b.array[d];
290 for (
VectorDimIdxType d=0; retFlag && (d < NumberOfDimensions); ++d){ retFlag = retFlag && (this->array[d] == b.array[d]); }
297 return !(*
this == b);
304 for (
VectorDimIdxType d=0; retFlag && (d < NumberOfDimensions); ++d){ retFlag = retFlag && (this->array[d] < b.array[d]); }
311 for (
VectorDimIdxType d=0; retFlag && (d < NumberOfDimensions); ++d){ retFlag = retFlag && (this->array[d] <= b.array[d]); }
318 for (
VectorDimIdxType d=0; retFlag && (d < NumberOfDimensions); ++d){ retFlag = retFlag && (this->array[d] > b.array[d]); }
325 for (
VectorDimIdxType d=0; retFlag && (d < NumberOfDimensions); ++d){ retFlag = retFlag && (this->array[d] >= b.array[d]); }
348 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] &= b.array[d]; }
354 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] |= b.array[d]; }
360 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] ^= b.array[d]; }
383 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] &= b; }
389 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] |= b; }
395 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] ^= b; }
413 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] >>= b; }
419 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ this->array[d] <<= b; }
427 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){ retVec.array[d] = -retVec.array[d]; }
449 if (this->array[d] >= val){ idx=d; val=this->array[d]; }
457 return this->array[getMaxIdx()];
466 if (this->array[d] <= val){ idx=d; val=this->array[d]; }
474 return this->array[getMinIdx()];
498 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){
if (this->array[d]<0){
return true; } }
505 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){
if (this->array[d]<=0){
return false; } }
512 double product = 1.0;
515 product *= ((double) this->array[d]);
567 retVal *= a.array[d];
577 retVal += a.array[d];
589 retVal += a.array[d]*b.array[d];
599 if (this->array[d] < threshold)
601 this->array[d] = fillVal;
609 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){
if (this->array[d] > threshold){ this->array[d] = fillVal; } }
615 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){
if (this->array[d] == threshold){ this->array[d] = fillVal; } }
622 std::string
print(
const std::string &openStr=
"",
623 const std::string &sepStr=
" ",
624 const std::string &termStr=
"",
625 const MLint16 numDigits=-1)
const;
639 template <
class TVectorBase>
640 ostream&
operator<<(ostream& s,
const ML_NAMESPACE::TVector<TVectorBase> &v){
642 for (ML_NAMESPACE::VectorDimIdxType d=0; d < ML_NAMESPACE::TVector<TVectorBase>::NumberOfDimensions-1; ++d){ s << v.array[d] <<
","; }
643 s << v.array[ML_NAMESPACE::TVector<TVectorBase>::NumberOfDimensions-1];
661 template <
class TVectorBase>
672 template <
class TVectorBase>
676 for (
VectorDimIdxType d=0; d < NumberOfDimensions; ++d){
if (num>d){ arr[d] = this->array[d]; } }
684 template <
class TVectorBase>
688 stride.array[0] = offset;
689 for (
VectorDimIdxType d=1; d < NumberOfDimensions; ++d){ stride.array[d] = stride.array[d-1] * this->array[d-1]; }
698 template <
class TVectorBase>
704 vectorPos.array[d] = offsetPos / this->array[d];
705 offsetPos = offsetPos % this->array[d];
708 vectorPos.array[0] = offsetPos / this->array[0];
714 template <
class TVectorBase>
719 if (1 != this->array[d]){
729 template <
class TVectorBase>
742 template <
class TVectorBase>
747 if (a.array[d] < c.array[d]){ c.array[d] = a.array[d]; }
753 template <
class TVectorBase>
758 if (a.array[d] > c.array[d]){ c.array[d] = a.array[d]; }
766 template <
class TVectorBase>
772 c.array[d] = a.array[d] % b.array[d];
778 template <
class TVectorBase>
783 if (c.array[d]<0){ c.array[d] = -c.array[d]; }
791 template <
class TVectorBase>
793 const std::string &sepStr,
794 const std::string &termStr,
797 std::string result = openStr;
807 snprintf(frmt, 99,
"%%%dlld", numDigits);
810 snprintf(frmt, 99,
"%%lld");
814 snprintf(buf, 511, frmt, this->array[d]);
815 result += buf + sepStr;
817 snprintf(buf, 511, frmt, this->array[NumberOfDimensions-1]);
818 result += buf + termStr;
Declaration of integer vector type traits:
TVectorNDBase()
Do not allow a direct creation of this class as object, because this container does not initialize it...
CompIntType ComponentType
Integer type used by this vector.
ML integer image vector class to be specialized for different purposes.
ComponentType getMax() const
Returns the maximum component value.
void set(const ComponentType v=0)
Sets all components to v or - if v is not specified - to 0.
static TVector< TVectorBase > compAbs(const TVector< TVectorBase > &a)
Static version: Returns a vector with all components of a replaced by their absolute values.
static ComponentType compMul(const TVector< TVectorBase > &a)
Static version: Returns the product of all components without check for integer overflow.
TVector< TVectorBase > compMin(const TVector< TVectorBase > &b) const
Returns the component wise minimum of components of this and b.
static TVector< TVectorBase > divCeil(const TVector< TVectorBase > &a, const TVector< TVectorBase > &b)
Static version: Returns the component wise division of the vector a by vector b with rounding up.
bool allBiggerZero() const
Returns whether all components > 0.
MLint getMaxIdx() const
Searches the maximum component and returns the index of its first occurrence.
void fillGreaterComps(const ComponentType threshold=0, const ComponentType fillVal=0)
Sets the components which are greater than threshold to fillVal.
static TVector< TVectorBase > compMax(const TVector< TVectorBase > &a, const TVector< TVectorBase > &b)
Static version: Returns the component wise maximum of vectors a and b.
TVector< TVectorBase > compMax(const TVector< TVectorBase > &b) const
Returns the component wise maximum of components of this and b.
void set(const TVector &v)
Like assignment operator.
bool hasNegativeComp() const
Returns whether any component is negative.
void fillEqualComps(const ComponentType threshold=0, const ComponentType fillVal=0)
Sets the components which are equal to threshold to fillVal.
void fillSmallerComps(const ComponentType threshold=0, const ComponentType fillVal=0)
Sets the components which are smaller than threshold to fillVal.
TVector< TVectorBase > & operator=(const TVector< TVectorBase > &v2)
TVector< TVectorBase > getStrides(const ComponentType offset=1) const
Interprets the vector as image extension and returns a stride vector.
ComponentType dot(const TVector< TVectorBase > &b) const
Returns the scalar product of vectors this and b. There is no check for integer overflow.
TVector(const MLint num, const ComponentType *const arr, const ComponentType deflt)
Constructor which initializes the vector components by copying them from arr[0] ,....
static ComponentType dot(const TVector< TVectorBase > &a, const TVector< TVectorBase > &b)
Static version: Returns the scalar product of vectors a and b without check for integer overflow.
TVector< TVectorBase > compAbs() const
Returns a vector with all components of this replaced by their absolute values.
MLint getExtDimension() const
Returns the index to the highest vector component which is not 1 which is useful to get the real dime...
TVectorBase ParentClass
Parent class of this class.
bool sanityCheckNumberOfVoxels() const
Returns whether product of all components is in range of integer type.
TVector(const TVector< TVectorBase > &v2)
std::string print(const std::string &openStr="", const std::string &sepStr=" ", const std::string &termStr="", const MLint16 numDigits=-1) const
Returns the string openStr + v.array[0] + sepStr + ... + v.array[NumberOfDimensions-2] + sepStr + v....
TVector(const ComponentType i)
Constructor which initializes all components to i.
void copy(const MLint num, ComponentType *const arr) const
Copy the first num elements from internal array into arr starting with array[0] to arr[0] to array[nu...
MLint getMinIdx() const
Searches the minimum component and returns the index of its first occurrence.
TVector(const ComponentType xp, const TVector &p)
Constructor which assigns X to x and copies other elements from p.
static TVector< TVectorBase > compMod(const TVector< TVectorBase > &a, const TVector< TVectorBase > &b)
Static version: Returns the rest dividing vector a by b component wise.
ComponentType operator[](const size_t i) const
Constant indexing operator permits read access to array[0], ..., array[NumberOfDimensions-1] via inde...
TVector(const TVector< TVectorBase2 > &v2)
Copy constructor from another TVector of same or other integer type, values are cast.
TVectorBase::ComponentType ComponentType
Integer type used by this vector.
ComponentType compSum() const
Returns the sum of all components. There is no check for integer overflow.
ComponentType getMin() const
Returns the minimum component value.
static ComponentType compSum(const TVector< TVectorBase > &a)
Static version: Returns the sum of all components without check for integer overflow.
static TVector< TVectorBase > compMin(const TVector< TVectorBase > &a, const TVector< TVectorBase > &b)
Static version: Returns the component wise minimum of vectors a and b.
TVector< TVectorBase > divCeil(const TVector< TVectorBase > &b) const
Returns the component wise division of the vector this by vector b with rounding up.
TVector< TVectorBase > compMod(const TVector< TVectorBase > &b) const
Returns the rest of the division of this by b component wise.
ComponentType compMul() const
Returns the product of all components. There is no check for integer overflow.
ComponentType & operator[](const size_t i)
Indexing operator permits read and write access to array[0], ..., array[NumberOfDimensions-1] via ind...
TVector()
Constructor. All components are initialized to 0.
TVector< TVectorBase > getVectorPosition(ComponentType offsetPos) const
Interprets the vector as a stride vector of an image and returns the offset position offsetPos from t...
#define ML_CHECK_THROW(x)
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
int VectorDimIdxType
Signed integer type used as count and index type to traverse the array of the TVector.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
Define a template to get the min and max values for each basic integer type.