Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Vector2.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Vector2.h (C) 2000-2023 */
9/* */
10/* 2-dimensional vector. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_VECTOR2_H
13#define ARCANE_UTILS_VECTOR2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include <iostream>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
35template <typename T>
37{
38 public:
39
40 using ThatClass = Vector2<T>;
41 using value_type = T;
42
43 public:
44
45 T x = {};
46 T y = {};
47
48 public:
49
51 constexpr ARCCORE_HOST_DEVICE
52 Vector2() = default;
53
55 constexpr ARCCORE_HOST_DEVICE Vector2(const T& ax, const T& ay)
56 : x(ax)
57 , y(ay)
58 {
59 }
60
62 constexpr ARCCORE_HOST_DEVICE explicit Vector2(const T& v)
63 : x(v)
64 , y(v)
65 {
66 }
67
69 constexpr Vector2(const std::array<T, 2>& v)
70 : x(v[0])
71 , y(v[1])
72 {
73 }
74
76 constexpr Vector2(std::initializer_list<T> v)
77 {
78 _setFromList(v);
79 }
80
82 constexpr Vector2& operator=(std::initializer_list<T> v)
83 {
84 _setFromList(v);
85 return (*this);
86 }
87
88 public:
89
90 friend constexpr ARCCORE_HOST_DEVICE bool
91 operator<(const Vector2<T>& v1, const Vector2<T>& v2)
92 {
93 if (v1.x == v2.x) {
94 return v1.y < v2.y;
95 }
96 return (v1.x < v2.x);
97 }
98
100 friend std::ostream& operator<<(std::ostream& o, const Vector2<T>& t)
101 {
102 t._print(o);
103 return o;
104 }
105
106 friend constexpr ARCCORE_HOST_DEVICE bool
107 operator==(const Vector2<T>& v1, const Vector2<T>& v2)
108 {
109 return v1.x == v2.x && v1.y == v2.y;
110 }
111
112 friend constexpr ARCCORE_HOST_DEVICE bool
113 operator!=(const Vector2<T>& v1, const Vector2<T>& v2)
114 {
115 return !(v1 == v2);
116 }
117
118 public:
119
121 constexpr ARCCORE_HOST_DEVICE void operator+=(const T& b)
122 {
123 x += b;
124 y += b;
125 }
126
128 constexpr ARCCORE_HOST_DEVICE void operator+=(const ThatClass& b)
129 {
130 x += b.x;
131 y += b.y;
132 }
133
135 constexpr ARCCORE_HOST_DEVICE void operator-=(const T& b)
136 {
137 x -= b;
138 y -= b;
139 }
140
142 constexpr ARCCORE_HOST_DEVICE void operator-=(const ThatClass& b)
143 {
144 x -= b.x;
145 y -= b.y;
146 }
147
149 constexpr ARCCORE_HOST_DEVICE void operator*=(const T& b)
150 {
151 x *= b;
152 y *= b;
153 }
154
156 constexpr ARCCORE_HOST_DEVICE void operator/=(const T& b)
157 {
158 x /= b;
159 y /= b;
160 }
161
163 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
164 {
165 return ThatClass(a.x + b.x, a.y + b.y);
166 }
167
169 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const T& b)
170 {
171 return ThatClass(a.x + b, a.y + b);
172 }
173
175 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const T& a, const ThatClass& b)
176 {
177 return ThatClass(a + b.x, a + b.y);
178 }
179
181 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
182 {
183 return ThatClass(a.x - b.x, a.y - b.y);
184 }
185
187 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const T& b)
188 {
189 return ThatClass(a.x - b, a.y - b);
190 }
191
193 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const { return ThatClass(-x, -y); }
194
196 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const T& a, const ThatClass& b)
197 {
198 return ThatClass(b.x * a, b.y * a);
199 }
200
202 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& a, const T& b)
203 {
204 return ThatClass(a.x * b, a.y * b);
205 }
206
208 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& a, const T& b)
209 {
210 return ThatClass(a.x / b, a.y / b);
211 }
212
213 private:
214
215 void _print(std::ostream& o) const
216 {
217 o << '{' << x << ',' << y << '}';
218 }
219 constexpr void _setFromList(std::initializer_list<T> v)
220 {
221 auto s = v.size();
222 auto ptr = v.begin();
223 if (s > 0)
224 x = *ptr;
225 ++ptr;
226 if (s > 1)
227 y = *ptr;
228 }
229};
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234} // namespace Arcane
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239#endif
Declarations of types used in Arcane.
Class managing a 2-dimensional vector of type T.
Definition Vector2.h:37
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &a, const T &b)
Divides each component of a by b.
Definition Vector2.h:208
constexpr __host__ __device__ ThatClass operator-() const
Returns the opposite of the instance.
Definition Vector2.h:193
friend constexpr __host__ __device__ ThatClass operator*(const T &a, const ThatClass &b)
Multiplies each component of b by a.
Definition Vector2.h:196
constexpr Vector2 & operator=(std::initializer_list< T > v)
Positions the instance with the list v.
Definition Vector2.h:82
constexpr Vector2(std::initializer_list< T > v)
Constructs the instance with the list v.
Definition Vector2.h:76
constexpr __host__ __device__ Vector2(const T &ax, const T &ay)
Constructs the triplet (ax,ay,az).
Definition Vector2.h:55
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Subtracts each component of a by each component of b.
Definition Vector2.h:181
constexpr __host__ __device__ void operator+=(const T &b)
Adds b to the instance.
Definition Vector2.h:121
constexpr Vector2(const std::array< T, 2 > &v)
Constructs the instance with the array v.
Definition Vector2.h:69
friend constexpr __host__ __device__ ThatClass operator+(const T &a, const ThatClass &b)
Returns b by adding a to each component.
Definition Vector2.h:175
constexpr __host__ __device__ void operator-=(const ThatClass &b)
Subtracts b from the instance.
Definition Vector2.h:142
friend std::ostream & operator<<(std::ostream &o, const Vector2< T > &t)
Writes t to the stream o.
Definition Vector2.h:100
constexpr __host__ __device__ void operator+=(const ThatClass &b)
Adds b to the instance.
Definition Vector2.h:128
constexpr __host__ __device__ void operator*=(const T &b)
Multiplies each component of the instance by b.
Definition Vector2.h:149
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &a, const T &b)
Multiplies each component of a by b.
Definition Vector2.h:202
constexpr __host__ __device__ Vector2(const T &v)
Constructs the instance with the triplet (v,v,v).
Definition Vector2.h:62
constexpr __host__ __device__ void operator-=(const T &b)
Subtracts b from the instance.
Definition Vector2.h:135
constexpr __host__ __device__ Vector2()=default
Constructs the zero vector.
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const T &b)
Returns a by adding b to each component.
Definition Vector2.h:169
constexpr __host__ __device__ void operator/=(const T &b)
Divides each component of the instance by b.
Definition Vector2.h:156
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const T &b)
Subtracts each component of a by b.
Definition Vector2.h:187
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Sums component by component of a and b.
Definition Vector2.h:163
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool operator<(const Item &item1, const Item &item2)
Compare two entities.
Definition Item.h:566