1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _math_Vector_HeaderFile
16 #define _math_Vector_HeaderFile
18 #include <math_SingleTab.hxx>
22 // resolve name collisions with X11 headers
29 //! This class implements the real vector abstract data type.
30 //! Vectors can have an arbitrary range which must be defined at
31 //! the declaration and cannot be changed after this declaration.
33 //! math_Vector V1(-3, 5); // a vector with range [-3..5]
36 //! Vector are copied through assignement :
38 //! math_Vector V2( 1, 9);
41 //! V1(1) = 2.0; // the vector V2 will not be modified.
44 //! The Exception RangeError is raised when trying to access outside
45 //! the range of a vector :
47 //! V1(11) = 0.0 // --> will raise RangeError;
50 //! The Exception DimensionError is raised when the dimensions of two
51 //! vectors are not compatible :
53 //! math_Vector V3(1, 2);
54 //! V3 = V1; // --> will raise DimensionError;
55 //! V1.Add(V3) // --> will raise DimensionError;
63 //! Contructs a non-initialized vector in the range [theLower..theUpper]
64 //! "theLower" and "theUpper" are the indexes of the lower and upper bounds of the constructed vector.
65 Standard_EXPORT math_Vector(const Standard_Integer theLower, const Standard_Integer theUpper);
67 //! Contructs a vector in the range [theLower..theUpper]
68 //! whose values are all initialized with the value "theInitialValue"
69 Standard_EXPORT math_Vector(const Standard_Integer theLower, const Standard_Integer theUpper, const Standard_Real theInitialValue);
71 //! Constructs a vector in the range [theLower..theUpper]
72 //! with the "c array" theTab.
73 Standard_EXPORT math_Vector(const Standard_Address theTab, const Standard_Integer theLower, const Standard_Integer theUpper);
75 //! Constructor for converting gp_XY to math_Vector
76 Standard_EXPORT math_Vector(const gp_XY& Other);
78 //! Constructor for converting gp_XYZ to math_Vector
79 Standard_EXPORT math_Vector(const gp_XYZ& Other);
81 //! Initialize all the elements of a vector with "theInitialValue".
82 Standard_EXPORT void Init(const Standard_Real theInitialValue);
84 //! Constructs a copy for initialization.
85 //! An exception is raised if the lengths of the vectors are different.
86 Standard_EXPORT math_Vector(const math_Vector& theOther);
88 //! Returns the length of a vector
89 inline Standard_Integer Length() const
91 return UpperIndex - LowerIndex +1;
94 //! Returns the value of the theLower index of a vector.
95 inline Standard_Integer Lower() const
100 //! Returns the value of the theUpper index of a vector.
101 inline Standard_Integer Upper() const
106 //! Returns the value or the square of the norm of this vector.
107 Standard_EXPORT Standard_Real Norm() const;
109 //! Returns the value of the square of the norm of a vector.
110 Standard_EXPORT Standard_Real Norm2() const;
112 //! Returns the value of the "Index" of the maximum element of a vector.
113 Standard_EXPORT Standard_Integer Max() const;
115 //! Returns the value of the "Index" of the minimum element of a vector.
116 Standard_EXPORT Standard_Integer Min() const;
118 //! Normalizes this vector (the norm of the result
119 //! is equal to 1.0) and assigns the result to this vector
121 //! Standard_NullValue if this vector is null (i.e. if its norm is
122 //! less than or equal to Standard_Real::RealEpsilon().
123 Standard_EXPORT void Normalize();
125 //! Normalizes this vector (the norm of the result
126 //! is equal to 1.0) and creates a new vector
128 //! Standard_NullValue if this vector is null (i.e. if its norm is
129 //! less than or equal to Standard_Real::RealEpsilon().
130 Standard_EXPORT Standard_NODISCARD math_Vector Normalized() const;
132 //! Inverts this vector and assigns the result to this vector.
133 Standard_EXPORT void Invert();
135 //! Inverts this vector and creates a new vector.
136 Standard_EXPORT math_Vector Inverse() const;
138 //! sets a vector from "theI1" to "theI2" to the vector "theV";
139 //! An exception is raised if "theI1" is less than "LowerIndex" or "theI2" is greater than "UpperIndex" or "theI1" is greater than "theI2".
140 //! An exception is raised if "theI2-theI1+1" is different from the "Length" of "theV".
141 Standard_EXPORT void Set(const Standard_Integer theI1, const Standard_Integer theI2, const math_Vector& theV);
143 //!Creates a new vector by inverting the values of this vector
144 //! between indexes "theI1" and "theI2".
145 //! If the values of this vector were (1., 2., 3., 4.,5., 6.),
146 //! by slicing it between indexes 2 and 5 the values
147 //! of the resulting vector are (1., 5., 4., 3., 2., 6.)
148 Standard_EXPORT math_Vector Slice(const Standard_Integer theI1, const Standard_Integer theI2) const;
150 //! returns the product of a vector and a real value.
151 Standard_EXPORT void Multiply(const Standard_Real theRight);
153 void operator *=(const Standard_Real theRight)
158 //! returns the product of a vector and a real value.
159 Standard_EXPORT Standard_NODISCARD math_Vector Multiplied(const Standard_Real theRight) const;
161 Standard_NODISCARD math_Vector operator*(const Standard_Real theRight) const
163 return Multiplied(theRight);
166 //! returns the product of a vector and a real value.
167 Standard_EXPORT Standard_NODISCARD math_Vector TMultiplied(const Standard_Real theRight) const;
169 friend inline math_Vector operator* (const Standard_Real theLeft, const math_Vector& theRight)
171 return theRight.Multiplied(theLeft);
174 //! divides a vector by the value "theRight".
175 //! An exception is raised if "theRight" = 0.
176 Standard_EXPORT void Divide(const Standard_Real theRight);
178 void operator /=(const Standard_Real theRight)
183 //! divides a vector by the value "theRight".
184 //! An exception is raised if "theRight" = 0.
185 Standard_EXPORT Standard_NODISCARD math_Vector Divided(const Standard_Real theRight) const;
187 Standard_NODISCARD math_Vector operator/(const Standard_Real theRight) const
189 return Divided(theRight);
192 //! adds the vector "theRight" to a vector.
193 //! An exception is raised if the vectors have not the same length.
195 //! In order to avoid time-consuming copying of vectors, it
196 //! is preferable to use operator += or the function Add whenever possible.
197 Standard_EXPORT void Add(const math_Vector& theRight);
199 void operator +=(const math_Vector& theRight)
204 //! adds the vector theRight to a vector.
205 //! An exception is raised if the vectors have not the same length.
206 //! An exception is raised if the lengths are not equal.
207 Standard_EXPORT Standard_NODISCARD math_Vector Added(const math_Vector& theRight) const;
209 Standard_NODISCARD math_Vector operator+(const math_Vector& theRight) const
211 return Added(theRight);
214 //! sets a vector to the product of the vector "theLeft"
215 //! with the matrix "theRight".
216 Standard_EXPORT void Multiply(const math_Vector& theLeft, const math_Matrix& theRight);
218 //!sets a vector to the product of the matrix "theLeft"
219 //! with the vector "theRight".
220 Standard_EXPORT void Multiply(const math_Matrix& theLeft, const math_Vector& theRight);
222 //! sets a vector to the product of the transpose
223 //! of the matrix "theTLeft" by the vector "theRight".
224 Standard_EXPORT void TMultiply(const math_Matrix& theTLeft, const math_Vector& theRight);
226 //! sets a vector to the product of the vector
227 //! "theLeft" by the transpose of the matrix "theTRight".
228 Standard_EXPORT void TMultiply(const math_Vector& theLeft, const math_Matrix& theTRight);
230 //! sets a vector to the sum of the vector "theLeft"
231 //! and the vector "theRight".
232 //! An exception is raised if the lengths are different.
233 Standard_EXPORT void Add(const math_Vector& theLeft, const math_Vector& theRight);
235 //! sets a vector to the Subtraction of the
236 //! vector theRight from the vector theLeft.
237 //! An exception is raised if the vectors have not the same length.
239 //! In order to avoid time-consuming copying of vectors, it
240 //! is preferable to use operator -= or the function
241 //! Subtract whenever possible.
242 Standard_EXPORT void Subtract(const math_Vector& theLeft,const math_Vector& theRight);
244 //! accesses (in read or write mode) the value of index "theNum" of a vector.
245 inline Standard_Real& Value(const Standard_Integer theNum) const
247 Standard_RangeError_Raise_if(theNum < LowerIndex || theNum > UpperIndex, " ");
248 return Array(theNum);
251 Standard_Real& operator()(const Standard_Integer theNum) const
253 return Value(theNum);
256 //! Initialises a vector by copying "theOther".
257 //! An exception is raised if the Lengths are differents.
258 Standard_EXPORT math_Vector& Initialized(const math_Vector& theOther);
260 math_Vector& operator=(const math_Vector& theOther)
262 return Initialized(theOther);
265 //! returns the inner product of 2 vectors.
266 //! An exception is raised if the lengths are not equal.
267 Standard_EXPORT Standard_NODISCARD Standard_Real Multiplied(const math_Vector& theRight) const;
268 Standard_NODISCARD Standard_Real operator*(const math_Vector& theRight) const
270 return Multiplied(theRight);
273 //! returns the product of a vector by a matrix.
274 Standard_EXPORT Standard_NODISCARD math_Vector Multiplied(const math_Matrix& theRight) const;
276 Standard_NODISCARD math_Vector operator*(const math_Matrix& theRight) const
278 return Multiplied(theRight);
281 //! returns the opposite of a vector.
282 Standard_EXPORT math_Vector Opposite();
284 math_Vector operator-()
289 //! returns the subtraction of "theRight" from "me".
290 //! An exception is raised if the vectors have not the same length.
291 Standard_EXPORT void Subtract(const math_Vector& theRight);
293 void operator-=(const math_Vector& theRight)
298 //! returns the subtraction of "theRight" from "me".
299 //! An exception is raised if the vectors have not the same length.
300 Standard_EXPORT Standard_NODISCARD math_Vector Subtracted(const math_Vector& theRight) const;
302 Standard_NODISCARD math_Vector operator-(const math_Vector& theRight) const
304 return Subtracted(theRight);
307 //! returns the multiplication of a real by a vector.
308 //! "me" = "theLeft" * "theRight"
309 Standard_EXPORT void Multiply(const Standard_Real theLeft,const math_Vector& theRight);
311 //! Prints information on the current state of the object.
312 //! Is used to redefine the operator <<.
313 Standard_EXPORT void Dump(Standard_OStream& theO) const;
315 friend inline Standard_OStream& operator<<(Standard_OStream& theO, const math_Vector& theVec)
321 friend class math_Matrix;
325 //! Is used internally to set the "theLower" value of the vector.
326 void SetLower(const Standard_Integer theLower);
330 Standard_Integer LowerIndex;
331 Standard_Integer UpperIndex;
332 math_SingleTab<Standard_Real> Array;