Vector3.h 79.9 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
43
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
222
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
608
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
//======================================================================================================================
//
//  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 Vector3.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 3D 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 "core/debug/CheckFunctions.h"

#include <boost/functional/hash.hpp>
#include <boost/type_traits.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/utility/enable_if.hpp>

#include <cmath>
#include <iostream>
#include <limits>


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 3-dimensional vector.
// \ingroup math
//
// The Vector3 class is the representation of a 3D vector with a total of 3 statically allocated
// elements of arbitrary type. The naming convention of the elements is as follows:

                             \f[\left(\begin{array}{*{3}{c}}
                             x & y & z \\
                             \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}{*{3}{c}}
                             0 & 1 & 2 \\
                             \end{array}\right)\f]
*/
template< typename Type >
class Vector3
{
   static_assert( boost::is_arithmetic<Type>::value, "Vector3 only accepts arithmetic data types" );

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

public:
   //**Type definitions****************************************************************************
   typedef typename SqrtTrait<Type>::Type Length;  //!< Vector length return type.
                                                   /*!< Return type of the Vector3<Type>::length
                                                        function. */
   typedef Type value_type;
   //*******************************************************************************************************************

   //**Constructors*****************************************************************************************************
                              explicit inline Vector3();
                              explicit inline Vector3( Type init );
   template< typename Other > explicit inline Vector3( Other init );
                              explicit inline Vector3( Type x, Type y, Type z );
                              explicit inline Vector3( const Type* init );
                                       inline Vector3( const Vector3& v );

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

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

   //**Operators********************************************************************************************************
   /*!\name Operators */
   //@{
   inline Vector3&                              operator= ( const Vector3& v );
   template< typename Other > inline Vector3&   operator= ( const Vector3<Other>& v );
   template< typename Other > inline bool       operator==( Other rhs )                 const;
   template< typename Other > inline bool       operator==( const Vector3<Other>& rhs ) const;
   template< typename Other > inline bool       operator!=( Other rhs )                 const;
   template< typename Other > inline bool       operator!=( const Vector3<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 Vector3       operator-()                             const;
   template< typename Other > inline Vector3&      operator%=( const Vector3<Other>& rhs );       //cross product
   template< typename Other > inline Vector3&      operator+=( const Vector3<Other>& rhs );
   template< typename Other > inline Vector3&      operator-=( const Vector3<Other>& rhs );
   template< typename Other > inline Vector3&      operator*=( Other rhs );
   template< typename Other > inline Vector3&      operator/=( Other rhs );
   template< typename Other > inline Vector3<HIGH> operator% ( const Vector3<Other>& rhs ) const; //cross product
   template< typename Other > inline Vector3<HIGH> operator+ ( const Vector3<Other>& rhs ) const;
   template< typename Other > inline Vector3<HIGH> operator- ( const Vector3<Other>& rhs ) const;
   template< typename Other > inline Vector3<HIGH> operator* ( Other rhs )                 const;
   template< typename Other > inline HIGH          operator* ( const Vector3<Other>& rhs ) const;
   template< typename Other > inline Vector3<HIGH> operator/ ( Other rhs )                 const;
   //@}
   //*******************************************************************************************************************

   //**Utility functions***************************************************************************
   /*!\name Utility functions */
   //@{
   inline uint_t          indexOfMax( )                  const;
   inline uint_t          indexOfMin( )                  const;
   inline Type            max( )                         const;
   inline Type            min( )                         const;
   inline void            set( Type x, Type y, Type z );
   inline Length          length()                       const;
   inline Type            sqrLength()                    const;
   inline Vector3<Length> getNormalized()                const;
   inline Vector3<Length> getNormalizedOrZero()          const;
   inline void            reset();
   //@}
   //*******************************************************************************************************************

 private:
   //**Member variables****************************************************************************
   /*!\name Member variables */
   //@{
   Type v_[3];  //!< The three 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}{*{3}{c}}
                     0 & 1 & 2 \\
                     \end{array}\right)\f] */
   //@}
   //*******************************************************************************************************************
};
//**********************************************************************************************************************

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


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

