Vector2.h 68.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file Vector2.h
//! \ingroup core
//! \author Klaus Iglberger
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//! \author Martin Bauer <martin.bauer@fau.de>
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//! \brief Header file for the implementation of a 2D vector
//
//======================================================================================================================

#pragma once

#include "FPClassify.h"
#include "MathTrait.h"
#include "SqrtTrait.h"
#include "Utility.h"

#include "core/DataTypes.h"
#include "core/VectorTrait.h"
#include "core/debug/Debug.h"
#include "core/mpi/Datatype.h"
#include "core/mpi/RecvBuffer.h"
#include "core/mpi/SendBuffer.h"

#include <cmath>
#include <iostream>
#include <limits>
43
#include <type_traits>
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

namespace walberla {
namespace math {


//**********************************************************************************************************************
// Definitions
//**********************************************************************************************************************

//! High-order return value.
/*! Abbreviation for the evaluation of the higher-order data type in a numerical operation. */
#define HIGH typename MathTrait<Type,Other>::High



//======================================================================================================================
//
//  CLASS DEFINITION
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Efficient, generic implementation of a 2-dimensional vector.
// \ingroup math
//
// The Vector2 class is the representation of a 2D vector with a total of 2 statically allocated
// elements of arbitrary type. The naming convention of the elements is as follows:

                             \f[\left(\begin{array}{*{2}{c}}
                             x & y \\
                             \end{array}\right)\f]

// These elements can be accessed directly with the subscript operator. The numbering of the
// vector elements is

                             \f[\left(\begin{array}{*{2}{c}}
                             0 & 1 \\
                             \end{array}\right)\f]
*/
template< typename Type >
class Vector2
{
86
   static_assert( std::is_arithmetic<Type>::value, "Vector2 only accepts arithmetic data types" );
87
88
89
90
91
92
93
94
95
96

private:
   //**Friend declarations*************************************************************************
   /*! \cond internal */
   template< typename Other > friend class Vector2;
   /*! \endcond */
   //*******************************************************************************************************************

public:
   //**Type definitions****************************************************************************
97
   using Length = typename SqrtTrait<Type>::Type;  //!< Vector length return type.
98
99
                                                   /*!< Return type of the Vector2<Type>::length
                                                        function. */
100
   using value_type = Type;
101
102
103
   //*******************************************************************************************************************

   //**Constructors*****************************************************************************************************
104
                              explicit inline Vector2() = default;
105
106
107
108
                              explicit inline Vector2( Type init );
   template< typename Other > explicit inline Vector2( Other init );
                              explicit inline Vector2( Type x, Type y );
                              explicit inline Vector2( const Type* init );
109
                                       inline Vector2( const Vector2& v ) = default;
110
111
112
113
114
115
116
117
118
119
120
121

   template< typename Other >
   inline Vector2( const Vector2<Other>& v );
   //*******************************************************************************************************************

   //**Destructor*******************************************************************************************************
   // No explicitly declared destructor.
   //*******************************************************************************************************************

   //**Operators********************************************************************************************************
   /*!\name Operators */
   //@{
122
   inline Vector2&                              operator= ( const Vector2& v ) = default;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
   template< typename Other > inline Vector2&   operator= ( const Vector2<Other>& v );
   template< typename Other > inline bool       operator==( Other rhs )                 const;
   template< typename Other > inline bool       operator==( const Vector2<Other>& rhs ) const;
   template< typename Other > inline bool       operator!=( Other rhs )                 const;
   template< typename Other > inline bool       operator!=( const Vector2<Other>& rhs ) const;
   inline Type&                                 operator[]( uint_t index );
   inline const Type&                           operator[]( uint_t index )                const;
   //@}
   //*******************************************************************************************************************

   //**Arithmetic operators************************************************************************
   /*!\name Arithmetic operators
   // \brief The return type of the arithmetic operators depends on the involved data types of
   // \brief the vectors. HIGH denotes the more significant data type of the arithmetic operation
   // \brief (for further detail see the MathTrait class description).
   */
   //@{
                              inline Vector2       operator-()                             const;
   template< typename Other > inline Vector2&      operator+=( const Vector2<Other>& rhs );
   template< typename Other > inline Vector2&      operator-=( const Vector2<Other>& rhs );
   template< typename Other > inline Vector2&      operator*=( Other rhs );
   template< typename Other > inline Vector2&      operator/=( Other rhs );
   template< typename Other > inline Vector2<HIGH> operator+ ( const Vector2<Other>& rhs ) const;
   template< typename Other > inline Vector2<HIGH> operator- ( const Vector2<Other>& rhs ) const;
   template< typename Other > inline Vector2<HIGH> operator* ( Other rhs )                 const;
   template< typename Other > inline HIGH          operator* ( const Vector2<Other>& rhs ) const;
   template< typename Other > inline Vector2<HIGH> operator/ ( Other rhs )                 const;
   //@}
   //*******************************************************************************************************************

