ESyS-Particle  2.3
vec3.h
Go to the documentation of this file.
1 // //
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 #include <iostream>
17 #include <math.h>
18 #include <string>
19 
20 #include "Error.h"
21 
22 using std::ostream;
23 using std::istream;
24 using std::string;
25 
26 class Mat3;
27 
28 class VecErr:public MError
29 {
30  public:
31  VecErr(const string&);
32  virtual ~VecErr(){};
33 };
34 
35 struct VDMulVadd;
36 struct VDMul;
37 
38 class Vec3
39 {
40  protected:
41  double data[3];
42 
43  public:
44  // constructors
45  Vec3();
46  Vec3(double,double,double);
47  Vec3(const Vec3&);
48 
49  // vec-vec operators
50  Vec3& operator=(const Vec3&);
51  Vec3& operator-=(const Vec3&);
52  Vec3& operator+=(const Vec3&);
53  Vec3 operator+(const Vec3&) const;
54  Vec3 operator-(const Vec3&) const;
55  double operator*(const Vec3&) const;
56  inline Vec3 operator-() { return Vec3(-data[0],-data[1],-data[2]); } ;
57 
58  // vec-dbl ops
59  Vec3 operator*(double) const;
60  Vec3 operator/(double) const;
61 
62  double norm() const;
63  double norm2() const;
64  Vec3 unit() const;
65  Vec3 unit_s() const; //safe version (throw exceptions)
66  double max() const;
67  double min() const;
68 
69  bool operator==(const Vec3&);
70  bool operator!=(const Vec3&);
71 
72  friend Vec3 cmax(const Vec3&,const Vec3&);
73  friend Vec3 cmin(const Vec3&,const Vec3&);
74 
75  friend Vec3 cross(const Vec3&,const Vec3&);
76 
77  friend Vec3 operator*(double,const Vec3&);
78 
79  //n+1-ary operators
80  void mul_add_and_assign(const Vec3*,const Vec3*,const double&);
81  void mul_and_assign(const Vec3*,const double&);
82 
83  Vec3(const VDMulVadd&);
84  Vec3& operator=(const VDMulVadd&);
85 
86  Vec3(const VDMul&);
87  Vec3& operator=(const VDMul&);
88 
89  //access stuff
90  inline double X() const {return data[0];};
91  inline double Y() const {return data[1];};
92  inline double Z() const {return data[2];};
93  inline double operator[](int i) const {return data[i];};
94  inline double& operator[](int i) {return data[i];};
95 
96  // in/output
97  friend ostream& operator << (ostream&,const Vec3&);
98  friend istream& operator >> (istream&,Vec3&);
99 
100  friend class Mat3;
101 };
102 
103 //------------------------------
104 // stuff for 'n+1-ary' ops
105 // see Stroustrup, p.675 f.
106 //------------------------------
107 
108 struct VDMul
109 {
110  const Vec3& v;
111  const double d;
112 
113  VDMul(const Vec3& vv, const double& dd):v(vv),d(dd){}
114 
115  // operator Vec3(){return Vec3(v.x*d,v.y*d,v.z*d);};
116 };
117 
118 inline VDMul operator*(const Vec3& vv, const double& dd)
119 {
120  return VDMul(vv,dd);
121 }
122 
123 struct VDMulVadd
124 {
125  const Vec3& v1;
126  const Vec3& v2;
127  const double& d;
128 
129  VDMulVadd(const VDMul vd,const Vec3& vv):v1(vd.v),v2(vv),d(vd.d){}
130 
131  //operator Vec3();
132 };
133 
134 inline VDMulVadd operator+(const VDMul vd,const Vec3& vv)
135 {
136  return VDMulVadd(vd,vv);
137 }
138 
139 #endif
VEC3_INLINE Vec3 & operator=(const Vec3 &)
Definition: vec3.hpp:56
VEC3_INLINE Vec3 operator/(double) const
Definition: vec3.hpp:139
const Vec3 & v2
Definition: vec3.h:126
Definition: vec3.h:46
VEC3_INLINE void mul_add_and_assign(const Vec3 *, const Vec3 *, const double &)
Definition: vec3.cpp:234
const Vec3 & v
Definition: vec3.h:110
VecErr(const string &)
Definition: vec3.hpp:20
virtual ~VecErr()
Definition: vec3.h:32
const double d
Definition: vec3.h:111
const double & d
Definition: vec3.h:127
friend class Mat3
Definition: vec3.h:100
VEC3_INLINE bool operator!=(const Vec3 &) const
Definition: vec3.hpp:278
VDMul(const Vec3 &vv, const double &dd)
Definition: vec3.h:113
VEC3_INLINE Vec3 operator+(const Vec3 &) const
Definition: vec3.hpp:88
double Y() const
Definition: vec3.h:91
double & operator[](int i)
Definition: vec3.h:94
VEC3_INLINE double norm() const
Definition: vec3.hpp:211
VDMul operator*(const Vec3 &vv, const double &dd)
Definition: vec3.h:118
VEC3_INLINE Vec3 & operator-=(const Vec3 &)
Definition: vec3.hpp:72
VEC3_INLINE bool operator==(const Vec3 &) const
Definition: vec3.hpp:273
const Vec3 & v1
Definition: vec3.h:125
VEC3_INLINE friend ostream & operator<<(ostream &, const Vec3 &)
Definition: vec3.cpp:194
Definition: vec3.h:108
VDMulVadd operator+(const VDMul vd, const Vec3 &vv)
Definition: vec3.h:134
VEC3_INLINE void mul_and_assign(const Vec3 *, const double &)
Definition: vec3.cpp:241
Definition: vec3.h:36
VEC3_INLINE friend Vec3 cmin(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:240
VEC3_INLINE double max() const
Definition: vec3.hpp:258
VEC3_INLINE friend Vec3 cmax(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:231
VEC3_INLINE Vec3()
The zero vector.
Definition: vec3.hpp:26
Definition: vec3.h:123
VEC3_INLINE friend Vec3 cross(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:187
VEC3_INLINE friend istream & operator>>(istream &, Vec3 &)
Definition: vec3.cpp:201
VEC3_INLINE Vec3 operator*(const Matrix3 &m) const
Definition: vec3.hpp:103
VEC3_INLINE Vec3 operator-() const
Definition: vec3.hpp:98
double X() const
Definition: vec3.h:90
double operator[](int i) const
Definition: vec3.h:93
double Z() const
Definition: vec3.h:92
Vec3 operator-()
Definition: vec3.h:56
VEC3_INLINE double min() const
Definition: vec3.hpp:266
VEC3_INLINE Vec3 & operator+=(const Vec3 &)
Definition: vec3.hpp:80
VDMulVadd(const VDMul vd, const Vec3 &vv)
Definition: vec3.h:129
double data[3]
Definition: vec3.h:49
VEC3_INLINE Vec3 unit_s() const
Definition: vec3.hpp:250
Definition: Error.h:21
VEC3_INLINE double norm2() const
Definition: vec3.hpp:218
VEC3_INLINE Vec3 unit() const
Definition: vec3.hpp:225