Vector.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Vector
29 
30 Description
31  Templated 3D Vector derived from VectorSpace adding construction from
32  3 components, element access using x(), y() and z() member functions and
33  the inner-product (dot-product) and cross-product operators.
34 
35  A centre() member function which returns the Vector for which it is called
36  is defined so that point which is a typedef to Vector<scalar> behaves as
37  other shapes in the shape hierarchy.
38 
39 SourceFiles
40  VectorI.H
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef Foam_Vector_H
45 #define Foam_Vector_H
46 
47 #include "contiguous.H"
48 #include "VectorSpace.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 template<class T> class List;
57 
58 /*---------------------------------------------------------------------------*\
59  Class Vector Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class Cmpt>
63 class Vector
64 :
65  public VectorSpace<Vector<Cmpt>, Cmpt, 3>
66 {
67 public:
68 
69  // Typedefs
70 
71  //- Equivalent type of labels used for valid component indexing
72  typedef Vector<label> labelType;
73 
74 
75  // Member Constants
76 
77  //- Rank of Vector is 1
78  static constexpr direction rank = 1;
79 
80 
81  //- Component labeling enumeration
82  enum components { X, Y, Z };
83 
84 
85  // Generated Methods
86 
87  //- Default construct
88  Vector() = default;
89 
90  //- Copy construct
91  Vector(const Vector&) = default;
92 
93  //- Move construct
94  Vector(Vector&&) = default;
95 
96  //- Copy assignment
97  Vector& operator=(const Vector&) = default;
98 
99  //- Move assignment
100  Vector& operator=(Vector&&) = default;
101 
102 
103  // Constructors
104 
105  //- Construct initialized to zero
106  inline Vector(Foam::zero);
107 
108  //- Copy construct from VectorSpace of the same rank
109  template<class Cmpt2>
110  inline Vector(const VectorSpace<Vector<Cmpt2>, Cmpt2, 3>& vs);
111 
112  //- Construct from three components
113  inline Vector(const Cmpt& vx, const Cmpt& vy, const Cmpt& vz);
114 
115  //- Construct from Istream
116  inline explicit Vector(Istream& is);
117 
118 
119  // Member Functions
120 
121  // Component Access
122 
123  //- Access to the vector x component
124  const Cmpt& x() const noexcept { return this->v_[components::X]; }
125 
126  //- Access to the vector y component
127  const Cmpt& y() const noexcept { return this->v_[components::Y]; }
128 
129  //- Access to the vector z component
130  const Cmpt& z() const noexcept { return this->v_[components::Z]; }
131 
132  //- Access to the vector x component
133  Cmpt& x() noexcept { return this->v_[components::X]; }
134 
135  //- Access to the vector y component
136  Cmpt& y() noexcept { return this->v_[components::Y]; }
137 
138  //- Access to the vector z component
139  Cmpt& z() noexcept { return this->v_[components::Z]; }
140 
141 
142  // Vector Operations
143 
144  //- Return \c this (for point which is a typedef to Vector<scalar>)
145  inline const Vector<Cmpt>& centre
146  (
147  const Foam::UList<Vector<Cmpt>>& /* (unused) */
148  ) const noexcept;
149 
150  //- The length (L2-norm) of the vector
151  inline scalar mag() const;
152 
153  //- The length (L2-norm) squared of the vector.
154  inline scalar magSqr() const;
156  //- The L2-norm distance from another vector.
157  //- The mag() of the difference.
158  inline scalar dist(const Vector<Cmpt>& v2) const;
159 
160  //- The L2-norm distance squared from another vector.
161  //- The magSqr() of the difference.
162  inline scalar distSqr(const Vector<Cmpt>& v2) const;
163 
164  //- Inplace normalise the vector by its magnitude
165  // For small magnitudes (less than ROOTVSMALL) set to zero.
166  // Will not be particularly useful for a vector of labels
167  inline Vector<Cmpt>& normalise(const scalar tol = ROOTVSMALL);
168 
169  //- Inplace removal of components that are collinear to the given
170  //- unit vector.
171  inline Vector<Cmpt>& removeCollinear(const Vector<Cmpt>& unitVec);
172 
173  //- Scalar-product of \c this with another Vector.
174  inline Cmpt inner(const Vector<Cmpt>& v2) const;
175 
176  //- Cross-product of \c this with another Vector.
177  inline Vector<Cmpt> cross(const Vector<Cmpt>& v2) const;
178 
179 
180  // Comparison Operations
181 
182  //- Lexicographically compare \em a and \em b with order (x:y:z)
183  static inline bool less_xyz
184  (
185  const Vector<Cmpt>& a,
186  const Vector<Cmpt>& b
187  );
188 
189  //- Lexicographically compare \em a and \em b with order (y:z:x)
190  static inline bool less_yzx
191  (
192  const Vector<Cmpt>& a,
193  const Vector<Cmpt>& b
194  );
195 
196  //- Lexicographically compare \em a and \em b with order (z:x:y)
197  static inline bool less_zxy
198  (
199  const Vector<Cmpt>& a,
200  const Vector<Cmpt>& b
201  );
202 
203 
204  // Member Operators
205 
206  //- Inherit VectorSpace += operations
207  using Vector::vsType::operator+=;
208 
209  //- Inherit VectorSpace -= operations
210  using Vector::vsType::operator-=;
211 
212  //- Add compatible vector to this
213  template<class Cmpt2>
214  std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
215  inline operator+=(const Vector<Cmpt2>& b)
216  {
217  this->x() += b.x();
218  this->y() += b.y();
219  this->z() += b.z();
220  }
221 
222  //- Subtract compatible vector from this
223  template<class Cmpt2>
224  std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
225  inline operator-=(const Vector<Cmpt2>& b)
226  {
227  this->x() -= b.x();
228  this->y() -= b.y();
229  this->z() -= b.z();
230  }
231 };
232 
233 
234 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
235 
236 //- Data are contiguous if component type is contiguous
237 template<class Cmpt>
238 struct is_contiguous<Vector<Cmpt>> : is_contiguous<Cmpt> {};
239 
240 //- Data are contiguous label if component type is label
241 template<class Cmpt>
242 struct is_contiguous_label<Vector<Cmpt>> : is_contiguous_label<Cmpt> {};
243 
244 //- Data are contiguous scalar if component type is scalar
245 template<class Cmpt>
246 struct is_contiguous_scalar<Vector<Cmpt>> : is_contiguous_scalar<Cmpt> {};
247 
248 
249 template<class Cmpt>
250 class typeOfRank<Cmpt, 1>
251 {
252 public:
253 
254  typedef Vector<Cmpt> type;
255 };
256 
257 
258 template<class Cmpt>
259 class symmTypeOfRank<Cmpt, 1>
260 {
261 public:
262 
263  typedef Vector<Cmpt> type;
264 };
265 
266 
267 template<class Cmpt>
268 class typeOfSolve<Vector<Cmpt>>
269 {
270 public:
271 
272  typedef Vector<solveScalar> type;
273 };
274 
275 
276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 } // End namespace Foam
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 #include "VectorI.H"
283 
284 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285 
286 #endif
287 
288 // ************************************************************************* //
std::enable_if_t< std::is_convertible_v< Cmpt2, Cmpt >, void > operator-=(const Vector< Cmpt2 > &b)
Subtract compatible vector from this.
Definition: Vector.H:289
static bool less_yzx(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (y:z:x)
Definition: VectorI.H:197
uint8_t direction
Definition: direction.H:46
Vector< label > labelType
Equivalent type of labels used for valid component indexing.
Definition: Vector.H:69
scalar distSqr(const Vector< Cmpt > &v2) const
The L2-norm distance squared from another vector. The magSqr() of the difference. ...
Definition: VectorI.H:95
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Vector()=default
Default construct.
Templated vector space.
Definition: VectorSpace.H:57
Cmpt inner(const Vector< Cmpt > &v2) const
Scalar-product of this with another Vector.
Definition: VectorI.H:146
Definition: FixedList.H:920
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector.H:150
Vector< Cmpt > & normalise(const scalar tol=ROOTVSMALL)
Inplace normalise the vector by its magnitude.
Definition: VectorI.H:114
static constexpr direction rank
Rank of Vector is 1.
Definition: Vector.H:77
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:805
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
static bool less_zxy(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (z:x:y)
Definition: VectorI.H:221
scalar magSqr() const
The length (L2-norm) squared of the vector.
Definition: VectorI.H:76
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
Templated 3D Vector derived from VectorSpace adding construction from 3 components, element access using x(), y() and z() member functions and the inner-product (dot-product) and cross-product operators.
Definition: Vector.H:58
const direction noexcept
Definition: scalarImpl.H:265
const Cmpt & x() const noexcept
Access to the vector x component.
Definition: Vector.H:145
const Vector< Cmpt > & centre(const Foam::UList< Vector< Cmpt >> &) const noexcept
Return this (for point which is a typedef to Vector<scalar>)
Definition: VectorI.H:67
scalar mag() const
The length (L2-norm) of the vector.
Definition: VectorI.H:88
friend Ostream & operator(Ostream &, const VectorSpace< Vector< Cmpt >, Cmpt, Ncmpts > &)
static bool less_xyz(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (x:y:z)
Definition: VectorI.H:173
const Cmpt & z() const noexcept
Access to the vector z component.
Definition: Vector.H:155
PtrList< volScalarField > & Y
Vector & operator=(const Vector &)=default
Copy assignment.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
components
Component labeling enumeration.
Definition: Vector.H:83
Vector< Cmpt > cross(const Vector< Cmpt > &v2) const
Cross-product of this with another Vector.
Definition: VectorI.H:156
Vector< Cmpt > & removeCollinear(const Vector< Cmpt > &unitVec)
Inplace removal of components that are collinear to the given unit vector.
Definition: VectorI.H:136
scalar dist(const Vector< Cmpt > &v2) const
The L2-norm distance from another vector. The mag() of the difference.
Definition: VectorI.H:107
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:86
Namespace for OpenFOAM.