source: PSPA/Interface_Web/trunk/pspaWT/include/mathematicalTools.h @ 123

Last change on this file since 123 was 52, checked in by lemeur, 12 years ago

introduction workingarea

File size: 5.3 KB
Line 
1#ifndef MATHEMATICALTOOLS_SEEN
2#define MATHEMATICALTOOLS_SEEN
3#include <iostream>
4#include <fstream>
5#include <sstream>
6#include <vector>
7#include <cmath>
8
9
10
11using namespace std;
12
13 
14
15
16class mathTools
17{
18
19
20 public :
21  // return the index K such that :
22  // vec[K] <= xx < vec[K+1]
23  // vec is assumed to be increasingly ordered
24  // if xx is outside , return -1
25  static int locateInVector(const vector<double>& vec, double xx)
26  {
27    int n= vec.size();
28    int jl = 0;
29    int ju = n+1;
30    int jm;
31    if ( vec[0] == xx ) return 0;
32    if ( vec.back() <= xx || vec[0] > xx) return -1;
33    while ( ju - jl > 1 ) 
34      {
35        jm = (ju + jl)/2;
36        if ( vec[jm-1] <= xx ) jl = jm;
37        else ju = jm;
38      }
39    return jl - 1; 
40  }
41
42
43
44
45  // algo. Boris (Birdsall p. 356) in 2D
46  //  'tgArcMoitie' is equal to tg(theta/2), if theta is
47  // the desired rotation angle.
48  // if tg(theta/2) is given exactly, the rotation is 'exact'
49  //
50  static void borisBunemanRotation(double tgArcMoitie, double& vz, double& vx)
51  {
52    double t2 = tgArcMoitie * tgArcMoitie;
53    double sinAngle = 2.0 * tgArcMoitie / ( 1 + t2 );
54    double vauxz = vz + vx*tgArcMoitie;
55    vx = vx - vauxz*sinAngle;
56    vz = vauxz + vx*tgArcMoitie;
57  }
58
59};
60
61
62
63
64class TRIDVECTOR
65{
66  double vec_[3];
67
68  public :
69
70  TRIDVECTOR() 
71    {
72      int k;
73      for (k=0; k<3; k++) vec_[k] = 0.0;
74    }
75
76TRIDVECTOR( const TRIDVECTOR& triv) 
77{
78  setComponents( triv.vec_[0],triv.vec_[1], triv.vec_[2]); 
79}
80
81
82
83TRIDVECTOR(double x,double y,double z)
84{
85    vec_[0] = x;
86    vec_[1] = y;
87    vec_[2] = z;
88}
89
90 inline double& operator() (int index) { return vec_[index]; }
91 inline const double& operator() (int index) const { return vec_[index]; }
92
93inline void operator = (const TRIDVECTOR& triv) 
94{
95  setComponents( triv.vec_[0],triv.vec_[1], triv.vec_[2]); 
96}
97
98
99inline TRIDVECTOR& operator = (double value) 
100{
101  setComponents( value,value, value); 
102  return *this;
103}
104
105 inline void operator *= (const double& factor)
106 {
107   int k;
108   for (k=0; k < 3; k++) vec_[k] *= factor;
109   //   return *this;
110 }
111
112
113
114 inline TRIDVECTOR operator + (const TRIDVECTOR& v2)
115 {
116   //   int k;
117   return TRIDVECTOR(vec_[0] + v2.vec_[0], vec_[1] + v2.vec_[1], vec_[2] + v2.vec_[2]);
118     //   for (k=0; k < 3; k++) vec_[k] += v2.vec_[k];
119     //   return *this;
120 }
121
122 inline void operator += (const TRIDVECTOR& v2)
123 {
124   int k;
125   for (k=0; k < 3; k++) vec_[k] += v2.vec_[k];
126   //   return *this;
127 }
128
129
130 inline TRIDVECTOR operator * (const double& facteur) const
131 {
132   return TRIDVECTOR( vec_[0] * facteur,  vec_[1] * facteur,  vec_[2] * facteur);
133 }
134
135
136 inline TRIDVECTOR operator * (const TRIDVECTOR& v2)
137 {
138   double auxx, auxy, auxz;
139//   for ( k=0; k<3; k++) aux[k] = vec_[k];
140   auxx  = vec_[1] * v2.vec_[2]  - vec_[2] * v2.vec_[1];
141   auxy  = vec_[2] * v2.vec_[0]  - vec_[0] * v2.vec_[2];
142   auxz  = vec_[0] * v2.vec_[1]  - vec_[1] * v2.vec_[0];
143//   return *this;
144return TRIDVECTOR(auxx, auxy, auxz);
145 }
146
147
148
149inline double getComponent(int index ) const {return vec_[index];}
150
151 inline void setComponent(int index, double value )  {vec_[index] = value;}
152
153 inline void incrementComponent(int index, double value )  {vec_[index] += value;}
154
155
156 inline void getComponents(double& x,double& y,double& z) const
157   {
158     x = vec_[0];
159     y = vec_[1];
160     z = vec_[2];
161   }
162
163 inline void setComponents(double x, double y, double z) 
164   {
165     vec_[0] = x;
166     vec_[1] = y;
167     vec_[2] = z;
168   }
169
170
171
172  // algo. Boris (Birdsall p. 356)
173  // the input vector 'tgArcMoitie' is in the direction of the axis of
174  // rotation. Its module is equal to tg(theta/2), if theta is
175  // the desired rotation angle.
176  // if tg(theta/2) is given exactly, the rotation is 'exact'
177  //
178  void borisBunemanRotation(const TRIDVECTOR& tgArcMoitie)
179  {
180    double t2 = tgArcMoitie.norm2();
181    double fac = 2.0 / ( 1.0 + t2);
182    TRIDVECTOR sinArc = tgArcMoitie * fac;
183    TRIDVECTOR vecPrim(*this);
184    vecPrim += (*this) * tgArcMoitie;
185    (*this) += vecPrim * sinArc;
186  }
187
188
189
190
191// rotation in ZX plane
192inline void rotateZXComponentsBuneman(double tgAngleMoitie )
193{
194  mathTools::borisBunemanRotation(tgAngleMoitie, vec_[2], vec_[0]);
195}
196
197// transform the vector Er, Etheta, Ez to a vector Ex,Ey,Ez, assuming the end
198// of the vector at (x,y) in the plane (X,Y)
199  inline void fromCylindricalToCartesian(double costet, double sintet)
200  {
201     double auxr = vec_[0];
202     double auxtet = vec_[1];
203     vec_[0] = auxr * costet - auxtet * sintet;
204     vec_[1] = auxr * sintet + auxtet * costet;
205  }
206
207
208inline void clear()
209{
210     vec_[0] = 0.0;
211     vec_[1] = 0.0;
212     vec_[2] = 0.0;
213}
214
215inline double norm2() const
216{
217  return vec_[0]*vec_[0] + vec_[1]*vec_[1] + vec_[2]*vec_[2];
218}
219inline double norm() const
220{
221  return sqrt(abs(norm2()));
222}
223
224inline void renormalize()
225{
226  int k;
227  double normeInv = 1.0/norm();
228  for (k=0; k< 3 ; k++) vec_[k] *= normeInv;
229}
230
231inline void opposite()
232{
233  int k;
234  for (k=0; k< 3 ; k++) vec_[k] = -vec_[k];
235}
236
237inline void print() const
238{
239  cout << " x comp. = " << vec_[0] << " y comp. = " << vec_[1] << " z comp. = " << vec_[2] << endl;
240  cout << " norme = " << norm() << endl;
241}
242
243  string output_flow() const
244  {
245    ostringstream sortie;
246    sortie << " "  << vec_[0] << " " <<  vec_[1] << "  " << vec_[2];
247    return sortie.str();
248  }
249
250bool input_flow( ifstream& ifs)
251 {
252    bool test;
253    if ( ifs >> vec_[0] >> vec_[1] >> vec_[2]) test= true;
254    else test =  false;
255    return test;
256 }
257
258
259};
260
261
262
263
264
265
266
267#endif
Note: See TracBrowser for help on using the repository browser.