MeshKernel
Vector.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2021.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation version 3.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // contact: delft3d.support@deltares.nl
18 // Stichting Deltares
19 // P.O. Box 177
20 // 2600 MH Delft, The Netherlands
21 //
22 // All indications and logos of, and references to, "Delft3D" and "Deltares"
23 // are registered trademarks of Stichting Deltares, and remain the property of
24 // Stichting Deltares. All rights reserved.
25 //
26 //------------------------------------------------------------------------------
27 
28 #pragma once
29 
30 #include <array>
31 #include <cmath>
32 
33 #include "MeshKernel/Constants.hpp"
34 
35 namespace meshkernel
36 {
38  class Vector
39  {
40  public:
42  Vector() : m_values{constants::missing::doubleValue, constants::missing::doubleValue} {}
43 
48  Vector(const double x, const double y) : m_values{x, y} {}
49 
52  [[nodiscard]] double x() const
53  {
54  return m_values[0];
55  }
56 
59  [[nodiscard]] double& x()
60  {
61  return m_values[0];
62  }
63 
66  [[nodiscard]] double y() const
67  {
68  return m_values[1];
69  }
72  [[nodiscard]] double& y()
73  {
74  return m_values[1];
75  }
76 
78  double operator[](const UInt i) const;
79 
81  double& operator[](const UInt i);
82 
84  Vector& operator+=(const Vector& vec);
85 
87  Vector& operator-=(const Vector& vec);
88 
92  Vector& operator/=(const double alpha);
93 
95  Vector& operator*=(const double alpha);
96 
100  void normalise();
101 
105  double length() const;
106 
110  double lengthSquared() const;
111 
112  private:
114  std::array<double, 2> m_values;
115  };
116 
120  Vector normalise(const Vector& vec);
121 
123  double dot(const Vector& v1, const Vector& v2);
124 
126  double angleBetween(const Vector& v1, const Vector& v2);
127 
131  Vector operator-(const Vector& vec);
132 
134  Vector operator+(const Vector& v1, const Vector& v2);
135 
137  Vector operator-(const Vector& v1, const Vector& v2);
138 
140  Vector operator*(const double alpha, const Vector& vec);
141 
143  Vector operator*(const Vector& vec, const double alpha);
144 
146  Vector operator/(const Vector& vec, const double alpha);
147 
148 } // namespace meshkernel
149 
150 inline double meshkernel::Vector::operator[](const UInt i) const
151 {
152  return m_values[i];
153 }
154 
155 inline double& meshkernel::Vector::operator[](const UInt i)
156 {
157  return m_values[i];
158 }
159 
161 {
162  m_values[0] += vec.m_values[0];
163  m_values[1] += vec.m_values[1];
164  return *this;
165 }
166 
168 {
169  m_values[0] -= vec.m_values[0];
170  m_values[1] -= vec.m_values[1];
171  return *this;
172 }
173 
175 {
176  m_values[0] /= alpha;
177  m_values[1] /= alpha;
178  return *this;
179 }
180 
182 {
183  m_values[0] *= alpha;
184  m_values[1] *= alpha;
185  return *this;
186 }
187 
189 {
190  double lengthInv = 1.0 / length();
191  m_values[0] *= lengthInv;
192  m_values[1] *= lengthInv;
193 }
194 
195 inline double meshkernel::Vector::length() const
196 {
197  // TODO check implementation of hypot.
198  return std::hypot(m_values[0], m_values[1]);
199 }
200 
202 {
203  return m_values[0] * m_values[0] + m_values[1] * m_values[1];
204 }
205 
207 {
208  double lengthInv = 1.0 / vec.length();
209  Vector normalised(vec.x() * lengthInv, vec.y() * lengthInv);
210  return normalised;
211 }
212 
213 inline double meshkernel::dot(const Vector& v1, const Vector& v2)
214 {
215  return v1.x() * v2.x() + v1.y() * v2.y();
216 }
217 
218 inline double meshkernel::angleBetween(const Vector& v1, const Vector& v2)
219 {
220  return std::atan2(v1.y() * v2.x() - v1.x() * v2.y(), v1.x() * v2.x() + v1.y() * v2.y());
221 }
222 
224 {
225  return Vector(-vec.x(), -vec.y());
226 }
227 
229 {
230  return Vector(v1.x() + v2.x(), v1.y() + v2.y());
231 }
232 
234 {
235  return Vector(v1.x() - v2.x(), v1.y() - v2.y());
236 }
237 
238 inline meshkernel::Vector meshkernel::operator*(const double alpha, const Vector& vec)
239 {
240  return Vector(alpha * vec.x(), alpha * vec.y());
241 }
242 
243 inline meshkernel::Vector meshkernel::operator*(const Vector& vec, const double alpha)
244 {
245  return alpha * vec;
246 }
247 
248 inline meshkernel::Vector meshkernel::operator/(const Vector& vec, const double alpha)
249 {
250  return Vector(vec.x() / alpha, vec.y() / alpha);
251 }
meshkernel::Vector::operator/=
Vector & operator/=(const double alpha)
Inplace divide vector by scalar.
Definition: Vector.hpp:174
meshkernel::Vector::y
double & y()
Gets the y coordinate of the vector.
Definition: Vector.hpp:72
meshkernel::Vector::Vector
Vector()
Default constructor.
Definition: Vector.hpp:42
meshkernel::operator/
Cartesian3DPoint operator/(const Cartesian3DPoint &p, const double value)
Divide Cartesian point p by a scalar value.
Definition: Operations.hpp:634
meshkernel::angleBetween
double angleBetween(const Vector &v1, const Vector &v2)
Compute the angle between two vector.
Definition: Vector.hpp:218
meshkernel::Vector
A class defining a vector.
Definition: Vector.hpp:38
meshkernel::operator-
Cartesian3DPoint operator-(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Subtract Cartesian point p2 from p1.
Definition: Operations.hpp:629
meshkernel::operator*
Cartesian3DPoint operator*(const Cartesian3DPoint &p, const double value)
Multiply Cartesian point p by a scalar value.
Definition: Operations.hpp:644
meshkernel::Vector::x
double & x()
Gets the x coordinate of the vector.
Definition: Vector.hpp:59
meshkernel::Vector::lengthSquared
double lengthSquared() const
Compute the length squared of the vector.
Definition: Vector.hpp:201
meshkernel::Vector::operator+=
Vector & operator+=(const Vector &vec)
Inplace add vector to vector.
Definition: Vector.hpp:160
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::Vector::y
double y() const
Gets the y coordinate of the vector.
Definition: Vector.hpp:66
meshkernel::UInt
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
meshkernel::operator+
Cartesian3DPoint operator+(const Cartesian3DPoint &p1, const Cartesian3DPoint &p2)
Add Cartesian point p2 to p1.
Definition: Operations.hpp:624
meshkernel::Vector::length
double length() const
Compute the length of the vector.
Definition: Vector.hpp:195
meshkernel::Vector::x
double x() const
Gets the x coordinate of the vector.
Definition: Vector.hpp:52
meshkernel::dot
double dot(const Point &p1, const Point &p2)
Compute the dot product of two points.
Definition: Point.hpp:351
meshkernel::Vector::operator-=
Vector & operator-=(const Vector &vec)
Inplace subtract vector from vector.
Definition: Vector.hpp:167
meshkernel::Vector::Vector
Vector(const double x, const double y)
Class constructor.
Definition: Vector.hpp:48
meshkernel::Vector::operator[]
double operator[](const UInt i) const
Get the value of the vector.
Definition: Vector.hpp:150
meshkernel::Vector::normalise
void normalise()
Normalise the vector in place.
Definition: Vector.hpp:188
meshkernel::Vector::operator*=
Vector & operator*=(const double alpha)
Inplace multiply vector by scalar.
Definition: Vector.hpp:181
meshkernel::normalise
Vector normalise(const Vector &vec)
Return the normalised vector.
Definition: Vector.hpp:206