ESyS-Particle  2.3.2
vec3.h
Go to the documentation of this file.
1 
2 // //
3 // Copyright (c) 2003-2014 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.opensource.org/licenses/osl-3.0.php //
10 // //
12 
13 #ifndef __VEC3_H
14 #define __VEC3_H
15 
16 #define DO_INLINE_VEC3 1
17 
18 #if DO_INLINE_VEC3 >= 1
19 #define VEC3_INLINE inline
20 #else
21 #define VEC3_INLINE
22 #endif
23 
24 #include <iostream>
25 #include <math.h>
26 #include <string>
27 
28 #include "Foundation/Error.h"
29 
30 using std::ostream;
31 using std::istream;
32 using std::string;
33 
34 class Matrix3;
35 
36 class VecErr:public MError
37 {
38  public:
39  VecErr(const string&);
40  virtual ~VecErr(){};
41 };
42 
43 struct VDMulVadd;
44 struct VDMul;
45 
46 class Vec3
47 {
48 protected:
49  double data[3];
50 
51 public:
52  static const Vec3 ZERO;
53  // constructors
54  VEC3_INLINE Vec3();
55  VEC3_INLINE explicit Vec3(double s);
56  VEC3_INLINE Vec3(double,double,double);
57  VEC3_INLINE Vec3(const Vec3&);
58 
59  // vec-vec operators
60  VEC3_INLINE Vec3& operator=(const Vec3&);
61  VEC3_INLINE Vec3& operator=(double s);
64  VEC3_INLINE Vec3 operator+(const Vec3&) const;
65  VEC3_INLINE Vec3 operator-(const Vec3&) const;
66 //wangyc added !
67  VEC3_INLINE Vec3 operator*(const Matrix3 &m) const;
68  VEC3_INLINE double operator*(const Vec3&) const;
69  VEC3_INLINE Vec3 operator-() const;
70 
71  // vec-dbl ops
72  VEC3_INLINE Vec3 operator*(double) const;
73  VEC3_INLINE Vec3& operator*=(double);
74  VEC3_INLINE Vec3 operator/(double) const;
75  VEC3_INLINE Vec3 operator-(double) const;
76  VEC3_INLINE Vec3 operator+(double) const;
77  VEC3_INLINE Vec3& operator+=(double);
78  VEC3_INLINE Vec3& operator-=(double);
79 
80 // wangyc added !
81  VEC3_INLINE Vec3& operator/=(double);
82  VEC3_INLINE double norm() const;
83  VEC3_INLINE double norm2() const;
84  VEC3_INLINE Vec3 unit() const;
85  VEC3_INLINE Vec3 unit_s() const; //safe version (throw exceptions)
86  VEC3_INLINE double max() const;
87  VEC3_INLINE double min() const;
88 
89  VEC3_INLINE Vec3 rotate(const Vec3 &axis, const Vec3 &axisPt) const;
90 
91  VEC3_INLINE bool operator==(const Vec3&) const;
92  VEC3_INLINE bool operator!=(const Vec3&) const;
93 
94  VEC3_INLINE friend Vec3 cmax(const Vec3&,const Vec3&);
95  VEC3_INLINE friend Vec3 cmin(const Vec3&,const Vec3&);
96 
97  VEC3_INLINE friend Vec3 cross(const Vec3&,const Vec3&);
98  VEC3_INLINE friend double dot(const Vec3&,const Vec3&);
99  VEC3_INLINE friend Vec3 operator*(double,const Vec3&);
100 
101  //n+1-ary operators
102  VEC3_INLINE void mul_add_and_assign(const Vec3*,const Vec3*,const double&);
103  VEC3_INLINE void mul_and_assign(const Vec3*,const double&);
104 
105  VEC3_INLINE Vec3(const VDMulVadd&);
107 
108  VEC3_INLINE Vec3(const VDMul&);
109  VEC3_INLINE Vec3& operator=(const VDMul&);
110 
111  //access stuff
112 // wangyc added !
113  VEC3_INLINE void set_x(double x) {data[0] = x;}
114  VEC3_INLINE void set_y(double y) {data[1] = y;}
115  VEC3_INLINE void set_z(double z) {data[2] = z;}
116 // void set_xyz(double x, double y, double z)
117 // { data[0] = x; data[1] = y; data[2] = z;}
118 
119  VEC3_INLINE double& X() {return data[0];};
120  VEC3_INLINE double& Y() {return data[1];};
121  VEC3_INLINE double& Z() {return data[2];};
122  VEC3_INLINE double X() const {return data[0];};
123  VEC3_INLINE double Y() const {return data[1];};
124  VEC3_INLINE double Z() const {return data[2];};
125  VEC3_INLINE const double &operator[](int i) const {return data[i];}
126  VEC3_INLINE double& operator[](int i) {return data[i];}
127 
128  // in/output
129  VEC3_INLINE friend ostream& operator << (ostream&,const Vec3&);
130  VEC3_INLINE friend istream& operator >> (istream&,Vec3&);
131 
132  // comparison -> enable to use of Vec3 as key in STL map and set
133  bool operator<(const Vec3&) const;
134 
135  friend class Matrix3;
136 };
137 
138 VEC3_INLINE Vec3 comp_max(const Vec3&,const Vec3&);
139 VEC3_INLINE Vec3 comp_min(const Vec3&,const Vec3&);
140 
141 #if DO_INLINE_VEC3 >= 1
142 #include "Foundation/vec3.hpp"
143 #endif
144 
145 #endif // __VEC3_H