IFEM  90A354
Vec3.h
Go to the documentation of this file.
1 // $Id$
2 //==============================================================================
12 //==============================================================================
13 
14 #ifndef _VEC3_H
15 #define _VEC3_H
16 
17 #include <vector>
18 #include <iostream>
19 #include <cmath>
20 
21 
26 class Vec3
27 {
28  Real v[3];
29 
30 public:
31  Real& x;
32  Real& y;
33  Real& z;
34 
36  Vec3() : x(v[0]), y(v[1]), z(v[2]) { x = y = z = Real(0); }
38  Vec3(Real X, Real Y, Real Z = Real(0)) : x(v[0]), y(v[1]), z(v[2])
39  { x = X; y = Y; z = Z; }
41  Vec3(const Real* pos, size_t n = 3) : x(v[0]), y(v[1]), z(v[2])
42  { for (size_t i = 0; i < 3; i++) v[i] = i < n ? pos[i] : Real(0); }
44  Vec3(const std::vector<Real>& X) : x(v[0]), y(v[1]), z(v[2])
45  { for (size_t i = 0; i < 3; i++) v[i] = i < X.size() ? X[i] : Real(0); }
47  Vec3(const Vec3& X) : x(v[0]), y(v[1]), z(v[2])
48  { x = X.x; y = X.y; z = X.z; }
50  Vec3(const Vec3& X, const Vec3& Y) : x(v[0]), y(v[1]), z(v[2])
51  { this->cross(X,Y); }
52 
54  virtual ~Vec3() {}
55 
57  Vec3& operator=(const Vec3& X) { x = X.x; y = X.y; z = X.z; return *this; }
59  Vec3& operator=(Real val) { x = y = z = val; return *this; }
60 
62  const Real& operator()(int i) const { return v[i-1]; }
64  const Real& operator[](int i) const { return v[i]; }
66  Real& operator()(int i) { return v[i-1]; }
68  Real& operator[](int i) { return v[i]; }
69 
71  const Real* ptr() const { return v; }
72 
74  std::vector<Real> vec(size_t n = 3) const
75  { return std::vector<Real>(v, v + (n < 3 ? n : 3)); }
76 
78  Vec3& operator*=(Real c) { x *= c; y *= c; z *= c; return *this; }
80  Vec3& operator/=(Real d) { x /= d; y /= d; z /= d; return *this; }
81 
83  Vec3& operator+=(const Vec3& X)
84  {
85  x += X.x;
86  y += X.y;
87  z += X.z;
88  return *this;
89  }
90 
92  Vec3& operator-=(const Vec3& X)
93  {
94  x -= X.x;
95  y -= X.y;
96  z -= X.z;
97  return *this;
98  }
99 
101  Vec3 operator-() const { return Vec3(-x,-y,-z); }
102 
104  Real sum() const { return x+y+z; }
106  Real asum() const { return std::fabs(x) + std::fabs(y) + std::fabs(z); }
107 
109  Real length2() const { return x*x+y*y+z*z; }
110 
112  double length() const { return sqrt(this->length2()); }
113 
115  Real normalize(double tol = 1.0e-16)
116  {
117  double len = this->length();
118  if (len <= tol) return len;
119  x /= len; y /= len; z /= len;
120  return len;
121  }
122 
124  Vec3& cross(const Vec3& a, const Vec3& b)
125  {
126  x = a.y*b.z - a.z*b.y;
127  y = a.z*b.x - a.x*b.z;
128  z = a.x*b.y - a.y*b.x;
129  return *this;
130  }
131 
133  bool equal(const Vec3& a, double tol = 1.0e-6) const
134  {
135  if (fabs(x-a.x) <= tol)
136  if (fabs(y-a.y) <= tol)
137  if (fabs(z-a.z) <= tol)
138  return true;
139 
140  return false;
141  }
142 
144  bool isZero(double tol = 1.0e-6) const
145  {
146  if (fabs(x) <= tol)
147  if (fabs(y) <= tol)
148  if (fabs(z) <= tol)
149  return true;
150 
151  return false;
152  }
153 
158  bool lessThan(const Vec3& a, double tol = 1.0e-6) const
159  {
160  for (int i = 2; i >= 0; i--)
161  if (v[i]+tol < a.v[i])
162  return true;
163  else if (v[i]-tol > a.v[i])
164  return false;
165 
166  return false;
167  }
168 
170  bool inside(const Vec3& a, const Vec3& b, double tol = 1.0e-6) const
171  {
172  for (int i = 0; i < 3; i++)
173  if (v[i]+tol < a.v[i] || v[i]-tol > b.v[i])
174  return false;
175 
176  return true;
177  }
178 
180  virtual std::ostream& print(std::ostream& os, double tol = 1.0e-12) const
181  {
182  if (fabs(x) > tol)
183  os << x;
184  else
185  os <<"0";
186 
187  if (fabs(y) > tol)
188  os <<" "<< y;
189  else
190  os <<" 0";
191 
192  if (fabs(z) > tol)
193  os <<" "<< z;
194  else
195  os <<" 0";
196 
197  return os;
198  }
199 
200  static double comparisonTolerance;
201 };
202 
203 
208 class Vec4 : public Vec3
209 {
210 public:
211  const Real* u;
212 
213  Real t;
214  int idx;
215 
217  explicit Vec4(const Real* par = nullptr, Real T = Real(0))
218  {
219  u = par;
220  t = T;
221  idx = -1;
222  }
223 
225  explicit Vec4(Real T)
226  {
227  u = nullptr;
228  t = T;
229  idx = -1;
230  }
231 
233  Vec4(Real X, Real Y, Real Z, Real T = Real(0)) : Vec3(X,Y,Z)
234  {
235  u = nullptr;
236  t = T;
237  idx = -1;
238  }
239 
241  Vec4(const Vec3& X, Real T = Real(0), int id = -1) : Vec3(X)
242  {
243  u = nullptr;
244  t = T;
245  idx = id;
246  }
247 
249  Vec4(const Vec3& X, Real T, const Real* par) : Vec3(X)
250  {
251  u = par;
252  t = T;
253  idx = -1;
254  }
255 
257  Vec4(const std::vector<Real>& X, const Real* par = nullptr) : Vec3(X)
258  {
259  u = par;
260  t = X.size() > 3 ? X[3] : Real(0);
261  idx = -1;
262  }
263 
265  Vec4(const Vec4& X) : Vec3(X)
266  {
267  u = X.u;
268  t = X.t;
269  idx = X.idx;
270  }
271 
273  Vec4& operator=(const Vec4& X)
274  {
275  x = X.x;
276  y = X.y;
277  z = X.z;
278  t = X.t;
279  idx = X.idx;
280  u = X.u;
281 
282  return *this;
283  }
284 
286  Vec4& operator=(Real val) { x = y = z = val; return *this; }
287 
289  Vec4& assign(const Vec3& X)
290  {
291  const Vec4* x4 = dynamic_cast<const Vec4*>(&X);
292  if (x4)
293  this->operator=(*x4);
294  else
295  {
296  x = X.x;
297  y = X.y;
298  z = X.z;
299  }
300 
301  return *this;
302  }
303 
305  std::ostream& print(std::ostream& os, double tol = 1.0e-12) const override
306  {
307  if (idx >= 0) os <<"(idx="<< idx <<") ";
308  this->Vec3::print(os,tol);
309  if (u) os <<" ("<< u[0] <<" "<< u[1] <<" "<< u[2] <<")";
310  if (t > Real(0)) os <<" "<< t;
311  return os;
312  }
313 };
314 
315 
316 using Vec3Pair = std::pair<Vec3,Vec3>;
317 using PointValue = std::pair<Vec3,Real>;
318 using Vec3Vec = std::vector<Vec3>;
319 using PointValues = std::vector<PointValue>;
320 
321 #endif
#define Real
The floating point type to use.
Definition: ImmersedBoundaries.h:18
std::vector< Vec3 > Vec3Vec
An array of point vectors.
Definition: Vec3.h:318
std::pair< Vec3, Real > PointValue
A point with associated value.
Definition: Vec3.h:317
std::pair< Vec3, Vec3 > Vec3Pair
A pair of two point vectors.
Definition: Vec3.h:316
std::vector< PointValue > PointValues
An array of point values.
Definition: Vec3.h:319
Simple class for representing a point in 3D space.
Definition: Vec3.h:27
Vec3 & operator+=(const Vec3 &X)
Add the given vector X to *this.
Definition: Vec3.h:83
virtual std::ostream & print(std::ostream &os, double tol=1.0e-12) const
Print out a vector.
Definition: Vec3.h:180
bool lessThan(const Vec3 &a, double tol=1.0e-6) const
Check if one vector is less than the other.
Definition: Vec3.h:158
Vec3(const Vec3 &X, const Vec3 &Y)
Constructor creating a cross product of two other vectors.
Definition: Vec3.h:50
Real & operator()(int i)
Indexing operator for component assignment (1-based).
Definition: Vec3.h:66
const Real & operator[](int i) const
Indexing operator for component reference (0-based).
Definition: Vec3.h:64
Real asum() const
Return the sum of absolute values of the vector components.
Definition: Vec3.h:106
Real length2() const
Return the square of the length of the vector.
Definition: Vec3.h:109
bool equal(const Vec3 &a, double tol=1.0e-6) const
Equality check between two vectors.
Definition: Vec3.h:133
Vec3 & operator/=(Real d)
Division by a scalar.
Definition: Vec3.h:80
Real & x
Reference to X-component.
Definition: Vec3.h:31
const Real & operator()(int i) const
Indexing operator for component reference (1-based).
Definition: Vec3.h:62
Vec3 & cross(const Vec3 &a, const Vec3 &b)
Cross product between two vectors.
Definition: Vec3.h:124
virtual ~Vec3()
Empty destructor.
Definition: Vec3.h:54
Vec3 & operator=(Real val)
Overloaded assignment operator.
Definition: Vec3.h:59
Real v[3]
The actual point vector.
Definition: Vec3.h:28
Vec3()
Default constructor creating a point at origin.
Definition: Vec3.h:36
static double comparisonTolerance
Coordinate comparison tolerance.
Definition: Vec3.h:200
Real sum() const
Return the sum of the vector components.
Definition: Vec3.h:104
Vec3 operator-() const
Negation operator.
Definition: Vec3.h:101
Vec3 & operator=(const Vec3 &X)
Assignment operator.
Definition: Vec3.h:57
std::vector< Real > vec(size_t n=3) const
Conversion to std::vector.
Definition: Vec3.h:74
bool isZero(double tol=1.0e-6) const
Check if the vector is zero with the given tolerance.
Definition: Vec3.h:144
const Real * ptr() const
Reference through a pointer.
Definition: Vec3.h:71
Vec3 & operator*=(Real c)
Multiplication with a scalar.
Definition: Vec3.h:78
double length() const
Return the length of the vector.
Definition: Vec3.h:112
Real & z
Reference to Z-component.
Definition: Vec3.h:33
Vec3(const std::vector< Real > &X)
Constructor creating a point from the given std::vector.
Definition: Vec3.h:44
Real normalize(double tol=1.0e-16)
Normalize the vector and return its length.
Definition: Vec3.h:115
Real & y
Reference to Y-component.
Definition: Vec3.h:32
Real & operator[](int i)
Indexing operator for component assignment (0-based).
Definition: Vec3.h:68
Vec3(const Real *pos, size_t n=3)
Constructor creating a point at the specified location.
Definition: Vec3.h:41
Vec3 & operator-=(const Vec3 &X)
Subtract the given vector X from *this.
Definition: Vec3.h:92
Vec3(const Vec3 &X)
Copy constructor.
Definition: Vec3.h:47
bool inside(const Vec3 &a, const Vec3 &b, double tol=1.0e-6) const
Check if a vector is inside the box defined by two other vectors.
Definition: Vec3.h:170
Vec3(Real X, Real Y, Real Z=Real(0))
Constructor creating a point at the specified location.
Definition: Vec3.h:38
Simple class for representing a point in 3D space and time.
Definition: Vec3.h:209
Vec4(Real T)
Constructor creating a point at origin and time T.
Definition: Vec3.h:225
Vec4(const Vec3 &X, Real T=Real(0), int id=-1)
Constructor creating a point at the specified location.
Definition: Vec3.h:241
Vec4(Real X, Real Y, Real Z, Real T=Real(0))
Constructor creating a point at the specified location.
Definition: Vec3.h:233
Vec4(const Vec4 &X)
Copy constructor.
Definition: Vec3.h:265
Real t
The time coordinate.
Definition: Vec3.h:213
Vec4(const std::vector< Real > &X, const Real *par=nullptr)
Constructor creating a point from the given std::vector.
Definition: Vec3.h:257
Vec4 & operator=(const Vec4 &X)
Assignment operator.
Definition: Vec3.h:273
Vec4 & operator=(Real val)
Overloaded assignment operator.
Definition: Vec3.h:286
std::ostream & print(std::ostream &os, double tol=1.0e-12) const override
Print out a vector.
Definition: Vec3.h:305
Vec4(const Real *par=nullptr, Real T=Real(0))
Default constructor creating a point at origin.
Definition: Vec3.h:217
Vec4 & assign(const Vec3 &X)
Assignment method.
Definition: Vec3.h:289
int idx
Nodal point index.
Definition: Vec3.h:214
const Real * u
Spline parameters of point.
Definition: Vec3.h:211
Vec4(const Vec3 &X, Real T, const Real *par)
Constructor creating a point at the specified location.
Definition: Vec3.h:249