Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:15

0001 // Copyright (c) 1997-1999 Matra Datavision
0002 // Copyright (c) 1999-2023 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _math_VectorBase_HeaderFile
0016 #define _math_VectorBase_HeaderFile
0017 
0018 #include <NCollection_Array1.hxx>
0019 #include <gp_XY.hxx>
0020 #include <gp_XYZ.hxx>
0021 
0022 // resolve name collisions with X11 headers
0023 #ifdef Opposite
0024 #undef Opposite
0025 #endif
0026 
0027 #include <math_Matrix.hxx>
0028 
0029 #include <array>
0030 
0031 //! This class implements the real vector abstract data type.
0032 //! Vectors can have an arbitrary range which must be defined at
0033 //! the declaration and cannot be changed after this declaration.
0034 //! @code
0035 //!    math_VectorBase<TheItemType> V1(-3, 5); // a vector with range [-3..5]
0036 //! @endcode
0037 //!
0038 //! Vector are copied through assignment:
0039 //! @code
0040 //!    math_VectorBase<TheItemType> V2( 1, 9);
0041 //!    ....
0042 //!    V2 = V1;
0043 //!    V1(1) = 2.0; // the vector V2 will not be modified.
0044 //! @endcode
0045 //!
0046 //! The Exception RangeError is raised when trying to access outside
0047 //! the range of a vector :
0048 //! @code
0049 //!    V1(11) = 0.0 // --> will raise RangeError;
0050 //! @endcode
0051 //!
0052 //! The Exception DimensionError is raised when the dimensions of two
0053 //! vectors are not compatible :
0054 //! @code
0055 //!    math_VectorBase<TheItemType> V3(1, 2);
0056 //!    V3 = V1;    // --> will raise DimensionError;
0057 //!    V1.Add(V3)  // --> will raise DimensionError;
0058 //! @endcode
0059 template<typename TheItemType>
0060 class math_VectorBase
0061 {
0062   static const int THE_BUFFER_SIZE = 32;
0063 
0064 public:
0065   //! Memory allocation
0066   DEFINE_STANDARD_ALLOC;
0067   DEFINE_NCOLLECTION_ALLOC;
0068 public:
0069 
0070   //! Constructs a non-initialized vector in the range [theLower..theUpper]
0071   //! "theLower" and "theUpper" are the indexes of the lower and upper bounds of the constructed vector.
0072   inline math_VectorBase(const Standard_Integer theLower, const Standard_Integer theUpper);
0073 
0074   //! Constructs a vector in the range [theLower..theUpper]
0075   //! whose values are all initialized with the value "theInitialValue"
0076   inline math_VectorBase(const Standard_Integer theLower, const Standard_Integer theUpper, const TheItemType theInitialValue);
0077 
0078   //! Constructs a vector in the range [theLower..theUpper]
0079   //! whose values are all initialized with the value "theInitialValue"
0080   inline math_VectorBase(const TheItemType* theTab, const Standard_Integer theLower, const Standard_Integer theUpper);
0081 
0082   //! Constructor for converting gp_XY to math_VectorBase
0083   inline math_VectorBase(const gp_XY& Other);
0084 
0085   //! Constructor for converting gp_XYZ to math_VectorBase
0086   inline math_VectorBase(const gp_XYZ& Other);
0087 
0088   //! Initialize all the elements of a vector with "theInitialValue".
0089   void Init(const TheItemType theInitialValue);
0090 
0091   //! Constructs a copy for initialization.
0092   //! An exception is raised if the lengths of the vectors are different.
0093   inline math_VectorBase(const math_VectorBase& theOther);
0094 
0095   //! Returns the length of a vector
0096   inline Standard_Integer Length() const
0097   {
0098     return Array.Length();
0099   }
0100 
0101   //! Returns the lower index of the vector
0102   inline Standard_Integer Lower() const
0103   {
0104     return Array.Lower();
0105   }
0106 
0107   //! Returns the upper index of the vector
0108   inline Standard_Integer Upper() const
0109   {
0110     return Array.Upper();
0111   }
0112 
0113   //! Returns the value or the square  of the norm of this vector.
0114   inline Standard_Real Norm() const;
0115 
0116   //! Returns the value of the square of the norm of a vector.
0117   inline Standard_Real Norm2() const;
0118 
0119   //! Returns the index of the maximum element of a vector. (first found)
0120   inline Standard_Integer Max() const;
0121 
0122   //!  Returns the index of the  minimum element  of a vector. (first found)
0123   inline Standard_Integer Min() const;
0124 
0125   //! Normalizes this vector (the norm of the result
0126   //! is equal to 1.0) and assigns the result to this vector
0127   //! Exceptions
0128   //! Standard_NullValue if this vector is null (i.e. if its norm is
0129   //! less than or equal to Standard_Real::RealEpsilon().
0130   inline void Normalize();
0131 
0132   //! Normalizes this vector (the norm of the result
0133   //! is equal to 1.0) and creates a new vector
0134   //! Exceptions
0135   //! Standard_NullValue if this vector is null (i.e. if its norm is
0136   //! less than or equal to Standard_Real::RealEpsilon().
0137   Standard_NODISCARD inline math_VectorBase Normalized() const;
0138 
0139   //! Inverts this vector and assigns the result to this vector.
0140   inline void Invert();
0141 
0142   //! Inverts this vector and creates a new vector.
0143   inline math_VectorBase Inverse() const;
0144 
0145   //! sets a vector from "theI1" to "theI2" to the vector "theV";
0146   //! An exception is raised if "theI1" is less than "LowerIndex" or "theI2" is greater than "UpperIndex" or "theI1" is greater than "theI2".
0147   //! An exception is raised if "theI2-theI1+1" is different from the "Length" of "theV".
0148   inline void Set(const Standard_Integer theI1, const Standard_Integer theI2, const math_VectorBase& theV);
0149 
0150   //!Creates a new vector by inverting the values of this vector
0151   //! between indexes "theI1" and "theI2".
0152   //! If the values of this vector were (1., 2., 3., 4.,5., 6.),
0153   //! by slicing it between indexes 2 and 5 the values
0154   //! of the resulting vector are (1., 5., 4., 3., 2., 6.)
0155   inline math_VectorBase Slice(const Standard_Integer theI1, const Standard_Integer theI2) const;
0156 
0157   //! Updates current vector by multiplying each element on current value.
0158   inline void Multiply(const TheItemType theRight);
0159 
0160   void operator *=(const TheItemType theRight)
0161   {
0162     Multiply(theRight);
0163   }
0164 
0165   //! returns the product of a vector and a real value.
0166   Standard_NODISCARD inline math_VectorBase Multiplied(const TheItemType theRight) const;
0167 
0168   Standard_NODISCARD math_VectorBase operator*(const TheItemType theRight) const
0169   {
0170     return Multiplied(theRight);
0171   }
0172 
0173   //! returns the product of a vector and a real value.
0174   Standard_NODISCARD inline math_VectorBase TMultiplied(const TheItemType theRight) const;
0175 
0176   friend inline math_VectorBase operator* (const TheItemType theLeft, const math_VectorBase& theRight)
0177   {
0178     return theRight.Multiplied(theLeft);
0179   }
0180 
0181   //! divides a vector by the value "theRight".
0182   //! An exception is raised if "theRight" = 0.
0183   inline void Divide(const TheItemType theRight);
0184 
0185   void operator /=(const TheItemType theRight)
0186   {
0187     Divide(theRight);
0188   }
0189 
0190   //! Returns new vector as dividing current vector with the value "theRight".
0191   //! An exception is raised if "theRight" = 0.
0192   Standard_NODISCARD inline math_VectorBase Divided(const TheItemType theRight) const;
0193 
0194   Standard_NODISCARD math_VectorBase operator/(const TheItemType theRight) const
0195   {
0196     return Divided(theRight);
0197   }
0198 
0199   //! adds the vector "theRight" to a vector.
0200   //! An exception is raised if the vectors have not the same length.
0201   //! Warning
0202   //! In order to avoid time-consuming copying of vectors, it
0203   //! is preferable to use operator += or the function Add whenever possible.
0204   inline void Add(const math_VectorBase& theRight);
0205 
0206   void operator +=(const math_VectorBase& theRight)
0207   {
0208     Add(theRight);
0209   }
0210 
0211   //! Returns new vector as adding curent vector with the value "theRight".
0212   //! An exception is raised if the vectors have not the same length.
0213   //! An exception is raised if the lengths are not equal.
0214   Standard_NODISCARD inline math_VectorBase Added(const math_VectorBase& theRight) const;
0215 
0216   Standard_NODISCARD math_VectorBase operator+(const math_VectorBase& theRight) const
0217   {
0218     return Added(theRight);
0219   }
0220 
0221   //! sets a vector to the product of the vector "theLeft"
0222   //! with the matrix "theRight".
0223   inline void Multiply(const math_VectorBase& theLeft, const math_Matrix& theRight);
0224 
0225   //!sets a vector to the product of the matrix "theLeft"
0226   //! with the vector "theRight".
0227   inline void Multiply(const math_Matrix& theLeft, const math_VectorBase& theRight);
0228 
0229   //! sets a vector to the product of the transpose
0230   //! of the matrix "theTLeft" by the vector "theRight".
0231   inline void TMultiply(const math_Matrix& theTLeft, const math_VectorBase& theRight);
0232 
0233   //! sets a vector to the product of the vector
0234   //! "theLeft" by the transpose of the matrix "theTRight".
0235   inline void TMultiply(const math_VectorBase& theLeft, const math_Matrix& theTRight);
0236 
0237   //! sets a vector to the sum of the vector "theLeft"
0238   //! and the vector "theRight".
0239   //! An exception is raised if the lengths are different.
0240   inline void Add(const math_VectorBase& theLeft, const math_VectorBase& theRight);
0241 
0242   //! sets a vector to the Subtraction of the
0243   //! vector theRight from the vector theLeft.
0244   //! An exception is raised if the vectors have not the same length.
0245   //! Warning
0246   //! In order to avoid time-consuming copying of vectors, it
0247   //! is preferable to use operator -= or the function
0248   //! Subtract whenever possible.
0249   inline void Subtract(const math_VectorBase& theLeft, const math_VectorBase& theRight);
0250 
0251   //! accesses the value of index "theNum" of a vector.
0252   const TheItemType& Value(const Standard_Integer theNum) const
0253   {
0254     return Array(theNum);
0255   }
0256 
0257   //! accesses (in read or write mode) the value of index "theNum" of a vector.
0258   inline TheItemType& Value(const Standard_Integer theNum)
0259   {
0260     return Array(theNum);
0261   }
0262 
0263   const TheItemType& operator()(const Standard_Integer theNum) const
0264   {
0265     return Value(theNum);
0266   }
0267 
0268   TheItemType& operator()(const Standard_Integer theNum)
0269   {
0270     return Value(theNum);
0271   }
0272 
0273   //! Initialises a vector by copying "theOther".
0274   //! An exception is raised if the Lengths are different.
0275   inline math_VectorBase& Initialized(const math_VectorBase& theOther);
0276 
0277   math_VectorBase& operator=(const math_VectorBase& theOther)
0278   {
0279     return Initialized(theOther);
0280   }
0281 
0282   //! returns the inner product of 2 vectors.
0283   //! An exception is raised if the lengths are not equal.
0284   Standard_NODISCARD inline TheItemType Multiplied(const math_VectorBase& theRight) const;
0285   Standard_NODISCARD inline TheItemType operator*(const math_VectorBase& theRight) const
0286   {
0287     return Multiplied(theRight);
0288   }
0289 
0290   //! returns the product of a vector by a matrix.
0291   Standard_NODISCARD inline math_VectorBase Multiplied(const math_Matrix& theRight) const;
0292 
0293   Standard_NODISCARD math_VectorBase operator*(const math_Matrix& theRight) const
0294   {
0295     return Multiplied(theRight);
0296   }
0297 
0298   //! returns the opposite of a vector.
0299   inline math_VectorBase Opposite();
0300 
0301   math_VectorBase operator-()
0302   {
0303     return Opposite();
0304   }
0305 
0306   //! returns the subtraction of "theRight" from "me".
0307   //! An exception is raised if the vectors have not the same length.
0308   inline void Subtract(const math_VectorBase& theRight);
0309 
0310   void operator-=(const math_VectorBase& theRight)
0311   {
0312     Subtract(theRight);
0313   }
0314 
0315   //! returns the subtraction of "theRight" from "me".
0316   //! An exception is raised if the vectors have not the same length.
0317   Standard_NODISCARD inline math_VectorBase Subtracted(const math_VectorBase& theRight) const;
0318 
0319   Standard_NODISCARD math_VectorBase operator-(const math_VectorBase& theRight) const
0320   {
0321     return Subtracted(theRight);
0322   }
0323 
0324   //! returns the multiplication of a real by a vector.
0325   //! "me" = "theLeft" * "theRight"
0326   inline void Multiply(const TheItemType theLeft, const math_VectorBase& theRight);
0327 
0328   //! Prints information on the current state of the object.
0329   //! Is used to redefine the operator <<.
0330   inline void Dump(Standard_OStream& theO) const;
0331 
0332   friend inline Standard_OStream& operator<<(Standard_OStream& theO, const math_VectorBase& theVec)
0333   {
0334     theVec.Dump(theO);
0335     return theO;
0336   }
0337 
0338   friend class math_Matrix;
0339 
0340 protected:
0341 
0342   //! Is used internally to set the "theLower" value of the vector.
0343   inline void SetLower(const Standard_Integer theLower);
0344 
0345 private:
0346   std::array<TheItemType, THE_BUFFER_SIZE> myBuffer;
0347   NCollection_Array1<TheItemType> Array;
0348 };
0349 
0350 #include <math_VectorBase.lxx>
0351 
0352 #endif