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