Loading...
Searching...
No Matches
Vector2D.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-2025 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::Vector2D
29
30Description
31 Templated 2D Vector derived from VectorSpace adding construction from
32 2 components, element access using x() and y() member functions and
33 the inner-product (dot-product).
34
35SourceFiles
36 Vector2DI.H
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Foam_Vector2D_H
41#define Foam_Vector2D_H
42
43#include "contiguous.H"
44#include "VectorSpace.H"
45
46// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47
48namespace Foam
49{
50
51/*---------------------------------------------------------------------------*\
52 Class Vector2D Declaration
53\*---------------------------------------------------------------------------*/
54
55template<class Cmpt>
56class Vector2D
57:
58 public VectorSpace<Vector2D<Cmpt>, Cmpt, 2>
59{
60public:
61
62 // Typedefs
63
64 //- Equivalent type of labels used for valid component indexing
66
67
68 // Member Constants
69
70 //- Rank of Vector2D is 1
71 static constexpr direction rank = 1;
72
73
74 //- Component labeling enumeration
75 enum components { X, Y };
77
78 // Generated Methods
79
80 //- Default construct
81 Vector2D() = default;
82
83 //- Copy construct
84 Vector2D(const Vector2D&) = default;
85
86 //- Copy assignment
87 Vector2D& operator=(const Vector2D&) = default;
88
90 // Constructors
91
92 //- Construct initialized to zero
93 inline Vector2D(Foam::zero);
95 //- Copy construct from VectorSpace of the same rank
96 inline Vector2D(const VectorSpace<Vector2D<Cmpt>, Cmpt, 2>& vs);
97
98 //- Construct from two components
99 inline Vector2D(const Cmpt& vx, const Cmpt& vy);
100
101 //- Construct from Istream
102 inline explicit Vector2D(Istream& is);
103
104
105 // Member Functions
106
107 // Component Access
108
109 //- Access to the vector x component
110 const Cmpt& x() const noexcept { return this->v_[components::X]; }
111
112 //- Access to the vector y component
113 const Cmpt& y() const noexcept { return this->v_[components::Y]; }
114
115 //- Access to the vector x component
116 Cmpt& x() noexcept { return this->v_[components::X]; }
118 //- Access to the vector y component
119 Cmpt& y() noexcept { return this->v_[components::Y]; }
120
121
122 // Vector Operations
123
124 //- The length (L2-norm) of the vector
125 inline scalar mag() const;
126
127 //- The length (L2-norm) squared of the vector.
128 inline scalar magSqr() const;
129
130 //- The L2-norm distance from another vector.
131 //- The mag() of the difference.
132 inline scalar dist(const Vector2D<Cmpt>& v2) const;
133
134 //- The L2-norm distance squared from another vector.
135 //- The magSqr() of the difference.
136 inline scalar distSqr(const Vector2D<Cmpt>& v2) const;
138 //- Normalise the vector by its magnitude
139 // For small magnitudes (less than ROOTVSMALL) set to zero.
140 // Will not be particularly useful for a vector of labels
141 inline Vector2D<Cmpt>& normalise(const scalar tol = ROOTVSMALL);
143 //- Inplace removal of components that are collinear to the given
144 //- unit vector.
145 inline Vector2D<Cmpt>& removeCollinear(const Vector2D<Cmpt>& unitVec);
146
147 //- Perp dot product (dot product with perpendicular vector)
148 inline scalar perp(const Vector2D<Cmpt>& b) const;
149
150 //- Return true if vector is within tol
151 inline bool isClose
152 (
153 const Vector2D<Cmpt>& b,
154 const scalar tol = 1e-10
155 ) const;
156
157
158 // Comparison Operations
159
160 //- Lexicographically compare \em a and \em b with order (x:y)
161 static inline bool less_xy
162 (
163 const Vector2D<Cmpt>& a,
164 const Vector2D<Cmpt>& b
165 );
166
167 //- Lexicographically compare \em a and \em b with order (y:x)
168 static inline bool less_yx
169 (
170 const Vector2D<Cmpt>& a,
171 const Vector2D<Cmpt>& b
172 );
173
174
175 // Member Operators
176
177 //- Inherit VectorSpace += operations
179
180 //- Inherit VectorSpace -= operations
182
183 //- Add compatible 2D vector to this
184 template<class Cmpt2>
185 std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
187 {
188 this->x() += b.x();
189 this->y() += b.y();
190 }
192 //- Subtract compatible 2D vector from this
193 template<class Cmpt2>
194 std::enable_if_t<std::is_convertible_v<Cmpt2, Cmpt>, void>
195 inline operator-=(const Vector2D<Cmpt2>& b)
196 {
197 this->x() -= b.x();
198 this->y() -= b.y();
199 }
200};
201
202
203// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
204
205//- Data are contiguous if component type is contiguous
206template<class Cmpt>
207struct is_contiguous<Vector2D<Cmpt>> : is_contiguous<Cmpt> {};
208
209//- Data are contiguous label if component type is label
210template<class Cmpt>
211struct is_contiguous_label<Vector2D<Cmpt>> : is_contiguous_label<Cmpt> {};
213//- Data are contiguous scalar if component type is scalar
214template<class Cmpt>
215struct is_contiguous_scalar<Vector2D<Cmpt>> : is_contiguous_scalar<Cmpt> {};
216
217
218// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219
220} // End namespace Foam
221
222// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223
224#include "Vector2DI.H"
225
226// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227
228#endif
229
230// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
Templated 2D Vector derived from VectorSpace adding construction from 2 components,...
Definition Vector2D.H:54
Cmpt & y() noexcept
Access to the vector y component.
Definition Vector2D.H:142
std::enable_if_t< std::is_convertible_v< Cmpt2, Cmpt >, void > operator+=(const Vector2D< Cmpt2 > &b)
Add compatible 2D vector to this.
Definition Vector2D.H:236
const Cmpt & x() const noexcept
Access to the vector x component.
Definition Vector2D.H:127
scalar perp(const Vector2D< Cmpt > &b) const
Perp dot product (dot product with perpendicular vector).
Definition Vector2DI.H:163
components
Component labeling enumeration.
Definition Vector2D.H:76
scalar dist(const Vector2D< Cmpt > &v2) const
The L2-norm distance from another vector. The mag() of the difference.
Definition Vector2DI.H:89
scalar distSqr(const Vector2D< Cmpt > &v2) const
The L2-norm distance squared from another vector. The magSqr() of the difference.
Definition Vector2DI.H:78
Vector2D(const Cmpt &vx, const Cmpt &vy)
Construct from two components.
Definition Vector2DI.H:42
Vector2D(Istream &is)
Construct from Istream.
Definition Vector2DI.H:50
Cmpt & x() noexcept
Access to the vector x component.
Definition Vector2D.H:137
static bool less_yx(const Vector2D< Cmpt > &a, const Vector2D< Cmpt > &b)
Lexicographically compare a and b with order (y:x).
Definition Vector2DI.H:138
static constexpr direction rank
Definition Vector2D.H:70
Vector2D & operator=(const Vector2D &)=default
Copy assignment.
bool isClose(const Vector2D< Cmpt > &b, const scalar tol=1e-10) const
Return true if vector is within tol.
Definition Vector2DI.H:171
scalar mag() const
The length (L2-norm) of the vector.
Definition Vector2DI.H:70
Vector2D()=default
Default construct.
Vector2D(const VectorSpace< Vector2D< Cmpt >, Cmpt, 2 > &vs)
Copy construct from VectorSpace of the same rank.
Definition Vector2DI.H:33
Vector2D(Foam::zero)
Construct initialized to zero.
Definition Vector2DI.H:25
Vector2D(const Vector2D &)=default
Copy construct.
const Cmpt & y() const noexcept
Access to the vector y component.
Definition Vector2D.H:132
scalar magSqr() const
The length (L2-norm) squared of the vector.
Definition Vector2DI.H:59
Vector2D< Cmpt > & removeCollinear(const Vector2D< Cmpt > &unitVec)
Inplace removal of components that are collinear to the given unit vector.
Definition Vector2DI.H:115
static bool less_xy(const Vector2D< Cmpt > &a, const Vector2D< Cmpt > &b)
Lexicographically compare a and b with order (x:y).
Definition Vector2DI.H:126
Vector2D< label > labelType
Definition Vector2D.H:62
std::enable_if_t< std::is_convertible_v< Cmpt2, Cmpt >, void > operator-=(const Vector2D< Cmpt2 > &b)
Subtract compatible 2D vector from this.
Definition Vector2D.H:247
Vector2D< Cmpt > & normalise(const scalar tol=ROOTVSMALL)
Normalise the vector by its magnitude.
Definition Vector2DI.H:96
Cmpt v_[Ncmpts]
The components of this vector space.
Definition VectorSpace.H:81
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
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
volScalarField & e
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