Arcane  4.2.1.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
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/* Vecteur à 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
31/*!
32 * \brief Classe gérant un vecteur de dimension 3 de type \a T
33 *
34 * Les 3 éléments du vecteur peuvent être accédés par \a x, \a y ou \a z.
35 */
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
52 //! Construit le vecteur nul.
53 constexpr ARCCORE_HOST_DEVICE
54 Vector3() = default;
55
56 //! Construit le triplet (ax,ay,az)
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
64 //! Construit l'instance avec le triplet (v,v,v).
65 constexpr ARCCORE_HOST_DEVICE explicit Vector3(const T& v)
66 : x(v)
67 , y(v)
68 , z(v)
69 {
70 }
71
72 //! Construit l'instance avec le tableau \a v
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
80 //! Construit l'instance avec la liste \a v
81 constexpr Vector3(std::initializer_list<T> v)
82 {
83 _setFromList(v);
84 }
85
86 //! Positionne l'instance avec la liste \a v
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
107 //! Ecrit le triplet \a t sur le flot \a o
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
128 //! Ajoute \a b à l'instance
129 constexpr ARCCORE_HOST_DEVICE void operator+=(const T& b)
130 {
131 x += b;
132 y += b;
133 z += b;
134 }
135
136 //! Ajoute \a b à l'instance
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
144 //! Soustrait \a b de l'instance
145 constexpr ARCCORE_HOST_DEVICE void operator-=(const T& b)
146 {
147 x -= b;
148 y -= b;
149 z -= b;
150 }
151
152 //! Soustrait \a b de l'instance
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
160 //! Multiplie chaque composante de l'instance par \a b
161 constexpr ARCCORE_HOST_DEVICE void operator*=(const T& b)
162 {
163 x *= b;
164 y *= b;
165 z *= b;
166 }
167
168 //! Divise chaque composante de l'instance par \a b
169 constexpr ARCCORE_HOST_DEVICE void operator/=(const T& b)
170 {
171 x /= b;
172 y /= b;
173 z /= b;
174 }
175
176 //! Additionne composante par composante \a a et \a b
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
182 //! Retourne \a a en ajoutant \a b à chaque composante
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
188 //! Retourne \a b en ajoutant \a a à chaque composante
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
194 //! Soustrait chaque composante de \a a par chaque composante de \a b
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
200 //! Soustrait chaque composante de \a a par \a b
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
206 //! Retourne l'opposé de l'instance
207 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const { return ThatClass(-x, -y, -z); }
208
209
210 //! Multiplie chaque composante de \a b par \a a
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
216 //! Multiplie chaque composante de \a a par \a b
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
222 //! Divise chaque composante de \a a par \a b
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
Déclarations des types utilisés dans Arcane.
Classe gérant un vecteur de dimension 3 de type T.
Definition Vector3.h:38
constexpr __host__ __device__ void operator-=(const T &b)
Soustrait b de l'instance.
Definition Vector3.h:145
friend std::ostream & operator<<(std::ostream &o, const Vector3< T > &t)
Ecrit le triplet t sur le flot o.
Definition Vector3.h:108
constexpr Vector3(std::initializer_list< T > v)
Construit l'instance avec la liste v.
Definition Vector3.h:81
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &a, const T &b)
Divise chaque composante de a par b.
Definition Vector3.h:223
friend constexpr __host__ __device__ ThatClass operator*(const T &a, const ThatClass &b)
Multiplie chaque composante de b par a.
Definition Vector3.h:211
constexpr __host__ __device__ void operator/=(const T &b)
Divise chaque composante de l'instance par b.
Definition Vector3.h:169
constexpr __host__ __device__ void operator*=(const T &b)
Multiplie chaque composante de l'instance par b.
Definition Vector3.h:161
constexpr __host__ __device__ Vector3(const T &ax, const T &ay, const T &az)
Construit le triplet (ax,ay,az).
Definition Vector3.h:57
constexpr Vector3(const std::array< T, 3 > &v)
Construit l'instance avec le tableau v.
Definition Vector3.h:73
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Soustrait chaque composante de a par chaque composante de b.
Definition Vector3.h:195
constexpr __host__ __device__ ThatClass operator-() const
Retourne l'opposé de l'instance.
Definition Vector3.h:207
friend constexpr __host__ __device__ ThatClass operator+(const T &a, const ThatClass &b)
Retourne b en ajoutant a à chaque composante.
Definition Vector3.h:189
constexpr __host__ __device__ void operator-=(const ThatClass &b)
Soustrait b de l'instance.
Definition Vector3.h:153
constexpr Vector3 & operator=(std::initializer_list< T > v)
Positionne l'instance avec la liste v.
Definition Vector3.h:87
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &a, const T &b)
Multiplie chaque composante de a par b.
Definition Vector3.h:217
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const T &b)
Retourne a en ajoutant b à chaque composante.
Definition Vector3.h:183
constexpr __host__ __device__ void operator+=(const ThatClass &b)
Ajoute b à l'instance.
Definition Vector3.h:137
constexpr __host__ __device__ void operator+=(const T &b)
Ajoute b à l'instance.
Definition Vector3.h:129
constexpr __host__ __device__ Vector3()=default
Construit le vecteur nul.
constexpr __host__ __device__ Vector3(const T &v)
Construit l'instance avec le triplet (v,v,v).
Definition Vector3.h:65
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const T &b)
Soustrait chaque composante de a par b.
Definition Vector3.h:201
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Additionne composante par composante a et 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 deux entités.
Definition Item.h:551