Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Real2Proxy.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/* Real2Proxy.h (C) 2000-2008 */
9/* */
10/* Proxy of a 'Real2'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_REAL2PROXY_H
13#define ARCANE_UTILS_REAL2PROXY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Real2.h"
18#include "arcane/utils/BuiltInProxy.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*!
30 * \brief Class managing a 2-dimensional real vector.
31
32 The vector comprises two components \a x and \a y which are of type \b Real.
33
34 \code
35 Real2Proxy value (1.0,2.3); // Created a pair (x=1.0, y=2.3)
36 cout << value.x; // Prints the x component
37 value.y += 3.2; // Adds 3.2 to the \b y component
38 \endcode
39 */
40class ARCANE_UTILS_EXPORT Real2Proxy
41{
42 public:
43
44 //! Constructs the pair (ax,ay)
45 Real2Proxy(Real2& value, const MemoryAccessInfo& info)
46 : x(value.x, info)
47 , y(value.y, info)
48 , m_value(value)
49 , m_info(info)
50 {}
51
52 //! Constructs a pair identical to \a f
54 : x(f.x)
55 , y(f.y)
56 , m_value(f.m_value)
57 , m_info(f.m_info)
58 {}
59 const Real2& operator=(Real2Proxy f)
60 {
61 x = f.x;
62 y = f.y;
63 return m_value;
64 }
65 const Real2& operator=(Real2 f)
66 {
67 x = f.x;
68 y = f.y;
69 return m_value;
70 }
71
72 //! Assigns the pair (v,v) to the instance.
74 {
75 x = y = v;
76 return m_value;
77 }
78 operator Real2() const
79 {
80 return getValue();
81 }
82 //operator Real2&()
83 //{
84 // return getValueMutable();
85 //}
86 public:
87
88 RealProxy x; //!< first component of the pair
89 RealProxy y; //!< second component of the pair
90
91 private:
92
93 Real2& m_value;
94 MemoryAccessInfo m_info;
95
96 public:
97
98 //! Returns a copy of the pair.
99 Real2 copy() const { return m_value; }
100
101 //! Resets the pair with default constructors.
103 {
104 x = y = 0.;
105 return (*this);
106 }
107
108 //! Assigns the pair (ax,ay,az) to the instance
110 {
111 x = ax;
112 y = ay;
113 return (*this);
114 }
115
116 //! Copies the pair \a f
118 {
119 x = f.x;
120 y = f.y;
121 return (*this);
122 }
123
124 /*!
125 * \brief Compares the pair with the zero pair.
126 *
127 * In the case of an integral #value_type, the pair is
128 * zero if and only if each of its components is equal to 0.
129 *
130 * For #value_type of the floating point type (float, double or #Real), the pair
131 * is zero if and only if each of its components is less
132 * than a given epsilon. The value of the epsilon used is that
133 * of float_info<value_type>::nearlyEpsilon():
134 * \f[A=0 \Leftrightarrow |A.x|<\epsilon,|A.y|<\epsilon \f]
135 *
136 * \retval true if the pair is equal to the zero pair,
137 * \retval false otherwise.
138 */
139 bool isNearlyZero() const
140 {
141 return math::isNearlyZero(x.getValue()) && math::isNearlyZero(y.getValue());
142 }
143
144 //! Returns the squared norm of the pair \f$x^2+y^2+z^2\f$
145 Real abs2() const
146 {
147 return x * x + y * y;
148 }
149
150 //! Returns the norm of the pair \f$\sqrt{x^2+y^2+z^2}\f$
151 Real abs() const
152 {
153 return _sqrt(abs2());
154 }
155
156 /*!
157 * \brief Reads a pair from the stream \a i
158 * The pair is read in the form of three #value_type values.
159 */
160 istream& assign(istream& i);
161
162 //! Writes the pair to the stream \a o readable by an assign()
163 ostream& print(ostream& o) const;
164
165 //! Writes the pair to the stream \a o in the form (x,y)
166 ostream& printXy(ostream& o) const;
167
168 //! Adds \a b to the pair
170 {
171 x += b.x;
172 y += b.y;
173 return (*this);
174 }
175
176 //! Subtracts \a b from the pair
178 {
179 x -= b.x;
180 y -= b.y;
181 return (*this);
182 }
183
184 //! Multiplies each component of the pair by the corresponding component of \a b
186 {
187 x *= b.x;
188 y *= b.y;
189 return (*this);
190 }
191
192 //! Divides each component of the pair by the corresponding component of \a b
194 {
195 x /= b.x;
196 y /= b.y;
197 return (*this);
198 }
199
200 //! Adds \a b to each component of the pair
202 {
203 x += b;
204 y += b;
205 return (*this);
206 }
207
208 //! Subtracts \a b from each component of the pair
210 {
211 x -= b;
212 y -= b;
213 return (*this);
214 }
215
216 //! Multiplies each component of the pair by \a b
218 {
219 x *= b;
220 y *= b;
221 return (*this);
222 }
223
224 //! Divides each component of the pair by \a b
226 {
227 x /= b;
228 y /= b;
229 return (*this);
230 }
231
232 //! Adds \a b to the pair.
233 Real2Proxy& operator+=(Real2 b) { return add(b); }
234
235 //! Subtracts \a b from the pair
236 Real2Proxy& operator-=(Real2 b) { return sub(b); }
237
238 //! Multiplies each component of the pair by the corresponding component of \a b
239 Real2Proxy& operator*=(Real2 b) { return mul(b); }
240
241 //! Multiplies each component of the pair by the real \a b
243 {
244 x *= b;
245 y *= b;
246 }
247
248 //! Divides each component of the pair by the corresponding component of \a b
249 Real2Proxy& operator/=(Real2 b) { return div(b); }
250
251 //! Divides each component of the pair by the real \a b
253 {
254 x /= b;
255 y /= b;
256 }
257
258 //! Creates a pair that equals this pair added to \a b
259 Real2 operator+(Real2 b) const { return Real2(x + b.x, y + b.y); }
260
261 //! Creates a pair that equals \a b subtracted from this pair
262 Real2 operator-(Real2 b) const { return Real2(x - b.x, y - b.y); }
263
264 //! Creates a pair opposite to the current pair
265 Real2 operator-() const { return Real2(-x, -y); }
266
267 /*!
268 * \brief Creates a pair that equals this pair where each component has been
269 * multiplied by the corresponding component of \a b.
270 */
271 Real2 operator*(Real2 b) const { return Real2(x * b.x, y * b.y); }
272
273 /*!
274 * \brief Creates a pair that equals this pair where each component has been divided
275 * by the corresponding component of \a b.
276 */
277 Real2 operator/(Real2 b) const { return Real2(x / b.x, y / b.y); }
278
279 /*!
280 * \brief Normalizes the pair.
281 *
282 * If the pair is non-zero, divides each component by the norm of the pair
283 * (abs()), such that after calling this method, abs() equals \a 1.
284 * If the pair is zero, does nothing.
285 */
287 {
288 Real d = abs();
289 if (!math::isZero(d))
290 divSame(d);
291 return (*this);
292 }
293
294 /*!
295 * \brief Compares the pair to \a b.
296 *
297 * In the case of an integral #value_type, two pairs
298 * are equal if and only if each of their components are strictly
299 * equal.
300 *
301 * For #value_type of the floating point type (float, double or #Real), two pairs
302 * are identical if and only if the absolute value of the difference
303 * between each of their corresponding components is less
304 * than a given epsilon. The value of the epsilon used is that
305 * of float_info<value_type>::nearlyEpsilon():
306 * \f[A=B \Leftrightarrow |A.x-B.x|<\epsilon,|A.y-B.y|<\epsilon,|A.z-B.z|<\epsilon \f]
307 * \retval true if the two pairs are equal,
308 * \retval false otherwise.
309 */
310 bool operator==(Real2 b) const
311 {
312 return _eq(x, b.x) && _eq(y, b.y);
313 }
314
315 /*!
316 * \brief Compares two pairs.
317 * For the notion of equality, see operator==()
318 * \retval true if the two pairs are different,
319 * \retval false otherwise.
320 */
321 bool operator!=(Real2 b) const
322 {
323 return !operator==(b);
324 }
325
326 public:
327
328 Real2 getValue() const
329 {
330 m_info.setRead();
331 return m_value;
332 }
333 Real2& getValueMutable()
334 {
335 m_info.setReadOrWrite();
336 return m_value;
337 }
338
339 private:
340
341 /*!
342 * \brief Compares the values of \a a and \a b with the TypeEqualT comparator
343 * \retval true if \a a and \a b are equal,
344 * \retval false otherwise.
345 */
346 static bool _eq(Real a, Real b)
347 {
348 return math::isEqual(a, b);
349 }
350
351 //! Returns the square root of \a a
352 static Real _sqrt(Real a)
353 {
354 return math::sqrt(a);
355 }
356};
357
358/*---------------------------------------------------------------------------*/
359/*---------------------------------------------------------------------------*/
360
361/*!
362 * \brief Multiplication by a scalar.
363 */
364inline Real2 operator*(Real sca, const Real2Proxy& vec)
365{
366 return Real2(vec.x * sca, vec.y * sca);
367}
368
369/*---------------------------------------------------------------------------*/
370/*---------------------------------------------------------------------------*/
371
372/*!
373 * \brief Multiplication by a scalar.
374 */
375inline Real2
376operator*(const Real2Proxy& vec, Real sca)
377{
378 return Real2(vec.x * sca, vec.y * sca);
379}
380
381/*---------------------------------------------------------------------------*/
382/*---------------------------------------------------------------------------*/
383
384/*!
385 * \brief Division by a scalar.
386 */
387inline Real2
388operator/(const Real2Proxy& vec, Real sca)
389{
390 return Real2(vec.x / sca, vec.y / sca);
391}
392
393/*---------------------------------------------------------------------------*/
394/*---------------------------------------------------------------------------*/
395
396/*!
397 * \brief Comparison operator.
398 *
399 * This operator allows sorting Real2Proxy for example
400 * in std::set
401 */
402inline bool
403operator<(const Real2Proxy& v1, const Real2Proxy& v2)
404{
405 return v1.getValue() < v2.getValue();
406}
407
408/*---------------------------------------------------------------------------*/
409/*---------------------------------------------------------------------------*/
410
411/*!
412 * \brief Writes the pair \a t to the stream \a o
413 * \relates Real2Proxy
414 */
415inline ostream&
416operator<<(ostream& o, Real2Proxy t)
417{
418 return t.printXy(o);
419}
420
421/*!
422 * \brief Reads the pair \a t from the stream \a o.
423 * \relates Real2Proxy
424 */
425inline istream&
426operator>>(istream& i, Real2Proxy& t)
427{
428 return t.assign(i);
429}
430
431/*---------------------------------------------------------------------------*/
432/*---------------------------------------------------------------------------*/
433
434} // namespace Arcane
435
436/*---------------------------------------------------------------------------*/
437/*---------------------------------------------------------------------------*/
438
439#endif
Class managing a 2-dimensional real vector.
Definition Real2Proxy.h:41
Real abs2() const
Returns the squared norm of the pair .
Definition Real2Proxy.h:145
Real2 copy() const
Returns a copy of the pair.
Definition Real2Proxy.h:99
void operator/=(Real b)
Divides each component of the pair by the real b.
Definition Real2Proxy.h:252
RealProxy x
first component of the pair
Definition Real2Proxy.h:88
Real2Proxy & subSame(Real b)
Subtracts b from each component of the pair.
Definition Real2Proxy.h:209
Real2 operator*(Real2 b) const
Creates a pair that equals this pair where each component has been multiplied by the corresponding co...
Definition Real2Proxy.h:271
Real2Proxy & mulSame(Real b)
Multiplies each component of the pair by b.
Definition Real2Proxy.h:217
istream & operator>>(istream &i, Real2Proxy &t)
Reads the pair t from the stream o.
Definition Real2Proxy.h:426
Real2Proxy & operator/=(Real2 b)
Divides each component of the pair by the corresponding component of b.
Definition Real2Proxy.h:249
Real2Proxy & sub(Real2 b)
Subtracts b from the pair.
Definition Real2Proxy.h:177
void operator*=(Real b)
Multiplies each component of the pair by the real b.
Definition Real2Proxy.h:242
Real2Proxy & div(Real2 b)
Divides each component of the pair by the corresponding component of b.
Definition Real2Proxy.h:193
Real2Proxy & assign(Real2 f)
Copies the pair f.
Definition Real2Proxy.h:117
Real2 operator-() const
Creates a pair opposite to the current pair.
Definition Real2Proxy.h:265
ostream & print(ostream &o) const
Writes the pair to the stream o readable by an assign().
Real2Proxy & add(Real2 b)
Adds b to the pair.
Definition Real2Proxy.h:169
bool operator!=(Real2 b) const
Compares two pairs. For the notion of equality, see operator==().
Definition Real2Proxy.h:321
Real abs() const
Returns the norm of the pair .
Definition Real2Proxy.h:151
Real2Proxy & normalize()
Normalizes the pair.
Definition Real2Proxy.h:286
Real2Proxy & operator-=(Real2 b)
Subtracts b from the pair.
Definition Real2Proxy.h:236
Real2 operator+(Real2 b) const
Creates a pair that equals this pair added to b.
Definition Real2Proxy.h:259
Real2Proxy & operator*=(Real2 b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2Proxy.h:239
ostream & printXy(ostream &o) const
Writes the pair to the stream o in the form (x,y).
Real2Proxy & divSame(Real b)
Divides each component of the pair by b.
Definition Real2Proxy.h:225
bool operator==(Real2 b) const
Compares the pair to b.
Definition Real2Proxy.h:310
Real2Proxy & reset()
Resets the pair with default constructors.
Definition Real2Proxy.h:102
Real2Proxy & addSame(Real b)
Adds b to each component of the pair.
Definition Real2Proxy.h:201
Real2 operator/(Real2 b) const
Creates a pair that equals this pair where each component has been divided by the corresponding compo...
Definition Real2Proxy.h:277
Real2Proxy & mul(Real2 b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2Proxy.h:185
bool isNearlyZero() const
Compares the pair with the zero pair.
Definition Real2Proxy.h:139
const Real2 & operator=(Real v)
Assigns the pair (v,v) to the instance.
Definition Real2Proxy.h:73
RealProxy y
second component of the pair
Definition Real2Proxy.h:89
Real2Proxy & operator+=(Real2 b)
Adds b to the pair.
Definition Real2Proxy.h:233
Real2Proxy & assign(Real ax, Real ay)
Assigns the pair (ax,ay,az) to the instance.
Definition Real2Proxy.h:109
istream & assign(istream &i)
Reads a pair from the stream i The pair is read in the form of three value_type values.
Real2Proxy(Real2 &value, const MemoryAccessInfo &info)
Constructs the pair (ax,ay).
Definition Real2Proxy.h:45
Real2 operator-(Real2 b) const
Creates a pair that equals b subtracted from this pair.
Definition Real2Proxy.h:262
Real2Proxy(const Real2Proxy &f)
Constructs a pair identical to f.
Definition Real2Proxy.h:53
Class managing a 2-dimensional real vector.
Definition Real2.h:122
bool isZero(const BuiltInProxy< _Type > &a)
Tests if a value is exactly equal to zero.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Real2 operator*(Real sca, const Real2Proxy &vec)
Multiplication by a scalar.
Definition Real2Proxy.h:364
bool operator<(const Item &item1, const Item &item2)
Compare two entities.
Definition Item.h:566
double Real
Type representing a real number.
Real2 operator/(const Real2Proxy &vec, Real sca)
Division by a scalar.
Definition Real2Proxy.h:388
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.
Real y
second component of the pair
Definition Real2.h:35
Real x
first component of the pair
Definition Real2.h:34