D3Vector.h
Go to the documentation of this file.
1 /** Vector a 3-vector of double.
2  *
3  * See also WireCell::Point.
4  */
5 
6 #ifndef WIRECELLDATA_VECTOR
7 #define WIRECELLDATA_VECTOR
8 
9 #include <cmath>
10 #include <algorithm>
11 #include <vector>
12 #include <iostream> // for ostream
13 
14 namespace WireCell {
15 
16  /** Dimension-3 vector class.
17  *
18  * Adapted in laziness from:
19  * http://rosettacode.org/wiki/Vector_products#C.2B.2B
20  *
21  */
22  template< class T >
23  class D3Vector {
24 
25  template< class U >
26  friend std::ostream & operator<<( std::ostream & , const D3Vector<U> & ) ;
27 
28  typedef std::vector<T> D3VectorStore;
29  D3VectorStore m_v;
30 
31  public :
32 
33 
34  /// Construct from elements.
35  D3Vector( const T& a=0, const T& b=0, const T& c=0) : m_v(3) { this->set(a,b,c); }
36 
37  // Copy constructor.
38  D3Vector( const D3Vector& o) : m_v(3) { this->set(o.x(), o.y(), o.z()); }
39 
40  D3Vector( const T d[3] ) : m_v(3) { this->set(d[0], d[1], d[2]); }
41 
42  // Assignment.
44  this->set(o.x(), o.y(), o.z());
45  return *this;
46  }
47 
48  /// Set vector from elements;
49  void set(const T& a=0, const T& b=0, const T& c=0) {
50  m_v.resize(3);
51  m_v[0] = a;
52  m_v[1] = b;
53  m_v[2] = c;
54  }
55  T x(const T& val) { return m_v[0] = val; }
56  T y(const T& val) { return m_v[1] = val; }
57  T z(const T& val) { return m_v[2] = val; }
58 
59 
60  /// Convert from other typed vector.
61  template< class TT >
62  D3Vector( const D3Vector<TT>& o) : m_v(3) { this->set(o.x(), o.y(), o.z()); }
63 
64  /// Access elements by name.
65  T x() const { return m_v[0]; }
66  T y() const { return m_v[1]; }
67  T z() const { return m_v[2]; }
68 
69  /// Access elements by copy.
70  T operator[](std::size_t index) const {
71  return m_v.at(index);
72  }
73 
74  /// Access elements by reference.
75  T& operator[](std::size_t index) {
76  return m_v.at(index); // throw if out of bounds
77  }
78 
79  /// Return the dot product of this vector and the other.
80  T dot ( const D3Vector & rhs ) const {
81  T scalar = x() * rhs.x() + y() * rhs.y() + z() * rhs.z() ;
82  return scalar;
83  }
84 
85  /// Return the magnitude of this vector.
86  T magnitude() const {
87  return std::sqrt(x()*x()+y()*y()+z()*z());
88  }
89 
90  /// Return a normalized vector in the direction of this vector.
91  D3Vector norm() const {
92  T m = this->magnitude();
93  if (m <= 0) {
94  return D3Vector();
95  }
96  return D3Vector(x()/m, y()/m, z()/m);
97  }
98 
99  /// Return the cross product of this vector and the other.
100  D3Vector cross ( const D3Vector & rhs ) const {
101  T a = y() * rhs.z() - z() * rhs.y() ;
102  T b = z() * rhs.x() - x() * rhs.z() ;
103  T c = x() * rhs.y() - y() * rhs.x() ;
104  D3Vector product( a , b , c ) ;
105  return product ;
106  }
107 
108  /// Return the triple cross product of this vector and the other two.
109  D3Vector triplevec( D3Vector & a , D3Vector & b ) const {
110  return cross ( a.cross( b ) ) ;
111  }
112  /// Return the dot-cross product of this vector and the other two.
113  T triplescal( D3Vector & a, D3Vector & b ) const {
114  return dot( a.cross( b ) ) ;
115  }
116 
117  bool operator< (const D3Vector& rhs) const {
118  if (z() < rhs.z()) return true;
119  if (y() < rhs.y()) return true;
120  if (x() < rhs.x()) return true;
121  return false;
122  }
123 
125  this->set(x()+other.x(), y()+other.y(), z()+other.z());
126  return *this;
127  }
128 
129  bool operator!() const {
130  return m_v.size() != 3;
131  }
132  operator bool() const {
133  return m_v.size() == 3;
134  }
135  // can call set(x,y,z) to revalidate.
136  void invalidate() {
137  m_v.clear();
138  m_v.shrink_to_fit();
139  }
140 
141  } ;
142 
143  template< class T >
144  std::ostream & operator<< ( std::ostream & os , const D3Vector<T> & vec ) {
145  os << "(" << vec.x() << " " << vec.y() << " " << vec.z() << ")" ;
146  return os ;
147  }
148 
149  template< class T >
151  return D3Vector<T>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
152  }
153 
154  template< class T >
156  return D3Vector<T>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
157  }
158 
159  template< class T >
161  return D3Vector<T>(a.x()*s, a.y()*s, a.z()*s);
162  }
163 
164  template< class T >
166  return D3Vector<T>(a.x()/s, a.y()/s, a.z()/s);
167  }
168 
169  template< class T >
170  bool operator==(const D3Vector<T>& a,const D3Vector<T>& b){
171  return a.x() == b.x() && a.y() == b.y() && a.z() == b.z();
172  }
173 
174  template< class T >
175  bool operator!=(const D3Vector<T>& a,const D3Vector<T>& b){
176  return ! (a == b);
177  }
178 
179  template< class T >
181  return a*s;
182  }
183 
184 
185 // template< class T >
186 // std::pair< float, float >
187 // box_interesect(const D3Vector<T>& minbound, const D3Vector<T>& maxbound,
188 // const D3Vector<T>& point, const D3Vector<T>& ray)
189 
190 
191 } // namespace WireCell
192 
193 #endif
D3Vector(const D3Vector &o)
Definition: D3Vector.h:38
bool operator==(const D3Vector< T > &a, const D3Vector< T > &b)
Definition: D3Vector.h:170
D3Vector(const T d[3])
Definition: D3Vector.h:40
static const double m
Definition: Units.h:79
D3Vector(const T &a=0, const T &b=0, const T &c=0)
Construct from elements.
Definition: D3Vector.h:35
T magnitude() const
Return the magnitude of this vector.
Definition: D3Vector.h:86
T y(const T &val)
Definition: D3Vector.h:56
D3Vector< T > operator*(const D3Vector< T > a, T s)
Definition: D3Vector.h:160
D3Vector & operator+=(const D3Vector &other)
Definition: D3Vector.h:124
D3Vector & operator=(const D3Vector &o)
Definition: D3Vector.h:43
D3Vector triplevec(D3Vector &a, D3Vector &b) const
Return the triple cross product of this vector and the other two.
Definition: D3Vector.h:109
T operator[](std::size_t index) const
Access elements by copy.
Definition: D3Vector.h:70
T z() const
Definition: D3Vector.h:67
T & operator[](std::size_t index)
Access elements by reference.
Definition: D3Vector.h:75
T triplescal(D3Vector &a, D3Vector &b) const
Return the dot-cross product of this vector and the other two.
Definition: D3Vector.h:113
D3Vector norm() const
Return a normalized vector in the direction of this vector.
Definition: D3Vector.h:91
D3Vector(const D3Vector< TT > &o)
Convert from other typed vector.
Definition: D3Vector.h:62
bool operator<(const D3Vector &rhs) const
Definition: D3Vector.h:117
D3Vector< T > operator/(const D3Vector< T > a, T s)
Definition: D3Vector.h:165
T x() const
Access elements by name.
Definition: D3Vector.h:65
T x(const T &val)
Definition: D3Vector.h:55
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
T y() const
Definition: D3Vector.h:66
Definition: Main.h:22
T dot(const D3Vector &rhs) const
Return the dot product of this vector and the other.
Definition: D3Vector.h:80
std::vector< T > D3VectorStore
Definition: D3Vector.h:28
static bool * b
Definition: config.cpp:1043
bool operator!() const
Definition: D3Vector.h:129
D3Vector cross(const D3Vector &rhs) const
Return the cross product of this vector and the other.
Definition: D3Vector.h:100
bool operator!=(const D3Vector< T > &a, const D3Vector< T > &b)
Definition: D3Vector.h:175
D3VectorStore m_v
Definition: D3Vector.h:29
T z(const T &val)
Definition: D3Vector.h:57
int bool
Definition: qglobal.h:345
static QCString * s
Definition: config.cpp:1042
D3Vector< T > operator-(const D3Vector< T > a, const D3Vector< T > b)
Definition: D3Vector.h:150
D3Vector< T > operator+(const D3Vector< T > a, const D3Vector< T > b)
Definition: D3Vector.h:155