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-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::Vector2D
29 
30 Description
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 
35 SourceFiles
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 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class Vector2D Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 template<class Cmpt>
56 class Vector2D
57 :
58  public VectorSpace<Vector2D<Cmpt>, Cmpt, 2>
59 {
60 public:
61 
62  // Typedefs
63 
64  //- Equivalent type of labels used for valid component indexing
65  typedef Vector2D<label> labelType;
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 };
76 
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 
89 
90  // Constructors
91 
92  //- Construct initialized to zero
93  inline Vector2D(const Foam::zero);
94 
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_[X]; }
111 
112  //- Access to the vector y component
113  const Cmpt& y() const noexcept { return this->v_[Y]; }
114 
115  //- Access to the vector x component
116  Cmpt& x() noexcept { return this->v_[X]; }
117 
118  //- Access to the vector y component
119  Cmpt& y() noexcept { return this->v_[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 
176 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
177 
178 //- Data are contiguous if component type is contiguous
179 template<class Cmpt>
180 struct is_contiguous<Vector2D<Cmpt>> : is_contiguous<Cmpt> {};
181 
182 //- Data are contiguous label if component type is label
183 template<class Cmpt>
184 struct is_contiguous_label<Vector2D<Cmpt>> : is_contiguous_label<Cmpt> {};
185 
186 //- Data are contiguous scalar if component type is scalar
187 template<class Cmpt>
188 struct is_contiguous_scalar<Vector2D<Cmpt>> : is_contiguous_scalar<Cmpt> {};
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #include "Vector2DI.H"
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #endif
202 
203 // ************************************************************************* //
Vector2D< Cmpt > & removeCollinear(const Vector2D< Cmpt > &unitVec)
Inplace removal of components that are collinear to the given unit vector.
Definition: Vector2DI.H:115
uint8_t direction
Definition: direction.H:46
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector2D.H:132
Vector2D & operator=(const Vector2D &)=default
Copy assignment.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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
Templated vector space.
Definition: VectorSpace.H:52
scalar perp(const Vector2D< Cmpt > &b) const
Perp dot product (dot product with perpendicular vector)
Definition: Vector2DI.H:163
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
scalar distSqr(const Vector2D< Cmpt > &v2) const
The L2-norm distance squared from another vector. The magSqr() of the difference. ...
Definition: Vector2DI.H:78
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
scalar dist(const Vector2D< Cmpt > &v2) const
The L2-norm distance from another vector. The mag() of the difference.
Definition: Vector2DI.H:89
scalar magSqr() const
The length (L2-norm) squared of the vector.
Definition: Vector2DI.H:59
Vector2D< Cmpt > & normalise(const scalar tol=ROOTVSMALL)
Normalise the vector by its magnitude.
Definition: Vector2DI.H:96
const direction noexcept
Definition: Scalar.H:258
const Cmpt & x() const noexcept
Access to the vector x component.
Definition: Vector2D.H:127
scalar mag() const
The length (L2-norm) of the vector.
Definition: Vector2DI.H:70
components
Component labeling enumeration.
Definition: Vector2D.H:76
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
bool isClose(const Vector2D< Cmpt > &b, const scalar tol=1e-10) const
Return true if vector is within tol.
Definition: Vector2DI.H:171
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
static constexpr direction rank
Rank of Vector2D is 1.
Definition: Vector2D.H:70
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Vector2D()=default
Default construct.
Templated 2D Vector derived from VectorSpace adding construction from 2 components, element access using x() and y() member functions and the inner-product (dot-product).
Definition: Vector2D.H:51
Vector2D< label > labelType
Equivalent type of labels used for valid component indexing.
Definition: Vector2D.H:62
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:81
Namespace for OpenFOAM.