   //**Utility functions***************************************************************************
   /*!\name Utility functions */
   //@{
   inline uint_t          indexOfMax( )                  const;
   inline uint_t          indexOfMin( )                  const;
   inline void            set( Type x, Type y );
   inline Length          length()                       const;
   inline Type            sqrLength()                    const;
   inline Vector2<Length> getNormalized()                const;
162
   inline Type*           data()                         {return v_;}
163
   inline Type const *    data()                         const {return v_;}
164
165
166
167
168
169
170
   //@}
   //*******************************************************************************************************************

 private:
   //**Member variables****************************************************************************
   /*!\name Member variables */
   //@{
171
172
173
174
175
176
177
178
179
180
   /**
    * The two statically allocated vector elements.
    *
    * Access to the vector values is gained via the subscript operator.
    * The order of the elements is
    * \f[\left(\begin{array}{*{2}{c}}
    * 0 & 1 \\
    * \end{array}\right)\f]
   **/
   Type v_[2] = {Type(), Type()};
181
182
183
   //@}
   //*******************************************************************************************************************
};
184
static_assert( std::is_trivially_copyable<Vector2<real_t>>::value, "Vector2<real_t> has to be trivially copyable!");
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//**********************************************************************************************************************

template<typename T>
Vector2<T> & normalize( Vector2<T> & v );


//======================================================================================================================
//
//  CONSTRUCTORS
//
//======================================================================================================================


//**********************************************************************************************************************
/*!\fn Vector2<Type>::Vector2( Type init )
// \brief Constructor for a homogenous initialization of all elements.
//
// \param init Initial value for all vector elements.
*/
template< typename Type >
inline Vector2<Type>::Vector2( Type init )
{
   v_[0] = v_[1] = init;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>::Vector2( Type init )
// \brief Constructor for a homogenous initialization of all elements.
//
// \param init Initial value for all vector elements.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>::Vector2( Other init )
{
222
   static_assert( std::is_arithmetic<Other>::value, "Vector2 only accepts arithmetic data types in Vector2( Other init )");
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

   v_[0] = v_[1] = init;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>::Vector2( Type x, Type y )
// \brief Constructor for a direct initialization of all vector elements.
//
// \param x The initial value for the x-component.
// \param y The initial value for the y-component.
*/
template< typename Type >
inline Vector2<Type>::Vector2( Type x, Type y )
{
   v_[0] = x;
   v_[1] = y;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>::Vector2( const Type* init )
// \brief Constructor for an array initializer.
//
// \param init Pointer to the initialization array.
//
// The array is assumed to have at least three valid elements.
*/
template< typename Type >
inline Vector2<Type>::Vector2( const Type* init )
{
   v_[0] = init[0];
   v_[1] = init[1];
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>::Vector2( const Vector2<Other>& v )
// \brief Conversion constructor from different Vector2 instances.
//
// \param v Vector to be copied.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>::Vector2( const Vector2<Other>& v )
{
   v_[0] = v.v_[0];
   v_[1] = v.v_[1];
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  OPERATORS
//
//======================================================================================================================


//**********************************************************************************************************************
/*!\fn Vector2<Type>& Vector2<Type>::operator=( const Vector2<Other>& v )
// \brief Assignment operator for different Vector2 instances.
//
// \param v Vector to be copied.
// \return Reference to the assigned vector.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>& Vector2<Type>::operator=( const Vector2<Other>& v )
{
   // This implementation is faster than the synthesized default copy assignment operator and
   // faster than an implementation with the C library function 'memcpy' in combination with a
   // protection against self-assignment. Additionally, this version goes without a protection
   // against self-assignment.
   v_[0] = v.v_[0];
   v_[1] = v.v_[1];
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector2<Type>::operator==( Other rhs ) const
// \brief Equality operator for the comparison of a vector and a scalar value.
//
// \param rhs The right-hand-side scalar value for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
template< typename Other >
inline bool Vector2<Type>::operator==( Other rhs ) const
{
   // In order to compare the vector and the scalar value, the data values of the lower-order
   // data type are converted to the higher-order data type within the equal function.
Sebastian Eibl's avatar
Sebastian Eibl committed
325
   return equal( v_[0], rhs ) && equal( v_[1], rhs );
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector2<Type>::operator==( const Vector2<Other>& rhs ) const
// \brief Equality operator for the comparison of two vectors.
//
// \param rhs The right-hand-side vector for the comparison.
// \return bool
*/
template< typename Type >
template< typename Other >
inline bool Vector2<Type>::operator==( const Vector2<Other>& rhs ) const
{
   // In order to compare the two vectors, the data values of the lower-order data
   // type are converted to the higher-order data type within the equal function.
Sebastian Eibl's avatar
Sebastian Eibl committed
343
   return equal( v_[0], rhs.v_[0] ) && equal( v_[1], rhs.v_[1] );
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector2<Type>::operator!=( Other rhs ) const
// \brief Inequality operator for the comparison of a vector and a scalar value.
//
// \param rhs The right-hand-side scalar value for the comparison.
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
template< typename Other >
inline bool Vector2<Type>::operator!=( Other rhs ) const
{
   // In order to compare the vector and the scalar value, the data values of the lower-order
   // data type are converted to the higher-order data type within the equal function.
Sebastian Eibl's avatar
Sebastian Eibl committed
364
   return !(*this == rhs);
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector2<Type>::operator!=( const Vector2<Other>& rhs ) const
// \brief Inequality operator for the comparison of two vectors.
//
// \param rhs The right-hand-side vector for the comparison.
// \return bool
*/
template< typename Type >
template< typename Other >
inline bool Vector2<Type>::operator!=( const Vector2<Other>& rhs ) const
{
   // In order to compare the two vectors, the data values of the lower-order data
   // type are converted to the higher-order data type within the equal function.
Sebastian Eibl's avatar
Sebastian Eibl committed
382
   return !(*this == rhs);
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Type& Vector2<Type>::operator[]( uint_t index )
// \brief Subscript operator for the direct access to the vector elements.
//
// \param index Access index. The index has to be in the range \f$[0..2]\f$.
// \return Reference to the accessed value.
*/
template< typename Type >
inline Type& Vector2<Type>::operator[]( uint_t index )
{
   WALBERLA_ASSERT_LESS( index, 2 , "Invalid vector access index" );
   return v_[index];
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn const Type& Vector2<Type>::operator[]( uint_t index ) const
// \brief Subscript operator for the direct access to the vector elements.
//
// \param index Access index. The index has to be in the range \f$[0..2]\f$.
// \return Reference-to-const to the accessed value.
*/
template< typename Type >
inline const Type& Vector2<Type>::operator[]( uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, 2, "Invalid vector access index" );
   return v_[index];
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  ARITHMETIC OPERATORS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\fn Vector2<Type> Vector2<Type>::operator-() const
// \brief Unary minus operator for the inversion of a vector (\f$ \vec{a} = -\vec{b} \f$).
//
// \return The inverse of the vector.
*/
template< typename Type >
inline Vector2<Type> Vector2<Type>::operator-() const
{
   return Vector2( -v_[0], -v_[1] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>& Vector2<Type>::operator+=( const Vector2<Other>& rhs )
// \brief Addition assignment operator for the addition of two vectors (\f$ \vec{a}+=\vec{b} \f$).
//
// \param rhs The right-hand-side vector to be added to the vector.
// \return Reference to the vector.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>& Vector2<Type>::operator+=( const Vector2<Other>& rhs )
{
   v_[0] += rhs.v_[0];
   v_[1] += rhs.v_[1];
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>& Vector2<Type>::operator-=( const Vector2<Other>& rhs )
// \brief Subtraction assignment operator for the subtraction of two vectors
// \brief (\f$ \vec{a}-=\vec{b} \f$).
//
// \param rhs The right-hand-side vector to be subtracted from the vector.
// \return Reference to the vector.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>& Vector2<Type>::operator-=( const Vector2<Other>& rhs )
{
   v_[0] -= rhs.v_[0];
   v_[1] -= rhs.v_[1];
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>& Vector2<Type>::operator*=( Other rhs )
// \brief Multiplication assignment operator for the multiplication between a vector and
// \brief a scalar value (\f$ \vec{a}*=s \f$).
//
// \param rhs The right-hand-side scalar value for the multiplication.
// \return Reference to the vector.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>& Vector2<Type>::operator*=( Other rhs )
{
   v_[0] *= rhs;
   v_[1] *= rhs;
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Type>& Vector2<Type>::operator/=( Other rhs )
// \brief Division assignment operator for the division of a vector by a scalar value
// \brief (\f$ \vec{a}/=s \f$).
//
// \param rhs The right-hand-side scalar value for the division.
// \return Reference to the vector.
//
// \b Note: No check for 0 is applied.
*/
template< typename Type >
template< typename Other >
inline Vector2<Type>& Vector2<Type>::operator/=( Other rhs )
{
   // Depending on the two involved data types, an integer division is applied or a
   // floating point division is selected.
   if( std::numeric_limits<HIGH>::is_integer ) {
      v_[0] /= rhs;
      v_[1] /= rhs;
      return *this;
   }
   else {
      const HIGH tmp( 1/static_cast<HIGH>( rhs ) );
      v_[0] = static_cast<Type>( static_cast<HIGH>( v_[0] ) * tmp );
      v_[1] = static_cast<Type>( static_cast<HIGH>( v_[1] ) * tmp );
      return *this;
   }
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<HIGH> Vector2<Type>::operator+( const Vector2<Other>& rhs ) const
// \brief Addition operator for the addition of two vectors (\f$ \vec{a}=\vec{b}+\vec{c} \f$).
//
// \param rhs The right-hand-side vector to be added to the vector.
// \return The sum of the two vectors.
//
// The operator returns a vector of the higher-order data type of the two involved vector
// data types (in fact the two template arguments \a Type and \a Other ).
*/
template< typename Type >
template< typename Other >
inline Vector2<HIGH> Vector2<Type>::operator+( const Vector2<Other>& rhs ) const
{
   return Vector2<HIGH>( v_[0]+rhs.v_[0], v_[1]+rhs.v_[1] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<HIGH> Vector2<Type>::operator-( const Vector2<Other>& rhs ) const
// \brief Subtraction operator for the subtraction of two vectors (\f$ \vec{a}=\vec{b}-\vec{c} \f$).
//
// \param rhs The right-hand-side vector to be subtracted from the vector.
// \return The difference of the two vectors.
//
// The operator returns a vector of the higher-order data type of the two involved vector
// data types (in fact the two template arguments \a Type and \a Other ).
*/
template< typename Type >
template< typename Other >
inline Vector2<HIGH> Vector2<Type>::operator-( const Vector2<Other>& rhs ) const
{
   return Vector2<HIGH>( v_[0]-rhs.v_[0], v_[1]-rhs.v_[1] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<HIGH> Vector2<Type>::operator*( Other rhs ) const
// \brief Multiplication operator for the multiplication of a vector and a scalar value
// \brief (\f$ \vec{a}=\vec{b}*s \f$).
//
// \param rhs The right-hand-side scalar value for the multiplication.
// \return The scaled result vector.
//
// The operator returns a vector of the higher-order data type of the two involved data types
// (in fact the two template arguments \a Type and \a Other ).
*/
template< typename Type >
template< typename Other >
inline Vector2<HIGH> Vector2<Type>::operator*( Other rhs ) const
{
   return Vector2<HIGH>( v_[0]*rhs, v_[1]*rhs );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn HIGH Vector2<Type>::operator*( const Vector2<Other>& rhs ) const
// \brief Multiplication operator for the scalar product (inner product) of two vectors
// \brief (\f$ s=\vec{a}*\vec{b} \f$).
//
// \param rhs The right-hand-side vector for the inner product.
// \return The scalar product.
//
// The operator returns a scalar value of the higher-order data type of the two involved data
// types (in fact the two template arguments \a Type and \a Other ).
*/
template< typename Type >
template< typename Other >
inline HIGH Vector2<Type>::operator*( const Vector2<Other>& rhs ) const
{
   return ( v_[0]*rhs.v_[0] + v_[1]*rhs.v_[1]  );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<HIGH> Vector2<Type>::operator/( Other rhs ) const
608
// \brief Division operator for the division of a vector by a scalar value
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
// \brief (\f$ \vec{a}=\vec{b}/s \f$).
//
// \param rhs The right-hand-side scalar value for the division.
// \return The scaled result vector.
//
// The operator returns a vector of the higher-order data type of the two involved data types
// (in fact the two template arguments \a Type and \a Other ).\n
// \b Note: No check for 0 is applied.
*/
template< typename Type >
template< typename Other >
inline Vector2<HIGH> Vector2<Type>::operator/( Other rhs ) const
{
   // Depending on the two involved data types, an integer division is applied or a
   // floating point division is selected.
   if( std::numeric_limits<HIGH>::is_integer ) {
      return Vector2<HIGH>( v_[0]/rhs, v_[1]/rhs );
   }
   else {
      const HIGH tmp( 1/static_cast<HIGH>( rhs ) );
      return Vector2<HIGH>( v_[0]*tmp, v_[1]*tmp );
   }
}
//**********************************************************************************************************************


//======================================================================================================================
//
//  UTILITY FUNCTIONS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\fn Vector2<Type>::indexOfMax( )
// \brief Returns index of absolute maximum value
*/
template< typename Type >
inline uint_t Vector2<Type>::indexOfMax( ) const {
   return math::abs( v_[0] ) > math::abs( v_[1] ) ? 0u : 1u;
}
//**********************************************************************************************************************

//**********************************************************************************************************************
/*!\fn Vector2<Type>::indexOfMin( )
// \brief Returns index of absolute minimum value
*/
template< typename Type >
inline uint_t Vector2<Type>::indexOfMin( ) const {
   return math::abs( v_[0] ) < math::abs( v_[1] ) ? 0u : 1u;
}
//**********************************************************************************************************************

//**********************************************************************************************************************
/*!\fn Vector2<Type>::set( Type x, Type y )
// \brief Set function for a direct assignment of all vector elements.
//
// \param x The initial value for the x-component.
// \param y The initial value for the y-component.
*/
template< typename Type >
inline void Vector2<Type>::set( Type x, Type y )
{
   v_[0] = x;
   v_[1] = y;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Length Vector2<Type>::length() const
// \brief Calculation of the vector length \f$|\vec{a}|\f$.
//
// \return The length of the vector.
//
// The return type of the length function depends on the actual type of the vector instance:
//
// <table border="0" cellspacing="0" cellpadding="1">
//    <tr>
//       <td width="250px"> \b Type </td>
//       <td width="100px"> \b Length </td>
//    </tr>
//    <tr>
//       <td>float</td>
//       <td>float</td>
//    </tr>
//    <tr>
//       <td>integral data types and double</td>
//       <td>double</td>
//    </tr>
//    <tr>
//       <td>long double</td>
//       <td>long double</td>
//    </tr>
// </table>
*/
template< typename Type >
inline typename SqrtTrait<Type>::Type Vector2<Type>::length() const
{
   return std::sqrt( static_cast<typename SqrtTrait<Type>::Type>( v_[0]*v_[0] + v_[1]*v_[1] ) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Type Vector2<Type>::sqrLength() const
// \brief Calculation of the vector square length \f$|\vec{a}|^2\f$.
//
// \return The square length of the vector.
*/
template< typename Type >
inline Type Vector2<Type>::sqrLength() const
{
   return ( v_[0]*v_[0] + v_[1]*v_[1] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<Length>& Vector2<Type>::getNormalized() const
// \brief Calculation of the normalized vector (\f$|\vec{a}|=1\f$).
//
// \pre \f$|\vec{a}|\neq0\f$
//
// \return The normalized vector.
//
// The function returns the normalized vector.
*/
template< typename Type >
Vector2<typename Vector2<Type>::Length> Vector2<Type>::getNormalized() const
{
   const Length len( length() );

   WALBERLA_ASSERT_FLOAT_UNEQUAL( len, Length(0) );

   Vector2<Length> result ( static_cast<Length>( v_[0] ) / len,
                            static_cast<Length>( v_[1] ) / len );

   WALBERLA_ASSERT_FLOAT_EQUAL( result.sqrLength(), 1.0 );

   return result;
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  GLOBAL OPERATORS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\name Vector2 operators */
//@{

// The following overloads of the comparison operators are necessary to disambiguate
// comparisons between a scalar value and a vector.
template< typename Type > inline bool operator==( unsigned char  scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( char           scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( signed char    scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( wchar_t        scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( unsigned short scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( short          scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( unsigned int   scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( int            scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( unsigned long  scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( long           scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( float          scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( double         scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator==( long double    scalar, const Vector2<Type>& vec );

template< typename Type > inline bool operator!=( unsigned char  scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( char           scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( signed char    scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( wchar_t        scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( unsigned short scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( short          scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( unsigned int   scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( int            scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( unsigned long  scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( long           scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( float          scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( double         scalar, const Vector2<Type>& vec );
template< typename Type > inline bool operator!=( long double    scalar, const Vector2<Type>& vec );

template< typename Type >
std::ostream& operator<<( std::ostream& os, const Vector2<Type>& v );

template< typename Type >
std::istream& operator>>( std::istream& is, Vector2<Type>& v );

template< typename Type >
inline bool isnan( const Vector2<Type>& v );

template< typename Type >
inline const Vector2<Type> abs( const Vector2<Type>& v );

template< typename Type >
inline const Vector2<Type> fabs( const Vector2<Type>& v );
//@}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( unsigned char scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of an unsigned char scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( unsigned char scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( char scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a char scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( char scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( signed char scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a signed char scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( signed char scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( wchar_t scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a wchar_t scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( wchar_t scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( unsigned short scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of an unsigned short scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( unsigned short scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( short scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a short scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( short scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( uint_t scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of an uint_t scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( unsigned int scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( int scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of an int scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( int scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( unsigned long scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of an unsigned long scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( unsigned long scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( long scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a long scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( long scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( float scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a float scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( float scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( double scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a double scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( double scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator==( long double scalar, const Vector2<Type>& vec )
// \brief Equality operator for the comparison of a long double scalar and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
// \param vec The right-hand-side vector for the comparison.
// \return bool
//
// If all values of the vector are equal to the scalar value, the equality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator==( long double scalar, const Vector2<Type>& vec )
{
   return vec == scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( unsigned char scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of an unsigned char scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1065
// \param vec The right-hand-side vector for the comparison.
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( unsigned char scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( char scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a char scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1084
// \param vec The right-hand-side vector for the comparison.
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( char scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( signed char scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a signed char scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1103
// \param vec The right-hand-side vector for the comparison.
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( signed char scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( wchar_t scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a wchar_t scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1122
// \param vec The right-hand-side vector for the comparison.
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( wchar_t scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( unsigned short scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of an unsigned short scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1141
// \param vec The right-hand-side vector for the comparison.
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( unsigned short scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( short scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a short scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1160
// \param vec The right-hand-side vector for the comparison.
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( short scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( uint_t scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of an uint_t scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1179
// \param vec The right-hand-side vector for the comparison.
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( unsigned int scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( int scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of an int scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1198
// \param vec The right-hand-side vector for the comparison.
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( int scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( unsigned long scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of an unsigned long scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1217
// \param vec The right-hand-side vector for the comparison.
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( unsigned long scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( long scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a long scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1236
// \param vec The right-hand-side vector for the comparison.
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( long scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( float scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a float scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1255
// \param vec The right-hand-side vector for the comparison.
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( float scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( double scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a double scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1274
// \param vec The right-hand-side vector for the comparison.
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( double scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool operator!=( long double scalar, const Vector2<Type>& vec )
// \brief Inequality operator for the comparison of a long double scalar value and a vector.
//
// \param scalar The left-hand-side scalar value for the comparison.
1293
// \param vec The right-hand-side vector for the comparison.
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
// \return bool
//
// If one value of the vector is inequal to the scalar value, the inequality test returns true,
// otherwise false.
*/
template< typename Type >
inline bool operator!=( long double scalar, const Vector2<Type>& vec )
{
   return vec != scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector2<HIGH> operator*( Other scalar, const Vector2<Type>& vec )
// \brief Multiplication operator for the multiplication of a scalar value and a vector
// \brief (\f$ \vec{a}=s*\vec{b} \f$).
//
// \param scalar The left-hand-side scalar value for the multiplication.
// \param vec The right-hand-side vector for the multiplication.
// \return The scaled result vector.
*/
template< typename Type, typename Other >
1317
inline typename std::enable_if< std::is_fundamental<Other>::value, Vector2<HIGH> >::type
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
   operator*( Other scalar, const Vector2<Type>& vec )
{
   return vec * scalar;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn std::ostream& operator<<( std::ostream& os, const Vector2<Type>& v )
// \brief Global output operator for 2-dimensional vectors.
//
// \param os Reference to the output stream.
// \param v Reference to a constant vector object.
// \return Reference to the output stream.
*/
template< typename Type >
std::ostream& operator<<( std::ostream& os, const Vector2<Type>& v )
{
   return os << "<" << v[0] << "," << v[1] << ">";
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn std::istream& operator>>( std::istream& is, Vector2<Type>& v )
// \brief Global input operator for 2-dimensional vectors.
//
// \param is Reference to the input stream.
// \param v Reference to a vector object.
// \return The input stream.
*/
template< typename Type >
std::istream& operator>>( std::istream& is, Vector2<Type>& v )
{
   if( !is ) return is;

   char bracket1, bracket2, comma1;
   Type x(0), y(0);
   const std::istream::pos_type pos( is.tellg() );
   const std::istream::fmtflags oldFlags( is.flags() );

   // Setting the 'skip whitespaces' flag
   is >> std::skipws;

   // Extracting the vector
   if( !(is >> bracket1 >> x >> comma1 >> y >> bracket2) ||
       bracket1 != '<' || comma1 != ',' || bracket2 != '>' ) {
      is.clear();
      is.seekg( pos );
      is.setstate( std::istream::failbit );
      is.flags( oldFlags );
      return is;
   }

1372
   // Transferring the input to the vector values
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
   v[0] = x; v[1] = y;

   // Resetting the flags
   is.flags( oldFlags );

   return is;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn std::istream& operator>>( std::istream& is, Vector2<bool>& v )
// \brief Specialization for input operator for 2-dimensional vectors of bool.
//
// The operator can parse e.g. <1, 0, 1> and <true, false, true> from the istream
//
// \param is Reference to the input stream.
// \param v Reference to a bool vector object.
// \return The input stream.
*/
template<>
inline std::istream& operator>>( std::istream& is, Vector2<bool>& v )
{
   if( !is ) return is;

   char bracket1, bracket2, comma1;
   bool x(false), y(false);
   const std::istream::pos_type pos( is.tellg() );
   const std::istream::fmtflags oldFlags( is.flags() );


   // try parsing e.g. <true, false, true>
   // Setting the 'skip whitespaces' flag
   is >> std::skipws >> std::boolalpha;

   // Extracting the vector
   if( !(is >> bracket1 >> x >> comma1 >> y >> bracket2) ||
       bracket1 != '<' || comma1 != ','  || bracket2 != '>' ) {
      is.clear();
      is.seekg( pos );
      is.flags( oldFlags );

      // try parsing e.g. <1, 0, 1>
      // Setting the 'skip whitespaces' flag
      is >> std::skipws >> std::noboolalpha;

      // Extracting the vector
      if( !(is >> bracket1 >> x >> comma1 >> y >> bracket2) ||
         bracket1 != '<' || comma1 != ','  || bracket2 != '>' ) {
            is.clear();
            is.seekg( pos );
            is.setstate( std::istream::failbit );
            is.flags( oldFlags );

            return is;
      }
   }

1431
   // Transferring the input to the vector values
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
   v[0] = x; v[1] = y;

   // Resetting the flags
   is.flags( oldFlags );

   return is;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool isnan( const Vector2<Type>& v )
// \brief Checks the given vector for not-a-number elements.
//
// \param v The vector to be checked for not-a-number elements.
// \return \a true if at least one element of the vector is not-a-number, \a false otherwise.
*/
template< typename Type >
inline bool isnan( const Vector2<Type>& v )
{
Sebastian Eibl's avatar
Sebastian Eibl committed
1452
   return walberla::math::isnan( v[0] ) || walberla::math::isnan( v[1] );
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn const Vector2<Type> abs( const Vector2<Type>& v )
// \brief Returns a vector containing the absolute values of each single element of \a v.
//
// \param v The integral input vector.
// \return The absolute value of each single element of \a v.
//
// The \a abs function calculates the absolute value of each element of the input vector \a v.
// This function can only be applied to vectors of integral data type. For floating point vectors,
// the pe::fabs( const Vector2& ) function can be used.
*/
template< typename Type >
inline const Vector2<Type> abs( const Vector2<Type>& v )
{
   static_assert( std::numeric_limits<Type>::is_integer, "v has to be of integral type!" );
   return Vector2<Type>( std::abs(v[0]), std::abs(v[1]) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn const Vector2<Type> fabs( const Vector2<Type>& v )
// \brief Returns a vector containing the absolute values of each single element of \a v.
//
// \param v The floating point input vector.
// \return The absolute value of each single element of \a v.
//
// The \a fabs function calculates the absolute value of each element of the input vector \a v.
// This function can only be applied to floating point vectors. For vectors of integral data
// type, the pe::abs( const Vector2& ) function can be used.
*/
template< typename Type >
inline const Vector2<Type> fabs( const Vector2<Type>& v )
{
   static_assert( !std::numeric_limits<Type>::is_integer, "v has to be of floating point type!" );
   return Vector2<Type>( std::fabs(v[0]), std::fabs(v[1]) );
}
//**********************************************************************************************************************



//**********************************************************************************************************************
/**
// \brief Normalization of the vector (\f$|\vec{a}|=1\f$).
//
// \pre \f$|\vec{a}|\neq0\f$
//
// \return Reference to the normalized vector.
//
// Normalization of the vector to a length of 1. If you want to normalize a vector of integral T
// use member function getNormalized() instead
*/
template<typename T>
Vector2<T> & normalize( Vector2<T> & v )
{
1512
   static_assert( std::is_floating_point<T>::value,
1513
      "You can only normalize floating point vectors in-place!");
1514
   static_assert( (std::is_same<T, typename Vector2<T>::Length>::value),
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
      "The type of your Vector2's length does not match its element type!" );

   const T len( v.length() );

   WALBERLA_ASSERT_FLOAT_UNEQUAL( len, typename Vector2<T>::Length(0) );

   v[0] /= len;
   v[1] /= len;

   WALBERLA_ASSERT_FLOAT_EQUAL( v.sqrLength(), T(1.0) );

   return v;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/**
// \brief Functor providing a lexicographical ordering for Vector2.
//
// \tparam T Datatype of the compared Vector2's elements.
*/
template< typename T >
struct Vector2LexicographicalyLess
{
   /**
   // \brief Provides a lexicographical less-than-operator for Vector2.
   //
   // \param lhs left hand side of less-than-operator.
   // \param rhs right hand side of less-than-operator.
1545
   // \returns true if lhs < rhs (lexicographically), else returns false.
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
   */
   bool operator()( const Vector2<T> & lhs, const Vector2<T> & rhs ) const
   {
      return std::lexicographical_compare( &lhs[0], &lhs[0] + 2, &rhs[0], &rhs[0] + 2 );
   }
};
//**********************************************************************************************************************


//**********************************************************************************************************************
/**
// \brief Function providing a hash value for Vector2.
//
// \tparam  T Datatype of the Vector2's elements.
// \param   v The vector the hash is computed for.
// \returns   A hash for the entire Vector2.
*/
template< typename T >
size_t hash_value( const Vector2<T> & v )
{
   size_t seed = 0;
1567
   std::hash<T> hasher;
1568

1569
1570
   seed ^= hasher(v[0]) + 0x9e3779b9 + (seed<<6) + (seed>>2);
   seed ^= hasher(v[1]) + 0x9e3779b9 + (seed<<6) + (seed>>2);
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601

   return seed;

}
//**********************************************************************************************************************


} // namespace math

using math::Vector2;

} // namespace walberla

#undef HIGH


//======================================================================================================================
//
//  Send/Recv Buffer Serialization Specialization
//
//======================================================================================================================

namespace walberla {
namespace mpi {

   template< typename T,    // Element type of SendBuffer
             typename G,    // Growth policy of SendBuffer
             typename VT >  // Element type of vector
   mpi::GenericSendBuffer<T,G>& operator<<( mpi::GenericSendBuffer<T,G> & buf, const Vector2<VT> & vec )
   {
      buf.addDebugMarker( "v2" );
1602
1603
1604
1605
      static_assert ( std::is_trivially_copyable< Vector2<VT> >::value,
                      "type has to be trivially copyable for the memcpy to work correctly" );
      auto pos = buf.forward(sizeof(Vector2<VT>));
      std::memcpy(pos, &vec, sizeof(Vector2<VT>));
1606
1607
1608
1609
1610
1611
1612
1613
      return buf;
   }

   template< typename T,    // Element type  of RecvBuffer
             typename VT >  // Element type of vector
   mpi::GenericRecvBuffer<T>& operator>>( mpi::GenericRecvBuffer<T> & buf, Vector2<VT> & vec )
   {
      buf.readDebugMarker( "v2" );
1614
1615
1616
1617
1618
      static_assert ( std::is_trivially_copyable< Vector2<VT> >::value,
                      "type has to be trivially copyable for the memcpy to work correctly" );
      auto pos = buf.skip(sizeof(Vector2<VT>));
      //suppress https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-Wclass-memaccess
      std::memcpy(static_cast<void*>(&vec), pos, sizeof(Vector2<VT>));
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
      return buf;
   }

   template<typename VT>
   struct BufferSizeTrait< walberla::math::Vector2<VT> > {
      static const bool constantSize = true;
      static const uint_t size = 2 * BufferSizeTrait<VT>::size + mpi::BUFFER_DEBUG_OVERHEAD;
   };


}
}

//======================================================================================================================
//
//  MPI Datatype
//
//======================================================================================================================

namespace walberla {

   template< typename T>
   struct MPITrait< Vector2<T> >
   {
      static inline MPI_Datatype type()
      {
1645
1646
         // cannot use mpi::Datatype here because its destructor calls MPI_Type_free and static variables are destroyed after the MPI_Finalize
         static MPI_Datatype datatype;
1647
1648
1649
         static bool initialized = false;

         if( ! initialized ) {
1650
1651
            MPI_Type_contiguous(2, MPITrait<T>::type(), &datatype );
            MPI_Type_commit( &datatype );
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
            initialized = true;
         }
         return datatype;
      }
   };
} // namespace walberla



//======================================================================================================================
//
//  Vector Trait Specialization
//
//======================================================================================================================

namespace walberla {

   // Specialization of VectorTrait for Vector2's
   template<typename T>
   struct VectorTrait< Vector2<T> >
   {
1673
      using OutputType = T;
1674
1675
1676
1677
1678
1679
1680

      static const uint_t F_SIZE =  2u;
      static   T  get( const Vector2<T> & v, uint_t f )       { return v[f]; }
      static void set(       Vector2<T> & v, uint_t f, T val) { v[f] = val;  }
   };

} // namespace walberla
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692

namespace std
{
    template<typename T>
    struct hash< walberla::Vector2<T> >
    {
        std::size_t operator()( walberla::Vector2<T> const & v ) const noexcept
        {
            return walberla::Vector2<T>::hash_value( v );
        }
    };
} // namespace std