MarlinKinfit  0.4.0
TwoVector.h
1 // Class TwoVector
3 //
4 // Author: Benno List
5 // Last update: $Date: 2009/05/22 12:12:07 $
6 // by: $Author: blist $
7 //
8 // Description: class for 2-vectors
9 //
11 
12 #ifndef __TWOVECTOR_H
13 #define __TWOVECTOR_H
14 
15 #include <iostream>
16 #include <cmath>
17 
18 class TwoVector {
19  public:
20  inline TwoVector();
21  inline TwoVector(double x_, double y_);
22  // automatically generated copy constructor and assignment is fine
23 
24  inline double getX() const;
25  inline double getY() const;
26 
27  inline double getMag2() const;
28  inline double getMag() const;
29 
30  inline double getPhi() const;
31 
32  inline double getComponent (int i) const;
33 
34  inline TwoVector& setValues(double x_, double y_);
35 
36  inline TwoVector& operator+= (const TwoVector& rhs);
37  inline TwoVector& operator-= (const TwoVector& rhs);
38  inline TwoVector& operator*= (double rhs);
39 
40  private:
41  double x, y;
42 };
43 
44 TwoVector::TwoVector()
45 : x(0), y(0)
46 {}
47 
48 TwoVector::TwoVector(double x_, double y_)
49 : x(x_), y(y_)
50 {}
51 
52 double TwoVector::getX() const { return x; }
53 double TwoVector::getY() const { return y; }
54 
55 double TwoVector::getMag2() const { return x*x + y*y; }
56 double TwoVector::getMag()const { return std::sqrt(getMag2()); }
57 
58 double TwoVector::getPhi() const { return std::atan2(y, x); }
59 
60 double TwoVector::getComponent(int i) const {
61  switch (i) {
62  case 0: return getX();
63  case 1: return getY();
64  }
65  return NAN; // not-a-number, defined in cmath
66 }
67 
68 TwoVector& TwoVector::setValues(double x_, double y_) {
69  x = x_;
70  y = y_;
71  return *this;
72 }
73 
74 
75 TwoVector& TwoVector::operator+= (const TwoVector& rhs) {
76  x += rhs.x;
77  y += rhs.y;
78  return *this;
79 }
80 
81 TwoVector& TwoVector::operator-= (const TwoVector& rhs) {
82  x -= rhs.x;
83  y -= rhs.y;
84  return *this;
85 }
86 
87 TwoVector& TwoVector::operator*= (double rhs) {
88  x *= rhs;
89  y *= rhs;
90  return *this;
91 }
92 
93 inline TwoVector operator+ (const TwoVector& lhs, const TwoVector& rhs) {
94  return TwoVector (lhs.getX()+rhs.getX(), lhs.getY()+rhs.getY());
95 }
96 
97 inline TwoVector operator- (const TwoVector& lhs, const TwoVector& rhs) {
98  return TwoVector (lhs.getX()-rhs.getX(), lhs.getY()-rhs.getY());
99 }
100 
101 inline TwoVector operator- (const TwoVector& rhs) {
102  return TwoVector (-rhs.getX(), -rhs.getY());
103 }
104 
105 inline double operator* (const TwoVector& lhs, const TwoVector& rhs) {
106  return lhs.getX()*rhs.getX() + lhs.getY()*rhs.getY();
107 }
108 
109 inline TwoVector operator* (double lhs, const TwoVector& rhs) {
110  return TwoVector (lhs*rhs.getX(), lhs*rhs.getY());
111 }
112 
113 inline std::ostream& operator<< (std::ostream& out, const TwoVector& v) {
114  out << "(" << v.getX() << ", " << v.getY() << ")";
115  return out;
116 }
117 
118 
119 
120 #endif /* #ifndef __TWOVECTOR_H */
121 
122 
std::ostream & operator<<(std::ostream &os, const BaseConstraint &bc)
Prints out a BaseConstraint, using its print method.
Definition: BaseConstraint.h:128
Definition: TwoVector.h:18