Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Real2.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/* Real2.h (C) 2000-2026 */
9/* */
10/* 2-dimensional vector of 'Real'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_REAL2_H
13#define ARCANE_UTILS_REAL2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Numeric.h"
18
19#include <iosfwd>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
31{
32 public:
33
36
46 ARCCORE_HOST_DEVICE Real operator[](Integer i) const
47 {
48 ARCCORE_CHECK_AT(i, 2);
49 return (&x)[i];
50 }
51
61 ARCCORE_HOST_DEVICE Real operator()(Integer i) const
62 {
63 ARCCORE_CHECK_AT(i, 2);
64 return (&x)[i];
65 }
66
76 ARCCORE_HOST_DEVICE Real& operator[](Integer i)
77 {
78 ARCCORE_CHECK_AT(i, 2);
79 return (&x)[i];
80 }
81
91 ARCCORE_HOST_DEVICE Real& operator()(Integer i)
92 {
93 ARCCORE_CHECK_AT(i, 2);
94 return (&x)[i];
95 }
96
98 ARCCORE_HOST_DEVICE void setComponent(Integer i, Real value)
99 {
100 ARCCORE_CHECK_AT(i, 2);
101 (&x)[i] = value;
102 }
103};
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
120class ARCANE_UTILS_EXPORT Real2
121: public Real2POD
122{
123 public:
124
126 constexpr ARCCORE_HOST_DEVICE Real2()
127 : Real2POD()
128 {
129 x = 0.;
130 y = 0.;
131 }
132
133 constexpr ARCCORE_HOST_DEVICE Real2(Real ax, Real ay)
134 : Real2POD()
135 {
136 x = ax;
137 y = ay;
138 }
139
140 Real2(const Real2& f) = default;
142 constexpr ARCCORE_HOST_DEVICE explicit Real2(const Real2POD& f)
143 : Real2POD()
144 {
145 x = f.x;
146 y = f.y;
147 }
148
150 constexpr ARCCORE_HOST_DEVICE explicit Real2(Real v)
151 : Real2POD()
152 {
153 x = y = v;
154 }
155
157 inline constexpr ARCCORE_HOST_DEVICE explicit Real2(const Real3& v);
158
160 constexpr ARCCORE_HOST_DEVICE Real2(ConstArrayView<Real> av)
161 : Real2POD()
162 {
163 x = av[0];
164 y = av[1];
165 }
166
167 Real2& operator=(const Real2& f) = default;
168
170 constexpr ARCCORE_HOST_DEVICE Real2& operator=(Real v)
171 {
172 x = y = v;
173 return (*this);
174 }
175
176 public:
177
178 constexpr ARCCORE_HOST_DEVICE static Real2 null() { return Real2(0., 0.); }
179
180 public:
181
183 constexpr ARCCORE_HOST_DEVICE Real2 copy() const { return (*this); }
184
186 constexpr ARCCORE_HOST_DEVICE Real2& reset()
187 {
188 x = y = 0.0;
189 return (*this);
190 }
191
193 constexpr ARCCORE_HOST_DEVICE Real2& assign(Real ax, Real ay)
194 {
195 x = ax;
196 y = ay;
197 return (*this);
198 }
199
201 constexpr ARCCORE_HOST_DEVICE Real2& assign(Real2 f)
202 {
203 x = f.x;
204 y = f.y;
205 return (*this);
206 }
207
209 constexpr ARCCORE_HOST_DEVICE ArrayView<Real> view()
210 {
211 return { 2, &x };
212 }
213
215 constexpr ARCCORE_HOST_DEVICE ConstArrayView<Real> constView() const
216 {
217 return { 2, &x };
218 }
219
221 ARCCORE_HOST_DEVICE Real2 absolute() const { return Real2(math::abs(x), math::abs(y)); }
222
227 std::istream& assign(std::istream& i);
228
230 std::ostream& print(std::ostream& o) const;
231
233 std::ostream& printXy(std::ostream& o) const;
234
236 constexpr ARCCORE_HOST_DEVICE Real2& add(Real2 b)
237 {
238 x += b.x;
239 y += b.y;
240 return (*this);
241 }
242
244 constexpr ARCCORE_HOST_DEVICE Real2& sub(Real2 b)
245 {
246 x -= b.x;
247 y -= b.y;
248 return (*this);
249 }
250
252 constexpr ARCCORE_HOST_DEVICE Real2& mul(Real2 b)
253 {
254 x *= b.x;
255 y *= b.y;
256 return (*this);
257 }
258
260 constexpr ARCCORE_HOST_DEVICE Real2& div(Real2 b)
261 {
262 x /= b.x;
263 y /= b.y;
264 return (*this);
265 }
266
268 constexpr ARCCORE_HOST_DEVICE Real2& addSame(Real b)
269 {
270 x += b;
271 y += b;
272 return (*this);
273 }
274
276 constexpr ARCCORE_HOST_DEVICE Real2& subSame(Real b)
277 {
278 x -= b;
279 y -= b;
280 return (*this);
281 }
282
284 constexpr ARCCORE_HOST_DEVICE Real2& mulSame(Real b)
285 {
286 x *= b;
287 y *= b;
288 return (*this);
289 }
290
292 constexpr ARCCORE_HOST_DEVICE Real2& divSame(Real b)
293 {
294 x /= b;
295 y /= b;
296 return (*this);
297 }
298
300 constexpr ARCCORE_HOST_DEVICE Real2& operator+=(Real2 b) { return add(b); }
301
303 constexpr ARCCORE_HOST_DEVICE Real2& operator-=(Real2 b) { return sub(b); }
304
306 constexpr ARCCORE_HOST_DEVICE Real2& operator*=(Real2 b) { return mul(b); }
307
309 constexpr ARCCORE_HOST_DEVICE void operator*=(Real b)
310 {
311 x *= b;
312 y *= b;
313 }
314
316 constexpr ARCCORE_HOST_DEVICE Real2& operator/=(Real2 b) { return div(b); }
317
319 constexpr ARCCORE_HOST_DEVICE void operator/=(Real b)
320 {
321 x /= b;
322 y /= b;
323 }
324
326 constexpr ARCCORE_HOST_DEVICE Real2 operator+(Real2 b) const { return Real2(x + b.x, y + b.y); }
327
329 constexpr ARCCORE_HOST_DEVICE Real2 operator-(Real2 b) const { return Real2(x - b.x, y - b.y); }
330
332 constexpr ARCCORE_HOST_DEVICE Real2 operator-() const { return Real2(-x, -y); }
333
338 constexpr ARCCORE_HOST_DEVICE Real2 operator*(Real2 b) const { return Real2(x * b.x, y * b.y); }
339
344 constexpr ARCCORE_HOST_DEVICE Real2 operator/(Real2 b) const { return Real2(x / b.x, y / b.y); }
345
347 friend constexpr ARCCORE_HOST_DEVICE Real2 operator*(Real sca, Real2 vec)
348 {
349 return Real2(vec.x * sca, vec.y * sca);
350 }
351
353 friend constexpr ARCCORE_HOST_DEVICE Real2 operator*(Real2 vec, Real sca)
354 {
355 return Real2(vec.x * sca, vec.y * sca);
356 }
357
359 friend constexpr ARCCORE_HOST_DEVICE Real2 operator/(Real2 vec, Real sca)
360 {
361 return Real2(vec.x / sca, vec.y / sca);
362 }
363
370 friend constexpr ARCCORE_HOST_DEVICE bool operator<(Real2 v1, Real2 v2)
371 {
372 if (v1.x == v2.x) {
373 return v1.y < v2.y;
374 }
375 return (v1.x < v2.x);
376 }
377
379 friend std::ostream& operator<<(std::ostream& o, Real2 t)
380 {
381 return t.printXy(o);
382 }
383
385 friend std::istream& operator>>(std::istream& i, Real2& t)
386 {
387 return t.assign(i);
388 }
389
396 constexpr ARCCORE_HOST_DEVICE bool operator==(Real2 b) const
397 {
398 return _eq(x, b.x) && _eq(y, b.y);
399 }
400
407 constexpr ARCCORE_HOST_DEVICE bool operator!=(Real2 b) const { return !operator==(b); }
408
409 public:
410
412 // TODO: make obsolete mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::squareNormL2(*this) instead")
413 constexpr ARCCORE_HOST_DEVICE Real squareNormL2() const { return x * x + y * y; }
414
416 ARCCORE_DEPRECATED_2021("Use math::squareNormL2(*this) instead")
417 ARCCORE_HOST_DEVICE Real abs2() const { return x * x + y * y; }
418
420 ARCCORE_DEPRECATED_2021("Use math::normL2(*this) instead")
421 inline ARCCORE_HOST_DEVICE Real abs() const;
422
429 // TODO: make obsolete mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::isNearlyZero(const Real2&) instead")
430 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const;
431
433 // TODO: make obsolete mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::normL2(const Real2&) instead")
434 ARCCORE_HOST_DEVICE Real normL2() const;
435
436 // TODO: make obsolete mid-2026 ARCANE_DEPRECATED_REASON("Y2026: Use math::mutableNormalize(Real2&) instead")
444 inline Real2& normalize();
445
446 private:
447
453 constexpr ARCCORE_HOST_DEVICE static bool _eq(Real a, Real b);
454
456 ARCCORE_HOST_DEVICE static Real _sqrt(Real a);
457};
458
459/*---------------------------------------------------------------------------*/
460/*---------------------------------------------------------------------------*/
461
462namespace math
463{
470 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero(const Real2& v)
471 {
472 return math::isNearlyZero(v.x) && math::isNearlyZero(v.y);
473 }
474
476 inline constexpr ARCCORE_HOST_DEVICE Real squareNormL2(const Real2& v)
477 {
478 return v.x * v.x + v.y * v.y;
479 }
480
482 inline ARCCORE_HOST_DEVICE Real normL2(const Real2& v)
483 {
485 }
486
495 {
496 Real d = math::normL2(v);
497 if (!math::isZero(d))
498 v.divSame(d);
499 return v;
500 }
501
508 inline Real2 normalizeL2(const Real2& v)
509 {
510 Real d = math::normL2(v);
511 if (!math::isZero(d))
512 return v / d;
513 return v;
514 }
515} // namespace math
516
517/*---------------------------------------------------------------------------*/
518/*---------------------------------------------------------------------------*/
519
520inline constexpr ARCCORE_HOST_DEVICE bool Real2::
521isNearlyZero() const
522{
523 return math::isNearlyZero(*this);
524}
525
526inline constexpr ARCCORE_HOST_DEVICE bool Real2::
527_eq(Real a, Real b)
528{
529 return math::isEqual(a, b);
530}
531
532inline ARCCORE_HOST_DEVICE Real Real2::
533_sqrt(Real a)
534{
535 return math::sqrt(a);
536}
537
538inline ARCCORE_HOST_DEVICE Real Real2::
539normL2() const
540{
541 return math::normL2(*this);
542}
543
544inline Real2& Real2::
545normalize()
546{
547 return math::mutableNormalize(*this);
548}
549
550inline ARCCORE_HOST_DEVICE Real Real2::
551abs() const
552{
553 return math::normL2(*this);
554}
555
556/*---------------------------------------------------------------------------*/
557/*---------------------------------------------------------------------------*/
558
559} // End namespace Arcane
560
561/*---------------------------------------------------------------------------*/
562/*---------------------------------------------------------------------------*/
563
564#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 operator-() const
Creates a pair opposite to the current pair.
Definition Real2.h:332
std::ostream & printXy(std::ostream &o) const
Writes the pair to stream o in the form (x,y).
Definition Real2.cc:47
constexpr __host__ __device__ Real2 & operator-=(Real2 b)
Subtracts b from the pair.
Definition Real2.h:303
constexpr __host__ __device__ bool operator!=(Real2 b) const
Compares two pairs. For the concept of equality, see operator==().
Definition Real2.h:407
constexpr __host__ __device__ Real2 & operator=(Real v)
Assigns the pair (v,v) to the instance.
Definition Real2.h:170
constexpr __host__ __device__ Real squareNormL2() const
Returns the squared norm of the pair $ .
Definition Real2.h:413
constexpr __host__ __device__ bool operator==(Real2 b) const
Compares the current instance component by component to b.
Definition Real2.h:396
constexpr __host__ __device__ Real2 & addSame(Real b)
Adds b to each component of the pair.
Definition Real2.h:268
friend std::ostream & operator<<(std::ostream &o, Real2 t)
Writes the pair t to the stream o.
Definition Real2.h:379
constexpr __host__ __device__ Real2 & reset()
Resets the pair using default constructors.
Definition Real2.h:186
constexpr __host__ __device__ Real2()
Constructs the zero vector.
Definition Real2.h:126
friend std::istream & operator>>(std::istream &i, Real2 &t)
Reads the pair t from the stream o.
Definition Real2.h:385
constexpr __host__ __device__ Real2 & operator/=(Real2 b)
Divides each component of the pair by the corresponding component of b.
Definition Real2.h:316
__host__ __device__ Real2 absolute() const
Absolute value component by component.
Definition Real2.h:221
constexpr __host__ __device__ Real2 & operator*=(Real2 b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2.h:306
constexpr __host__ __device__ Real2 & sub(Real2 b)
Subtracts b from the pair.
Definition Real2.h:244
constexpr __host__ __device__ Real2 operator*(Real2 b) const
Creates a pair that equals this pair, where each component has been multiplied by the corresponding c...
Definition Real2.h:338
constexpr __host__ __device__ Real2(Real v)
Constructs the instance with the triplet (v,v,v).
Definition Real2.h:150
constexpr __host__ __device__ Real2(ConstArrayView< Real > av)
Constructs the pair (av[0], av[1]).
Definition Real2.h:160
constexpr __host__ __device__ Real2 & subSame(Real b)
Subtracts b from each component of the pair.
Definition Real2.h:276
constexpr __host__ __device__ Real2 & assign(Real2 f)
Copies the pair f.
Definition Real2.h:201
constexpr __host__ __device__ Real2 & div(Real2 b)
Divides each component of the pair by the corresponding component of b.
Definition Real2.h:260
constexpr __host__ __device__ Real2 & mul(Real2 b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2.h:252
constexpr __host__ __device__ ConstArrayView< Real > constView() const
Returns a constant view of the two elements of the vector.
Definition Real2.h:215
constexpr __host__ __device__ Real2 & add(Real2 b)
Adds b to the pair.
Definition Real2.h:236
constexpr __host__ static __device__ bool _eq(Real a, Real b)
Compares the values of a and b using the TypeEqualT comparator.
Definition Real2.h:527
constexpr __host__ __device__ ArrayView< Real > view()
Returns a view of the two elements of the vector.
Definition Real2.h:209
constexpr __host__ __device__ Real2 operator/(Real2 b) const
Creates a pair that equals this pair, where each component has been divided by the corresponding comp...
Definition Real2.h:344
friend constexpr __host__ __device__ Real2 operator*(Real sca, Real2 vec)
Multiplication by a scalar.
Definition Real2.h:347
constexpr __host__ __device__ Real2 operator+(Real2 b) const
Creates a pair that equals this pair added to b.
Definition Real2.h:326
constexpr __host__ __device__ Real2(const Real2POD &f)
Constructs a copy identical to f.
Definition Real2.h:142
constexpr __host__ __device__ Real2 operator-(Real2 b) const
Creates a pair that equals b subtracted from this pair.
Definition Real2.h:329
constexpr __host__ __device__ Real2 copy() const
Returns a copy of the pair.
Definition Real2.h:183
constexpr __host__ __device__ void operator/=(Real b)
Divides each component of the pair by the real number b.
Definition Real2.h:319
__host__ __device__ Real abs2() const
Returns the squared norm of the pair $ .
Definition Real2.h:417
friend constexpr __host__ __device__ Real2 operator/(Real2 vec, Real sca)
Division by a scalar.
Definition Real2.h:359
friend constexpr __host__ __device__ bool operator<(Real2 v1, Real2 v2)
Comparison operator.
Definition Real2.h:370
constexpr __host__ __device__ Real2 & mulSame(Real b)
Multiplies each component of the pair by b.
Definition Real2.h:284
constexpr __host__ __device__ Real2(Real ax, Real ay)
Constructs the pair (ax,ay).
Definition Real2.h:133
constexpr __host__ __device__ Real2 & operator+=(Real2 b)
Adds b to the pair.
Definition Real2.h:300
constexpr __host__ __device__ void operator*=(Real b)
Multiplies each component of the pair by the real number b.
Definition Real2.h:309
constexpr __host__ __device__ Real2 & assign(Real ax, Real ay)
Assigns the triplet (ax,ay,az) to the instance.
Definition Real2.h:193
constexpr __host__ __device__ Real2 & divSame(Real b)
Divides each component of the pair by b.
Definition Real2.h:292
Real2(const Real2 &f)=default
Constructs a copy identical to f.
friend constexpr __host__ __device__ Real2 operator*(Real2 vec, Real sca)
Multiplication by a scalar.
Definition Real2.h:353
Class managing a 3-dimensional real vector.
Definition Real3.h:132
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) const
Definition Real2.h:46
__host__ __device__ void setComponent(Integer i, Real value)
Positions the i th component at value.
Definition Real2.h:98
__host__ __device__ Real & operator()(Integer i)
Definition Real2.h:91
__host__ __device__ Real operator()(Integer i) const
Definition Real2.h:61
__host__ __device__ Real & operator[](Integer i)
Definition Real2.h:76