//**********************************************************************************************************************
/*!\fn Vector3<Type>::Vector3()
// \brief The default constructor for Vector3.
//
// All vector elements are initialized to the default value (i.e. 0 for integral data types).
*/
template< typename Type >
inline Vector3<Type>::Vector3()
{
   v_[0] = v_[1] = v_[2] = Type();
}
//**********************************************************************************************************************


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


//**********************************************************************************************************************
/*!\fn Vector3<Type>::Vector3( 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 Vector3<Type>::Vector3( Other init )
{
   static_assert( boost::is_arithmetic<Other>::value, "Vector3 only accepts arithmetic data types in Vector3( Other init )");

   v_[0] = v_[1] = v_[2] = numeric_cast<Type>(init);
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>::Vector3( Type x, Type y, Type z )
// \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.
// \param z The initial value for the z-component.
*/
template< typename Type >
inline Vector3<Type>::Vector3( Type x, Type y, Type z )
{
   v_[0] = x;
   v_[1] = y;
   v_[2] = z;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>::Vector3( 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 Vector3<Type>::Vector3( const Type* init )
{
   v_[0] = init[0];
   v_[1] = init[1];
   v_[2] = init[2];
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>::Vector3( const Vector3& v )
// \brief The copy constructor for Vector3.
//
// \param v Vector to be copied.
//
// The copy constructor is explicitly defined in order to enable/facilitate NRV optimization.
*/
template< typename Type >
inline Vector3<Type>::Vector3( const Vector3& v )
{
   v_[0] = v.v_[0];
   v_[1] = v.v_[1];
   v_[2] = v.v_[2];
}
//**********************************************************************************************************************


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




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

//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<Type>::operator=( const Vector3& v )
// \brief Copy assignment operator for Vector3.
//
// \param v Vector to be copied.
// \return Reference to the assigned vector.
//
// Explicit definition of a copy assignment operator for performance reasons.
*/
template< typename Type >
inline Vector3<Type>& Vector3<Type>::operator=( const Vector3& 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];
   v_[2] = v.v_[2];
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<Type>::operator=( const Vector3<Other>& v )
// \brief Assignment operator for different Vector3 instances.
//
// \param v Vector to be copied.
// \return Reference to the assigned vector.
*/
template< typename Type >
template< typename Other >
inline Vector3<Type>& Vector3<Type>::operator=( const Vector3<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];
   v_[2] = v.v_[2];
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector3<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 Vector3<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.
   if( !equal( v_[0], rhs ) || !equal( v_[1], rhs ) || !equal( v_[2], rhs ) )
      return false;
   else return true;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector3<Type>::operator==( const Vector3<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 Vector3<Type>::operator==( const Vector3<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.
   if( !equal( v_[0], rhs.v_[0] ) || !equal( v_[1], rhs.v_[1] ) || !equal( v_[2], rhs.v_[2] ) )
      return false;
   else return true;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector3<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 Vector3<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.
   if( !equal( v_[0], rhs ) || !equal( v_[1], rhs ) || !equal( v_[2], rhs ) )
      return true;
   else return false;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn bool Vector3<Type>::operator!=( const Vector3<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 Vector3<Type>::operator!=( const Vector3<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.
   if( !equal( v_[0], rhs.v_[0] ) || !equal( v_[1], rhs.v_[1] ) || !equal( v_[2], rhs.v_[2] ) )
      return true;
   else return false;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Type& Vector3<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& Vector3<Type>::operator[]( uint_t index )
{
   WALBERLA_ASSERT_LESS( index, 3 , "Invalid vector access index" );
   return v_[index];
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn const Type& Vector3<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& Vector3<Type>::operator[]( uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, 3, "Invalid vector access index" );
   return v_[index];
}
//**********************************************************************************************************************




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

//**********************************************************************************************************************
/*!\fn Vector3<Type> Vector3<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 Vector3<Type> Vector3<Type>::operator-() const
{
   return Vector3( -v_[0], -v_[1], -v_[2] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type> & Vector3<Type>::operator%=( const Vector3<Other>& rhs )
// \brief Cross product (outer product) of two vectors (\f$ \vec{a}=\vec{b}\times\vec{c} \f$).
//
// \param rhs The right-hand-side vector for the cross product.
// \return The cross product.
*/
template< typename Type >
template< typename Other >
inline Vector3<Type>& Vector3<Type>::operator%=( const Vector3<Other>& rhs )
{
   Type tmp0 = v_[1] * rhs.v_[2] - v_[2] * rhs.v_[1];
   Type tmp1 = v_[2] * rhs.v_[0] - v_[0] * rhs.v_[2];
   v_[2]     = v_[0] * rhs.v_[1] - v_[1] * rhs.v_[0];
   v_[1]     = tmp1;
   v_[0]     = tmp0;
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<Type>::operator+=( const Vector3<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 Vector3<Type>& Vector3<Type>::operator+=( const Vector3<Other>& rhs )
{
   v_[0] += numeric_cast<Type>(rhs.v_[0]);
   v_[1] += numeric_cast<Type>(rhs.v_[1]);
   v_[2] += numeric_cast<Type>(rhs.v_[2]);
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<Type>::operator-=( const Vector3<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 Vector3<Type>& Vector3<Type>::operator-=( const Vector3<Other>& rhs )
{
   v_[0] -= numeric_cast<Type>(rhs.v_[0]);
   v_[1] -= numeric_cast<Type>(rhs.v_[1]);
   v_[2] -= numeric_cast<Type>(rhs.v_[2]);
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<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 Vector3<Type>& Vector3<Type>::operator*=( Other rhs )
{
   v_[0] *= numeric_cast<Type>(rhs);
   v_[1] *= numeric_cast<Type>(rhs);
   v_[2] *= numeric_cast<Type>(rhs);
   return *this;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Type>& Vector3<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 Vector3<Type>& Vector3<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] /= numeric_cast<Type>(rhs);
      v_[1] /= numeric_cast<Type>(rhs);
      v_[2] /= numeric_cast<Type>(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 );
      v_[2] = static_cast<Type>( static_cast<HIGH>( v_[2] ) * tmp );
      return *this;
   }
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<HIGH> Vector3<Type>::operator%( const Vector3<Other>& rhs ) const
// \brief Cross product (outer product) of two vectors (\f$ \vec{a}=\vec{b}\times\vec{c} \f$).
//
// \param rhs The right-hand-side vector for the cross product.
// \return The cross product.
*/
template< typename Type >
template< typename Other >
inline Vector3<HIGH> Vector3<Type>::operator%( const Vector3<Other>& rhs ) const
{
   return Vector3<HIGH>( v_[1] * rhs.v_[2] - v_[2] * rhs.v_[1],
                         v_[2] * rhs.v_[0] - v_[0] * rhs.v_[2],
                         v_[0] * rhs.v_[1] - v_[1] * rhs.v_[0] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<HIGH> Vector3<Type>::operator+( const Vector3<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 Vector3<HIGH> Vector3<Type>::operator+( const Vector3<Other>& rhs ) const
{
   return Vector3<HIGH>( v_[0]+numeric_cast<Type>(rhs.v_[0]), v_[1]+numeric_cast<Type>(rhs.v_[1]), v_[2]+numeric_cast<Type>(rhs.v_[2]) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<HIGH> Vector3<Type>::operator-( const Vector3<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 Vector3<HIGH> Vector3<Type>::operator-( const Vector3<Other>& rhs ) const
{
   return Vector3<HIGH>( v_[0]-numeric_cast<Type>(rhs.v_[0]), v_[1]-numeric_cast<Type>(rhs.v_[1]), v_[2]-numeric_cast<Type>(rhs.v_[2]) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<HIGH> Vector3<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 Vector3<HIGH> Vector3<Type>::operator*( Other rhs ) const
{
   return Vector3<HIGH>( v_[0]*numeric_cast<Type>(rhs), v_[1]*numeric_cast<Type>(rhs), v_[2]*numeric_cast<Type>(rhs) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn HIGH Vector3<Type>::operator*( const Vector3<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 Vector3<Type>::operator*( const Vector3<Other>& rhs ) const
{
   return ( v_[0]*numeric_cast<Type>(rhs.v_[0]) + v_[1]*numeric_cast<Type>(rhs.v_[1]) + v_[2]*numeric_cast<Type>(rhs.v_[2]) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<HIGH> Vector3<Type>::operator/( Other rhs ) const
// \brief Division operator for the divison of a vector by a scalar value
// \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 Vector3<HIGH> Vector3<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 Vector3<HIGH>( v_[0]/rhs, v_[1]/rhs, v_[2]/rhs );
   }
   else {
      const HIGH tmp( 1/static_cast<HIGH>( rhs ) );
      return Vector3<HIGH>( v_[0]*tmp, v_[1]*tmp, v_[2]*tmp );
   }
}
//**********************************************************************************************************************


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

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

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

//**********************************************************************************************************************
/*!\fn Vector3<Type>::max( )
// \brief Returns maximum value
*/
template< typename Type >
inline Type Vector3<Type>::max( ) const {
   return std::max(v_[0], std::max(v_[1], v_[2]));
}
//**********************************************************************************************************************

//**********************************************************************************************************************
/*!\fn Vector3<Type>::min( )
// \brief Returns minimum value
*/
template< typename Type >
inline Type Vector3<Type>::min( ) const {
   return std::min(v_[0], std::min(v_[1], v_[2]));
}
//**********************************************************************************************************************

//**********************************************************************************************************************
/*!\fn Vector3<Type>::set( Type x, Type y, Type z )
// \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.
// \param z The initial value for the z-component.
*/
template< typename Type >
inline void Vector3<Type>::set( Type x, Type y, Type z )
{
   v_[0] = x;
   v_[1] = y;
   v_[2] = z;
}
//**********************************************************************************************************************

//**********************************************************************************************************************
/*!\fn Length Vector3<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 Vector3<Type>::length() const
{
   return std::sqrt( static_cast<typename SqrtTrait<Type>::Type>( v_[0]*v_[0] + v_[1]*v_[1] + v_[2]*v_[2] ) );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Type Vector3<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 Vector3<Type>::sqrLength() const
{
   return ( v_[0]*v_[0] + v_[1]*v_[1] + v_[2]*v_[2] );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\fn Vector3<Length>& Vector3<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 >
Vector3<typename Vector3<Type>::Length> Vector3<Type>::getNormalized() const
{
   const Length len( length() );

   WALBERLA_ASSERT_FLOAT_UNEQUAL( len, Length(0) );

   const Length ilen( Length(1) / len );

   Vector3<Length> result ( static_cast<Length>( v_[0] ) * ilen,
                            static_cast<Length>( v_[1] ) * ilen,
                            static_cast<Length>( v_[2] ) * ilen );

   WALBERLA_ASSERT_FLOAT_EQUAL( result.sqrLength(), 1.0 );

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

//**********************************************************************************************************************
/*!\fn Vector3<Length>& Vector3<Type>::getNormalized() const
// \brief Calculation of the normalized vector (\f$|\vec{a}|=1\f$) without precondition.
//
// \return The normalized vector or the original vector if vector is too small.
*/
template< typename Type >
Vector3<typename Vector3<Type>::Length> Vector3<Type>::getNormalizedOrZero() const
{
   const Length len( length() );

   if (floatIsEqual( len, Length(0) )) return *this;

   const Length ilen( Length(1) / len );

   Vector3<Length> result ( static_cast<Length>( v_[0] ) * ilen,
                            static_cast<Length>( v_[1] ) * ilen,
                            static_cast<Length>( v_[2] ) * ilen );

   WALBERLA_ASSERT_FLOAT_EQUAL( result.sqrLength(), 1.0 );

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

//**********************************************************************************************************************
/*!\fn void Vector3<Type>::reset()
// \brief Sets all components of the vector to 0.
*/
template< typename Type >
void Vector3<Type>::reset()
{
   v_[0] = 0;
   v_[1] = 0;
   v_[2] = 0;
}
//**********************************************************************************************************************




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

//**********************************************************************************************************************
/*!\name Vector3 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 Vector3<Type>& vec );
template< typename Type > inline bool operator==( char           scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( signed char    scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( wchar_t        scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( unsigned short scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( short          scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( unsigned int   scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( int            scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( unsigned long  scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( long           scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( float          scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( double         scalar, const Vector3<Type>& vec );
template< typename Type > inline bool operator==( long double    scalar, const Vector3<Type>& vec );

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

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

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

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

template< typename Type >
inline bool isinf( const Vector3<Type>& v );

template< typename Type >
inline bool finite( const Vector3<Type>& v );

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

template< typename Type >
inline const Vector3<Type> fabs( const Vector3<Type>& v );
//@}
For faster browsing, not all history is shown. View entire blame