OpenMEEG
vect3.h
Go to the documentation of this file.
1 // Project Name: OpenMEEG (http://openmeeg.github.io)
2 // © INRIA and ENPC under the French open source license CeCILL-B.
3 // See full copyright notice in the file LICENSE.txt
4 // If you make a copy of this file, you must either:
5 // - provide also LICENSE.txt and modify this header to refer to it.
6 // - replace this header by the LICENSE.txt content.
7 
8 #pragma once
9 
10 #ifdef WIN32
11 #define _USE_MATH_DEFINES
12 #define _CRT_SECURE_NO_DEPRECATE 1
13 #endif
14 #include <cstdlib>
15 #include <cmath>
16 #include <iostream>
17 #include <om_common.h>
18 #include <vector>
19 
20 #include <OpenMEEG_Export.h>
21 
22 namespace OpenMEEG {
23 
24  inline double sqr(const double x) { return x*x; }
25 
27 
28  class OPENMEEG_EXPORT Vect3 {
29 
30  double m[3];
31 
32  public:
33 
34  Vect3(const double x1,const double x2,const double x3) { m[0] = x1; m[1] = x2; m[2] = x3; }
35  Vect3(const double a=0.0) { std::fill(&m[0],&m[3],a); }
36 
37  Vect3(const Vect3& v) { std::copy(&v.m[0],&v.m[3],&m[0]); }
38 
39  operator const double*() const { return m; }
40 
41  Vect3& operator=(const double v) {
42  for (unsigned i=0;i<3;++i)
43  m[i] = v;
44  return *this;
45  }
46 
47  Vect3& operator=(const Vect3& v) {
48  std::copy(&v.m[0],&v.m[3],&m[0]);
49  return *this;
50  }
51 
52  double& x() { return m[0]; }
53  double x() const { return m[0]; }
54 
55  double& y() { return m[1]; }
56  double y() const { return m[1]; }
57 
58  double& z() { return m[2]; }
59  double z() const { return m[2]; }
60 
61  double operator<(const Vect3& v) const { return ((m[0]!=v.x()) ? (m[0]<v.x()) : ((m[1]!=v.y()) ? (m[1]<v.y()) : (m[2]<v.z()))); }
62 
63  double norm() const { return sqrt(norm2()); }
64  double norm2() const { return sqr(m[0])+sqr(m[1])+sqr(m[2]); }
65 
66  bool operator==(const Vect3& v) const { return (m[0]==v.x() && m[1]==v.y() && m[2]==v.z()); }
67  bool operator!=(const Vect3& v) const { return (m[0]!=v.x() || m[1]!=v.y() || m[2]!=v.z()); }
68 
69  void operator+=(const Vect3& v) { m[0] += v.x(); m[1] += v.y(); m[2] += v.z(); }
70  void operator-=(const Vect3& v) { m[0] -= v.x(); m[1] -= v.y(); m[2] -= v.z(); }
71  void operator*=(const double d) { m[0] *= d; m[1] *= d; m[2] *= d; }
72  void operator/=(const double d) { operator*=(1.0/d); }
73 
74  void multadd(const double d,const Vect3& v) {m[0] += d*v.x(); m[1] += d*v.y(); m[2] += d*v.z();}
75 
76  Vect3 operator+(const Vect3& v) const { return Vect3(m[0]+v.x(),m[1]+v.y(),m[2]+v.z()); }
77  Vect3 operator-(const Vect3& v) const { return Vect3(m[0]-v.x(),m[1]-v.y(),m[2]-v.z()); }
78  Vect3 operator^(const Vect3& v) const { return Vect3(m[1]*v.z()-m[2]*v.y(),m[2]*v.x()-m[0]*v.z(),m[0]*v.y()-m[1]*v.x()); }
79  Vect3 operator*(const double d) const { return Vect3(d*m[0],d*m[1],d*m[2]); }
80  Vect3 operator/(const double d) const { return Vect3(m[0]/d,m[1]/d,m[2]/d); }
81 
82  double operator()(const int i) const {
83  om_assert(i>=0 && i<3);
84  return m[i];
85  }
86 
87  double& operator()(const int i) {
88  om_assert(i>=0 && i<3);
89  return m[i];
90  }
91 
92  Vect3 operator-() const { return Vect3(-m[0],-m[1],-m[2]); }
93 
94  inline double solid_angle(const Vect3& v1,const Vect3& v2,const Vect3& v3) const;
95 
97  *this /= (*this).norm();
98  return *this;
99  }
100 
101  friend std::ostream& operator<<(std::ostream& os,const Vect3& v);
102  friend std::istream& operator>>(std::istream& is,Vect3& v);
103  };
104 
105  inline Vect3 operator*(const double d,const Vect3& V) { return V*d; }
106  inline double dotprod(const Vect3& V1,const Vect3& V2) { return V1.x()*V2.x()+V1.y()*V2.y()+V1.z()*V2.z(); }
107  inline Vect3 crossprod(const Vect3& V1,const Vect3& V2) { return V1^V2; }
108  inline double det(const Vect3& V1,const Vect3& V2,const Vect3& V3) { return dotprod(V1,crossprod(V2,V3)); }
109 
110  inline double Vect3::solid_angle(const Vect3& V1,const Vect3& V2,const Vect3& V3) const {
111  // De Munck : Good sign directly
112  const Vect3& V0 = *this;
113  const Vect3& Y1 = V1-V0;
114  const Vect3& Y2 = V2-V0;
115  const Vect3& Y3 = V3-V0;
116  const double y1 = Y1.norm();
117  const double y2 = Y2.norm();
118  const double y3 = Y3.norm();
119  const double d = det(Y1,Y2,Y3);
120  return (fabs(d)<1e-10) ? 0.0 : 2*atan2(d,(y1*y2*y3+y1*dotprod(Y2,Y3)+y2*dotprod(Y3,Y1)+y3*dotprod(Y1,Y2)));
121  }
122 
123  inline std::istream& operator>>(std::istream& is,Vect3& v) {
124  return is >> v.x() >> v.y() >> v.z();
125  }
126 
127  inline std::ostream& operator<<(std::ostream& os,const Vect3& v) {
128  return os << v.x() << ' ' << v.y() << ' ' << v.z() ;
129  }
130 
131  typedef Vect3 Normal;
132  typedef std::vector<Normal> Normals;
133 }
Vect3.
Definition: vect3.h:28
Vect3 operator^(const Vect3 &v) const
Definition: vect3.h:78
Vect3 operator+(const Vect3 &v) const
Definition: vect3.h:76
double & y()
Definition: vect3.h:55
Vect3 operator*(const double d) const
Definition: vect3.h:79
void operator+=(const Vect3 &v)
Definition: vect3.h:69
double operator<(const Vect3 &v) const
Definition: vect3.h:61
void operator-=(const Vect3 &v)
Definition: vect3.h:70
double norm() const
Definition: vect3.h:63
Vect3 operator-(const Vect3 &v) const
Definition: vect3.h:77
double solid_angle(const Vect3 &v1, const Vect3 &v2, const Vect3 &v3) const
Definition: vect3.h:110
double & operator()(const int i)
Definition: vect3.h:87
bool operator!=(const Vect3 &v) const
Definition: vect3.h:67
double & z()
Definition: vect3.h:58
void operator/=(const double d)
Definition: vect3.h:72
double operator()(const int i) const
Definition: vect3.h:82
Vect3 operator/(const double d) const
Definition: vect3.h:80
double norm2() const
Definition: vect3.h:64
Vect3(const double a=0.0)
Definition: vect3.h:35
Vect3 & operator=(const Vect3 &v)
Definition: vect3.h:47
void operator*=(const double d)
Definition: vect3.h:71
Vect3 & normalize()
Definition: vect3.h:96
Vect3(const Vect3 &v)
Definition: vect3.h:37
Vect3 operator-() const
Definition: vect3.h:92
double & x()
Definition: vect3.h:52
double y() const
Definition: vect3.h:56
void multadd(const double d, const Vect3 &v)
Definition: vect3.h:74
Vect3 & operator=(const double v)
Definition: vect3.h:41
double z() const
Definition: vect3.h:59
Vect3(const double x1, const double x2, const double x3)
Definition: vect3.h:34
bool operator==(const Vect3 &v) const
Definition: vect3.h:66
double x() const
Definition: vect3.h:53
Vect3 operator*(const double d, const Vect3 &V)
Definition: vect3.h:105
Vect3 crossprod(const Vect3 &V1, const Vect3 &V2)
Definition: vect3.h:107
std::ostream & operator<<(std::ostream &os, const Conductivity< REP > &m)
double det(const Vect3 &V1, const Vect3 &V2, const Vect3 &V3)
Definition: vect3.h:108
Vect3 Normal
Definition: vect3.h:131
std::istream & operator>>(std::istream &is, Conductivity< REP > &m)
double sqr(const double x)
Definition: vect3.h:24
std::vector< Normal > Normals
Definition: vect3.h:132
double dotprod(const Vect3 &V1, const Vect3 &V2)
Definition: vect3.h:106