Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
Vector3.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/* Vector3.h (C) 2000-2026 */
9/* */
10/* Vector in 3 dimensions. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_VECTOR3_H
13#define ARCANE_UTILS_VECTOR3_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 = Vector3<T>;
42 using value_type = T;
43
44 public:
45
46 T x = {};
47 T y = {};
48 T z = {};
49
50 public:
51
53 constexpr ARCCORE_HOST_DEVICE
54 Vector3() = default;
55
57 constexpr ARCCORE_HOST_DEVICE Vector3(const T& ax, const T& ay, const T& az)
58 : x(ax)
59 , y(ay)
60 , z(az)
61 {
62 }
63
65 constexpr ARCCORE_HOST_DEVICE explicit Vector3(const T& v)
66 : x(v)
67 , y(v)
68 , z(v)
69 {
70 }
71
73 constexpr explicit Vector3(const std::array<T, 3>& v)
74 : x(v[0])
75 , y(v[1])
76 , z(v[2])
77 {
78 }
79
81 constexpr Vector3(std::initializer_list<T> v)
82 {
83 _setFromList(v);
84 }
85
87 constexpr Vector3& operator=(std::initializer_list<T> v)
88 {
89 _setFromList(v);
90 return (*this);
91 }
92
93 public:
94
95 friend constexpr ARCCORE_HOST_DEVICE bool
96 operator<(const Vector3<T>& v1, const Vector3<T>& v2)
97 {
98 if (v1.x == v2.x) {
99 if (v1.y == v2.y)
100 return v1.z < v2.z;
101 else
102 return v1.y < v2.y;
103 }
104 return (v1.x < v2.x);
105 }
106
108 friend std::ostream& operator<<(std::ostream& o, const Vector3<T>& t)
109 {
110 t._print(o);
111 return o;
112 }
113
114 friend constexpr ARCCORE_HOST_DEVICE bool
115 operator==(const Vector3<T>& v1, const Vector3<T>& v2)
116 {
117 return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
118 }
119
120 friend constexpr ARCCORE_HOST_DEVICE bool
121 operator!=(const Vector3<T>& v1, const Vector3<T>& v2)
122 {
123 return !(v1 == v2);
124 }
125
126 public:
127
129 constexpr ARCCORE_HOST_DEVICE void operator+=(const T& b)
130 {
131 x += b;
132 y += b;
133 z += b;
134 }
135
137 constexpr ARCCORE_HOST_DEVICE void operator+=(const ThatClass& b)
138 {
139 x += b.x;
140 y += b.y;
141 z += b.z;
142 }
143
145 constexpr ARCCORE_HOST_DEVICE void operator-=(const T& b)
146 {
147 x -= b;
148 y -= b;
149 z -= b;
150 }
151
153 constexpr ARCCORE_HOST_DEVICE void operator-=(const ThatClass& b)
154 {
155 x -= b.x;
156 y -= b.y;
157 z -= b.z;
158 }
159
161 constexpr ARCCORE_HOST_DEVICE void operator*=(const T& b)
162 {
163 x *= b;
164 y *= b;
165 z *= b;
166 }
167
169 constexpr ARCCORE_HOST_DEVICE void operator/=(const T& b)
170 {
171 x /= b;
172 y /= b;
173 z /= b;
174 }
175
177 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
178 {
179 return ThatClass(a.x + b.x, a.y + b.y, a.z + b.z);
180 }
181
183 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const T& b)
184 {
185 return ThatClass(a.x + b, a.y + b, a.z + b);
186 }
187
189 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const T& a, const ThatClass& b)
190 {
191 return ThatClass(a + b.x, a + b.y, a + b.z);
192 }
193
195 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
196 {
197 return ThatClass(a.x - b.x, a.y - b.y, a.z - b.z);
198 }
199
201 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const T& b)
202 {
203 return ThatClass(a.x - b, a.y - b, a.z - b);
204 }
205
207 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const { return ThatClass(-x, -y, -z); }
208
209
211 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const T& a, const ThatClass& b)
212 {
213 return ThatClass(b.x * a, b.y * a, b.z * a);
214 }
215
217 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& a, const T& b)
218 {
219 return ThatClass(a.x * b, a.y * b, a.z * b);
220 }
221
223 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& a, const T& b)
224 {
225 return ThatClass(a.x / b, a.y / b, a.z / b);
226 }
227
228 private:
229
230 void _print(std::ostream& o) const
231 {
232 o << '{' << x << ',' << y << ',' << z << '}';
233 }
234 constexpr void _setFromList(std::initializer_list<T> v)
235 {
236 auto s = v.size();
237 auto ptr = v.begin();
238 if (s > 0)
239 x = *ptr;
240 ++ptr;
241 if (s > 1)
242 y = *ptr;
243 ++ptr;
244 if (s > 2)
245 z = *ptr;
246 }
247};
248
249/*---------------------------------------------------------------------------*/
250/*---------------------------------------------------------------------------*/
251
252} // namespace Arcane
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257#endif
Declarations of types used in Arcane.
Class managing a 3-dimensional vector of type T.
Definition Vector3.h:38
constexpr __host__ __device__ void operator-=(const T &b)
Subtracts b from the instance.
Definition Vector3.h:145
friend std::ostream & operator<<(std::ostream &o, const Vector3< T > &t)
Writes the triplet t to the stream o.
Definition Vector3.h:108
constexpr Vector3(std::initializer_list< T > v)
Constructs the instance with the list v.
Definition Vector3.h:81
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &a, const T &b)
Divides each component of a by b.
Definition Vector3.h:223
friend constexpr __host__ __device__ ThatClass operator*(const T &a, const ThatClass &b)
Multiplies each component of b by a.
Definition Vector3.h:211
constexpr __host__ __device__ void operator/=(const T &b)
Divides each component of the instance by b.
Definition Vector3.h:169
constexpr __host__ __device__ void operator*=(const T &b)
Multiplies each component of the instance by b.
Definition Vector3.h:161
constexpr __host__ __device__ Vector3(const T &ax, const T &ay, const T &az)
Constructs the triplet (ax,ay,az).
Definition Vector3.h:57
constexpr Vector3(const std::array< T, 3 > &v)
Constructs the instance with the array v.
Definition Vector3.h:73
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Subtracts each component of a by each component of b.
Definition Vector3.h:195
constexpr __host__ __device__ ThatClass operator-() const
Returns the opposite of the instance.
Definition Vector3.h:207
friend constexpr __host__ __device__ ThatClass operator+(const T &a, const ThatClass &b)
Returns b by adding a to each component.
Definition Vector3.h:189
constexpr __host__ __device__ void operator-=(const ThatClass &b)
Subtracts b from the instance.
Definition Vector3.h:153
constexpr Vector3 & operator=(std::initializer_list< T > v)
Positions the instance with the list v.
Definition Vector3.h:87
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &a, const T &b)
Multiplies each component of a by b.
Definition Vector3.h:217
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const T &b)
Returns a by adding b to each component.
Definition Vector3.h:183
constexpr __host__ __device__ void operator+=(const ThatClass &b)
Adds b to the instance.
Definition Vector3.h:137
constexpr __host__ __device__ void operator+=(const T &b)
Adds b to the instance.
Definition Vector3.h:129
constexpr __host__ __device__ Vector3()=default
Constructs the zero vector.
constexpr __host__ __device__ Vector3(const T &v)
Constructs the instance with the triplet (v,v,v).
Definition Vector3.h:65
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const T &b)
Subtracts each component of a by b.
Definition Vector3.h:201
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Sums component by component of a and b.
Definition Vector3.h:177
-- 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