ESyS-Particle  2.3.4
vec3.h
Go to the documentation of this file.
1 // //
3 // Copyright (c) 2003-2017 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.apache.org/licenses/LICENSE-2.0 //
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&);
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::mul_and_assign
void mul_and_assign(const Vec3 *, const double &)
operator*
VDMul operator*(const Vec3 &vv, const double &dd)
Definition: vec3.h:118
Vec3::max
double max() const
Vec3::Vec3
Vec3(double, double, double)
Vec3::Vec3
Vec3(const VDMulVadd &)
Vec3::Vec3
Vec3()
Vec3::operator=
Vec3 & operator=(const VDMulVadd &)
Vec3::operator=
Vec3 & operator=(const VDMul &)
Vec3::cmin
VEC3_INLINE friend Vec3 cmin(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:240
Vec3::operator=
Vec3 & operator=(const Vec3 &)
Vec3::operator<<
VEC3_INLINE friend ostream & operator<<(ostream &, const Vec3 &)
Definition: vec3.cpp:194
VDMul::d
const double d
Definition: vec3.h:111
operator+
VDMulVadd operator+(const VDMul vd, const Vec3 &vv)
Definition: vec3.h:134
Vec3::data
double data[3]
Definition: vec3.h:49
Vec3::Z
double Z() const
Definition: vec3.h:92
Vec3::cmax
VEC3_INLINE friend Vec3 cmax(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:231
Vec3::operator-
Vec3 operator-()
Definition: vec3.h:56
VDMul
Definition: vec3.h:109
Vec3::operator!=
VEC3_INLINE bool operator!=(const Vec3 &) const
Definition: vec3.hpp:278
Vec3::unit
Vec3 unit() const
VDMulVadd::VDMulVadd
VDMulVadd(const VDMul vd, const Vec3 &vv)
Definition: vec3.h:129
Vec3::cross
VEC3_INLINE friend Vec3 cross(const Vec3 &, const Vec3 &)
Definition: vec3.hpp:187
VecErr
Definition: vec3.h:37
Vec3::operator>>
VEC3_INLINE friend istream & operator>>(istream &, Vec3 &)
Definition: vec3.cpp:201
Vec3::norm
double norm() const
Vec3::operator-
Vec3 operator-(const Vec3 &) const
Vec3::operator*
double operator*(const Vec3 &) const
Vec3::operator==
VEC3_INLINE bool operator==(const Vec3 &) const
Definition: vec3.hpp:273
Vec3::Vec3
Vec3(const VDMul &)
Error.h
Vec3::Vec3
Vec3(const Vec3 &)
Vec3::X
double X() const
Definition: vec3.h:90
VDMulVadd::v2
const Vec3 & v2
Definition: vec3.h:126
Vec3::operator[]
double operator[](int i) const
Definition: vec3.h:93
VDMul::v
const Vec3 & v
Definition: vec3.h:110
Vec3::min
double min() const
Vec3::mul_add_and_assign
void mul_add_and_assign(const Vec3 *, const Vec3 *, const double &)
Vec3::Y
double Y() const
Definition: vec3.h:91
MError
Definition: Error.h:22
Vec3
Definition: vec3.h:47
Vec3::operator*
Vec3 operator*(double) const
Vec3::operator-=
Vec3 & operator-=(const Vec3 &)
Vec3::operator+
Vec3 operator+(const Vec3 &) const
VDMulVadd::d
const double & d
Definition: vec3.h:127
Vec3::norm2
double norm2() const
VDMul::VDMul
VDMul(const Vec3 &vv, const double &dd)
Definition: vec3.h:113
VDMulVadd
Definition: vec3.h:124
Vec3::operator+=
Vec3 & operator+=(const Vec3 &)
VecErr::VecErr
VecErr(const string &)
Vec3::unit_s
Vec3 unit_s() const
Vec3::Mat3
friend class Mat3
Definition: vec3.h:100
VDMulVadd::v1
const Vec3 & v1
Definition: vec3.h:125
Vec3::operator*
VEC3_INLINE Vec3 operator*(const Matrix3 &m) const
Definition: vec3.hpp:103
Vec3::operator/
Vec3 operator/(double) const
VecErr::~VecErr
virtual ~VecErr()
Definition: vec3.h:32
Vec3::operator[]
double & operator[](int i)
Definition: vec3.h:94