SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
apfVector.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Scientific Computation Research Center
3  *
4  * This work is open source software, licensed under the terms of the
5  * BSD license as described in the LICENSE file in the top-level directory.
6  */
7 
8 #ifndef APF_VECTOR_H
9 #define APF_VECTOR_H
10 
14 #include <cmath>
15 #include "apfArray.h"
16 #include <ostream>
17 
18 namespace apf {
19 
23 extern double const pi;
24 
25 template <std::size_t N>
26 class Vector;
27 
45 template <std::size_t N>
46 class Vector : public Array<double,N>
47 {
48  public:
50  Vector() {}
52  Vector(double const* v)
53  {
54  for (std::size_t i=0; i < N; ++i)
55  (*this)[i] = v[i];
56  }
58  Vector<N> operator+(Vector<N> const& b) const
59  {
60  Vector<N> c;
61  for (std::size_t i=0; i < N; ++i)
62  c[i] = (*this)[i] + b[i];
63  return c;
64  }
67  {
68  for (std::size_t i=0; i < N; ++i)
69  (*this)[i] += b[i];
70  return (*this);
71  }
73  Vector<N> operator-(Vector<N> const& b) const
74  {
75  Vector<N> c;
76  for (std::size_t i=0; i < N; ++i)
77  c[i] = (*this)[i] - b[i];
78  return c;
79  }
83  Vector<N> operator*(double s) const
84  {
85  Vector<N> c;
86  for (std::size_t i=0; i < N; ++i)
87  c[i] = (*this)[i] * s;
88  return c;
89  }
92  Vector<N> operator/(double s) const
93  {
94  Vector<N> c;
95  for (std::size_t i=0; i < N; ++i)
96  c[i] = (*this)[i] / s;
97  return c;
98  }
103  double operator*(Vector<N> const& b) const
104  {
105  double r=0;
106  for (std::size_t i=0; i < N; ++i)
107  r += (*this)[i] * b[i];
108  return r;
109  }
111  double getLength() const {return sqrt((*this)*(*this));}
113  Vector<N> normalize() const {return (*this) / getLength();}
115  void zero()
116  {
117  for (std::size_t i=0; i < N; ++i)
118  (*this)[i] = 0.0;
119  }
120 };
121 
123 inline Vector<3> cross(Vector<3> const& a, Vector<3> const& b)
124 {
125  Vector<3> r;
126  r[0] = a[1]*b[2] - a[2]*b[1];
127  r[1] = a[2]*b[0] - a[0]*b[2];
128  r[2] = a[0]*b[1] - a[1]*b[0];
129  return r;
130 }
131 
133 template<std::size_t N>
135 {
136  return b*((a*b)/(b*b));
137 }
138 
140 template<std::size_t N>
141 Vector<N> reject(Vector<N> const& a, Vector<N> const& b)
142 {
143  return a - project(a, b);
144 }
145 
150 class Vector3 : public Vector<3>
151 {
152  public:
153  Vector3() {}
154  Vector3(Vector<3> const& other):
155  Vector<3>(other)
156  {}
159  Vector3(double a, double b, double c)
160  {
161  (*this)[0] = a;
162  (*this)[1] = b;
163  (*this)[2] = c;
164  }
167  Vector3(double const* abc)
168  {
169  (*this)[0] = abc[0];
170  (*this)[1] = abc[1];
171  (*this)[2] = abc[2];
172  }
175  void toArray(double* abc) const
176  {
177  abc[0] = (*this)[0];
178  abc[1] = (*this)[1];
179  abc[2] = (*this)[2];
180  }
183  void fromArray(const double* abc)
184  {
185  (*this)[0] = abc[0];
186  (*this)[1] = abc[1];
187  (*this)[2] = abc[2];
188  }
190  double x() const {return (*this)[0];}
192  double y() const {return (*this)[1];}
194  double z() const {return (*this)[2];}
196  double& x() {return (*this)[0];}
198  double& y() {return (*this)[1];}
200  double& z() {return (*this)[2];}
201 };
202 
203 }
204 
206 std::ostream& operator<<(std::ostream& s, apf::Vector3 const& v);
207 
208 #endif
double & z()
mutable z component
Definition: apfVector.h:200
double & y()
mutable y component
Definition: apfVector.h:198
double & x()
mutable x component
Definition: apfVector.h:196
void fromArray(const double *abc)
read vector from array
Definition: apfVector.h:183
Vector< N > & operator+=(Vector< N > const &b)
add a vector to this vector
Definition: apfVector.h:66
std::ostream & operator<<(std::ostream &s, apf::Vector3 const &v)
write apf::Vector3 to a C++ stream
void toArray(double *abc) const
write vector to array
Definition: apfVector.h:175
Vector< N > operator/(double s) const
divide a vector by a scalar
Definition: apfVector.h:92
void zero()
zero the vector
Definition: apfVector.h:115
double getLength() const
get the vector magnitude
Definition: apfVector.h:111
template-generic vector of N doubles
Definition: apfVector.h:26
Vector< N > operator+(Vector< N > const &b) const
add two vectors
Definition: apfVector.h:58
double y() const
immutable y component
Definition: apfVector.h:192
Vector3(double const *abc)
construct from array
Definition: apfVector.h:167
convenience wrapper over apf::Vector&lt;3&gt;
Definition: apfVector.h:150
double const pi
The mathematical constant pi.
Vector< N > normalize() const
divide the vector by its magnitude
Definition: apfVector.h:113
Vector()
mandatory
Definition: apfVector.h:50
double x() const
immutable x component
Definition: apfVector.h:190
double sqrt(double A)
wrapper for standard sqrt function
Definition: mthAD.h:588
Vector< N > operator-(Vector< N > const &b) const
subtract two vectors
Definition: apfVector.h:73
Vector< N > reject(Vector< N > const &a, Vector< N > const &b)
vector rejection
Definition: apfVector.h:141
double operator*(Vector< N > const &b) const
vector dot product
Definition: apfVector.h:103
Vector< N > operator*(double s) const
multiply a vector times a scalar
Definition: apfVector.h:83
double z() const
immutable z component
Definition: apfVector.h:194
Vector< N > project(Vector< N > const &a, Vector< N > const &b)
Returns vector (a) projected onto vector (b)
Definition: apfVector.h:134
Vector< 3 > cross(Vector< 3 > const &a, Vector< 3 > const &b)
3D vector cross product
Definition: apfVector.h:123
Vector3(double a, double b, double c)
construct from 3 values
Definition: apfVector.h:159
Vector(double const *v)
construct from array
Definition: apfVector.h:52