olp-cpp-sdk  1.22.0
Vector.h
1 /*
2  * Copyright (C) 2019-2021 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 namespace olp {
23 namespace math {
24 
25 template <typename T>
27 struct Vector2 {
28  Vector2() {}
35  Vector2(T a, T b) {
36  x = a;
37  y = b;
38  }
39 
46  this->x = static_cast<T>(v.x);
47  this->y = static_cast<T>(v.y);
48  return *this;
49  }
50 
56  Vector2<T> operator*(T const& s) const { return Vector2<T>(x * s, y * s); }
58  Vector2<T> operator-() { return Vector2<T>(-x, -y); }
59 
65  Vector2<T> operator-(Vector2<T> const& v) const {
66  return Vector2<T>(x - v.x, y - v.y);
67  }
68 
74  Vector2<T> operator+(Vector2<T> const& v) const {
75  return Vector2<T>(x + v.x, y + v.y);
76  }
77 
79  T x;
81  T y;
82 };
83 
84 template <typename T>
86 struct Vector3 {
87  Vector3() {}
95  Vector3(T a, T b, T c) {
96  x = a;
97  y = b;
98  z = c;
99  }
105  explicit Vector3(T const& s) : x(s), y(s), z(s) {}
106 
112  Vector3<T> operator*(T const& s) const {
113  return Vector3<T>(x * s, y * s, z * s);
114  }
116  Vector3<T> operator-() const { return Vector3<T>(-x, -y, -z); }
117 
123  Vector3<T> operator-(Vector3<T> const& v) const {
124  return Vector3<T>(x - v.x, y - v.y, z - v.z);
125  }
126 
132  Vector3<T> operator+(Vector3<T> const& v) const {
133  return Vector3<T>(x + v.x, y + v.y, z + v.z);
134  }
135 
143  return Vector3<bool>(x < v.x, y < v.y, z < v.z);
144  }
145 
147  T x;
149  T y;
151  T z;
152 };
153 
154 template <typename T, unsigned int N>
156 struct VectorImpl;
157 
158 template <typename T>
160 struct VectorImpl<T, 2> {
162  using type = Vector2<T>;
163 };
164 
165 template <typename T>
167 struct VectorImpl<T, 3> {
169  using type = Vector3<T>;
170 };
171 
172 template <typename T, unsigned int N>
173 using Vector = typename VectorImpl<T, N>::type;
174 
175 } // namespace math
176 } // namespace olp
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
Represents 2D vectors and points.
Definition: Vector.h:27
Vector2< T > & operator=(Vector2< T > const &v)
Assigns components of one vector to another vector.
Definition: Vector.h:45
Vector2< T > operator-()
Negates the vector.
Definition: Vector.h:58
T x
The X component of the vector.
Definition: Vector.h:79
T y
The Y component of the vector.
Definition: Vector.h:81
Vector2(T a, T b)
Creates a Vector2 instance with the given X and Y components.
Definition: Vector.h:35
Vector2< T > operator+(Vector2< T > const &v) const
Adds corresponding components of two vectors.
Definition: Vector.h:74
Vector2< T > operator-(Vector2< T > const &v) const
Subtracts one vector from another.
Definition: Vector.h:65
Vector2< T > operator*(T const &s) const
Multiplies each vector component by a number.
Definition: Vector.h:56
Represents 3D vectors and points.
Definition: Vector.h:86
Vector3< T > operator-() const
Negates the vector.
Definition: Vector.h:116
Vector3< T > operator*(T const &s) const
Multiplies each vector component by a number.
Definition: Vector.h:112
Vector3< T > operator-(Vector3< T > const &v) const
Subtracts one vector from another.
Definition: Vector.h:123
Vector3(T a, T b, T c)
Creates a Vector3 instance with the given X, Y, and Z components.
Definition: Vector.h:95
Vector3< bool > LessThan(Vector3< T > const &v) const
Checks whether the parameters of one vector are less than the parameters of the other vector.
Definition: Vector.h:142
Vector3< T > operator+(Vector3< T > const &v) const
Adds corresponding components of two vectors.
Definition: Vector.h:132
T z
The Z component of the vector.
Definition: Vector.h:151
T y
The Y component of the vector.
Definition: Vector.h:149
T x
The X component of the vector.
Definition: Vector.h:147
Vector3(T const &s)
Creates a Vector3 instance.
Definition: Vector.h:105
The implementation structure of a vector.
Definition: Vector.h:156