Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Vector
29
30Description
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
39SourceFiles
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
52namespace Foam
53{
54
55// Forward Declarations
56template<class T> class List;
57
58/*---------------------------------------------------------------------------*\
59 Class Vector Declaration
60\*---------------------------------------------------------------------------*/
61
62template<class Cmpt>
63class Vector
64:
65 public VectorSpace<Vector<Cmpt>, Cmpt, 3>
66{
67public:
68
69 // Typedefs
70
71 //- Equivalent type of labels used for valid component indexing
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 };
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
97 // Constructors
98
99 //- Construct initialized to zero
100 inline Vector(Foam::zero);
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_[components::X]; }
119
120 //- Access to the vector y component
121 const Cmpt& y() const noexcept { return this->v_[components::Y]; }
122
123 //- Access to the vector z component
124 const Cmpt& z() const noexcept { return this->v_[components::Z]; }
126 //- Access to the vector x component
127 Cmpt& x() noexcept { return this->v_[components::X]; }
128
129 //- Access to the vector y component
130 Cmpt& y() noexcept { return this->v_[components::Y]; }
131
132 //- Access to the vector z component
133 Cmpt& z() noexcept { return this->v_[components::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
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,
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 // Member Operators
199
200 //- Inherit VectorSpace += operations
202
203 //- Inherit VectorSpace -= operations
205
206 //- Add compatible vector to this
207 template<class Cmpt2>
208 std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
209 inline operator+=(const Vector<Cmpt2>& b)
210 {
211 this->x() += b.x();
212 this->y() += b.y();
213 this->z() += b.z();
214 }
215
216 //- Subtract compatible vector from this
217 template<class Cmpt2>
218 std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
219 inline operator-=(const Vector<Cmpt2>& b)
220 {
221 this->x() -= b.x();
222 this->y() -= b.y();
223 this->z() -= b.z();
224 }
226
227
228// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
229
230//- Data are contiguous if component type is contiguous
231template<class Cmpt>
232struct is_contiguous<Vector<Cmpt>> : is_contiguous<Cmpt> {};
233
234//- Data are contiguous label if component type is label
235template<class Cmpt>
236struct is_contiguous_label<Vector<Cmpt>> : is_contiguous_label<Cmpt> {};
237
238//- Data are contiguous scalar if component type is scalar
239template<class Cmpt>
240struct is_contiguous_scalar<Vector<Cmpt>> : is_contiguous_scalar<Cmpt> {};
241
242
243template<class Cmpt>
244class typeOfRank<Cmpt, 1>
245{
246public:
247
248 typedef Vector<Cmpt> type;
249};
250
251
252template<class Cmpt>
253class symmTypeOfRank<Cmpt, 1>
254{
255public:
256
257 typedef Vector<Cmpt> type;
258};
259
260
261template<class Cmpt>
262class typeOfSolve<Vector<Cmpt>>
263{
264public:
265
266 typedef Vector<solveScalar> type;
268
269
270// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271
272} // End namespace Foam
273
274// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275
276#include "VectorI.H"
277
278// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280#endif
281
282// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
VectorSpace< Form, Cmpt, Ncmpts > vsType
VectorSpace type.
Definition VectorSpace.H:86
Cmpt v_[Ncmpts]
The components of this vector space.
Definition VectorSpace.H:81
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
Templated 3D Vector derived from VectorSpace adding construction from 3 components,...
Definition Vector.H:61
static bool less_xyz(const Vector< vector > &a, const Vector< vector > &b)
Cmpt & y() noexcept
Access to the vector y component.
Definition Vector.H:155
const Cmpt & x() const noexcept
Access to the vector x component.
Definition Vector.H:135
Vector< label > labelType
Definition Vector.H:69
Vector & operator=(const Vector &)=default
Copy assignment.
scalar dist(const Vector< vector > &v2) const
components
Component labeling enumeration.
Definition Vector.H:83
Cmpt & z() noexcept
Access to the vector z component.
Definition Vector.H:160
const Cmpt & z() const noexcept
Access to the vector z component.
Definition Vector.H:145
vector inner(const Vector< vector > &v2) const
std::enable_if_t< std::is_convertible_v< Cmpt2, Cmpt >, void > operator-=(const Vector< Cmpt2 > &b)
Subtract compatible vector from this.
Definition Vector.H:279
Vector< vector > cross(const Vector< vector > &v2) const
Cmpt & x() noexcept
Access to the vector x component.
Definition Vector.H:150
Vector(Istream &is)
Construct from Istream.
Definition VectorI.H:57
static constexpr direction rank
Definition Vector.H:77
Vector< vector > & normalise(const scalar tol=ROOTVSMALL)
std::enable_if_t< std::is_convertible_v< Cmpt2, Cmpt >, void > operator+=(const Vector< Cmpt2 > &b)
Add compatible vector to this.
Definition Vector.H:267
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 distSqr(const Vector< vector > &v2) const
static bool less_zxy(const Vector< vector > &a, const Vector< vector > &b)
scalar mag() const
Vector(Foam::zero)
Construct initialized to zero.
Definition VectorI.H:25
Vector(const Vector &)=default
Copy construct.
Vector(const Cmpt &vx, const Cmpt &vy, const Cmpt &vz)
Construct from three components.
Definition VectorI.H:44
static bool less_yzx(const Vector< vector > &a, const Vector< vector > &b)
Vector()=default
Default construct.
const Cmpt & y() const noexcept
Access to the vector y component.
Definition Vector.H:140
Vector(const VectorSpace< Vector< Cmpt2 >, Cmpt2, 3 > &vs)
Copy construct from VectorSpace of the same rank.
Definition VectorI.H:34
scalar magSqr() const
Vector< vector > & removeCollinear(const Vector< vector > &unitVec)
Vector< Cmpt > type
Definition Vector.H:314
Vector< solveScalar > type
Definition Vector.H:332
The extended precision type (solveScalar for float).
Definition products.H:81
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Namespace for OpenFOAM.
uint8_t direction
Definition direction.H:49
const direction noexcept
Definition scalarImpl.H:265
volScalarField & b
A template class to specify if a data type is composed solely of Foam::label elements.
Definition contiguous.H:82
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition contiguous.H:87
A template class to specify that a data type can be considered as being contiguous in memory.
Definition contiguous.H:70