Geant4  10.01.p03
UVector2.icc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UVector2.icc
12 //
13 // Implementation of inline methods of UVector2
14 //
15 // 19.10.12 Marek Gayer
16 // Created from original implementation in CLHEP
17 // --------------------------------------------------------------------
18 
19 #include <cmath>
20 
21 namespace CLHEP {
22 
23 inline double Hep2Vector::x() const {
24  return dx;
25 }
26 
27 inline double Hep2Vector::y() const {
28  return dy;
29 }
30 
31 inline Hep2Vector::Hep2Vector(double x1, double y1)
32 : dx(x1), dy(y1) {}
33 
34 inline Hep2Vector::Hep2Vector( const Hep3Vector & s)
35 : dx(s.x()), dy(s.y()) {}
36 
37 inline void Hep2Vector::setX(double x1) {
38  dx = x1;
39 }
40 
41 inline void Hep2Vector::setY(double y1) {
42  dy = y1;
43 }
44 
45 inline void Hep2Vector::set(double x1, double y1) {
46  dx = x1;
47  dy = y1;
48 }
49 
50 double & Hep2Vector::operator[] (int i) { return operator()(i); }
51 double Hep2Vector::operator[] (int i) const { return operator()(i); }
52 
53 inline Hep2Vector::Hep2Vector(const Hep2Vector & p)
54 : dx(p.x()), dy(p.y()) {}
55 
56 inline Hep2Vector::~Hep2Vector() {}
57 
58 inline Hep2Vector & Hep2Vector::operator = (const Hep2Vector & p) {
59  dx = p.x();
60  dy = p.y();
61  return *this;
62 }
63 
64 inline bool Hep2Vector::operator == (const Hep2Vector& v) const {
65  return (v.x()==x() && v.y()==y()) ? true : false;
66 }
67 
68 inline bool Hep2Vector::operator != (const Hep2Vector& v) const {
69  return (v.x()!=x() || v.y()!=y()) ? true : false;
70 }
71 
72 inline Hep2Vector& Hep2Vector::operator += (const Hep2Vector & p) {
73  dx += p.x();
74  dy += p.y();
75  return *this;
76 }
77 
78 inline Hep2Vector& Hep2Vector::operator -= (const Hep2Vector & p) {
79  dx -= p.x();
80  dy -= p.y();
81  return *this;
82 }
83 
84 inline Hep2Vector Hep2Vector::operator - () const {
85  return Hep2Vector(-dx, -dy);
86 }
87 
88 inline Hep2Vector& Hep2Vector::operator *= (double a) {
89  dx *= a;
90  dy *= a;
91  return *this;
92 }
93 
94 inline double Hep2Vector::dot(const Hep2Vector & p) const {
95  return dx*p.x() + dy*p.y();
96 }
97 
98 inline double Hep2Vector::mag2() const {
99  return dx*dx + dy*dy;
100 }
101 
102 inline double Hep2Vector::mag() const {
103  return std::sqrt(mag2());
104 }
105 
106 inline double Hep2Vector::r() const {
107  return std::sqrt(mag2());
108 }
109 
110 inline Hep2Vector Hep2Vector::unit() const {
111  double tot = mag2();
112  Hep2Vector p(*this);
113  return tot > 0.0 ? p *= (1.0/std::sqrt(tot)) : Hep2Vector(1,0);
114 }
115 
116 inline Hep2Vector Hep2Vector::orthogonal() const {
117  double x1 = std::fabs(dx), y1 = std::fabs(dy);
118  if (x1 < y1) {
119  return Hep2Vector(dy,-dx);
120  }else{
121  return Hep2Vector(-dy,dx);
122  }
123 }
124 
125 inline double Hep2Vector::phi() const {
126  return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
127 }
128 
129 inline double Hep2Vector::angle(const Hep2Vector & q) const {
130  double ptot2 = mag2()*q.mag2();
131  return ptot2 <= 0.0 ? 0.0 : std::acos(dot(q)/std::sqrt(ptot2));
132 }
133 
134 inline void Hep2Vector::setMag(double r1){
135  double ph = phi();
136  setX( r1 * std::cos(ph) );
137  setY( r1 * std::sin(ph) );
138 }
139 
140 inline void Hep2Vector::setR(double r1){
141  setMag(r1);
142 }
143 
144 inline void Hep2Vector::setPhi(double phi1){
145  double ma = mag();
146  setX( ma * std::cos(phi1) );
147  setY( ma * std::sin(phi1) );
148 }
149 
150 inline void Hep2Vector::setPolar(double r1, double phi1){
151  setX( r1 * std::cos(phi1) );
152  setY( r1 * std::sin(phi1) );
153 }
154 
155 inline Hep2Vector operator + (const Hep2Vector & a, const Hep2Vector & b) {
156  return Hep2Vector(a.x() + b.x(), a.y() + b.y());
157 }
158 
159 inline Hep2Vector operator - (const Hep2Vector & a, const Hep2Vector & b) {
160  return Hep2Vector(a.x() - b.x(), a.y() - b.y());
161 }
162 
163 inline Hep2Vector operator * (const Hep2Vector & p, double a) {
164  return Hep2Vector(a*p.x(), a*p.y());
165 }
166 
167 inline Hep2Vector operator * (double a, const Hep2Vector & p) {
168  return Hep2Vector(a*p.x(), a*p.y());
169 }
170 
171 inline double operator * (const Hep2Vector & a, const Hep2Vector & b) {
172  return a.dot(b);
173 }
174 
175 inline double Hep2Vector::getTolerance () {
176  return tolerance;
177 }
178 
179 } // namespace CLHEP
180