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  //- Copy assignment
94  Vector& operator=(const Vector&) = default;
95 
96 
97  // Constructors
98 
99  //- Construct initialized to zero
100  inline Vector(const Foam::zero);
101 
102  //- Copy construct from VectorSpace of the same rank
103  template<class Cmpt2>
104  inline Vector(const VectorSpace<Vector<Cmpt2>, Cmpt2, 3>& vs);
105 
106  //- Construct from three components
107  inline Vector(const Cmpt& vx, const Cmpt& vy, const Cmpt& vz);
108 
109  //- Construct from Istream
110  inline explicit Vector(Istream& is);
111 
112 
113  // Member Functions
114 
115  // Component Access
116 
117  //- Access to the vector x component
118  const Cmpt& x() const noexcept { return this->v_[X]; }
119 
120  //- Access to the vector y component
121  const Cmpt& y() const noexcept { return this->v_[Y]; }
122 
123  //- Access to the vector z component
124  const Cmpt& z() const noexcept { return this->v_[Z]; }
125 
126  //- Access to the vector x component
127  Cmpt& x() noexcept { return this->v_[X]; }
128 
129  //- Access to the vector y component
130  Cmpt& y() noexcept { return this->v_[Y]; }
131 
132  //- Access to the vector z component
133  Cmpt& z() noexcept { return this->v_[Z]; }
134 
136  // Vector Operations
137 
138  //- Return \c this (for point which is a typedef to Vector<scalar>)
139  inline const Vector<Cmpt>& centre
140  (
141  const Foam::UList<Vector<Cmpt>>& /* (unused) */
142  ) const noexcept;
143 
144  //- The length (L2-norm) of the vector
145  inline scalar mag() const;
146 
147  //- The length (L2-norm) squared of the vector.
148  inline scalar magSqr() const;
149 
150  //- The L2-norm distance from another vector.
151  //- The mag() of the difference.
152  inline scalar dist(const Vector<Cmpt>& v2) const;
153 
154  //- The L2-norm distance squared from another vector.
155  //- The magSqr() of the difference.
156  inline scalar distSqr(const Vector<Cmpt>& v2) const;
157 
158  //- Inplace normalise the vector by its magnitude
159  // For small magnitudes (less than ROOTVSMALL) set to zero.
160  // Will not be particularly useful for a vector of labels
161  inline Vector<Cmpt>& normalise(const scalar tol = ROOTVSMALL);
162 
163  //- Inplace removal of components that are collinear to the given
164  //- unit vector.
165  inline Vector<Cmpt>& removeCollinear(const Vector<Cmpt>& unitVec);
166 
167  //- Scalar-product of \c this with another Vector.
168  inline Cmpt inner(const Vector<Cmpt>& v2) const;
169 
170  //- Cross-product of \c this with another Vector.
171  inline Vector<Cmpt> cross(const Vector<Cmpt>& v2) const;
172 
173 
174  // Comparison Operations
175 
176  //- Lexicographically compare \em a and \em b with order (x:y:z)
177  static inline bool less_xyz
178  (
179  const Vector<Cmpt>& a,
180  const Vector<Cmpt>& b
181  );
182 
183  //- Lexicographically compare \em a and \em b with order (y:z:x)
184  static inline bool less_yzx
185  (
186  const Vector<Cmpt>& a,
187  const Vector<Cmpt>& b
188  );
189 
190  //- Lexicographically compare \em a and \em b with order (z:x:y)
191  static inline bool less_zxy
192  (
193  const Vector<Cmpt>& a,
194  const Vector<Cmpt>& b
195  );
196 };
197 
198 
199 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
200 
201 //- Data are contiguous if component type is contiguous
202 template<class Cmpt>
203 struct is_contiguous<Vector<Cmpt>> : is_contiguous<Cmpt> {};
204 
205 //- Data are contiguous label if component type is label
206 template<class Cmpt>
207 struct is_contiguous_label<Vector<Cmpt>> : is_contiguous_label<Cmpt> {};
208 
209 //- Data are contiguous scalar if component type is scalar
210 template<class Cmpt>
211 struct is_contiguous_scalar<Vector<Cmpt>> : is_contiguous_scalar<Cmpt> {};
212 
213 
214 template<class Cmpt>
215 class typeOfRank<Cmpt, 1>
216 {
217 public:
218 
219  typedef Vector<Cmpt> type;
220 };
221 
222 
223 template<class Cmpt>
224 class symmTypeOfRank<Cmpt, 1>
225 {
226 public:
227 
228  typedef Vector<Cmpt> type;
229 };
230 
231 
232 template<class Cmpt>
233 class typeOfSolve<Vector<Cmpt>>
234 {
235 public:
236 
237  typedef Vector<solveScalar> type;
238 };
239 
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 } // End namespace Foam
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 #include "VectorI.H"
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
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:52
Cmpt inner(const Vector< Cmpt > &v2) const
Scalar-product of this with another Vector.
Definition: VectorI.H:146
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector.H:140
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:799
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: Scalar.H:258
const Cmpt & x() const noexcept
Access to the vector x component.
Definition: Vector.H:135
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
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:145
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
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:81
Namespace for OpenFOAM.