Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-22 09:14:02

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 
0069 public:
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
0072   //! vector.
0073   inline math_VectorBase(const Standard_Integer theLower, const Standard_Integer theUpper);
0074 
0075   //! Constructs a vector in the range [theLower..theUpper]
0076   //! whose values are all initialized with the value "theInitialValue"
0077   inline math_VectorBase(const Standard_Integer theLower,
0078                          const Standard_Integer theUpper,
0079                          const TheItemType      theInitialValue);
0080 
0081   //! Constructs a vector in the range [theLower..theUpper]
0082   //! whose values are all initialized with the value "theInitialValue"
0083   inline math_VectorBase(const TheItemType*     theTab,
0084                          const Standard_Integer theLower,
0085                          const Standard_Integer theUpper);
0086 
0087   //! Constructor for converting gp_XY to math_VectorBase
0088   inline math_VectorBase(const gp_XY& Other);
0089 
0090   //! Constructor for converting gp_XYZ to math_VectorBase
0091   inline math_VectorBase(const gp_XYZ& Other);
0092 
0093   //! Initialize all the elements of a vector with "theInitialValue".
0094   void Init(const TheItemType theInitialValue);
0095 
0096   //! Constructs a copy for initialization.
0097   //! An exception is raised if the lengths of the vectors are different.
0098   inline math_VectorBase(const math_VectorBase& theOther);
0099 
0100   //! Returns the length of a vector
0101   inline Standard_Integer Length() const { return Array.Length(); }
0102 
0103   //! Returns the lower index of the vector
0104   inline Standard_Integer Lower() const { return Array.Lower(); }
0105 
0106   //! Returns the upper index of the vector
0107   inline Standard_Integer Upper() const { return Array.Upper(); }
0108 
0109   //! Returns the value or the square  of the norm of this vector.
0110   inline Standard_Real Norm() const;
0111 
0112   //! Returns the value of the square of the norm of a vector.
0113   inline Standard_Real Norm2() const;
0114 
0115   //! Returns the index of the maximum element of a vector. (first found)
0116   inline Standard_Integer Max() const;
0117 
0118   //!  Returns the index of the  minimum element  of a vector. (first found)
0119   inline Standard_Integer Min() const;
0120 
0121   //! Normalizes this vector (the norm of the result
0122   //! is equal to 1.0) and assigns the result to this vector
0123   //! Exceptions
0124   //! Standard_NullValue if this vector is null (i.e. if its norm is
0125   //! less than or equal to Standard_Real::RealEpsilon().
0126   inline void Normalize();
0127 
0128   //! Normalizes this vector (the norm of the result
0129   //! is equal to 1.0) and creates a new vector
0130   //! Exceptions
0131   //! Standard_NullValue if this vector is null (i.e. if its norm is
0132   //! less than or equal to Standard_Real::RealEpsilon().
0133   Standard_NODISCARD inline math_VectorBase Normalized() const;
0134 
0135   //! Inverts this vector and assigns the result to this vector.
0136   inline void Invert();
0137 
0138   //! Inverts this vector and creates a new vector.
0139   inline math_VectorBase Inverse() const;
0140 
0141   //! sets a vector from "theI1" to "theI2" to the vector "theV";
0142   //! An exception is raised if "theI1" is less than "LowerIndex" or "theI2" is greater than
0143   //! "UpperIndex" or "theI1" is greater than "theI2". An exception is raised if "theI2-theI1+1" is
0144   //! different from the "Length" of "theV".
0145   inline void Set(const Standard_Integer theI1,
0146                   const Standard_Integer theI2,
0147                   const math_VectorBase& theV);
0148 
0149   //! Creates a new vector by inverting the values of this vector
0150   //!  between indexes "theI1" and "theI2".
0151   //!  If the values of this vector were (1., 2., 3., 4.,5., 6.),
0152   //!  by slicing it between indexes 2 and 5 the values
0153   //!  of the resulting vector are (1., 5., 4., 3., 2., 6.)
0154   inline math_VectorBase Slice(const Standard_Integer theI1, const Standard_Integer theI2) const;
0155 
0156   //! Updates current vector by multiplying each element on current value.
0157   inline void Multiply(const TheItemType theRight);
0158 
0159   void operator*=(const TheItemType theRight) { Multiply(theRight); }
0160 
0161   //! returns the product of a vector and a real value.
0162   Standard_NODISCARD inline math_VectorBase Multiplied(const TheItemType theRight) const;
0163 
0164   Standard_NODISCARD math_VectorBase operator*(const TheItemType theRight) const
0165   {
0166     return Multiplied(theRight);
0167   }
0168 
0169   //! returns the product of a vector and a real value.
0170   Standard_NODISCARD inline math_VectorBase TMultiplied(const TheItemType theRight) const;
0171 
0172   friend inline math_VectorBase operator*(const TheItemType      theLeft,
0173                                           const math_VectorBase& theRight)
0174   {
0175     return theRight.Multiplied(theLeft);
0176   }
0177 
0178   //! divides a vector by the value "theRight".
0179   //! An exception is raised if "theRight" = 0.
0180   inline void Divide(const TheItemType theRight);
0181 
0182   void operator/=(const TheItemType theRight) { Divide(theRight); }
0183 
0184   //! Returns new vector as dividing current vector with the value "theRight".
0185   //! An exception is raised if "theRight" = 0.
0186   Standard_NODISCARD inline math_VectorBase Divided(const TheItemType theRight) const;
0187 
0188   Standard_NODISCARD math_VectorBase operator/(const TheItemType theRight) const
0189   {
0190     return Divided(theRight);
0191   }
0192 
0193   //! adds the vector "theRight" to a vector.
0194   //! An exception is raised if the vectors have not the same length.
0195   //! Warning
0196   //! In order to avoid time-consuming copying of vectors, it
0197   //! is preferable to use operator += or the function Add whenever possible.
0198   inline void Add(const math_VectorBase& theRight);
0199 
0200   void operator+=(const math_VectorBase& theRight) { Add(theRight); }
0201 
0202   //! Returns new vector as adding current vector with the value "theRight".
0203   //! An exception is raised if the vectors do not have the same length.
0204   //! An exception is raised if the lengths are not equal.
0205   Standard_NODISCARD inline math_VectorBase Added(const math_VectorBase& theRight) const;
0206 
0207   Standard_NODISCARD math_VectorBase operator+(const math_VectorBase& theRight) const
0208   {
0209     return Added(theRight);
0210   }
0211 
0212   //! sets a vector to the product of the vector "theLeft"
0213   //! with the matrix "theRight".
0214   inline void Multiply(const math_VectorBase& theLeft, const math_Matrix& theRight);
0215 
0216   //! sets a vector to the product of the matrix "theLeft"
0217   //!  with the vector "theRight".
0218   inline void Multiply(const math_Matrix& theLeft, const math_VectorBase& theRight);
0219 
0220   //! sets a vector to the product of the transpose
0221   //! of the matrix "theTLeft" by the vector "theRight".
0222   inline void TMultiply(const math_Matrix& theTLeft, const math_VectorBase& theRight);
0223 
0224   //! sets a vector to the product of the vector
0225   //! "theLeft" by the transpose of the matrix "theTRight".
0226   inline void TMultiply(const math_VectorBase& theLeft, const math_Matrix& theTRight);
0227 
0228   //! sets a vector to the sum of the vector "theLeft"
0229   //! and the vector "theRight".
0230   //! An exception is raised if the lengths are different.
0231   inline void Add(const math_VectorBase& theLeft, const math_VectorBase& theRight);
0232 
0233   //! sets a vector to the Subtraction of the
0234   //! vector theRight from the vector theLeft.
0235   //! An exception is raised if the vectors have not the same length.
0236   //! Warning
0237   //! In order to avoid time-consuming copying of vectors, it
0238   //! is preferable to use operator -= or the function
0239   //! Subtract whenever possible.
0240   inline void Subtract(const math_VectorBase& theLeft, const math_VectorBase& theRight);
0241 
0242   //! accesses the value of index "theNum" of a vector.
0243   const TheItemType& Value(const Standard_Integer theNum) const { return Array(theNum); }
0244 
0245   //! accesses (in read or write mode) the value of index "theNum" of a vector.
0246   inline TheItemType& Value(const Standard_Integer theNum) { return Array(theNum); }
0247 
0248   const TheItemType& operator()(const Standard_Integer theNum) const { return Value(theNum); }
0249 
0250   TheItemType& operator()(const Standard_Integer theNum) { return Value(theNum); }
0251 
0252   //! Initialises a vector by copying "theOther".
0253   //! An exception is raised if the Lengths are different.
0254   inline math_VectorBase& Initialized(const math_VectorBase& theOther);
0255 
0256   math_VectorBase& operator=(const math_VectorBase& theOther) { return Initialized(theOther); }
0257 
0258   //! returns the inner product of 2 vectors.
0259   //! An exception is raised if the lengths are not equal.
0260   Standard_NODISCARD inline TheItemType Multiplied(const math_VectorBase& theRight) const;
0261 
0262   Standard_NODISCARD inline TheItemType operator*(const math_VectorBase& theRight) const
0263   {
0264     return Multiplied(theRight);
0265   }
0266 
0267   //! returns the product of a vector by a matrix.
0268   Standard_NODISCARD inline math_VectorBase Multiplied(const math_Matrix& theRight) const;
0269 
0270   Standard_NODISCARD math_VectorBase operator*(const math_Matrix& theRight) const
0271   {
0272     return Multiplied(theRight);
0273   }
0274 
0275   //! returns the opposite of a vector.
0276   inline math_VectorBase Opposite();
0277 
0278   math_VectorBase operator-() { return Opposite(); }
0279 
0280   //! returns the subtraction of "theRight" from "me".
0281   //! An exception is raised if the vectors have not the same length.
0282   inline void Subtract(const math_VectorBase& theRight);
0283 
0284   void operator-=(const math_VectorBase& theRight) { Subtract(theRight); }
0285 
0286   //! returns the subtraction of "theRight" from "me".
0287   //! An exception is raised if the vectors have not the same length.
0288   Standard_NODISCARD inline math_VectorBase Subtracted(const math_VectorBase& theRight) const;
0289 
0290   Standard_NODISCARD math_VectorBase operator-(const math_VectorBase& theRight) const
0291   {
0292     return Subtracted(theRight);
0293   }
0294 
0295   //! returns the multiplication of a real by a vector.
0296   //! "me" = "theLeft" * "theRight"
0297   inline void Multiply(const TheItemType theLeft, const math_VectorBase& theRight);
0298 
0299   //! Prints information on the current state of the object.
0300   //! Is used to redefine the operator <<.
0301   inline void Dump(Standard_OStream& theO) const;
0302 
0303   friend inline Standard_OStream& operator<<(Standard_OStream& theO, const math_VectorBase& theVec)
0304   {
0305     theVec.Dump(theO);
0306     return theO;
0307   }
0308 
0309   friend class math_Matrix;
0310 
0311 protected:
0312   //! Is used internally to set the "theLower" value of the vector.
0313   inline void SetLower(const Standard_Integer theLower);
0314 
0315 private:
0316   std::array<TheItemType, THE_BUFFER_SIZE> myBuffer;
0317   NCollection_Array1<TheItemType>          Array;
0318 };
0319 
0320 #include <math_VectorBase.lxx>
0321 
0322 #endif