Arcane  4.2.1.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-2026 */
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#include <array>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
36template <typename T>
38{
39 public:
40
41 using ThatClass = Vector2<T>;
42 using value_type = T;
43
44 public:
45
46 T x = {};
47 T y = {};
48
49 public:
50
52 constexpr ARCCORE_HOST_DEVICE
53 Vector2() = default;
54
56 constexpr ARCCORE_HOST_DEVICE Vector2(const T& ax, const T& ay)
57 : x(ax)
58 , y(ay)
59 {
60 }
61
63 constexpr ARCCORE_HOST_DEVICE explicit Vector2(const T& v)
64 : x(v)
65 , y(v)
66 {
67 }
68
70 constexpr Vector2(const std::array<T, 2>& v)
71 : x(v[0])
72 , y(v[1])
73 {
74 }
75
77 constexpr Vector2(std::initializer_list<T> v)
78 {
79 _setFromList(v);
80 }
81
83 constexpr Vector2& operator=(std::initializer_list<T> v)
84 {
85 _setFromList(v);
86 return (*this);
87 }
88
89 public:
90
91 friend constexpr ARCCORE_HOST_DEVICE bool
92 operator<(const Vector2<T>& v1, const Vector2<T>& v2)
93 {
94 if (v1.x == v2.x) {
95 return v1.y < v2.y;
96 }
97 return (v1.x < v2.x);
98 }
99
101 friend std::ostream& operator<<(std::ostream& o, const Vector2<T>& t)
102 {
103 t._print(o);
104 return o;
105 }
106
107 friend constexpr ARCCORE_HOST_DEVICE bool
108 operator==(const Vector2<T>& v1, const Vector2<T>& v2)
109 {
110 return v1.x == v2.x && v1.y == v2.y;
111 }
112
113 friend constexpr ARCCORE_HOST_DEVICE bool
114 operator!=(const Vector2<T>& v1, const Vector2<T>& v2)
115 {
116 return !(v1 == v2);
117 }
118
119 public:
120
122 constexpr ARCCORE_HOST_DEVICE void operator+=(const T& b)
123 {
124 x += b;
125 y += b;
126 }
127
129 constexpr ARCCORE_HOST_DEVICE void operator+=(const ThatClass& b)
130 {
131 x += b.x;
132 y += b.y;
133 }
134
136 constexpr ARCCORE_HOST_DEVICE void operator-=(const T& b)
137 {
138 x -= b;
139 y -= b;
140 }
141
143 constexpr ARCCORE_HOST_DEVICE void operator-=(const ThatClass& b)
144 {
145 x -= b.x;
146 y -= b.y;
147 }
148
150 constexpr ARCCORE_HOST_DEVICE void operator*=(const T& b)
151 {
152 x *= b;
153 y *= b;
154 }
155
157 constexpr ARCCORE_HOST_DEVICE void operator/=(const T& b)
158 {
159 x /= b;
160 y /= b;
161 }
162
164 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
165 {
166 return ThatClass(a.x + b.x, a.y + b.y);
167 }
168
170 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const T& b)
171 {
172 return ThatClass(a.x + b, a.y + b);
173 }
174
176 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const T& a, const ThatClass& b)
177 {
178 return ThatClass(a + b.x, a + b.y);
179 }
180
182 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
183 {
184 return ThatClass(a.x - b.x, a.y - b.y);
185 }
186
188 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const T& b)
189 {
190 return ThatClass(a.x - b, a.y - b);
191 }
192
194 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const { return ThatClass(-x, -y); }
195
197 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const T& a, const ThatClass& b)
198 {
199 return ThatClass(b.x * a, b.y * a);
200 }
201
203 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& a, const T& b)
204 {
205 return ThatClass(a.x * b, a.y * b);
206 }
207
209 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& a, const T& b)
210 {
211 return ThatClass(a.x / b, a.y / b);
212 }
213
214 private:
215
216 void _print(std::ostream& o) const
217 {
218 o << '{' << x << ',' << y << '}';
219 }
220 constexpr void _setFromList(std::initializer_list<T> v)
221 {
222 auto s = v.size();
223 auto ptr = v.begin();
224 if (s > 0)
225 x = *ptr;
226 ++ptr;
227 if (s > 1)
228 y = *ptr;
229 }
230};
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
235} // namespace Arcane
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240#endif
Declarations of types used in Arcane.
Class managing a 2-dimensional vector of type T.
Definition Vector2.h:38
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &a, const T &b)
Divides each component of a by b.
Definition Vector2.h:209
constexpr __host__ __device__ ThatClass operator-() const
Returns the opposite of the instance.
Definition Vector2.h:194
friend constexpr __host__ __device__ ThatClass operator*(const T &a, const ThatClass &b)
Multiplies each component of b by a.
Definition Vector2.h:197
constexpr Vector2 & operator=(std::initializer_list< T > v)
Positions the instance with the list v.
Definition Vector2.h:83
constexpr Vector2(std::initializer_list< T > v)
Constructs the instance with the list v.
Definition Vector2.h:77
constexpr __host__ __device__ Vector2(const T &ax, const T &ay)
Constructs the triplet (ax,ay,az).
Definition Vector2.h:56
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:182
constexpr __host__ __device__ void operator+=(const T &b)
Adds b to the instance.
Definition Vector2.h:122
constexpr Vector2(const std::array< T, 2 > &v)
Constructs the instance with the array v.
Definition Vector2.h:70
friend constexpr __host__ __device__ ThatClass operator+(const T &a, const ThatClass &b)
Returns b by adding a to each component.
Definition Vector2.h:176
constexpr __host__ __device__ void operator-=(const ThatClass &b)
Subtracts b from the instance.
Definition Vector2.h:143
friend std::ostream & operator<<(std::ostream &o, const Vector2< T > &t)
Writes t to the stream o.
Definition Vector2.h:101
constexpr __host__ __device__ void operator+=(const ThatClass &b)
Adds b to the instance.
Definition Vector2.h:129
constexpr __host__ __device__ void operator*=(const T &b)
Multiplies each component of the instance by b.
Definition Vector2.h:150
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &a, const T &b)
Multiplies each component of a by b.
Definition Vector2.h:203
constexpr __host__ __device__ Vector2(const T &v)
Constructs the instance with the triplet (v,v,v).
Definition Vector2.h:63
constexpr __host__ __device__ void operator-=(const T &b)
Subtracts b from the instance.
Definition Vector2.h:136
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:170
constexpr __host__ __device__ void operator/=(const T &b)
Divides each component of the instance by b.
Definition Vector2.h:157
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const T &b)
Subtracts each component of a by b.
Definition Vector2.h:188
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Sums component by component of a and b.
Definition Vector2.h:164
-- 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