Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Real3.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/* Real3.h (C) 2000-2026 */
9/* */
10/* Vector of 3 dimensions of 'Real'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_REAL3_H
13#define ARCANE_UTILS_REAL3_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Numeric.h"
18#include "arcane/utils/Real2.h"
19
20#include <iosfwd>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
32{
33 public:
34
38
48 ARCCORE_HOST_DEVICE Real operator[](Integer i) const
49 {
50 ARCCORE_CHECK_AT(i, 3);
51 return (&x)[i];
52 }
53
63 ARCCORE_HOST_DEVICE Real operator()(Integer i) const
64 {
65 ARCCORE_CHECK_AT(i, 3);
66 return (&x)[i];
67 }
68
78 ARCCORE_HOST_DEVICE Real& operator[](Integer i)
79 {
80 ARCCORE_CHECK_AT(i, 3);
81 return (&x)[i];
82 }
83
93 ARCCORE_HOST_DEVICE Real& operator()(Integer i)
94 {
95 ARCCORE_CHECK_AT(i, 3);
96 return (&x)[i];
97 }
98
100 ARCCORE_HOST_DEVICE void setComponent(Integer i, Real value)
101 {
102 ARCCORE_CHECK_AT(i, 3);
103 (&x)[i] = value;
104 }
105};
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
129
130class ARCANE_UTILS_EXPORT Real3
131: public Real3POD
132{
133 public:
134
136 constexpr ARCCORE_HOST_DEVICE Real3()
137 : Real3POD()
138 {
139 x = 0.0;
140 y = 0.0;
141 z = 0.0;
142 }
143
145 constexpr ARCCORE_HOST_DEVICE Real3(Real ax, Real ay, Real az)
146 : Real3POD()
147 {
148 x = ax;
149 y = ay;
150 z = az;
151 }
152
154 Real3(const Real3& f) = default;
155
157 constexpr ARCCORE_HOST_DEVICE explicit Real3(const Real3POD& f)
158 : Real3POD()
159 {
160 x = f.x;
161 y = f.y;
162 z = f.z;
163 }
164
166 constexpr ARCCORE_HOST_DEVICE explicit Real3(Real v)
167 : Real3POD()
168 {
169 x = y = z = v;
170 }
171
173 constexpr ARCCORE_HOST_DEVICE explicit Real3(const Real2& f)
174 : Real3POD()
175 {
176 x = f.x;
177 y = f.y;
178 z = 0.0;
179 }
180
182 constexpr ARCCORE_HOST_DEVICE Real3(ConstArrayView<Real> av)
183 : Real3POD()
184 {
185 x = av[0];
186 y = av[1];
187 z = av[2];
188 }
189
191 Real3& operator=(const Real3& f) = default;
192
194 constexpr ARCCORE_HOST_DEVICE Real3& operator=(Real v)
195 {
196 x = y = z = v;
197 return (*this);
198 }
199
200 public:
201
202 constexpr ARCCORE_HOST_DEVICE static Real3 null() { return Real3(0., 0., 0.); }
203 constexpr ARCCORE_HOST_DEVICE static Real3 zero() { return Real3(0., 0., 0.); }
204
205 public:
206
208 constexpr ARCCORE_HOST_DEVICE Real3 copy() const { return (*this); }
209
211 constexpr ARCCORE_HOST_DEVICE Real3& reset()
212 {
213 x = y = z = 0.;
214 return (*this);
215 }
216
218 constexpr ARCCORE_HOST_DEVICE Real3& assign(Real ax, Real ay, Real az)
219 {
220 x = ax;
221 y = ay;
222 z = az;
223 return (*this);
224 }
225
227 constexpr ARCCORE_HOST_DEVICE Real3& assign(Real3 f)
228 {
229 x = f.x;
230 y = f.y;
231 z = f.z;
232 return (*this);
233 }
234
236 constexpr ARCCORE_HOST_DEVICE ArrayView<Real> view()
237 {
238 return { 3, &x };
239 }
240
242 constexpr ARCCORE_HOST_DEVICE ConstArrayView<Real> constView() const
243 {
244 return { 3, &x };
245 }
246
248 ARCCORE_HOST_DEVICE Real3 absolute() const { return Real3(math::abs(x), math::abs(y), math::abs(z)); }
249
254 std::istream& assign(std::istream& i);
255
257 std::ostream& print(std::ostream& o) const;
258
260 std::ostream& printXyz(std::ostream& o) const;
261
263 constexpr ARCCORE_HOST_DEVICE Real3& add(Real3 b)
264 {
265 x += b.x;
266 y += b.y;
267 z += b.z;
268 return (*this);
269 }
270
272 constexpr ARCCORE_HOST_DEVICE Real3& sub(Real3 b)
273 {
274 x -= b.x;
275 y -= b.y;
276 z -= b.z;
277 return (*this);
278 }
279
281 constexpr ARCCORE_HOST_DEVICE Real3& mul(Real3 b)
282 {
283 x *= b.x;
284 y *= b.y;
285 z *= b.z;
286 return (*this);
287 }
288
290 constexpr ARCCORE_HOST_DEVICE Real3& div(Real3 b)
291 {
292 x /= b.x;
293 y /= b.y;
294 z /= b.z;
295 return (*this);
296 }
297
299 constexpr ARCCORE_HOST_DEVICE Real3& addSame(Real b)
300 {
301 x += b;
302 y += b;
303 z += b;
304 return (*this);
305 }
306
308 constexpr ARCCORE_HOST_DEVICE Real3& subSame(Real b)
309 {
310 x -= b;
311 y -= b;
312 z -= b;
313 return (*this);
314 }
315
317 constexpr ARCCORE_HOST_DEVICE Real3& mulSame(Real b)
318 {
319 x *= b;
320 y *= b;
321 z *= b;
322 return (*this);
323 }
324
326 constexpr ARCCORE_HOST_DEVICE Real3& divSame(Real b)
327 {
328 x /= b;
329 y /= b;
330 z /= b;
331 return (*this);
332 }
333
335 constexpr ARCCORE_HOST_DEVICE Real3& operator+=(Real3 b) { return add(b); }
336
338 constexpr ARCCORE_HOST_DEVICE Real3& operator-=(Real3 b) { return sub(b); }
339
341 constexpr ARCCORE_HOST_DEVICE Real3& operator*=(Real3 b) { return mul(b); }
342
344 constexpr ARCCORE_HOST_DEVICE void operator*=(Real b)
345 {
346 x *= b;
347 y *= b;
348 z *= b;
349 }
350
352 constexpr ARCCORE_HOST_DEVICE Real3& operator/=(Real3 b) { return div(b); }
353
355 constexpr ARCCORE_HOST_DEVICE void operator/=(Real b)
356 {
357 x /= b;
358 y /= b;
359 z /= b;
360 }
361
363 constexpr ARCCORE_HOST_DEVICE Real3 operator+(Real3 b) const { return Real3(x + b.x, y + b.y, z + b.z); }
364
366 constexpr ARCCORE_HOST_DEVICE Real3 operator-(Real3 b) const { return Real3(x - b.x, y - b.y, z - b.z); }
367
369 constexpr ARCCORE_HOST_DEVICE Real3 operator-() const { return Real3(-x, -y, -z); }
370
375 constexpr ARCCORE_HOST_DEVICE Real3 operator*(Real3 b) const { return Real3(x * b.x, y * b.y, z * b.z); }
376
381 constexpr ARCCORE_HOST_DEVICE Real3 operator/(Real3 b) const { return Real3(x / b.x, y / b.y, z / b.z); }
382
384 friend constexpr ARCCORE_HOST_DEVICE Real3 operator*(Real sca, Real3 vec)
385 {
386 return Real3(vec.x * sca, vec.y * sca, vec.z * sca);
387 }
388
390 friend constexpr ARCCORE_HOST_DEVICE Real3 operator*(Real3 vec, Real sca)
391 {
392 return Real3(vec.x * sca, vec.y * sca, vec.z * sca);
393 }
394
396 friend constexpr ARCCORE_HOST_DEVICE Real3 operator/(Real3 vec, Real sca)
397 {
398 return Real3(vec.x / sca, vec.y / sca, vec.z / sca);
399 }
400
401 public:
402
409 friend constexpr ARCCORE_HOST_DEVICE bool operator<(Real3 v1, Real3 v2)
410 {
411 if (v1.x == v2.x) {
412 if (v1.y == v2.y)
413 return v1.z < v2.z;
414 else
415 return v1.y < v2.y;
416 }
417 return (v1.x < v2.x);
418 }
419
421 friend std::ostream& operator<<(std::ostream& o, Real3 t)
422 {
423 return t.printXyz(o);
424 }
425
427 friend std::istream& operator>>(std::istream& i, Real3& t)
428 {
429 return t.assign(i);
430 }
431
438 constexpr ARCCORE_HOST_DEVICE bool operator==(Real3 b) const
439 {
440 return _eq(x, b.x) && _eq(y, b.y) && _eq(z, b.z);
441 }
442
449 constexpr ARCCORE_HOST_DEVICE bool operator!=(Real3 b) const { return !operator==(b); }
450
451 public:
452
454 // TODO: deprecate mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::squareNormL2(const Real3&) instead")
455 constexpr ARCCORE_HOST_DEVICE Real squareNormL2() const { return x * x + y * y + z * z; }
456
458 // TODO: deprecate mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::normL2(const Real3&) instead")
459 inline ARCCORE_HOST_DEVICE Real normL2() const;
460
462 ARCCORE_DEPRECATED_2021("Use math::squareNormL2(const Real3&) instead")
463 constexpr ARCCORE_HOST_DEVICE Real abs2() const { return x * x + y * y + z * z; }
464
466 ARCCORE_DEPRECATED_2021("Use math::normL2(const Real3&) instead")
467 inline ARCCORE_HOST_DEVICE Real abs() const;
468
469 // TODO: deprecate mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::isNearlyZero(const Real3&) instead")
470 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const;
471
472 // TODO: deprecate mid-2026: ARCANE_DEPRECATED_REASON("Y2024: Use math::mutableNormalize(Real3&) instead")
473 inline Real3& normalize();
474
475 private:
476
482 inline constexpr ARCCORE_HOST_DEVICE static bool _eq(Real a, Real b);
483
485 inline ARCCORE_HOST_DEVICE static Real _sqrt(Real a);
486};
487
488/*---------------------------------------------------------------------------*/
489/*---------------------------------------------------------------------------*/
490
491inline constexpr ARCCORE_HOST_DEVICE Real2::
492Real2(const Real3& v)
493: Real2POD()
494{
495 x = v.x;
496 y = v.y;
497}
498
499/*---------------------------------------------------------------------------*/
500/*---------------------------------------------------------------------------*/
501
502namespace math
503{
505 inline constexpr ARCCORE_HOST_DEVICE Real squareNormL2(const Real3& v)
506 {
507 return v.x * v.x + v.y * v.y + v.z * v.z;
508 }
509
516 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero(const Real3& v)
517 {
518 return math::isNearlyZero(v.x) && math::isNearlyZero(v.y) && math::isNearlyZero(v.z);
519 }
520
522 inline ARCCORE_HOST_DEVICE Real normL2(const Real3& v)
523 {
525 }
526
535 {
536 Real d = math::normL2(v);
537 if (!math::isZero(d))
538 v.divSame(d);
539 return v;
540 }
541
548 inline Real3 normalizeL2(const Real3& v)
549 {
550 Real d = math::normL2(v);
551 if (!math::isZero(d))
552 return v / d;
553 return v;
554 }
555} // namespace math
556
557/*---------------------------------------------------------------------------*/
558/*---------------------------------------------------------------------------*/
559
560inline Real3& Real3::
561normalize()
562{
563 return math::mutableNormalize(*this);
564}
565
566inline constexpr ARCCORE_HOST_DEVICE bool Real3::
567isNearlyZero() const
568{
569 return math::isNearlyZero(*this);
570}
571
572inline constexpr ARCCORE_HOST_DEVICE bool Real3::
573_eq(Real a, Real b)
574{
575 return math::isEqual(a, b);
576}
577
578ARCCORE_HOST_DEVICE inline Real Real3::
579_sqrt(Real a)
580{
581 return math::sqrt(a);
582}
583
584inline ARCCORE_HOST_DEVICE Real Real3::
585normL2() const
586{
587 return math::normL2(*this);
588}
589
590inline ARCCORE_HOST_DEVICE Real Real3::
591abs() const
592{
593 return math::normL2(*this);
594}
595
596/*---------------------------------------------------------------------------*/
597/*---------------------------------------------------------------------------*/
598
599} // End namespace Arcane
600
601/*---------------------------------------------------------------------------*/
602/*---------------------------------------------------------------------------*/
603
604#endif
Modifiable view of an array of type T.
Constant view of an array of type T.
Class managing a 2-dimensional real vector.
Definition Real2.h:122
constexpr __host__ __device__ Real2()
Constructs the zero vector.
Definition Real2.h:126
Class managing a 3-dimensional real vector.
Definition Real3.h:132
constexpr __host__ __device__ void operator*=(Real b)
Multiplies each component of the triplet by the real number b.
Definition Real3.h:344
Real3(const Real3 &f)=default
Constructs a triplet identical to f.
friend constexpr __host__ __device__ Real3 operator/(Real3 vec, Real sca)
Division by a scalar.
Definition Real3.h:396
Real3 & operator=(const Real3 &f)=default
Copy assignment operator.
constexpr __host__ __device__ Real3 & operator/=(Real3 b)
Divides each component of the triplet by the corresponding component of b.
Definition Real3.h:352
constexpr __host__ __device__ Real3 operator*(Real3 b) const
Creates a triplet that equals this triplet whose each component has been multiplied by the correspond...
Definition Real3.h:375
constexpr __host__ __device__ Real3(Real ax, Real ay, Real az)
Constructs the triplet (ax,ay,az).
Definition Real3.h:145
constexpr __host__ static __device__ bool _eq(Real a, Real b)
Compares the values of a and b using the TypeEqualT comparator.
Definition Real3.h:573
constexpr __host__ __device__ ArrayView< Real > view()
Returns a view of the three elements of the vector.
Definition Real3.h:236
constexpr __host__ __device__ Real3 & subSame(Real b)
Subtracts b from each component of the triplet.
Definition Real3.h:308
friend constexpr __host__ __device__ Real3 operator*(Real3 vec, Real sca)
Multiplication by a scalar.
Definition Real3.h:390
constexpr __host__ __device__ Real3 operator-() const
Creates a triplet opposite to the current triplet.
Definition Real3.h:369
constexpr __host__ __device__ Real3 operator+(Real3 b) const
Creates a triplet that equals this triplet added to b.
Definition Real3.h:363
constexpr __host__ __device__ Real3 & operator*=(Real3 b)
Multiplies each component of the triplet by the corresponding component of b.
Definition Real3.h:341
constexpr __host__ __device__ bool operator!=(Real3 b) const
Compares two triplets. For the notion of equality, see operator==().
Definition Real3.h:449
constexpr __host__ __device__ Real3 operator/(Real3 b) const
Creates a triplet that equals this triplet whose each component has been divided by the corresponding...
Definition Real3.h:381
constexpr __host__ __device__ bool operator==(Real3 b) const
Compares the current instance component by component to b.
Definition Real3.h:438
constexpr __host__ __device__ Real3 & mulSame(Real b)
Multiplies each component of the triplet by b.
Definition Real3.h:317
constexpr __host__ __device__ Real3 & operator=(Real v)
Assigns the triplet (v,v,v) to the instance.
Definition Real3.h:194
constexpr __host__ __device__ Real3 & add(Real3 b)
Adds b to the triplet.
Definition Real3.h:263
constexpr __host__ __device__ Real3(const Real3POD &f)
Constructs a triplet identical to f.
Definition Real3.h:157
friend constexpr __host__ __device__ Real3 operator*(Real sca, Real3 vec)
Multiplication by a scalar.
Definition Real3.h:384
constexpr __host__ __device__ Real3 & addSame(Real b)
Adds b to each component of the triplet.
Definition Real3.h:299
constexpr __host__ __device__ Real3(Real v)
Constructs the instance with the triplet (v,v,v).
Definition Real3.h:166
constexpr __host__ __device__ ConstArrayView< Real > constView() const
Returns a constant view of the three elements of the vector.
Definition Real3.h:242
constexpr __host__ __device__ void operator/=(Real b)
Divides each component of the triplet by the real number b.
Definition Real3.h:355
std::ostream & printXyz(std::ostream &o) const
Writes the triplet to the stream o in the form (x,y,z).
Definition Real3.cc:49
constexpr __host__ __device__ Real3 & operator-=(Real3 b)
Subtracts b from the triplet.
Definition Real3.h:338
constexpr __host__ __device__ Real3(ConstArrayView< Real > av)
Constructs the triplet (av[0], av[1], av[2]).
Definition Real3.h:182
friend constexpr __host__ __device__ bool operator<(Real3 v1, Real3 v2)
Comparison operator.
Definition Real3.h:409
constexpr __host__ __device__ Real squareNormL2() const
Returns the square of the L2 norm of the triplet $ .
Definition Real3.h:455
friend std::istream & operator>>(std::istream &i, Real3 &t)
Reads the triplet t from the stream o.
Definition Real3.h:427
constexpr __host__ __device__ Real3(const Real2 &f)
Constructs a triplet identical to f.
Definition Real3.h:173
constexpr __host__ __device__ Real3 copy() const
Returns a copy of the triplet.
Definition Real3.h:208
constexpr __host__ __device__ Real3 & reset()
Resets the triplet using default constructors.
Definition Real3.h:211
constexpr __host__ __device__ Real3 & divSame(Real b)
Divides each component of the triplet by b.
Definition Real3.h:326
constexpr __host__ __device__ Real3()
Constructs the zero vector.
Definition Real3.h:136
constexpr __host__ __device__ Real3 & sub(Real3 b)
Subtracts b from the triplet.
Definition Real3.h:272
__host__ __device__ Real3 absolute() const
Absolute value component by component.
Definition Real3.h:248
constexpr __host__ __device__ Real3 & operator+=(Real3 b)
Adds b to the triplet.
Definition Real3.h:335
constexpr __host__ __device__ Real3 operator-(Real3 b) const
Creates a triplet that equals b subtracted from this triplet.
Definition Real3.h:366
constexpr __host__ __device__ Real3 & div(Real3 b)
Divides each component of the triplet by the corresponding component of b.
Definition Real3.h:290
constexpr __host__ __device__ Real3 & assign(Real ax, Real ay, Real az)
Assigns the triplet (ax,ay,az) to the instance.
Definition Real3.h:218
constexpr __host__ __device__ Real abs2() const
Returns the square of the norm of the triplet $ .
Definition Real3.h:463
constexpr __host__ __device__ Real3 & assign(Real3 f)
Copies the triplet f.
Definition Real3.h:227
friend std::ostream & operator<<(std::ostream &o, Real3 t)
Writes the triplet t to the stream o.
Definition Real3.h:421
constexpr __host__ __device__ Real3 & mul(Real3 b)
Multiplies each component of the triplet by the corresponding component of b.
Definition Real3.h:281
Namespace for mathematical functions.
Definition MathUtils.h:36
Real2 normalizeL2(const Real2 &v)
Returns the pair v normalized by the L2 norm.
Definition Real2.h:508
constexpr __host__ __device__ Real squareNormL2(const Real2 &v)
Returns the squared norm of the pair $ .
Definition Real2.h:476
__host__ __device__ double sqrt(double v)
Square root of v.
Definition Math.h:142
bool isZero(const BuiltInProxy< _Type > &a)
Tests if a value is exactly equal to zero.
Real2 & mutableNormalize(Real2 &v)
Normalizes the pair.
Definition Real2.h:494
constexpr __host__ __device__ bool isEqual(const _Type &a, const _Type &b)
Tests the bit-by-bit equality between two values.
Definition Numeric.h:260
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
Real y
second component of the pair
Definition Real2.h:35
Real x
first component of the pair
Definition Real2.h:34
__host__ __device__ Real & operator()(Integer i)
Definition Real3.h:93
__host__ __device__ Real operator[](Integer i) const
Definition Real3.h:48
Real y
second component of the triplet
Definition Real3.h:36
Real z
third component of the triplet
Definition Real3.h:37
__host__ __device__ Real operator()(Integer i) const
Definition Real3.h:63
__host__ __device__ void setComponent(Integer i, Real value)
Sets the i-th component to value.
Definition Real3.h:100
__host__ __device__ Real & operator[](Integer i)
Definition Real3.h:78
Real x
first component of the triplet
Definition Real3.h:35