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 *=
static_cast<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){
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;
CompIntType ComponentType
Integer type used by this vector.
Declaration of integer vector type traits.
TVectorNDBase()
Do not allow a direct creation of this class as object, as this container does not initialize itself.
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 checking for integer overflow.
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 that 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.
void set(const TVector &v)
Like assignment operator.
bool hasNegativeComp() const
Returns whether any component is negative.
TVector< TVectorBase > compMax(const TVector< TVectorBase > &b) const
Returns the component-wise maximum of components of this and b.
void fillEqualComps(const ComponentType threshold=0, const ComponentType fillVal=0)
Sets the components that are equal to threshold to fillVal.
void fillSmallerComps(const ComponentType threshold=0, const ComponentType fillVal=0)
Sets the components that are smaller than threshold to fillVal.
TVector< TVectorBase > getStrides(const ComponentType offset=1) const
Interprets the vector as image extension and returns a stride vector.
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 > compMin(const TVector< TVectorBase > &b) const
Returns the component-wise minimum of components of this and b.
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 that 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 checking for integer overflow.
TVector< TVectorBase > & operator=(const TVector< TVectorBase > &v2)
MLint getExtDimension() const
Returns the index to the highest vector component that is not 1.
TVectorBase ParentClass
Parent class of this class.
bool sanityCheckNumberOfVoxels() const
Returns whether the 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 + ...
TVector(const ComponentType i)
Constructor that initializes all components to i.
void copy(const MLint num, ComponentType *const arr) const
Copies the first num elements from internal array into arr, starting with array[0] to arr[0] to array...
MLint getMinIdx() const
Searches the minimum component and returns the index of its first occurrence.
TVector(const ComponentType xp, const TVector &p)
Constructor that assigns X to x and copies other elements from p.
ComponentType & operator[](const size_t i)
Indexing operator permits read and write access to array[0], ..., array[NumberOfDimensions-1] via ind...
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.
TVector< TVectorBase > compAbs() const
Returns a vector with all components of this replaced by their absolute values.
ComponentType getMin() const
Returns the minimum component value.
static ComponentType compSum(const TVector< TVectorBase > &a)
Static version: Returns the sum of all components without checking 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.
ComponentType compMul() const
Returns the product of all components. There is no check for integer overflow.
TVector< TVectorBase > compMod(const TVector< TVectorBase > &b) const
Returns the rest of the division of this by b component-wise.
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)
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
ScopeGuard< Functor > operator+(ScopeGuardOnExit, Functor &&fn)
FloatingPointVector< T, size, DataContainer > operator/(FloatingPointVector< T, size, DataContainer > lhs, MLdouble rhs)
Component-wise division of lhs by specialized rhs of type MLdouble.
FloatingPointVector< T, size, DataContainer > compMax(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component-wise maximum of buffer1 and buffer2.
FloatingPointVector< T, size, DataContainer > & operator/=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component-wise division of *this by scalar value.
FloatingPointVector< T, size, DataContainer > & operator-=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component-wise subtraction of buffer from *this.
int VectorDimIdxType
Signed integer type used as count and index type to traverse the array of the TVector.
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Returns true if yes.
T compMul(const FloatingPointVector< T, size, DataContainer > &vec)
Returns the product of all components.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Returns true if yes.
Tvec3< DT > operator^(const Tvec3< DT > &a, const Tvec3< DT2 > &b)
Returns a vector orthogonal to a and b.
FloatingPointVector< T, size, DataContainer > compMin(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component-wise minimum of buffer1 and buffer2.
FloatingPointVector< T, size, DataContainer > & operator+=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component-wise addition.
FloatingPointVector< T, size, DataContainer > operator-(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component-wise subtraction of rhs from lhs.
FloatingPointVector< T, size, DataContainer > compAbs(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec without negative sign.
FloatingPointVector< T, size, DataContainer > & operator*=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component-wise multiplication *this with specialized MLdouble scalar value.
Defines a template to get the minimum and maximum values for each basic integer type.