Vector2DI.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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Cmpt>
33 :
35 {}
36 
37 
38 template<class Cmpt>
40 (
41  const VectorSpace<Vector2D<Cmpt>, Cmpt, 2>& vs
42 )
43 :
44  Vector2D::vsType(vs)
45 {}
46 
47 
48 template<class Cmpt>
49 inline Foam::Vector2D<Cmpt>::Vector2D(const Cmpt& vx, const Cmpt& vy)
50 {
51  this->v_[X] = vx;
52  this->v_[Y] = vy;
53 }
54 
55 
56 template<class Cmpt>
58 :
60 {}
61 
62 
63 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 
65 template<class Cmpt>
66 inline Foam::scalar Foam::Vector2D<Cmpt>::magSqr() const
67 {
68  return
69  (
70  Foam::magSqr(this->x())
71  + Foam::magSqr(this->y())
72  );
73 }
74 
75 
76 template<class Cmpt>
77 inline Foam::scalar Foam::Vector2D<Cmpt>::mag() const
78 {
79  return ::sqrt(this->magSqr());
80 }
81 
82 
83 template<class Cmpt>
84 inline Foam::scalar
86 {
87  return
88  (
89  Foam::magSqr(v2.x() - this->x())
90  + Foam::magSqr(v2.y() - this->y())
91  );
92 }
93 
94 
95 template<class Cmpt>
96 inline Foam::scalar Foam::Vector2D<Cmpt>::dist(const Vector2D<Cmpt>& v2) const
97 {
98  return ::sqrt(this->distSqr(v2));
99 }
100 
101 
102 template<class Cmpt>
104 {
105  const scalar s = this->mag();
106 
107  if (s < tol)
108  {
109  *this = Zero;
110  }
111  else
112  {
113  *this /= s;
114  }
116  return *this;
117 }
118 
119 
120 template<class Cmpt>
121 inline Foam::Vector2D<Cmpt>&
123 {
124  *this -= (*this & unitVec) * unitVec;
125  return *this;
126 }
127 
128 
129 // * * * * * * * * * * * * * Comparison Operations * * * * * * * * * * * * * //
130 
131 template<class Cmpt>
132 inline bool
134 {
135  return
136  (
137  (a.x() < b.x())
138  || (!(b.x() < a.x()) && (a.y() < b.y()))
139  );
140 }
141 
142 
143 template<class Cmpt>
144 inline bool
146 {
147  return
148  (
149  (a.y() < b.y())
150  || (!(b.y() < a.y()) && (a.x() < b.x()))
151  );
152 }
153 
154 
155 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
157 namespace Foam
158 {
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 template<class Cmpt>
163 inline Cmpt operator&(const Vector2D<Cmpt>& v1, const Vector2D<Cmpt>& v2)
164 {
165  return Cmpt(v1.x()*v2.x() + v1.y()*v2.y());
166 }
167 
168 
169 template<class Cmpt>
170 inline scalar Vector2D<Cmpt>::perp(const Vector2D<Cmpt>& b) const
171 {
172  return x()*b.y() - y()*b.x();
173 }
174 
175 
176 template<class Cmpt>
177 inline bool Vector2D<Cmpt>::isClose
178 (
179  const Vector2D<Cmpt>& b,
180  const scalar tol
181 ) const
182 {
183  return (Foam::mag(x() - b.x()) < tol && Foam::mag(y() - b.y()) < tol);
184 }
185 
186 
187 template<class Cmpt>
188 inline Vector2D<Cmpt> operator*(const Cmpt& s, const Vector2D<Cmpt>& v)
189 {
190  return Vector2D<Cmpt>(s*v.x(), s*v.y());
191 }
192 
193 
194 template<class Cmpt>
195 inline Vector2D<Cmpt> operator*(const Vector2D<Cmpt>& v, const Cmpt& s)
196 {
197  return s*v;
198 }
199 
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace Foam
204 
205 // ************************************************************************* //
Vector2D< Cmpt > & removeCollinear(const Vector2D< Cmpt > &unitVec)
Inplace removal of components that are collinear to the given unit vector.
Definition: Vector2DI.H:115
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector2D.H:132
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
dimensionedScalar sqrt(const dimensionedScalar &ds)
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
scalar y
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
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 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
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
tmp< GeometricField< Type, faPatchField, areaMesh > > operator &(const faMatrix< Type > &, const DimensionedField< Type, areaMesh > &)
PtrList< volScalarField > & Y
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.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
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
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127